commit.c 28 KB

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