commit.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * linux/fs/jbd2/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/jbd2.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/mm.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/crc32.h>
  24. #include <linux/writeback.h>
  25. #include <linux/backing-dev.h>
  26. /*
  27. * Default IO end handler for temporary BJ_IO buffer_heads.
  28. */
  29. static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
  30. {
  31. BUFFER_TRACE(bh, "");
  32. if (uptodate)
  33. set_buffer_uptodate(bh);
  34. else
  35. clear_buffer_uptodate(bh);
  36. unlock_buffer(bh);
  37. }
  38. /*
  39. * When an ext4 file is truncated, it is possible that some pages are not
  40. * successfully freed, because they are attached to a committing transaction.
  41. * After the transaction commits, these pages are left on the LRU, with no
  42. * ->mapping, and with attached buffers. These pages are trivially reclaimable
  43. * by the VM, but their apparent absence upsets the VM accounting, and it makes
  44. * the numbers in /proc/meminfo look odd.
  45. *
  46. * So here, we have a buffer which has just come off the forget list. Look to
  47. * see if we can strip all buffers from the backing page.
  48. *
  49. * Called under lock_journal(), and possibly under journal_datalist_lock. The
  50. * caller provided us with a ref against the buffer, and we drop that here.
  51. */
  52. static void release_buffer_page(struct buffer_head *bh)
  53. {
  54. struct page *page;
  55. if (buffer_dirty(bh))
  56. goto nope;
  57. if (atomic_read(&bh->b_count) != 1)
  58. goto nope;
  59. page = bh->b_page;
  60. if (!page)
  61. goto nope;
  62. if (page->mapping)
  63. goto nope;
  64. /* OK, it's a truncated page */
  65. if (TestSetPageLocked(page))
  66. goto nope;
  67. page_cache_get(page);
  68. __brelse(bh);
  69. try_to_free_buffers(page);
  70. unlock_page(page);
  71. page_cache_release(page);
  72. return;
  73. nope:
  74. __brelse(bh);
  75. }
  76. /*
  77. * Done it all: now submit the commit record. We should have
  78. * cleaned up our previous buffers by now, so if we are in abort
  79. * mode we can now just skip the rest of the journal write
  80. * entirely.
  81. *
  82. * Returns 1 if the journal needs to be aborted or 0 on success
  83. */
  84. static int journal_submit_commit_record(journal_t *journal,
  85. transaction_t *commit_transaction,
  86. struct buffer_head **cbh,
  87. __u32 crc32_sum)
  88. {
  89. struct journal_head *descriptor;
  90. struct commit_header *tmp;
  91. struct buffer_head *bh;
  92. int ret;
  93. int barrier_done = 0;
  94. struct timespec now = current_kernel_time();
  95. if (is_journal_aborted(journal))
  96. return 0;
  97. descriptor = jbd2_journal_get_descriptor_buffer(journal);
  98. if (!descriptor)
  99. return 1;
  100. bh = jh2bh(descriptor);
  101. tmp = (struct commit_header *)bh->b_data;
  102. tmp->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
  103. tmp->h_blocktype = cpu_to_be32(JBD2_COMMIT_BLOCK);
  104. tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
  105. tmp->h_commit_sec = cpu_to_be64(now.tv_sec);
  106. tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec);
  107. if (JBD2_HAS_COMPAT_FEATURE(journal,
  108. JBD2_FEATURE_COMPAT_CHECKSUM)) {
  109. tmp->h_chksum_type = JBD2_CRC32_CHKSUM;
  110. tmp->h_chksum_size = JBD2_CRC32_CHKSUM_SIZE;
  111. tmp->h_chksum[0] = cpu_to_be32(crc32_sum);
  112. }
  113. JBUFFER_TRACE(descriptor, "submit commit block");
  114. lock_buffer(bh);
  115. get_bh(bh);
  116. set_buffer_dirty(bh);
  117. set_buffer_uptodate(bh);
  118. bh->b_end_io = journal_end_buffer_io_sync;
  119. if (journal->j_flags & JBD2_BARRIER &&
  120. !JBD2_HAS_INCOMPAT_FEATURE(journal,
  121. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
  122. set_buffer_ordered(bh);
  123. barrier_done = 1;
  124. }
  125. ret = submit_bh(WRITE, bh);
  126. if (barrier_done)
  127. clear_buffer_ordered(bh);
  128. /* is it possible for another commit to fail at roughly
  129. * the same time as this one? If so, we don't want to
  130. * trust the barrier flag in the super, but instead want
  131. * to remember if we sent a barrier request
  132. */
  133. if (ret == -EOPNOTSUPP && barrier_done) {
  134. char b[BDEVNAME_SIZE];
  135. printk(KERN_WARNING
  136. "JBD: barrier-based sync failed on %s - "
  137. "disabling barriers\n",
  138. bdevname(journal->j_dev, b));
  139. spin_lock(&journal->j_state_lock);
  140. journal->j_flags &= ~JBD2_BARRIER;
  141. spin_unlock(&journal->j_state_lock);
  142. /* And try again, without the barrier */
  143. lock_buffer(bh);
  144. set_buffer_uptodate(bh);
  145. set_buffer_dirty(bh);
  146. ret = submit_bh(WRITE, bh);
  147. }
  148. *cbh = bh;
  149. return ret;
  150. }
  151. /*
  152. * This function along with journal_submit_commit_record
  153. * allows to write the commit record asynchronously.
  154. */
  155. static int journal_wait_on_commit_record(struct buffer_head *bh)
  156. {
  157. int ret = 0;
  158. clear_buffer_dirty(bh);
  159. wait_on_buffer(bh);
  160. if (unlikely(!buffer_uptodate(bh)))
  161. ret = -EIO;
  162. put_bh(bh); /* One for getblk() */
  163. jbd2_journal_put_journal_head(bh2jh(bh));
  164. return ret;
  165. }
  166. /*
  167. * write the filemap data using writepage() address_space_operations.
  168. * We don't do block allocation here even for delalloc. We don't
  169. * use writepages() because with dealyed allocation we may be doing
  170. * block allocation in writepages().
  171. */
  172. static int journal_submit_inode_data_buffers(struct address_space *mapping)
  173. {
  174. int ret;
  175. struct writeback_control wbc = {
  176. .sync_mode = WB_SYNC_ALL,
  177. .nr_to_write = mapping->nrpages * 2,
  178. .range_start = 0,
  179. .range_end = i_size_read(mapping->host),
  180. .for_writepages = 1,
  181. };
  182. ret = generic_writepages(mapping, &wbc);
  183. return ret;
  184. }
  185. /*
  186. * Submit all the data buffers of inode associated with the transaction to
  187. * disk.
  188. *
  189. * We are in a committing transaction. Therefore no new inode can be added to
  190. * our inode list. We use JI_COMMIT_RUNNING flag to protect inode we currently
  191. * operate on from being released while we write out pages.
  192. */
  193. static int journal_submit_data_buffers(journal_t *journal,
  194. transaction_t *commit_transaction)
  195. {
  196. struct jbd2_inode *jinode;
  197. int err, ret = 0;
  198. struct address_space *mapping;
  199. spin_lock(&journal->j_list_lock);
  200. list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
  201. mapping = jinode->i_vfs_inode->i_mapping;
  202. jinode->i_flags |= JI_COMMIT_RUNNING;
  203. spin_unlock(&journal->j_list_lock);
  204. /*
  205. * submit the inode data buffers. We use writepage
  206. * instead of writepages. Because writepages can do
  207. * block allocation with delalloc. We need to write
  208. * only allocated blocks here.
  209. */
  210. err = journal_submit_inode_data_buffers(mapping);
  211. if (!ret)
  212. ret = err;
  213. spin_lock(&journal->j_list_lock);
  214. J_ASSERT(jinode->i_transaction == commit_transaction);
  215. jinode->i_flags &= ~JI_COMMIT_RUNNING;
  216. wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
  217. }
  218. spin_unlock(&journal->j_list_lock);
  219. return ret;
  220. }
  221. /*
  222. * Wait for data submitted for writeout, refile inodes to proper
  223. * transaction if needed.
  224. *
  225. */
  226. static int journal_finish_inode_data_buffers(journal_t *journal,
  227. transaction_t *commit_transaction)
  228. {
  229. struct jbd2_inode *jinode, *next_i;
  230. int err, ret = 0;
  231. /* For locking, see the comment in journal_submit_data_buffers() */
  232. spin_lock(&journal->j_list_lock);
  233. list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
  234. jinode->i_flags |= JI_COMMIT_RUNNING;
  235. spin_unlock(&journal->j_list_lock);
  236. err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping);
  237. if (!ret)
  238. ret = err;
  239. spin_lock(&journal->j_list_lock);
  240. jinode->i_flags &= ~JI_COMMIT_RUNNING;
  241. wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
  242. }
  243. /* Now refile inode to proper lists */
  244. list_for_each_entry_safe(jinode, next_i,
  245. &commit_transaction->t_inode_list, i_list) {
  246. list_del(&jinode->i_list);
  247. if (jinode->i_next_transaction) {
  248. jinode->i_transaction = jinode->i_next_transaction;
  249. jinode->i_next_transaction = NULL;
  250. list_add(&jinode->i_list,
  251. &jinode->i_transaction->t_inode_list);
  252. } else {
  253. jinode->i_transaction = NULL;
  254. }
  255. }
  256. spin_unlock(&journal->j_list_lock);
  257. return ret;
  258. }
  259. static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
  260. {
  261. struct page *page = bh->b_page;
  262. char *addr;
  263. __u32 checksum;
  264. addr = kmap_atomic(page, KM_USER0);
  265. checksum = crc32_be(crc32_sum,
  266. (void *)(addr + offset_in_page(bh->b_data)), bh->b_size);
  267. kunmap_atomic(addr, KM_USER0);
  268. return checksum;
  269. }
  270. static void write_tag_block(int tag_bytes, journal_block_tag_t *tag,
  271. unsigned long long block)
  272. {
  273. tag->t_blocknr = cpu_to_be32(block & (u32)~0);
  274. if (tag_bytes > JBD2_TAG_SIZE32)
  275. tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1);
  276. }
  277. /*
  278. * jbd2_journal_commit_transaction
  279. *
  280. * The primary function for committing a transaction to the log. This
  281. * function is called by the journal thread to begin a complete commit.
  282. */
  283. void jbd2_journal_commit_transaction(journal_t *journal)
  284. {
  285. struct transaction_stats_s stats;
  286. transaction_t *commit_transaction;
  287. struct journal_head *jh, *new_jh, *descriptor;
  288. struct buffer_head **wbuf = journal->j_wbuf;
  289. int bufs;
  290. int flags;
  291. int err;
  292. unsigned long long blocknr;
  293. char *tagp = NULL;
  294. journal_header_t *header;
  295. journal_block_tag_t *tag = NULL;
  296. int space_left = 0;
  297. int first_tag = 0;
  298. int tag_flag;
  299. int i;
  300. int tag_bytes = journal_tag_bytes(journal);
  301. struct buffer_head *cbh = NULL; /* For transactional checksums */
  302. __u32 crc32_sum = ~0;
  303. /*
  304. * First job: lock down the current transaction and wait for
  305. * all outstanding updates to complete.
  306. */
  307. #ifdef COMMIT_STATS
  308. spin_lock(&journal->j_list_lock);
  309. summarise_journal_usage(journal);
  310. spin_unlock(&journal->j_list_lock);
  311. #endif
  312. /* Do we need to erase the effects of a prior jbd2_journal_flush? */
  313. if (journal->j_flags & JBD2_FLUSHED) {
  314. jbd_debug(3, "super block updated\n");
  315. jbd2_journal_update_superblock(journal, 1);
  316. } else {
  317. jbd_debug(3, "superblock not updated\n");
  318. }
  319. J_ASSERT(journal->j_running_transaction != NULL);
  320. J_ASSERT(journal->j_committing_transaction == NULL);
  321. commit_transaction = journal->j_running_transaction;
  322. J_ASSERT(commit_transaction->t_state == T_RUNNING);
  323. jbd_debug(1, "JBD: starting commit of transaction %d\n",
  324. commit_transaction->t_tid);
  325. spin_lock(&journal->j_state_lock);
  326. commit_transaction->t_state = T_LOCKED;
  327. stats.u.run.rs_wait = commit_transaction->t_max_wait;
  328. stats.u.run.rs_locked = jiffies;
  329. stats.u.run.rs_running = jbd2_time_diff(commit_transaction->t_start,
  330. stats.u.run.rs_locked);
  331. spin_lock(&commit_transaction->t_handle_lock);
  332. while (commit_transaction->t_updates) {
  333. DEFINE_WAIT(wait);
  334. prepare_to_wait(&journal->j_wait_updates, &wait,
  335. TASK_UNINTERRUPTIBLE);
  336. if (commit_transaction->t_updates) {
  337. spin_unlock(&commit_transaction->t_handle_lock);
  338. spin_unlock(&journal->j_state_lock);
  339. schedule();
  340. spin_lock(&journal->j_state_lock);
  341. spin_lock(&commit_transaction->t_handle_lock);
  342. }
  343. finish_wait(&journal->j_wait_updates, &wait);
  344. }
  345. spin_unlock(&commit_transaction->t_handle_lock);
  346. J_ASSERT (commit_transaction->t_outstanding_credits <=
  347. journal->j_max_transaction_buffers);
  348. /*
  349. * First thing we are allowed to do is to discard any remaining
  350. * BJ_Reserved buffers. Note, it is _not_ permissible to assume
  351. * that there are no such buffers: if a large filesystem
  352. * operation like a truncate needs to split itself over multiple
  353. * transactions, then it may try to do a jbd2_journal_restart() while
  354. * there are still BJ_Reserved buffers outstanding. These must
  355. * be released cleanly from the current transaction.
  356. *
  357. * In this case, the filesystem must still reserve write access
  358. * again before modifying the buffer in the new transaction, but
  359. * we do not require it to remember exactly which old buffers it
  360. * has reserved. This is consistent with the existing behaviour
  361. * that multiple jbd2_journal_get_write_access() calls to the same
  362. * buffer are perfectly permissable.
  363. */
  364. while (commit_transaction->t_reserved_list) {
  365. jh = commit_transaction->t_reserved_list;
  366. JBUFFER_TRACE(jh, "reserved, unused: refile");
  367. /*
  368. * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may
  369. * leave undo-committed data.
  370. */
  371. if (jh->b_committed_data) {
  372. struct buffer_head *bh = jh2bh(jh);
  373. jbd_lock_bh_state(bh);
  374. jbd2_free(jh->b_committed_data, bh->b_size);
  375. jh->b_committed_data = NULL;
  376. jbd_unlock_bh_state(bh);
  377. }
  378. jbd2_journal_refile_buffer(journal, jh);
  379. }
  380. /*
  381. * Now try to drop any written-back buffers from the journal's
  382. * checkpoint lists. We do this *before* commit because it potentially
  383. * frees some memory
  384. */
  385. spin_lock(&journal->j_list_lock);
  386. __jbd2_journal_clean_checkpoint_list(journal);
  387. spin_unlock(&journal->j_list_lock);
  388. jbd_debug (3, "JBD: commit phase 1\n");
  389. /*
  390. * Switch to a new revoke table.
  391. */
  392. jbd2_journal_switch_revoke_table(journal);
  393. stats.u.run.rs_flushing = jiffies;
  394. stats.u.run.rs_locked = jbd2_time_diff(stats.u.run.rs_locked,
  395. stats.u.run.rs_flushing);
  396. commit_transaction->t_state = T_FLUSH;
  397. journal->j_committing_transaction = commit_transaction;
  398. journal->j_running_transaction = NULL;
  399. commit_transaction->t_log_start = journal->j_head;
  400. wake_up(&journal->j_wait_transaction_locked);
  401. spin_unlock(&journal->j_state_lock);
  402. jbd_debug (3, "JBD: commit phase 2\n");
  403. /*
  404. * Now start flushing things to disk, in the order they appear
  405. * on the transaction lists. Data blocks go first.
  406. */
  407. err = journal_submit_data_buffers(journal, commit_transaction);
  408. if (err)
  409. jbd2_journal_abort(journal, err);
  410. jbd2_journal_write_revoke_records(journal, commit_transaction);
  411. jbd_debug(3, "JBD: commit phase 2\n");
  412. /*
  413. * Way to go: we have now written out all of the data for a
  414. * transaction! Now comes the tricky part: we need to write out
  415. * metadata. Loop over the transaction's entire buffer list:
  416. */
  417. spin_lock(&journal->j_state_lock);
  418. commit_transaction->t_state = T_COMMIT;
  419. spin_unlock(&journal->j_state_lock);
  420. stats.u.run.rs_logging = jiffies;
  421. stats.u.run.rs_flushing = jbd2_time_diff(stats.u.run.rs_flushing,
  422. stats.u.run.rs_logging);
  423. stats.u.run.rs_blocks = commit_transaction->t_outstanding_credits;
  424. stats.u.run.rs_blocks_logged = 0;
  425. J_ASSERT(commit_transaction->t_nr_buffers <=
  426. commit_transaction->t_outstanding_credits);
  427. err = 0;
  428. descriptor = NULL;
  429. bufs = 0;
  430. while (commit_transaction->t_buffers) {
  431. /* Find the next buffer to be journaled... */
  432. jh = commit_transaction->t_buffers;
  433. /* If we're in abort mode, we just un-journal the buffer and
  434. release it for background writing. */
  435. if (is_journal_aborted(journal)) {
  436. JBUFFER_TRACE(jh, "journal is aborting: refile");
  437. jbd2_journal_refile_buffer(journal, jh);
  438. /* If that was the last one, we need to clean up
  439. * any descriptor buffers which may have been
  440. * already allocated, even if we are now
  441. * aborting. */
  442. if (!commit_transaction->t_buffers)
  443. goto start_journal_io;
  444. continue;
  445. }
  446. /* Make sure we have a descriptor block in which to
  447. record the metadata buffer. */
  448. if (!descriptor) {
  449. struct buffer_head *bh;
  450. J_ASSERT (bufs == 0);
  451. jbd_debug(4, "JBD: get descriptor\n");
  452. descriptor = jbd2_journal_get_descriptor_buffer(journal);
  453. if (!descriptor) {
  454. jbd2_journal_abort(journal, -EIO);
  455. continue;
  456. }
  457. bh = jh2bh(descriptor);
  458. jbd_debug(4, "JBD: got buffer %llu (%p)\n",
  459. (unsigned long long)bh->b_blocknr, bh->b_data);
  460. header = (journal_header_t *)&bh->b_data[0];
  461. header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
  462. header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK);
  463. header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
  464. tagp = &bh->b_data[sizeof(journal_header_t)];
  465. space_left = bh->b_size - sizeof(journal_header_t);
  466. first_tag = 1;
  467. set_buffer_jwrite(bh);
  468. set_buffer_dirty(bh);
  469. wbuf[bufs++] = bh;
  470. /* Record it so that we can wait for IO
  471. completion later */
  472. BUFFER_TRACE(bh, "ph3: file as descriptor");
  473. jbd2_journal_file_buffer(descriptor, commit_transaction,
  474. BJ_LogCtl);
  475. }
  476. /* Where is the buffer to be written? */
  477. err = jbd2_journal_next_log_block(journal, &blocknr);
  478. /* If the block mapping failed, just abandon the buffer
  479. and repeat this loop: we'll fall into the
  480. refile-on-abort condition above. */
  481. if (err) {
  482. jbd2_journal_abort(journal, err);
  483. continue;
  484. }
  485. /*
  486. * start_this_handle() uses t_outstanding_credits to determine
  487. * the free space in the log, but this counter is changed
  488. * by jbd2_journal_next_log_block() also.
  489. */
  490. commit_transaction->t_outstanding_credits--;
  491. /* Bump b_count to prevent truncate from stumbling over
  492. the shadowed buffer! @@@ This can go if we ever get
  493. rid of the BJ_IO/BJ_Shadow pairing of buffers. */
  494. atomic_inc(&jh2bh(jh)->b_count);
  495. /* Make a temporary IO buffer with which to write it out
  496. (this will requeue both the metadata buffer and the
  497. temporary IO buffer). new_bh goes on BJ_IO*/
  498. set_bit(BH_JWrite, &jh2bh(jh)->b_state);
  499. /*
  500. * akpm: jbd2_journal_write_metadata_buffer() sets
  501. * new_bh->b_transaction to commit_transaction.
  502. * We need to clean this up before we release new_bh
  503. * (which is of type BJ_IO)
  504. */
  505. JBUFFER_TRACE(jh, "ph3: write metadata");
  506. flags = jbd2_journal_write_metadata_buffer(commit_transaction,
  507. jh, &new_jh, blocknr);
  508. set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
  509. wbuf[bufs++] = jh2bh(new_jh);
  510. /* Record the new block's tag in the current descriptor
  511. buffer */
  512. tag_flag = 0;
  513. if (flags & 1)
  514. tag_flag |= JBD2_FLAG_ESCAPE;
  515. if (!first_tag)
  516. tag_flag |= JBD2_FLAG_SAME_UUID;
  517. tag = (journal_block_tag_t *) tagp;
  518. write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr);
  519. tag->t_flags = cpu_to_be32(tag_flag);
  520. tagp += tag_bytes;
  521. space_left -= tag_bytes;
  522. if (first_tag) {
  523. memcpy (tagp, journal->j_uuid, 16);
  524. tagp += 16;
  525. space_left -= 16;
  526. first_tag = 0;
  527. }
  528. /* If there's no more to do, or if the descriptor is full,
  529. let the IO rip! */
  530. if (bufs == journal->j_wbufsize ||
  531. commit_transaction->t_buffers == NULL ||
  532. space_left < tag_bytes + 16) {
  533. jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
  534. /* Write an end-of-descriptor marker before
  535. submitting the IOs. "tag" still points to
  536. the last tag we set up. */
  537. tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG);
  538. start_journal_io:
  539. for (i = 0; i < bufs; i++) {
  540. struct buffer_head *bh = wbuf[i];
  541. /*
  542. * Compute checksum.
  543. */
  544. if (JBD2_HAS_COMPAT_FEATURE(journal,
  545. JBD2_FEATURE_COMPAT_CHECKSUM)) {
  546. crc32_sum =
  547. jbd2_checksum_data(crc32_sum, bh);
  548. }
  549. lock_buffer(bh);
  550. clear_buffer_dirty(bh);
  551. set_buffer_uptodate(bh);
  552. bh->b_end_io = journal_end_buffer_io_sync;
  553. submit_bh(WRITE, bh);
  554. }
  555. cond_resched();
  556. stats.u.run.rs_blocks_logged += bufs;
  557. /* Force a new descriptor to be generated next
  558. time round the loop. */
  559. descriptor = NULL;
  560. bufs = 0;
  561. }
  562. }
  563. /* Done it all: now write the commit record asynchronously. */
  564. if (JBD2_HAS_INCOMPAT_FEATURE(journal,
  565. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
  566. err = journal_submit_commit_record(journal, commit_transaction,
  567. &cbh, crc32_sum);
  568. if (err)
  569. __jbd2_journal_abort_hard(journal);
  570. }
  571. /*
  572. * This is the right place to wait for data buffers both for ASYNC
  573. * and !ASYNC commit. If commit is ASYNC, we need to wait only after
  574. * the commit block went to disk (which happens above). If commit is
  575. * SYNC, we need to wait for data buffers before we start writing
  576. * commit block, which happens below in such setting.
  577. */
  578. err = journal_finish_inode_data_buffers(journal, commit_transaction);
  579. if (err)
  580. jbd2_journal_abort(journal, err);
  581. /* Lo and behold: we have just managed to send a transaction to
  582. the log. Before we can commit it, wait for the IO so far to
  583. complete. Control buffers being written are on the
  584. transaction's t_log_list queue, and metadata buffers are on
  585. the t_iobuf_list queue.
  586. Wait for the buffers in reverse order. That way we are
  587. less likely to be woken up until all IOs have completed, and
  588. so we incur less scheduling load.
  589. */
  590. jbd_debug(3, "JBD: commit phase 3\n");
  591. /*
  592. * akpm: these are BJ_IO, and j_list_lock is not needed.
  593. * See __journal_try_to_free_buffer.
  594. */
  595. wait_for_iobuf:
  596. while (commit_transaction->t_iobuf_list != NULL) {
  597. struct buffer_head *bh;
  598. jh = commit_transaction->t_iobuf_list->b_tprev;
  599. bh = jh2bh(jh);
  600. if (buffer_locked(bh)) {
  601. wait_on_buffer(bh);
  602. goto wait_for_iobuf;
  603. }
  604. if (cond_resched())
  605. goto wait_for_iobuf;
  606. if (unlikely(!buffer_uptodate(bh)))
  607. err = -EIO;
  608. clear_buffer_jwrite(bh);
  609. JBUFFER_TRACE(jh, "ph4: unfile after journal write");
  610. jbd2_journal_unfile_buffer(journal, jh);
  611. /*
  612. * ->t_iobuf_list should contain only dummy buffer_heads
  613. * which were created by jbd2_journal_write_metadata_buffer().
  614. */
  615. BUFFER_TRACE(bh, "dumping temporary bh");
  616. jbd2_journal_put_journal_head(jh);
  617. __brelse(bh);
  618. J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
  619. free_buffer_head(bh);
  620. /* We also have to unlock and free the corresponding
  621. shadowed buffer */
  622. jh = commit_transaction->t_shadow_list->b_tprev;
  623. bh = jh2bh(jh);
  624. clear_bit(BH_JWrite, &bh->b_state);
  625. J_ASSERT_BH(bh, buffer_jbddirty(bh));
  626. /* The metadata is now released for reuse, but we need
  627. to remember it against this transaction so that when
  628. we finally commit, we can do any checkpointing
  629. required. */
  630. JBUFFER_TRACE(jh, "file as BJ_Forget");
  631. jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget);
  632. /* Wake up any transactions which were waiting for this
  633. IO to complete */
  634. wake_up_bit(&bh->b_state, BH_Unshadow);
  635. JBUFFER_TRACE(jh, "brelse shadowed buffer");
  636. __brelse(bh);
  637. }
  638. J_ASSERT (commit_transaction->t_shadow_list == NULL);
  639. jbd_debug(3, "JBD: commit phase 4\n");
  640. /* Here we wait for the revoke record and descriptor record buffers */
  641. wait_for_ctlbuf:
  642. while (commit_transaction->t_log_list != NULL) {
  643. struct buffer_head *bh;
  644. jh = commit_transaction->t_log_list->b_tprev;
  645. bh = jh2bh(jh);
  646. if (buffer_locked(bh)) {
  647. wait_on_buffer(bh);
  648. goto wait_for_ctlbuf;
  649. }
  650. if (cond_resched())
  651. goto wait_for_ctlbuf;
  652. if (unlikely(!buffer_uptodate(bh)))
  653. err = -EIO;
  654. BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
  655. clear_buffer_jwrite(bh);
  656. jbd2_journal_unfile_buffer(journal, jh);
  657. jbd2_journal_put_journal_head(jh);
  658. __brelse(bh); /* One for getblk */
  659. /* AKPM: bforget here */
  660. }
  661. jbd_debug(3, "JBD: commit phase 5\n");
  662. if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
  663. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
  664. err = journal_submit_commit_record(journal, commit_transaction,
  665. &cbh, crc32_sum);
  666. if (err)
  667. __jbd2_journal_abort_hard(journal);
  668. }
  669. if (!err && !is_journal_aborted(journal))
  670. err = journal_wait_on_commit_record(cbh);
  671. if (err)
  672. jbd2_journal_abort(journal, err);
  673. /* End of a transaction! Finally, we can do checkpoint
  674. processing: any buffers committed as a result of this
  675. transaction can be removed from any checkpoint list it was on
  676. before. */
  677. jbd_debug(3, "JBD: commit phase 6\n");
  678. J_ASSERT(list_empty(&commit_transaction->t_inode_list));
  679. J_ASSERT(commit_transaction->t_buffers == NULL);
  680. J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
  681. J_ASSERT(commit_transaction->t_iobuf_list == NULL);
  682. J_ASSERT(commit_transaction->t_shadow_list == NULL);
  683. J_ASSERT(commit_transaction->t_log_list == NULL);
  684. restart_loop:
  685. /*
  686. * As there are other places (journal_unmap_buffer()) adding buffers
  687. * to this list we have to be careful and hold the j_list_lock.
  688. */
  689. spin_lock(&journal->j_list_lock);
  690. while (commit_transaction->t_forget) {
  691. transaction_t *cp_transaction;
  692. struct buffer_head *bh;
  693. jh = commit_transaction->t_forget;
  694. spin_unlock(&journal->j_list_lock);
  695. bh = jh2bh(jh);
  696. jbd_lock_bh_state(bh);
  697. J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
  698. jh->b_transaction == journal->j_running_transaction);
  699. /*
  700. * If there is undo-protected committed data against
  701. * this buffer, then we can remove it now. If it is a
  702. * buffer needing such protection, the old frozen_data
  703. * field now points to a committed version of the
  704. * buffer, so rotate that field to the new committed
  705. * data.
  706. *
  707. * Otherwise, we can just throw away the frozen data now.
  708. */
  709. if (jh->b_committed_data) {
  710. jbd2_free(jh->b_committed_data, bh->b_size);
  711. jh->b_committed_data = NULL;
  712. if (jh->b_frozen_data) {
  713. jh->b_committed_data = jh->b_frozen_data;
  714. jh->b_frozen_data = NULL;
  715. }
  716. } else if (jh->b_frozen_data) {
  717. jbd2_free(jh->b_frozen_data, bh->b_size);
  718. jh->b_frozen_data = NULL;
  719. }
  720. spin_lock(&journal->j_list_lock);
  721. cp_transaction = jh->b_cp_transaction;
  722. if (cp_transaction) {
  723. JBUFFER_TRACE(jh, "remove from old cp transaction");
  724. cp_transaction->t_chp_stats.cs_dropped++;
  725. __jbd2_journal_remove_checkpoint(jh);
  726. }
  727. /* Only re-checkpoint the buffer_head if it is marked
  728. * dirty. If the buffer was added to the BJ_Forget list
  729. * by jbd2_journal_forget, it may no longer be dirty and
  730. * there's no point in keeping a checkpoint record for
  731. * it. */
  732. /* A buffer which has been freed while still being
  733. * journaled by a previous transaction may end up still
  734. * being dirty here, but we want to avoid writing back
  735. * that buffer in the future now that the last use has
  736. * been committed. That's not only a performance gain,
  737. * it also stops aliasing problems if the buffer is left
  738. * behind for writeback and gets reallocated for another
  739. * use in a different page. */
  740. if (buffer_freed(bh)) {
  741. clear_buffer_freed(bh);
  742. clear_buffer_jbddirty(bh);
  743. }
  744. if (buffer_jbddirty(bh)) {
  745. JBUFFER_TRACE(jh, "add to new checkpointing trans");
  746. __jbd2_journal_insert_checkpoint(jh, commit_transaction);
  747. JBUFFER_TRACE(jh, "refile for checkpoint writeback");
  748. __jbd2_journal_refile_buffer(jh);
  749. jbd_unlock_bh_state(bh);
  750. } else {
  751. J_ASSERT_BH(bh, !buffer_dirty(bh));
  752. /* The buffer on BJ_Forget list and not jbddirty means
  753. * it has been freed by this transaction and hence it
  754. * could not have been reallocated until this
  755. * transaction has committed. *BUT* it could be
  756. * reallocated once we have written all the data to
  757. * disk and before we process the buffer on BJ_Forget
  758. * list. */
  759. JBUFFER_TRACE(jh, "refile or unfile freed buffer");
  760. __jbd2_journal_refile_buffer(jh);
  761. if (!jh->b_transaction) {
  762. jbd_unlock_bh_state(bh);
  763. /* needs a brelse */
  764. jbd2_journal_remove_journal_head(bh);
  765. release_buffer_page(bh);
  766. } else
  767. jbd_unlock_bh_state(bh);
  768. }
  769. cond_resched_lock(&journal->j_list_lock);
  770. }
  771. spin_unlock(&journal->j_list_lock);
  772. /*
  773. * This is a bit sleazy. We use j_list_lock to protect transition
  774. * of a transaction into T_FINISHED state and calling
  775. * __jbd2_journal_drop_transaction(). Otherwise we could race with
  776. * other checkpointing code processing the transaction...
  777. */
  778. spin_lock(&journal->j_state_lock);
  779. spin_lock(&journal->j_list_lock);
  780. /*
  781. * Now recheck if some buffers did not get attached to the transaction
  782. * while the lock was dropped...
  783. */
  784. if (commit_transaction->t_forget) {
  785. spin_unlock(&journal->j_list_lock);
  786. spin_unlock(&journal->j_state_lock);
  787. goto restart_loop;
  788. }
  789. /* Done with this transaction! */
  790. jbd_debug(3, "JBD: commit phase 7\n");
  791. J_ASSERT(commit_transaction->t_state == T_COMMIT);
  792. commit_transaction->t_start = jiffies;
  793. stats.u.run.rs_logging = jbd2_time_diff(stats.u.run.rs_logging,
  794. commit_transaction->t_start);
  795. /*
  796. * File the transaction for history
  797. */
  798. stats.ts_type = JBD2_STATS_RUN;
  799. stats.ts_tid = commit_transaction->t_tid;
  800. stats.u.run.rs_handle_count = commit_transaction->t_handle_count;
  801. spin_lock(&journal->j_history_lock);
  802. memcpy(journal->j_history + journal->j_history_cur, &stats,
  803. sizeof(stats));
  804. if (++journal->j_history_cur == journal->j_history_max)
  805. journal->j_history_cur = 0;
  806. /*
  807. * Calculate overall stats
  808. */
  809. journal->j_stats.ts_tid++;
  810. journal->j_stats.u.run.rs_wait += stats.u.run.rs_wait;
  811. journal->j_stats.u.run.rs_running += stats.u.run.rs_running;
  812. journal->j_stats.u.run.rs_locked += stats.u.run.rs_locked;
  813. journal->j_stats.u.run.rs_flushing += stats.u.run.rs_flushing;
  814. journal->j_stats.u.run.rs_logging += stats.u.run.rs_logging;
  815. journal->j_stats.u.run.rs_handle_count += stats.u.run.rs_handle_count;
  816. journal->j_stats.u.run.rs_blocks += stats.u.run.rs_blocks;
  817. journal->j_stats.u.run.rs_blocks_logged += stats.u.run.rs_blocks_logged;
  818. spin_unlock(&journal->j_history_lock);
  819. commit_transaction->t_state = T_FINISHED;
  820. J_ASSERT(commit_transaction == journal->j_committing_transaction);
  821. journal->j_commit_sequence = commit_transaction->t_tid;
  822. journal->j_committing_transaction = NULL;
  823. spin_unlock(&journal->j_state_lock);
  824. if (commit_transaction->t_checkpoint_list == NULL &&
  825. commit_transaction->t_checkpoint_io_list == NULL) {
  826. __jbd2_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. }