xfs_log_cil.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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_log_format.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_trans_resv.h"
  22. #include "xfs_sb.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_error.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_extent_busy.h"
  28. #include "xfs_discard.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_trans_priv.h"
  31. #include "xfs_log.h"
  32. #include "xfs_log_priv.h"
  33. /*
  34. * Allocate a new ticket. Failing to get a new ticket makes it really hard to
  35. * recover, so we don't allow failure here. Also, we allocate in a context that
  36. * we don't want to be issuing transactions from, so we need to tell the
  37. * allocation code this as well.
  38. *
  39. * We don't reserve any space for the ticket - we are going to steal whatever
  40. * space we require from transactions as they commit. To ensure we reserve all
  41. * the space required, we need to set the current reservation of the ticket to
  42. * zero so that we know to steal the initial transaction overhead from the
  43. * first transaction commit.
  44. */
  45. static struct xlog_ticket *
  46. xlog_cil_ticket_alloc(
  47. struct xlog *log)
  48. {
  49. struct xlog_ticket *tic;
  50. tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0,
  51. KM_SLEEP|KM_NOFS);
  52. tic->t_trans_type = XFS_TRANS_CHECKPOINT;
  53. /*
  54. * set the current reservation to zero so we know to steal the basic
  55. * transaction overhead reservation from the first transaction commit.
  56. */
  57. tic->t_curr_res = 0;
  58. return tic;
  59. }
  60. /*
  61. * After the first stage of log recovery is done, we know where the head and
  62. * tail of the log are. We need this log initialisation done before we can
  63. * initialise the first CIL checkpoint context.
  64. *
  65. * Here we allocate a log ticket to track space usage during a CIL push. This
  66. * ticket is passed to xlog_write() directly so that we don't slowly leak log
  67. * space by failing to account for space used by log headers and additional
  68. * region headers for split regions.
  69. */
  70. void
  71. xlog_cil_init_post_recovery(
  72. struct xlog *log)
  73. {
  74. log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
  75. log->l_cilp->xc_ctx->sequence = 1;
  76. }
  77. /*
  78. * Prepare the log item for insertion into the CIL. Calculate the difference in
  79. * log space and vectors it will consume, and if it is a new item pin it as
  80. * well.
  81. */
  82. STATIC void
  83. xfs_cil_prepare_item(
  84. struct xlog *log,
  85. struct xfs_log_vec *lv,
  86. struct xfs_log_vec *old_lv,
  87. int *diff_len,
  88. int *diff_iovecs)
  89. {
  90. /* Account for the new LV being passed in */
  91. if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED) {
  92. *diff_len += lv->lv_bytes;
  93. *diff_iovecs += lv->lv_niovecs;
  94. }
  95. /*
  96. * If there is no old LV, this is the first time we've seen the item in
  97. * this CIL context and so we need to pin it. If we are replacing the
  98. * old_lv, then remove the space it accounts for and free it.
  99. */
  100. if (!old_lv)
  101. lv->lv_item->li_ops->iop_pin(lv->lv_item);
  102. else if (old_lv != lv) {
  103. ASSERT(lv->lv_buf_len != XFS_LOG_VEC_ORDERED);
  104. *diff_len -= old_lv->lv_bytes;
  105. *diff_iovecs -= old_lv->lv_niovecs;
  106. kmem_free(old_lv);
  107. }
  108. /* attach new log vector to log item */
  109. lv->lv_item->li_lv = lv;
  110. /*
  111. * If this is the first time the item is being committed to the
  112. * CIL, store the sequence number on the log item so we can
  113. * tell in future commits whether this is the first checkpoint
  114. * the item is being committed into.
  115. */
  116. if (!lv->lv_item->li_seq)
  117. lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
  118. }
  119. /*
  120. * Format log item into a flat buffers
  121. *
  122. * For delayed logging, we need to hold a formatted buffer containing all the
  123. * changes on the log item. This enables us to relog the item in memory and
  124. * write it out asynchronously without needing to relock the object that was
  125. * modified at the time it gets written into the iclog.
  126. *
  127. * This function builds a vector for the changes in each log item in the
  128. * transaction. It then works out the length of the buffer needed for each log
  129. * item, allocates them and formats the vector for the item into the buffer.
  130. * The buffer is then attached to the log item are then inserted into the
  131. * Committed Item List for tracking until the next checkpoint is written out.
  132. *
  133. * We don't set up region headers during this process; we simply copy the
  134. * regions into the flat buffer. We can do this because we still have to do a
  135. * formatting step to write the regions into the iclog buffer. Writing the
  136. * ophdrs during the iclog write means that we can support splitting large
  137. * regions across iclog boundares without needing a change in the format of the
  138. * item/region encapsulation.
  139. *
  140. * Hence what we need to do now is change the rewrite the vector array to point
  141. * to the copied region inside the buffer we just allocated. This allows us to
  142. * format the regions into the iclog as though they are being formatted
  143. * directly out of the objects themselves.
  144. */
  145. static void
  146. xlog_cil_insert_format_items(
  147. struct xlog *log,
  148. struct xfs_trans *tp,
  149. int *diff_len,
  150. int *diff_iovecs)
  151. {
  152. struct xfs_log_item_desc *lidp;
  153. /* Bail out if we didn't find a log item. */
  154. if (list_empty(&tp->t_items)) {
  155. ASSERT(0);
  156. return;
  157. }
  158. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  159. struct xfs_log_item *lip = lidp->lid_item;
  160. struct xfs_log_vec *lv;
  161. struct xfs_log_vec *old_lv;
  162. int niovecs = 0;
  163. int nbytes = 0;
  164. int buf_size;
  165. bool ordered = false;
  166. /* Skip items which aren't dirty in this transaction. */
  167. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  168. continue;
  169. /* get number of vecs and size of data to be stored */
  170. lip->li_ops->iop_size(lip, &niovecs, &nbytes);
  171. /* Skip items that do not have any vectors for writing */
  172. if (!niovecs)
  173. continue;
  174. /*
  175. * Ordered items need to be tracked but we do not wish to write
  176. * them. We need a logvec to track the object, but we do not
  177. * need an iovec or buffer to be allocated for copying data.
  178. */
  179. if (niovecs == XFS_LOG_VEC_ORDERED) {
  180. ordered = true;
  181. niovecs = 0;
  182. nbytes = 0;
  183. }
  184. /*
  185. * We 64-bit align the length of each iovec so that the start
  186. * of the next one is naturally aligned. We'll need to
  187. * account for that slack space here. Then round nbytes up
  188. * to 64-bit alignment so that the initial buffer alignment is
  189. * easy to calculate and verify.
  190. */
  191. nbytes += niovecs * sizeof(uint64_t);
  192. nbytes = round_up(nbytes, sizeof(uint64_t));
  193. /* grab the old item if it exists for reservation accounting */
  194. old_lv = lip->li_lv;
  195. /*
  196. * The data buffer needs to start 64-bit aligned, so round up
  197. * that space to ensure we can align it appropriately and not
  198. * overrun the buffer.
  199. */
  200. buf_size = nbytes +
  201. round_up((sizeof(struct xfs_log_vec) +
  202. niovecs * sizeof(struct xfs_log_iovec)),
  203. sizeof(uint64_t));
  204. /* compare to existing item size */
  205. if (lip->li_lv && buf_size <= lip->li_lv->lv_size) {
  206. /* same or smaller, optimise common overwrite case */
  207. lv = lip->li_lv;
  208. lv->lv_next = NULL;
  209. if (ordered)
  210. goto insert;
  211. /*
  212. * set the item up as though it is a new insertion so
  213. * that the space reservation accounting is correct.
  214. */
  215. *diff_iovecs -= lv->lv_niovecs;
  216. *diff_len -= lv->lv_bytes;
  217. } else {
  218. /* allocate new data chunk */
  219. lv = kmem_zalloc(buf_size, KM_SLEEP|KM_NOFS);
  220. lv->lv_item = lip;
  221. lv->lv_size = buf_size;
  222. if (ordered) {
  223. /* track as an ordered logvec */
  224. ASSERT(lip->li_lv == NULL);
  225. lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
  226. goto insert;
  227. }
  228. lv->lv_iovecp = (struct xfs_log_iovec *)&lv[1];
  229. }
  230. /* Ensure the lv is set up according to ->iop_size */
  231. lv->lv_niovecs = niovecs;
  232. /* The allocated data region lies beyond the iovec region */
  233. lv->lv_buf_len = 0;
  234. lv->lv_bytes = 0;
  235. lv->lv_buf = (char *)lv + buf_size - nbytes;
  236. ASSERT(IS_ALIGNED((unsigned long)lv->lv_buf, sizeof(uint64_t)));
  237. lip->li_ops->iop_format(lip, lv);
  238. insert:
  239. ASSERT(lv->lv_buf_len <= nbytes);
  240. xfs_cil_prepare_item(log, lv, old_lv, diff_len, diff_iovecs);
  241. }
  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 xlog *log,
  253. struct xfs_trans *tp)
  254. {
  255. struct xfs_cil *cil = log->l_cilp;
  256. struct xfs_cil_ctx *ctx = cil->xc_ctx;
  257. struct xfs_log_item_desc *lidp;
  258. int len = 0;
  259. int diff_iovecs = 0;
  260. int iclog_space;
  261. ASSERT(tp);
  262. /*
  263. * We can do this safely because the context can't checkpoint until we
  264. * are done so it doesn't matter exactly how we update the CIL.
  265. */
  266. xlog_cil_insert_format_items(log, tp, &len, &diff_iovecs);
  267. /*
  268. * Now (re-)position everything modified at the tail of the CIL.
  269. * We do this here so we only need to take the CIL lock once during
  270. * the transaction commit.
  271. */
  272. spin_lock(&cil->xc_cil_lock);
  273. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  274. struct xfs_log_item *lip = lidp->lid_item;
  275. /* Skip items which aren't dirty in this transaction. */
  276. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  277. continue;
  278. list_move_tail(&lip->li_cil, &cil->xc_cil);
  279. }
  280. /* account for space used by new iovec headers */
  281. len += diff_iovecs * sizeof(xlog_op_header_t);
  282. ctx->nvecs += diff_iovecs;
  283. /* attach the transaction to the CIL if it has any busy extents */
  284. if (!list_empty(&tp->t_busy))
  285. list_splice_init(&tp->t_busy, &ctx->busy_extents);
  286. /*
  287. * Now transfer enough transaction reservation to the context ticket
  288. * for the checkpoint. The context ticket is special - the unit
  289. * reservation has to grow as well as the current reservation as we
  290. * steal from tickets so we can correctly determine the space used
  291. * during the transaction commit.
  292. */
  293. if (ctx->ticket->t_curr_res == 0) {
  294. ctx->ticket->t_curr_res = ctx->ticket->t_unit_res;
  295. tp->t_ticket->t_curr_res -= ctx->ticket->t_unit_res;
  296. }
  297. /* do we need space for more log record headers? */
  298. iclog_space = log->l_iclog_size - log->l_iclog_hsize;
  299. if (len > 0 && (ctx->space_used / iclog_space !=
  300. (ctx->space_used + len) / iclog_space)) {
  301. int hdrs;
  302. hdrs = (len + iclog_space - 1) / iclog_space;
  303. /* need to take into account split region headers, too */
  304. hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
  305. ctx->ticket->t_unit_res += hdrs;
  306. ctx->ticket->t_curr_res += hdrs;
  307. tp->t_ticket->t_curr_res -= hdrs;
  308. ASSERT(tp->t_ticket->t_curr_res >= len);
  309. }
  310. tp->t_ticket->t_curr_res -= len;
  311. ctx->space_used += len;
  312. spin_unlock(&cil->xc_cil_lock);
  313. }
  314. static void
  315. xlog_cil_free_logvec(
  316. struct xfs_log_vec *log_vector)
  317. {
  318. struct xfs_log_vec *lv;
  319. for (lv = log_vector; lv; ) {
  320. struct xfs_log_vec *next = lv->lv_next;
  321. kmem_free(lv);
  322. lv = next;
  323. }
  324. }
  325. /*
  326. * Mark all items committed and clear busy extents. We free the log vector
  327. * chains in a separate pass so that we unpin the log items as quickly as
  328. * possible.
  329. */
  330. static void
  331. xlog_cil_committed(
  332. void *args,
  333. int abort)
  334. {
  335. struct xfs_cil_ctx *ctx = args;
  336. struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
  337. xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
  338. ctx->start_lsn, abort);
  339. xfs_extent_busy_sort(&ctx->busy_extents);
  340. xfs_extent_busy_clear(mp, &ctx->busy_extents,
  341. (mp->m_flags & XFS_MOUNT_DISCARD) && !abort);
  342. /*
  343. * If we are aborting the commit, wake up anyone waiting on the
  344. * committing list. If we don't, then a shutdown we can leave processes
  345. * waiting in xlog_cil_force_lsn() waiting on a sequence commit that
  346. * will never happen because we aborted it.
  347. */
  348. spin_lock(&ctx->cil->xc_push_lock);
  349. if (abort)
  350. wake_up_all(&ctx->cil->xc_commit_wait);
  351. list_del(&ctx->committing);
  352. spin_unlock(&ctx->cil->xc_push_lock);
  353. xlog_cil_free_logvec(ctx->lv_chain);
  354. if (!list_empty(&ctx->busy_extents)) {
  355. ASSERT(mp->m_flags & XFS_MOUNT_DISCARD);
  356. xfs_discard_extents(mp, &ctx->busy_extents);
  357. xfs_extent_busy_clear(mp, &ctx->busy_extents, false);
  358. }
  359. kmem_free(ctx);
  360. }
  361. /*
  362. * Push the Committed Item List to the log. If @push_seq flag is zero, then it
  363. * is a background flush and so we can chose to ignore it. Otherwise, if the
  364. * current sequence is the same as @push_seq we need to do a flush. If
  365. * @push_seq is less than the current sequence, then it has already been
  366. * flushed and we don't need to do anything - the caller will wait for it to
  367. * complete if necessary.
  368. *
  369. * @push_seq is a value rather than a flag because that allows us to do an
  370. * unlocked check of the sequence number for a match. Hence we can allows log
  371. * forces to run racily and not issue pushes for the same sequence twice. If we
  372. * get a race between multiple pushes for the same sequence they will block on
  373. * the first one and then abort, hence avoiding needless pushes.
  374. */
  375. STATIC int
  376. xlog_cil_push(
  377. struct xlog *log)
  378. {
  379. struct xfs_cil *cil = log->l_cilp;
  380. struct xfs_log_vec *lv;
  381. struct xfs_cil_ctx *ctx;
  382. struct xfs_cil_ctx *new_ctx;
  383. struct xlog_in_core *commit_iclog;
  384. struct xlog_ticket *tic;
  385. int num_iovecs;
  386. int error = 0;
  387. struct xfs_trans_header thdr;
  388. struct xfs_log_iovec lhdr;
  389. struct xfs_log_vec lvhdr = { NULL };
  390. xfs_lsn_t commit_lsn;
  391. xfs_lsn_t push_seq;
  392. if (!cil)
  393. return 0;
  394. new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
  395. new_ctx->ticket = xlog_cil_ticket_alloc(log);
  396. down_write(&cil->xc_ctx_lock);
  397. ctx = cil->xc_ctx;
  398. spin_lock(&cil->xc_push_lock);
  399. push_seq = cil->xc_push_seq;
  400. ASSERT(push_seq <= ctx->sequence);
  401. /*
  402. * Check if we've anything to push. If there is nothing, then we don't
  403. * move on to a new sequence number and so we have to be able to push
  404. * this sequence again later.
  405. */
  406. if (list_empty(&cil->xc_cil)) {
  407. cil->xc_push_seq = 0;
  408. spin_unlock(&cil->xc_push_lock);
  409. goto out_skip;
  410. }
  411. /* check for a previously pushed seqeunce */
  412. if (push_seq < cil->xc_ctx->sequence) {
  413. spin_unlock(&cil->xc_push_lock);
  414. goto out_skip;
  415. }
  416. /*
  417. * We are now going to push this context, so add it to the committing
  418. * list before we do anything else. This ensures that anyone waiting on
  419. * this push can easily detect the difference between a "push in
  420. * progress" and "CIL is empty, nothing to do".
  421. *
  422. * IOWs, a wait loop can now check for:
  423. * the current sequence not being found on the committing list;
  424. * an empty CIL; and
  425. * an unchanged sequence number
  426. * to detect a push that had nothing to do and therefore does not need
  427. * waiting on. If the CIL is not empty, we get put on the committing
  428. * list before emptying the CIL and bumping the sequence number. Hence
  429. * an empty CIL and an unchanged sequence number means we jumped out
  430. * above after doing nothing.
  431. *
  432. * Hence the waiter will either find the commit sequence on the
  433. * committing list or the sequence number will be unchanged and the CIL
  434. * still dirty. In that latter case, the push has not yet started, and
  435. * so the waiter will have to continue trying to check the CIL
  436. * committing list until it is found. In extreme cases of delay, the
  437. * sequence may fully commit between the attempts the wait makes to wait
  438. * on the commit sequence.
  439. */
  440. list_add(&ctx->committing, &cil->xc_committing);
  441. spin_unlock(&cil->xc_push_lock);
  442. /*
  443. * pull all the log vectors off the items in the CIL, and
  444. * remove the items from the CIL. We don't need the CIL lock
  445. * here because it's only needed on the transaction commit
  446. * side which is currently locked out by the flush lock.
  447. */
  448. lv = NULL;
  449. num_iovecs = 0;
  450. while (!list_empty(&cil->xc_cil)) {
  451. struct xfs_log_item *item;
  452. item = list_first_entry(&cil->xc_cil,
  453. struct xfs_log_item, li_cil);
  454. list_del_init(&item->li_cil);
  455. if (!ctx->lv_chain)
  456. ctx->lv_chain = item->li_lv;
  457. else
  458. lv->lv_next = item->li_lv;
  459. lv = item->li_lv;
  460. item->li_lv = NULL;
  461. num_iovecs += lv->lv_niovecs;
  462. }
  463. /*
  464. * initialise the new context and attach it to the CIL. Then attach
  465. * the current context to the CIL committing lsit so it can be found
  466. * during log forces to extract the commit lsn of the sequence that
  467. * needs to be forced.
  468. */
  469. INIT_LIST_HEAD(&new_ctx->committing);
  470. INIT_LIST_HEAD(&new_ctx->busy_extents);
  471. new_ctx->sequence = ctx->sequence + 1;
  472. new_ctx->cil = cil;
  473. cil->xc_ctx = new_ctx;
  474. /*
  475. * The switch is now done, so we can drop the context lock and move out
  476. * of a shared context. We can't just go straight to the commit record,
  477. * though - we need to synchronise with previous and future commits so
  478. * that the commit records are correctly ordered in the log to ensure
  479. * that we process items during log IO completion in the correct order.
  480. *
  481. * For example, if we get an EFI in one checkpoint and the EFD in the
  482. * next (e.g. due to log forces), we do not want the checkpoint with
  483. * the EFD to be committed before the checkpoint with the EFI. Hence
  484. * we must strictly order the commit records of the checkpoints so
  485. * that: a) the checkpoint callbacks are attached to the iclogs in the
  486. * correct order; and b) the checkpoints are replayed in correct order
  487. * in log recovery.
  488. *
  489. * Hence we need to add this context to the committing context list so
  490. * that higher sequences will wait for us to write out a commit record
  491. * before they do.
  492. *
  493. * xfs_log_force_lsn requires us to mirror the new sequence into the cil
  494. * structure atomically with the addition of this sequence to the
  495. * committing list. This also ensures that we can do unlocked checks
  496. * against the current sequence in log forces without risking
  497. * deferencing a freed context pointer.
  498. */
  499. spin_lock(&cil->xc_push_lock);
  500. cil->xc_current_sequence = new_ctx->sequence;
  501. spin_unlock(&cil->xc_push_lock);
  502. up_write(&cil->xc_ctx_lock);
  503. /*
  504. * Build a checkpoint transaction header and write it to the log to
  505. * begin the transaction. We need to account for the space used by the
  506. * transaction header here as it is not accounted for in xlog_write().
  507. *
  508. * The LSN we need to pass to the log items on transaction commit is
  509. * the LSN reported by the first log vector write. If we use the commit
  510. * record lsn then we can move the tail beyond the grant write head.
  511. */
  512. tic = ctx->ticket;
  513. thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
  514. thdr.th_type = XFS_TRANS_CHECKPOINT;
  515. thdr.th_tid = tic->t_tid;
  516. thdr.th_num_items = num_iovecs;
  517. lhdr.i_addr = &thdr;
  518. lhdr.i_len = sizeof(xfs_trans_header_t);
  519. lhdr.i_type = XLOG_REG_TYPE_TRANSHDR;
  520. tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t);
  521. lvhdr.lv_niovecs = 1;
  522. lvhdr.lv_iovecp = &lhdr;
  523. lvhdr.lv_next = ctx->lv_chain;
  524. error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0);
  525. if (error)
  526. goto out_abort_free_ticket;
  527. /*
  528. * now that we've written the checkpoint into the log, strictly
  529. * order the commit records so replay will get them in the right order.
  530. */
  531. restart:
  532. spin_lock(&cil->xc_push_lock);
  533. list_for_each_entry(new_ctx, &cil->xc_committing, committing) {
  534. /*
  535. * Avoid getting stuck in this loop because we were woken by the
  536. * shutdown, but then went back to sleep once already in the
  537. * shutdown state.
  538. */
  539. if (XLOG_FORCED_SHUTDOWN(log)) {
  540. spin_unlock(&cil->xc_push_lock);
  541. goto out_abort_free_ticket;
  542. }
  543. /*
  544. * Higher sequences will wait for this one so skip them.
  545. * Don't wait for our own sequence, either.
  546. */
  547. if (new_ctx->sequence >= ctx->sequence)
  548. continue;
  549. if (!new_ctx->commit_lsn) {
  550. /*
  551. * It is still being pushed! Wait for the push to
  552. * complete, then start again from the beginning.
  553. */
  554. xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
  555. goto restart;
  556. }
  557. }
  558. spin_unlock(&cil->xc_push_lock);
  559. /* xfs_log_done always frees the ticket on error. */
  560. commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0);
  561. if (commit_lsn == -1)
  562. goto out_abort;
  563. /* attach all the transactions w/ busy extents to iclog */
  564. ctx->log_cb.cb_func = xlog_cil_committed;
  565. ctx->log_cb.cb_arg = ctx;
  566. error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb);
  567. if (error)
  568. goto out_abort;
  569. /*
  570. * now the checkpoint commit is complete and we've attached the
  571. * callbacks to the iclog we can assign the commit LSN to the context
  572. * and wake up anyone who is waiting for the commit to complete.
  573. */
  574. spin_lock(&cil->xc_push_lock);
  575. ctx->commit_lsn = commit_lsn;
  576. wake_up_all(&cil->xc_commit_wait);
  577. spin_unlock(&cil->xc_push_lock);
  578. /* release the hounds! */
  579. return xfs_log_release_iclog(log->l_mp, commit_iclog);
  580. out_skip:
  581. up_write(&cil->xc_ctx_lock);
  582. xfs_log_ticket_put(new_ctx->ticket);
  583. kmem_free(new_ctx);
  584. return 0;
  585. out_abort_free_ticket:
  586. xfs_log_ticket_put(tic);
  587. out_abort:
  588. xlog_cil_committed(ctx, XFS_LI_ABORTED);
  589. return -EIO;
  590. }
  591. static void
  592. xlog_cil_push_work(
  593. struct work_struct *work)
  594. {
  595. struct xfs_cil *cil = container_of(work, struct xfs_cil,
  596. xc_push_work);
  597. xlog_cil_push(cil->xc_log);
  598. }
  599. /*
  600. * We need to push CIL every so often so we don't cache more than we can fit in
  601. * the log. The limit really is that a checkpoint can't be more than half the
  602. * log (the current checkpoint is not allowed to overwrite the previous
  603. * checkpoint), but commit latency and memory usage limit this to a smaller
  604. * size.
  605. */
  606. static void
  607. xlog_cil_push_background(
  608. struct xlog *log)
  609. {
  610. struct xfs_cil *cil = log->l_cilp;
  611. /*
  612. * The cil won't be empty because we are called while holding the
  613. * context lock so whatever we added to the CIL will still be there
  614. */
  615. ASSERT(!list_empty(&cil->xc_cil));
  616. /*
  617. * don't do a background push if we haven't used up all the
  618. * space available yet.
  619. */
  620. if (cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log))
  621. return;
  622. spin_lock(&cil->xc_push_lock);
  623. if (cil->xc_push_seq < cil->xc_current_sequence) {
  624. cil->xc_push_seq = cil->xc_current_sequence;
  625. queue_work(log->l_mp->m_cil_workqueue, &cil->xc_push_work);
  626. }
  627. spin_unlock(&cil->xc_push_lock);
  628. }
  629. /*
  630. * xlog_cil_push_now() is used to trigger an immediate CIL push to the sequence
  631. * number that is passed. When it returns, the work will be queued for
  632. * @push_seq, but it won't be completed. The caller is expected to do any
  633. * waiting for push_seq to complete if it is required.
  634. */
  635. static void
  636. xlog_cil_push_now(
  637. struct xlog *log,
  638. xfs_lsn_t push_seq)
  639. {
  640. struct xfs_cil *cil = log->l_cilp;
  641. if (!cil)
  642. return;
  643. ASSERT(push_seq && push_seq <= cil->xc_current_sequence);
  644. /* start on any pending background push to minimise wait time on it */
  645. flush_work(&cil->xc_push_work);
  646. /*
  647. * If the CIL is empty or we've already pushed the sequence then
  648. * there's no work we need to do.
  649. */
  650. spin_lock(&cil->xc_push_lock);
  651. if (list_empty(&cil->xc_cil) || push_seq <= cil->xc_push_seq) {
  652. spin_unlock(&cil->xc_push_lock);
  653. return;
  654. }
  655. cil->xc_push_seq = push_seq;
  656. queue_work(log->l_mp->m_cil_workqueue, &cil->xc_push_work);
  657. spin_unlock(&cil->xc_push_lock);
  658. }
  659. bool
  660. xlog_cil_empty(
  661. struct xlog *log)
  662. {
  663. struct xfs_cil *cil = log->l_cilp;
  664. bool empty = false;
  665. spin_lock(&cil->xc_push_lock);
  666. if (list_empty(&cil->xc_cil))
  667. empty = true;
  668. spin_unlock(&cil->xc_push_lock);
  669. return empty;
  670. }
  671. /*
  672. * Commit a transaction with the given vector to the Committed Item List.
  673. *
  674. * To do this, we need to format the item, pin it in memory if required and
  675. * account for the space used by the transaction. Once we have done that we
  676. * need to release the unused reservation for the transaction, attach the
  677. * transaction to the checkpoint context so we carry the busy extents through
  678. * to checkpoint completion, and then unlock all the items in the transaction.
  679. *
  680. * Called with the context lock already held in read mode to lock out
  681. * background commit, returns without it held once background commits are
  682. * allowed again.
  683. */
  684. void
  685. xfs_log_commit_cil(
  686. struct xfs_mount *mp,
  687. struct xfs_trans *tp,
  688. xfs_lsn_t *commit_lsn,
  689. int flags)
  690. {
  691. struct xlog *log = mp->m_log;
  692. struct xfs_cil *cil = log->l_cilp;
  693. int log_flags = 0;
  694. if (flags & XFS_TRANS_RELEASE_LOG_RES)
  695. log_flags = XFS_LOG_REL_PERM_RESERV;
  696. /* lock out background commit */
  697. down_read(&cil->xc_ctx_lock);
  698. xlog_cil_insert_items(log, tp);
  699. /* check we didn't blow the reservation */
  700. if (tp->t_ticket->t_curr_res < 0)
  701. xlog_print_tic_res(mp, tp->t_ticket);
  702. tp->t_commit_lsn = cil->xc_ctx->sequence;
  703. if (commit_lsn)
  704. *commit_lsn = tp->t_commit_lsn;
  705. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  706. xfs_trans_unreserve_and_mod_sb(tp);
  707. /*
  708. * Once all the items of the transaction have been copied to the CIL,
  709. * the items can be unlocked and freed.
  710. *
  711. * This needs to be done before we drop the CIL context lock because we
  712. * have to update state in the log items and unlock them before they go
  713. * to disk. If we don't, then the CIL checkpoint can race with us and
  714. * we can run checkpoint completion before we've updated and unlocked
  715. * the log items. This affects (at least) processing of stale buffers,
  716. * inodes and EFIs.
  717. */
  718. xfs_trans_free_items(tp, tp->t_commit_lsn, 0);
  719. xlog_cil_push_background(log);
  720. up_read(&cil->xc_ctx_lock);
  721. }
  722. /*
  723. * Conditionally push the CIL based on the sequence passed in.
  724. *
  725. * We only need to push if we haven't already pushed the sequence
  726. * number given. Hence the only time we will trigger a push here is
  727. * if the push sequence is the same as the current context.
  728. *
  729. * We return the current commit lsn to allow the callers to determine if a
  730. * iclog flush is necessary following this call.
  731. */
  732. xfs_lsn_t
  733. xlog_cil_force_lsn(
  734. struct xlog *log,
  735. xfs_lsn_t sequence)
  736. {
  737. struct xfs_cil *cil = log->l_cilp;
  738. struct xfs_cil_ctx *ctx;
  739. xfs_lsn_t commit_lsn = NULLCOMMITLSN;
  740. ASSERT(sequence <= cil->xc_current_sequence);
  741. /*
  742. * check to see if we need to force out the current context.
  743. * xlog_cil_push() handles racing pushes for the same sequence,
  744. * so no need to deal with it here.
  745. */
  746. restart:
  747. xlog_cil_push_now(log, sequence);
  748. /*
  749. * See if we can find a previous sequence still committing.
  750. * We need to wait for all previous sequence commits to complete
  751. * before allowing the force of push_seq to go ahead. Hence block
  752. * on commits for those as well.
  753. */
  754. spin_lock(&cil->xc_push_lock);
  755. list_for_each_entry(ctx, &cil->xc_committing, committing) {
  756. /*
  757. * Avoid getting stuck in this loop because we were woken by the
  758. * shutdown, but then went back to sleep once already in the
  759. * shutdown state.
  760. */
  761. if (XLOG_FORCED_SHUTDOWN(log))
  762. goto out_shutdown;
  763. if (ctx->sequence > sequence)
  764. continue;
  765. if (!ctx->commit_lsn) {
  766. /*
  767. * It is still being pushed! Wait for the push to
  768. * complete, then start again from the beginning.
  769. */
  770. xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
  771. goto restart;
  772. }
  773. if (ctx->sequence != sequence)
  774. continue;
  775. /* found it! */
  776. commit_lsn = ctx->commit_lsn;
  777. }
  778. /*
  779. * The call to xlog_cil_push_now() executes the push in the background.
  780. * Hence by the time we have got here it our sequence may not have been
  781. * pushed yet. This is true if the current sequence still matches the
  782. * push sequence after the above wait loop and the CIL still contains
  783. * dirty objects. This is guaranteed by the push code first adding the
  784. * context to the committing list before emptying the CIL.
  785. *
  786. * Hence if we don't find the context in the committing list and the
  787. * current sequence number is unchanged then the CIL contents are
  788. * significant. If the CIL is empty, if means there was nothing to push
  789. * and that means there is nothing to wait for. If the CIL is not empty,
  790. * it means we haven't yet started the push, because if it had started
  791. * we would have found the context on the committing list.
  792. */
  793. if (sequence == cil->xc_current_sequence &&
  794. !list_empty(&cil->xc_cil)) {
  795. spin_unlock(&cil->xc_push_lock);
  796. goto restart;
  797. }
  798. spin_unlock(&cil->xc_push_lock);
  799. return commit_lsn;
  800. /*
  801. * We detected a shutdown in progress. We need to trigger the log force
  802. * to pass through it's iclog state machine error handling, even though
  803. * we are already in a shutdown state. Hence we can't return
  804. * NULLCOMMITLSN here as that has special meaning to log forces (i.e.
  805. * LSN is already stable), so we return a zero LSN instead.
  806. */
  807. out_shutdown:
  808. spin_unlock(&cil->xc_push_lock);
  809. return 0;
  810. }
  811. /*
  812. * Check if the current log item was first committed in this sequence.
  813. * We can't rely on just the log item being in the CIL, we have to check
  814. * the recorded commit sequence number.
  815. *
  816. * Note: for this to be used in a non-racy manner, it has to be called with
  817. * CIL flushing locked out. As a result, it should only be used during the
  818. * transaction commit process when deciding what to format into the item.
  819. */
  820. bool
  821. xfs_log_item_in_current_chkpt(
  822. struct xfs_log_item *lip)
  823. {
  824. struct xfs_cil_ctx *ctx;
  825. if (list_empty(&lip->li_cil))
  826. return false;
  827. ctx = lip->li_mountp->m_log->l_cilp->xc_ctx;
  828. /*
  829. * li_seq is written on the first commit of a log item to record the
  830. * first checkpoint it is written to. Hence if it is different to the
  831. * current sequence, we're in a new checkpoint.
  832. */
  833. if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0)
  834. return false;
  835. return true;
  836. }
  837. /*
  838. * Perform initial CIL structure initialisation.
  839. */
  840. int
  841. xlog_cil_init(
  842. struct xlog *log)
  843. {
  844. struct xfs_cil *cil;
  845. struct xfs_cil_ctx *ctx;
  846. cil = kmem_zalloc(sizeof(*cil), KM_SLEEP|KM_MAYFAIL);
  847. if (!cil)
  848. return -ENOMEM;
  849. ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP|KM_MAYFAIL);
  850. if (!ctx) {
  851. kmem_free(cil);
  852. return -ENOMEM;
  853. }
  854. INIT_WORK(&cil->xc_push_work, xlog_cil_push_work);
  855. INIT_LIST_HEAD(&cil->xc_cil);
  856. INIT_LIST_HEAD(&cil->xc_committing);
  857. spin_lock_init(&cil->xc_cil_lock);
  858. spin_lock_init(&cil->xc_push_lock);
  859. init_rwsem(&cil->xc_ctx_lock);
  860. init_waitqueue_head(&cil->xc_commit_wait);
  861. INIT_LIST_HEAD(&ctx->committing);
  862. INIT_LIST_HEAD(&ctx->busy_extents);
  863. ctx->sequence = 1;
  864. ctx->cil = cil;
  865. cil->xc_ctx = ctx;
  866. cil->xc_current_sequence = ctx->sequence;
  867. cil->xc_log = log;
  868. log->l_cilp = cil;
  869. return 0;
  870. }
  871. void
  872. xlog_cil_destroy(
  873. struct xlog *log)
  874. {
  875. if (log->l_cilp->xc_ctx) {
  876. if (log->l_cilp->xc_ctx->ticket)
  877. xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket);
  878. kmem_free(log->l_cilp->xc_ctx);
  879. }
  880. ASSERT(list_empty(&log->l_cilp->xc_cil));
  881. kmem_free(log->l_cilp);
  882. }