commit.c 35 KB

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