blob: 11bb11f48b3ad7aa70d80a39af28c65f1f2d2dd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/fs/jbd/commit.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
16#include <linux/time.h>
17#include <linux/fs.h>
18#include <linux/jbd.h>
19#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
21#include <linux/pagemap.h>
Theodore Ts'o512a0042009-03-27 22:14:27 -040022#include <linux/bio.h>
Jens Axboe65ab8022011-03-17 10:56:45 +010023#include <linux/blkdev.h>
Lukas Czerner99cb1a32011-05-23 18:33:02 +020024#include <trace/events/jbd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Default IO end handler for temporary BJ_IO buffer_heads.
28 */
29static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
30{
31 BUFFER_TRACE(bh, "");
32 if (uptodate)
33 set_buffer_uptodate(bh);
34 else
35 clear_buffer_uptodate(bh);
36 unlock_buffer(bh);
37}
38
39/*
40 * When an ext3-ordered file is truncated, it is possible that many pages are
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070041 * not successfully freed, because they are attached to a committing transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * After the transaction commits, these pages are left on the LRU, with no
43 * ->mapping, and with attached buffers. These pages are trivially reclaimable
44 * by the VM, but their apparent absence upsets the VM accounting, and it makes
45 * the numbers in /proc/meminfo look odd.
46 *
47 * So here, we have a buffer which has just come off the forget list. Look to
48 * see if we can strip all buffers from the backing page.
49 *
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070050 * Called under journal->j_list_lock. The caller provided us with a ref
51 * against the buffer, and we drop that here.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53static void release_buffer_page(struct buffer_head *bh)
54{
55 struct page *page;
56
57 if (buffer_dirty(bh))
58 goto nope;
59 if (atomic_read(&bh->b_count) != 1)
60 goto nope;
61 page = bh->b_page;
62 if (!page)
63 goto nope;
64 if (page->mapping)
65 goto nope;
66
67 /* OK, it's a truncated page */
Nick Piggin529ae9a2008-08-02 12:01:03 +020068 if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 goto nope;
70
71 page_cache_get(page);
72 __brelse(bh);
73 try_to_free_buffers(page);
74 unlock_page(page);
75 page_cache_release(page);
76 return;
77
78nope:
79 __brelse(bh);
80}
81
82/*
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070083 * Decrement reference counter for data buffer. If it has been marked
84 * 'BH_Freed', release it and the page to which it belongs if possible.
85 */
86static void release_data_buffer(struct buffer_head *bh)
87{
88 if (buffer_freed(bh)) {
Jan Kara09e05d42012-07-11 23:16:25 +020089 WARN_ON_ONCE(buffer_dirty(bh));
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070090 clear_buffer_freed(bh);
Jan Kara09e05d42012-07-11 23:16:25 +020091 clear_buffer_mapped(bh);
92 clear_buffer_new(bh);
93 clear_buffer_req(bh);
94 bh->b_bdev = NULL;
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070095 release_buffer_page(bh);
96 } else
97 put_bh(bh);
98}
99
100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is
102 * held. For ranking reasons we must trylock. If we lose, schedule away and
103 * return 0. j_list_lock is dropped in this case.
104 */
105static int inverted_lock(journal_t *journal, struct buffer_head *bh)
106{
107 if (!jbd_trylock_bh_state(bh)) {
108 spin_unlock(&journal->j_list_lock);
109 schedule();
110 return 0;
111 }
112 return 1;
113}
114
115/* Done it all: now write the commit record. We should have
116 * cleaned up our previous buffers by now, so if we are in abort
117 * mode we can now just skip the rest of the journal write
118 * entirely.
119 *
120 * Returns 1 if the journal needs to be aborted or 0 on success
121 */
122static int journal_write_commit_record(journal_t *journal,
123 transaction_t *commit_transaction)
124{
125 struct journal_head *descriptor;
126 struct buffer_head *bh;
Jan Kara53152172008-02-01 08:26:46 -0500127 journal_header_t *header;
128 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 if (is_journal_aborted(journal))
131 return 0;
132
133 descriptor = journal_get_descriptor_buffer(journal);
134 if (!descriptor)
135 return 1;
136
137 bh = jh2bh(descriptor);
138
Jan Kara53152172008-02-01 08:26:46 -0500139 header = (journal_header_t *)(bh->b_data);
140 header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
141 header->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK);
142 header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 JBUFFER_TRACE(descriptor, "write commit block");
145 set_buffer_dirty(bh);
Christoph Hellwig87e99512010-08-11 17:05:45 +0200146
Christoph Hellwig45244512010-08-18 05:29:16 -0400147 if (journal->j_flags & JFS_BARRIER)
148 ret = __sync_dirty_buffer(bh, WRITE_SYNC | WRITE_FLUSH_FUA);
149 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 ret = sync_dirty_buffer(bh);
Christoph Hellwig87e99512010-08-11 17:05:45 +0200151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 put_bh(bh); /* One for getblk() */
153 journal_put_journal_head(descriptor);
154
155 return (ret == -EIO);
156}
157
Theodore Ts'o512a0042009-03-27 22:14:27 -0400158static void journal_do_submit_data(struct buffer_head **wbuf, int bufs,
159 int write_op)
Jan Kara3998b932006-09-25 23:30:53 -0700160{
161 int i;
162
163 for (i = 0; i < bufs; i++) {
164 wbuf[i]->b_end_io = end_buffer_write_sync;
Darrick J. Wong7136851112013-04-29 15:07:25 -0700165 /*
166 * Here we write back pagecache data that may be mmaped. Since
167 * we cannot afford to clean the page and set PageWriteback
168 * here due to lock ordering (page lock ranks above transaction
169 * start), the data can change while IO is in flight. Tell the
170 * block layer it should bounce the bio pages if stable data
171 * during write is required.
172 *
173 * We use up our safety reference in submit_bh().
174 */
175 _submit_bh(write_op, wbuf[i], 1 << BIO_SNAP_STABLE);
Jan Kara3998b932006-09-25 23:30:53 -0700176 }
177}
178
179/*
180 * Submit all the data buffers to disk
181 */
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700182static int journal_submit_data_buffers(journal_t *journal,
Theodore Ts'o512a0042009-03-27 22:14:27 -0400183 transaction_t *commit_transaction,
184 int write_op)
Jan Kara3998b932006-09-25 23:30:53 -0700185{
186 struct journal_head *jh;
187 struct buffer_head *bh;
188 int locked;
189 int bufs = 0;
190 struct buffer_head **wbuf = journal->j_wbuf;
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700191 int err = 0;
Jan Kara3998b932006-09-25 23:30:53 -0700192
193 /*
194 * Whenever we unlock the journal and sleep, things can get added
195 * onto ->t_sync_datalist, so we have to keep looping back to
196 * write_out_data until we *know* that the list is empty.
197 *
198 * Cleanup any flushed data buffers from the data list. Even in
199 * abort mode, we want to flush this out as soon as possible.
200 */
201write_out_data:
202 cond_resched();
203 spin_lock(&journal->j_list_lock);
204
205 while (commit_transaction->t_sync_datalist) {
206 jh = commit_transaction->t_sync_datalist;
207 bh = jh2bh(jh);
208 locked = 0;
209
210 /* Get reference just to make sure buffer does not disappear
211 * when we are forced to drop various locks */
212 get_bh(bh);
213 /* If the buffer is dirty, we need to submit IO and hence
214 * we need the buffer lock. We try to lock the buffer without
215 * blocking. If we fail, we need to drop j_list_lock and do
216 * blocking lock_buffer().
217 */
218 if (buffer_dirty(bh)) {
Nick Pigginca5de402008-08-02 12:02:13 +0200219 if (!trylock_buffer(bh)) {
Jan Kara3998b932006-09-25 23:30:53 -0700220 BUFFER_TRACE(bh, "needs blocking lock");
221 spin_unlock(&journal->j_list_lock);
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200222 trace_jbd_do_submit_data(journal,
223 commit_transaction);
Jan Kara3998b932006-09-25 23:30:53 -0700224 /* Write out all data to prevent deadlocks */
Theodore Ts'o512a0042009-03-27 22:14:27 -0400225 journal_do_submit_data(wbuf, bufs, write_op);
Jan Kara3998b932006-09-25 23:30:53 -0700226 bufs = 0;
227 lock_buffer(bh);
228 spin_lock(&journal->j_list_lock);
229 }
230 locked = 1;
231 }
232 /* We have to get bh_state lock. Again out of order, sigh. */
233 if (!inverted_lock(journal, bh)) {
234 jbd_lock_bh_state(bh);
235 spin_lock(&journal->j_list_lock);
236 }
237 /* Someone already cleaned up the buffer? */
Jan Karaa61d90d2009-06-09 16:26:26 -0700238 if (!buffer_jbd(bh) || bh2jh(bh) != jh
Jan Kara3998b932006-09-25 23:30:53 -0700239 || jh->b_transaction != commit_transaction
240 || jh->b_jlist != BJ_SyncData) {
241 jbd_unlock_bh_state(bh);
242 if (locked)
243 unlock_buffer(bh);
244 BUFFER_TRACE(bh, "already cleaned up");
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -0700245 release_data_buffer(bh);
Jan Kara3998b932006-09-25 23:30:53 -0700246 continue;
247 }
248 if (locked && test_clear_buffer_dirty(bh)) {
249 BUFFER_TRACE(bh, "needs writeout, adding to array");
250 wbuf[bufs++] = bh;
251 __journal_file_buffer(jh, commit_transaction,
252 BJ_Locked);
253 jbd_unlock_bh_state(bh);
254 if (bufs == journal->j_wbufsize) {
255 spin_unlock(&journal->j_list_lock);
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200256 trace_jbd_do_submit_data(journal,
257 commit_transaction);
Theodore Ts'o512a0042009-03-27 22:14:27 -0400258 journal_do_submit_data(wbuf, bufs, write_op);
Jan Kara3998b932006-09-25 23:30:53 -0700259 bufs = 0;
260 goto write_out_data;
261 }
Hisashi Hifumi6f5a9da2006-12-22 01:11:50 -0800262 } else if (!locked && buffer_locked(bh)) {
263 __journal_file_buffer(jh, commit_transaction,
264 BJ_Locked);
265 jbd_unlock_bh_state(bh);
266 put_bh(bh);
267 } else {
Jan Kara3998b932006-09-25 23:30:53 -0700268 BUFFER_TRACE(bh, "writeout complete: unfile");
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700269 if (unlikely(!buffer_uptodate(bh)))
270 err = -EIO;
Jan Kara3998b932006-09-25 23:30:53 -0700271 __journal_unfile_buffer(jh);
272 jbd_unlock_bh_state(bh);
273 if (locked)
274 unlock_buffer(bh);
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -0700275 release_data_buffer(bh);
Jan Kara3998b932006-09-25 23:30:53 -0700276 }
277
Nick Piggin95c354f2008-01-30 13:31:20 +0100278 if (need_resched() || spin_needbreak(&journal->j_list_lock)) {
Jan Kara3998b932006-09-25 23:30:53 -0700279 spin_unlock(&journal->j_list_lock);
280 goto write_out_data;
281 }
282 }
283 spin_unlock(&journal->j_list_lock);
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200284 trace_jbd_do_submit_data(journal, commit_transaction);
Theodore Ts'o512a0042009-03-27 22:14:27 -0400285 journal_do_submit_data(wbuf, bufs, write_op);
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700286
287 return err;
Jan Kara3998b932006-09-25 23:30:53 -0700288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
291 * journal_commit_transaction
292 *
293 * The primary function for committing a transaction to the log. This
294 * function is called by the journal thread to begin a complete commit.
295 */
296void journal_commit_transaction(journal_t *journal)
297{
298 transaction_t *commit_transaction;
299 struct journal_head *jh, *new_jh, *descriptor;
300 struct buffer_head **wbuf = journal->j_wbuf;
301 int bufs;
302 int flags;
303 int err;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200304 unsigned int blocknr;
Josef Bacikf420d4d2009-01-07 18:07:24 -0800305 ktime_t start_time;
306 u64 commit_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 char *tagp = NULL;
308 journal_header_t *header;
309 journal_block_tag_t *tag = NULL;
310 int space_left = 0;
311 int first_tag = 0;
312 int tag_flag;
313 int i;
Jens Axboe65ab8022011-03-17 10:56:45 +0100314 struct blk_plug plug;
Jan Kara2db938b2011-02-21 17:25:37 +0100315 int write_op = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
318 * First job: lock down the current transaction and wait for
319 * all outstanding updates to complete.
320 */
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Do we need to erase the effects of a prior journal_flush? */
323 if (journal->j_flags & JFS_FLUSHED) {
324 jbd_debug(3, "super block updated\n");
Jan Kara1ce84862012-04-07 12:50:13 +0200325 mutex_lock(&journal->j_checkpoint_mutex);
Jan Karafd2cbd42012-04-07 11:05:19 +0200326 /*
327 * We hold j_checkpoint_mutex so tail cannot change under us.
328 * We don't need any special data guarantees for writing sb
329 * since journal is empty and it is ok for write to be
330 * flushed only with transaction commit.
331 */
332 journal_update_sb_log_tail(journal, journal->j_tail_sequence,
333 journal->j_tail, WRITE_SYNC);
Jan Kara1ce84862012-04-07 12:50:13 +0200334 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 } else {
336 jbd_debug(3, "superblock not updated\n");
337 }
338
339 J_ASSERT(journal->j_running_transaction != NULL);
340 J_ASSERT(journal->j_committing_transaction == NULL);
341
342 commit_transaction = journal->j_running_transaction;
343 J_ASSERT(commit_transaction->t_state == T_RUNNING);
344
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200345 trace_jbd_start_commit(journal, commit_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 jbd_debug(1, "JBD: starting commit of transaction %d\n",
347 commit_transaction->t_tid);
348
349 spin_lock(&journal->j_state_lock);
350 commit_transaction->t_state = T_LOCKED;
351
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200352 trace_jbd_commit_locking(journal, commit_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 spin_lock(&commit_transaction->t_handle_lock);
354 while (commit_transaction->t_updates) {
355 DEFINE_WAIT(wait);
356
357 prepare_to_wait(&journal->j_wait_updates, &wait,
358 TASK_UNINTERRUPTIBLE);
359 if (commit_transaction->t_updates) {
360 spin_unlock(&commit_transaction->t_handle_lock);
361 spin_unlock(&journal->j_state_lock);
362 schedule();
363 spin_lock(&journal->j_state_lock);
364 spin_lock(&commit_transaction->t_handle_lock);
365 }
366 finish_wait(&journal->j_wait_updates, &wait);
367 }
368 spin_unlock(&commit_transaction->t_handle_lock);
369
370 J_ASSERT (commit_transaction->t_outstanding_credits <=
371 journal->j_max_transaction_buffers);
372
373 /*
374 * First thing we are allowed to do is to discard any remaining
375 * BJ_Reserved buffers. Note, it is _not_ permissible to assume
376 * that there are no such buffers: if a large filesystem
377 * operation like a truncate needs to split itself over multiple
378 * transactions, then it may try to do a journal_restart() while
379 * there are still BJ_Reserved buffers outstanding. These must
380 * be released cleanly from the current transaction.
381 *
382 * In this case, the filesystem must still reserve write access
383 * again before modifying the buffer in the new transaction, but
384 * we do not require it to remember exactly which old buffers it
385 * has reserved. This is consistent with the existing behaviour
386 * that multiple journal_get_write_access() calls to the same
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300387 * buffer are perfectly permissible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
389 while (commit_transaction->t_reserved_list) {
390 jh = commit_transaction->t_reserved_list;
391 JBUFFER_TRACE(jh, "reserved, unused: refile");
392 /*
393 * A journal_get_undo_access()+journal_release_buffer() may
394 * leave undo-committed data.
395 */
396 if (jh->b_committed_data) {
397 struct buffer_head *bh = jh2bh(jh);
398
399 jbd_lock_bh_state(bh);
Mingming Caoc089d492007-10-16 18:38:25 -0400400 jbd_free(jh->b_committed_data, bh->b_size);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800401 jh->b_committed_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 jbd_unlock_bh_state(bh);
403 }
404 journal_refile_buffer(journal, jh);
405 }
406
407 /*
408 * Now try to drop any written-back buffers from the journal's
409 * checkpoint lists. We do this *before* commit because it potentially
410 * frees some memory
411 */
412 spin_lock(&journal->j_list_lock);
413 __journal_clean_checkpoint_list(journal);
414 spin_unlock(&journal->j_list_lock);
415
416 jbd_debug (3, "JBD: commit phase 1\n");
417
418 /*
Yongqiang Yang8c111b32011-11-19 17:34:29 +0800419 * Clear revoked flag to reflect there is no revoked buffers
420 * in the next transaction which is going to be started.
421 */
422 journal_clear_buffer_revoked_flags(journal);
423
424 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * Switch to a new revoke table.
426 */
427 journal_switch_revoke_table(journal);
428
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200429 trace_jbd_commit_flushing(journal, commit_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 commit_transaction->t_state = T_FLUSH;
431 journal->j_committing_transaction = commit_transaction;
432 journal->j_running_transaction = NULL;
Josef Bacikf420d4d2009-01-07 18:07:24 -0800433 start_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 commit_transaction->t_log_start = journal->j_head;
435 wake_up(&journal->j_wait_transaction_locked);
436 spin_unlock(&journal->j_state_lock);
437
438 jbd_debug (3, "JBD: commit phase 2\n");
439
Jan Kara2db938b2011-02-21 17:25:37 +0100440 if (tid_geq(journal->j_commit_waited, commit_transaction->t_tid))
441 write_op = WRITE_SYNC;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * Now start flushing things to disk, in the order they appear
445 * on the transaction lists. Data blocks go first.
446 */
Jens Axboe65ab8022011-03-17 10:56:45 +0100447 blk_start_plug(&plug);
Theodore Ts'o512a0042009-03-27 22:14:27 -0400448 err = journal_submit_data_buffers(journal, commit_transaction,
Jan Kara2db938b2011-02-21 17:25:37 +0100449 write_op);
Jens Axboe65ab8022011-03-17 10:56:45 +0100450 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /*
453 * Wait for all previously submitted IO to complete.
454 */
Jan Kara3998b932006-09-25 23:30:53 -0700455 spin_lock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 while (commit_transaction->t_locked_list) {
457 struct buffer_head *bh;
458
459 jh = commit_transaction->t_locked_list->b_tprev;
460 bh = jh2bh(jh);
461 get_bh(bh);
462 if (buffer_locked(bh)) {
463 spin_unlock(&journal->j_list_lock);
464 wait_on_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_lock(&journal->j_list_lock);
466 }
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700467 if (unlikely(!buffer_uptodate(bh))) {
Nick Piggin529ae9a2008-08-02 12:01:03 +0200468 if (!trylock_page(bh->b_page)) {
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700469 spin_unlock(&journal->j_list_lock);
470 lock_page(bh->b_page);
471 spin_lock(&journal->j_list_lock);
472 }
473 if (bh->b_page->mapping)
474 set_bit(AS_EIO, &bh->b_page->mapping->flags);
475
476 unlock_page(bh->b_page);
477 SetPageError(bh->b_page);
478 err = -EIO;
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!inverted_lock(journal, bh)) {
481 put_bh(bh);
482 spin_lock(&journal->j_list_lock);
483 continue;
484 }
Jan Karaa61d90d2009-06-09 16:26:26 -0700485 if (buffer_jbd(bh) && bh2jh(bh) == jh &&
486 jh->b_transaction == commit_transaction &&
Jan Karabb189242011-06-24 23:11:59 +0200487 jh->b_jlist == BJ_Locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 __journal_unfile_buffer(jh);
Jan Karabb189242011-06-24 23:11:59 +0200489 jbd_unlock_bh_state(bh);
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -0700490 release_data_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 cond_resched_lock(&journal->j_list_lock);
492 }
493 spin_unlock(&journal->j_list_lock);
494
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700495 if (err) {
496 char b[BDEVNAME_SIZE];
497
498 printk(KERN_WARNING
499 "JBD: Detected IO errors while flushing file data "
500 "on %s\n", bdevname(journal->j_fs_dev, b));
Hidehiro Kawai0e4fb5e2008-10-18 20:27:57 -0700501 if (journal->j_flags & JFS_ABORT_ON_SYNCDATA_ERR)
502 journal_abort(journal, err);
Hidehiro Kawaicbe5f462008-07-25 01:46:30 -0700503 err = 0;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Jens Axboe65ab8022011-03-17 10:56:45 +0100506 blk_start_plug(&plug);
507
Jan Kara2db938b2011-02-21 17:25:37 +0100508 journal_write_revoke_records(journal, commit_transaction, write_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /*
511 * If we found any dirty or locked buffers, then we should have
512 * looped back up to the write_out_data label. If there weren't
513 * any then journal_clean_data_list should have wiped the list
514 * clean by now, so check that it is in fact empty.
515 */
516 J_ASSERT (commit_transaction->t_sync_datalist == NULL);
517
518 jbd_debug (3, "JBD: commit phase 3\n");
519
520 /*
521 * Way to go: we have now written out all of the data for a
522 * transaction! Now comes the tricky part: we need to write out
523 * metadata. Loop over the transaction's entire buffer list:
524 */
Mingming Cao772279c2008-05-14 16:05:41 -0700525 spin_lock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 commit_transaction->t_state = T_COMMIT;
Mingming Cao772279c2008-05-14 16:05:41 -0700527 spin_unlock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200529 trace_jbd_commit_logging(journal, commit_transaction);
Josef Bacik5b9a4992008-04-28 02:16:12 -0700530 J_ASSERT(commit_transaction->t_nr_buffers <=
531 commit_transaction->t_outstanding_credits);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 descriptor = NULL;
534 bufs = 0;
535 while (commit_transaction->t_buffers) {
536
537 /* Find the next buffer to be journaled... */
538
539 jh = commit_transaction->t_buffers;
540
541 /* If we're in abort mode, we just un-journal the buffer and
Hidehiro Kawai885e3532008-10-18 20:27:54 -0700542 release it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (is_journal_aborted(journal)) {
Hidehiro Kawai885e3532008-10-18 20:27:54 -0700545 clear_buffer_jbddirty(jh2bh(jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 JBUFFER_TRACE(jh, "journal is aborting: refile");
547 journal_refile_buffer(journal, jh);
548 /* If that was the last one, we need to clean up
549 * any descriptor buffers which may have been
550 * already allocated, even if we are now
551 * aborting. */
552 if (!commit_transaction->t_buffers)
553 goto start_journal_io;
554 continue;
555 }
556
557 /* Make sure we have a descriptor block in which to
558 record the metadata buffer. */
559
560 if (!descriptor) {
561 struct buffer_head *bh;
562
563 J_ASSERT (bufs == 0);
564
565 jbd_debug(4, "JBD: get descriptor\n");
566
567 descriptor = journal_get_descriptor_buffer(journal);
568 if (!descriptor) {
Jan Kara7a266e72007-10-18 23:39:22 -0700569 journal_abort(journal, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 continue;
571 }
572
573 bh = jh2bh(descriptor);
574 jbd_debug(4, "JBD: got buffer %llu (%p)\n",
575 (unsigned long long)bh->b_blocknr, bh->b_data);
576 header = (journal_header_t *)&bh->b_data[0];
577 header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
578 header->h_blocktype = cpu_to_be32(JFS_DESCRIPTOR_BLOCK);
579 header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
580
581 tagp = &bh->b_data[sizeof(journal_header_t)];
582 space_left = bh->b_size - sizeof(journal_header_t);
583 first_tag = 1;
584 set_buffer_jwrite(bh);
585 set_buffer_dirty(bh);
586 wbuf[bufs++] = bh;
587
588 /* Record it so that we can wait for IO
589 completion later */
590 BUFFER_TRACE(bh, "ph3: file as descriptor");
591 journal_file_buffer(descriptor, commit_transaction,
592 BJ_LogCtl);
593 }
594
595 /* Where is the buffer to be written? */
596
597 err = journal_next_log_block(journal, &blocknr);
598 /* If the block mapping failed, just abandon the buffer
599 and repeat this loop: we'll fall into the
600 refile-on-abort condition above. */
601 if (err) {
Jan Kara7a266e72007-10-18 23:39:22 -0700602 journal_abort(journal, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 continue;
604 }
605
606 /*
607 * start_this_handle() uses t_outstanding_credits to determine
608 * the free space in the log, but this counter is changed
609 * by journal_next_log_block() also.
610 */
611 commit_transaction->t_outstanding_credits--;
612
613 /* Bump b_count to prevent truncate from stumbling over
614 the shadowed buffer! @@@ This can go if we ever get
615 rid of the BJ_IO/BJ_Shadow pairing of buffers. */
Namhyung Kime4d5e3a2010-10-16 17:11:02 +0900616 get_bh(jh2bh(jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* Make a temporary IO buffer with which to write it out
619 (this will requeue both the metadata buffer and the
620 temporary IO buffer). new_bh goes on BJ_IO*/
621
Namhyung Kima910eef2010-10-08 20:05:06 +0900622 set_buffer_jwrite(jh2bh(jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
624 * akpm: journal_write_metadata_buffer() sets
625 * new_bh->b_transaction to commit_transaction.
626 * We need to clean this up before we release new_bh
627 * (which is of type BJ_IO)
628 */
629 JBUFFER_TRACE(jh, "ph3: write metadata");
630 flags = journal_write_metadata_buffer(commit_transaction,
631 jh, &new_jh, blocknr);
Namhyung Kima910eef2010-10-08 20:05:06 +0900632 set_buffer_jwrite(jh2bh(new_jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 wbuf[bufs++] = jh2bh(new_jh);
634
635 /* Record the new block's tag in the current descriptor
636 buffer */
637
638 tag_flag = 0;
639 if (flags & 1)
640 tag_flag |= JFS_FLAG_ESCAPE;
641 if (!first_tag)
642 tag_flag |= JFS_FLAG_SAME_UUID;
643
644 tag = (journal_block_tag_t *) tagp;
645 tag->t_blocknr = cpu_to_be32(jh2bh(jh)->b_blocknr);
646 tag->t_flags = cpu_to_be32(tag_flag);
647 tagp += sizeof(journal_block_tag_t);
648 space_left -= sizeof(journal_block_tag_t);
649
650 if (first_tag) {
651 memcpy (tagp, journal->j_uuid, 16);
652 tagp += 16;
653 space_left -= 16;
654 first_tag = 0;
655 }
656
657 /* If there's no more to do, or if the descriptor is full,
658 let the IO rip! */
659
660 if (bufs == journal->j_wbufsize ||
661 commit_transaction->t_buffers == NULL ||
662 space_left < sizeof(journal_block_tag_t) + 16) {
663
664 jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
665
666 /* Write an end-of-descriptor marker before
667 submitting the IOs. "tag" still points to
668 the last tag we set up. */
669
670 tag->t_flags |= cpu_to_be32(JFS_FLAG_LAST_TAG);
671
672start_journal_io:
673 for (i = 0; i < bufs; i++) {
674 struct buffer_head *bh = wbuf[i];
675 lock_buffer(bh);
676 clear_buffer_dirty(bh);
677 set_buffer_uptodate(bh);
678 bh->b_end_io = journal_end_buffer_io_sync;
Darrick J. Wong7136851112013-04-29 15:07:25 -0700679 /*
680 * In data=journal mode, here we can end up
681 * writing pagecache data that might be
682 * mmapped. Since we can't afford to clean the
683 * page and set PageWriteback (see the comment
684 * near the other use of _submit_bh()), the
685 * data can change while the write is in
686 * flight. Tell the block layer to bounce the
687 * bio pages if stable pages are required.
688 */
689 _submit_bh(write_op, bh, 1 << BIO_SNAP_STABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 cond_resched();
692
693 /* Force a new descriptor to be generated next
694 time round the loop. */
695 descriptor = NULL;
696 bufs = 0;
697 }
698 }
699
Jens Axboe65ab8022011-03-17 10:56:45 +0100700 blk_finish_plug(&plug);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* Lo and behold: we have just managed to send a transaction to
703 the log. Before we can commit it, wait for the IO so far to
704 complete. Control buffers being written are on the
705 transaction's t_log_list queue, and metadata buffers are on
706 the t_iobuf_list queue.
707
708 Wait for the buffers in reverse order. That way we are
709 less likely to be woken up until all IOs have completed, and
710 so we incur less scheduling load.
711 */
712
713 jbd_debug(3, "JBD: commit phase 4\n");
714
715 /*
716 * akpm: these are BJ_IO, and j_list_lock is not needed.
717 * See __journal_try_to_free_buffer.
718 */
719wait_for_iobuf:
720 while (commit_transaction->t_iobuf_list != NULL) {
721 struct buffer_head *bh;
722
723 jh = commit_transaction->t_iobuf_list->b_tprev;
724 bh = jh2bh(jh);
725 if (buffer_locked(bh)) {
726 wait_on_buffer(bh);
727 goto wait_for_iobuf;
728 }
729 if (cond_resched())
730 goto wait_for_iobuf;
731
732 if (unlikely(!buffer_uptodate(bh)))
733 err = -EIO;
734
735 clear_buffer_jwrite(bh);
736
737 JBUFFER_TRACE(jh, "ph4: unfile after journal write");
738 journal_unfile_buffer(journal, jh);
739
740 /*
741 * ->t_iobuf_list should contain only dummy buffer_heads
742 * which were created by journal_write_metadata_buffer().
743 */
744 BUFFER_TRACE(bh, "dumping temporary bh");
745 journal_put_journal_head(jh);
746 __brelse(bh);
747 J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
748 free_buffer_head(bh);
749
750 /* We also have to unlock and free the corresponding
751 shadowed buffer */
752 jh = commit_transaction->t_shadow_list->b_tprev;
753 bh = jh2bh(jh);
Namhyung Kima910eef2010-10-08 20:05:06 +0900754 clear_buffer_jwrite(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 J_ASSERT_BH(bh, buffer_jbddirty(bh));
756
757 /* The metadata is now released for reuse, but we need
758 to remember it against this transaction so that when
759 we finally commit, we can do any checkpointing
760 required. */
761 JBUFFER_TRACE(jh, "file as BJ_Forget");
762 journal_file_buffer(jh, commit_transaction, BJ_Forget);
Jan Kara2842bb22011-05-05 13:59:35 +0200763 /*
764 * Wake up any transactions which were waiting for this
765 * IO to complete. The barrier must be here so that changes
766 * by journal_file_buffer() take effect before wake_up_bit()
767 * does the waitqueue check.
768 */
769 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 wake_up_bit(&bh->b_state, BH_Unshadow);
771 JBUFFER_TRACE(jh, "brelse shadowed buffer");
772 __brelse(bh);
773 }
774
775 J_ASSERT (commit_transaction->t_shadow_list == NULL);
776
777 jbd_debug(3, "JBD: commit phase 5\n");
778
779 /* Here we wait for the revoke record and descriptor record buffers */
780 wait_for_ctlbuf:
781 while (commit_transaction->t_log_list != NULL) {
782 struct buffer_head *bh;
783
784 jh = commit_transaction->t_log_list->b_tprev;
785 bh = jh2bh(jh);
786 if (buffer_locked(bh)) {
787 wait_on_buffer(bh);
788 goto wait_for_ctlbuf;
789 }
790 if (cond_resched())
791 goto wait_for_ctlbuf;
792
793 if (unlikely(!buffer_uptodate(bh)))
794 err = -EIO;
795
796 BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
797 clear_buffer_jwrite(bh);
798 journal_unfile_buffer(journal, jh);
799 journal_put_journal_head(jh);
800 __brelse(bh); /* One for getblk */
801 /* AKPM: bforget here */
802 }
803
Hidehiro Kawaid1645e52008-10-18 20:27:53 -0700804 if (err)
805 journal_abort(journal, err);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 jbd_debug(3, "JBD: commit phase 6\n");
808
Jan Kara03f4d802010-04-15 22:16:24 +0200809 /* All metadata is written, now write commit record and do cleanup */
810 spin_lock(&journal->j_state_lock);
811 J_ASSERT(commit_transaction->t_state == T_COMMIT);
812 commit_transaction->t_state = T_COMMIT_RECORD;
813 spin_unlock(&journal->j_state_lock);
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (journal_write_commit_record(journal, commit_transaction))
816 err = -EIO;
817
818 if (err)
Jan Kara7a266e72007-10-18 23:39:22 -0700819 journal_abort(journal, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* End of a transaction! Finally, we can do checkpoint
822 processing: any buffers committed as a result of this
823 transaction can be removed from any checkpoint list it was on
824 before. */
825
826 jbd_debug(3, "JBD: commit phase 7\n");
827
828 J_ASSERT(commit_transaction->t_sync_datalist == NULL);
829 J_ASSERT(commit_transaction->t_buffers == NULL);
830 J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
831 J_ASSERT(commit_transaction->t_iobuf_list == NULL);
832 J_ASSERT(commit_transaction->t_shadow_list == NULL);
833 J_ASSERT(commit_transaction->t_log_list == NULL);
834
835restart_loop:
Jan Karae6c9f5c2005-09-06 15:19:09 -0700836 /*
837 * As there are other places (journal_unmap_buffer()) adding buffers
838 * to this list we have to be careful and hold the j_list_lock.
839 */
840 spin_lock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 while (commit_transaction->t_forget) {
842 transaction_t *cp_transaction;
843 struct buffer_head *bh;
Jan Karabb189242011-06-24 23:11:59 +0200844 int try_to_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 jh = commit_transaction->t_forget;
Jan Karae6c9f5c2005-09-06 15:19:09 -0700847 spin_unlock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 bh = jh2bh(jh);
Jan Karabb189242011-06-24 23:11:59 +0200849 /*
850 * Get a reference so that bh cannot be freed before we are
851 * done with it.
852 */
853 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 jbd_lock_bh_state(bh);
855 J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
856 jh->b_transaction == journal->j_running_transaction);
857
858 /*
859 * If there is undo-protected committed data against
860 * this buffer, then we can remove it now. If it is a
861 * buffer needing such protection, the old frozen_data
862 * field now points to a committed version of the
863 * buffer, so rotate that field to the new committed
864 * data.
865 *
866 * Otherwise, we can just throw away the frozen data now.
867 */
868 if (jh->b_committed_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400869 jbd_free(jh->b_committed_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 jh->b_committed_data = NULL;
871 if (jh->b_frozen_data) {
872 jh->b_committed_data = jh->b_frozen_data;
873 jh->b_frozen_data = NULL;
874 }
875 } else if (jh->b_frozen_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400876 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 jh->b_frozen_data = NULL;
878 }
879
880 spin_lock(&journal->j_list_lock);
881 cp_transaction = jh->b_cp_transaction;
882 if (cp_transaction) {
883 JBUFFER_TRACE(jh, "remove from old cp transaction");
884 __journal_remove_checkpoint(jh);
885 }
886
887 /* Only re-checkpoint the buffer_head if it is marked
888 * dirty. If the buffer was added to the BJ_Forget list
889 * by journal_forget, it may no longer be dirty and
890 * there's no point in keeping a checkpoint record for
891 * it. */
892
Jan Kara09e05d42012-07-11 23:16:25 +0200893 /*
894 * A buffer which has been freed while still being journaled by
895 * a previous transaction.
896 */
897 if (buffer_freed(bh)) {
898 /*
899 * If the running transaction is the one containing
900 * "add to orphan" operation (b_next_transaction !=
901 * NULL), we have to wait for that transaction to
902 * commit before we can really get rid of the buffer.
903 * So just clear b_modified to not confuse transaction
904 * credit accounting and refile the buffer to
905 * BJ_Forget of the running transaction. If the just
906 * committed transaction contains "add to orphan"
907 * operation, we can completely invalidate the buffer
908 * now. We are rather throughout in that since the
909 * buffer may be still accessible when blocksize <
910 * pagesize and it is attached to the last partial
911 * page.
912 */
913 jh->b_modified = 0;
914 if (!jh->b_next_transaction) {
915 clear_buffer_freed(bh);
916 clear_buffer_jbddirty(bh);
917 clear_buffer_mapped(bh);
918 clear_buffer_new(bh);
919 clear_buffer_req(bh);
920 bh->b_bdev = NULL;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
924 if (buffer_jbddirty(bh)) {
925 JBUFFER_TRACE(jh, "add to new checkpointing trans");
926 __journal_insert_checkpoint(jh, commit_transaction);
Hidehiro Kawai885e3532008-10-18 20:27:54 -0700927 if (is_journal_aborted(journal))
928 clear_buffer_jbddirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 } else {
930 J_ASSERT_BH(bh, !buffer_dirty(bh));
Jan Karabb189242011-06-24 23:11:59 +0200931 /*
932 * The buffer on BJ_Forget list and not jbddirty means
Jan Kara9ada7342006-06-23 02:05:25 -0700933 * it has been freed by this transaction and hence it
934 * could not have been reallocated until this
935 * transaction has committed. *BUT* it could be
936 * reallocated once we have written all the data to
937 * disk and before we process the buffer on BJ_Forget
Jan Karabb189242011-06-24 23:11:59 +0200938 * list.
939 */
940 if (!jh->b_next_transaction)
941 try_to_free = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Jan Karabb189242011-06-24 23:11:59 +0200943 JBUFFER_TRACE(jh, "refile or unfile freed buffer");
944 __journal_refile_buffer(jh);
945 jbd_unlock_bh_state(bh);
946 if (try_to_free)
947 release_buffer_page(bh);
948 else
949 __brelse(bh);
Jan Karae6c9f5c2005-09-06 15:19:09 -0700950 cond_resched_lock(&journal->j_list_lock);
951 }
952 spin_unlock(&journal->j_list_lock);
953 /*
Jan Karad4beaf42007-12-04 23:45:27 -0800954 * This is a bit sleazy. We use j_list_lock to protect transition
955 * of a transaction into T_FINISHED state and calling
956 * __journal_drop_transaction(). Otherwise we could race with
957 * other checkpointing code processing the transaction...
Jan Karae6c9f5c2005-09-06 15:19:09 -0700958 */
959 spin_lock(&journal->j_state_lock);
960 spin_lock(&journal->j_list_lock);
961 /*
962 * Now recheck if some buffers did not get attached to the transaction
963 * while the lock was dropped...
964 */
965 if (commit_transaction->t_forget) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_unlock(&journal->j_list_lock);
Jan Karae6c9f5c2005-09-06 15:19:09 -0700967 spin_unlock(&journal->j_state_lock);
968 goto restart_loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
971 /* Done with this transaction! */
972
973 jbd_debug(3, "JBD: commit phase 8\n");
974
Jan Kara03f4d802010-04-15 22:16:24 +0200975 J_ASSERT(commit_transaction->t_state == T_COMMIT_RECORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 commit_transaction->t_state = T_FINISHED;
978 J_ASSERT(commit_transaction == journal->j_committing_transaction);
979 journal->j_commit_sequence = commit_transaction->t_tid;
980 journal->j_committing_transaction = NULL;
Josef Bacikf420d4d2009-01-07 18:07:24 -0800981 commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
982
983 /*
984 * weight the commit time higher than the average time so we don't
985 * react too strongly to vast changes in commit time
986 */
987 if (likely(journal->j_average_commit_time))
988 journal->j_average_commit_time = (commit_time*3 +
989 journal->j_average_commit_time) / 4;
990 else
991 journal->j_average_commit_time = commit_time;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 spin_unlock(&journal->j_state_lock);
994
Jan Karafe28e422007-07-15 23:37:18 -0700995 if (commit_transaction->t_checkpoint_list == NULL &&
996 commit_transaction->t_checkpoint_io_list == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 __journal_drop_transaction(journal, commit_transaction);
998 } else {
999 if (journal->j_checkpoint_transactions == NULL) {
1000 journal->j_checkpoint_transactions = commit_transaction;
1001 commit_transaction->t_cpnext = commit_transaction;
1002 commit_transaction->t_cpprev = commit_transaction;
1003 } else {
1004 commit_transaction->t_cpnext =
1005 journal->j_checkpoint_transactions;
1006 commit_transaction->t_cpprev =
1007 commit_transaction->t_cpnext->t_cpprev;
1008 commit_transaction->t_cpnext->t_cpprev =
1009 commit_transaction;
1010 commit_transaction->t_cpprev->t_cpnext =
1011 commit_transaction;
1012 }
1013 }
1014 spin_unlock(&journal->j_list_lock);
1015
Lukas Czerner99cb1a32011-05-23 18:33:02 +02001016 trace_jbd_end_commit(journal, commit_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 jbd_debug(1, "JBD: commit %d complete, head %d\n",
1018 journal->j_commit_sequence, journal->j_tail_sequence);
1019
1020 wake_up(&journal->j_wait_done_commit);
1021}