commit.c 35 KB

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