checkpoint.c 23 KB

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