xfs_log_cil.c 24 KB

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