xfs_log_cil.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * Copyright (c) 2010 Red Hat, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write the Free Software Foundation,
  15. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "xfs.h"
  18. #include "xfs_fs.h"
  19. #include "xfs_types.h"
  20. #include "xfs_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_trans_priv.h"
  25. #include "xfs_log_priv.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_error.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_discard.h"
  32. /*
  33. * Perform initial CIL structure initialisation.
  34. */
  35. int
  36. xlog_cil_init(
  37. struct log *log)
  38. {
  39. struct xfs_cil *cil;
  40. struct xfs_cil_ctx *ctx;
  41. cil = kmem_zalloc(sizeof(*cil), KM_SLEEP|KM_MAYFAIL);
  42. if (!cil)
  43. return ENOMEM;
  44. ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP|KM_MAYFAIL);
  45. if (!ctx) {
  46. kmem_free(cil);
  47. return ENOMEM;
  48. }
  49. INIT_LIST_HEAD(&cil->xc_cil);
  50. INIT_LIST_HEAD(&cil->xc_committing);
  51. spin_lock_init(&cil->xc_cil_lock);
  52. init_rwsem(&cil->xc_ctx_lock);
  53. init_waitqueue_head(&cil->xc_commit_wait);
  54. INIT_LIST_HEAD(&ctx->committing);
  55. INIT_LIST_HEAD(&ctx->busy_extents);
  56. ctx->sequence = 1;
  57. ctx->cil = cil;
  58. cil->xc_ctx = ctx;
  59. cil->xc_current_sequence = ctx->sequence;
  60. cil->xc_log = log;
  61. log->l_cilp = cil;
  62. return 0;
  63. }
  64. void
  65. xlog_cil_destroy(
  66. struct log *log)
  67. {
  68. if (log->l_cilp->xc_ctx) {
  69. if (log->l_cilp->xc_ctx->ticket)
  70. xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket);
  71. kmem_free(log->l_cilp->xc_ctx);
  72. }
  73. ASSERT(list_empty(&log->l_cilp->xc_cil));
  74. kmem_free(log->l_cilp);
  75. }
  76. /*
  77. * Allocate a new ticket. Failing to get a new ticket makes it really hard to
  78. * recover, so we don't allow failure here. Also, we allocate in a context that
  79. * we don't want to be issuing transactions from, so we need to tell the
  80. * allocation code this as well.
  81. *
  82. * We don't reserve any space for the ticket - we are going to steal whatever
  83. * space we require from transactions as they commit. To ensure we reserve all
  84. * the space required, we need to set the current reservation of the ticket to
  85. * zero so that we know to steal the initial transaction overhead from the
  86. * first transaction commit.
  87. */
  88. static struct xlog_ticket *
  89. xlog_cil_ticket_alloc(
  90. struct log *log)
  91. {
  92. struct xlog_ticket *tic;
  93. tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0,
  94. KM_SLEEP|KM_NOFS);
  95. tic->t_trans_type = XFS_TRANS_CHECKPOINT;
  96. /*
  97. * set the current reservation to zero so we know to steal the basic
  98. * transaction overhead reservation from the first transaction commit.
  99. */
  100. tic->t_curr_res = 0;
  101. return tic;
  102. }
  103. /*
  104. * After the first stage of log recovery is done, we know where the head and
  105. * tail of the log are. We need this log initialisation done before we can
  106. * initialise the first CIL checkpoint context.
  107. *
  108. * Here we allocate a log ticket to track space usage during a CIL push. This
  109. * ticket is passed to xlog_write() directly so that we don't slowly leak log
  110. * space by failing to account for space used by log headers and additional
  111. * region headers for split regions.
  112. */
  113. void
  114. xlog_cil_init_post_recovery(
  115. struct log *log)
  116. {
  117. log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
  118. log->l_cilp->xc_ctx->sequence = 1;
  119. log->l_cilp->xc_ctx->commit_lsn = xlog_assign_lsn(log->l_curr_cycle,
  120. log->l_curr_block);
  121. }
  122. /*
  123. * Format log item into a flat buffers
  124. *
  125. * For delayed logging, we need to hold a formatted buffer containing all the
  126. * changes on the log item. This enables us to relog the item in memory and
  127. * write it out asynchronously without needing to relock the object that was
  128. * modified at the time it gets written into the iclog.
  129. *
  130. * This function builds a vector for the changes in each log item in the
  131. * transaction. It then works out the length of the buffer needed for each log
  132. * item, allocates them and formats the vector for the item into the buffer.
  133. * The buffer is then attached to the log item are then inserted into the
  134. * Committed Item List for tracking until the next checkpoint is written out.
  135. *
  136. * We don't set up region headers during this process; we simply copy the
  137. * regions into the flat buffer. We can do this because we still have to do a
  138. * formatting step to write the regions into the iclog buffer. Writing the
  139. * ophdrs during the iclog write means that we can support splitting large
  140. * regions across iclog boundares without needing a change in the format of the
  141. * item/region encapsulation.
  142. *
  143. * Hence what we need to do now is change the rewrite the vector array to point
  144. * to the copied region inside the buffer we just allocated. This allows us to
  145. * format the regions into the iclog as though they are being formatted
  146. * directly out of the objects themselves.
  147. */
  148. static struct xfs_log_vec *
  149. xlog_cil_prepare_log_vecs(
  150. struct xfs_trans *tp)
  151. {
  152. struct xfs_log_item_desc *lidp;
  153. struct xfs_log_vec *lv = NULL;
  154. struct xfs_log_vec *ret_lv = NULL;
  155. /* Bail out if we didn't find a log item. */
  156. if (list_empty(&tp->t_items)) {
  157. ASSERT(0);
  158. return NULL;
  159. }
  160. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  161. struct xfs_log_vec *new_lv;
  162. void *ptr;
  163. int index;
  164. int len = 0;
  165. uint niovecs;
  166. /* Skip items which aren't dirty in this transaction. */
  167. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  168. continue;
  169. /* Skip items that do not have any vectors for writing */
  170. niovecs = IOP_SIZE(lidp->lid_item);
  171. if (!niovecs)
  172. continue;
  173. new_lv = kmem_zalloc(sizeof(*new_lv) +
  174. niovecs * sizeof(struct xfs_log_iovec),
  175. KM_SLEEP);
  176. /* The allocated iovec region lies beyond the log vector. */
  177. new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
  178. new_lv->lv_niovecs = niovecs;
  179. new_lv->lv_item = lidp->lid_item;
  180. /* build the vector array and calculate it's length */
  181. IOP_FORMAT(new_lv->lv_item, new_lv->lv_iovecp);
  182. for (index = 0; index < new_lv->lv_niovecs; index++)
  183. len += new_lv->lv_iovecp[index].i_len;
  184. new_lv->lv_buf_len = len;
  185. new_lv->lv_buf = kmem_alloc(new_lv->lv_buf_len,
  186. KM_SLEEP|KM_NOFS);
  187. ptr = new_lv->lv_buf;
  188. for (index = 0; index < new_lv->lv_niovecs; index++) {
  189. struct xfs_log_iovec *vec = &new_lv->lv_iovecp[index];
  190. memcpy(ptr, vec->i_addr, vec->i_len);
  191. vec->i_addr = ptr;
  192. ptr += vec->i_len;
  193. }
  194. ASSERT(ptr == new_lv->lv_buf + new_lv->lv_buf_len);
  195. if (!ret_lv)
  196. ret_lv = new_lv;
  197. else
  198. lv->lv_next = new_lv;
  199. lv = new_lv;
  200. }
  201. return ret_lv;
  202. }
  203. /*
  204. * Prepare the log item for insertion into the CIL. Calculate the difference in
  205. * log space and vectors it will consume, and if it is a new item pin it as
  206. * well.
  207. */
  208. STATIC void
  209. xfs_cil_prepare_item(
  210. struct log *log,
  211. struct xfs_log_vec *lv,
  212. int *len,
  213. int *diff_iovecs)
  214. {
  215. struct xfs_log_vec *old = lv->lv_item->li_lv;
  216. if (old) {
  217. /* existing lv on log item, space used is a delta */
  218. ASSERT(!list_empty(&lv->lv_item->li_cil));
  219. ASSERT(old->lv_buf && old->lv_buf_len && old->lv_niovecs);
  220. *len += lv->lv_buf_len - old->lv_buf_len;
  221. *diff_iovecs += lv->lv_niovecs - old->lv_niovecs;
  222. kmem_free(old->lv_buf);
  223. kmem_free(old);
  224. } else {
  225. /* new lv, must pin the log item */
  226. ASSERT(!lv->lv_item->li_lv);
  227. ASSERT(list_empty(&lv->lv_item->li_cil));
  228. *len += lv->lv_buf_len;
  229. *diff_iovecs += lv->lv_niovecs;
  230. IOP_PIN(lv->lv_item);
  231. }
  232. /* attach new log vector to log item */
  233. lv->lv_item->li_lv = lv;
  234. /*
  235. * If this is the first time the item is being committed to the
  236. * CIL, store the sequence number on the log item so we can
  237. * tell in future commits whether this is the first checkpoint
  238. * the item is being committed into.
  239. */
  240. if (!lv->lv_item->li_seq)
  241. lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
  242. }
  243. /*
  244. * Insert the log items into the CIL and calculate the difference in space
  245. * consumed by the item. Add the space to the checkpoint ticket and calculate
  246. * if the change requires additional log metadata. If it does, take that space
  247. * as well. Remove the amount of space we added to the checkpoint ticket from
  248. * the current transaction ticket so that the accounting works out correctly.
  249. */
  250. static void
  251. xlog_cil_insert_items(
  252. struct log *log,
  253. struct xfs_log_vec *log_vector,
  254. struct xlog_ticket *ticket)
  255. {
  256. struct xfs_cil *cil = log->l_cilp;
  257. struct xfs_cil_ctx *ctx = cil->xc_ctx;
  258. struct xfs_log_vec *lv;
  259. int len = 0;
  260. int diff_iovecs = 0;
  261. int iclog_space;
  262. ASSERT(log_vector);
  263. /*
  264. * Do all the accounting aggregation and switching of log vectors
  265. * around in a separate loop to the insertion of items into the CIL.
  266. * Then we can do a separate loop to update the CIL within a single
  267. * lock/unlock pair. This reduces the number of round trips on the CIL
  268. * lock from O(nr_logvectors) to O(1) and greatly reduces the overall
  269. * hold time for the transaction commit.
  270. *
  271. * If this is the first time the item is being placed into the CIL in
  272. * this context, pin it so it can't be written to disk until the CIL is
  273. * flushed to the iclog and the iclog written to disk.
  274. *
  275. * We can do this safely because the context can't checkpoint until we
  276. * are done so it doesn't matter exactly how we update the CIL.
  277. */
  278. for (lv = log_vector; lv; lv = lv->lv_next)
  279. xfs_cil_prepare_item(log, lv, &len, &diff_iovecs);
  280. /* account for space used by new iovec headers */
  281. len += diff_iovecs * sizeof(xlog_op_header_t);
  282. spin_lock(&cil->xc_cil_lock);
  283. /* move the items to the tail of the CIL */
  284. for (lv = log_vector; lv; lv = lv->lv_next)
  285. list_move_tail(&lv->lv_item->li_cil, &cil->xc_cil);
  286. ctx->nvecs += diff_iovecs;
  287. /*
  288. * Now transfer enough transaction reservation to the context ticket
  289. * for the checkpoint. The context ticket is special - the unit
  290. * reservation has to grow as well as the current reservation as we
  291. * steal from tickets so we can correctly determine the space used
  292. * during the transaction commit.
  293. */
  294. if (ctx->ticket->t_curr_res == 0) {
  295. /* first commit in checkpoint, steal the header reservation */
  296. ASSERT(ticket->t_curr_res >= ctx->ticket->t_unit_res + len);
  297. ctx->ticket->t_curr_res = ctx->ticket->t_unit_res;
  298. ticket->t_curr_res -= ctx->ticket->t_unit_res;
  299. }
  300. /* do we need space for more log record headers? */
  301. iclog_space = log->l_iclog_size - log->l_iclog_hsize;
  302. if (len > 0 && (ctx->space_used / iclog_space !=
  303. (ctx->space_used + len) / iclog_space)) {
  304. int hdrs;
  305. hdrs = (len + iclog_space - 1) / iclog_space;
  306. /* need to take into account split region headers, too */
  307. hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
  308. ctx->ticket->t_unit_res += hdrs;
  309. ctx->ticket->t_curr_res += hdrs;
  310. ticket->t_curr_res -= hdrs;
  311. ASSERT(ticket->t_curr_res >= len);
  312. }
  313. ticket->t_curr_res -= len;
  314. ctx->space_used += len;
  315. spin_unlock(&cil->xc_cil_lock);
  316. }
  317. static void
  318. xlog_cil_free_logvec(
  319. struct xfs_log_vec *log_vector)
  320. {
  321. struct xfs_log_vec *lv;
  322. for (lv = log_vector; lv; ) {
  323. struct xfs_log_vec *next = lv->lv_next;
  324. kmem_free(lv->lv_buf);
  325. kmem_free(lv);
  326. lv = next;
  327. }
  328. }
  329. /*
  330. * Mark all items committed and clear busy extents. We free the log vector
  331. * chains in a separate pass so that we unpin the log items as quickly as
  332. * possible.
  333. */
  334. static void
  335. xlog_cil_committed(
  336. void *args,
  337. int abort)
  338. {
  339. struct xfs_cil_ctx *ctx = args;
  340. struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
  341. xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
  342. ctx->start_lsn, abort);
  343. xfs_alloc_busy_sort(&ctx->busy_extents);
  344. xfs_alloc_busy_clear(mp, &ctx->busy_extents,
  345. (mp->m_flags & XFS_MOUNT_DISCARD) && !abort);
  346. spin_lock(&ctx->cil->xc_cil_lock);
  347. list_del(&ctx->committing);
  348. spin_unlock(&ctx->cil->xc_cil_lock);
  349. xlog_cil_free_logvec(ctx->lv_chain);
  350. if (!list_empty(&ctx->busy_extents)) {
  351. ASSERT(mp->m_flags & XFS_MOUNT_DISCARD);
  352. xfs_discard_extents(mp, &ctx->busy_extents);
  353. xfs_alloc_busy_clear(mp, &ctx->busy_extents, false);
  354. }
  355. kmem_free(ctx);
  356. }
  357. /*
  358. * Push the Committed Item List to the log. If @push_seq flag is zero, then it
  359. * is a background flush and so we can chose to ignore it. Otherwise, if the
  360. * current sequence is the same as @push_seq we need to do a flush. If
  361. * @push_seq is less than the current sequence, then it has already been
  362. * flushed and we don't need to do anything - the caller will wait for it to
  363. * complete if necessary.
  364. *
  365. * @push_seq is a value rather than a flag because that allows us to do an
  366. * unlocked check of the sequence number for a match. Hence we can allows log
  367. * forces to run racily and not issue pushes for the same sequence twice. If we
  368. * get a race between multiple pushes for the same sequence they will block on
  369. * the first one and then abort, hence avoiding needless pushes.
  370. */
  371. STATIC int
  372. xlog_cil_push(
  373. struct log *log,
  374. xfs_lsn_t push_seq)
  375. {
  376. struct xfs_cil *cil = log->l_cilp;
  377. struct xfs_log_vec *lv;
  378. struct xfs_cil_ctx *ctx;
  379. struct xfs_cil_ctx *new_ctx;
  380. struct xlog_in_core *commit_iclog;
  381. struct xlog_ticket *tic;
  382. int num_lv;
  383. int num_iovecs;
  384. int len;
  385. int error = 0;
  386. struct xfs_trans_header thdr;
  387. struct xfs_log_iovec lhdr;
  388. struct xfs_log_vec lvhdr = { NULL };
  389. xfs_lsn_t commit_lsn;
  390. if (!cil)
  391. return 0;
  392. ASSERT(!push_seq || push_seq <= cil->xc_ctx->sequence);
  393. new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
  394. new_ctx->ticket = xlog_cil_ticket_alloc(log);
  395. /*
  396. * Lock out transaction commit, but don't block for background pushes
  397. * unless we are well over the CIL space limit. See the definition of
  398. * XLOG_CIL_HARD_SPACE_LIMIT() for the full explanation of the logic
  399. * used here.
  400. */
  401. if (!down_write_trylock(&cil->xc_ctx_lock)) {
  402. if (!push_seq &&
  403. cil->xc_ctx->space_used < XLOG_CIL_HARD_SPACE_LIMIT(log))
  404. goto out_free_ticket;
  405. down_write(&cil->xc_ctx_lock);
  406. }
  407. ctx = cil->xc_ctx;
  408. /* check if we've anything to push */
  409. if (list_empty(&cil->xc_cil))
  410. goto out_skip;
  411. /* check for spurious background flush */
  412. if (!push_seq && cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log))
  413. goto out_skip;
  414. /* check for a previously pushed seqeunce */
  415. if (push_seq && push_seq < cil->xc_ctx->sequence)
  416. goto out_skip;
  417. /*
  418. * pull all the log vectors off the items in the CIL, and
  419. * remove the items from the CIL. We don't need the CIL lock
  420. * here because it's only needed on the transaction commit
  421. * side which is currently locked out by the flush lock.
  422. */
  423. lv = NULL;
  424. num_lv = 0;
  425. num_iovecs = 0;
  426. len = 0;
  427. while (!list_empty(&cil->xc_cil)) {
  428. struct xfs_log_item *item;
  429. int i;
  430. item = list_first_entry(&cil->xc_cil,
  431. struct xfs_log_item, li_cil);
  432. list_del_init(&item->li_cil);
  433. if (!ctx->lv_chain)
  434. ctx->lv_chain = item->li_lv;
  435. else
  436. lv->lv_next = item->li_lv;
  437. lv = item->li_lv;
  438. item->li_lv = NULL;
  439. num_lv++;
  440. num_iovecs += lv->lv_niovecs;
  441. for (i = 0; i < lv->lv_niovecs; i++)
  442. len += lv->lv_iovecp[i].i_len;
  443. }
  444. /*
  445. * initialise the new context and attach it to the CIL. Then attach
  446. * the current context to the CIL committing lsit so it can be found
  447. * during log forces to extract the commit lsn of the sequence that
  448. * needs to be forced.
  449. */
  450. INIT_LIST_HEAD(&new_ctx->committing);
  451. INIT_LIST_HEAD(&new_ctx->busy_extents);
  452. new_ctx->sequence = ctx->sequence + 1;
  453. new_ctx->cil = cil;
  454. cil->xc_ctx = new_ctx;
  455. /*
  456. * mirror the new sequence into the cil structure so that we can do
  457. * unlocked checks against the current sequence in log forces without
  458. * risking deferencing a freed context pointer.
  459. */
  460. cil->xc_current_sequence = new_ctx->sequence;
  461. /*
  462. * The switch is now done, so we can drop the context lock and move out
  463. * of a shared context. We can't just go straight to the commit record,
  464. * though - we need to synchronise with previous and future commits so
  465. * that the commit records are correctly ordered in the log to ensure
  466. * that we process items during log IO completion in the correct order.
  467. *
  468. * For example, if we get an EFI in one checkpoint and the EFD in the
  469. * next (e.g. due to log forces), we do not want the checkpoint with
  470. * the EFD to be committed before the checkpoint with the EFI. Hence
  471. * we must strictly order the commit records of the checkpoints so
  472. * that: a) the checkpoint callbacks are attached to the iclogs in the
  473. * correct order; and b) the checkpoints are replayed in correct order
  474. * in log recovery.
  475. *
  476. * Hence we need to add this context to the committing context list so
  477. * that higher sequences will wait for us to write out a commit record
  478. * before they do.
  479. */
  480. spin_lock(&cil->xc_cil_lock);
  481. list_add(&ctx->committing, &cil->xc_committing);
  482. spin_unlock(&cil->xc_cil_lock);
  483. up_write(&cil->xc_ctx_lock);
  484. /*
  485. * Build a checkpoint transaction header and write it to the log to
  486. * begin the transaction. We need to account for the space used by the
  487. * transaction header here as it is not accounted for in xlog_write().
  488. *
  489. * The LSN we need to pass to the log items on transaction commit is
  490. * the LSN reported by the first log vector write. If we use the commit
  491. * record lsn then we can move the tail beyond the grant write head.
  492. */
  493. tic = ctx->ticket;
  494. thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
  495. thdr.th_type = XFS_TRANS_CHECKPOINT;
  496. thdr.th_tid = tic->t_tid;
  497. thdr.th_num_items = num_iovecs;
  498. lhdr.i_addr = &thdr;
  499. lhdr.i_len = sizeof(xfs_trans_header_t);
  500. lhdr.i_type = XLOG_REG_TYPE_TRANSHDR;
  501. tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t);
  502. lvhdr.lv_niovecs = 1;
  503. lvhdr.lv_iovecp = &lhdr;
  504. lvhdr.lv_next = ctx->lv_chain;
  505. error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0);
  506. if (error)
  507. goto out_abort_free_ticket;
  508. /*
  509. * now that we've written the checkpoint into the log, strictly
  510. * order the commit records so replay will get them in the right order.
  511. */
  512. restart:
  513. spin_lock(&cil->xc_cil_lock);
  514. list_for_each_entry(new_ctx, &cil->xc_committing, committing) {
  515. /*
  516. * Higher sequences will wait for this one so skip them.
  517. * Don't wait for own own sequence, either.
  518. */
  519. if (new_ctx->sequence >= ctx->sequence)
  520. continue;
  521. if (!new_ctx->commit_lsn) {
  522. /*
  523. * It is still being pushed! Wait for the push to
  524. * complete, then start again from the beginning.
  525. */
  526. xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
  527. goto restart;
  528. }
  529. }
  530. spin_unlock(&cil->xc_cil_lock);
  531. /* xfs_log_done always frees the ticket on error. */
  532. commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0);
  533. if (commit_lsn == -1)
  534. goto out_abort;
  535. /* attach all the transactions w/ busy extents to iclog */
  536. ctx->log_cb.cb_func = xlog_cil_committed;
  537. ctx->log_cb.cb_arg = ctx;
  538. error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb);
  539. if (error)
  540. goto out_abort;
  541. /*
  542. * now the checkpoint commit is complete and we've attached the
  543. * callbacks to the iclog we can assign the commit LSN to the context
  544. * and wake up anyone who is waiting for the commit to complete.
  545. */
  546. spin_lock(&cil->xc_cil_lock);
  547. ctx->commit_lsn = commit_lsn;
  548. wake_up_all(&cil->xc_commit_wait);
  549. spin_unlock(&cil->xc_cil_lock);
  550. /* release the hounds! */
  551. return xfs_log_release_iclog(log->l_mp, commit_iclog);
  552. out_skip:
  553. up_write(&cil->xc_ctx_lock);
  554. out_free_ticket:
  555. xfs_log_ticket_put(new_ctx->ticket);
  556. kmem_free(new_ctx);
  557. return 0;
  558. out_abort_free_ticket:
  559. xfs_log_ticket_put(tic);
  560. out_abort:
  561. xlog_cil_committed(ctx, XFS_LI_ABORTED);
  562. return XFS_ERROR(EIO);
  563. }
  564. /*
  565. * Commit a transaction with the given vector to the Committed Item List.
  566. *
  567. * To do this, we need to format the item, pin it in memory if required and
  568. * account for the space used by the transaction. Once we have done that we
  569. * need to release the unused reservation for the transaction, attach the
  570. * transaction to the checkpoint context so we carry the busy extents through
  571. * to checkpoint completion, and then unlock all the items in the transaction.
  572. *
  573. * For more specific information about the order of operations in
  574. * xfs_log_commit_cil() please refer to the comments in
  575. * xfs_trans_commit_iclog().
  576. *
  577. * Called with the context lock already held in read mode to lock out
  578. * background commit, returns without it held once background commits are
  579. * allowed again.
  580. */
  581. int
  582. xfs_log_commit_cil(
  583. struct xfs_mount *mp,
  584. struct xfs_trans *tp,
  585. xfs_lsn_t *commit_lsn,
  586. int flags)
  587. {
  588. struct log *log = mp->m_log;
  589. int log_flags = 0;
  590. int push = 0;
  591. struct xfs_log_vec *log_vector;
  592. if (flags & XFS_TRANS_RELEASE_LOG_RES)
  593. log_flags = XFS_LOG_REL_PERM_RESERV;
  594. /*
  595. * Do all the hard work of formatting items (including memory
  596. * allocation) outside the CIL context lock. This prevents stalling CIL
  597. * pushes when we are low on memory and a transaction commit spends a
  598. * lot of time in memory reclaim.
  599. */
  600. log_vector = xlog_cil_prepare_log_vecs(tp);
  601. if (!log_vector)
  602. return ENOMEM;
  603. /* lock out background commit */
  604. down_read(&log->l_cilp->xc_ctx_lock);
  605. if (commit_lsn)
  606. *commit_lsn = log->l_cilp->xc_ctx->sequence;
  607. xlog_cil_insert_items(log, log_vector, tp->t_ticket);
  608. /* check we didn't blow the reservation */
  609. if (tp->t_ticket->t_curr_res < 0)
  610. xlog_print_tic_res(log->l_mp, tp->t_ticket);
  611. /* attach the transaction to the CIL if it has any busy extents */
  612. if (!list_empty(&tp->t_busy)) {
  613. spin_lock(&log->l_cilp->xc_cil_lock);
  614. list_splice_init(&tp->t_busy,
  615. &log->l_cilp->xc_ctx->busy_extents);
  616. spin_unlock(&log->l_cilp->xc_cil_lock);
  617. }
  618. tp->t_commit_lsn = *commit_lsn;
  619. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  620. xfs_trans_unreserve_and_mod_sb(tp);
  621. /*
  622. * Once all the items of the transaction have been copied to the CIL,
  623. * the items can be unlocked and freed.
  624. *
  625. * This needs to be done before we drop the CIL context lock because we
  626. * have to update state in the log items and unlock them before they go
  627. * to disk. If we don't, then the CIL checkpoint can race with us and
  628. * we can run checkpoint completion before we've updated and unlocked
  629. * the log items. This affects (at least) processing of stale buffers,
  630. * inodes and EFIs.
  631. */
  632. xfs_trans_free_items(tp, *commit_lsn, 0);
  633. /* check for background commit before unlock */
  634. if (log->l_cilp->xc_ctx->space_used > XLOG_CIL_SPACE_LIMIT(log))
  635. push = 1;
  636. up_read(&log->l_cilp->xc_ctx_lock);
  637. /*
  638. * We need to push CIL every so often so we don't cache more than we
  639. * can fit in the log. The limit really is that a checkpoint can't be
  640. * more than half the log (the current checkpoint is not allowed to
  641. * overwrite the previous checkpoint), but commit latency and memory
  642. * usage limit this to a smaller size in most cases.
  643. */
  644. if (push)
  645. xlog_cil_push(log, 0);
  646. return 0;
  647. }
  648. /*
  649. * Conditionally push the CIL based on the sequence passed in.
  650. *
  651. * We only need to push if we haven't already pushed the sequence
  652. * number given. Hence the only time we will trigger a push here is
  653. * if the push sequence is the same as the current context.
  654. *
  655. * We return the current commit lsn to allow the callers to determine if a
  656. * iclog flush is necessary following this call.
  657. *
  658. * XXX: Initially, just push the CIL unconditionally and return whatever
  659. * commit lsn is there. It'll be empty, so this is broken for now.
  660. */
  661. xfs_lsn_t
  662. xlog_cil_force_lsn(
  663. struct log *log,
  664. xfs_lsn_t sequence)
  665. {
  666. struct xfs_cil *cil = log->l_cilp;
  667. struct xfs_cil_ctx *ctx;
  668. xfs_lsn_t commit_lsn = NULLCOMMITLSN;
  669. ASSERT(sequence <= cil->xc_current_sequence);
  670. /*
  671. * check to see if we need to force out the current context.
  672. * xlog_cil_push() handles racing pushes for the same sequence,
  673. * so no need to deal with it here.
  674. */
  675. if (sequence == cil->xc_current_sequence)
  676. xlog_cil_push(log, sequence);
  677. /*
  678. * See if we can find a previous sequence still committing.
  679. * We need to wait for all previous sequence commits to complete
  680. * before allowing the force of push_seq to go ahead. Hence block
  681. * on commits for those as well.
  682. */
  683. restart:
  684. spin_lock(&cil->xc_cil_lock);
  685. list_for_each_entry(ctx, &cil->xc_committing, committing) {
  686. if (ctx->sequence > sequence)
  687. continue;
  688. if (!ctx->commit_lsn) {
  689. /*
  690. * It is still being pushed! Wait for the push to
  691. * complete, then start again from the beginning.
  692. */
  693. xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
  694. goto restart;
  695. }
  696. if (ctx->sequence != sequence)
  697. continue;
  698. /* found it! */
  699. commit_lsn = ctx->commit_lsn;
  700. }
  701. spin_unlock(&cil->xc_cil_lock);
  702. return commit_lsn;
  703. }
  704. /*
  705. * Check if the current log item was first committed in this sequence.
  706. * We can't rely on just the log item being in the CIL, we have to check
  707. * the recorded commit sequence number.
  708. *
  709. * Note: for this to be used in a non-racy manner, it has to be called with
  710. * CIL flushing locked out. As a result, it should only be used during the
  711. * transaction commit process when deciding what to format into the item.
  712. */
  713. bool
  714. xfs_log_item_in_current_chkpt(
  715. struct xfs_log_item *lip)
  716. {
  717. struct xfs_cil_ctx *ctx;
  718. if (list_empty(&lip->li_cil))
  719. return false;
  720. ctx = lip->li_mountp->m_log->l_cilp->xc_ctx;
  721. /*
  722. * li_seq is written on the first commit of a log item to record the
  723. * first checkpoint it is written to. Hence if it is different to the
  724. * current sequence, we're in a new checkpoint.
  725. */
  726. if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0)
  727. return false;
  728. return true;
  729. }