commit.c 35 KB

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