checkpoint.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * linux/fs/jbd2/checkpoint.c
  3. *
  4. * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
  5. *
  6. * Copyright 1999 Red Hat Software --- All Rights Reserved
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * Checkpoint routines for the generic filesystem journaling code.
  13. * Part of the ext2fs journaling system.
  14. *
  15. * Checkpointing is the process of ensuring that a section of the log is
  16. * committed fully to disk, so that that portion of the log can be
  17. * reused.
  18. */
  19. #include <linux/time.h>
  20. #include <linux/fs.h>
  21. #include <linux/jbd2.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/blkdev.h>
  25. #include <trace/events/jbd2.h>
  26. /*
  27. * Unlink a buffer from a transaction checkpoint list.
  28. *
  29. * Called with j_list_lock held.
  30. */
  31. static inline void __buffer_unlink_first(struct journal_head *jh)
  32. {
  33. transaction_t *transaction = jh->b_cp_transaction;
  34. jh->b_cpnext->b_cpprev = jh->b_cpprev;
  35. jh->b_cpprev->b_cpnext = jh->b_cpnext;
  36. if (transaction->t_checkpoint_list == jh) {
  37. transaction->t_checkpoint_list = jh->b_cpnext;
  38. if (transaction->t_checkpoint_list == jh)
  39. transaction->t_checkpoint_list = NULL;
  40. }
  41. }
  42. /*
  43. * Unlink a buffer from a transaction checkpoint(io) list.
  44. *
  45. * Called with j_list_lock held.
  46. */
  47. static inline void __buffer_unlink(struct journal_head *jh)
  48. {
  49. transaction_t *transaction = jh->b_cp_transaction;
  50. __buffer_unlink_first(jh);
  51. if (transaction->t_checkpoint_io_list == jh) {
  52. transaction->t_checkpoint_io_list = jh->b_cpnext;
  53. if (transaction->t_checkpoint_io_list == jh)
  54. transaction->t_checkpoint_io_list = NULL;
  55. }
  56. }
  57. /*
  58. * Move a buffer from the checkpoint list to the checkpoint io list
  59. *
  60. * Called with j_list_lock held
  61. */
  62. static inline void __buffer_relink_io(struct journal_head *jh)
  63. {
  64. transaction_t *transaction = jh->b_cp_transaction;
  65. __buffer_unlink_first(jh);
  66. if (!transaction->t_checkpoint_io_list) {
  67. jh->b_cpnext = jh->b_cpprev = jh;
  68. } else {
  69. jh->b_cpnext = transaction->t_checkpoint_io_list;
  70. jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev;
  71. jh->b_cpprev->b_cpnext = jh;
  72. jh->b_cpnext->b_cpprev = jh;
  73. }
  74. transaction->t_checkpoint_io_list = jh;
  75. }
  76. /*
  77. * Try to release a checkpointed buffer from its transaction.
  78. * Returns 1 if we released it and 2 if we also released the
  79. * whole transaction.
  80. *
  81. * Requires j_list_lock
  82. */
  83. static int __try_to_free_cp_buf(struct journal_head *jh)
  84. {
  85. int ret = 0;
  86. struct buffer_head *bh = jh2bh(jh);
  87. if (jh->b_transaction == NULL && !buffer_locked(bh) &&
  88. !buffer_dirty(bh) && !buffer_write_io_error(bh)) {
  89. /*
  90. * Get our reference so that bh cannot be freed before
  91. * we unlock it
  92. */
  93. get_bh(bh);
  94. JBUFFER_TRACE(jh, "remove from checkpoint list");
  95. ret = __jbd2_journal_remove_checkpoint(jh) + 1;
  96. BUFFER_TRACE(bh, "release");
  97. __brelse(bh);
  98. }
  99. return ret;
  100. }
  101. /*
  102. * __jbd2_log_wait_for_space: wait until there is space in the journal.
  103. *
  104. * Called under j-state_lock *only*. It will be unlocked if we have to wait
  105. * for a checkpoint to free up some space in the log.
  106. */
  107. void __jbd2_log_wait_for_space(journal_t *journal)
  108. {
  109. int nblocks, space_left;
  110. /* assert_spin_locked(&journal->j_state_lock); */
  111. nblocks = jbd_space_needed(journal);
  112. while (__jbd2_log_space_left(journal) < nblocks) {
  113. if (journal->j_flags & JBD2_ABORT)
  114. return;
  115. write_unlock(&journal->j_state_lock);
  116. mutex_lock(&journal->j_checkpoint_mutex);
  117. /*
  118. * Test again, another process may have checkpointed while we
  119. * were waiting for the checkpoint lock. If there are no
  120. * transactions ready to be checkpointed, try to recover
  121. * journal space by calling cleanup_journal_tail(), and if
  122. * that doesn't work, by waiting for the currently committing
  123. * transaction to complete. If there is absolutely no way
  124. * to make progress, this is either a BUG or corrupted
  125. * filesystem, so abort the journal and leave a stack
  126. * trace for forensic evidence.
  127. */
  128. write_lock(&journal->j_state_lock);
  129. spin_lock(&journal->j_list_lock);
  130. nblocks = jbd_space_needed(journal);
  131. space_left = __jbd2_log_space_left(journal);
  132. if (space_left < nblocks) {
  133. int chkpt = journal->j_checkpoint_transactions != NULL;
  134. tid_t tid = 0;
  135. if (journal->j_committing_transaction)
  136. tid = journal->j_committing_transaction->t_tid;
  137. spin_unlock(&journal->j_list_lock);
  138. write_unlock(&journal->j_state_lock);
  139. if (chkpt) {
  140. jbd2_log_do_checkpoint(journal);
  141. } else if (jbd2_cleanup_journal_tail(journal) == 0) {
  142. /* We were able to recover space; yay! */
  143. ;
  144. } else if (tid) {
  145. jbd2_log_wait_commit(journal, tid);
  146. } else {
  147. printk(KERN_ERR "%s: needed %d blocks and "
  148. "only had %d space available\n",
  149. __func__, nblocks, space_left);
  150. printk(KERN_ERR "%s: no way to get more "
  151. "journal space in %s\n", __func__,
  152. journal->j_devname);
  153. WARN_ON(1);
  154. jbd2_journal_abort(journal, 0);
  155. }
  156. write_lock(&journal->j_state_lock);
  157. } else {
  158. spin_unlock(&journal->j_list_lock);
  159. }
  160. mutex_unlock(&journal->j_checkpoint_mutex);
  161. }
  162. }
  163. /*
  164. * Clean up transaction's list of buffers submitted for io.
  165. * We wait for any pending IO to complete and remove any clean
  166. * buffers. Note that we take the buffers in the opposite ordering
  167. * from the one in which they were submitted for IO.
  168. *
  169. * Return 0 on success, and return <0 if some buffers have failed
  170. * to be written out.
  171. *
  172. * Called with j_list_lock held.
  173. */
  174. static int __wait_cp_io(journal_t *journal, transaction_t *transaction)
  175. {
  176. struct journal_head *jh;
  177. struct buffer_head *bh;
  178. tid_t this_tid;
  179. int released = 0;
  180. int ret = 0;
  181. this_tid = transaction->t_tid;
  182. restart:
  183. /* Did somebody clean up the transaction in the meanwhile? */
  184. if (journal->j_checkpoint_transactions != transaction ||
  185. transaction->t_tid != this_tid)
  186. return ret;
  187. while (!released && transaction->t_checkpoint_io_list) {
  188. jh = transaction->t_checkpoint_io_list;
  189. bh = jh2bh(jh);
  190. get_bh(bh);
  191. if (buffer_locked(bh)) {
  192. spin_unlock(&journal->j_list_lock);
  193. wait_on_buffer(bh);
  194. /* the journal_head may have gone by now */
  195. BUFFER_TRACE(bh, "brelse");
  196. __brelse(bh);
  197. spin_lock(&journal->j_list_lock);
  198. goto restart;
  199. }
  200. if (unlikely(buffer_write_io_error(bh)))
  201. ret = -EIO;
  202. /*
  203. * Now in whatever state the buffer currently is, we know that
  204. * it has been written out and so we can drop it from the list
  205. */
  206. released = __jbd2_journal_remove_checkpoint(jh);
  207. __brelse(bh);
  208. }
  209. return ret;
  210. }
  211. static void
  212. __flush_batch(journal_t *journal, int *batch_count)
  213. {
  214. int i;
  215. struct blk_plug plug;
  216. blk_start_plug(&plug);
  217. for (i = 0; i < *batch_count; i++)
  218. write_dirty_buffer(journal->j_chkpt_bhs[i], WRITE_SYNC);
  219. blk_finish_plug(&plug);
  220. for (i = 0; i < *batch_count; i++) {
  221. struct buffer_head *bh = journal->j_chkpt_bhs[i];
  222. BUFFER_TRACE(bh, "brelse");
  223. __brelse(bh);
  224. }
  225. *batch_count = 0;
  226. }
  227. /*
  228. * Try to flush one buffer from the checkpoint list to disk.
  229. *
  230. * Return 1 if something happened which requires us to abort the current
  231. * scan of the checkpoint list. Return <0 if the buffer has failed to
  232. * be written out.
  233. *
  234. * Called with j_list_lock held and drops it if 1 is returned
  235. */
  236. static int __process_buffer(journal_t *journal, struct journal_head *jh,
  237. int *batch_count, transaction_t *transaction)
  238. {
  239. struct buffer_head *bh = jh2bh(jh);
  240. int ret = 0;
  241. if (buffer_locked(bh)) {
  242. get_bh(bh);
  243. spin_unlock(&journal->j_list_lock);
  244. wait_on_buffer(bh);
  245. /* the journal_head may have gone by now */
  246. BUFFER_TRACE(bh, "brelse");
  247. __brelse(bh);
  248. ret = 1;
  249. } else if (jh->b_transaction != NULL) {
  250. transaction_t *t = jh->b_transaction;
  251. tid_t tid = t->t_tid;
  252. transaction->t_chp_stats.cs_forced_to_close++;
  253. spin_unlock(&journal->j_list_lock);
  254. if (unlikely(journal->j_flags & JBD2_UNMOUNT))
  255. /*
  256. * The journal thread is dead; so starting and
  257. * waiting for a commit to finish will cause
  258. * us to wait for a _very_ long time.
  259. */
  260. printk(KERN_ERR "JBD2: %s: "
  261. "Waiting for Godot: block %llu\n",
  262. journal->j_devname,
  263. (unsigned long long) bh->b_blocknr);
  264. jbd2_log_start_commit(journal, tid);
  265. jbd2_log_wait_commit(journal, tid);
  266. ret = 1;
  267. } else if (!buffer_dirty(bh)) {
  268. ret = 1;
  269. if (unlikely(buffer_write_io_error(bh)))
  270. ret = -EIO;
  271. get_bh(bh);
  272. BUFFER_TRACE(bh, "remove from checkpoint");
  273. __jbd2_journal_remove_checkpoint(jh);
  274. spin_unlock(&journal->j_list_lock);
  275. __brelse(bh);
  276. } else {
  277. /*
  278. * Important: we are about to write the buffer, and
  279. * possibly block, while still holding the journal lock.
  280. * We cannot afford to let the transaction logic start
  281. * messing around with this buffer before we write it to
  282. * disk, as that would break recoverability.
  283. */
  284. BUFFER_TRACE(bh, "queue");
  285. get_bh(bh);
  286. J_ASSERT_BH(bh, !buffer_jwrite(bh));
  287. journal->j_chkpt_bhs[*batch_count] = bh;
  288. __buffer_relink_io(jh);
  289. transaction->t_chp_stats.cs_written++;
  290. (*batch_count)++;
  291. if (*batch_count == JBD2_NR_BATCH) {
  292. spin_unlock(&journal->j_list_lock);
  293. __flush_batch(journal, batch_count);
  294. ret = 1;
  295. }
  296. }
  297. return ret;
  298. }
  299. /*
  300. * Perform an actual checkpoint. We take the first transaction on the
  301. * list of transactions to be checkpointed and send all its buffers
  302. * to disk. We submit larger chunks of data at once.
  303. *
  304. * The journal should be locked before calling this function.
  305. * Called with j_checkpoint_mutex held.
  306. */
  307. int jbd2_log_do_checkpoint(journal_t *journal)
  308. {
  309. transaction_t *transaction;
  310. tid_t this_tid;
  311. int result;
  312. jbd_debug(1, "Start checkpoint\n");
  313. /*
  314. * First thing: if there are any transactions in the log which
  315. * don't need checkpointing, just eliminate them from the
  316. * journal straight away.
  317. */
  318. result = jbd2_cleanup_journal_tail(journal);
  319. trace_jbd2_checkpoint(journal, result);
  320. jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
  321. if (result <= 0)
  322. return result;
  323. /*
  324. * OK, we need to start writing disk blocks. Take one transaction
  325. * and write it.
  326. */
  327. result = 0;
  328. spin_lock(&journal->j_list_lock);
  329. if (!journal->j_checkpoint_transactions)
  330. goto out;
  331. transaction = journal->j_checkpoint_transactions;
  332. if (transaction->t_chp_stats.cs_chp_time == 0)
  333. transaction->t_chp_stats.cs_chp_time = jiffies;
  334. this_tid = transaction->t_tid;
  335. restart:
  336. /*
  337. * If someone cleaned up this transaction while we slept, we're
  338. * done (maybe it's a new transaction, but it fell at the same
  339. * address).
  340. */
  341. if (journal->j_checkpoint_transactions == transaction &&
  342. transaction->t_tid == this_tid) {
  343. int batch_count = 0;
  344. struct journal_head *jh;
  345. int retry = 0, err;
  346. while (!retry && transaction->t_checkpoint_list) {
  347. jh = transaction->t_checkpoint_list;
  348. retry = __process_buffer(journal, jh, &batch_count,
  349. transaction);
  350. if (retry < 0 && !result)
  351. result = retry;
  352. if (!retry && (need_resched() ||
  353. spin_needbreak(&journal->j_list_lock))) {
  354. spin_unlock(&journal->j_list_lock);
  355. retry = 1;
  356. break;
  357. }
  358. }
  359. if (batch_count) {
  360. if (!retry) {
  361. spin_unlock(&journal->j_list_lock);
  362. retry = 1;
  363. }
  364. __flush_batch(journal, &batch_count);
  365. }
  366. if (retry) {
  367. spin_lock(&journal->j_list_lock);
  368. goto restart;
  369. }
  370. /*
  371. * Now we have cleaned up the first transaction's checkpoint
  372. * list. Let's clean up the second one
  373. */
  374. err = __wait_cp_io(journal, transaction);
  375. if (!result)
  376. result = err;
  377. }
  378. out:
  379. spin_unlock(&journal->j_list_lock);
  380. if (result < 0)
  381. jbd2_journal_abort(journal, result);
  382. else
  383. result = jbd2_cleanup_journal_tail(journal);
  384. return (result < 0) ? result : 0;
  385. }
  386. /*
  387. * Check the list of checkpoint transactions for the journal to see if
  388. * we have already got rid of any since the last update of the log tail
  389. * in the journal superblock. If so, we can instantly roll the
  390. * superblock forward to remove those transactions from the log.
  391. *
  392. * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
  393. *
  394. * Called with the journal lock held.
  395. *
  396. * This is the only part of the journaling code which really needs to be
  397. * aware of transaction aborts. Checkpointing involves writing to the
  398. * main filesystem area rather than to the journal, so it can proceed
  399. * even in abort state, but we must not update the super block if
  400. * checkpointing may have failed. Otherwise, we would lose some metadata
  401. * buffers which should be written-back to the filesystem.
  402. */
  403. int jbd2_cleanup_journal_tail(journal_t *journal)
  404. {
  405. tid_t first_tid;
  406. unsigned long blocknr;
  407. if (is_journal_aborted(journal))
  408. return -EIO;
  409. if (!jbd2_journal_get_log_tail(journal, &first_tid, &blocknr))
  410. return 1;
  411. J_ASSERT(blocknr != 0);
  412. /*
  413. * We need to make sure that any blocks that were recently written out
  414. * --- perhaps by jbd2_log_do_checkpoint() --- are flushed out before
  415. * we drop the transactions from the journal. It's unlikely this will
  416. * be necessary, especially with an appropriately sized journal, but we
  417. * need this to guarantee correctness. Fortunately
  418. * jbd2_cleanup_journal_tail() doesn't get called all that often.
  419. */
  420. if (journal->j_flags & JBD2_BARRIER)
  421. blkdev_issue_flush(journal->j_fs_dev, GFP_NOFS, NULL);
  422. return __jbd2_update_log_tail(journal, first_tid, blocknr);
  423. }
  424. /* Checkpoint list management */
  425. /*
  426. * journal_clean_one_cp_list
  427. *
  428. * Find all the written-back checkpoint buffers in the given list and
  429. * release them. If 'destroy' is set, clean all buffers unconditionally.
  430. *
  431. * Called with the journal locked.
  432. * Called with j_list_lock held.
  433. * Returns number of buffers reaped (for debug)
  434. */
  435. static int journal_clean_one_cp_list(struct journal_head *jh, int *released, bool destroy)
  436. {
  437. struct journal_head *last_jh;
  438. struct journal_head *next_jh = jh;
  439. int ret, freed = 0;
  440. *released = 0;
  441. if (!jh)
  442. return 0;
  443. last_jh = jh->b_cpprev;
  444. do {
  445. jh = next_jh;
  446. next_jh = jh->b_cpnext;
  447. if (!destroy)
  448. ret = __try_to_free_cp_buf(jh);
  449. else
  450. ret = __jbd2_journal_remove_checkpoint(jh) + 1;
  451. if (ret) {
  452. freed++;
  453. if (ret == 2) {
  454. *released = 1;
  455. return freed;
  456. }
  457. }
  458. /*
  459. * This function only frees up some memory
  460. * if possible so we dont have an obligation
  461. * to finish processing. Bail out if preemption
  462. * requested:
  463. */
  464. if (need_resched())
  465. return freed;
  466. } while (jh != last_jh);
  467. return freed;
  468. }
  469. /*
  470. * journal_clean_checkpoint_list
  471. *
  472. * Find all the written-back checkpoint buffers in the journal and release them.
  473. *
  474. * If 'destroy' is set, release all buffers unconditionally.
  475. *
  476. * Called with the journal locked.
  477. * Called with j_list_lock held.
  478. * Returns number of buffers reaped (for debug)
  479. */
  480. int __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy)
  481. {
  482. transaction_t *transaction, *last_transaction, *next_transaction;
  483. int ret = 0;
  484. int released;
  485. transaction = journal->j_checkpoint_transactions;
  486. if (!transaction)
  487. goto out;
  488. last_transaction = transaction->t_cpprev;
  489. next_transaction = transaction;
  490. do {
  491. transaction = next_transaction;
  492. next_transaction = transaction->t_cpnext;
  493. ret += journal_clean_one_cp_list(transaction->
  494. t_checkpoint_list, &released, destroy);
  495. /*
  496. * This function only frees up some memory if possible so we
  497. * dont have an obligation to finish processing. Bail out if
  498. * preemption requested:
  499. */
  500. if (need_resched())
  501. goto out;
  502. if (released)
  503. continue;
  504. /*
  505. * It is essential that we are as careful as in the case of
  506. * t_checkpoint_list with removing the buffer from the list as
  507. * we can possibly see not yet submitted buffers on io_list
  508. */
  509. ret += journal_clean_one_cp_list(transaction->
  510. t_checkpoint_io_list, &released, destroy);
  511. if (need_resched())
  512. goto out;
  513. } while (transaction != last_transaction);
  514. out:
  515. return ret;
  516. }
  517. /*
  518. * Remove buffers from all checkpoint lists as journal is aborted and we just
  519. * need to free memory
  520. */
  521. void jbd2_journal_destroy_checkpoint(journal_t *journal)
  522. {
  523. /*
  524. * We loop because __jbd2_journal_clean_checkpoint_list() may abort
  525. * early due to a need of rescheduling.
  526. */
  527. while (1) {
  528. spin_lock(&journal->j_list_lock);
  529. if (!journal->j_checkpoint_transactions) {
  530. spin_unlock(&journal->j_list_lock);
  531. break;
  532. }
  533. __jbd2_journal_clean_checkpoint_list(journal, true);
  534. spin_unlock(&journal->j_list_lock);
  535. cond_resched();
  536. }
  537. }
  538. /*
  539. * journal_remove_checkpoint: called after a buffer has been committed
  540. * to disk (either by being write-back flushed to disk, or being
  541. * committed to the log).
  542. *
  543. * We cannot safely clean a transaction out of the log until all of the
  544. * buffer updates committed in that transaction have safely been stored
  545. * elsewhere on disk. To achieve this, all of the buffers in a
  546. * transaction need to be maintained on the transaction's checkpoint
  547. * lists until they have been rewritten, at which point this function is
  548. * called to remove the buffer from the existing transaction's
  549. * checkpoint lists.
  550. *
  551. * The function returns 1 if it frees the transaction, 0 otherwise.
  552. * The function can free jh and bh.
  553. *
  554. * This function is called with j_list_lock held.
  555. */
  556. int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
  557. {
  558. struct transaction_chp_stats_s *stats;
  559. transaction_t *transaction;
  560. journal_t *journal;
  561. int ret = 0;
  562. JBUFFER_TRACE(jh, "entry");
  563. if ((transaction = jh->b_cp_transaction) == NULL) {
  564. JBUFFER_TRACE(jh, "not on transaction");
  565. goto out;
  566. }
  567. journal = transaction->t_journal;
  568. JBUFFER_TRACE(jh, "removing from transaction");
  569. __buffer_unlink(jh);
  570. jh->b_cp_transaction = NULL;
  571. jbd2_journal_put_journal_head(jh);
  572. if (transaction->t_checkpoint_list != NULL ||
  573. transaction->t_checkpoint_io_list != NULL)
  574. goto out;
  575. /*
  576. * There is one special case to worry about: if we have just pulled the
  577. * buffer off a running or committing transaction's checkpoing list,
  578. * then even if the checkpoint list is empty, the transaction obviously
  579. * cannot be dropped!
  580. *
  581. * The locking here around t_state is a bit sleazy.
  582. * See the comment at the end of jbd2_journal_commit_transaction().
  583. */
  584. if (transaction->t_state != T_FINISHED)
  585. goto out;
  586. /* OK, that was the last buffer for the transaction: we can now
  587. safely remove this transaction from the log */
  588. stats = &transaction->t_chp_stats;
  589. if (stats->cs_chp_time)
  590. stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
  591. jiffies);
  592. trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
  593. transaction->t_tid, stats);
  594. __jbd2_journal_drop_transaction(journal, transaction);
  595. if ((journal->j_commit_callback == NULL) || (transaction->t_callbacked)) {
  596. jbd2_journal_free_transaction(transaction);
  597. } else {
  598. transaction->t_dropped = 1;
  599. }
  600. /* Just in case anybody was waiting for more transactions to be
  601. checkpointed... */
  602. wake_up(&journal->j_wait_logspace);
  603. ret = 1;
  604. out:
  605. return ret;
  606. }
  607. /*
  608. * journal_insert_checkpoint: put a committed buffer onto a checkpoint
  609. * list so that we know when it is safe to clean the transaction out of
  610. * the log.
  611. *
  612. * Called with the journal locked.
  613. * Called with j_list_lock held.
  614. */
  615. void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
  616. transaction_t *transaction)
  617. {
  618. JBUFFER_TRACE(jh, "entry");
  619. J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
  620. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  621. /* Get reference for checkpointing transaction */
  622. jbd2_journal_grab_journal_head(jh2bh(jh));
  623. jh->b_cp_transaction = transaction;
  624. if (!transaction->t_checkpoint_list) {
  625. jh->b_cpnext = jh->b_cpprev = jh;
  626. } else {
  627. jh->b_cpnext = transaction->t_checkpoint_list;
  628. jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
  629. jh->b_cpprev->b_cpnext = jh;
  630. jh->b_cpnext->b_cpprev = jh;
  631. }
  632. transaction->t_checkpoint_list = jh;
  633. }
  634. /*
  635. * We've finished with this transaction structure: adios...
  636. *
  637. * The transaction must have no links except for the checkpoint by this
  638. * point.
  639. *
  640. * Called with the journal locked.
  641. * Called with j_list_lock held.
  642. */
  643. void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
  644. {
  645. assert_spin_locked(&journal->j_list_lock);
  646. if (transaction->t_cpnext) {
  647. transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
  648. transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
  649. if (journal->j_checkpoint_transactions == transaction)
  650. journal->j_checkpoint_transactions =
  651. transaction->t_cpnext;
  652. if (journal->j_checkpoint_transactions == transaction)
  653. journal->j_checkpoint_transactions = NULL;
  654. }
  655. J_ASSERT(transaction->t_state == T_FINISHED);
  656. J_ASSERT(transaction->t_buffers == NULL);
  657. J_ASSERT(transaction->t_forget == NULL);
  658. J_ASSERT(transaction->t_shadow_list == NULL);
  659. J_ASSERT(transaction->t_checkpoint_list == NULL);
  660. J_ASSERT(transaction->t_checkpoint_io_list == NULL);
  661. J_ASSERT(atomic_read(&transaction->t_updates) == 0);
  662. J_ASSERT(journal->j_committing_transaction != transaction);
  663. J_ASSERT(journal->j_running_transaction != transaction);
  664. trace_jbd2_drop_transaction(journal, transaction);
  665. jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
  666. }