commit.c 27 KB

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