checkpoint.c 17 KB

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