commit.c 32 KB

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