xfs_log_cil.c 28 KB

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