commit.c 28 KB

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