commit.c 31 KB

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