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