commit.c 31 KB

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