checkpoint.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * linux/fs/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.
  26. *
  27. * Called with j_list_lock held.
  28. */
  29. static inline void __buffer_unlink(struct journal_head *jh)
  30. {
  31. transaction_t *transaction;
  32. transaction = jh->b_cp_transaction;
  33. jh->b_cp_transaction = NULL;
  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. * Try to release a checkpointed buffer from its transaction.
  43. * Returns 1 if we released it.
  44. * Requires j_list_lock
  45. * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  46. */
  47. static int __try_to_free_cp_buf(struct journal_head *jh)
  48. {
  49. int ret = 0;
  50. struct buffer_head *bh = jh2bh(jh);
  51. if (jh->b_jlist == BJ_None && !buffer_locked(bh) && !buffer_dirty(bh)) {
  52. JBUFFER_TRACE(jh, "remove from checkpoint list");
  53. __journal_remove_checkpoint(jh);
  54. jbd_unlock_bh_state(bh);
  55. journal_remove_journal_head(bh);
  56. BUFFER_TRACE(bh, "release");
  57. __brelse(bh);
  58. ret = 1;
  59. } else {
  60. jbd_unlock_bh_state(bh);
  61. }
  62. return ret;
  63. }
  64. /*
  65. * __log_wait_for_space: wait until there is space in the journal.
  66. *
  67. * Called under j-state_lock *only*. It will be unlocked if we have to wait
  68. * for a checkpoint to free up some space in the log.
  69. */
  70. void __log_wait_for_space(journal_t *journal)
  71. {
  72. int nblocks;
  73. assert_spin_locked(&journal->j_state_lock);
  74. nblocks = jbd_space_needed(journal);
  75. while (__log_space_left(journal) < nblocks) {
  76. if (journal->j_flags & JFS_ABORT)
  77. return;
  78. spin_unlock(&journal->j_state_lock);
  79. mutex_lock(&journal->j_checkpoint_mutex);
  80. /*
  81. * Test again, another process may have checkpointed while we
  82. * were waiting for the checkpoint lock
  83. */
  84. spin_lock(&journal->j_state_lock);
  85. nblocks = jbd_space_needed(journal);
  86. if (__log_space_left(journal) < nblocks) {
  87. spin_unlock(&journal->j_state_lock);
  88. log_do_checkpoint(journal);
  89. spin_lock(&journal->j_state_lock);
  90. }
  91. mutex_unlock(&journal->j_checkpoint_mutex);
  92. }
  93. }
  94. /*
  95. * We were unable to perform jbd_trylock_bh_state() inside j_list_lock.
  96. * The caller must restart a list walk. Wait for someone else to run
  97. * jbd_unlock_bh_state().
  98. */
  99. static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh)
  100. {
  101. get_bh(bh);
  102. spin_unlock(&journal->j_list_lock);
  103. jbd_lock_bh_state(bh);
  104. jbd_unlock_bh_state(bh);
  105. put_bh(bh);
  106. }
  107. /*
  108. * Clean up a transaction's checkpoint list.
  109. *
  110. * We wait for any pending IO to complete and make sure any clean
  111. * buffers are removed from the transaction.
  112. *
  113. * Return 1 if we performed any actions which might have destroyed the
  114. * checkpoint. (journal_remove_checkpoint() deletes the transaction when
  115. * the last checkpoint buffer is cleansed)
  116. *
  117. * Called with j_list_lock held.
  118. */
  119. static int __cleanup_transaction(journal_t *journal, transaction_t *transaction)
  120. {
  121. struct journal_head *jh, *next_jh, *last_jh;
  122. struct buffer_head *bh;
  123. int ret = 0;
  124. assert_spin_locked(&journal->j_list_lock);
  125. jh = transaction->t_checkpoint_list;
  126. if (!jh)
  127. return 0;
  128. last_jh = jh->b_cpprev;
  129. next_jh = jh;
  130. do {
  131. jh = next_jh;
  132. bh = jh2bh(jh);
  133. if (buffer_locked(bh)) {
  134. atomic_inc(&bh->b_count);
  135. spin_unlock(&journal->j_list_lock);
  136. wait_on_buffer(bh);
  137. /* the journal_head may have gone by now */
  138. BUFFER_TRACE(bh, "brelse");
  139. __brelse(bh);
  140. goto out_return_1;
  141. }
  142. /*
  143. * This is foul
  144. */
  145. if (!jbd_trylock_bh_state(bh)) {
  146. jbd_sync_bh(journal, bh);
  147. goto out_return_1;
  148. }
  149. if (jh->b_transaction != NULL) {
  150. transaction_t *t = jh->b_transaction;
  151. tid_t tid = t->t_tid;
  152. spin_unlock(&journal->j_list_lock);
  153. jbd_unlock_bh_state(bh);
  154. log_start_commit(journal, tid);
  155. log_wait_commit(journal, tid);
  156. goto out_return_1;
  157. }
  158. /*
  159. * AKPM: I think the buffer_jbddirty test is redundant - it
  160. * shouldn't have NULL b_transaction?
  161. */
  162. next_jh = jh->b_cpnext;
  163. if (!buffer_dirty(bh) && !buffer_jbddirty(bh)) {
  164. BUFFER_TRACE(bh, "remove from checkpoint");
  165. __journal_remove_checkpoint(jh);
  166. jbd_unlock_bh_state(bh);
  167. journal_remove_journal_head(bh);
  168. __brelse(bh);
  169. ret = 1;
  170. } else {
  171. jbd_unlock_bh_state(bh);
  172. }
  173. } while (jh != last_jh);
  174. return ret;
  175. out_return_1:
  176. spin_lock(&journal->j_list_lock);
  177. return 1;
  178. }
  179. #define NR_BATCH 64
  180. static void
  181. __flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count)
  182. {
  183. int i;
  184. spin_unlock(&journal->j_list_lock);
  185. ll_rw_block(SWRITE, *batch_count, bhs);
  186. spin_lock(&journal->j_list_lock);
  187. for (i = 0; i < *batch_count; i++) {
  188. struct buffer_head *bh = bhs[i];
  189. clear_buffer_jwrite(bh);
  190. BUFFER_TRACE(bh, "brelse");
  191. __brelse(bh);
  192. }
  193. *batch_count = 0;
  194. }
  195. /*
  196. * Try to flush one buffer from the checkpoint list to disk.
  197. *
  198. * Return 1 if something happened which requires us to abort the current
  199. * scan of the checkpoint list.
  200. *
  201. * Called with j_list_lock held.
  202. * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
  203. */
  204. static int __flush_buffer(journal_t *journal, struct journal_head *jh,
  205. struct buffer_head **bhs, int *batch_count,
  206. int *drop_count)
  207. {
  208. struct buffer_head *bh = jh2bh(jh);
  209. int ret = 0;
  210. if (buffer_dirty(bh) && !buffer_locked(bh) && jh->b_jlist == BJ_None) {
  211. J_ASSERT_JH(jh, jh->b_transaction == NULL);
  212. /*
  213. * Important: we are about to write the buffer, and
  214. * possibly block, while still holding the journal lock.
  215. * We cannot afford to let the transaction logic start
  216. * messing around with this buffer before we write it to
  217. * disk, as that would break recoverability.
  218. */
  219. BUFFER_TRACE(bh, "queue");
  220. get_bh(bh);
  221. J_ASSERT_BH(bh, !buffer_jwrite(bh));
  222. set_buffer_jwrite(bh);
  223. bhs[*batch_count] = bh;
  224. jbd_unlock_bh_state(bh);
  225. (*batch_count)++;
  226. if (*batch_count == NR_BATCH) {
  227. __flush_batch(journal, bhs, batch_count);
  228. ret = 1;
  229. }
  230. } else {
  231. int last_buffer = 0;
  232. if (jh->b_cpnext == jh) {
  233. /* We may be about to drop the transaction. Tell the
  234. * caller that the lists have changed.
  235. */
  236. last_buffer = 1;
  237. }
  238. if (__try_to_free_cp_buf(jh)) {
  239. (*drop_count)++;
  240. ret = last_buffer;
  241. }
  242. }
  243. return ret;
  244. }
  245. /*
  246. * Perform an actual checkpoint. We don't write out only enough to
  247. * satisfy the current blocked requests: rather we submit a reasonably
  248. * sized chunk of the outstanding data to disk at once for
  249. * efficiency. __log_wait_for_space() will retry if we didn't free enough.
  250. *
  251. * However, we _do_ take into account the amount requested so that once
  252. * the IO has been queued, we can return as soon as enough of it has
  253. * completed to disk.
  254. *
  255. * The journal should be locked before calling this function.
  256. */
  257. int log_do_checkpoint(journal_t *journal)
  258. {
  259. int result;
  260. int batch_count = 0;
  261. struct buffer_head *bhs[NR_BATCH];
  262. jbd_debug(1, "Start checkpoint\n");
  263. /*
  264. * First thing: if there are any transactions in the log which
  265. * don't need checkpointing, just eliminate them from the
  266. * journal straight away.
  267. */
  268. result = cleanup_journal_tail(journal);
  269. jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
  270. if (result <= 0)
  271. return result;
  272. /*
  273. * OK, we need to start writing disk blocks. Try to free up a
  274. * quarter of the log in a single checkpoint if we can.
  275. */
  276. /*
  277. * AKPM: check this code. I had a feeling a while back that it
  278. * degenerates into a busy loop at unmount time.
  279. */
  280. spin_lock(&journal->j_list_lock);
  281. while (journal->j_checkpoint_transactions) {
  282. transaction_t *transaction;
  283. struct journal_head *jh, *last_jh, *next_jh;
  284. int drop_count = 0;
  285. int cleanup_ret, retry = 0;
  286. tid_t this_tid;
  287. transaction = journal->j_checkpoint_transactions;
  288. this_tid = transaction->t_tid;
  289. jh = transaction->t_checkpoint_list;
  290. last_jh = jh->b_cpprev;
  291. next_jh = jh;
  292. do {
  293. struct buffer_head *bh;
  294. jh = next_jh;
  295. next_jh = jh->b_cpnext;
  296. bh = jh2bh(jh);
  297. if (!jbd_trylock_bh_state(bh)) {
  298. jbd_sync_bh(journal, bh);
  299. spin_lock(&journal->j_list_lock);
  300. retry = 1;
  301. break;
  302. }
  303. retry = __flush_buffer(journal, jh, bhs, &batch_count, &drop_count);
  304. if (cond_resched_lock(&journal->j_list_lock)) {
  305. retry = 1;
  306. break;
  307. }
  308. } while (jh != last_jh && !retry);
  309. if (batch_count) {
  310. __flush_batch(journal, bhs, &batch_count);
  311. retry = 1;
  312. }
  313. /*
  314. * If someone cleaned up this transaction while we slept, we're
  315. * done
  316. */
  317. if (journal->j_checkpoint_transactions != transaction)
  318. break;
  319. if (retry)
  320. continue;
  321. /*
  322. * Maybe it's a new transaction, but it fell at the same
  323. * address
  324. */
  325. if (transaction->t_tid != this_tid)
  326. continue;
  327. /*
  328. * We have walked the whole transaction list without
  329. * finding anything to write to disk. We had better be
  330. * able to make some progress or we are in trouble.
  331. */
  332. cleanup_ret = __cleanup_transaction(journal, transaction);
  333. J_ASSERT(drop_count != 0 || cleanup_ret != 0);
  334. if (journal->j_checkpoint_transactions != transaction)
  335. break;
  336. }
  337. spin_unlock(&journal->j_list_lock);
  338. result = cleanup_journal_tail(journal);
  339. if (result < 0)
  340. return result;
  341. return 0;
  342. }
  343. /*
  344. * Check the list of checkpoint transactions for the journal to see if
  345. * we have already got rid of any since the last update of the log tail
  346. * in the journal superblock. If so, we can instantly roll the
  347. * superblock forward to remove those transactions from the log.
  348. *
  349. * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
  350. *
  351. * Called with the journal lock held.
  352. *
  353. * This is the only part of the journaling code which really needs to be
  354. * aware of transaction aborts. Checkpointing involves writing to the
  355. * main filesystem area rather than to the journal, so it can proceed
  356. * even in abort state, but we must not update the journal superblock if
  357. * we have an abort error outstanding.
  358. */
  359. int cleanup_journal_tail(journal_t *journal)
  360. {
  361. transaction_t * transaction;
  362. tid_t first_tid;
  363. unsigned long blocknr, freed;
  364. /* OK, work out the oldest transaction remaining in the log, and
  365. * the log block it starts at.
  366. *
  367. * If the log is now empty, we need to work out which is the
  368. * next transaction ID we will write, and where it will
  369. * start. */
  370. spin_lock(&journal->j_state_lock);
  371. spin_lock(&journal->j_list_lock);
  372. transaction = journal->j_checkpoint_transactions;
  373. if (transaction) {
  374. first_tid = transaction->t_tid;
  375. blocknr = transaction->t_log_start;
  376. } else if ((transaction = journal->j_committing_transaction) != NULL) {
  377. first_tid = transaction->t_tid;
  378. blocknr = transaction->t_log_start;
  379. } else if ((transaction = journal->j_running_transaction) != NULL) {
  380. first_tid = transaction->t_tid;
  381. blocknr = journal->j_head;
  382. } else {
  383. first_tid = journal->j_transaction_sequence;
  384. blocknr = journal->j_head;
  385. }
  386. spin_unlock(&journal->j_list_lock);
  387. J_ASSERT(blocknr != 0);
  388. /* If the oldest pinned transaction is at the tail of the log
  389. already then there's not much we can do right now. */
  390. if (journal->j_tail_sequence == first_tid) {
  391. spin_unlock(&journal->j_state_lock);
  392. return 1;
  393. }
  394. /* OK, update the superblock to recover the freed space.
  395. * Physical blocks come first: have we wrapped beyond the end of
  396. * the log? */
  397. freed = blocknr - journal->j_tail;
  398. if (blocknr < journal->j_tail)
  399. freed = freed + journal->j_last - journal->j_first;
  400. jbd_debug(1,
  401. "Cleaning journal tail from %d to %d (offset %lu), "
  402. "freeing %lu\n",
  403. journal->j_tail_sequence, first_tid, blocknr, freed);
  404. journal->j_free += freed;
  405. journal->j_tail_sequence = first_tid;
  406. journal->j_tail = blocknr;
  407. spin_unlock(&journal->j_state_lock);
  408. if (!(journal->j_flags & JFS_ABORT))
  409. journal_update_superblock(journal, 1);
  410. return 0;
  411. }
  412. /* Checkpoint list management */
  413. /*
  414. * journal_clean_checkpoint_list
  415. *
  416. * Find all the written-back checkpoint buffers in the journal and release them.
  417. *
  418. * Called with the journal locked.
  419. * Called with j_list_lock held.
  420. * Returns number of bufers reaped (for debug)
  421. */
  422. int __journal_clean_checkpoint_list(journal_t *journal)
  423. {
  424. transaction_t *transaction, *last_transaction, *next_transaction;
  425. int ret = 0;
  426. transaction = journal->j_checkpoint_transactions;
  427. if (transaction == 0)
  428. goto out;
  429. last_transaction = transaction->t_cpprev;
  430. next_transaction = transaction;
  431. do {
  432. struct journal_head *jh;
  433. transaction = next_transaction;
  434. next_transaction = transaction->t_cpnext;
  435. jh = transaction->t_checkpoint_list;
  436. if (jh) {
  437. struct journal_head *last_jh = jh->b_cpprev;
  438. struct journal_head *next_jh = jh;
  439. do {
  440. jh = next_jh;
  441. next_jh = jh->b_cpnext;
  442. /* Use trylock because of the ranknig */
  443. if (jbd_trylock_bh_state(jh2bh(jh)))
  444. ret += __try_to_free_cp_buf(jh);
  445. /*
  446. * This function only frees up some memory
  447. * if possible so we dont have an obligation
  448. * to finish processing. Bail out if preemption
  449. * requested:
  450. */
  451. if (need_resched())
  452. goto out;
  453. } while (jh != last_jh);
  454. }
  455. } while (transaction != last_transaction);
  456. out:
  457. return ret;
  458. }
  459. /*
  460. * journal_remove_checkpoint: called after a buffer has been committed
  461. * to disk (either by being write-back flushed to disk, or being
  462. * committed to the log).
  463. *
  464. * We cannot safely clean a transaction out of the log until all of the
  465. * buffer updates committed in that transaction have safely been stored
  466. * elsewhere on disk. To achieve this, all of the buffers in a
  467. * transaction need to be maintained on the transaction's checkpoint
  468. * list until they have been rewritten, at which point this function is
  469. * called to remove the buffer from the existing transaction's
  470. * checkpoint list.
  471. *
  472. * This function is called with the journal locked.
  473. * This function is called with j_list_lock held.
  474. */
  475. void __journal_remove_checkpoint(struct journal_head *jh)
  476. {
  477. transaction_t *transaction;
  478. journal_t *journal;
  479. JBUFFER_TRACE(jh, "entry");
  480. if ((transaction = jh->b_cp_transaction) == NULL) {
  481. JBUFFER_TRACE(jh, "not on transaction");
  482. goto out;
  483. }
  484. journal = transaction->t_journal;
  485. __buffer_unlink(jh);
  486. if (transaction->t_checkpoint_list != NULL)
  487. goto out;
  488. JBUFFER_TRACE(jh, "transaction has no more buffers");
  489. /*
  490. * There is one special case to worry about: if we have just pulled the
  491. * buffer off a committing transaction's forget list, then even if the
  492. * checkpoint list is empty, the transaction obviously cannot be
  493. * dropped!
  494. *
  495. * The locking here around j_committing_transaction is a bit sleazy.
  496. * See the comment at the end of journal_commit_transaction().
  497. */
  498. if (transaction == journal->j_committing_transaction) {
  499. JBUFFER_TRACE(jh, "belongs to committing transaction");
  500. goto out;
  501. }
  502. /* OK, that was the last buffer for the transaction: we can now
  503. safely remove this transaction from the log */
  504. __journal_drop_transaction(journal, transaction);
  505. /* Just in case anybody was waiting for more transactions to be
  506. checkpointed... */
  507. wake_up(&journal->j_wait_logspace);
  508. out:
  509. JBUFFER_TRACE(jh, "exit");
  510. }
  511. /*
  512. * journal_insert_checkpoint: put a committed buffer onto a checkpoint
  513. * list so that we know when it is safe to clean the transaction out of
  514. * the log.
  515. *
  516. * Called with the journal locked.
  517. * Called with j_list_lock held.
  518. */
  519. void __journal_insert_checkpoint(struct journal_head *jh,
  520. transaction_t *transaction)
  521. {
  522. JBUFFER_TRACE(jh, "entry");
  523. J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
  524. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  525. jh->b_cp_transaction = transaction;
  526. if (!transaction->t_checkpoint_list) {
  527. jh->b_cpnext = jh->b_cpprev = jh;
  528. } else {
  529. jh->b_cpnext = transaction->t_checkpoint_list;
  530. jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
  531. jh->b_cpprev->b_cpnext = jh;
  532. jh->b_cpnext->b_cpprev = jh;
  533. }
  534. transaction->t_checkpoint_list = jh;
  535. }
  536. /*
  537. * We've finished with this transaction structure: adios...
  538. *
  539. * The transaction must have no links except for the checkpoint by this
  540. * point.
  541. *
  542. * Called with the journal locked.
  543. * Called with j_list_lock held.
  544. */
  545. void __journal_drop_transaction(journal_t *journal, transaction_t *transaction)
  546. {
  547. assert_spin_locked(&journal->j_list_lock);
  548. if (transaction->t_cpnext) {
  549. transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
  550. transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
  551. if (journal->j_checkpoint_transactions == transaction)
  552. journal->j_checkpoint_transactions =
  553. transaction->t_cpnext;
  554. if (journal->j_checkpoint_transactions == transaction)
  555. journal->j_checkpoint_transactions = NULL;
  556. }
  557. J_ASSERT(transaction->t_state == T_FINISHED);
  558. J_ASSERT(transaction->t_buffers == NULL);
  559. J_ASSERT(transaction->t_sync_datalist == NULL);
  560. J_ASSERT(transaction->t_forget == NULL);
  561. J_ASSERT(transaction->t_iobuf_list == NULL);
  562. J_ASSERT(transaction->t_shadow_list == NULL);
  563. J_ASSERT(transaction->t_log_list == NULL);
  564. J_ASSERT(transaction->t_checkpoint_list == NULL);
  565. J_ASSERT(transaction->t_updates == 0);
  566. J_ASSERT(journal->j_committing_transaction != transaction);
  567. J_ASSERT(journal->j_running_transaction != transaction);
  568. jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
  569. kfree(transaction);
  570. }