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