commit.c 34 KB

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