commit.c 27 KB

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