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