checkpoint.c 21 KB

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