xfs_log_cil.c 26 KB

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