xfs_log_cil.c 22 KB

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