commit.c 29 KB

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