commit.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * linux/fs/jbd/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/jbd.h>
  18. #include <linux/errno.h>
  19. #include <linux/mm.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/bio.h>
  22. #include <linux/blkdev.h>
  23. #include <trace/events/jbd.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 successfully 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 journal->j_list_lock. The caller provided us with a ref
  48. * 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 (!trylock_page(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. * Decrement reference counter for data buffer. If it has been marked
  76. * 'BH_Freed', release it and the page to which it belongs if possible.
  77. */
  78. static void release_data_buffer(struct buffer_head *bh)
  79. {
  80. if (buffer_freed(bh)) {
  81. WARN_ON_ONCE(buffer_dirty(bh));
  82. clear_buffer_freed(bh);
  83. clear_buffer_mapped(bh);
  84. clear_buffer_new(bh);
  85. clear_buffer_req(bh);
  86. bh->b_bdev = NULL;
  87. release_buffer_page(bh);
  88. } else
  89. put_bh(bh);
  90. }
  91. /*
  92. * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is
  93. * held. For ranking reasons we must trylock. If we lose, schedule away and
  94. * return 0. j_list_lock is dropped in this case.
  95. */
  96. static int inverted_lock(journal_t *journal, struct buffer_head *bh)
  97. {
  98. if (!jbd_trylock_bh_state(bh)) {
  99. spin_unlock(&journal->j_list_lock);
  100. schedule();
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. /* Done it all: now write the commit record. We should have
  106. * cleaned up our previous buffers by now, so if we are in abort
  107. * mode we can now just skip the rest of the journal write
  108. * entirely.
  109. *
  110. * Returns 1 if the journal needs to be aborted or 0 on success
  111. */
  112. static int journal_write_commit_record(journal_t *journal,
  113. transaction_t *commit_transaction)
  114. {
  115. struct journal_head *descriptor;
  116. struct buffer_head *bh;
  117. journal_header_t *header;
  118. int ret;
  119. if (is_journal_aborted(journal))
  120. return 0;
  121. descriptor = journal_get_descriptor_buffer(journal);
  122. if (!descriptor)
  123. return 1;
  124. bh = jh2bh(descriptor);
  125. header = (journal_header_t *)(bh->b_data);
  126. header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  127. header->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK);
  128. header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
  129. JBUFFER_TRACE(descriptor, "write commit block");
  130. set_buffer_dirty(bh);
  131. if (journal->j_flags & JFS_BARRIER)
  132. ret = __sync_dirty_buffer(bh, WRITE_SYNC | WRITE_FLUSH_FUA);
  133. else
  134. ret = sync_dirty_buffer(bh);
  135. put_bh(bh); /* One for getblk() */
  136. journal_put_journal_head(descriptor);
  137. return (ret == -EIO);
  138. }
  139. static void journal_do_submit_data(struct buffer_head **wbuf, int bufs,
  140. int write_op)
  141. {
  142. int i;
  143. for (i = 0; i < bufs; i++) {
  144. wbuf[i]->b_end_io = end_buffer_write_sync;
  145. /*
  146. * Here we write back pagecache data that may be mmaped. Since
  147. * we cannot afford to clean the page and set PageWriteback
  148. * here due to lock ordering (page lock ranks above transaction
  149. * start), the data can change while IO is in flight. Tell the
  150. * block layer it should bounce the bio pages if stable data
  151. * during write is required.
  152. *
  153. * We use up our safety reference in submit_bh().
  154. */
  155. _submit_bh(write_op, wbuf[i], 1 << BIO_SNAP_STABLE);
  156. }
  157. }
  158. /*
  159. * Submit all the data buffers to disk
  160. */
  161. static int journal_submit_data_buffers(journal_t *journal,
  162. transaction_t *commit_transaction,
  163. int write_op)
  164. {
  165. struct journal_head *jh;
  166. struct buffer_head *bh;
  167. int locked;
  168. int bufs = 0;
  169. struct buffer_head **wbuf = journal->j_wbuf;
  170. int err = 0;
  171. /*
  172. * Whenever we unlock the journal and sleep, things can get added
  173. * onto ->t_sync_datalist, so we have to keep looping back to
  174. * write_out_data until we *know* that the list is empty.
  175. *
  176. * Cleanup any flushed data buffers from the data list. Even in
  177. * abort mode, we want to flush this out as soon as possible.
  178. */
  179. write_out_data:
  180. cond_resched();
  181. spin_lock(&journal->j_list_lock);
  182. while (commit_transaction->t_sync_datalist) {
  183. jh = commit_transaction->t_sync_datalist;
  184. bh = jh2bh(jh);
  185. locked = 0;
  186. /* Get reference just to make sure buffer does not disappear
  187. * when we are forced to drop various locks */
  188. get_bh(bh);
  189. /* If the buffer is dirty, we need to submit IO and hence
  190. * we need the buffer lock. We try to lock the buffer without
  191. * blocking. If we fail, we need to drop j_list_lock and do
  192. * blocking lock_buffer().
  193. */
  194. if (buffer_dirty(bh)) {
  195. if (!trylock_buffer(bh)) {
  196. BUFFER_TRACE(bh, "needs blocking lock");
  197. spin_unlock(&journal->j_list_lock);
  198. trace_jbd_do_submit_data(journal,
  199. commit_transaction);
  200. /* Write out all data to prevent deadlocks */
  201. journal_do_submit_data(wbuf, bufs, write_op);
  202. bufs = 0;
  203. lock_buffer(bh);
  204. spin_lock(&journal->j_list_lock);
  205. }
  206. locked = 1;
  207. }
  208. /* We have to get bh_state lock. Again out of order, sigh. */
  209. if (!inverted_lock(journal, bh)) {
  210. jbd_lock_bh_state(bh);
  211. spin_lock(&journal->j_list_lock);
  212. }
  213. /* Someone already cleaned up the buffer? */
  214. if (!buffer_jbd(bh) || bh2jh(bh) != jh
  215. || jh->b_transaction != commit_transaction
  216. || jh->b_jlist != BJ_SyncData) {
  217. jbd_unlock_bh_state(bh);
  218. if (locked)
  219. unlock_buffer(bh);
  220. BUFFER_TRACE(bh, "already cleaned up");
  221. release_data_buffer(bh);
  222. continue;
  223. }
  224. if (locked && test_clear_buffer_dirty(bh)) {
  225. BUFFER_TRACE(bh, "needs writeout, adding to array");
  226. wbuf[bufs++] = bh;
  227. __journal_file_buffer(jh, commit_transaction,
  228. BJ_Locked);
  229. jbd_unlock_bh_state(bh);
  230. if (bufs == journal->j_wbufsize) {
  231. spin_unlock(&journal->j_list_lock);
  232. trace_jbd_do_submit_data(journal,
  233. commit_transaction);
  234. journal_do_submit_data(wbuf, bufs, write_op);
  235. bufs = 0;
  236. goto write_out_data;
  237. }
  238. } else if (!locked && buffer_locked(bh)) {
  239. __journal_file_buffer(jh, commit_transaction,
  240. BJ_Locked);
  241. jbd_unlock_bh_state(bh);
  242. put_bh(bh);
  243. } else {
  244. BUFFER_TRACE(bh, "writeout complete: unfile");
  245. if (unlikely(!buffer_uptodate(bh)))
  246. err = -EIO;
  247. __journal_unfile_buffer(jh);
  248. jbd_unlock_bh_state(bh);
  249. if (locked)
  250. unlock_buffer(bh);
  251. release_data_buffer(bh);
  252. }
  253. if (need_resched() || spin_needbreak(&journal->j_list_lock)) {
  254. spin_unlock(&journal->j_list_lock);
  255. goto write_out_data;
  256. }
  257. }
  258. spin_unlock(&journal->j_list_lock);
  259. trace_jbd_do_submit_data(journal, commit_transaction);
  260. journal_do_submit_data(wbuf, bufs, write_op);
  261. return err;
  262. }
  263. /*
  264. * journal_commit_transaction
  265. *
  266. * The primary function for committing a transaction to the log. This
  267. * function is called by the journal thread to begin a complete commit.
  268. */
  269. void journal_commit_transaction(journal_t *journal)
  270. {
  271. transaction_t *commit_transaction;
  272. struct journal_head *jh, *new_jh, *descriptor;
  273. struct buffer_head **wbuf = journal->j_wbuf;
  274. int bufs;
  275. int flags;
  276. int err;
  277. unsigned int blocknr;
  278. ktime_t start_time;
  279. u64 commit_time;
  280. char *tagp = NULL;
  281. journal_header_t *header;
  282. journal_block_tag_t *tag = NULL;
  283. int space_left = 0;
  284. int first_tag = 0;
  285. int tag_flag;
  286. int i;
  287. struct blk_plug plug;
  288. int write_op = WRITE;
  289. /*
  290. * First job: lock down the current transaction and wait for
  291. * all outstanding updates to complete.
  292. */
  293. /* Do we need to erase the effects of a prior journal_flush? */
  294. if (journal->j_flags & JFS_FLUSHED) {
  295. jbd_debug(3, "super block updated\n");
  296. mutex_lock(&journal->j_checkpoint_mutex);
  297. /*
  298. * We hold j_checkpoint_mutex so tail cannot change under us.
  299. * We don't need any special data guarantees for writing sb
  300. * since journal is empty and it is ok for write to be
  301. * flushed only with transaction commit.
  302. */
  303. journal_update_sb_log_tail(journal, journal->j_tail_sequence,
  304. journal->j_tail, WRITE_SYNC);
  305. mutex_unlock(&journal->j_checkpoint_mutex);
  306. } else {
  307. jbd_debug(3, "superblock not updated\n");
  308. }
  309. J_ASSERT(journal->j_running_transaction != NULL);
  310. J_ASSERT(journal->j_committing_transaction == NULL);
  311. commit_transaction = journal->j_running_transaction;
  312. trace_jbd_start_commit(journal, commit_transaction);
  313. jbd_debug(1, "JBD: starting commit of transaction %d\n",
  314. commit_transaction->t_tid);
  315. spin_lock(&journal->j_state_lock);
  316. J_ASSERT(commit_transaction->t_state == T_RUNNING);
  317. commit_transaction->t_state = T_LOCKED;
  318. trace_jbd_commit_locking(journal, commit_transaction);
  319. spin_lock(&commit_transaction->t_handle_lock);
  320. while (commit_transaction->t_updates) {
  321. DEFINE_WAIT(wait);
  322. prepare_to_wait(&journal->j_wait_updates, &wait,
  323. TASK_UNINTERRUPTIBLE);
  324. if (commit_transaction->t_updates) {
  325. spin_unlock(&commit_transaction->t_handle_lock);
  326. spin_unlock(&journal->j_state_lock);
  327. schedule();
  328. spin_lock(&journal->j_state_lock);
  329. spin_lock(&commit_transaction->t_handle_lock);
  330. }
  331. finish_wait(&journal->j_wait_updates, &wait);
  332. }
  333. spin_unlock(&commit_transaction->t_handle_lock);
  334. J_ASSERT (commit_transaction->t_outstanding_credits <=
  335. journal->j_max_transaction_buffers);
  336. /*
  337. * First thing we are allowed to do is to discard any remaining
  338. * BJ_Reserved buffers. Note, it is _not_ permissible to assume
  339. * that there are no such buffers: if a large filesystem
  340. * operation like a truncate needs to split itself over multiple
  341. * transactions, then it may try to do a journal_restart() while
  342. * there are still BJ_Reserved buffers outstanding. These must
  343. * be released cleanly from the current transaction.
  344. *
  345. * In this case, the filesystem must still reserve write access
  346. * again before modifying the buffer in the new transaction, but
  347. * we do not require it to remember exactly which old buffers it
  348. * has reserved. This is consistent with the existing behaviour
  349. * that multiple journal_get_write_access() calls to the same
  350. * buffer are perfectly permissible.
  351. */
  352. while (commit_transaction->t_reserved_list) {
  353. jh = commit_transaction->t_reserved_list;
  354. JBUFFER_TRACE(jh, "reserved, unused: refile");
  355. /*
  356. * A journal_get_undo_access()+journal_release_buffer() may
  357. * leave undo-committed data.
  358. */
  359. if (jh->b_committed_data) {
  360. struct buffer_head *bh = jh2bh(jh);
  361. jbd_lock_bh_state(bh);
  362. jbd_free(jh->b_committed_data, bh->b_size);
  363. jh->b_committed_data = NULL;
  364. jbd_unlock_bh_state(bh);
  365. }
  366. journal_refile_buffer(journal, jh);
  367. }
  368. /*
  369. * Now try to drop any written-back buffers from the journal's
  370. * checkpoint lists. We do this *before* commit because it potentially
  371. * frees some memory
  372. */
  373. spin_lock(&journal->j_list_lock);
  374. __journal_clean_checkpoint_list(journal);
  375. spin_unlock(&journal->j_list_lock);
  376. jbd_debug (3, "JBD: commit phase 1\n");
  377. /*
  378. * Clear revoked flag to reflect there is no revoked buffers
  379. * in the next transaction which is going to be started.
  380. */
  381. journal_clear_buffer_revoked_flags(journal);
  382. /*
  383. * Switch to a new revoke table.
  384. */
  385. journal_switch_revoke_table(journal);
  386. trace_jbd_commit_flushing(journal, commit_transaction);
  387. commit_transaction->t_state = T_FLUSH;
  388. journal->j_committing_transaction = commit_transaction;
  389. journal->j_running_transaction = NULL;
  390. start_time = ktime_get();
  391. commit_transaction->t_log_start = journal->j_head;
  392. wake_up(&journal->j_wait_transaction_locked);
  393. spin_unlock(&journal->j_state_lock);
  394. jbd_debug (3, "JBD: commit phase 2\n");
  395. if (tid_geq(journal->j_commit_waited, commit_transaction->t_tid))
  396. write_op = WRITE_SYNC;
  397. /*
  398. * Now start flushing things to disk, in the order they appear
  399. * on the transaction lists. Data blocks go first.
  400. */
  401. blk_start_plug(&plug);
  402. err = journal_submit_data_buffers(journal, commit_transaction,
  403. write_op);
  404. blk_finish_plug(&plug);
  405. /*
  406. * Wait for all previously submitted IO to complete.
  407. */
  408. spin_lock(&journal->j_list_lock);
  409. while (commit_transaction->t_locked_list) {
  410. struct buffer_head *bh;
  411. jh = commit_transaction->t_locked_list->b_tprev;
  412. bh = jh2bh(jh);
  413. get_bh(bh);
  414. if (buffer_locked(bh)) {
  415. spin_unlock(&journal->j_list_lock);
  416. wait_on_buffer(bh);
  417. spin_lock(&journal->j_list_lock);
  418. }
  419. if (unlikely(!buffer_uptodate(bh))) {
  420. if (!trylock_page(bh->b_page)) {
  421. spin_unlock(&journal->j_list_lock);
  422. lock_page(bh->b_page);
  423. spin_lock(&journal->j_list_lock);
  424. }
  425. if (bh->b_page->mapping)
  426. set_bit(AS_EIO, &bh->b_page->mapping->flags);
  427. unlock_page(bh->b_page);
  428. SetPageError(bh->b_page);
  429. err = -EIO;
  430. }
  431. if (!inverted_lock(journal, bh)) {
  432. put_bh(bh);
  433. spin_lock(&journal->j_list_lock);
  434. continue;
  435. }
  436. if (buffer_jbd(bh) && bh2jh(bh) == jh &&
  437. jh->b_transaction == commit_transaction &&
  438. jh->b_jlist == BJ_Locked)
  439. __journal_unfile_buffer(jh);
  440. jbd_unlock_bh_state(bh);
  441. release_data_buffer(bh);
  442. cond_resched_lock(&journal->j_list_lock);
  443. }
  444. spin_unlock(&journal->j_list_lock);
  445. if (err) {
  446. char b[BDEVNAME_SIZE];
  447. printk(KERN_WARNING
  448. "JBD: Detected IO errors while flushing file data "
  449. "on %s\n", bdevname(journal->j_fs_dev, b));
  450. if (journal->j_flags & JFS_ABORT_ON_SYNCDATA_ERR)
  451. journal_abort(journal, err);
  452. err = 0;
  453. }
  454. blk_start_plug(&plug);
  455. journal_write_revoke_records(journal, commit_transaction, write_op);
  456. /*
  457. * If we found any dirty or locked buffers, then we should have
  458. * looped back up to the write_out_data label. If there weren't
  459. * any then journal_clean_data_list should have wiped the list
  460. * clean by now, so check that it is in fact empty.
  461. */
  462. J_ASSERT (commit_transaction->t_sync_datalist == NULL);
  463. jbd_debug (3, "JBD: commit phase 3\n");
  464. /*
  465. * Way to go: we have now written out all of the data for a
  466. * transaction! Now comes the tricky part: we need to write out
  467. * metadata. Loop over the transaction's entire buffer list:
  468. */
  469. spin_lock(&journal->j_state_lock);
  470. commit_transaction->t_state = T_COMMIT;
  471. spin_unlock(&journal->j_state_lock);
  472. trace_jbd_commit_logging(journal, commit_transaction);
  473. J_ASSERT(commit_transaction->t_nr_buffers <=
  474. commit_transaction->t_outstanding_credits);
  475. descriptor = NULL;
  476. bufs = 0;
  477. while (commit_transaction->t_buffers) {
  478. /* Find the next buffer to be journaled... */
  479. jh = commit_transaction->t_buffers;
  480. /* If we're in abort mode, we just un-journal the buffer and
  481. release it. */
  482. if (is_journal_aborted(journal)) {
  483. clear_buffer_jbddirty(jh2bh(jh));
  484. JBUFFER_TRACE(jh, "journal is aborting: refile");
  485. journal_refile_buffer(journal, jh);
  486. /* If that was the last one, we need to clean up
  487. * any descriptor buffers which may have been
  488. * already allocated, even if we are now
  489. * aborting. */
  490. if (!commit_transaction->t_buffers)
  491. goto start_journal_io;
  492. continue;
  493. }
  494. /* Make sure we have a descriptor block in which to
  495. record the metadata buffer. */
  496. if (!descriptor) {
  497. struct buffer_head *bh;
  498. J_ASSERT (bufs == 0);
  499. jbd_debug(4, "JBD: get descriptor\n");
  500. descriptor = journal_get_descriptor_buffer(journal);
  501. if (!descriptor) {
  502. journal_abort(journal, -EIO);
  503. continue;
  504. }
  505. bh = jh2bh(descriptor);
  506. jbd_debug(4, "JBD: got buffer %llu (%p)\n",
  507. (unsigned long long)bh->b_blocknr, bh->b_data);
  508. header = (journal_header_t *)&bh->b_data[0];
  509. header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  510. header->h_blocktype = cpu_to_be32(JFS_DESCRIPTOR_BLOCK);
  511. header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
  512. tagp = &bh->b_data[sizeof(journal_header_t)];
  513. space_left = bh->b_size - sizeof(journal_header_t);
  514. first_tag = 1;
  515. set_buffer_jwrite(bh);
  516. set_buffer_dirty(bh);
  517. wbuf[bufs++] = bh;
  518. /* Record it so that we can wait for IO
  519. completion later */
  520. BUFFER_TRACE(bh, "ph3: file as descriptor");
  521. journal_file_buffer(descriptor, commit_transaction,
  522. BJ_LogCtl);
  523. }
  524. /* Where is the buffer to be written? */
  525. err = journal_next_log_block(journal, &blocknr);
  526. /* If the block mapping failed, just abandon the buffer
  527. and repeat this loop: we'll fall into the
  528. refile-on-abort condition above. */
  529. if (err) {
  530. journal_abort(journal, err);
  531. continue;
  532. }
  533. /*
  534. * start_this_handle() uses t_outstanding_credits to determine
  535. * the free space in the log, but this counter is changed
  536. * by journal_next_log_block() also.
  537. */
  538. commit_transaction->t_outstanding_credits--;
  539. /* Bump b_count to prevent truncate from stumbling over
  540. the shadowed buffer! @@@ This can go if we ever get
  541. rid of the BJ_IO/BJ_Shadow pairing of buffers. */
  542. get_bh(jh2bh(jh));
  543. /* Make a temporary IO buffer with which to write it out
  544. (this will requeue both the metadata buffer and the
  545. temporary IO buffer). new_bh goes on BJ_IO*/
  546. set_buffer_jwrite(jh2bh(jh));
  547. /*
  548. * akpm: journal_write_metadata_buffer() sets
  549. * new_bh->b_transaction to commit_transaction.
  550. * We need to clean this up before we release new_bh
  551. * (which is of type BJ_IO)
  552. */
  553. JBUFFER_TRACE(jh, "ph3: write metadata");
  554. flags = journal_write_metadata_buffer(commit_transaction,
  555. jh, &new_jh, blocknr);
  556. set_buffer_jwrite(jh2bh(new_jh));
  557. wbuf[bufs++] = jh2bh(new_jh);
  558. /* Record the new block's tag in the current descriptor
  559. buffer */
  560. tag_flag = 0;
  561. if (flags & 1)
  562. tag_flag |= JFS_FLAG_ESCAPE;
  563. if (!first_tag)
  564. tag_flag |= JFS_FLAG_SAME_UUID;
  565. tag = (journal_block_tag_t *) tagp;
  566. tag->t_blocknr = cpu_to_be32(jh2bh(jh)->b_blocknr);
  567. tag->t_flags = cpu_to_be32(tag_flag);
  568. tagp += sizeof(journal_block_tag_t);
  569. space_left -= sizeof(journal_block_tag_t);
  570. if (first_tag) {
  571. memcpy (tagp, journal->j_uuid, 16);
  572. tagp += 16;
  573. space_left -= 16;
  574. first_tag = 0;
  575. }
  576. /* If there's no more to do, or if the descriptor is full,
  577. let the IO rip! */
  578. if (bufs == journal->j_wbufsize ||
  579. commit_transaction->t_buffers == NULL ||
  580. space_left < sizeof(journal_block_tag_t) + 16) {
  581. jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
  582. /* Write an end-of-descriptor marker before
  583. submitting the IOs. "tag" still points to
  584. the last tag we set up. */
  585. tag->t_flags |= cpu_to_be32(JFS_FLAG_LAST_TAG);
  586. start_journal_io:
  587. for (i = 0; i < bufs; i++) {
  588. struct buffer_head *bh = wbuf[i];
  589. lock_buffer(bh);
  590. clear_buffer_dirty(bh);
  591. set_buffer_uptodate(bh);
  592. bh->b_end_io = journal_end_buffer_io_sync;
  593. /*
  594. * In data=journal mode, here we can end up
  595. * writing pagecache data that might be
  596. * mmapped. Since we can't afford to clean the
  597. * page and set PageWriteback (see the comment
  598. * near the other use of _submit_bh()), the
  599. * data can change while the write is in
  600. * flight. Tell the block layer to bounce the
  601. * bio pages if stable pages are required.
  602. */
  603. _submit_bh(write_op, bh, 1 << BIO_SNAP_STABLE);
  604. }
  605. cond_resched();
  606. /* Force a new descriptor to be generated next
  607. time round the loop. */
  608. descriptor = NULL;
  609. bufs = 0;
  610. }
  611. }
  612. blk_finish_plug(&plug);
  613. /* Lo and behold: we have just managed to send a transaction to
  614. the log. Before we can commit it, wait for the IO so far to
  615. complete. Control buffers being written are on the
  616. transaction's t_log_list queue, and metadata buffers are on
  617. the t_iobuf_list queue.
  618. Wait for the buffers in reverse order. That way we are
  619. less likely to be woken up until all IOs have completed, and
  620. so we incur less scheduling load.
  621. */
  622. jbd_debug(3, "JBD: commit phase 4\n");
  623. /*
  624. * akpm: these are BJ_IO, and j_list_lock is not needed.
  625. * See __journal_try_to_free_buffer.
  626. */
  627. wait_for_iobuf:
  628. while (commit_transaction->t_iobuf_list != NULL) {
  629. struct buffer_head *bh;
  630. jh = commit_transaction->t_iobuf_list->b_tprev;
  631. bh = jh2bh(jh);
  632. if (buffer_locked(bh)) {
  633. wait_on_buffer(bh);
  634. goto wait_for_iobuf;
  635. }
  636. if (cond_resched())
  637. goto wait_for_iobuf;
  638. if (unlikely(!buffer_uptodate(bh)))
  639. err = -EIO;
  640. clear_buffer_jwrite(bh);
  641. JBUFFER_TRACE(jh, "ph4: unfile after journal write");
  642. journal_unfile_buffer(journal, jh);
  643. /*
  644. * ->t_iobuf_list should contain only dummy buffer_heads
  645. * which were created by journal_write_metadata_buffer().
  646. */
  647. BUFFER_TRACE(bh, "dumping temporary bh");
  648. journal_put_journal_head(jh);
  649. __brelse(bh);
  650. J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
  651. free_buffer_head(bh);
  652. /* We also have to unlock and free the corresponding
  653. shadowed buffer */
  654. jh = commit_transaction->t_shadow_list->b_tprev;
  655. bh = jh2bh(jh);
  656. clear_buffer_jwrite(bh);
  657. J_ASSERT_BH(bh, buffer_jbddirty(bh));
  658. /* The metadata is now released for reuse, but we need
  659. to remember it against this transaction so that when
  660. we finally commit, we can do any checkpointing
  661. required. */
  662. JBUFFER_TRACE(jh, "file as BJ_Forget");
  663. journal_file_buffer(jh, commit_transaction, BJ_Forget);
  664. /*
  665. * Wake up any transactions which were waiting for this
  666. * IO to complete. The barrier must be here so that changes
  667. * by journal_file_buffer() take effect before wake_up_bit()
  668. * does the waitqueue check.
  669. */
  670. smp_mb();
  671. wake_up_bit(&bh->b_state, BH_Unshadow);
  672. JBUFFER_TRACE(jh, "brelse shadowed buffer");
  673. __brelse(bh);
  674. }
  675. J_ASSERT (commit_transaction->t_shadow_list == NULL);
  676. jbd_debug(3, "JBD: commit phase 5\n");
  677. /* Here we wait for the revoke record and descriptor record buffers */
  678. wait_for_ctlbuf:
  679. while (commit_transaction->t_log_list != NULL) {
  680. struct buffer_head *bh;
  681. jh = commit_transaction->t_log_list->b_tprev;
  682. bh = jh2bh(jh);
  683. if (buffer_locked(bh)) {
  684. wait_on_buffer(bh);
  685. goto wait_for_ctlbuf;
  686. }
  687. if (cond_resched())
  688. goto wait_for_ctlbuf;
  689. if (unlikely(!buffer_uptodate(bh)))
  690. err = -EIO;
  691. BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
  692. clear_buffer_jwrite(bh);
  693. journal_unfile_buffer(journal, jh);
  694. journal_put_journal_head(jh);
  695. __brelse(bh); /* One for getblk */
  696. /* AKPM: bforget here */
  697. }
  698. if (err)
  699. journal_abort(journal, err);
  700. jbd_debug(3, "JBD: commit phase 6\n");
  701. /* All metadata is written, now write commit record and do cleanup */
  702. spin_lock(&journal->j_state_lock);
  703. J_ASSERT(commit_transaction->t_state == T_COMMIT);
  704. commit_transaction->t_state = T_COMMIT_RECORD;
  705. spin_unlock(&journal->j_state_lock);
  706. if (journal_write_commit_record(journal, commit_transaction))
  707. err = -EIO;
  708. if (err)
  709. journal_abort(journal, err);
  710. /* End of a transaction! Finally, we can do checkpoint
  711. processing: any buffers committed as a result of this
  712. transaction can be removed from any checkpoint list it was on
  713. before. */
  714. jbd_debug(3, "JBD: commit phase 7\n");
  715. J_ASSERT(commit_transaction->t_sync_datalist == NULL);
  716. J_ASSERT(commit_transaction->t_buffers == NULL);
  717. J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
  718. J_ASSERT(commit_transaction->t_iobuf_list == NULL);
  719. J_ASSERT(commit_transaction->t_shadow_list == NULL);
  720. J_ASSERT(commit_transaction->t_log_list == NULL);
  721. restart_loop:
  722. /*
  723. * As there are other places (journal_unmap_buffer()) adding buffers
  724. * to this list we have to be careful and hold the j_list_lock.
  725. */
  726. spin_lock(&journal->j_list_lock);
  727. while (commit_transaction->t_forget) {
  728. transaction_t *cp_transaction;
  729. struct buffer_head *bh;
  730. int try_to_free = 0;
  731. jh = commit_transaction->t_forget;
  732. spin_unlock(&journal->j_list_lock);
  733. bh = jh2bh(jh);
  734. /*
  735. * Get a reference so that bh cannot be freed before we are
  736. * done with it.
  737. */
  738. get_bh(bh);
  739. jbd_lock_bh_state(bh);
  740. J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
  741. jh->b_transaction == journal->j_running_transaction);
  742. /*
  743. * If there is undo-protected committed data against
  744. * this buffer, then we can remove it now. If it is a
  745. * buffer needing such protection, the old frozen_data
  746. * field now points to a committed version of the
  747. * buffer, so rotate that field to the new committed
  748. * data.
  749. *
  750. * Otherwise, we can just throw away the frozen data now.
  751. */
  752. if (jh->b_committed_data) {
  753. jbd_free(jh->b_committed_data, bh->b_size);
  754. jh->b_committed_data = NULL;
  755. if (jh->b_frozen_data) {
  756. jh->b_committed_data = jh->b_frozen_data;
  757. jh->b_frozen_data = NULL;
  758. }
  759. } else if (jh->b_frozen_data) {
  760. jbd_free(jh->b_frozen_data, bh->b_size);
  761. jh->b_frozen_data = NULL;
  762. }
  763. spin_lock(&journal->j_list_lock);
  764. cp_transaction = jh->b_cp_transaction;
  765. if (cp_transaction) {
  766. JBUFFER_TRACE(jh, "remove from old cp transaction");
  767. __journal_remove_checkpoint(jh);
  768. }
  769. /* Only re-checkpoint the buffer_head if it is marked
  770. * dirty. If the buffer was added to the BJ_Forget list
  771. * by journal_forget, it may no longer be dirty and
  772. * there's no point in keeping a checkpoint record for
  773. * it. */
  774. /*
  775. * A buffer which has been freed while still being journaled by
  776. * a previous transaction.
  777. */
  778. if (buffer_freed(bh)) {
  779. /*
  780. * If the running transaction is the one containing
  781. * "add to orphan" operation (b_next_transaction !=
  782. * NULL), we have to wait for that transaction to
  783. * commit before we can really get rid of the buffer.
  784. * So just clear b_modified to not confuse transaction
  785. * credit accounting and refile the buffer to
  786. * BJ_Forget of the running transaction. If the just
  787. * committed transaction contains "add to orphan"
  788. * operation, we can completely invalidate the buffer
  789. * now. We are rather throughout in that since the
  790. * buffer may be still accessible when blocksize <
  791. * pagesize and it is attached to the last partial
  792. * page.
  793. */
  794. jh->b_modified = 0;
  795. if (!jh->b_next_transaction) {
  796. clear_buffer_freed(bh);
  797. clear_buffer_jbddirty(bh);
  798. clear_buffer_mapped(bh);
  799. clear_buffer_new(bh);
  800. clear_buffer_req(bh);
  801. bh->b_bdev = NULL;
  802. }
  803. }
  804. if (buffer_jbddirty(bh)) {
  805. JBUFFER_TRACE(jh, "add to new checkpointing trans");
  806. __journal_insert_checkpoint(jh, commit_transaction);
  807. if (is_journal_aborted(journal))
  808. clear_buffer_jbddirty(bh);
  809. } else {
  810. J_ASSERT_BH(bh, !buffer_dirty(bh));
  811. /*
  812. * The buffer on BJ_Forget list and not jbddirty means
  813. * it has been freed by this transaction and hence it
  814. * could not have been reallocated until this
  815. * transaction has committed. *BUT* it could be
  816. * reallocated once we have written all the data to
  817. * disk and before we process the buffer on BJ_Forget
  818. * list.
  819. */
  820. if (!jh->b_next_transaction)
  821. try_to_free = 1;
  822. }
  823. JBUFFER_TRACE(jh, "refile or unfile freed buffer");
  824. __journal_refile_buffer(jh);
  825. jbd_unlock_bh_state(bh);
  826. if (try_to_free)
  827. release_buffer_page(bh);
  828. else
  829. __brelse(bh);
  830. cond_resched_lock(&journal->j_list_lock);
  831. }
  832. spin_unlock(&journal->j_list_lock);
  833. /*
  834. * This is a bit sleazy. We use j_list_lock to protect transition
  835. * of a transaction into T_FINISHED state and calling
  836. * __journal_drop_transaction(). Otherwise we could race with
  837. * other checkpointing code processing the transaction...
  838. */
  839. spin_lock(&journal->j_state_lock);
  840. spin_lock(&journal->j_list_lock);
  841. /*
  842. * Now recheck if some buffers did not get attached to the transaction
  843. * while the lock was dropped...
  844. */
  845. if (commit_transaction->t_forget) {
  846. spin_unlock(&journal->j_list_lock);
  847. spin_unlock(&journal->j_state_lock);
  848. goto restart_loop;
  849. }
  850. /* Done with this transaction! */
  851. jbd_debug(3, "JBD: commit phase 8\n");
  852. J_ASSERT(commit_transaction->t_state == T_COMMIT_RECORD);
  853. commit_transaction->t_state = T_FINISHED;
  854. J_ASSERT(commit_transaction == journal->j_committing_transaction);
  855. journal->j_commit_sequence = commit_transaction->t_tid;
  856. journal->j_committing_transaction = NULL;
  857. commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
  858. /*
  859. * weight the commit time higher than the average time so we don't
  860. * react too strongly to vast changes in commit time
  861. */
  862. if (likely(journal->j_average_commit_time))
  863. journal->j_average_commit_time = (commit_time*3 +
  864. journal->j_average_commit_time) / 4;
  865. else
  866. journal->j_average_commit_time = commit_time;
  867. spin_unlock(&journal->j_state_lock);
  868. if (commit_transaction->t_checkpoint_list == NULL &&
  869. commit_transaction->t_checkpoint_io_list == NULL) {
  870. __journal_drop_transaction(journal, commit_transaction);
  871. } else {
  872. if (journal->j_checkpoint_transactions == NULL) {
  873. journal->j_checkpoint_transactions = commit_transaction;
  874. commit_transaction->t_cpnext = commit_transaction;
  875. commit_transaction->t_cpprev = commit_transaction;
  876. } else {
  877. commit_transaction->t_cpnext =
  878. journal->j_checkpoint_transactions;
  879. commit_transaction->t_cpprev =
  880. commit_transaction->t_cpnext->t_cpprev;
  881. commit_transaction->t_cpnext->t_cpprev =
  882. commit_transaction;
  883. commit_transaction->t_cpprev->t_cpnext =
  884. commit_transaction;
  885. }
  886. }
  887. spin_unlock(&journal->j_list_lock);
  888. trace_jbd_end_commit(journal, commit_transaction);
  889. jbd_debug(1, "JBD: commit %d complete, head %d\n",
  890. journal->j_commit_sequence, journal->j_tail_sequence);
  891. wake_up(&journal->j_wait_done_commit);
  892. }