checkpoint.c 20 KB

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