blob: 05f0754f2b466f1446b8501331bc5b698f34f8a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02002 * linux/fs/jbd/checkpoint.c
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
5 *
6 * Copyright 1999 Red Hat Software --- 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 *
Mingming Caoae6ddcc2006-09-27 01:49:27 -070012 * Checkpoint routines for the generic filesystem journaling code.
13 * Part of the ext2fs journaling system.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * Checkpointing is the process of ensuring that a section of the log is
16 * committed fully to disk, so that that portion of the log can be
17 * reused.
18 */
19
20#include <linux/time.h>
21#include <linux/fs.h>
22#include <linux/jbd.h>
23#include <linux/errno.h>
24#include <linux/slab.h>
Tao Maa212d1a2011-06-07 11:56:50 +080025#include <linux/blkdev.h>
Lukas Czerner99cb1a32011-05-23 18:33:02 +020026#include <trace/events/jbd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
Jan Kara78ce89c2006-06-23 02:06:05 -070029 * Unlink a buffer from a transaction checkpoint list.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 * Called with j_list_lock held.
32 */
Jan Kara78ce89c2006-06-23 02:06:05 -070033static inline void __buffer_unlink_first(struct journal_head *jh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Jan Kara78ce89c2006-06-23 02:06:05 -070035 transaction_t *transaction = jh->b_cp_transaction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Mark Fasheh7c8903f2006-02-14 13:53:03 -080037 jh->b_cpnext->b_cpprev = jh->b_cpprev;
38 jh->b_cpprev->b_cpnext = jh->b_cpnext;
Jan Kara78ce89c2006-06-23 02:06:05 -070039 if (transaction->t_checkpoint_list == jh) {
Mark Fasheh7c8903f2006-02-14 13:53:03 -080040 transaction->t_checkpoint_list = jh->b_cpnext;
Jan Kara78ce89c2006-06-23 02:06:05 -070041 if (transaction->t_checkpoint_list == jh)
42 transaction->t_checkpoint_list = NULL;
43 }
44}
45
46/*
47 * Unlink a buffer from a transaction checkpoint(io) list.
48 *
49 * Called with j_list_lock held.
50 */
51static inline void __buffer_unlink(struct journal_head *jh)
52{
53 transaction_t *transaction = jh->b_cp_transaction;
54
55 __buffer_unlink_first(jh);
56 if (transaction->t_checkpoint_io_list == jh) {
57 transaction->t_checkpoint_io_list = jh->b_cpnext;
58 if (transaction->t_checkpoint_io_list == jh)
59 transaction->t_checkpoint_io_list = NULL;
60 }
61}
62
63/*
64 * Move a buffer from the checkpoint list to the checkpoint io list
65 *
66 * Called with j_list_lock held
67 */
68static inline void __buffer_relink_io(struct journal_head *jh)
69{
70 transaction_t *transaction = jh->b_cp_transaction;
71
72 __buffer_unlink_first(jh);
73
74 if (!transaction->t_checkpoint_io_list) {
75 jh->b_cpnext = jh->b_cpprev = jh;
76 } else {
77 jh->b_cpnext = transaction->t_checkpoint_io_list;
78 jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev;
79 jh->b_cpprev->b_cpnext = jh;
80 jh->b_cpnext->b_cpprev = jh;
81 }
82 transaction->t_checkpoint_io_list = jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85/*
86 * Try to release a checkpointed buffer from its transaction.
Jan Kara78ce89c2006-06-23 02:06:05 -070087 * Returns 1 if we released it and 2 if we also released the
88 * whole transaction.
89 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Requires j_list_lock
91 * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
92 */
93static int __try_to_free_cp_buf(struct journal_head *jh)
94{
95 int ret = 0;
96 struct buffer_head *bh = jh2bh(jh);
97
Hidehiro Kawai4afe9782008-10-22 14:15:00 -070098 if (jh->b_jlist == BJ_None && !buffer_locked(bh) &&
Hidehiro Kawai9f818b42008-10-22 14:15:02 -070099 !buffer_dirty(bh) && !buffer_write_io_error(bh)) {
Jan Karabb189242011-06-24 23:11:59 +0200100 /*
101 * Get our reference so that bh cannot be freed before
102 * we unlock it
103 */
104 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 JBUFFER_TRACE(jh, "remove from checkpoint list");
Jan Kara78ce89c2006-06-23 02:06:05 -0700106 ret = __journal_remove_checkpoint(jh) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 jbd_unlock_bh_state(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 BUFFER_TRACE(bh, "release");
109 __brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 } else {
111 jbd_unlock_bh_state(bh);
112 }
113 return ret;
114}
115
116/*
117 * __log_wait_for_space: wait until there is space in the journal.
118 *
119 * Called under j-state_lock *only*. It will be unlocked if we have to wait
120 * for a checkpoint to free up some space in the log.
121 */
122void __log_wait_for_space(journal_t *journal)
123{
Theodore Ts'oe219cca2008-11-06 22:37:59 -0500124 int nblocks, space_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 assert_spin_locked(&journal->j_state_lock);
126
127 nblocks = jbd_space_needed(journal);
128 while (__log_space_left(journal) < nblocks) {
129 if (journal->j_flags & JFS_ABORT)
130 return;
131 spin_unlock(&journal->j_state_lock);
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800132 mutex_lock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /*
135 * Test again, another process may have checkpointed while we
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700136 * were waiting for the checkpoint lock. If there are no
Theodore Ts'oe219cca2008-11-06 22:37:59 -0500137 * transactions ready to be checkpointed, try to recover
138 * journal space by calling cleanup_journal_tail(), and if
139 * that doesn't work, by waiting for the currently committing
140 * transaction to complete. If there is absolutely no way
141 * to make progress, this is either a BUG or corrupted
142 * filesystem, so abort the journal and leave a stack
143 * trace for forensic evidence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
145 spin_lock(&journal->j_state_lock);
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700146 spin_lock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 nblocks = jbd_space_needed(journal);
Theodore Ts'oe219cca2008-11-06 22:37:59 -0500148 space_left = __log_space_left(journal);
149 if (space_left < nblocks) {
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700150 int chkpt = journal->j_checkpoint_transactions != NULL;
Theodore Ts'oe219cca2008-11-06 22:37:59 -0500151 tid_t tid = 0;
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700152
Theodore Ts'oe219cca2008-11-06 22:37:59 -0500153 if (journal->j_committing_transaction)
154 tid = journal->j_committing_transaction->t_tid;
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700155 spin_unlock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 spin_unlock(&journal->j_state_lock);
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700157 if (chkpt) {
158 log_do_checkpoint(journal);
Theodore Ts'oe219cca2008-11-06 22:37:59 -0500159 } else if (cleanup_journal_tail(journal) == 0) {
160 /* We were able to recover space; yay! */
161 ;
162 } else if (tid) {
163 log_wait_commit(journal, tid);
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700164 } else {
Theodore Ts'oe219cca2008-11-06 22:37:59 -0500165 printk(KERN_ERR "%s: needed %d blocks and "
166 "only had %d space available\n",
167 __func__, nblocks, space_left);
168 printk(KERN_ERR "%s: no way to get more "
169 "journal space\n", __func__);
170 WARN_ON(1);
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700171 journal_abort(journal, 0);
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 spin_lock(&journal->j_state_lock);
Duane Griffinbe07c4e2008-10-22 14:15:03 -0700174 } else {
175 spin_unlock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800177 mutex_unlock(&journal->j_checkpoint_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179}
180
181/*
182 * We were unable to perform jbd_trylock_bh_state() inside j_list_lock.
183 * The caller must restart a list walk. Wait for someone else to run
184 * jbd_unlock_bh_state().
185 */
186static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh)
Josh Triplette7ab8d62006-09-27 01:49:26 -0700187 __releases(journal->j_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 get_bh(bh);
190 spin_unlock(&journal->j_list_lock);
191 jbd_lock_bh_state(bh);
192 jbd_unlock_bh_state(bh);
193 put_bh(bh);
194}
195
196/*
Jan Kara78ce89c2006-06-23 02:06:05 -0700197 * Clean up transaction's list of buffers submitted for io.
198 * We wait for any pending IO to complete and remove any clean
199 * buffers. Note that we take the buffers in the opposite ordering
200 * from the one in which they were submitted for IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700202 * Return 0 on success, and return <0 if some buffers have failed
203 * to be written out.
204 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Called with j_list_lock held.
206 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700207static int __wait_cp_io(journal_t *journal, transaction_t *transaction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Jan Kara78ce89c2006-06-23 02:06:05 -0700209 struct journal_head *jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct buffer_head *bh;
Jan Kara78ce89c2006-06-23 02:06:05 -0700211 tid_t this_tid;
212 int released = 0;
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700213 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jan Kara78ce89c2006-06-23 02:06:05 -0700215 this_tid = transaction->t_tid;
216restart:
217 /* Did somebody clean up the transaction in the meanwhile? */
218 if (journal->j_checkpoint_transactions != transaction ||
219 transaction->t_tid != this_tid)
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700220 return ret;
Jan Kara78ce89c2006-06-23 02:06:05 -0700221 while (!released && transaction->t_checkpoint_io_list) {
222 jh = transaction->t_checkpoint_io_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 bh = jh2bh(jh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700224 if (!jbd_trylock_bh_state(bh)) {
225 jbd_sync_bh(journal, bh);
226 spin_lock(&journal->j_list_lock);
227 goto restart;
228 }
Jan Karabb189242011-06-24 23:11:59 +0200229 get_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (buffer_locked(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_unlock(&journal->j_list_lock);
Jan Kara78ce89c2006-06-23 02:06:05 -0700232 jbd_unlock_bh_state(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 wait_on_buffer(bh);
234 /* the journal_head may have gone by now */
235 BUFFER_TRACE(bh, "brelse");
236 __brelse(bh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700237 spin_lock(&journal->j_list_lock);
238 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Hidehiro Kawai9f818b42008-10-22 14:15:02 -0700240 if (unlikely(buffer_write_io_error(bh)))
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700241 ret = -EIO;
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /*
Jan Kara78ce89c2006-06-23 02:06:05 -0700244 * Now in whatever state the buffer currently is, we know that
245 * it has been written out and so we can drop it from the list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Jan Kara78ce89c2006-06-23 02:06:05 -0700247 released = __journal_remove_checkpoint(jh);
248 jbd_unlock_bh_state(bh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700249 __brelse(bh);
250 }
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700251
252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255#define NR_BATCH 64
256
257static void
258__flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count)
259{
260 int i;
Tao Maa212d1a2011-06-07 11:56:50 +0800261 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Tao Maa212d1a2011-06-07 11:56:50 +0800263 blk_start_plug(&plug);
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200264 for (i = 0; i < *batch_count; i++)
Tao Maa212d1a2011-06-07 11:56:50 +0800265 write_dirty_buffer(bhs[i], WRITE_SYNC);
266 blk_finish_plug(&plug);
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 for (i = 0; i < *batch_count; i++) {
269 struct buffer_head *bh = bhs[i];
270 clear_buffer_jwrite(bh);
271 BUFFER_TRACE(bh, "brelse");
272 __brelse(bh);
273 }
274 *batch_count = 0;
275}
276
277/*
278 * Try to flush one buffer from the checkpoint list to disk.
279 *
280 * Return 1 if something happened which requires us to abort the current
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700281 * scan of the checkpoint list. Return <0 if the buffer has failed to
282 * be written out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Jan Kara78ce89c2006-06-23 02:06:05 -0700284 * Called with j_list_lock held and drops it if 1 is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
286 */
Jan Kara78ce89c2006-06-23 02:06:05 -0700287static int __process_buffer(journal_t *journal, struct journal_head *jh,
288 struct buffer_head **bhs, int *batch_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct buffer_head *bh = jh2bh(jh);
291 int ret = 0;
292
Jan Kara78ce89c2006-06-23 02:06:05 -0700293 if (buffer_locked(bh)) {
Namhyung Kime4d5e3a2010-10-16 17:11:02 +0900294 get_bh(bh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700295 spin_unlock(&journal->j_list_lock);
296 jbd_unlock_bh_state(bh);
297 wait_on_buffer(bh);
298 /* the journal_head may have gone by now */
299 BUFFER_TRACE(bh, "brelse");
300 __brelse(bh);
301 ret = 1;
302 } else if (jh->b_transaction != NULL) {
303 transaction_t *t = jh->b_transaction;
304 tid_t tid = t->t_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Jan Kara78ce89c2006-06-23 02:06:05 -0700306 spin_unlock(&journal->j_list_lock);
307 jbd_unlock_bh_state(bh);
308 log_start_commit(journal, tid);
309 log_wait_commit(journal, tid);
310 ret = 1;
311 } else if (!buffer_dirty(bh)) {
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700312 ret = 1;
Hidehiro Kawai9f818b42008-10-22 14:15:02 -0700313 if (unlikely(buffer_write_io_error(bh)))
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700314 ret = -EIO;
Jan Karabb189242011-06-24 23:11:59 +0200315 get_bh(bh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700316 J_ASSERT_JH(jh, !buffer_jbddirty(bh));
317 BUFFER_TRACE(bh, "remove from checkpoint");
318 __journal_remove_checkpoint(jh);
319 spin_unlock(&journal->j_list_lock);
320 jbd_unlock_bh_state(bh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700321 __brelse(bh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700322 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
324 * Important: we are about to write the buffer, and
325 * possibly block, while still holding the journal lock.
326 * We cannot afford to let the transaction logic start
327 * messing around with this buffer before we write it to
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700328 * disk, as that would break recoverability.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330 BUFFER_TRACE(bh, "queue");
331 get_bh(bh);
332 J_ASSERT_BH(bh, !buffer_jwrite(bh));
333 set_buffer_jwrite(bh);
334 bhs[*batch_count] = bh;
Jan Kara78ce89c2006-06-23 02:06:05 -0700335 __buffer_relink_io(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 jbd_unlock_bh_state(bh);
337 (*batch_count)++;
338 if (*batch_count == NR_BATCH) {
Jan Kara78ce89c2006-06-23 02:06:05 -0700339 spin_unlock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 __flush_batch(journal, bhs, batch_count);
341 ret = 1;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 return ret;
345}
346
347/*
Jan Kara78ce89c2006-06-23 02:06:05 -0700348 * Perform an actual checkpoint. We take the first transaction on the
349 * list of transactions to be checkpointed and send all its buffers
350 * to disk. We submit larger chunks of data at once.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700351 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * The journal should be locked before calling this function.
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700353 * Called with j_checkpoint_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
355int log_do_checkpoint(journal_t *journal)
356{
Jan Kara78ce89c2006-06-23 02:06:05 -0700357 transaction_t *transaction;
358 tid_t this_tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 jbd_debug(1, "Start checkpoint\n");
362
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700363 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * First thing: if there are any transactions in the log which
365 * don't need checkpointing, just eliminate them from the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700366 * journal straight away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
368 result = cleanup_journal_tail(journal);
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200369 trace_jbd_checkpoint(journal, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
371 if (result <= 0)
372 return result;
373
374 /*
Jan Kara78ce89c2006-06-23 02:06:05 -0700375 * OK, we need to start writing disk blocks. Take one transaction
376 * and write it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700378 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock(&journal->j_list_lock);
Jan Kara78ce89c2006-06-23 02:06:05 -0700380 if (!journal->j_checkpoint_transactions)
381 goto out;
382 transaction = journal->j_checkpoint_transactions;
383 this_tid = transaction->t_tid;
384restart:
385 /*
386 * If someone cleaned up this transaction while we slept, we're
387 * done (maybe it's a new transaction, but it fell at the same
388 * address).
389 */
390 if (journal->j_checkpoint_transactions == transaction &&
391 transaction->t_tid == this_tid) {
392 int batch_count = 0;
393 struct buffer_head *bhs[NR_BATCH];
394 struct journal_head *jh;
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700395 int retry = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Jan Kara78ce89c2006-06-23 02:06:05 -0700397 while (!retry && transaction->t_checkpoint_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct buffer_head *bh;
399
Jan Kara78ce89c2006-06-23 02:06:05 -0700400 jh = transaction->t_checkpoint_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 bh = jh2bh(jh);
402 if (!jbd_trylock_bh_state(bh)) {
403 jbd_sync_bh(journal, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retry = 1;
405 break;
406 }
Jan Kara78ce89c2006-06-23 02:06:05 -0700407 retry = __process_buffer(journal, jh, bhs,&batch_count);
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700408 if (retry < 0 && !result)
409 result = retry;
Nick Piggin95c354f2008-01-30 13:31:20 +0100410 if (!retry && (need_resched() ||
411 spin_needbreak(&journal->j_list_lock))) {
Jan Kara78ce89c2006-06-23 02:06:05 -0700412 spin_unlock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 retry = 1;
414 break;
415 }
Jan Karaf93ea412006-01-06 00:19:55 -0800416 }
417
Jan Kara78ce89c2006-06-23 02:06:05 -0700418 if (batch_count) {
419 if (!retry) {
420 spin_unlock(&journal->j_list_lock);
421 retry = 1;
422 }
423 __flush_batch(journal, bhs, &batch_count);
424 }
425
426 if (retry) {
427 spin_lock(&journal->j_list_lock);
428 goto restart;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /*
Jan Kara78ce89c2006-06-23 02:06:05 -0700431 * Now we have cleaned up the first transaction's checkpoint
432 * list. Let's clean up the second one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700434 err = __wait_cp_io(journal, transaction);
435 if (!result)
436 result = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Jan Kara78ce89c2006-06-23 02:06:05 -0700438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 spin_unlock(&journal->j_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (result < 0)
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700441 journal_abort(journal, result);
442 else
443 result = cleanup_journal_tail(journal);
444
445 return (result < 0) ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * Check the list of checkpoint transactions for the journal to see if
450 * we have already got rid of any since the last update of the log tail
451 * in the journal superblock. If so, we can instantly roll the
452 * superblock forward to remove those transactions from the log.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700453 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700455 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * This is the only part of the journaling code which really needs to be
457 * aware of transaction aborts. Checkpointing involves writing to the
458 * main filesystem area rather than to the journal, so it can proceed
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700459 * even in abort state, but we must not update the super block if
460 * checkpointing may have failed. Otherwise, we would lose some metadata
461 * buffers which should be written-back to the filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
463
464int cleanup_journal_tail(journal_t *journal)
465{
466 transaction_t * transaction;
467 tid_t first_tid;
Jan Kara9c28cbc2009-08-03 19:21:00 +0200468 unsigned int blocknr, freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Hidehiro Kawai4afe9782008-10-22 14:15:00 -0700470 if (is_journal_aborted(journal))
471 return 1;
472
Jan Kara353b67d2011-11-26 00:35:39 +0100473 /*
474 * OK, work out the oldest transaction remaining in the log, and
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700475 * the log block it starts at.
476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * If the log is now empty, we need to work out which is the
478 * next transaction ID we will write, and where it will
Jan Kara353b67d2011-11-26 00:35:39 +0100479 * start.
480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 spin_lock(&journal->j_state_lock);
482 spin_lock(&journal->j_list_lock);
483 transaction = journal->j_checkpoint_transactions;
484 if (transaction) {
485 first_tid = transaction->t_tid;
486 blocknr = transaction->t_log_start;
487 } else if ((transaction = journal->j_committing_transaction) != NULL) {
488 first_tid = transaction->t_tid;
489 blocknr = transaction->t_log_start;
490 } else if ((transaction = journal->j_running_transaction) != NULL) {
491 first_tid = transaction->t_tid;
492 blocknr = journal->j_head;
493 } else {
494 first_tid = journal->j_transaction_sequence;
495 blocknr = journal->j_head;
496 }
497 spin_unlock(&journal->j_list_lock);
498 J_ASSERT(blocknr != 0);
499
500 /* If the oldest pinned transaction is at the tail of the log
501 already then there's not much we can do right now. */
502 if (journal->j_tail_sequence == first_tid) {
503 spin_unlock(&journal->j_state_lock);
504 return 1;
505 }
Jan Kara353b67d2011-11-26 00:35:39 +0100506 spin_unlock(&journal->j_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Jan Kara353b67d2011-11-26 00:35:39 +0100508 /*
509 * We need to make sure that any blocks that were recently written out
510 * --- perhaps by log_do_checkpoint() --- are flushed out before we
511 * drop the transactions from the journal. It's unlikely this will be
512 * necessary, especially with an appropriately sized journal, but we
513 * need this to guarantee correctness. Fortunately
514 * cleanup_journal_tail() doesn't get called all that often.
515 */
516 if (journal->j_flags & JFS_BARRIER)
517 blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL);
518
519 spin_lock(&journal->j_state_lock);
520 if (!tid_gt(first_tid, journal->j_tail_sequence)) {
521 spin_unlock(&journal->j_state_lock);
522 /* Someone else cleaned up journal so return 0 */
523 return 0;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* OK, update the superblock to recover the freed space.
526 * Physical blocks come first: have we wrapped beyond the end of
527 * the log? */
528 freed = blocknr - journal->j_tail;
529 if (blocknr < journal->j_tail)
530 freed = freed + journal->j_last - journal->j_first;
531
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200532 trace_jbd_cleanup_journal_tail(journal, first_tid, blocknr, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 jbd_debug(1,
Jan Kara9c28cbc2009-08-03 19:21:00 +0200534 "Cleaning journal tail from %d to %d (offset %u), "
535 "freeing %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 journal->j_tail_sequence, first_tid, blocknr, freed);
537
538 journal->j_free += freed;
539 journal->j_tail_sequence = first_tid;
540 journal->j_tail = blocknr;
541 spin_unlock(&journal->j_state_lock);
542 if (!(journal->j_flags & JFS_ABORT))
543 journal_update_superblock(journal, 1);
544 return 0;
545}
546
547
548/* Checkpoint list management */
549
550/*
Jan Kara78ce89c2006-06-23 02:06:05 -0700551 * journal_clean_one_cp_list
Jan Karaf93ea412006-01-06 00:19:55 -0800552 *
Jan Karabb189242011-06-24 23:11:59 +0200553 * Find all the written-back checkpoint buffers in the given list and release
554 * them.
Jan Karaf93ea412006-01-06 00:19:55 -0800555 *
Jan Karaf93ea412006-01-06 00:19:55 -0800556 * Called with j_list_lock held.
Paul Bolle90802ed2011-12-05 13:00:34 +0100557 * Returns number of buffers reaped (for debug)
Jan Karaf93ea412006-01-06 00:19:55 -0800558 */
559
Jan Kara78ce89c2006-06-23 02:06:05 -0700560static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
561{
562 struct journal_head *last_jh;
563 struct journal_head *next_jh = jh;
564 int ret, freed = 0;
565
566 *released = 0;
567 if (!jh)
568 return 0;
569
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700570 last_jh = jh->b_cpprev;
Jan Kara78ce89c2006-06-23 02:06:05 -0700571 do {
572 jh = next_jh;
573 next_jh = jh->b_cpnext;
574 /* Use trylock because of the ranking */
575 if (jbd_trylock_bh_state(jh2bh(jh))) {
576 ret = __try_to_free_cp_buf(jh);
577 if (ret) {
578 freed++;
579 if (ret == 2) {
580 *released = 1;
581 return freed;
582 }
583 }
584 }
585 /*
586 * This function only frees up some memory
587 * if possible so we dont have an obligation
588 * to finish processing. Bail out if preemption
589 * requested:
590 */
591 if (need_resched())
592 return freed;
593 } while (jh != last_jh);
594
595 return freed;
596}
597
598/*
599 * journal_clean_checkpoint_list
600 *
601 * Find all the written-back checkpoint buffers in the journal and release them.
602 *
603 * Called with the journal locked.
604 * Called with j_list_lock held.
605 * Returns number of buffers reaped (for debug)
606 */
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608int __journal_clean_checkpoint_list(journal_t *journal)
609{
610 transaction_t *transaction, *last_transaction, *next_transaction;
Mark Fasheh7c8903f2006-02-14 13:53:03 -0800611 int ret = 0;
Jan Kara78ce89c2006-06-23 02:06:05 -0700612 int released;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 transaction = journal->j_checkpoint_transactions;
Jan Kara78ce89c2006-06-23 02:06:05 -0700615 if (!transaction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto out;
617
618 last_transaction = transaction->t_cpprev;
619 next_transaction = transaction;
620 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 transaction = next_transaction;
622 next_transaction = transaction->t_cpnext;
Jan Kara78ce89c2006-06-23 02:06:05 -0700623 ret += journal_clean_one_cp_list(transaction->
624 t_checkpoint_list, &released);
625 /*
626 * This function only frees up some memory if possible so we
627 * dont have an obligation to finish processing. Bail out if
628 * preemption requested:
629 */
630 if (need_resched())
631 goto out;
632 if (released)
633 continue;
634 /*
635 * It is essential that we are as careful as in the case of
636 * t_checkpoint_list with removing the buffer from the list as
637 * we can possibly see not yet submitted buffers on io_list
638 */
639 ret += journal_clean_one_cp_list(transaction->
640 t_checkpoint_io_list, &released);
641 if (need_resched())
642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } while (transaction != last_transaction);
644out:
645 return ret;
646}
647
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700648/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * journal_remove_checkpoint: called after a buffer has been committed
650 * to disk (either by being write-back flushed to disk, or being
651 * committed to the log).
652 *
653 * We cannot safely clean a transaction out of the log until all of the
654 * buffer updates committed in that transaction have safely been stored
655 * elsewhere on disk. To achieve this, all of the buffers in a
656 * transaction need to be maintained on the transaction's checkpoint
Jan Kara78ce89c2006-06-23 02:06:05 -0700657 * lists until they have been rewritten, at which point this function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * called to remove the buffer from the existing transaction's
Jan Kara78ce89c2006-06-23 02:06:05 -0700659 * checkpoint lists.
660 *
661 * The function returns 1 if it frees the transaction, 0 otherwise.
Jan Karabb189242011-06-24 23:11:59 +0200662 * The function can free jh and bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * This function is called with j_list_lock held.
Jan Kara78ce89c2006-06-23 02:06:05 -0700665 * This function is called with jbd_lock_bh_state(jh2bh(jh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
667
Jan Kara78ce89c2006-06-23 02:06:05 -0700668int __journal_remove_checkpoint(struct journal_head *jh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 transaction_t *transaction;
671 journal_t *journal;
Jan Kara78ce89c2006-06-23 02:06:05 -0700672 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 JBUFFER_TRACE(jh, "entry");
675
676 if ((transaction = jh->b_cp_transaction) == NULL) {
677 JBUFFER_TRACE(jh, "not on transaction");
678 goto out;
679 }
680 journal = transaction->t_journal;
681
Jan Karabb189242011-06-24 23:11:59 +0200682 JBUFFER_TRACE(jh, "removing from transaction");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 __buffer_unlink(jh);
Jan Kara78ce89c2006-06-23 02:06:05 -0700684 jh->b_cp_transaction = NULL;
Jan Karabb189242011-06-24 23:11:59 +0200685 journal_put_journal_head(jh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jan Kara78ce89c2006-06-23 02:06:05 -0700687 if (transaction->t_checkpoint_list != NULL ||
688 transaction->t_checkpoint_io_list != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /*
692 * There is one special case to worry about: if we have just pulled the
Jan Karad4beaf42007-12-04 23:45:27 -0800693 * buffer off a running or committing transaction's checkpoing list,
694 * then even if the checkpoint list is empty, the transaction obviously
695 * cannot be dropped!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
Jan Karad4beaf42007-12-04 23:45:27 -0800697 * The locking here around t_state is a bit sleazy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * See the comment at the end of journal_commit_transaction().
699 */
Jan Karabb189242011-06-24 23:11:59 +0200700 if (transaction->t_state != T_FINISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* OK, that was the last buffer for the transaction: we can now
704 safely remove this transaction from the log */
705
706 __journal_drop_transaction(journal, transaction);
707
708 /* Just in case anybody was waiting for more transactions to be
709 checkpointed... */
710 wake_up(&journal->j_wait_logspace);
Jan Kara78ce89c2006-06-23 02:06:05 -0700711 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712out:
Jan Kara78ce89c2006-06-23 02:06:05 -0700713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/*
717 * journal_insert_checkpoint: put a committed buffer onto a checkpoint
718 * list so that we know when it is safe to clean the transaction out of
719 * the log.
720 *
721 * Called with the journal locked.
722 * Called with j_list_lock held.
723 */
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700724void __journal_insert_checkpoint(struct journal_head *jh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 transaction_t *transaction)
726{
727 JBUFFER_TRACE(jh, "entry");
728 J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
729 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
730
Jan Karabb189242011-06-24 23:11:59 +0200731 /* Get reference for checkpointing transaction */
732 journal_grab_journal_head(jh2bh(jh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 jh->b_cp_transaction = transaction;
734
735 if (!transaction->t_checkpoint_list) {
736 jh->b_cpnext = jh->b_cpprev = jh;
737 } else {
738 jh->b_cpnext = transaction->t_checkpoint_list;
739 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
740 jh->b_cpprev->b_cpnext = jh;
741 jh->b_cpnext->b_cpprev = jh;
742 }
743 transaction->t_checkpoint_list = jh;
744}
745
746/*
747 * We've finished with this transaction structure: adios...
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700748 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * The transaction must have no links except for the checkpoint by this
750 * point.
751 *
752 * Called with the journal locked.
753 * Called with j_list_lock held.
754 */
755
756void __journal_drop_transaction(journal_t *journal, transaction_t *transaction)
757{
758 assert_spin_locked(&journal->j_list_lock);
759 if (transaction->t_cpnext) {
760 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
761 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
762 if (journal->j_checkpoint_transactions == transaction)
763 journal->j_checkpoint_transactions =
764 transaction->t_cpnext;
765 if (journal->j_checkpoint_transactions == transaction)
766 journal->j_checkpoint_transactions = NULL;
767 }
768
769 J_ASSERT(transaction->t_state == T_FINISHED);
770 J_ASSERT(transaction->t_buffers == NULL);
771 J_ASSERT(transaction->t_sync_datalist == NULL);
772 J_ASSERT(transaction->t_forget == NULL);
773 J_ASSERT(transaction->t_iobuf_list == NULL);
774 J_ASSERT(transaction->t_shadow_list == NULL);
775 J_ASSERT(transaction->t_log_list == NULL);
776 J_ASSERT(transaction->t_checkpoint_list == NULL);
Jan Kara78ce89c2006-06-23 02:06:05 -0700777 J_ASSERT(transaction->t_checkpoint_io_list == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 J_ASSERT(transaction->t_updates == 0);
779 J_ASSERT(journal->j_committing_transaction != transaction);
780 J_ASSERT(journal->j_running_transaction != transaction);
781
Lukas Czerner99cb1a32011-05-23 18:33:02 +0200782 trace_jbd_drop_transaction(journal, transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
784 kfree(transaction);
785}