commit.c 28 KB

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