checkpoint.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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/marker.h>
  23. #include <linux/errno.h>
  24. #include <linux/slab.h>
  25. /*
  26. * Unlink a buffer from a transaction checkpoint list.
  27. *
  28. * Called with j_list_lock held.
  29. */
  30. static inline void __buffer_unlink_first(struct journal_head *jh)
  31. {
  32. transaction_t *transaction = jh->b_cp_transaction;
  33. jh->b_cpnext->b_cpprev = jh->b_cpprev;
  34. jh->b_cpprev->b_cpnext = jh->b_cpnext;
  35. if (transaction->t_checkpoint_list == jh) {
  36. transaction->t_checkpoint_list = jh->b_cpnext;
  37. if (transaction->t_checkpoint_list == jh)
  38. transaction->t_checkpoint_list = NULL;
  39. }
  40. }
  41. /*
  42. * Unlink a buffer from a transaction checkpoint(io) list.
  43. *
  44. * Called with j_list_lock held.
  45. */
  46. static inline void __buffer_unlink(struct journal_head *jh)
  47. {
  48. transaction_t *transaction = jh->b_cp_transaction;
  49. __buffer_unlink_first(jh);
  50. if (transaction->t_checkpoint_io_list == jh) {
  51. transaction->t_checkpoint_io_list = jh->b_cpnext;
  52. if (transaction->t_checkpoint_io_list == jh)
  53. transaction->t_checkpoint_io_list = NULL;
  54. }
  55. }
  56. /*
  57. * Move a buffer from the checkpoint list to the checkpoint io list
  58. *
  59. * Called with j_list_lock held
  60. */
  61. static inline void __buffer_relink_io(struct journal_head *jh)
  62. {
  63. transaction_t *transaction = jh->b_cp_transaction;
  64. __buffer_unlink_first(jh);
  65. if (!transaction->t_checkpoint_io_list) {
  66. jh->b_cpnext = jh->b_cpprev = jh;
  67. } else {
  68. jh->b_cpnext = transaction->t_checkpoint_io_list;
  69. jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev;
  70. jh->b_cpprev->b_cpnext = jh;
  71. jh->b_cpnext->b_cpprev = jh;
  72. }
  73. transaction->t_checkpoint_io_list = jh;
  74. }
  75. /*
  76. * Try to release a checkpointed buffer from its transaction.
  77. * Returns 1 if we released it and 2 if we also released the
  78. * whole transaction.
  79. *
  80. * Requires j_list_lock
  81. * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  82. */
  83. static int __try_to_free_cp_buf(struct journal_head *jh)
  84. {
  85. int ret = 0;
  86. struct buffer_head *bh = jh2bh(jh);
  87. if (jh->b_jlist == BJ_None && !buffer_locked(bh) &&
  88. !buffer_dirty(bh) && !buffer_write_io_error(bh)) {
  89. JBUFFER_TRACE(jh, "remove from checkpoint list");
  90. ret = __jbd2_journal_remove_checkpoint(jh) + 1;
  91. jbd_unlock_bh_state(bh);
  92. jbd2_journal_remove_journal_head(bh);
  93. BUFFER_TRACE(bh, "release");
  94. __brelse(bh);
  95. } else {
  96. jbd_unlock_bh_state(bh);
  97. }
  98. return ret;
  99. }
  100. /*
  101. * __jbd2_log_wait_for_space: wait until there is space in the journal.
  102. *
  103. * Called under j-state_lock *only*. It will be unlocked if we have to wait
  104. * for a checkpoint to free up some space in the log.
  105. */
  106. void __jbd2_log_wait_for_space(journal_t *journal)
  107. {
  108. int nblocks, space_left;
  109. assert_spin_locked(&journal->j_state_lock);
  110. nblocks = jbd_space_needed(journal);
  111. while (__jbd2_log_space_left(journal) < nblocks) {
  112. if (journal->j_flags & JBD2_ABORT)
  113. return;
  114. spin_unlock(&journal->j_state_lock);
  115. mutex_lock(&journal->j_checkpoint_mutex);
  116. /*
  117. * Test again, another process may have checkpointed while we
  118. * were waiting for the checkpoint lock. If there are no
  119. * transactions ready to be checkpointed, try to recover
  120. * journal space by calling cleanup_journal_tail(), and if
  121. * that doesn't work, by waiting for the currently committing
  122. * transaction to complete. If there is absolutely no way
  123. * to make progress, this is either a BUG or corrupted
  124. * filesystem, so abort the journal and leave a stack
  125. * trace for forensic evidence.
  126. */
  127. spin_lock(&journal->j_state_lock);
  128. spin_lock(&journal->j_list_lock);
  129. nblocks = jbd_space_needed(journal);
  130. space_left = __jbd2_log_space_left(journal);
  131. if (space_left < nblocks) {
  132. int chkpt = journal->j_checkpoint_transactions != NULL;
  133. tid_t tid = 0;
  134. if (journal->j_committing_transaction)
  135. tid = journal->j_committing_transaction->t_tid;
  136. spin_unlock(&journal->j_list_lock);
  137. spin_unlock(&journal->j_state_lock);
  138. if (chkpt) {
  139. jbd2_log_do_checkpoint(journal);
  140. } else if (jbd2_cleanup_journal_tail(journal) == 0) {
  141. /* We were able to recover space; yay! */
  142. ;
  143. } else if (tid) {
  144. jbd2_log_wait_commit(journal, tid);
  145. } else {
  146. printk(KERN_ERR "%s: needed %d blocks and "
  147. "only had %d space available\n",
  148. __func__, nblocks, space_left);
  149. printk(KERN_ERR "%s: no way to get more "
  150. "journal space in %s\n", __func__,
  151. journal->j_devname);
  152. WARN_ON(1);
  153. jbd2_journal_abort(journal, 0);
  154. }
  155. spin_lock(&journal->j_state_lock);
  156. } else {
  157. spin_unlock(&journal->j_list_lock);
  158. }
  159. mutex_unlock(&journal->j_checkpoint_mutex);
  160. }
  161. }
  162. /*
  163. * We were unable to perform jbd_trylock_bh_state() inside j_list_lock.
  164. * The caller must restart a list walk. Wait for someone else to run
  165. * jbd_unlock_bh_state().
  166. */
  167. static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh)
  168. __releases(journal->j_list_lock)
  169. {
  170. get_bh(bh);
  171. spin_unlock(&journal->j_list_lock);
  172. jbd_lock_bh_state(bh);
  173. jbd_unlock_bh_state(bh);
  174. put_bh(bh);
  175. }
  176. /*
  177. * Clean up transaction's list of buffers submitted for io.
  178. * We wait for any pending IO to complete and remove any clean
  179. * buffers. Note that we take the buffers in the opposite ordering
  180. * from the one in which they were submitted for IO.
  181. *
  182. * Return 0 on success, and return <0 if some buffers have failed
  183. * to be written out.
  184. *
  185. * Called with j_list_lock held.
  186. */
  187. static int __wait_cp_io(journal_t *journal, transaction_t *transaction)
  188. {
  189. struct journal_head *jh;
  190. struct buffer_head *bh;
  191. tid_t this_tid;
  192. int released = 0;
  193. int ret = 0;
  194. this_tid = transaction->t_tid;
  195. restart:
  196. /* Did somebody clean up the transaction in the meanwhile? */
  197. if (journal->j_checkpoint_transactions != transaction ||
  198. transaction->t_tid != this_tid)
  199. return ret;
  200. while (!released && transaction->t_checkpoint_io_list) {
  201. jh = transaction->t_checkpoint_io_list;
  202. bh = jh2bh(jh);
  203. if (!jbd_trylock_bh_state(bh)) {
  204. jbd_sync_bh(journal, bh);
  205. spin_lock(&journal->j_list_lock);
  206. goto restart;
  207. }
  208. if (buffer_locked(bh)) {
  209. atomic_inc(&bh->b_count);
  210. spin_unlock(&journal->j_list_lock);
  211. jbd_unlock_bh_state(bh);
  212. wait_on_buffer(bh);
  213. /* the journal_head may have gone by now */
  214. BUFFER_TRACE(bh, "brelse");
  215. __brelse(bh);
  216. spin_lock(&journal->j_list_lock);
  217. goto restart;
  218. }
  219. if (unlikely(buffer_write_io_error(bh)))
  220. ret = -EIO;
  221. /*
  222. * Now in whatever state the buffer currently is, we know that
  223. * it has been written out and so we can drop it from the list
  224. */
  225. released = __jbd2_journal_remove_checkpoint(jh);
  226. jbd_unlock_bh_state(bh);
  227. jbd2_journal_remove_journal_head(bh);
  228. __brelse(bh);
  229. }
  230. return ret;
  231. }
  232. #define NR_BATCH 64
  233. static void
  234. __flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count)
  235. {
  236. int i;
  237. ll_rw_block(SWRITE, *batch_count, bhs);
  238. for (i = 0; i < *batch_count; i++) {
  239. struct buffer_head *bh = bhs[i];
  240. clear_buffer_jwrite(bh);
  241. BUFFER_TRACE(bh, "brelse");
  242. __brelse(bh);
  243. }
  244. *batch_count = 0;
  245. }
  246. /*
  247. * Try to flush one buffer from the checkpoint list to disk.
  248. *
  249. * Return 1 if something happened which requires us to abort the current
  250. * scan of the checkpoint list. Return <0 if the buffer has failed to
  251. * be written out.
  252. *
  253. * Called with j_list_lock held and drops it if 1 is returned
  254. * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  255. */
  256. static int __process_buffer(journal_t *journal, struct journal_head *jh,
  257. struct buffer_head **bhs, int *batch_count,
  258. 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. 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 == NR_BATCH) {
  309. spin_unlock(&journal->j_list_lock);
  310. __flush_batch(journal, bhs, 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_mark(jbd2_checkpoint, "dev %s need_checkpoint %d",
  337. journal->j_devname, result);
  338. jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
  339. if (result <= 0)
  340. return result;
  341. /*
  342. * OK, we need to start writing disk blocks. Take one transaction
  343. * and write it.
  344. */
  345. result = 0;
  346. spin_lock(&journal->j_list_lock);
  347. if (!journal->j_checkpoint_transactions)
  348. goto out;
  349. transaction = journal->j_checkpoint_transactions;
  350. if (transaction->t_chp_stats.cs_chp_time == 0)
  351. transaction->t_chp_stats.cs_chp_time = jiffies;
  352. this_tid = transaction->t_tid;
  353. restart:
  354. /*
  355. * If someone cleaned up this transaction while we slept, we're
  356. * done (maybe it's a new transaction, but it fell at the same
  357. * address).
  358. */
  359. if (journal->j_checkpoint_transactions == transaction &&
  360. transaction->t_tid == this_tid) {
  361. int batch_count = 0;
  362. struct buffer_head *bhs[NR_BATCH];
  363. struct journal_head *jh;
  364. int retry = 0, err;
  365. while (!retry && transaction->t_checkpoint_list) {
  366. struct buffer_head *bh;
  367. jh = transaction->t_checkpoint_list;
  368. bh = jh2bh(jh);
  369. if (!jbd_trylock_bh_state(bh)) {
  370. jbd_sync_bh(journal, bh);
  371. retry = 1;
  372. break;
  373. }
  374. retry = __process_buffer(journal, jh, bhs, &batch_count,
  375. transaction);
  376. if (retry < 0 && !result)
  377. result = retry;
  378. if (!retry && (need_resched() ||
  379. spin_needbreak(&journal->j_list_lock))) {
  380. spin_unlock(&journal->j_list_lock);
  381. retry = 1;
  382. break;
  383. }
  384. }
  385. if (batch_count) {
  386. if (!retry) {
  387. spin_unlock(&journal->j_list_lock);
  388. retry = 1;
  389. }
  390. __flush_batch(journal, bhs, &batch_count);
  391. }
  392. if (retry) {
  393. spin_lock(&journal->j_list_lock);
  394. goto restart;
  395. }
  396. /*
  397. * Now we have cleaned up the first transaction's checkpoint
  398. * list. Let's clean up the second one
  399. */
  400. err = __wait_cp_io(journal, transaction);
  401. if (!result)
  402. result = err;
  403. }
  404. out:
  405. spin_unlock(&journal->j_list_lock);
  406. if (result < 0)
  407. jbd2_journal_abort(journal, result);
  408. else
  409. result = jbd2_cleanup_journal_tail(journal);
  410. return (result < 0) ? result : 0;
  411. }
  412. /*
  413. * Check the list of checkpoint transactions for the journal to see if
  414. * we have already got rid of any since the last update of the log tail
  415. * in the journal superblock. If so, we can instantly roll the
  416. * superblock forward to remove those transactions from the log.
  417. *
  418. * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
  419. *
  420. * Called with the journal lock held.
  421. *
  422. * This is the only part of the journaling code which really needs to be
  423. * aware of transaction aborts. Checkpointing involves writing to the
  424. * main filesystem area rather than to the journal, so it can proceed
  425. * even in abort state, but we must not update the super block if
  426. * checkpointing may have failed. Otherwise, we would lose some metadata
  427. * buffers which should be written-back to the filesystem.
  428. */
  429. int jbd2_cleanup_journal_tail(journal_t *journal)
  430. {
  431. transaction_t * transaction;
  432. tid_t first_tid;
  433. unsigned long blocknr, freed;
  434. if (is_journal_aborted(journal))
  435. return 1;
  436. /* OK, work out the oldest transaction remaining in the log, and
  437. * the log block it starts at.
  438. *
  439. * If the log is now empty, we need to work out which is the
  440. * next transaction ID we will write, and where it will
  441. * start. */
  442. spin_lock(&journal->j_state_lock);
  443. spin_lock(&journal->j_list_lock);
  444. transaction = journal->j_checkpoint_transactions;
  445. if (transaction) {
  446. first_tid = transaction->t_tid;
  447. blocknr = transaction->t_log_start;
  448. } else if ((transaction = journal->j_committing_transaction) != NULL) {
  449. first_tid = transaction->t_tid;
  450. blocknr = transaction->t_log_start;
  451. } else if ((transaction = journal->j_running_transaction) != NULL) {
  452. first_tid = transaction->t_tid;
  453. blocknr = journal->j_head;
  454. } else {
  455. first_tid = journal->j_transaction_sequence;
  456. blocknr = journal->j_head;
  457. }
  458. spin_unlock(&journal->j_list_lock);
  459. J_ASSERT(blocknr != 0);
  460. /* If the oldest pinned transaction is at the tail of the log
  461. already then there's not much we can do right now. */
  462. if (journal->j_tail_sequence == first_tid) {
  463. spin_unlock(&journal->j_state_lock);
  464. return 1;
  465. }
  466. /* OK, update the superblock to recover the freed space.
  467. * Physical blocks come first: have we wrapped beyond the end of
  468. * the log? */
  469. freed = blocknr - journal->j_tail;
  470. if (blocknr < journal->j_tail)
  471. freed = freed + journal->j_last - journal->j_first;
  472. jbd_debug(1,
  473. "Cleaning journal tail from %d to %d (offset %lu), "
  474. "freeing %lu\n",
  475. journal->j_tail_sequence, first_tid, blocknr, freed);
  476. journal->j_free += freed;
  477. journal->j_tail_sequence = first_tid;
  478. journal->j_tail = blocknr;
  479. spin_unlock(&journal->j_state_lock);
  480. if (!(journal->j_flags & JBD2_ABORT))
  481. jbd2_journal_update_superblock(journal, 1);
  482. return 0;
  483. }
  484. /* Checkpoint list management */
  485. /*
  486. * journal_clean_one_cp_list
  487. *
  488. * Find all the written-back checkpoint buffers in the given list and release them.
  489. *
  490. * Called with the journal locked.
  491. * Called with j_list_lock held.
  492. * Returns number of bufers reaped (for debug)
  493. */
  494. static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
  495. {
  496. struct journal_head *last_jh;
  497. struct journal_head *next_jh = jh;
  498. int ret, freed = 0;
  499. *released = 0;
  500. if (!jh)
  501. return 0;
  502. last_jh = jh->b_cpprev;
  503. do {
  504. jh = next_jh;
  505. next_jh = jh->b_cpnext;
  506. /* Use trylock because of the ranking */
  507. if (jbd_trylock_bh_state(jh2bh(jh))) {
  508. ret = __try_to_free_cp_buf(jh);
  509. if (ret) {
  510. freed++;
  511. if (ret == 2) {
  512. *released = 1;
  513. return freed;
  514. }
  515. }
  516. }
  517. /*
  518. * This function only frees up some memory
  519. * if possible so we dont have an obligation
  520. * to finish processing. Bail out if preemption
  521. * requested:
  522. */
  523. if (need_resched())
  524. return freed;
  525. } while (jh != last_jh);
  526. return freed;
  527. }
  528. /*
  529. * journal_clean_checkpoint_list
  530. *
  531. * Find all the written-back checkpoint buffers in the journal and release them.
  532. *
  533. * Called with the journal locked.
  534. * Called with j_list_lock held.
  535. * Returns number of buffers reaped (for debug)
  536. */
  537. int __jbd2_journal_clean_checkpoint_list(journal_t *journal)
  538. {
  539. transaction_t *transaction, *last_transaction, *next_transaction;
  540. int ret = 0;
  541. int released;
  542. transaction = journal->j_checkpoint_transactions;
  543. if (!transaction)
  544. goto out;
  545. last_transaction = transaction->t_cpprev;
  546. next_transaction = transaction;
  547. do {
  548. transaction = next_transaction;
  549. next_transaction = transaction->t_cpnext;
  550. ret += journal_clean_one_cp_list(transaction->
  551. t_checkpoint_list, &released);
  552. /*
  553. * This function only frees up some memory if possible so we
  554. * dont have an obligation to finish processing. Bail out if
  555. * preemption requested:
  556. */
  557. if (need_resched())
  558. goto out;
  559. if (released)
  560. continue;
  561. /*
  562. * It is essential that we are as careful as in the case of
  563. * t_checkpoint_list with removing the buffer from the list as
  564. * we can possibly see not yet submitted buffers on io_list
  565. */
  566. ret += journal_clean_one_cp_list(transaction->
  567. t_checkpoint_io_list, &released);
  568. if (need_resched())
  569. goto out;
  570. } while (transaction != last_transaction);
  571. out:
  572. return ret;
  573. }
  574. /*
  575. * journal_remove_checkpoint: called after a buffer has been committed
  576. * to disk (either by being write-back flushed to disk, or being
  577. * committed to the log).
  578. *
  579. * We cannot safely clean a transaction out of the log until all of the
  580. * buffer updates committed in that transaction have safely been stored
  581. * elsewhere on disk. To achieve this, all of the buffers in a
  582. * transaction need to be maintained on the transaction's checkpoint
  583. * lists until they have been rewritten, at which point this function is
  584. * called to remove the buffer from the existing transaction's
  585. * checkpoint lists.
  586. *
  587. * The function returns 1 if it frees the transaction, 0 otherwise.
  588. *
  589. * This function is called with the journal locked.
  590. * This function is called with j_list_lock held.
  591. * This function is called with jbd_lock_bh_state(jh2bh(jh))
  592. */
  593. int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
  594. {
  595. transaction_t *transaction;
  596. journal_t *journal;
  597. int ret = 0;
  598. JBUFFER_TRACE(jh, "entry");
  599. if ((transaction = jh->b_cp_transaction) == NULL) {
  600. JBUFFER_TRACE(jh, "not on transaction");
  601. goto out;
  602. }
  603. journal = transaction->t_journal;
  604. __buffer_unlink(jh);
  605. jh->b_cp_transaction = NULL;
  606. if (transaction->t_checkpoint_list != NULL ||
  607. transaction->t_checkpoint_io_list != NULL)
  608. goto out;
  609. JBUFFER_TRACE(jh, "transaction has no more buffers");
  610. /*
  611. * There is one special case to worry about: if we have just pulled the
  612. * buffer off a running or committing transaction's checkpoing list,
  613. * then even if the checkpoint list is empty, the transaction obviously
  614. * cannot be dropped!
  615. *
  616. * The locking here around t_state is a bit sleazy.
  617. * See the comment at the end of jbd2_journal_commit_transaction().
  618. */
  619. if (transaction->t_state != T_FINISHED) {
  620. JBUFFER_TRACE(jh, "belongs to running/committing transaction");
  621. goto out;
  622. }
  623. /* OK, that was the last buffer for the transaction: we can now
  624. safely remove this transaction from the log */
  625. __jbd2_journal_drop_transaction(journal, transaction);
  626. /* Just in case anybody was waiting for more transactions to be
  627. checkpointed... */
  628. wake_up(&journal->j_wait_logspace);
  629. ret = 1;
  630. out:
  631. JBUFFER_TRACE(jh, "exit");
  632. return ret;
  633. }
  634. /*
  635. * journal_insert_checkpoint: put a committed buffer onto a checkpoint
  636. * list so that we know when it is safe to clean the transaction out of
  637. * the log.
  638. *
  639. * Called with the journal locked.
  640. * Called with j_list_lock held.
  641. */
  642. void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
  643. transaction_t *transaction)
  644. {
  645. JBUFFER_TRACE(jh, "entry");
  646. J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
  647. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  648. jh->b_cp_transaction = transaction;
  649. if (!transaction->t_checkpoint_list) {
  650. jh->b_cpnext = jh->b_cpprev = jh;
  651. } else {
  652. jh->b_cpnext = transaction->t_checkpoint_list;
  653. jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
  654. jh->b_cpprev->b_cpnext = jh;
  655. jh->b_cpnext->b_cpprev = jh;
  656. }
  657. transaction->t_checkpoint_list = jh;
  658. }
  659. /*
  660. * We've finished with this transaction structure: adios...
  661. *
  662. * The transaction must have no links except for the checkpoint by this
  663. * point.
  664. *
  665. * Called with the journal locked.
  666. * Called with j_list_lock held.
  667. */
  668. void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
  669. {
  670. assert_spin_locked(&journal->j_list_lock);
  671. if (transaction->t_cpnext) {
  672. transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
  673. transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
  674. if (journal->j_checkpoint_transactions == transaction)
  675. journal->j_checkpoint_transactions =
  676. transaction->t_cpnext;
  677. if (journal->j_checkpoint_transactions == transaction)
  678. journal->j_checkpoint_transactions = NULL;
  679. }
  680. J_ASSERT(transaction->t_state == T_FINISHED);
  681. J_ASSERT(transaction->t_buffers == NULL);
  682. J_ASSERT(transaction->t_forget == NULL);
  683. J_ASSERT(transaction->t_iobuf_list == NULL);
  684. J_ASSERT(transaction->t_shadow_list == NULL);
  685. J_ASSERT(transaction->t_log_list == NULL);
  686. J_ASSERT(transaction->t_checkpoint_list == NULL);
  687. J_ASSERT(transaction->t_checkpoint_io_list == NULL);
  688. J_ASSERT(transaction->t_updates == 0);
  689. J_ASSERT(journal->j_committing_transaction != transaction);
  690. J_ASSERT(journal->j_running_transaction != transaction);
  691. jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
  692. kfree(transaction);
  693. }