commit.c 34 KB

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