commit.c 34 KB

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