xfs_log_cil.c 26 KB

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