xfs_log_cil.c 30 KB

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