commit.c 34 KB

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