checkpoint.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  83. */
  84. static int __try_to_free_cp_buf(struct journal_head *jh)
  85. {
  86. int ret = 0;
  87. struct buffer_head *bh = jh2bh(jh);
  88. if (jh->b_jlist == BJ_None && !buffer_locked(bh) &&
  89. !buffer_dirty(bh) && !buffer_write_io_error(bh)) {
  90. JBUFFER_TRACE(jh, "remove from checkpoint list");
  91. ret = __jbd2_journal_remove_checkpoint(jh) + 1;
  92. jbd_unlock_bh_state(bh);
  93. jbd2_journal_remove_journal_head(bh);
  94. BUFFER_TRACE(bh, "release");
  95. __brelse(bh);
  96. } else {
  97. jbd_unlock_bh_state(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. * We were unable to perform jbd_trylock_bh_state() inside j_list_lock.
  165. * The caller must restart a list walk. Wait for someone else to run
  166. * jbd_unlock_bh_state().
  167. */
  168. static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh)
  169. __releases(journal->j_list_lock)
  170. {
  171. get_bh(bh);
  172. spin_unlock(&journal->j_list_lock);
  173. jbd_lock_bh_state(bh);
  174. jbd_unlock_bh_state(bh);
  175. put_bh(bh);
  176. }
  177. /*
  178. * Clean up transaction's list of buffers submitted for io.
  179. * We wait for any pending IO to complete and remove any clean
  180. * buffers. Note that we take the buffers in the opposite ordering
  181. * from the one in which they were submitted for IO.
  182. *
  183. * Return 0 on success, and return <0 if some buffers have failed
  184. * to be written out.
  185. *
  186. * Called with j_list_lock held.
  187. */
  188. static int __wait_cp_io(journal_t *journal, transaction_t *transaction)
  189. {
  190. struct journal_head *jh;
  191. struct buffer_head *bh;
  192. tid_t this_tid;
  193. int released = 0;
  194. int ret = 0;
  195. this_tid = transaction->t_tid;
  196. restart:
  197. /* Did somebody clean up the transaction in the meanwhile? */
  198. if (journal->j_checkpoint_transactions != transaction ||
  199. transaction->t_tid != this_tid)
  200. return ret;
  201. while (!released && transaction->t_checkpoint_io_list) {
  202. jh = transaction->t_checkpoint_io_list;
  203. bh = jh2bh(jh);
  204. if (!jbd_trylock_bh_state(bh)) {
  205. jbd_sync_bh(journal, bh);
  206. spin_lock(&journal->j_list_lock);
  207. goto restart;
  208. }
  209. if (buffer_locked(bh)) {
  210. atomic_inc(&bh->b_count);
  211. spin_unlock(&journal->j_list_lock);
  212. jbd_unlock_bh_state(bh);
  213. wait_on_buffer(bh);
  214. /* the journal_head may have gone by now */
  215. BUFFER_TRACE(bh, "brelse");
  216. __brelse(bh);
  217. spin_lock(&journal->j_list_lock);
  218. goto restart;
  219. }
  220. if (unlikely(buffer_write_io_error(bh)))
  221. ret = -EIO;
  222. /*
  223. * Now in whatever state the buffer currently is, we know that
  224. * it has been written out and so we can drop it from the list
  225. */
  226. released = __jbd2_journal_remove_checkpoint(jh);
  227. jbd_unlock_bh_state(bh);
  228. jbd2_journal_remove_journal_head(bh);
  229. __brelse(bh);
  230. }
  231. return ret;
  232. }
  233. static void
  234. __flush_batch(journal_t *journal, int *batch_count)
  235. {
  236. int i;
  237. for (i = 0; i < *batch_count; i++)
  238. write_dirty_buffer(journal->j_chkpt_bhs[i], WRITE);
  239. for (i = 0; i < *batch_count; i++) {
  240. struct buffer_head *bh = journal->j_chkpt_bhs[i];
  241. clear_buffer_jwrite(bh);
  242. BUFFER_TRACE(bh, "brelse");
  243. __brelse(bh);
  244. }
  245. *batch_count = 0;
  246. }
  247. /*
  248. * Try to flush one buffer from the checkpoint list to disk.
  249. *
  250. * Return 1 if something happened which requires us to abort the current
  251. * scan of the checkpoint list. Return <0 if the buffer has failed to
  252. * be written out.
  253. *
  254. * Called with j_list_lock held and drops it if 1 is returned
  255. * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  256. */
  257. static int __process_buffer(journal_t *journal, struct journal_head *jh,
  258. int *batch_count, transaction_t *transaction)
  259. {
  260. struct buffer_head *bh = jh2bh(jh);
  261. int ret = 0;
  262. if (buffer_locked(bh)) {
  263. atomic_inc(&bh->b_count);
  264. spin_unlock(&journal->j_list_lock);
  265. jbd_unlock_bh_state(bh);
  266. wait_on_buffer(bh);
  267. /* the journal_head may have gone by now */
  268. BUFFER_TRACE(bh, "brelse");
  269. __brelse(bh);
  270. ret = 1;
  271. } else if (jh->b_transaction != NULL) {
  272. transaction_t *t = jh->b_transaction;
  273. tid_t tid = t->t_tid;
  274. transaction->t_chp_stats.cs_forced_to_close++;
  275. spin_unlock(&journal->j_list_lock);
  276. jbd_unlock_bh_state(bh);
  277. jbd2_log_start_commit(journal, tid);
  278. jbd2_log_wait_commit(journal, tid);
  279. ret = 1;
  280. } else if (!buffer_dirty(bh)) {
  281. ret = 1;
  282. if (unlikely(buffer_write_io_error(bh)))
  283. ret = -EIO;
  284. J_ASSERT_JH(jh, !buffer_jbddirty(bh));
  285. BUFFER_TRACE(bh, "remove from checkpoint");
  286. __jbd2_journal_remove_checkpoint(jh);
  287. spin_unlock(&journal->j_list_lock);
  288. jbd_unlock_bh_state(bh);
  289. jbd2_journal_remove_journal_head(bh);
  290. __brelse(bh);
  291. } else {
  292. /*
  293. * Important: we are about to write the buffer, and
  294. * possibly block, while still holding the journal lock.
  295. * We cannot afford to let the transaction logic start
  296. * messing around with this buffer before we write it to
  297. * disk, as that would break recoverability.
  298. */
  299. BUFFER_TRACE(bh, "queue");
  300. get_bh(bh);
  301. J_ASSERT_BH(bh, !buffer_jwrite(bh));
  302. set_buffer_jwrite(bh);
  303. journal->j_chkpt_bhs[*batch_count] = bh;
  304. __buffer_relink_io(jh);
  305. jbd_unlock_bh_state(bh);
  306. transaction->t_chp_stats.cs_written++;
  307. (*batch_count)++;
  308. if (*batch_count == JBD2_NR_BATCH) {
  309. spin_unlock(&journal->j_list_lock);
  310. __flush_batch(journal, batch_count);
  311. ret = 1;
  312. }
  313. }
  314. return ret;
  315. }
  316. /*
  317. * Perform an actual checkpoint. We take the first transaction on the
  318. * list of transactions to be checkpointed and send all its buffers
  319. * to disk. We submit larger chunks of data at once.
  320. *
  321. * The journal should be locked before calling this function.
  322. * Called with j_checkpoint_mutex held.
  323. */
  324. int jbd2_log_do_checkpoint(journal_t *journal)
  325. {
  326. transaction_t *transaction;
  327. tid_t this_tid;
  328. int result;
  329. jbd_debug(1, "Start checkpoint\n");
  330. /*
  331. * First thing: if there are any transactions in the log which
  332. * don't need checkpointing, just eliminate them from the
  333. * journal straight away.
  334. */
  335. result = jbd2_cleanup_journal_tail(journal);
  336. trace_jbd2_checkpoint(journal, result);
  337. jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
  338. if (result <= 0)
  339. return result;
  340. /*
  341. * OK, we need to start writing disk blocks. Take one transaction
  342. * and write it.
  343. */
  344. result = 0;
  345. spin_lock(&journal->j_list_lock);
  346. if (!journal->j_checkpoint_transactions)
  347. goto out;
  348. transaction = journal->j_checkpoint_transactions;
  349. if (transaction->t_chp_stats.cs_chp_time == 0)
  350. transaction->t_chp_stats.cs_chp_time = jiffies;
  351. this_tid = transaction->t_tid;
  352. restart:
  353. /*
  354. * If someone cleaned up this transaction while we slept, we're
  355. * done (maybe it's a new transaction, but it fell at the same
  356. * address).
  357. */
  358. if (journal->j_checkpoint_transactions == transaction &&
  359. transaction->t_tid == this_tid) {
  360. int batch_count = 0;
  361. struct journal_head *jh;
  362. int retry = 0, err;
  363. while (!retry && transaction->t_checkpoint_list) {
  364. struct buffer_head *bh;
  365. jh = transaction->t_checkpoint_list;
  366. bh = jh2bh(jh);
  367. if (!jbd_trylock_bh_state(bh)) {
  368. jbd_sync_bh(journal, bh);
  369. retry = 1;
  370. break;
  371. }
  372. retry = __process_buffer(journal, jh, &batch_count,
  373. transaction);
  374. if (retry < 0 && !result)
  375. result = retry;
  376. if (!retry && (need_resched() ||
  377. spin_needbreak(&journal->j_list_lock))) {
  378. spin_unlock(&journal->j_list_lock);
  379. retry = 1;
  380. break;
  381. }
  382. }
  383. if (batch_count) {
  384. if (!retry) {
  385. spin_unlock(&journal->j_list_lock);
  386. retry = 1;
  387. }
  388. __flush_batch(journal, &batch_count);
  389. }
  390. if (retry) {
  391. spin_lock(&journal->j_list_lock);
  392. goto restart;
  393. }
  394. /*
  395. * Now we have cleaned up the first transaction's checkpoint
  396. * list. Let's clean up the second one
  397. */
  398. err = __wait_cp_io(journal, transaction);
  399. if (!result)
  400. result = err;
  401. }
  402. out:
  403. spin_unlock(&journal->j_list_lock);
  404. if (result < 0)
  405. jbd2_journal_abort(journal, result);
  406. else
  407. result = jbd2_cleanup_journal_tail(journal);
  408. return (result < 0) ? result : 0;
  409. }
  410. /*
  411. * Check the list of checkpoint transactions for the journal to see if
  412. * we have already got rid of any since the last update of the log tail
  413. * in the journal superblock. If so, we can instantly roll the
  414. * superblock forward to remove those transactions from the log.
  415. *
  416. * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
  417. *
  418. * Called with the journal lock held.
  419. *
  420. * This is the only part of the journaling code which really needs to be
  421. * aware of transaction aborts. Checkpointing involves writing to the
  422. * main filesystem area rather than to the journal, so it can proceed
  423. * even in abort state, but we must not update the super block if
  424. * checkpointing may have failed. Otherwise, we would lose some metadata
  425. * buffers which should be written-back to the filesystem.
  426. */
  427. int jbd2_cleanup_journal_tail(journal_t *journal)
  428. {
  429. transaction_t * transaction;
  430. tid_t first_tid;
  431. unsigned long blocknr, freed;
  432. if (is_journal_aborted(journal))
  433. return 1;
  434. /* OK, work out the oldest transaction remaining in the log, and
  435. * the log block it starts at.
  436. *
  437. * If the log is now empty, we need to work out which is the
  438. * next transaction ID we will write, and where it will
  439. * start. */
  440. write_lock(&journal->j_state_lock);
  441. spin_lock(&journal->j_list_lock);
  442. transaction = journal->j_checkpoint_transactions;
  443. if (transaction) {
  444. first_tid = transaction->t_tid;
  445. blocknr = transaction->t_log_start;
  446. } else if ((transaction = journal->j_committing_transaction) != NULL) {
  447. first_tid = transaction->t_tid;
  448. blocknr = transaction->t_log_start;
  449. } else if ((transaction = journal->j_running_transaction) != NULL) {
  450. first_tid = transaction->t_tid;
  451. blocknr = journal->j_head;
  452. } else {
  453. first_tid = journal->j_transaction_sequence;
  454. blocknr = journal->j_head;
  455. }
  456. spin_unlock(&journal->j_list_lock);
  457. J_ASSERT(blocknr != 0);
  458. /* If the oldest pinned transaction is at the tail of the log
  459. already then there's not much we can do right now. */
  460. if (journal->j_tail_sequence == first_tid) {
  461. write_unlock(&journal->j_state_lock);
  462. return 1;
  463. }
  464. /* OK, update the superblock to recover the freed space.
  465. * Physical blocks come first: have we wrapped beyond the end of
  466. * the log? */
  467. freed = blocknr - journal->j_tail;
  468. if (blocknr < journal->j_tail)
  469. freed = freed + journal->j_last - journal->j_first;
  470. trace_jbd2_cleanup_journal_tail(journal, first_tid, blocknr, freed);
  471. jbd_debug(1,
  472. "Cleaning journal tail from %d to %d (offset %lu), "
  473. "freeing %lu\n",
  474. journal->j_tail_sequence, first_tid, blocknr, freed);
  475. journal->j_free += freed;
  476. journal->j_tail_sequence = first_tid;
  477. journal->j_tail = blocknr;
  478. write_unlock(&journal->j_state_lock);
  479. /*
  480. * If there is an external journal, we need to make sure that
  481. * any data blocks that were recently written out --- perhaps
  482. * by jbd2_log_do_checkpoint() --- are flushed out before we
  483. * drop the transactions from the external journal. It's
  484. * unlikely this will be necessary, especially with a
  485. * appropriately sized journal, but we need this to guarantee
  486. * correctness. Fortunately jbd2_cleanup_journal_tail()
  487. * doesn't get called all that often.
  488. */
  489. if ((journal->j_fs_dev != journal->j_dev) &&
  490. (journal->j_flags & JBD2_BARRIER))
  491. blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL,
  492. BLKDEV_IFL_WAIT);
  493. if (!(journal->j_flags & JBD2_ABORT))
  494. jbd2_journal_update_superblock(journal, 1);
  495. return 0;
  496. }
  497. /* Checkpoint list management */
  498. /*
  499. * journal_clean_one_cp_list
  500. *
  501. * Find all the written-back checkpoint buffers in the given list and release them.
  502. *
  503. * Called with the journal locked.
  504. * Called with j_list_lock held.
  505. * Returns number of bufers reaped (for debug)
  506. */
  507. static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
  508. {
  509. struct journal_head *last_jh;
  510. struct journal_head *next_jh = jh;
  511. int ret, freed = 0;
  512. *released = 0;
  513. if (!jh)
  514. return 0;
  515. last_jh = jh->b_cpprev;
  516. do {
  517. jh = next_jh;
  518. next_jh = jh->b_cpnext;
  519. /* Use trylock because of the ranking */
  520. if (jbd_trylock_bh_state(jh2bh(jh))) {
  521. ret = __try_to_free_cp_buf(jh);
  522. if (ret) {
  523. freed++;
  524. if (ret == 2) {
  525. *released = 1;
  526. return freed;
  527. }
  528. }
  529. }
  530. /*
  531. * This function only frees up some memory
  532. * if possible so we dont have an obligation
  533. * to finish processing. Bail out if preemption
  534. * requested:
  535. */
  536. if (need_resched())
  537. return freed;
  538. } while (jh != last_jh);
  539. return freed;
  540. }
  541. /*
  542. * journal_clean_checkpoint_list
  543. *
  544. * Find all the written-back checkpoint buffers in the journal and release them.
  545. *
  546. * Called with the journal locked.
  547. * Called with j_list_lock held.
  548. * Returns number of buffers reaped (for debug)
  549. */
  550. int __jbd2_journal_clean_checkpoint_list(journal_t *journal)
  551. {
  552. transaction_t *transaction, *last_transaction, *next_transaction;
  553. int ret = 0;
  554. int released;
  555. transaction = journal->j_checkpoint_transactions;
  556. if (!transaction)
  557. goto out;
  558. last_transaction = transaction->t_cpprev;
  559. next_transaction = transaction;
  560. do {
  561. transaction = next_transaction;
  562. next_transaction = transaction->t_cpnext;
  563. ret += journal_clean_one_cp_list(transaction->
  564. t_checkpoint_list, &released);
  565. /*
  566. * This function only frees up some memory if possible so we
  567. * dont have an obligation to finish processing. Bail out if
  568. * preemption requested:
  569. */
  570. if (need_resched())
  571. goto out;
  572. if (released)
  573. continue;
  574. /*
  575. * It is essential that we are as careful as in the case of
  576. * t_checkpoint_list with removing the buffer from the list as
  577. * we can possibly see not yet submitted buffers on io_list
  578. */
  579. ret += journal_clean_one_cp_list(transaction->
  580. t_checkpoint_io_list, &released);
  581. if (need_resched())
  582. goto out;
  583. } while (transaction != last_transaction);
  584. out:
  585. return ret;
  586. }
  587. /*
  588. * journal_remove_checkpoint: called after a buffer has been committed
  589. * to disk (either by being write-back flushed to disk, or being
  590. * committed to the log).
  591. *
  592. * We cannot safely clean a transaction out of the log until all of the
  593. * buffer updates committed in that transaction have safely been stored
  594. * elsewhere on disk. To achieve this, all of the buffers in a
  595. * transaction need to be maintained on the transaction's checkpoint
  596. * lists until they have been rewritten, at which point this function is
  597. * called to remove the buffer from the existing transaction's
  598. * checkpoint lists.
  599. *
  600. * The function returns 1 if it frees the transaction, 0 otherwise.
  601. *
  602. * This function is called with the journal locked.
  603. * This function is called with j_list_lock held.
  604. * This function is called with jbd_lock_bh_state(jh2bh(jh))
  605. */
  606. int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
  607. {
  608. struct transaction_chp_stats_s *stats;
  609. transaction_t *transaction;
  610. journal_t *journal;
  611. int ret = 0;
  612. JBUFFER_TRACE(jh, "entry");
  613. if ((transaction = jh->b_cp_transaction) == NULL) {
  614. JBUFFER_TRACE(jh, "not on transaction");
  615. goto out;
  616. }
  617. journal = transaction->t_journal;
  618. __buffer_unlink(jh);
  619. jh->b_cp_transaction = NULL;
  620. if (transaction->t_checkpoint_list != NULL ||
  621. transaction->t_checkpoint_io_list != NULL)
  622. goto out;
  623. JBUFFER_TRACE(jh, "transaction has no more buffers");
  624. /*
  625. * There is one special case to worry about: if we have just pulled the
  626. * buffer off a running or committing transaction's checkpoing list,
  627. * then even if the checkpoint list is empty, the transaction obviously
  628. * cannot be dropped!
  629. *
  630. * The locking here around t_state is a bit sleazy.
  631. * See the comment at the end of jbd2_journal_commit_transaction().
  632. */
  633. if (transaction->t_state != T_FINISHED) {
  634. JBUFFER_TRACE(jh, "belongs to running/committing transaction");
  635. goto out;
  636. }
  637. /* OK, that was the last buffer for the transaction: we can now
  638. safely remove this transaction from the log */
  639. stats = &transaction->t_chp_stats;
  640. if (stats->cs_chp_time)
  641. stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
  642. jiffies);
  643. trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
  644. transaction->t_tid, stats);
  645. __jbd2_journal_drop_transaction(journal, transaction);
  646. kfree(transaction);
  647. /* Just in case anybody was waiting for more transactions to be
  648. checkpointed... */
  649. wake_up(&journal->j_wait_logspace);
  650. ret = 1;
  651. out:
  652. JBUFFER_TRACE(jh, "exit");
  653. return ret;
  654. }
  655. /*
  656. * journal_insert_checkpoint: put a committed buffer onto a checkpoint
  657. * list so that we know when it is safe to clean the transaction out of
  658. * the log.
  659. *
  660. * Called with the journal locked.
  661. * Called with j_list_lock held.
  662. */
  663. void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
  664. transaction_t *transaction)
  665. {
  666. JBUFFER_TRACE(jh, "entry");
  667. J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
  668. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  669. jh->b_cp_transaction = transaction;
  670. if (!transaction->t_checkpoint_list) {
  671. jh->b_cpnext = jh->b_cpprev = jh;
  672. } else {
  673. jh->b_cpnext = transaction->t_checkpoint_list;
  674. jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
  675. jh->b_cpprev->b_cpnext = jh;
  676. jh->b_cpnext->b_cpprev = jh;
  677. }
  678. transaction->t_checkpoint_list = jh;
  679. }
  680. /*
  681. * We've finished with this transaction structure: adios...
  682. *
  683. * The transaction must have no links except for the checkpoint by this
  684. * point.
  685. *
  686. * Called with the journal locked.
  687. * Called with j_list_lock held.
  688. */
  689. void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
  690. {
  691. assert_spin_locked(&journal->j_list_lock);
  692. if (transaction->t_cpnext) {
  693. transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
  694. transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
  695. if (journal->j_checkpoint_transactions == transaction)
  696. journal->j_checkpoint_transactions =
  697. transaction->t_cpnext;
  698. if (journal->j_checkpoint_transactions == transaction)
  699. journal->j_checkpoint_transactions = NULL;
  700. }
  701. J_ASSERT(transaction->t_state == T_FINISHED);
  702. J_ASSERT(transaction->t_buffers == NULL);
  703. J_ASSERT(transaction->t_forget == NULL);
  704. J_ASSERT(transaction->t_iobuf_list == NULL);
  705. J_ASSERT(transaction->t_shadow_list == NULL);
  706. J_ASSERT(transaction->t_log_list == NULL);
  707. J_ASSERT(transaction->t_checkpoint_list == NULL);
  708. J_ASSERT(transaction->t_checkpoint_io_list == NULL);
  709. J_ASSERT(atomic_read(&transaction->t_updates) == 0);
  710. J_ASSERT(journal->j_committing_transaction != transaction);
  711. J_ASSERT(journal->j_running_transaction != transaction);
  712. jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
  713. }