commit.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * linux/fs/commit.c
  3. *
  4. * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
  5. *
  6. * Copyright 1998 Red Hat corp --- 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. * Journal commit routines for the generic filesystem journaling code;
  13. * part of the ext2fs journaling system.
  14. */
  15. #include <linux/time.h>
  16. #include <linux/fs.h>
  17. #include <linux/jbd.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/mm.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/smp_lock.h>
  23. /*
  24. * Default IO end handler for temporary BJ_IO buffer_heads.
  25. */
  26. static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
  27. {
  28. BUFFER_TRACE(bh, "");
  29. if (uptodate)
  30. set_buffer_uptodate(bh);
  31. else
  32. clear_buffer_uptodate(bh);
  33. unlock_buffer(bh);
  34. }
  35. /*
  36. * When an ext3-ordered file is truncated, it is possible that many pages are
  37. * not sucessfully freed, because they are attached to a committing transaction.
  38. * After the transaction commits, these pages are left on the LRU, with no
  39. * ->mapping, and with attached buffers. These pages are trivially reclaimable
  40. * by the VM, but their apparent absence upsets the VM accounting, and it makes
  41. * the numbers in /proc/meminfo look odd.
  42. *
  43. * So here, we have a buffer which has just come off the forget list. Look to
  44. * see if we can strip all buffers from the backing page.
  45. *
  46. * Called under lock_journal(), and possibly under journal_datalist_lock. The
  47. * caller provided us with a ref against the buffer, and we drop that here.
  48. */
  49. static void release_buffer_page(struct buffer_head *bh)
  50. {
  51. struct page *page;
  52. if (buffer_dirty(bh))
  53. goto nope;
  54. if (atomic_read(&bh->b_count) != 1)
  55. goto nope;
  56. page = bh->b_page;
  57. if (!page)
  58. goto nope;
  59. if (page->mapping)
  60. goto nope;
  61. /* OK, it's a truncated page */
  62. if (TestSetPageLocked(page))
  63. goto nope;
  64. page_cache_get(page);
  65. __brelse(bh);
  66. try_to_free_buffers(page);
  67. unlock_page(page);
  68. page_cache_release(page);
  69. return;
  70. nope:
  71. __brelse(bh);
  72. }
  73. /*
  74. * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is
  75. * held. For ranking reasons we must trylock. If we lose, schedule away and
  76. * return 0. j_list_lock is dropped in this case.
  77. */
  78. static int inverted_lock(journal_t *journal, struct buffer_head *bh)
  79. {
  80. if (!jbd_trylock_bh_state(bh)) {
  81. spin_unlock(&journal->j_list_lock);
  82. schedule();
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. /* Done it all: now write the commit record. We should have
  88. * cleaned up our previous buffers by now, so if we are in abort
  89. * mode we can now just skip the rest of the journal write
  90. * entirely.
  91. *
  92. * Returns 1 if the journal needs to be aborted or 0 on success
  93. */
  94. static int journal_write_commit_record(journal_t *journal,
  95. transaction_t *commit_transaction)
  96. {
  97. struct journal_head *descriptor;
  98. struct buffer_head *bh;
  99. int i, ret;
  100. int barrier_done = 0;
  101. if (is_journal_aborted(journal))
  102. return 0;
  103. descriptor = journal_get_descriptor_buffer(journal);
  104. if (!descriptor)
  105. return 1;
  106. bh = jh2bh(descriptor);
  107. /* AKPM: buglet - add `i' to tmp! */
  108. for (i = 0; i < bh->b_size; i += 512) {
  109. journal_header_t *tmp = (journal_header_t*)bh->b_data;
  110. tmp->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  111. tmp->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK);
  112. tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
  113. }
  114. JBUFFER_TRACE(descriptor, "write commit block");
  115. set_buffer_dirty(bh);
  116. if (journal->j_flags & JFS_BARRIER) {
  117. set_buffer_ordered(bh);
  118. barrier_done = 1;
  119. }
  120. ret = sync_dirty_buffer(bh);
  121. /* is it possible for another commit to fail at roughly
  122. * the same time as this one? If so, we don't want to
  123. * trust the barrier flag in the super, but instead want
  124. * to remember if we sent a barrier request
  125. */
  126. if (ret == -EOPNOTSUPP && barrier_done) {
  127. char b[BDEVNAME_SIZE];
  128. printk(KERN_WARNING
  129. "JBD: barrier-based sync failed on %s - "
  130. "disabling barriers\n",
  131. bdevname(journal->j_dev, b));
  132. spin_lock(&journal->j_state_lock);
  133. journal->j_flags &= ~JFS_BARRIER;
  134. spin_unlock(&journal->j_state_lock);
  135. /* And try again, without the barrier */
  136. clear_buffer_ordered(bh);
  137. set_buffer_uptodate(bh);
  138. set_buffer_dirty(bh);
  139. ret = sync_dirty_buffer(bh);
  140. }
  141. put_bh(bh); /* One for getblk() */
  142. journal_put_journal_head(descriptor);
  143. return (ret == -EIO);
  144. }
  145. /*
  146. * journal_commit_transaction
  147. *
  148. * The primary function for committing a transaction to the log. This
  149. * function is called by the journal thread to begin a complete commit.
  150. */
  151. void journal_commit_transaction(journal_t *journal)
  152. {
  153. transaction_t *commit_transaction;
  154. struct journal_head *jh, *new_jh, *descriptor;
  155. struct buffer_head **wbuf = journal->j_wbuf;
  156. int bufs;
  157. int flags;
  158. int err;
  159. unsigned long blocknr;
  160. char *tagp = NULL;
  161. journal_header_t *header;
  162. journal_block_tag_t *tag = NULL;
  163. int space_left = 0;
  164. int first_tag = 0;
  165. int tag_flag;
  166. int i;
  167. /*
  168. * First job: lock down the current transaction and wait for
  169. * all outstanding updates to complete.
  170. */
  171. #ifdef COMMIT_STATS
  172. spin_lock(&journal->j_list_lock);
  173. summarise_journal_usage(journal);
  174. spin_unlock(&journal->j_list_lock);
  175. #endif
  176. /* Do we need to erase the effects of a prior journal_flush? */
  177. if (journal->j_flags & JFS_FLUSHED) {
  178. jbd_debug(3, "super block updated\n");
  179. journal_update_superblock(journal, 1);
  180. } else {
  181. jbd_debug(3, "superblock not updated\n");
  182. }
  183. J_ASSERT(journal->j_running_transaction != NULL);
  184. J_ASSERT(journal->j_committing_transaction == NULL);
  185. commit_transaction = journal->j_running_transaction;
  186. J_ASSERT(commit_transaction->t_state == T_RUNNING);
  187. jbd_debug(1, "JBD: starting commit of transaction %d\n",
  188. commit_transaction->t_tid);
  189. spin_lock(&journal->j_state_lock);
  190. commit_transaction->t_state = T_LOCKED;
  191. spin_lock(&commit_transaction->t_handle_lock);
  192. while (commit_transaction->t_updates) {
  193. DEFINE_WAIT(wait);
  194. prepare_to_wait(&journal->j_wait_updates, &wait,
  195. TASK_UNINTERRUPTIBLE);
  196. if (commit_transaction->t_updates) {
  197. spin_unlock(&commit_transaction->t_handle_lock);
  198. spin_unlock(&journal->j_state_lock);
  199. schedule();
  200. spin_lock(&journal->j_state_lock);
  201. spin_lock(&commit_transaction->t_handle_lock);
  202. }
  203. finish_wait(&journal->j_wait_updates, &wait);
  204. }
  205. spin_unlock(&commit_transaction->t_handle_lock);
  206. J_ASSERT (commit_transaction->t_outstanding_credits <=
  207. journal->j_max_transaction_buffers);
  208. /*
  209. * First thing we are allowed to do is to discard any remaining
  210. * BJ_Reserved buffers. Note, it is _not_ permissible to assume
  211. * that there are no such buffers: if a large filesystem
  212. * operation like a truncate needs to split itself over multiple
  213. * transactions, then it may try to do a journal_restart() while
  214. * there are still BJ_Reserved buffers outstanding. These must
  215. * be released cleanly from the current transaction.
  216. *
  217. * In this case, the filesystem must still reserve write access
  218. * again before modifying the buffer in the new transaction, but
  219. * we do not require it to remember exactly which old buffers it
  220. * has reserved. This is consistent with the existing behaviour
  221. * that multiple journal_get_write_access() calls to the same
  222. * buffer are perfectly permissable.
  223. */
  224. while (commit_transaction->t_reserved_list) {
  225. jh = commit_transaction->t_reserved_list;
  226. JBUFFER_TRACE(jh, "reserved, unused: refile");
  227. /*
  228. * A journal_get_undo_access()+journal_release_buffer() may
  229. * leave undo-committed data.
  230. */
  231. if (jh->b_committed_data) {
  232. struct buffer_head *bh = jh2bh(jh);
  233. jbd_lock_bh_state(bh);
  234. kfree(jh->b_committed_data);
  235. jh->b_committed_data = NULL;
  236. jbd_unlock_bh_state(bh);
  237. }
  238. journal_refile_buffer(journal, jh);
  239. }
  240. /*
  241. * Now try to drop any written-back buffers from the journal's
  242. * checkpoint lists. We do this *before* commit because it potentially
  243. * frees some memory
  244. */
  245. spin_lock(&journal->j_list_lock);
  246. __journal_clean_checkpoint_list(journal);
  247. spin_unlock(&journal->j_list_lock);
  248. jbd_debug (3, "JBD: commit phase 1\n");
  249. /*
  250. * Switch to a new revoke table.
  251. */
  252. journal_switch_revoke_table(journal);
  253. commit_transaction->t_state = T_FLUSH;
  254. journal->j_committing_transaction = commit_transaction;
  255. journal->j_running_transaction = NULL;
  256. commit_transaction->t_log_start = journal->j_head;
  257. wake_up(&journal->j_wait_transaction_locked);
  258. spin_unlock(&journal->j_state_lock);
  259. jbd_debug (3, "JBD: commit phase 2\n");
  260. /*
  261. * First, drop modified flag: all accesses to the buffers
  262. * will be tracked for a new trasaction only -bzzz
  263. */
  264. spin_lock(&journal->j_list_lock);
  265. if (commit_transaction->t_buffers) {
  266. new_jh = jh = commit_transaction->t_buffers->b_tnext;
  267. do {
  268. J_ASSERT_JH(new_jh, new_jh->b_modified == 1 ||
  269. new_jh->b_modified == 0);
  270. new_jh->b_modified = 0;
  271. new_jh = new_jh->b_tnext;
  272. } while (new_jh != jh);
  273. }
  274. spin_unlock(&journal->j_list_lock);
  275. /*
  276. * Now start flushing things to disk, in the order they appear
  277. * on the transaction lists. Data blocks go first.
  278. */
  279. err = 0;
  280. /*
  281. * Whenever we unlock the journal and sleep, things can get added
  282. * onto ->t_sync_datalist, so we have to keep looping back to
  283. * write_out_data until we *know* that the list is empty.
  284. */
  285. bufs = 0;
  286. /*
  287. * Cleanup any flushed data buffers from the data list. Even in
  288. * abort mode, we want to flush this out as soon as possible.
  289. */
  290. write_out_data:
  291. cond_resched();
  292. spin_lock(&journal->j_list_lock);
  293. while (commit_transaction->t_sync_datalist) {
  294. struct buffer_head *bh;
  295. jh = commit_transaction->t_sync_datalist;
  296. commit_transaction->t_sync_datalist = jh->b_tnext;
  297. bh = jh2bh(jh);
  298. if (buffer_locked(bh)) {
  299. BUFFER_TRACE(bh, "locked");
  300. if (!inverted_lock(journal, bh))
  301. goto write_out_data;
  302. __journal_temp_unlink_buffer(jh);
  303. __journal_file_buffer(jh, commit_transaction,
  304. BJ_Locked);
  305. jbd_unlock_bh_state(bh);
  306. if (lock_need_resched(&journal->j_list_lock)) {
  307. spin_unlock(&journal->j_list_lock);
  308. goto write_out_data;
  309. }
  310. } else {
  311. if (buffer_dirty(bh)) {
  312. BUFFER_TRACE(bh, "start journal writeout");
  313. get_bh(bh);
  314. wbuf[bufs++] = bh;
  315. if (bufs == journal->j_wbufsize) {
  316. jbd_debug(2, "submit %d writes\n",
  317. bufs);
  318. spin_unlock(&journal->j_list_lock);
  319. ll_rw_block(SWRITE, bufs, wbuf);
  320. journal_brelse_array(wbuf, bufs);
  321. bufs = 0;
  322. goto write_out_data;
  323. }
  324. } else {
  325. BUFFER_TRACE(bh, "writeout complete: unfile");
  326. if (!inverted_lock(journal, bh))
  327. goto write_out_data;
  328. __journal_unfile_buffer(jh);
  329. jbd_unlock_bh_state(bh);
  330. journal_remove_journal_head(bh);
  331. put_bh(bh);
  332. if (lock_need_resched(&journal->j_list_lock)) {
  333. spin_unlock(&journal->j_list_lock);
  334. goto write_out_data;
  335. }
  336. }
  337. }
  338. }
  339. if (bufs) {
  340. spin_unlock(&journal->j_list_lock);
  341. ll_rw_block(SWRITE, bufs, wbuf);
  342. journal_brelse_array(wbuf, bufs);
  343. spin_lock(&journal->j_list_lock);
  344. }
  345. /*
  346. * Wait for all previously submitted IO to complete.
  347. */
  348. while (commit_transaction->t_locked_list) {
  349. struct buffer_head *bh;
  350. jh = commit_transaction->t_locked_list->b_tprev;
  351. bh = jh2bh(jh);
  352. get_bh(bh);
  353. if (buffer_locked(bh)) {
  354. spin_unlock(&journal->j_list_lock);
  355. wait_on_buffer(bh);
  356. if (unlikely(!buffer_uptodate(bh)))
  357. err = -EIO;
  358. spin_lock(&journal->j_list_lock);
  359. }
  360. if (!inverted_lock(journal, bh)) {
  361. put_bh(bh);
  362. spin_lock(&journal->j_list_lock);
  363. continue;
  364. }
  365. if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
  366. __journal_unfile_buffer(jh);
  367. jbd_unlock_bh_state(bh);
  368. journal_remove_journal_head(bh);
  369. put_bh(bh);
  370. } else {
  371. jbd_unlock_bh_state(bh);
  372. }
  373. put_bh(bh);
  374. cond_resched_lock(&journal->j_list_lock);
  375. }
  376. spin_unlock(&journal->j_list_lock);
  377. if (err)
  378. __journal_abort_hard(journal);
  379. journal_write_revoke_records(journal, commit_transaction);
  380. jbd_debug(3, "JBD: commit phase 2\n");
  381. /*
  382. * If we found any dirty or locked buffers, then we should have
  383. * looped back up to the write_out_data label. If there weren't
  384. * any then journal_clean_data_list should have wiped the list
  385. * clean by now, so check that it is in fact empty.
  386. */
  387. J_ASSERT (commit_transaction->t_sync_datalist == NULL);
  388. jbd_debug (3, "JBD: commit phase 3\n");
  389. /*
  390. * Way to go: we have now written out all of the data for a
  391. * transaction! Now comes the tricky part: we need to write out
  392. * metadata. Loop over the transaction's entire buffer list:
  393. */
  394. commit_transaction->t_state = T_COMMIT;
  395. descriptor = NULL;
  396. bufs = 0;
  397. while (commit_transaction->t_buffers) {
  398. /* Find the next buffer to be journaled... */
  399. jh = commit_transaction->t_buffers;
  400. /* If we're in abort mode, we just un-journal the buffer and
  401. release it for background writing. */
  402. if (is_journal_aborted(journal)) {
  403. JBUFFER_TRACE(jh, "journal is aborting: refile");
  404. journal_refile_buffer(journal, jh);
  405. /* If that was the last one, we need to clean up
  406. * any descriptor buffers which may have been
  407. * already allocated, even if we are now
  408. * aborting. */
  409. if (!commit_transaction->t_buffers)
  410. goto start_journal_io;
  411. continue;
  412. }
  413. /* Make sure we have a descriptor block in which to
  414. record the metadata buffer. */
  415. if (!descriptor) {
  416. struct buffer_head *bh;
  417. J_ASSERT (bufs == 0);
  418. jbd_debug(4, "JBD: get descriptor\n");
  419. descriptor = journal_get_descriptor_buffer(journal);
  420. if (!descriptor) {
  421. __journal_abort_hard(journal);
  422. continue;
  423. }
  424. bh = jh2bh(descriptor);
  425. jbd_debug(4, "JBD: got buffer %llu (%p)\n",
  426. (unsigned long long)bh->b_blocknr, bh->b_data);
  427. header = (journal_header_t *)&bh->b_data[0];
  428. header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  429. header->h_blocktype = cpu_to_be32(JFS_DESCRIPTOR_BLOCK);
  430. header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
  431. tagp = &bh->b_data[sizeof(journal_header_t)];
  432. space_left = bh->b_size - sizeof(journal_header_t);
  433. first_tag = 1;
  434. set_buffer_jwrite(bh);
  435. set_buffer_dirty(bh);
  436. wbuf[bufs++] = bh;
  437. /* Record it so that we can wait for IO
  438. completion later */
  439. BUFFER_TRACE(bh, "ph3: file as descriptor");
  440. journal_file_buffer(descriptor, commit_transaction,
  441. BJ_LogCtl);
  442. }
  443. /* Where is the buffer to be written? */
  444. err = journal_next_log_block(journal, &blocknr);
  445. /* If the block mapping failed, just abandon the buffer
  446. and repeat this loop: we'll fall into the
  447. refile-on-abort condition above. */
  448. if (err) {
  449. __journal_abort_hard(journal);
  450. continue;
  451. }
  452. /*
  453. * start_this_handle() uses t_outstanding_credits to determine
  454. * the free space in the log, but this counter is changed
  455. * by journal_next_log_block() also.
  456. */
  457. commit_transaction->t_outstanding_credits--;
  458. /* Bump b_count to prevent truncate from stumbling over
  459. the shadowed buffer! @@@ This can go if we ever get
  460. rid of the BJ_IO/BJ_Shadow pairing of buffers. */
  461. atomic_inc(&jh2bh(jh)->b_count);
  462. /* Make a temporary IO buffer with which to write it out
  463. (this will requeue both the metadata buffer and the
  464. temporary IO buffer). new_bh goes on BJ_IO*/
  465. set_bit(BH_JWrite, &jh2bh(jh)->b_state);
  466. /*
  467. * akpm: journal_write_metadata_buffer() sets
  468. * new_bh->b_transaction to commit_transaction.
  469. * We need to clean this up before we release new_bh
  470. * (which is of type BJ_IO)
  471. */
  472. JBUFFER_TRACE(jh, "ph3: write metadata");
  473. flags = journal_write_metadata_buffer(commit_transaction,
  474. jh, &new_jh, blocknr);
  475. set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
  476. wbuf[bufs++] = jh2bh(new_jh);
  477. /* Record the new block's tag in the current descriptor
  478. buffer */
  479. tag_flag = 0;
  480. if (flags & 1)
  481. tag_flag |= JFS_FLAG_ESCAPE;
  482. if (!first_tag)
  483. tag_flag |= JFS_FLAG_SAME_UUID;
  484. tag = (journal_block_tag_t *) tagp;
  485. tag->t_blocknr = cpu_to_be32(jh2bh(jh)->b_blocknr);
  486. tag->t_flags = cpu_to_be32(tag_flag);
  487. tagp += sizeof(journal_block_tag_t);
  488. space_left -= sizeof(journal_block_tag_t);
  489. if (first_tag) {
  490. memcpy (tagp, journal->j_uuid, 16);
  491. tagp += 16;
  492. space_left -= 16;
  493. first_tag = 0;
  494. }
  495. /* If there's no more to do, or if the descriptor is full,
  496. let the IO rip! */
  497. if (bufs == journal->j_wbufsize ||
  498. commit_transaction->t_buffers == NULL ||
  499. space_left < sizeof(journal_block_tag_t) + 16) {
  500. jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
  501. /* Write an end-of-descriptor marker before
  502. submitting the IOs. "tag" still points to
  503. the last tag we set up. */
  504. tag->t_flags |= cpu_to_be32(JFS_FLAG_LAST_TAG);
  505. start_journal_io:
  506. for (i = 0; i < bufs; i++) {
  507. struct buffer_head *bh = wbuf[i];
  508. lock_buffer(bh);
  509. clear_buffer_dirty(bh);
  510. set_buffer_uptodate(bh);
  511. bh->b_end_io = journal_end_buffer_io_sync;
  512. submit_bh(WRITE, bh);
  513. }
  514. cond_resched();
  515. /* Force a new descriptor to be generated next
  516. time round the loop. */
  517. descriptor = NULL;
  518. bufs = 0;
  519. }
  520. }
  521. /* Lo and behold: we have just managed to send a transaction to
  522. the log. Before we can commit it, wait for the IO so far to
  523. complete. Control buffers being written are on the
  524. transaction's t_log_list queue, and metadata buffers are on
  525. the t_iobuf_list queue.
  526. Wait for the buffers in reverse order. That way we are
  527. less likely to be woken up until all IOs have completed, and
  528. so we incur less scheduling load.
  529. */
  530. jbd_debug(3, "JBD: commit phase 4\n");
  531. /*
  532. * akpm: these are BJ_IO, and j_list_lock is not needed.
  533. * See __journal_try_to_free_buffer.
  534. */
  535. wait_for_iobuf:
  536. while (commit_transaction->t_iobuf_list != NULL) {
  537. struct buffer_head *bh;
  538. jh = commit_transaction->t_iobuf_list->b_tprev;
  539. bh = jh2bh(jh);
  540. if (buffer_locked(bh)) {
  541. wait_on_buffer(bh);
  542. goto wait_for_iobuf;
  543. }
  544. if (cond_resched())
  545. goto wait_for_iobuf;
  546. if (unlikely(!buffer_uptodate(bh)))
  547. err = -EIO;
  548. clear_buffer_jwrite(bh);
  549. JBUFFER_TRACE(jh, "ph4: unfile after journal write");
  550. journal_unfile_buffer(journal, jh);
  551. /*
  552. * ->t_iobuf_list should contain only dummy buffer_heads
  553. * which were created by journal_write_metadata_buffer().
  554. */
  555. BUFFER_TRACE(bh, "dumping temporary bh");
  556. journal_put_journal_head(jh);
  557. __brelse(bh);
  558. J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
  559. free_buffer_head(bh);
  560. /* We also have to unlock and free the corresponding
  561. shadowed buffer */
  562. jh = commit_transaction->t_shadow_list->b_tprev;
  563. bh = jh2bh(jh);
  564. clear_bit(BH_JWrite, &bh->b_state);
  565. J_ASSERT_BH(bh, buffer_jbddirty(bh));
  566. /* The metadata is now released for reuse, but we need
  567. to remember it against this transaction so that when
  568. we finally commit, we can do any checkpointing
  569. required. */
  570. JBUFFER_TRACE(jh, "file as BJ_Forget");
  571. journal_file_buffer(jh, commit_transaction, BJ_Forget);
  572. /* Wake up any transactions which were waiting for this
  573. IO to complete */
  574. wake_up_bit(&bh->b_state, BH_Unshadow);
  575. JBUFFER_TRACE(jh, "brelse shadowed buffer");
  576. __brelse(bh);
  577. }
  578. J_ASSERT (commit_transaction->t_shadow_list == NULL);
  579. jbd_debug(3, "JBD: commit phase 5\n");
  580. /* Here we wait for the revoke record and descriptor record buffers */
  581. wait_for_ctlbuf:
  582. while (commit_transaction->t_log_list != NULL) {
  583. struct buffer_head *bh;
  584. jh = commit_transaction->t_log_list->b_tprev;
  585. bh = jh2bh(jh);
  586. if (buffer_locked(bh)) {
  587. wait_on_buffer(bh);
  588. goto wait_for_ctlbuf;
  589. }
  590. if (cond_resched())
  591. goto wait_for_ctlbuf;
  592. if (unlikely(!buffer_uptodate(bh)))
  593. err = -EIO;
  594. BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
  595. clear_buffer_jwrite(bh);
  596. journal_unfile_buffer(journal, jh);
  597. journal_put_journal_head(jh);
  598. __brelse(bh); /* One for getblk */
  599. /* AKPM: bforget here */
  600. }
  601. jbd_debug(3, "JBD: commit phase 6\n");
  602. if (journal_write_commit_record(journal, commit_transaction))
  603. err = -EIO;
  604. if (err)
  605. __journal_abort_hard(journal);
  606. /* End of a transaction! Finally, we can do checkpoint
  607. processing: any buffers committed as a result of this
  608. transaction can be removed from any checkpoint list it was on
  609. before. */
  610. jbd_debug(3, "JBD: commit phase 7\n");
  611. J_ASSERT(commit_transaction->t_sync_datalist == NULL);
  612. J_ASSERT(commit_transaction->t_buffers == NULL);
  613. J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
  614. J_ASSERT(commit_transaction->t_iobuf_list == NULL);
  615. J_ASSERT(commit_transaction->t_shadow_list == NULL);
  616. J_ASSERT(commit_transaction->t_log_list == NULL);
  617. restart_loop:
  618. /*
  619. * As there are other places (journal_unmap_buffer()) adding buffers
  620. * to this list we have to be careful and hold the j_list_lock.
  621. */
  622. spin_lock(&journal->j_list_lock);
  623. while (commit_transaction->t_forget) {
  624. transaction_t *cp_transaction;
  625. struct buffer_head *bh;
  626. jh = commit_transaction->t_forget;
  627. spin_unlock(&journal->j_list_lock);
  628. bh = jh2bh(jh);
  629. jbd_lock_bh_state(bh);
  630. J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
  631. jh->b_transaction == journal->j_running_transaction);
  632. /*
  633. * If there is undo-protected committed data against
  634. * this buffer, then we can remove it now. If it is a
  635. * buffer needing such protection, the old frozen_data
  636. * field now points to a committed version of the
  637. * buffer, so rotate that field to the new committed
  638. * data.
  639. *
  640. * Otherwise, we can just throw away the frozen data now.
  641. */
  642. if (jh->b_committed_data) {
  643. kfree(jh->b_committed_data);
  644. jh->b_committed_data = NULL;
  645. if (jh->b_frozen_data) {
  646. jh->b_committed_data = jh->b_frozen_data;
  647. jh->b_frozen_data = NULL;
  648. }
  649. } else if (jh->b_frozen_data) {
  650. kfree(jh->b_frozen_data);
  651. jh->b_frozen_data = NULL;
  652. }
  653. spin_lock(&journal->j_list_lock);
  654. cp_transaction = jh->b_cp_transaction;
  655. if (cp_transaction) {
  656. JBUFFER_TRACE(jh, "remove from old cp transaction");
  657. __journal_remove_checkpoint(jh);
  658. }
  659. /* Only re-checkpoint the buffer_head if it is marked
  660. * dirty. If the buffer was added to the BJ_Forget list
  661. * by journal_forget, it may no longer be dirty and
  662. * there's no point in keeping a checkpoint record for
  663. * it. */
  664. /* A buffer which has been freed while still being
  665. * journaled by a previous transaction may end up still
  666. * being dirty here, but we want to avoid writing back
  667. * that buffer in the future now that the last use has
  668. * been committed. That's not only a performance gain,
  669. * it also stops aliasing problems if the buffer is left
  670. * behind for writeback and gets reallocated for another
  671. * use in a different page. */
  672. if (buffer_freed(bh)) {
  673. clear_buffer_freed(bh);
  674. clear_buffer_jbddirty(bh);
  675. }
  676. if (buffer_jbddirty(bh)) {
  677. JBUFFER_TRACE(jh, "add to new checkpointing trans");
  678. __journal_insert_checkpoint(jh, commit_transaction);
  679. JBUFFER_TRACE(jh, "refile for checkpoint writeback");
  680. __journal_refile_buffer(jh);
  681. jbd_unlock_bh_state(bh);
  682. } else {
  683. J_ASSERT_BH(bh, !buffer_dirty(bh));
  684. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  685. __journal_unfile_buffer(jh);
  686. jbd_unlock_bh_state(bh);
  687. journal_remove_journal_head(bh); /* needs a brelse */
  688. release_buffer_page(bh);
  689. }
  690. cond_resched_lock(&journal->j_list_lock);
  691. }
  692. spin_unlock(&journal->j_list_lock);
  693. /*
  694. * This is a bit sleazy. We borrow j_list_lock to protect
  695. * journal->j_committing_transaction in __journal_remove_checkpoint.
  696. * Really, __journal_remove_checkpoint should be using j_state_lock but
  697. * it's a bit hassle to hold that across __journal_remove_checkpoint
  698. */
  699. spin_lock(&journal->j_state_lock);
  700. spin_lock(&journal->j_list_lock);
  701. /*
  702. * Now recheck if some buffers did not get attached to the transaction
  703. * while the lock was dropped...
  704. */
  705. if (commit_transaction->t_forget) {
  706. spin_unlock(&journal->j_list_lock);
  707. spin_unlock(&journal->j_state_lock);
  708. goto restart_loop;
  709. }
  710. /* Done with this transaction! */
  711. jbd_debug(3, "JBD: commit phase 8\n");
  712. J_ASSERT(commit_transaction->t_state == T_COMMIT);
  713. commit_transaction->t_state = T_FINISHED;
  714. J_ASSERT(commit_transaction == journal->j_committing_transaction);
  715. journal->j_commit_sequence = commit_transaction->t_tid;
  716. journal->j_committing_transaction = NULL;
  717. spin_unlock(&journal->j_state_lock);
  718. if (commit_transaction->t_checkpoint_list == NULL &&
  719. commit_transaction->t_checkpoint_io_list == NULL) {
  720. __journal_drop_transaction(journal, commit_transaction);
  721. } else {
  722. if (journal->j_checkpoint_transactions == NULL) {
  723. journal->j_checkpoint_transactions = commit_transaction;
  724. commit_transaction->t_cpnext = commit_transaction;
  725. commit_transaction->t_cpprev = commit_transaction;
  726. } else {
  727. commit_transaction->t_cpnext =
  728. journal->j_checkpoint_transactions;
  729. commit_transaction->t_cpprev =
  730. commit_transaction->t_cpnext->t_cpprev;
  731. commit_transaction->t_cpnext->t_cpprev =
  732. commit_transaction;
  733. commit_transaction->t_cpprev->t_cpnext =
  734. commit_transaction;
  735. }
  736. }
  737. spin_unlock(&journal->j_list_lock);
  738. jbd_debug(1, "JBD: commit %d complete, head %d\n",
  739. journal->j_commit_sequence, journal->j_tail_sequence);
  740. wake_up(&journal->j_wait_done_commit);
  741. }