commit.c 34 KB

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