commit.c 34 KB

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