commit.c 32 KB

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