xfs_log_cil.c 27 KB

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