commit.c 27 KB

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