commit.c 35 KB

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