commit.c 32 KB

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