blob: f943b9b3f208fcb4d279fb33b3570096bea2e3dc [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>
20#include <linux/slab.h>
21#include <linux/mm.h>
22#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Default IO end handler for temporary BJ_IO buffer_heads.
26 */
27static 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/*
38 * When an ext3-ordered file is truncated, it is possible that many pages are
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070039 * not successfully freed, because they are attached to a committing transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * After the transaction commits, these pages are left on the LRU, with no
41 * ->mapping, and with attached buffers. These pages are trivially reclaimable
42 * by the VM, but their apparent absence upsets the VM accounting, and it makes
43 * the numbers in /proc/meminfo look odd.
44 *
45 * So here, we have a buffer which has just come off the forget list. Look to
46 * see if we can strip all buffers from the backing page.
47 *
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070048 * Called under journal->j_list_lock. The caller provided us with a ref
49 * against the buffer, and we drop that here.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51static void release_buffer_page(struct buffer_head *bh)
52{
53 struct page *page;
54
55 if (buffer_dirty(bh))
56 goto nope;
57 if (atomic_read(&bh->b_count) != 1)
58 goto nope;
59 page = bh->b_page;
60 if (!page)
61 goto nope;
62 if (page->mapping)
63 goto nope;
64
65 /* OK, it's a truncated page */
66 if (TestSetPageLocked(page))
67 goto nope;
68
69 page_cache_get(page);
70 __brelse(bh);
71 try_to_free_buffers(page);
72 unlock_page(page);
73 page_cache_release(page);
74 return;
75
76nope:
77 __brelse(bh);
78}
79
80/*
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -070081 * Decrement reference counter for data buffer. If it has been marked
82 * 'BH_Freed', release it and the page to which it belongs if possible.
83 */
84static void release_data_buffer(struct buffer_head *bh)
85{
86 if (buffer_freed(bh)) {
87 clear_buffer_freed(bh);
88 release_buffer_page(bh);
89 } else
90 put_bh(bh);
91}
92
93/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is
95 * held. For ranking reasons we must trylock. If we lose, schedule away and
96 * return 0. j_list_lock is dropped in this case.
97 */
98static int inverted_lock(journal_t *journal, struct buffer_head *bh)
99{
100 if (!jbd_trylock_bh_state(bh)) {
101 spin_unlock(&journal->j_list_lock);
102 schedule();
103 return 0;
104 }
105 return 1;
106}
107
108/* Done it all: now write the commit record. We should have
109 * cleaned up our previous buffers by now, so if we are in abort
110 * mode we can now just skip the rest of the journal write
111 * entirely.
112 *
113 * Returns 1 if the journal needs to be aborted or 0 on success
114 */
115static int journal_write_commit_record(journal_t *journal,
116 transaction_t *commit_transaction)
117{
118 struct journal_head *descriptor;
119 struct buffer_head *bh;
Jan Kara53152172008-02-01 08:26:46 -0500120 journal_header_t *header;
121 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int barrier_done = 0;
123
124 if (is_journal_aborted(journal))
125 return 0;
126
127 descriptor = journal_get_descriptor_buffer(journal);
128 if (!descriptor)
129 return 1;
130
131 bh = jh2bh(descriptor);
132
Jan Kara53152172008-02-01 08:26:46 -0500133 header = (journal_header_t *)(bh->b_data);
134 header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
135 header->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK);
136 header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 JBUFFER_TRACE(descriptor, "write commit block");
139 set_buffer_dirty(bh);
140 if (journal->j_flags & JFS_BARRIER) {
141 set_buffer_ordered(bh);
142 barrier_done = 1;
143 }
144 ret = sync_dirty_buffer(bh);
Neil Brown28ae0942008-02-08 04:22:13 -0800145 if (barrier_done)
146 clear_buffer_ordered(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* is it possible for another commit to fail at roughly
148 * the same time as this one? If so, we don't want to
149 * trust the barrier flag in the super, but instead want
150 * to remember if we sent a barrier request
151 */
152 if (ret == -EOPNOTSUPP && barrier_done) {
153 char b[BDEVNAME_SIZE];
154
155 printk(KERN_WARNING
156 "JBD: barrier-based sync failed on %s - "
157 "disabling barriers\n",
158 bdevname(journal->j_dev, b));
159 spin_lock(&journal->j_state_lock);
160 journal->j_flags &= ~JFS_BARRIER;
161 spin_unlock(&journal->j_state_lock);
162
163 /* And try again, without the barrier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 set_buffer_uptodate(bh);
165 set_buffer_dirty(bh);
166 ret = sync_dirty_buffer(bh);
167 }
168 put_bh(bh); /* One for getblk() */
169 journal_put_journal_head(descriptor);
170
171 return (ret == -EIO);
172}
173
Jan Kara3998b932006-09-25 23:30:53 -0700174static void journal_do_submit_data(struct buffer_head **wbuf, int bufs)
175{
176 int i;
177
178 for (i = 0; i < bufs; i++) {
179 wbuf[i]->b_end_io = end_buffer_write_sync;
180 /* We use-up our safety reference in submit_bh() */
181 submit_bh(WRITE, wbuf[i]);
182 }
183}
184
185/*
186 * Submit all the data buffers to disk
187 */
188static void journal_submit_data_buffers(journal_t *journal,
189 transaction_t *commit_transaction)
190{
191 struct journal_head *jh;
192 struct buffer_head *bh;
193 int locked;
194 int bufs = 0;
195 struct buffer_head **wbuf = journal->j_wbuf;
196
197 /*
198 * Whenever we unlock the journal and sleep, things can get added
199 * onto ->t_sync_datalist, so we have to keep looping back to
200 * write_out_data until we *know* that the list is empty.
201 *
202 * Cleanup any flushed data buffers from the data list. Even in
203 * abort mode, we want to flush this out as soon as possible.
204 */
205write_out_data:
206 cond_resched();
207 spin_lock(&journal->j_list_lock);
208
209 while (commit_transaction->t_sync_datalist) {
210 jh = commit_transaction->t_sync_datalist;
211 bh = jh2bh(jh);
212 locked = 0;
213
214 /* Get reference just to make sure buffer does not disappear
215 * when we are forced to drop various locks */
216 get_bh(bh);
217 /* If the buffer is dirty, we need to submit IO and hence
218 * we need the buffer lock. We try to lock the buffer without
219 * blocking. If we fail, we need to drop j_list_lock and do
220 * blocking lock_buffer().
221 */
222 if (buffer_dirty(bh)) {
223 if (test_set_buffer_locked(bh)) {
224 BUFFER_TRACE(bh, "needs blocking lock");
225 spin_unlock(&journal->j_list_lock);
226 /* Write out all data to prevent deadlocks */
227 journal_do_submit_data(wbuf, bufs);
228 bufs = 0;
229 lock_buffer(bh);
230 spin_lock(&journal->j_list_lock);
231 }
232 locked = 1;
233 }
234 /* We have to get bh_state lock. Again out of order, sigh. */
235 if (!inverted_lock(journal, bh)) {
236 jbd_lock_bh_state(bh);
237 spin_lock(&journal->j_list_lock);
238 }
239 /* Someone already cleaned up the buffer? */
240 if (!buffer_jbd(bh)
241 || jh->b_transaction != commit_transaction
242 || jh->b_jlist != BJ_SyncData) {
243 jbd_unlock_bh_state(bh);
244 if (locked)
245 unlock_buffer(bh);
246 BUFFER_TRACE(bh, "already cleaned up");
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -0700247 release_data_buffer(bh);
Jan Kara3998b932006-09-25 23:30:53 -0700248 continue;
249 }
250 if (locked && test_clear_buffer_dirty(bh)) {
251 BUFFER_TRACE(bh, "needs writeout, adding to array");
252 wbuf[bufs++] = bh;
253 __journal_file_buffer(jh, commit_transaction,
254 BJ_Locked);
255 jbd_unlock_bh_state(bh);
256 if (bufs == journal->j_wbufsize) {
257 spin_unlock(&journal->j_list_lock);
258 journal_do_submit_data(wbuf, bufs);
259 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");
269 __journal_unfile_buffer(jh);
270 jbd_unlock_bh_state(bh);
271 if (locked)
272 unlock_buffer(bh);
273 journal_remove_journal_head(bh);
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -0700274 /* One for our safety reference, other for
Jan Kara3998b932006-09-25 23:30:53 -0700275 * journal_remove_journal_head() */
276 put_bh(bh);
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -0700277 release_data_buffer(bh);
Jan Kara3998b932006-09-25 23:30:53 -0700278 }
279
Nick Piggin95c354f2008-01-30 13:31:20 +0100280 if (need_resched() || spin_needbreak(&journal->j_list_lock)) {
Jan Kara3998b932006-09-25 23:30:53 -0700281 spin_unlock(&journal->j_list_lock);
282 goto write_out_data;
283 }
284 }
285 spin_unlock(&journal->j_list_lock);
286 journal_do_submit_data(wbuf, bufs);
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
290 * journal_commit_transaction
291 *
292 * The primary function for committing a transaction to the log. This
293 * function is called by the journal thread to begin a complete commit.
294 */
295void journal_commit_transaction(journal_t *journal)
296{
297 transaction_t *commit_transaction;
298 struct journal_head *jh, *new_jh, *descriptor;
299 struct buffer_head **wbuf = journal->j_wbuf;
300 int bufs;
301 int flags;
302 int err;
303 unsigned long blocknr;
304 char *tagp = NULL;
305 journal_header_t *header;
306 journal_block_tag_t *tag = NULL;
307 int space_left = 0;
308 int first_tag = 0;
309 int tag_flag;
310 int i;
311
312 /*
313 * First job: lock down the current transaction and wait for
314 * all outstanding updates to complete.
315 */
316
317#ifdef COMMIT_STATS
318 spin_lock(&journal->j_list_lock);
319 summarise_journal_usage(journal);
320 spin_unlock(&journal->j_list_lock);
321#endif
322
323 /* Do we need to erase the effects of a prior journal_flush? */
324 if (journal->j_flags & JFS_FLUSHED) {
325 jbd_debug(3, "super block updated\n");
326 journal_update_superblock(journal, 1);
327 } else {
328 jbd_debug(3, "superblock not updated\n");
329 }
330
331 J_ASSERT(journal->j_running_transaction != NULL);
332 J_ASSERT(journal->j_committing_transaction == NULL);
333
334 commit_transaction = journal->j_running_transaction;
335 J_ASSERT(commit_transaction->t_state == T_RUNNING);
336
337 jbd_debug(1, "JBD: starting commit of transaction %d\n",
338 commit_transaction->t_tid);
339
340 spin_lock(&journal->j_state_lock);
341 commit_transaction->t_state = T_LOCKED;
342
343 spin_lock(&commit_transaction->t_handle_lock);
344 while (commit_transaction->t_updates) {
345 DEFINE_WAIT(wait);
346
347 prepare_to_wait(&journal->j_wait_updates, &wait,
348 TASK_UNINTERRUPTIBLE);
349 if (commit_transaction->t_updates) {
350 spin_unlock(&commit_transaction->t_handle_lock);
351 spin_unlock(&journal->j_state_lock);
352 schedule();
353 spin_lock(&journal->j_state_lock);
354 spin_lock(&commit_transaction->t_handle_lock);
355 }
356 finish_wait(&journal->j_wait_updates, &wait);
357 }
358 spin_unlock(&commit_transaction->t_handle_lock);
359
360 J_ASSERT (commit_transaction->t_outstanding_credits <=
361 journal->j_max_transaction_buffers);
362
363 /*
364 * First thing we are allowed to do is to discard any remaining
365 * BJ_Reserved buffers. Note, it is _not_ permissible to assume
366 * that there are no such buffers: if a large filesystem
367 * operation like a truncate needs to split itself over multiple
368 * transactions, then it may try to do a journal_restart() while
369 * there are still BJ_Reserved buffers outstanding. These must
370 * be released cleanly from the current transaction.
371 *
372 * In this case, the filesystem must still reserve write access
373 * again before modifying the buffer in the new transaction, but
374 * we do not require it to remember exactly which old buffers it
375 * has reserved. This is consistent with the existing behaviour
376 * that multiple journal_get_write_access() calls to the same
377 * buffer are perfectly permissable.
378 */
379 while (commit_transaction->t_reserved_list) {
380 jh = commit_transaction->t_reserved_list;
381 JBUFFER_TRACE(jh, "reserved, unused: refile");
382 /*
383 * A journal_get_undo_access()+journal_release_buffer() may
384 * leave undo-committed data.
385 */
386 if (jh->b_committed_data) {
387 struct buffer_head *bh = jh2bh(jh);
388
389 jbd_lock_bh_state(bh);
Mingming Caoc089d492007-10-16 18:38:25 -0400390 jbd_free(jh->b_committed_data, bh->b_size);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800391 jh->b_committed_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 jbd_unlock_bh_state(bh);
393 }
394 journal_refile_buffer(journal, jh);
395 }
396
397 /*
398 * Now try to drop any written-back buffers from the journal's
399 * checkpoint lists. We do this *before* commit because it potentially
400 * frees some memory
401 */
402 spin_lock(&journal->j_list_lock);
403 __journal_clean_checkpoint_list(journal);
404 spin_unlock(&journal->j_list_lock);
405
406 jbd_debug (3, "JBD: commit phase 1\n");
407
408 /*
409 * Switch to a new revoke table.
410 */
411 journal_switch_revoke_table(journal);
412
413 commit_transaction->t_state = T_FLUSH;
414 journal->j_committing_transaction = commit_transaction;
415 journal->j_running_transaction = NULL;
416 commit_transaction->t_log_start = journal->j_head;
417 wake_up(&journal->j_wait_transaction_locked);
418 spin_unlock(&journal->j_state_lock);
419
420 jbd_debug (3, "JBD: commit phase 2\n");
421
422 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * Now start flushing things to disk, in the order they appear
424 * on the transaction lists. Data blocks go first.
425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 err = 0;
Jan Kara3998b932006-09-25 23:30:53 -0700427 journal_submit_data_buffers(journal, commit_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
430 * Wait for all previously submitted IO to complete.
431 */
Jan Kara3998b932006-09-25 23:30:53 -0700432 spin_lock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 while (commit_transaction->t_locked_list) {
434 struct buffer_head *bh;
435
436 jh = commit_transaction->t_locked_list->b_tprev;
437 bh = jh2bh(jh);
438 get_bh(bh);
439 if (buffer_locked(bh)) {
440 spin_unlock(&journal->j_list_lock);
441 wait_on_buffer(bh);
442 if (unlikely(!buffer_uptodate(bh)))
443 err = -EIO;
444 spin_lock(&journal->j_list_lock);
445 }
446 if (!inverted_lock(journal, bh)) {
447 put_bh(bh);
448 spin_lock(&journal->j_list_lock);
449 continue;
450 }
451 if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
452 __journal_unfile_buffer(jh);
453 jbd_unlock_bh_state(bh);
454 journal_remove_journal_head(bh);
455 put_bh(bh);
456 } else {
457 jbd_unlock_bh_state(bh);
458 }
Toshiyuki Okajimafc80c442008-07-25 01:46:29 -0700459 release_data_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 cond_resched_lock(&journal->j_list_lock);
461 }
462 spin_unlock(&journal->j_list_lock);
463
464 if (err)
Jan Kara7a266e72007-10-18 23:39:22 -0700465 journal_abort(journal, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 journal_write_revoke_records(journal, commit_transaction);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * If we found any dirty or locked buffers, then we should have
471 * looped back up to the write_out_data label. If there weren't
472 * any then journal_clean_data_list should have wiped the list
473 * clean by now, so check that it is in fact empty.
474 */
475 J_ASSERT (commit_transaction->t_sync_datalist == NULL);
476
477 jbd_debug (3, "JBD: commit phase 3\n");
478
479 /*
480 * Way to go: we have now written out all of the data for a
481 * transaction! Now comes the tricky part: we need to write out
482 * metadata. Loop over the transaction's entire buffer list:
483 */
Mingming Cao772279c2008-05-14 16:05:41 -0700484 spin_lock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 commit_transaction->t_state = T_COMMIT;
Mingming Cao772279c2008-05-14 16:05:41 -0700486 spin_unlock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Josef Bacik5b9a4992008-04-28 02:16:12 -0700488 J_ASSERT(commit_transaction->t_nr_buffers <=
489 commit_transaction->t_outstanding_credits);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 descriptor = NULL;
492 bufs = 0;
493 while (commit_transaction->t_buffers) {
494
495 /* Find the next buffer to be journaled... */
496
497 jh = commit_transaction->t_buffers;
498
499 /* If we're in abort mode, we just un-journal the buffer and
500 release it for background writing. */
501
502 if (is_journal_aborted(journal)) {
503 JBUFFER_TRACE(jh, "journal is aborting: refile");
504 journal_refile_buffer(journal, jh);
505 /* If that was the last one, we need to clean up
506 * any descriptor buffers which may have been
507 * already allocated, even if we are now
508 * aborting. */
509 if (!commit_transaction->t_buffers)
510 goto start_journal_io;
511 continue;
512 }
513
514 /* Make sure we have a descriptor block in which to
515 record the metadata buffer. */
516
517 if (!descriptor) {
518 struct buffer_head *bh;
519
520 J_ASSERT (bufs == 0);
521
522 jbd_debug(4, "JBD: get descriptor\n");
523
524 descriptor = journal_get_descriptor_buffer(journal);
525 if (!descriptor) {
Jan Kara7a266e72007-10-18 23:39:22 -0700526 journal_abort(journal, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 continue;
528 }
529
530 bh = jh2bh(descriptor);
531 jbd_debug(4, "JBD: got buffer %llu (%p)\n",
532 (unsigned long long)bh->b_blocknr, bh->b_data);
533 header = (journal_header_t *)&bh->b_data[0];
534 header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
535 header->h_blocktype = cpu_to_be32(JFS_DESCRIPTOR_BLOCK);
536 header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
537
538 tagp = &bh->b_data[sizeof(journal_header_t)];
539 space_left = bh->b_size - sizeof(journal_header_t);
540 first_tag = 1;
541 set_buffer_jwrite(bh);
542 set_buffer_dirty(bh);
543 wbuf[bufs++] = bh;
544
545 /* Record it so that we can wait for IO
546 completion later */
547 BUFFER_TRACE(bh, "ph3: file as descriptor");
548 journal_file_buffer(descriptor, commit_transaction,
549 BJ_LogCtl);
550 }
551
552 /* Where is the buffer to be written? */
553
554 err = journal_next_log_block(journal, &blocknr);
555 /* If the block mapping failed, just abandon the buffer
556 and repeat this loop: we'll fall into the
557 refile-on-abort condition above. */
558 if (err) {
Jan Kara7a266e72007-10-18 23:39:22 -0700559 journal_abort(journal, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 continue;
561 }
562
563 /*
564 * start_this_handle() uses t_outstanding_credits to determine
565 * the free space in the log, but this counter is changed
566 * by journal_next_log_block() also.
567 */
568 commit_transaction->t_outstanding_credits--;
569
570 /* Bump b_count to prevent truncate from stumbling over
571 the shadowed buffer! @@@ This can go if we ever get
572 rid of the BJ_IO/BJ_Shadow pairing of buffers. */
573 atomic_inc(&jh2bh(jh)->b_count);
574
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
579 set_bit(BH_JWrite, &jh2bh(jh)->b_state);
580 /*
581 * akpm: journal_write_metadata_buffer() sets
582 * new_bh->b_transaction to commit_transaction.
583 * We need to clean this up before we release new_bh
584 * (which is of type BJ_IO)
585 */
586 JBUFFER_TRACE(jh, "ph3: write metadata");
587 flags = journal_write_metadata_buffer(commit_transaction,
588 jh, &new_jh, blocknr);
589 set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
590 wbuf[bufs++] = jh2bh(new_jh);
591
592 /* Record the new block's tag in the current descriptor
593 buffer */
594
595 tag_flag = 0;
596 if (flags & 1)
597 tag_flag |= JFS_FLAG_ESCAPE;
598 if (!first_tag)
599 tag_flag |= JFS_FLAG_SAME_UUID;
600
601 tag = (journal_block_tag_t *) tagp;
602 tag->t_blocknr = cpu_to_be32(jh2bh(jh)->b_blocknr);
603 tag->t_flags = cpu_to_be32(tag_flag);
604 tagp += sizeof(journal_block_tag_t);
605 space_left -= sizeof(journal_block_tag_t);
606
607 if (first_tag) {
608 memcpy (tagp, journal->j_uuid, 16);
609 tagp += 16;
610 space_left -= 16;
611 first_tag = 0;
612 }
613
614 /* If there's no more to do, or if the descriptor is full,
615 let the IO rip! */
616
617 if (bufs == journal->j_wbufsize ||
618 commit_transaction->t_buffers == NULL ||
619 space_left < sizeof(journal_block_tag_t) + 16) {
620
621 jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
622
623 /* Write an end-of-descriptor marker before
624 submitting the IOs. "tag" still points to
625 the last tag we set up. */
626
627 tag->t_flags |= cpu_to_be32(JFS_FLAG_LAST_TAG);
628
629start_journal_io:
630 for (i = 0; i < bufs; i++) {
631 struct buffer_head *bh = wbuf[i];
632 lock_buffer(bh);
633 clear_buffer_dirty(bh);
634 set_buffer_uptodate(bh);
635 bh->b_end_io = journal_end_buffer_io_sync;
636 submit_bh(WRITE, bh);
637 }
638 cond_resched();
639
640 /* Force a new descriptor to be generated next
641 time round the loop. */
642 descriptor = NULL;
643 bufs = 0;
644 }
645 }
646
647 /* Lo and behold: we have just managed to send a transaction to
648 the log. Before we can commit it, wait for the IO so far to
649 complete. Control buffers being written are on the
650 transaction's t_log_list queue, and metadata buffers are on
651 the t_iobuf_list queue.
652
653 Wait for the buffers in reverse order. That way we are
654 less likely to be woken up until all IOs have completed, and
655 so we incur less scheduling load.
656 */
657
658 jbd_debug(3, "JBD: commit phase 4\n");
659
660 /*
661 * akpm: these are BJ_IO, and j_list_lock is not needed.
662 * See __journal_try_to_free_buffer.
663 */
664wait_for_iobuf:
665 while (commit_transaction->t_iobuf_list != NULL) {
666 struct buffer_head *bh;
667
668 jh = commit_transaction->t_iobuf_list->b_tprev;
669 bh = jh2bh(jh);
670 if (buffer_locked(bh)) {
671 wait_on_buffer(bh);
672 goto wait_for_iobuf;
673 }
674 if (cond_resched())
675 goto wait_for_iobuf;
676
677 if (unlikely(!buffer_uptodate(bh)))
678 err = -EIO;
679
680 clear_buffer_jwrite(bh);
681
682 JBUFFER_TRACE(jh, "ph4: unfile after journal write");
683 journal_unfile_buffer(journal, jh);
684
685 /*
686 * ->t_iobuf_list should contain only dummy buffer_heads
687 * which were created by journal_write_metadata_buffer().
688 */
689 BUFFER_TRACE(bh, "dumping temporary bh");
690 journal_put_journal_head(jh);
691 __brelse(bh);
692 J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
693 free_buffer_head(bh);
694
695 /* We also have to unlock and free the corresponding
696 shadowed buffer */
697 jh = commit_transaction->t_shadow_list->b_tprev;
698 bh = jh2bh(jh);
699 clear_bit(BH_JWrite, &bh->b_state);
700 J_ASSERT_BH(bh, buffer_jbddirty(bh));
701
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 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
715 J_ASSERT (commit_transaction->t_shadow_list == NULL);
716
717 jbd_debug(3, "JBD: commit phase 5\n");
718
719 /* Here we wait for the revoke record and descriptor record buffers */
720 wait_for_ctlbuf:
721 while (commit_transaction->t_log_list != NULL) {
722 struct buffer_head *bh;
723
724 jh = commit_transaction->t_log_list->b_tprev;
725 bh = jh2bh(jh);
726 if (buffer_locked(bh)) {
727 wait_on_buffer(bh);
728 goto wait_for_ctlbuf;
729 }
730 if (cond_resched())
731 goto wait_for_ctlbuf;
732
733 if (unlikely(!buffer_uptodate(bh)))
734 err = -EIO;
735
736 BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
737 clear_buffer_jwrite(bh);
738 journal_unfile_buffer(journal, jh);
739 journal_put_journal_head(jh);
740 __brelse(bh); /* One for getblk */
741 /* AKPM: bforget here */
742 }
743
744 jbd_debug(3, "JBD: commit phase 6\n");
745
746 if (journal_write_commit_record(journal, commit_transaction))
747 err = -EIO;
748
749 if (err)
Jan Kara7a266e72007-10-18 23:39:22 -0700750 journal_abort(journal, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* End of a transaction! Finally, we can do checkpoint
753 processing: any buffers committed as a result of this
754 transaction can be removed from any checkpoint list it was on
755 before. */
756
757 jbd_debug(3, "JBD: commit phase 7\n");
758
759 J_ASSERT(commit_transaction->t_sync_datalist == NULL);
760 J_ASSERT(commit_transaction->t_buffers == NULL);
761 J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
762 J_ASSERT(commit_transaction->t_iobuf_list == NULL);
763 J_ASSERT(commit_transaction->t_shadow_list == NULL);
764 J_ASSERT(commit_transaction->t_log_list == NULL);
765
766restart_loop:
Jan Karae6c9f5c2005-09-06 15:19:09 -0700767 /*
768 * As there are other places (journal_unmap_buffer()) adding buffers
769 * to this list we have to be careful and hold the j_list_lock.
770 */
771 spin_lock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 while (commit_transaction->t_forget) {
773 transaction_t *cp_transaction;
774 struct buffer_head *bh;
775
776 jh = commit_transaction->t_forget;
Jan Karae6c9f5c2005-09-06 15:19:09 -0700777 spin_unlock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 bh = jh2bh(jh);
779 jbd_lock_bh_state(bh);
780 J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
781 jh->b_transaction == journal->j_running_transaction);
782
783 /*
784 * If there is undo-protected committed data against
785 * this buffer, then we can remove it now. If it is a
786 * buffer needing such protection, the old frozen_data
787 * field now points to a committed version of the
788 * buffer, so rotate that field to the new committed
789 * data.
790 *
791 * Otherwise, we can just throw away the frozen data now.
792 */
793 if (jh->b_committed_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400794 jbd_free(jh->b_committed_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 jh->b_committed_data = NULL;
796 if (jh->b_frozen_data) {
797 jh->b_committed_data = jh->b_frozen_data;
798 jh->b_frozen_data = NULL;
799 }
800 } else if (jh->b_frozen_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400801 jbd_free(jh->b_frozen_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 jh->b_frozen_data = NULL;
803 }
804
805 spin_lock(&journal->j_list_lock);
806 cp_transaction = jh->b_cp_transaction;
807 if (cp_transaction) {
808 JBUFFER_TRACE(jh, "remove from old cp transaction");
809 __journal_remove_checkpoint(jh);
810 }
811
812 /* Only re-checkpoint the buffer_head if it is marked
813 * dirty. If the buffer was added to the BJ_Forget list
814 * by journal_forget, it may no longer be dirty and
815 * there's no point in keeping a checkpoint record for
816 * it. */
817
818 /* A buffer which has been freed while still being
819 * journaled by a previous transaction may end up still
820 * being dirty here, but we want to avoid writing back
821 * that buffer in the future now that the last use has
822 * been committed. That's not only a performance gain,
823 * it also stops aliasing problems if the buffer is left
824 * behind for writeback and gets reallocated for another
825 * use in a different page. */
826 if (buffer_freed(bh)) {
827 clear_buffer_freed(bh);
828 clear_buffer_jbddirty(bh);
829 }
830
831 if (buffer_jbddirty(bh)) {
832 JBUFFER_TRACE(jh, "add to new checkpointing trans");
833 __journal_insert_checkpoint(jh, commit_transaction);
834 JBUFFER_TRACE(jh, "refile for checkpoint writeback");
835 __journal_refile_buffer(jh);
836 jbd_unlock_bh_state(bh);
837 } else {
838 J_ASSERT_BH(bh, !buffer_dirty(bh));
Jan Kara9ada7342006-06-23 02:05:25 -0700839 /* The buffer on BJ_Forget list and not jbddirty means
840 * it has been freed by this transaction and hence it
841 * could not have been reallocated until this
842 * transaction has committed. *BUT* it could be
843 * reallocated once we have written all the data to
844 * disk and before we process the buffer on BJ_Forget
845 * list. */
846 JBUFFER_TRACE(jh, "refile or unfile freed buffer");
847 __journal_refile_buffer(jh);
848 if (!jh->b_transaction) {
849 jbd_unlock_bh_state(bh);
850 /* needs a brelse */
851 journal_remove_journal_head(bh);
852 release_buffer_page(bh);
853 } else
854 jbd_unlock_bh_state(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Jan Karae6c9f5c2005-09-06 15:19:09 -0700856 cond_resched_lock(&journal->j_list_lock);
857 }
858 spin_unlock(&journal->j_list_lock);
859 /*
Jan Karad4beaf42007-12-04 23:45:27 -0800860 * This is a bit sleazy. We use j_list_lock to protect transition
861 * of a transaction into T_FINISHED state and calling
862 * __journal_drop_transaction(). Otherwise we could race with
863 * other checkpointing code processing the transaction...
Jan Karae6c9f5c2005-09-06 15:19:09 -0700864 */
865 spin_lock(&journal->j_state_lock);
866 spin_lock(&journal->j_list_lock);
867 /*
868 * Now recheck if some buffers did not get attached to the transaction
869 * while the lock was dropped...
870 */
871 if (commit_transaction->t_forget) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 spin_unlock(&journal->j_list_lock);
Jan Karae6c9f5c2005-09-06 15:19:09 -0700873 spin_unlock(&journal->j_state_lock);
874 goto restart_loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
877 /* Done with this transaction! */
878
879 jbd_debug(3, "JBD: commit phase 8\n");
880
881 J_ASSERT(commit_transaction->t_state == T_COMMIT);
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 commit_transaction->t_state = T_FINISHED;
884 J_ASSERT(commit_transaction == journal->j_committing_transaction);
885 journal->j_commit_sequence = commit_transaction->t_tid;
886 journal->j_committing_transaction = NULL;
887 spin_unlock(&journal->j_state_lock);
888
Jan Karafe28e422007-07-15 23:37:18 -0700889 if (commit_transaction->t_checkpoint_list == NULL &&
890 commit_transaction->t_checkpoint_io_list == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 __journal_drop_transaction(journal, commit_transaction);
892 } else {
893 if (journal->j_checkpoint_transactions == NULL) {
894 journal->j_checkpoint_transactions = commit_transaction;
895 commit_transaction->t_cpnext = commit_transaction;
896 commit_transaction->t_cpprev = commit_transaction;
897 } else {
898 commit_transaction->t_cpnext =
899 journal->j_checkpoint_transactions;
900 commit_transaction->t_cpprev =
901 commit_transaction->t_cpnext->t_cpprev;
902 commit_transaction->t_cpnext->t_cpprev =
903 commit_transaction;
904 commit_transaction->t_cpprev->t_cpnext =
905 commit_transaction;
906 }
907 }
908 spin_unlock(&journal->j_list_lock);
909
910 jbd_debug(1, "JBD: commit %d complete, head %d\n",
911 journal->j_commit_sequence, journal->j_tail_sequence);
912
913 wake_up(&journal->j_wait_done_commit);
914}