blob: 92b6ac3df8ababdd3ee55fa7489ecbca73a952b3 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002 * linux/fs/jbd2/commit.c
Dave Kleikamp470decc2006-10-11 01:20:57 -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>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070018#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070019#include <linux/errno.h>
20#include <linux/slab.h>
21#include <linux/mm.h>
22#include <linux/pagemap.h>
Johann Lombardi8e85fb32008-01-28 23:58:27 -050023#include <linux/jiffies.h>
Girish Shilamkar818d2762008-01-28 23:58:27 -050024#include <linux/crc32.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -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
41 * not sucessfully freed, because they are attached to a committing transaction.
42 * 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 *
50 * Called under lock_journal(), and possibly under journal_datalist_lock. The
51 * caller provided us with a ref against the buffer, and we drop that here.
52 */
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 */
68 if (TestSetPageLocked(page))
69 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/*
83 * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is
84 * held. For ranking reasons we must trylock. If we lose, schedule away and
85 * return 0. j_list_lock is dropped in this case.
86 */
87static int inverted_lock(journal_t *journal, struct buffer_head *bh)
88{
89 if (!jbd_trylock_bh_state(bh)) {
90 spin_unlock(&journal->j_list_lock);
91 schedule();
92 return 0;
93 }
94 return 1;
95}
96
Girish Shilamkar818d2762008-01-28 23:58:27 -050097/*
98 * Done it all: now submit the commit record. We should have
Dave Kleikamp470decc2006-10-11 01:20:57 -070099 * cleaned up our previous buffers by now, so if we are in abort
100 * mode we can now just skip the rest of the journal write
101 * entirely.
102 *
103 * Returns 1 if the journal needs to be aborted or 0 on success
104 */
Girish Shilamkar818d2762008-01-28 23:58:27 -0500105static int journal_submit_commit_record(journal_t *journal,
106 transaction_t *commit_transaction,
107 struct buffer_head **cbh,
108 __u32 crc32_sum)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700109{
110 struct journal_head *descriptor;
Girish Shilamkar818d2762008-01-28 23:58:27 -0500111 struct commit_header *tmp;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700112 struct buffer_head *bh;
Girish Shilamkar818d2762008-01-28 23:58:27 -0500113 int ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700114 int barrier_done = 0;
Theodore Ts'o736603a2008-07-11 19:27:31 -0400115 struct timespec now = current_kernel_time();
Dave Kleikamp470decc2006-10-11 01:20:57 -0700116
117 if (is_journal_aborted(journal))
118 return 0;
119
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700120 descriptor = jbd2_journal_get_descriptor_buffer(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700121 if (!descriptor)
122 return 1;
123
124 bh = jh2bh(descriptor);
125
Girish Shilamkar818d2762008-01-28 23:58:27 -0500126 tmp = (struct commit_header *)bh->b_data;
127 tmp->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
128 tmp->h_blocktype = cpu_to_be32(JBD2_COMMIT_BLOCK);
129 tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
Theodore Ts'o736603a2008-07-11 19:27:31 -0400130 tmp->h_commit_sec = cpu_to_be64(now.tv_sec);
131 tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500132
133 if (JBD2_HAS_COMPAT_FEATURE(journal,
134 JBD2_FEATURE_COMPAT_CHECKSUM)) {
135 tmp->h_chksum_type = JBD2_CRC32_CHKSUM;
136 tmp->h_chksum_size = JBD2_CRC32_CHKSUM_SIZE;
137 tmp->h_chksum[0] = cpu_to_be32(crc32_sum);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700138 }
139
Girish Shilamkar818d2762008-01-28 23:58:27 -0500140 JBUFFER_TRACE(descriptor, "submit commit block");
141 lock_buffer(bh);
Aneesh Kumar K.Vc4b8e632008-02-05 10:55:26 -0500142 get_bh(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700143 set_buffer_dirty(bh);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500144 set_buffer_uptodate(bh);
145 bh->b_end_io = journal_end_buffer_io_sync;
146
147 if (journal->j_flags & JBD2_BARRIER &&
Aneesh Kumar K.V4d605172008-02-05 10:56:15 -0500148 !JBD2_HAS_INCOMPAT_FEATURE(journal,
Girish Shilamkar818d2762008-01-28 23:58:27 -0500149 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150 set_buffer_ordered(bh);
151 barrier_done = 1;
152 }
Girish Shilamkar818d2762008-01-28 23:58:27 -0500153 ret = submit_bh(WRITE, bh);
Dave Kleikampc4e35e072008-02-10 01:09:32 -0500154 if (barrier_done)
155 clear_buffer_ordered(bh);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500156
Dave Kleikamp470decc2006-10-11 01:20:57 -0700157 /* is it possible for another commit to fail at roughly
158 * the same time as this one? If so, we don't want to
159 * trust the barrier flag in the super, but instead want
160 * to remember if we sent a barrier request
161 */
162 if (ret == -EOPNOTSUPP && barrier_done) {
163 char b[BDEVNAME_SIZE];
164
165 printk(KERN_WARNING
166 "JBD: barrier-based sync failed on %s - "
167 "disabling barriers\n",
168 bdevname(journal->j_dev, b));
169 spin_lock(&journal->j_state_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700170 journal->j_flags &= ~JBD2_BARRIER;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700171 spin_unlock(&journal->j_state_lock);
172
173 /* And try again, without the barrier */
Theodore Ts'o034772b2008-06-03 22:31:11 -0400174 lock_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700175 set_buffer_uptodate(bh);
176 set_buffer_dirty(bh);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500177 ret = submit_bh(WRITE, bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700178 }
Girish Shilamkar818d2762008-01-28 23:58:27 -0500179 *cbh = bh;
180 return ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700181}
182
Girish Shilamkar818d2762008-01-28 23:58:27 -0500183/*
184 * This function along with journal_submit_commit_record
185 * allows to write the commit record asynchronously.
186 */
187static int journal_wait_on_commit_record(struct buffer_head *bh)
188{
189 int ret = 0;
190
191 clear_buffer_dirty(bh);
192 wait_on_buffer(bh);
193
194 if (unlikely(!buffer_uptodate(bh)))
195 ret = -EIO;
196 put_bh(bh); /* One for getblk() */
197 jbd2_journal_put_journal_head(bh2jh(bh));
198
199 return ret;
200}
201
202/*
203 * Wait for all submitted IO to complete.
204 */
205static int journal_wait_on_locked_list(journal_t *journal,
206 transaction_t *commit_transaction)
207{
208 int ret = 0;
209 struct journal_head *jh;
210
211 while (commit_transaction->t_locked_list) {
212 struct buffer_head *bh;
213
214 jh = commit_transaction->t_locked_list->b_tprev;
215 bh = jh2bh(jh);
216 get_bh(bh);
217 if (buffer_locked(bh)) {
218 spin_unlock(&journal->j_list_lock);
219 wait_on_buffer(bh);
220 if (unlikely(!buffer_uptodate(bh)))
221 ret = -EIO;
222 spin_lock(&journal->j_list_lock);
223 }
224 if (!inverted_lock(journal, bh)) {
225 put_bh(bh);
226 spin_lock(&journal->j_list_lock);
227 continue;
228 }
229 if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
230 __jbd2_journal_unfile_buffer(jh);
231 jbd_unlock_bh_state(bh);
232 jbd2_journal_remove_journal_head(bh);
233 put_bh(bh);
234 } else {
235 jbd_unlock_bh_state(bh);
236 }
237 put_bh(bh);
238 cond_resched_lock(&journal->j_list_lock);
239 }
240 return ret;
241 }
242
Dave Kleikamp470decc2006-10-11 01:20:57 -0700243static void journal_do_submit_data(struct buffer_head **wbuf, int bufs)
244{
245 int i;
246
247 for (i = 0; i < bufs; i++) {
248 wbuf[i]->b_end_io = end_buffer_write_sync;
249 /* We use-up our safety reference in submit_bh() */
250 submit_bh(WRITE, wbuf[i]);
251 }
252}
253
254/*
255 * Submit all the data buffers to disk
256 */
257static void journal_submit_data_buffers(journal_t *journal,
258 transaction_t *commit_transaction)
259{
260 struct journal_head *jh;
261 struct buffer_head *bh;
262 int locked;
263 int bufs = 0;
264 struct buffer_head **wbuf = journal->j_wbuf;
265
266 /*
267 * Whenever we unlock the journal and sleep, things can get added
268 * onto ->t_sync_datalist, so we have to keep looping back to
269 * write_out_data until we *know* that the list is empty.
270 *
271 * Cleanup any flushed data buffers from the data list. Even in
272 * abort mode, we want to flush this out as soon as possible.
273 */
274write_out_data:
275 cond_resched();
276 spin_lock(&journal->j_list_lock);
277
278 while (commit_transaction->t_sync_datalist) {
279 jh = commit_transaction->t_sync_datalist;
280 bh = jh2bh(jh);
281 locked = 0;
282
283 /* Get reference just to make sure buffer does not disappear
284 * when we are forced to drop various locks */
285 get_bh(bh);
286 /* If the buffer is dirty, we need to submit IO and hence
287 * we need the buffer lock. We try to lock the buffer without
288 * blocking. If we fail, we need to drop j_list_lock and do
289 * blocking lock_buffer().
290 */
291 if (buffer_dirty(bh)) {
292 if (test_set_buffer_locked(bh)) {
293 BUFFER_TRACE(bh, "needs blocking lock");
294 spin_unlock(&journal->j_list_lock);
295 /* Write out all data to prevent deadlocks */
296 journal_do_submit_data(wbuf, bufs);
297 bufs = 0;
298 lock_buffer(bh);
299 spin_lock(&journal->j_list_lock);
300 }
301 locked = 1;
302 }
303 /* We have to get bh_state lock. Again out of order, sigh. */
304 if (!inverted_lock(journal, bh)) {
305 jbd_lock_bh_state(bh);
306 spin_lock(&journal->j_list_lock);
307 }
308 /* Someone already cleaned up the buffer? */
309 if (!buffer_jbd(bh)
310 || jh->b_transaction != commit_transaction
311 || jh->b_jlist != BJ_SyncData) {
312 jbd_unlock_bh_state(bh);
313 if (locked)
314 unlock_buffer(bh);
315 BUFFER_TRACE(bh, "already cleaned up");
316 put_bh(bh);
317 continue;
318 }
319 if (locked && test_clear_buffer_dirty(bh)) {
320 BUFFER_TRACE(bh, "needs writeout, adding to array");
321 wbuf[bufs++] = bh;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700322 __jbd2_journal_file_buffer(jh, commit_transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700323 BJ_Locked);
324 jbd_unlock_bh_state(bh);
325 if (bufs == journal->j_wbufsize) {
326 spin_unlock(&journal->j_list_lock);
327 journal_do_submit_data(wbuf, bufs);
328 bufs = 0;
329 goto write_out_data;
330 }
Hisashi Hifumi12603922006-12-06 20:39:17 -0800331 } else if (!locked && buffer_locked(bh)) {
332 __jbd2_journal_file_buffer(jh, commit_transaction,
333 BJ_Locked);
334 jbd_unlock_bh_state(bh);
335 put_bh(bh);
336 } else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700337 BUFFER_TRACE(bh, "writeout complete: unfile");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700338 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700339 jbd_unlock_bh_state(bh);
340 if (locked)
341 unlock_buffer(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700342 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700343 /* Once for our safety reference, once for
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700344 * jbd2_journal_remove_journal_head() */
Dave Kleikamp470decc2006-10-11 01:20:57 -0700345 put_bh(bh);
346 put_bh(bh);
347 }
348
Nick Piggin95c354f2008-01-30 13:31:20 +0100349 if (need_resched() || spin_needbreak(&journal->j_list_lock)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700350 spin_unlock(&journal->j_list_lock);
351 goto write_out_data;
352 }
353 }
354 spin_unlock(&journal->j_list_lock);
355 journal_do_submit_data(wbuf, bufs);
356}
357
Girish Shilamkar818d2762008-01-28 23:58:27 -0500358static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
359{
360 struct page *page = bh->b_page;
361 char *addr;
362 __u32 checksum;
363
364 addr = kmap_atomic(page, KM_USER0);
365 checksum = crc32_be(crc32_sum,
366 (void *)(addr + offset_in_page(bh->b_data)), bh->b_size);
367 kunmap_atomic(addr, KM_USER0);
368
369 return checksum;
370}
371
372static void write_tag_block(int tag_bytes, journal_block_tag_t *tag,
Mingming Cao18eba7a2006-10-11 01:21:13 -0700373 unsigned long long block)
Zach Brownb517bea2006-10-11 01:21:08 -0700374{
375 tag->t_blocknr = cpu_to_be32(block & (u32)~0);
Mingming Caocd02ff02007-10-16 18:38:25 -0400376 if (tag_bytes > JBD2_TAG_SIZE32)
Zach Brownb517bea2006-10-11 01:21:08 -0700377 tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1);
378}
379
Dave Kleikamp470decc2006-10-11 01:20:57 -0700380/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700381 * jbd2_journal_commit_transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -0700382 *
383 * The primary function for committing a transaction to the log. This
384 * function is called by the journal thread to begin a complete commit.
385 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700386void jbd2_journal_commit_transaction(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700387{
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500388 struct transaction_stats_s stats;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700389 transaction_t *commit_transaction;
390 struct journal_head *jh, *new_jh, *descriptor;
391 struct buffer_head **wbuf = journal->j_wbuf;
392 int bufs;
393 int flags;
394 int err;
Mingming Cao18eba7a2006-10-11 01:21:13 -0700395 unsigned long long blocknr;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700396 char *tagp = NULL;
397 journal_header_t *header;
398 journal_block_tag_t *tag = NULL;
399 int space_left = 0;
400 int first_tag = 0;
401 int tag_flag;
402 int i;
Zach Brownb517bea2006-10-11 01:21:08 -0700403 int tag_bytes = journal_tag_bytes(journal);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500404 struct buffer_head *cbh = NULL; /* For transactional checksums */
405 __u32 crc32_sum = ~0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700406
407 /*
408 * First job: lock down the current transaction and wait for
409 * all outstanding updates to complete.
410 */
411
412#ifdef COMMIT_STATS
413 spin_lock(&journal->j_list_lock);
414 summarise_journal_usage(journal);
415 spin_unlock(&journal->j_list_lock);
416#endif
417
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700418 /* Do we need to erase the effects of a prior jbd2_journal_flush? */
419 if (journal->j_flags & JBD2_FLUSHED) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700420 jbd_debug(3, "super block updated\n");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700421 jbd2_journal_update_superblock(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700422 } else {
423 jbd_debug(3, "superblock not updated\n");
424 }
425
426 J_ASSERT(journal->j_running_transaction != NULL);
427 J_ASSERT(journal->j_committing_transaction == NULL);
428
429 commit_transaction = journal->j_running_transaction;
430 J_ASSERT(commit_transaction->t_state == T_RUNNING);
431
432 jbd_debug(1, "JBD: starting commit of transaction %d\n",
433 commit_transaction->t_tid);
434
435 spin_lock(&journal->j_state_lock);
436 commit_transaction->t_state = T_LOCKED;
437
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500438 stats.u.run.rs_wait = commit_transaction->t_max_wait;
439 stats.u.run.rs_locked = jiffies;
440 stats.u.run.rs_running = jbd2_time_diff(commit_transaction->t_start,
441 stats.u.run.rs_locked);
442
Dave Kleikamp470decc2006-10-11 01:20:57 -0700443 spin_lock(&commit_transaction->t_handle_lock);
444 while (commit_transaction->t_updates) {
445 DEFINE_WAIT(wait);
446
447 prepare_to_wait(&journal->j_wait_updates, &wait,
448 TASK_UNINTERRUPTIBLE);
449 if (commit_transaction->t_updates) {
450 spin_unlock(&commit_transaction->t_handle_lock);
451 spin_unlock(&journal->j_state_lock);
452 schedule();
453 spin_lock(&journal->j_state_lock);
454 spin_lock(&commit_transaction->t_handle_lock);
455 }
456 finish_wait(&journal->j_wait_updates, &wait);
457 }
458 spin_unlock(&commit_transaction->t_handle_lock);
459
460 J_ASSERT (commit_transaction->t_outstanding_credits <=
461 journal->j_max_transaction_buffers);
462
463 /*
464 * First thing we are allowed to do is to discard any remaining
465 * BJ_Reserved buffers. Note, it is _not_ permissible to assume
466 * that there are no such buffers: if a large filesystem
467 * operation like a truncate needs to split itself over multiple
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700468 * transactions, then it may try to do a jbd2_journal_restart() while
Dave Kleikamp470decc2006-10-11 01:20:57 -0700469 * there are still BJ_Reserved buffers outstanding. These must
470 * be released cleanly from the current transaction.
471 *
472 * In this case, the filesystem must still reserve write access
473 * again before modifying the buffer in the new transaction, but
474 * we do not require it to remember exactly which old buffers it
475 * has reserved. This is consistent with the existing behaviour
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700476 * that multiple jbd2_journal_get_write_access() calls to the same
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477 * buffer are perfectly permissable.
478 */
479 while (commit_transaction->t_reserved_list) {
480 jh = commit_transaction->t_reserved_list;
481 JBUFFER_TRACE(jh, "reserved, unused: refile");
482 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700483 * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may
Dave Kleikamp470decc2006-10-11 01:20:57 -0700484 * leave undo-committed data.
485 */
486 if (jh->b_committed_data) {
487 struct buffer_head *bh = jh2bh(jh);
488
489 jbd_lock_bh_state(bh);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400490 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700491 jh->b_committed_data = NULL;
492 jbd_unlock_bh_state(bh);
493 }
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700494 jbd2_journal_refile_buffer(journal, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700495 }
496
497 /*
498 * Now try to drop any written-back buffers from the journal's
499 * checkpoint lists. We do this *before* commit because it potentially
500 * frees some memory
501 */
502 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700503 __jbd2_journal_clean_checkpoint_list(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504 spin_unlock(&journal->j_list_lock);
505
506 jbd_debug (3, "JBD: commit phase 1\n");
507
508 /*
509 * Switch to a new revoke table.
510 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700511 jbd2_journal_switch_revoke_table(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700512
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500513 stats.u.run.rs_flushing = jiffies;
514 stats.u.run.rs_locked = jbd2_time_diff(stats.u.run.rs_locked,
515 stats.u.run.rs_flushing);
516
Dave Kleikamp470decc2006-10-11 01:20:57 -0700517 commit_transaction->t_state = T_FLUSH;
518 journal->j_committing_transaction = commit_transaction;
519 journal->j_running_transaction = NULL;
520 commit_transaction->t_log_start = journal->j_head;
521 wake_up(&journal->j_wait_transaction_locked);
522 spin_unlock(&journal->j_state_lock);
523
524 jbd_debug (3, "JBD: commit phase 2\n");
525
526 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 * Now start flushing things to disk, in the order they appear
528 * on the transaction lists. Data blocks go first.
529 */
530 err = 0;
531 journal_submit_data_buffers(journal, commit_transaction);
532
533 /*
Girish Shilamkar818d2762008-01-28 23:58:27 -0500534 * Wait for all previously submitted IO to complete if commit
535 * record is to be written synchronously.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 */
537 spin_lock(&journal->j_list_lock);
Girish Shilamkar818d2762008-01-28 23:58:27 -0500538 if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
539 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT))
540 err = journal_wait_on_locked_list(journal,
541 commit_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542
Dave Kleikamp470decc2006-10-11 01:20:57 -0700543 spin_unlock(&journal->j_list_lock);
544
545 if (err)
Jan Karaa7fa2ba2007-10-16 18:38:25 -0400546 jbd2_journal_abort(journal, err);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700547
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700548 jbd2_journal_write_revoke_records(journal, commit_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700549
550 jbd_debug(3, "JBD: commit phase 2\n");
551
552 /*
553 * If we found any dirty or locked buffers, then we should have
554 * looped back up to the write_out_data label. If there weren't
555 * any then journal_clean_data_list should have wiped the list
556 * clean by now, so check that it is in fact empty.
557 */
558 J_ASSERT (commit_transaction->t_sync_datalist == NULL);
559
560 jbd_debug (3, "JBD: commit phase 3\n");
561
562 /*
563 * Way to go: we have now written out all of the data for a
564 * transaction! Now comes the tricky part: we need to write out
565 * metadata. Loop over the transaction's entire buffer list:
566 */
Mingming Cao02c471c2008-05-15 14:46:17 -0400567 spin_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568 commit_transaction->t_state = T_COMMIT;
Mingming Cao02c471c2008-05-15 14:46:17 -0400569 spin_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700570
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500571 stats.u.run.rs_logging = jiffies;
572 stats.u.run.rs_flushing = jbd2_time_diff(stats.u.run.rs_flushing,
573 stats.u.run.rs_logging);
574 stats.u.run.rs_blocks = commit_transaction->t_outstanding_credits;
575 stats.u.run.rs_blocks_logged = 0;
576
Josef Bacik1dfc3222008-04-17 10:38:59 -0400577 J_ASSERT(commit_transaction->t_nr_buffers <=
578 commit_transaction->t_outstanding_credits);
579
Dave Kleikamp470decc2006-10-11 01:20:57 -0700580 descriptor = NULL;
581 bufs = 0;
582 while (commit_transaction->t_buffers) {
583
584 /* Find the next buffer to be journaled... */
585
586 jh = commit_transaction->t_buffers;
587
588 /* If we're in abort mode, we just un-journal the buffer and
589 release it for background writing. */
590
591 if (is_journal_aborted(journal)) {
592 JBUFFER_TRACE(jh, "journal is aborting: refile");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700593 jbd2_journal_refile_buffer(journal, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700594 /* If that was the last one, we need to clean up
595 * any descriptor buffers which may have been
596 * already allocated, even if we are now
597 * aborting. */
598 if (!commit_transaction->t_buffers)
599 goto start_journal_io;
600 continue;
601 }
602
603 /* Make sure we have a descriptor block in which to
604 record the metadata buffer. */
605
606 if (!descriptor) {
607 struct buffer_head *bh;
608
609 J_ASSERT (bufs == 0);
610
611 jbd_debug(4, "JBD: get descriptor\n");
612
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700613 descriptor = jbd2_journal_get_descriptor_buffer(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 if (!descriptor) {
Jan Karaa7fa2ba2007-10-16 18:38:25 -0400615 jbd2_journal_abort(journal, -EIO);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700616 continue;
617 }
618
619 bh = jh2bh(descriptor);
620 jbd_debug(4, "JBD: got buffer %llu (%p)\n",
621 (unsigned long long)bh->b_blocknr, bh->b_data);
622 header = (journal_header_t *)&bh->b_data[0];
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700623 header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
624 header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700625 header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
626
627 tagp = &bh->b_data[sizeof(journal_header_t)];
628 space_left = bh->b_size - sizeof(journal_header_t);
629 first_tag = 1;
630 set_buffer_jwrite(bh);
631 set_buffer_dirty(bh);
632 wbuf[bufs++] = bh;
633
634 /* Record it so that we can wait for IO
635 completion later */
636 BUFFER_TRACE(bh, "ph3: file as descriptor");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700637 jbd2_journal_file_buffer(descriptor, commit_transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700638 BJ_LogCtl);
639 }
640
641 /* Where is the buffer to be written? */
642
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700643 err = jbd2_journal_next_log_block(journal, &blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700644 /* If the block mapping failed, just abandon the buffer
645 and repeat this loop: we'll fall into the
646 refile-on-abort condition above. */
647 if (err) {
Jan Karaa7fa2ba2007-10-16 18:38:25 -0400648 jbd2_journal_abort(journal, err);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700649 continue;
650 }
651
652 /*
653 * start_this_handle() uses t_outstanding_credits to determine
654 * the free space in the log, but this counter is changed
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700655 * by jbd2_journal_next_log_block() also.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700656 */
657 commit_transaction->t_outstanding_credits--;
658
659 /* Bump b_count to prevent truncate from stumbling over
660 the shadowed buffer! @@@ This can go if we ever get
661 rid of the BJ_IO/BJ_Shadow pairing of buffers. */
662 atomic_inc(&jh2bh(jh)->b_count);
663
664 /* Make a temporary IO buffer with which to write it out
665 (this will requeue both the metadata buffer and the
666 temporary IO buffer). new_bh goes on BJ_IO*/
667
668 set_bit(BH_JWrite, &jh2bh(jh)->b_state);
669 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700670 * akpm: jbd2_journal_write_metadata_buffer() sets
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 * new_bh->b_transaction to commit_transaction.
672 * We need to clean this up before we release new_bh
673 * (which is of type BJ_IO)
674 */
675 JBUFFER_TRACE(jh, "ph3: write metadata");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700676 flags = jbd2_journal_write_metadata_buffer(commit_transaction,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700677 jh, &new_jh, blocknr);
678 set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
679 wbuf[bufs++] = jh2bh(new_jh);
680
681 /* Record the new block's tag in the current descriptor
682 buffer */
683
684 tag_flag = 0;
685 if (flags & 1)
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700686 tag_flag |= JBD2_FLAG_ESCAPE;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700687 if (!first_tag)
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700688 tag_flag |= JBD2_FLAG_SAME_UUID;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700689
690 tag = (journal_block_tag_t *) tagp;
Zach Brownb517bea2006-10-11 01:21:08 -0700691 write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700692 tag->t_flags = cpu_to_be32(tag_flag);
Zach Brownb517bea2006-10-11 01:21:08 -0700693 tagp += tag_bytes;
694 space_left -= tag_bytes;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700695
696 if (first_tag) {
697 memcpy (tagp, journal->j_uuid, 16);
698 tagp += 16;
699 space_left -= 16;
700 first_tag = 0;
701 }
702
703 /* If there's no more to do, or if the descriptor is full,
704 let the IO rip! */
705
706 if (bufs == journal->j_wbufsize ||
707 commit_transaction->t_buffers == NULL ||
Zach Brownb517bea2006-10-11 01:21:08 -0700708 space_left < tag_bytes + 16) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700709
710 jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
711
712 /* Write an end-of-descriptor marker before
713 submitting the IOs. "tag" still points to
714 the last tag we set up. */
715
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700716 tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700717
718start_journal_io:
719 for (i = 0; i < bufs; i++) {
720 struct buffer_head *bh = wbuf[i];
Girish Shilamkar818d2762008-01-28 23:58:27 -0500721 /*
722 * Compute checksum.
723 */
724 if (JBD2_HAS_COMPAT_FEATURE(journal,
725 JBD2_FEATURE_COMPAT_CHECKSUM)) {
726 crc32_sum =
727 jbd2_checksum_data(crc32_sum, bh);
728 }
729
Dave Kleikamp470decc2006-10-11 01:20:57 -0700730 lock_buffer(bh);
731 clear_buffer_dirty(bh);
732 set_buffer_uptodate(bh);
733 bh->b_end_io = journal_end_buffer_io_sync;
734 submit_bh(WRITE, bh);
735 }
736 cond_resched();
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500737 stats.u.run.rs_blocks_logged += bufs;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700738
739 /* Force a new descriptor to be generated next
740 time round the loop. */
741 descriptor = NULL;
742 bufs = 0;
743 }
744 }
745
Girish Shilamkar818d2762008-01-28 23:58:27 -0500746 /* Done it all: now write the commit record asynchronously. */
747
748 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
749 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
750 err = journal_submit_commit_record(journal, commit_transaction,
751 &cbh, crc32_sum);
752 if (err)
753 __jbd2_journal_abort_hard(journal);
754
755 spin_lock(&journal->j_list_lock);
756 err = journal_wait_on_locked_list(journal,
757 commit_transaction);
758 spin_unlock(&journal->j_list_lock);
759 if (err)
760 __jbd2_journal_abort_hard(journal);
761 }
762
Dave Kleikamp470decc2006-10-11 01:20:57 -0700763 /* Lo and behold: we have just managed to send a transaction to
764 the log. Before we can commit it, wait for the IO so far to
765 complete. Control buffers being written are on the
766 transaction's t_log_list queue, and metadata buffers are on
767 the t_iobuf_list queue.
768
769 Wait for the buffers in reverse order. That way we are
770 less likely to be woken up until all IOs have completed, and
771 so we incur less scheduling load.
772 */
773
774 jbd_debug(3, "JBD: commit phase 4\n");
775
776 /*
777 * akpm: these are BJ_IO, and j_list_lock is not needed.
778 * See __journal_try_to_free_buffer.
779 */
780wait_for_iobuf:
781 while (commit_transaction->t_iobuf_list != NULL) {
782 struct buffer_head *bh;
783
784 jh = commit_transaction->t_iobuf_list->b_tprev;
785 bh = jh2bh(jh);
786 if (buffer_locked(bh)) {
787 wait_on_buffer(bh);
788 goto wait_for_iobuf;
789 }
790 if (cond_resched())
791 goto wait_for_iobuf;
792
793 if (unlikely(!buffer_uptodate(bh)))
794 err = -EIO;
795
796 clear_buffer_jwrite(bh);
797
798 JBUFFER_TRACE(jh, "ph4: unfile after journal write");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700799 jbd2_journal_unfile_buffer(journal, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700800
801 /*
802 * ->t_iobuf_list should contain only dummy buffer_heads
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700803 * which were created by jbd2_journal_write_metadata_buffer().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700804 */
805 BUFFER_TRACE(bh, "dumping temporary bh");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700806 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700807 __brelse(bh);
808 J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
809 free_buffer_head(bh);
810
811 /* We also have to unlock and free the corresponding
812 shadowed buffer */
813 jh = commit_transaction->t_shadow_list->b_tprev;
814 bh = jh2bh(jh);
815 clear_bit(BH_JWrite, &bh->b_state);
816 J_ASSERT_BH(bh, buffer_jbddirty(bh));
817
818 /* The metadata is now released for reuse, but we need
819 to remember it against this transaction so that when
820 we finally commit, we can do any checkpointing
821 required. */
822 JBUFFER_TRACE(jh, "file as BJ_Forget");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700823 jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700824 /* Wake up any transactions which were waiting for this
825 IO to complete */
826 wake_up_bit(&bh->b_state, BH_Unshadow);
827 JBUFFER_TRACE(jh, "brelse shadowed buffer");
828 __brelse(bh);
829 }
830
831 J_ASSERT (commit_transaction->t_shadow_list == NULL);
832
833 jbd_debug(3, "JBD: commit phase 5\n");
834
835 /* Here we wait for the revoke record and descriptor record buffers */
836 wait_for_ctlbuf:
837 while (commit_transaction->t_log_list != NULL) {
838 struct buffer_head *bh;
839
840 jh = commit_transaction->t_log_list->b_tprev;
841 bh = jh2bh(jh);
842 if (buffer_locked(bh)) {
843 wait_on_buffer(bh);
844 goto wait_for_ctlbuf;
845 }
846 if (cond_resched())
847 goto wait_for_ctlbuf;
848
849 if (unlikely(!buffer_uptodate(bh)))
850 err = -EIO;
851
852 BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
853 clear_buffer_jwrite(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700854 jbd2_journal_unfile_buffer(journal, jh);
855 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700856 __brelse(bh); /* One for getblk */
857 /* AKPM: bforget here */
858 }
859
860 jbd_debug(3, "JBD: commit phase 6\n");
861
Girish Shilamkar818d2762008-01-28 23:58:27 -0500862 if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
863 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
864 err = journal_submit_commit_record(journal, commit_transaction,
865 &cbh, crc32_sum);
866 if (err)
867 __jbd2_journal_abort_hard(journal);
868 }
Mingming Caob048d842008-02-05 08:52:45 -0500869 if (!err && !is_journal_aborted(journal))
870 err = journal_wait_on_commit_record(cbh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700871
872 if (err)
Jan Karaa7fa2ba2007-10-16 18:38:25 -0400873 jbd2_journal_abort(journal, err);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700874
875 /* End of a transaction! Finally, we can do checkpoint
876 processing: any buffers committed as a result of this
877 transaction can be removed from any checkpoint list it was on
878 before. */
879
880 jbd_debug(3, "JBD: commit phase 7\n");
881
882 J_ASSERT(commit_transaction->t_sync_datalist == NULL);
883 J_ASSERT(commit_transaction->t_buffers == NULL);
884 J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
885 J_ASSERT(commit_transaction->t_iobuf_list == NULL);
886 J_ASSERT(commit_transaction->t_shadow_list == NULL);
887 J_ASSERT(commit_transaction->t_log_list == NULL);
888
889restart_loop:
890 /*
891 * As there are other places (journal_unmap_buffer()) adding buffers
892 * to this list we have to be careful and hold the j_list_lock.
893 */
894 spin_lock(&journal->j_list_lock);
895 while (commit_transaction->t_forget) {
896 transaction_t *cp_transaction;
897 struct buffer_head *bh;
898
899 jh = commit_transaction->t_forget;
900 spin_unlock(&journal->j_list_lock);
901 bh = jh2bh(jh);
902 jbd_lock_bh_state(bh);
903 J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
904 jh->b_transaction == journal->j_running_transaction);
905
906 /*
907 * If there is undo-protected committed data against
908 * this buffer, then we can remove it now. If it is a
909 * buffer needing such protection, the old frozen_data
910 * field now points to a committed version of the
911 * buffer, so rotate that field to the new committed
912 * data.
913 *
914 * Otherwise, we can just throw away the frozen data now.
915 */
916 if (jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400917 jbd2_free(jh->b_committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700918 jh->b_committed_data = NULL;
919 if (jh->b_frozen_data) {
920 jh->b_committed_data = jh->b_frozen_data;
921 jh->b_frozen_data = NULL;
922 }
923 } else if (jh->b_frozen_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400924 jbd2_free(jh->b_frozen_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700925 jh->b_frozen_data = NULL;
926 }
927
928 spin_lock(&journal->j_list_lock);
929 cp_transaction = jh->b_cp_transaction;
930 if (cp_transaction) {
931 JBUFFER_TRACE(jh, "remove from old cp transaction");
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500932 cp_transaction->t_chp_stats.cs_dropped++;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700933 __jbd2_journal_remove_checkpoint(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700934 }
935
936 /* Only re-checkpoint the buffer_head if it is marked
937 * dirty. If the buffer was added to the BJ_Forget list
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700938 * by jbd2_journal_forget, it may no longer be dirty and
Dave Kleikamp470decc2006-10-11 01:20:57 -0700939 * there's no point in keeping a checkpoint record for
940 * it. */
941
942 /* A buffer which has been freed while still being
943 * journaled by a previous transaction may end up still
944 * being dirty here, but we want to avoid writing back
945 * that buffer in the future now that the last use has
946 * been committed. That's not only a performance gain,
947 * it also stops aliasing problems if the buffer is left
948 * behind for writeback and gets reallocated for another
949 * use in a different page. */
950 if (buffer_freed(bh)) {
951 clear_buffer_freed(bh);
952 clear_buffer_jbddirty(bh);
953 }
954
955 if (buffer_jbddirty(bh)) {
956 JBUFFER_TRACE(jh, "add to new checkpointing trans");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700957 __jbd2_journal_insert_checkpoint(jh, commit_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700958 JBUFFER_TRACE(jh, "refile for checkpoint writeback");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700959 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700960 jbd_unlock_bh_state(bh);
961 } else {
962 J_ASSERT_BH(bh, !buffer_dirty(bh));
963 /* The buffer on BJ_Forget list and not jbddirty means
964 * it has been freed by this transaction and hence it
965 * could not have been reallocated until this
966 * transaction has committed. *BUT* it could be
967 * reallocated once we have written all the data to
968 * disk and before we process the buffer on BJ_Forget
969 * list. */
970 JBUFFER_TRACE(jh, "refile or unfile freed buffer");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700971 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700972 if (!jh->b_transaction) {
973 jbd_unlock_bh_state(bh);
974 /* needs a brelse */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700975 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700976 release_buffer_page(bh);
977 } else
978 jbd_unlock_bh_state(bh);
979 }
980 cond_resched_lock(&journal->j_list_lock);
981 }
982 spin_unlock(&journal->j_list_lock);
983 /*
Jan Karaf5a7a6b2008-01-28 23:58:27 -0500984 * This is a bit sleazy. We use j_list_lock to protect transition
985 * of a transaction into T_FINISHED state and calling
986 * __jbd2_journal_drop_transaction(). Otherwise we could race with
987 * other checkpointing code processing the transaction...
Dave Kleikamp470decc2006-10-11 01:20:57 -0700988 */
989 spin_lock(&journal->j_state_lock);
990 spin_lock(&journal->j_list_lock);
991 /*
992 * Now recheck if some buffers did not get attached to the transaction
993 * while the lock was dropped...
994 */
995 if (commit_transaction->t_forget) {
996 spin_unlock(&journal->j_list_lock);
997 spin_unlock(&journal->j_state_lock);
998 goto restart_loop;
999 }
1000
1001 /* Done with this transaction! */
1002
1003 jbd_debug(3, "JBD: commit phase 8\n");
1004
1005 J_ASSERT(commit_transaction->t_state == T_COMMIT);
1006
Johann Lombardi8e85fb32008-01-28 23:58:27 -05001007 commit_transaction->t_start = jiffies;
1008 stats.u.run.rs_logging = jbd2_time_diff(stats.u.run.rs_logging,
1009 commit_transaction->t_start);
1010
1011 /*
1012 * File the transaction for history
1013 */
1014 stats.ts_type = JBD2_STATS_RUN;
1015 stats.ts_tid = commit_transaction->t_tid;
1016 stats.u.run.rs_handle_count = commit_transaction->t_handle_count;
1017 spin_lock(&journal->j_history_lock);
1018 memcpy(journal->j_history + journal->j_history_cur, &stats,
1019 sizeof(stats));
1020 if (++journal->j_history_cur == journal->j_history_max)
1021 journal->j_history_cur = 0;
1022
1023 /*
1024 * Calculate overall stats
1025 */
1026 journal->j_stats.ts_tid++;
1027 journal->j_stats.u.run.rs_wait += stats.u.run.rs_wait;
1028 journal->j_stats.u.run.rs_running += stats.u.run.rs_running;
1029 journal->j_stats.u.run.rs_locked += stats.u.run.rs_locked;
1030 journal->j_stats.u.run.rs_flushing += stats.u.run.rs_flushing;
1031 journal->j_stats.u.run.rs_logging += stats.u.run.rs_logging;
1032 journal->j_stats.u.run.rs_handle_count += stats.u.run.rs_handle_count;
1033 journal->j_stats.u.run.rs_blocks += stats.u.run.rs_blocks;
1034 journal->j_stats.u.run.rs_blocks_logged += stats.u.run.rs_blocks_logged;
1035 spin_unlock(&journal->j_history_lock);
1036
Dave Kleikamp470decc2006-10-11 01:20:57 -07001037 commit_transaction->t_state = T_FINISHED;
1038 J_ASSERT(commit_transaction == journal->j_committing_transaction);
1039 journal->j_commit_sequence = commit_transaction->t_tid;
1040 journal->j_committing_transaction = NULL;
1041 spin_unlock(&journal->j_state_lock);
1042
Jan Karaf89b7792007-07-15 23:37:20 -07001043 if (commit_transaction->t_checkpoint_list == NULL &&
1044 commit_transaction->t_checkpoint_io_list == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001045 __jbd2_journal_drop_transaction(journal, commit_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 } else {
1047 if (journal->j_checkpoint_transactions == NULL) {
1048 journal->j_checkpoint_transactions = commit_transaction;
1049 commit_transaction->t_cpnext = commit_transaction;
1050 commit_transaction->t_cpprev = commit_transaction;
1051 } else {
1052 commit_transaction->t_cpnext =
1053 journal->j_checkpoint_transactions;
1054 commit_transaction->t_cpprev =
1055 commit_transaction->t_cpnext->t_cpprev;
1056 commit_transaction->t_cpnext->t_cpprev =
1057 commit_transaction;
1058 commit_transaction->t_cpprev->t_cpnext =
1059 commit_transaction;
1060 }
1061 }
1062 spin_unlock(&journal->j_list_lock);
1063
1064 jbd_debug(1, "JBD: commit %d complete, head %d\n",
1065 journal->j_commit_sequence, journal->j_tail_sequence);
1066
1067 wake_up(&journal->j_wait_done_commit);
1068}