checkpoint.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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, -ENOSPC);
  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], JBD2_JOURNAL_REQ_FLAGS);
  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. if (WARN_ON_ONCE(buffer_jwrite(bh))) {
  253. put_bh(bh); /* drop the ref we just took */
  254. spin_unlock(&journal->j_list_lock);
  255. /* Clean up any previously batched buffers */
  256. if (batch_count)
  257. __flush_batch(journal, &batch_count);
  258. jbd2_journal_abort(journal, -EFSCORRUPTED);
  259. return -EFSCORRUPTED;
  260. }
  261. journal->j_chkpt_bhs[batch_count++] = bh;
  262. transaction->t_chp_stats.cs_written++;
  263. transaction->t_checkpoint_list = jh->b_cpnext;
  264. }
  265. if ((batch_count == JBD2_NR_BATCH) ||
  266. need_resched() || spin_needbreak(&journal->j_list_lock) ||
  267. jh2bh(transaction->t_checkpoint_list) == journal->j_chkpt_bhs[0])
  268. goto unlock_and_flush;
  269. }
  270. if (batch_count) {
  271. unlock_and_flush:
  272. spin_unlock(&journal->j_list_lock);
  273. retry:
  274. if (batch_count)
  275. __flush_batch(journal, &batch_count);
  276. cond_resched();
  277. spin_lock(&journal->j_list_lock);
  278. goto restart;
  279. }
  280. out:
  281. spin_unlock(&journal->j_list_lock);
  282. result = jbd2_cleanup_journal_tail(journal);
  283. return (result < 0) ? result : 0;
  284. }
  285. /*
  286. * Check the list of checkpoint transactions for the journal to see if
  287. * we have already got rid of any since the last update of the log tail
  288. * in the journal superblock. If so, we can instantly roll the
  289. * superblock forward to remove those transactions from the log.
  290. *
  291. * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
  292. *
  293. * Called with the journal lock held.
  294. *
  295. * This is the only part of the journaling code which really needs to be
  296. * aware of transaction aborts. Checkpointing involves writing to the
  297. * main filesystem area rather than to the journal, so it can proceed
  298. * even in abort state, but we must not update the super block if
  299. * checkpointing may have failed. Otherwise, we would lose some metadata
  300. * buffers which should be written-back to the filesystem.
  301. */
  302. int jbd2_cleanup_journal_tail(journal_t *journal)
  303. {
  304. tid_t first_tid;
  305. unsigned long blocknr;
  306. if (is_journal_aborted(journal))
  307. return -EIO;
  308. if (!jbd2_journal_get_log_tail(journal, &first_tid, &blocknr))
  309. return 1;
  310. if (WARN_ON_ONCE(blocknr == 0)) {
  311. jbd2_journal_abort(journal, -EFSCORRUPTED);
  312. return -EFSCORRUPTED;
  313. }
  314. /*
  315. * We need to make sure that any blocks that were recently written out
  316. * --- perhaps by jbd2_log_do_checkpoint() --- are flushed out before
  317. * we drop the transactions from the journal. It's unlikely this will
  318. * be necessary, especially with an appropriately sized journal, but we
  319. * need this to guarantee correctness. Fortunately
  320. * jbd2_cleanup_journal_tail() doesn't get called all that often.
  321. */
  322. if (journal->j_flags & JBD2_BARRIER)
  323. blkdev_issue_flush(journal->j_fs_dev);
  324. return __jbd2_update_log_tail(journal, first_tid, blocknr);
  325. }
  326. /* Checkpoint list management */
  327. /*
  328. * journal_shrink_one_cp_list
  329. *
  330. * Find all the written-back checkpoint buffers in the given list
  331. * and try to release them. If the whole transaction is released, set
  332. * the 'released' parameter. Return the number of released checkpointed
  333. * buffers.
  334. *
  335. * Called with j_list_lock held.
  336. */
  337. static unsigned long journal_shrink_one_cp_list(struct journal_head *jh,
  338. enum jbd2_shrink_type type,
  339. bool *released)
  340. {
  341. struct journal_head *last_jh;
  342. struct journal_head *next_jh = jh;
  343. unsigned long nr_freed = 0;
  344. int ret;
  345. *released = false;
  346. if (!jh)
  347. return 0;
  348. last_jh = jh->b_cpprev;
  349. do {
  350. jh = next_jh;
  351. next_jh = jh->b_cpnext;
  352. if (type == JBD2_SHRINK_DESTROY) {
  353. ret = __jbd2_journal_remove_checkpoint(jh);
  354. } else {
  355. ret = jbd2_journal_try_remove_checkpoint(jh);
  356. if (ret < 0) {
  357. if (type == JBD2_SHRINK_BUSY_SKIP)
  358. continue;
  359. break;
  360. }
  361. }
  362. nr_freed++;
  363. if (ret) {
  364. *released = true;
  365. break;
  366. }
  367. if (need_resched())
  368. break;
  369. } while (jh != last_jh);
  370. return nr_freed;
  371. }
  372. /*
  373. * jbd2_journal_shrink_checkpoint_list
  374. *
  375. * Find 'nr_to_scan' written-back checkpoint buffers in the journal
  376. * and try to release them. Return the number of released checkpointed
  377. * buffers.
  378. *
  379. * Called with j_list_lock held.
  380. */
  381. unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
  382. unsigned long *nr_to_scan)
  383. {
  384. transaction_t *transaction, *last_transaction, *next_transaction;
  385. bool __maybe_unused released;
  386. tid_t first_tid = 0, last_tid = 0, next_tid = 0;
  387. tid_t tid = 0;
  388. unsigned long nr_freed = 0;
  389. unsigned long freed;
  390. bool first_set = false;
  391. again:
  392. spin_lock(&journal->j_list_lock);
  393. if (!journal->j_checkpoint_transactions) {
  394. spin_unlock(&journal->j_list_lock);
  395. goto out;
  396. }
  397. /*
  398. * Get next shrink transaction, resume previous scan or start
  399. * over again. If some others do checkpoint and drop transaction
  400. * from the checkpoint list, we ignore saved j_shrink_transaction
  401. * and start over unconditionally.
  402. */
  403. if (journal->j_shrink_transaction)
  404. transaction = journal->j_shrink_transaction;
  405. else
  406. transaction = journal->j_checkpoint_transactions;
  407. if (!first_set) {
  408. first_tid = transaction->t_tid;
  409. first_set = true;
  410. }
  411. last_transaction = journal->j_checkpoint_transactions->t_cpprev;
  412. next_transaction = transaction;
  413. last_tid = last_transaction->t_tid;
  414. do {
  415. transaction = next_transaction;
  416. next_transaction = transaction->t_cpnext;
  417. tid = transaction->t_tid;
  418. freed = journal_shrink_one_cp_list(transaction->t_checkpoint_list,
  419. JBD2_SHRINK_BUSY_SKIP, &released);
  420. nr_freed += freed;
  421. (*nr_to_scan) -= min(*nr_to_scan, freed);
  422. if (*nr_to_scan == 0)
  423. break;
  424. if (need_resched() || spin_needbreak(&journal->j_list_lock))
  425. break;
  426. } while (transaction != last_transaction);
  427. if (transaction != last_transaction) {
  428. journal->j_shrink_transaction = next_transaction;
  429. next_tid = next_transaction->t_tid;
  430. } else {
  431. journal->j_shrink_transaction = NULL;
  432. next_tid = 0;
  433. }
  434. spin_unlock(&journal->j_list_lock);
  435. cond_resched();
  436. if (*nr_to_scan && journal->j_shrink_transaction)
  437. goto again;
  438. out:
  439. trace_jbd2_shrink_checkpoint_list(journal, first_tid, tid, last_tid,
  440. nr_freed, next_tid);
  441. return nr_freed;
  442. }
  443. /*
  444. * journal_clean_checkpoint_list
  445. *
  446. * Find all the written-back checkpoint buffers in the journal and release them.
  447. * If 'type' is JBD2_SHRINK_DESTROY, release all buffers unconditionally. If
  448. * 'type' is JBD2_SHRINK_BUSY_STOP, will stop release buffers if encounters a
  449. * busy buffer. To avoid wasting CPU cycles scanning the buffer list in some
  450. * cases, don't pass JBD2_SHRINK_BUSY_SKIP 'type' for this function.
  451. *
  452. * Called with j_list_lock held.
  453. */
  454. void __jbd2_journal_clean_checkpoint_list(journal_t *journal,
  455. enum jbd2_shrink_type type)
  456. {
  457. transaction_t *transaction, *last_transaction, *next_transaction;
  458. bool released;
  459. WARN_ON_ONCE(type == JBD2_SHRINK_BUSY_SKIP);
  460. transaction = journal->j_checkpoint_transactions;
  461. if (!transaction)
  462. return;
  463. last_transaction = transaction->t_cpprev;
  464. next_transaction = transaction;
  465. do {
  466. transaction = next_transaction;
  467. next_transaction = transaction->t_cpnext;
  468. journal_shrink_one_cp_list(transaction->t_checkpoint_list,
  469. type, &released);
  470. /*
  471. * This function only frees up some memory if possible so we
  472. * dont have an obligation to finish processing. Bail out if
  473. * preemption requested:
  474. */
  475. if (need_resched())
  476. return;
  477. /*
  478. * Stop scanning if we couldn't free the transaction. This
  479. * avoids pointless scanning of transactions which still
  480. * weren't checkpointed.
  481. */
  482. if (!released)
  483. return;
  484. } while (transaction != last_transaction);
  485. }
  486. /*
  487. * Remove buffers from all checkpoint lists as journal is aborted and we just
  488. * need to free memory
  489. */
  490. void jbd2_journal_destroy_checkpoint(journal_t *journal)
  491. {
  492. /*
  493. * We loop because __jbd2_journal_clean_checkpoint_list() may abort
  494. * early due to a need of rescheduling.
  495. */
  496. while (1) {
  497. spin_lock(&journal->j_list_lock);
  498. if (!journal->j_checkpoint_transactions) {
  499. spin_unlock(&journal->j_list_lock);
  500. break;
  501. }
  502. __jbd2_journal_clean_checkpoint_list(journal, JBD2_SHRINK_DESTROY);
  503. spin_unlock(&journal->j_list_lock);
  504. cond_resched();
  505. }
  506. }
  507. /*
  508. * journal_remove_checkpoint: called after a buffer has been committed
  509. * to disk (either by being write-back flushed to disk, or being
  510. * committed to the log).
  511. *
  512. * We cannot safely clean a transaction out of the log until all of the
  513. * buffer updates committed in that transaction have safely been stored
  514. * elsewhere on disk. To achieve this, all of the buffers in a
  515. * transaction need to be maintained on the transaction's checkpoint
  516. * lists until they have been rewritten, at which point this function is
  517. * called to remove the buffer from the existing transaction's
  518. * checkpoint lists.
  519. *
  520. * The function returns 1 if it frees the transaction, 0 otherwise.
  521. * The function can free jh and bh.
  522. *
  523. * This function is called with j_list_lock held.
  524. */
  525. int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
  526. {
  527. struct transaction_chp_stats_s *stats;
  528. transaction_t *transaction;
  529. journal_t *journal;
  530. JBUFFER_TRACE(jh, "entry");
  531. transaction = jh->b_cp_transaction;
  532. if (!transaction) {
  533. JBUFFER_TRACE(jh, "not on transaction");
  534. return 0;
  535. }
  536. journal = transaction->t_journal;
  537. JBUFFER_TRACE(jh, "removing from transaction");
  538. __buffer_unlink(jh);
  539. jh->b_cp_transaction = NULL;
  540. percpu_counter_dec(&journal->j_checkpoint_jh_count);
  541. jbd2_journal_put_journal_head(jh);
  542. /* Is this transaction empty? */
  543. if (transaction->t_checkpoint_list)
  544. return 0;
  545. /*
  546. * There is one special case to worry about: if we have just pulled the
  547. * buffer off a running or committing transaction's checkpoing list,
  548. * then even if the checkpoint list is empty, the transaction obviously
  549. * cannot be dropped!
  550. *
  551. * The locking here around t_state is a bit sleazy.
  552. * See the comment at the end of jbd2_journal_commit_transaction().
  553. */
  554. if (transaction->t_state != T_FINISHED)
  555. return 0;
  556. /*
  557. * OK, that was the last buffer for the transaction, we can now
  558. * safely remove this transaction from the log.
  559. */
  560. stats = &transaction->t_chp_stats;
  561. if (stats->cs_chp_time)
  562. stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
  563. jiffies);
  564. trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
  565. transaction->t_tid, stats);
  566. __jbd2_journal_drop_transaction(journal, transaction);
  567. jbd2_journal_free_transaction(transaction);
  568. return 1;
  569. }
  570. /*
  571. * Check the checkpoint buffer and try to remove it from the checkpoint
  572. * list if it's clean. Returns -EBUSY if it is not clean, returns 1 if
  573. * it frees the transaction, 0 otherwise.
  574. *
  575. * This function is called with j_list_lock held.
  576. */
  577. int jbd2_journal_try_remove_checkpoint(struct journal_head *jh)
  578. {
  579. struct buffer_head *bh = jh2bh(jh);
  580. if (jh->b_transaction)
  581. return -EBUSY;
  582. if (!trylock_buffer(bh))
  583. return -EBUSY;
  584. if (buffer_dirty(bh)) {
  585. unlock_buffer(bh);
  586. return -EBUSY;
  587. }
  588. unlock_buffer(bh);
  589. /*
  590. * Buffer is clean and the IO has finished (we held the buffer
  591. * lock) so the checkpoint is done. We can safely remove the
  592. * buffer from this transaction.
  593. */
  594. JBUFFER_TRACE(jh, "remove from checkpoint list");
  595. return __jbd2_journal_remove_checkpoint(jh);
  596. }
  597. /*
  598. * journal_insert_checkpoint: put a committed buffer onto a checkpoint
  599. * list so that we know when it is safe to clean the transaction out of
  600. * the log.
  601. *
  602. * Called with the journal locked.
  603. * Called with j_list_lock held.
  604. */
  605. void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
  606. transaction_t *transaction)
  607. {
  608. JBUFFER_TRACE(jh, "entry");
  609. J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
  610. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  611. /* Get reference for checkpointing transaction */
  612. jbd2_journal_grab_journal_head(jh2bh(jh));
  613. jh->b_cp_transaction = transaction;
  614. if (!transaction->t_checkpoint_list) {
  615. jh->b_cpnext = jh->b_cpprev = jh;
  616. } else {
  617. jh->b_cpnext = transaction->t_checkpoint_list;
  618. jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
  619. jh->b_cpprev->b_cpnext = jh;
  620. jh->b_cpnext->b_cpprev = jh;
  621. }
  622. transaction->t_checkpoint_list = jh;
  623. percpu_counter_inc(&transaction->t_journal->j_checkpoint_jh_count);
  624. }
  625. /*
  626. * We've finished with this transaction structure: adios...
  627. *
  628. * The transaction must have no links except for the checkpoint by this
  629. * point.
  630. *
  631. * Called with the journal locked.
  632. * Called with j_list_lock held.
  633. */
  634. void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
  635. {
  636. assert_spin_locked(&journal->j_list_lock);
  637. journal->j_shrink_transaction = NULL;
  638. if (transaction->t_cpnext) {
  639. transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
  640. transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
  641. if (journal->j_checkpoint_transactions == transaction)
  642. journal->j_checkpoint_transactions =
  643. transaction->t_cpnext;
  644. if (journal->j_checkpoint_transactions == transaction)
  645. journal->j_checkpoint_transactions = NULL;
  646. }
  647. J_ASSERT(transaction->t_state == T_FINISHED);
  648. J_ASSERT(transaction->t_buffers == NULL);
  649. J_ASSERT(transaction->t_forget == NULL);
  650. J_ASSERT(transaction->t_shadow_list == NULL);
  651. J_ASSERT(transaction->t_checkpoint_list == NULL);
  652. J_ASSERT(atomic_read(&transaction->t_updates) == 0);
  653. J_ASSERT(journal->j_committing_transaction != transaction);
  654. J_ASSERT(journal->j_running_transaction != transaction);
  655. trace_jbd2_drop_transaction(journal, transaction);
  656. jbd2_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
  657. }