checkpoint.c 21 KB

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