commit.c 31 KB

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