xfs_log_cil.c 21 KB

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