blob: b51fbd4b291327528c583a6371ac1d9995817f79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02002 * linux/fs/jbd/transaction.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>, 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 * Generic filesystem transaction handling code; part of the ext2fs
Mingming Caoae6ddcc2006-09-27 01:49:27 -070013 * journaling system.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
17 * filesystem).
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>
25#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mm.h>
27#include <linux/highmem.h>
Josef Bacikf420d4d2009-01-07 18:07:24 -080028#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Adrian Bunkd394e122006-12-06 20:38:26 -080030static void __journal_temp_unlink_buffer(struct journal_head *jh);
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * get_transaction: obtain a new transaction_t object.
34 *
35 * Simply allocate and initialise a new transaction. Create it in
36 * RUNNING state and add it to the current journal (which should not
37 * have an existing running transaction: we only make a new transaction
38 * once we have started to commit the old one).
39 *
40 * Preconditions:
41 * The journal MUST be locked. We don't perform atomic mallocs on the
42 * new transaction and we can't block without protecting against other
43 * processes trying to touch the journal while it is in transition.
44 *
45 * Called under j_state_lock
46 */
47
48static transaction_t *
49get_transaction(journal_t *journal, transaction_t *transaction)
50{
51 transaction->t_journal = journal;
52 transaction->t_state = T_RUNNING;
Josef Bacikf420d4d2009-01-07 18:07:24 -080053 transaction->t_start_time = ktime_get();
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 transaction->t_tid = journal->j_transaction_sequence++;
55 transaction->t_expires = jiffies + journal->j_commit_interval;
56 spin_lock_init(&transaction->t_handle_lock);
57
58 /* Set up the commit timer for the new transaction. */
Arjan van de Ven44d306e2006-12-10 02:21:26 -080059 journal->j_commit_timer.expires = round_jiffies(transaction->t_expires);
Andrew Mortone3df1892006-03-25 03:06:53 -080060 add_timer(&journal->j_commit_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 J_ASSERT(journal->j_running_transaction == NULL);
63 journal->j_running_transaction = transaction;
64
65 return transaction;
66}
67
68/*
69 * Handle management.
70 *
71 * A handle_t is an object which represents a single atomic update to a
72 * filesystem, and which tracks all of the modifications which form part
73 * of that one update.
74 */
75
76/*
77 * start_this_handle: Given a handle, deal with any locking or stalling
78 * needed to make sure that there is enough journal space for the handle
79 * to begin. Attach the handle to a transaction and set up the
Mingming Caoae6ddcc2006-09-27 01:49:27 -070080 * transaction's buffer credits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int start_this_handle(journal_t *journal, handle_t *handle)
84{
85 transaction_t *transaction;
86 int needed;
87 int nblocks = handle->h_buffer_credits;
88 transaction_t *new_transaction = NULL;
89 int ret = 0;
90
91 if (nblocks > journal->j_max_transaction_buffers) {
92 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
93 current->comm, nblocks,
94 journal->j_max_transaction_buffers);
95 ret = -ENOSPC;
96 goto out;
97 }
98
99alloc_transaction:
100 if (!journal->j_running_transaction) {
Mingming Cao8c3478a2007-10-18 23:39:20 -0700101 new_transaction = kzalloc(sizeof(*new_transaction),
Mingming Caoa5005da2007-10-16 18:38:25 -0400102 GFP_NOFS|__GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (!new_transaction) {
104 ret = -ENOMEM;
105 goto out;
106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
109 jbd_debug(3, "New handle %p going live.\n", handle);
110
111repeat:
112
113 /*
114 * We need to hold j_state_lock until t_updates has been incremented,
115 * for proper journal barrier handling
116 */
117 spin_lock(&journal->j_state_lock);
118repeat_locked:
119 if (is_journal_aborted(journal) ||
120 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
121 spin_unlock(&journal->j_state_lock);
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700122 ret = -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 goto out;
124 }
125
126 /* Wait on the journal's transaction barrier if necessary */
127 if (journal->j_barrier_count) {
128 spin_unlock(&journal->j_state_lock);
129 wait_event(journal->j_wait_transaction_locked,
130 journal->j_barrier_count == 0);
131 goto repeat;
132 }
133
134 if (!journal->j_running_transaction) {
135 if (!new_transaction) {
136 spin_unlock(&journal->j_state_lock);
137 goto alloc_transaction;
138 }
139 get_transaction(journal, new_transaction);
140 new_transaction = NULL;
141 }
142
143 transaction = journal->j_running_transaction;
144
145 /*
146 * If the current transaction is locked down for commit, wait for the
147 * lock to be released.
148 */
149 if (transaction->t_state == T_LOCKED) {
150 DEFINE_WAIT(wait);
151
152 prepare_to_wait(&journal->j_wait_transaction_locked,
153 &wait, TASK_UNINTERRUPTIBLE);
154 spin_unlock(&journal->j_state_lock);
155 schedule();
156 finish_wait(&journal->j_wait_transaction_locked, &wait);
157 goto repeat;
158 }
159
160 /*
161 * If there is not enough space left in the log to write all potential
162 * buffers requested by this operation, we need to stall pending a log
163 * checkpoint to free some more log space.
164 */
165 spin_lock(&transaction->t_handle_lock);
166 needed = transaction->t_outstanding_credits + nblocks;
167
168 if (needed > journal->j_max_transaction_buffers) {
169 /*
170 * If the current transaction is already too large, then start
171 * to commit it: we can then go back and attach this handle to
172 * a new transaction.
173 */
174 DEFINE_WAIT(wait);
175
176 jbd_debug(2, "Handle %p starting new commit...\n", handle);
177 spin_unlock(&transaction->t_handle_lock);
178 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
179 TASK_UNINTERRUPTIBLE);
180 __log_start_commit(journal, transaction->t_tid);
181 spin_unlock(&journal->j_state_lock);
182 schedule();
183 finish_wait(&journal->j_wait_transaction_locked, &wait);
184 goto repeat;
185 }
186
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700187 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * The commit code assumes that it can get enough log space
189 * without forcing a checkpoint. This is *critical* for
190 * correctness: a checkpoint of a buffer which is also
191 * associated with a committing transaction creates a deadlock,
192 * so commit simply cannot force through checkpoints.
193 *
194 * We must therefore ensure the necessary space in the journal
195 * *before* starting to dirty potentially checkpointed buffers
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700196 * in the new transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
198 * The worst part is, any transaction currently committing can
199 * reduce the free space arbitrarily. Be careful to account for
200 * those buffers when checkpointing.
201 */
202
203 /*
204 * @@@ AKPM: This seems rather over-defensive. We're giving commit
205 * a _lot_ of headroom: 1/4 of the journal plus the size of
206 * the committing transaction. Really, we only need to give it
207 * committing_transaction->t_outstanding_credits plus "enough" for
208 * the log control blocks.
209 * Also, this test is inconsitent with the matching one in
210 * journal_extend().
211 */
212 if (__log_space_left(journal) < jbd_space_needed(journal)) {
213 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
214 spin_unlock(&transaction->t_handle_lock);
215 __log_wait_for_space(journal);
216 goto repeat_locked;
217 }
218
219 /* OK, account for the buffers that this operation expects to
220 * use and add the handle to the running transaction. */
221
222 handle->h_transaction = transaction;
223 transaction->t_outstanding_credits += nblocks;
224 transaction->t_updates++;
225 transaction->t_handle_count++;
226 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
227 handle, nblocks, transaction->t_outstanding_credits,
228 __log_space_left(journal));
229 spin_unlock(&transaction->t_handle_lock);
230 spin_unlock(&journal->j_state_lock);
231out:
Andrew Morton304c4c82006-06-23 02:05:31 -0700232 if (unlikely(new_transaction)) /* It's usually NULL */
233 kfree(new_transaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return ret;
235}
236
Peter Zijlstra34a3d1e2007-10-11 22:11:12 +0200237static struct lock_class_key jbd_handle_key;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/* Allocate a new handle. This should probably be in a slab... */
240static handle_t *new_handle(int nblocks)
241{
242 handle_t *handle = jbd_alloc_handle(GFP_NOFS);
243 if (!handle)
244 return NULL;
245 memset(handle, 0, sizeof(*handle));
246 handle->h_buffer_credits = nblocks;
247 handle->h_ref = 1;
248
Peter Zijlstra34a3d1e2007-10-11 22:11:12 +0200249 lockdep_init_map(&handle->h_lockdep_map, "jbd_handle", &jbd_handle_key, 0);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return handle;
252}
253
254/**
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700255 * handle_t *journal_start() - Obtain a new handle.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * @journal: Journal to start transaction on.
257 * @nblocks: number of block buffer we might modify
258 *
259 * We make sure that the transaction can guarantee at least nblocks of
260 * modified buffers in the log. We block until the log can guarantee
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700261 * that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 * This function is visible to journal users (like ext3fs), so is not
264 * called with the journal already locked.
265 *
266 * Return a pointer to a newly allocated handle, or NULL on failure
267 */
268handle_t *journal_start(journal_t *journal, int nblocks)
269{
270 handle_t *handle = journal_current_handle();
271 int err;
272
273 if (!journal)
274 return ERR_PTR(-EROFS);
275
276 if (handle) {
277 J_ASSERT(handle->h_transaction->t_journal == journal);
278 handle->h_ref++;
279 return handle;
280 }
281
282 handle = new_handle(nblocks);
283 if (!handle)
284 return ERR_PTR(-ENOMEM);
285
286 current->journal_info = handle;
287
288 err = start_this_handle(journal, handle);
289 if (err < 0) {
290 jbd_free_handle(handle);
291 current->journal_info = NULL;
292 handle = ERR_PTR(err);
Jonas Bonnf63dcda2008-01-17 15:21:13 -0800293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Peter Zijlstra34a3d1e2007-10-11 22:11:12 +0200295
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200296 lock_map_acquire(&handle->h_lockdep_map);
Peter Zijlstra34a3d1e2007-10-11 22:11:12 +0200297
Jonas Bonnf63dcda2008-01-17 15:21:13 -0800298out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return handle;
300}
301
302/**
303 * int journal_extend() - extend buffer credits.
304 * @handle: handle to 'extend'
305 * @nblocks: nr blocks to try to extend by.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * Some transactions, such as large extends and truncates, can be done
308 * atomically all at once or in several stages. The operation requests
309 * a credit for a number of buffer modications in advance, but can
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700310 * extend its credit if it needs more.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 *
312 * journal_extend tries to give the running handle more buffer credits.
313 * It does not guarantee that allocation - this is a best-effort only.
314 * The calling process MUST be able to deal cleanly with a failure to
315 * extend here.
316 *
317 * Return 0 on success, non-zero on failure.
318 *
319 * return code < 0 implies an error
320 * return code > 0 implies normal transaction-full status.
321 */
322int journal_extend(handle_t *handle, int nblocks)
323{
324 transaction_t *transaction = handle->h_transaction;
325 journal_t *journal = transaction->t_journal;
326 int result;
327 int wanted;
328
329 result = -EIO;
330 if (is_handle_aborted(handle))
331 goto out;
332
333 result = 1;
334
335 spin_lock(&journal->j_state_lock);
336
337 /* Don't extend a locked-down transaction! */
338 if (handle->h_transaction->t_state != T_RUNNING) {
339 jbd_debug(3, "denied handle %p %d blocks: "
340 "transaction not running\n", handle, nblocks);
341 goto error_out;
342 }
343
344 spin_lock(&transaction->t_handle_lock);
345 wanted = transaction->t_outstanding_credits + nblocks;
346
347 if (wanted > journal->j_max_transaction_buffers) {
348 jbd_debug(3, "denied handle %p %d blocks: "
349 "transaction too large\n", handle, nblocks);
350 goto unlock;
351 }
352
353 if (wanted > __log_space_left(journal)) {
354 jbd_debug(3, "denied handle %p %d blocks: "
355 "insufficient log space\n", handle, nblocks);
356 goto unlock;
357 }
358
359 handle->h_buffer_credits += nblocks;
360 transaction->t_outstanding_credits += nblocks;
361 result = 0;
362
363 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
364unlock:
365 spin_unlock(&transaction->t_handle_lock);
366error_out:
367 spin_unlock(&journal->j_state_lock);
368out:
369 return result;
370}
371
372
373/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800374 * int journal_restart() - restart a handle.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * @handle: handle to restart
376 * @nblocks: nr credits requested
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700377 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * Restart a handle for a multi-transaction filesystem
379 * operation.
380 *
381 * If the journal_extend() call above fails to grant new buffer credits
382 * to a running handle, a call to journal_restart will commit the
383 * handle's transaction so far and reattach the handle to a new
384 * transaction capabable of guaranteeing the requested number of
385 * credits.
386 */
387
388int journal_restart(handle_t *handle, int nblocks)
389{
390 transaction_t *transaction = handle->h_transaction;
391 journal_t *journal = transaction->t_journal;
392 int ret;
393
394 /* If we've had an abort of any type, don't even think about
395 * actually doing the restart! */
396 if (is_handle_aborted(handle))
397 return 0;
398
399 /*
400 * First unlink the handle from its current transaction, and start the
401 * commit on that.
402 */
403 J_ASSERT(transaction->t_updates > 0);
404 J_ASSERT(journal_current_handle() == handle);
405
406 spin_lock(&journal->j_state_lock);
407 spin_lock(&transaction->t_handle_lock);
408 transaction->t_outstanding_credits -= handle->h_buffer_credits;
409 transaction->t_updates--;
410
411 if (!transaction->t_updates)
412 wake_up(&journal->j_wait_updates);
413 spin_unlock(&transaction->t_handle_lock);
414
415 jbd_debug(2, "restarting handle %p\n", handle);
416 __log_start_commit(journal, transaction->t_tid);
417 spin_unlock(&journal->j_state_lock);
418
419 handle->h_buffer_credits = nblocks;
420 ret = start_this_handle(journal, handle);
421 return ret;
422}
423
424
425/**
426 * void journal_lock_updates () - establish a transaction barrier.
427 * @journal: Journal to establish a barrier on.
428 *
429 * This locks out any further updates from being started, and blocks
430 * until all existing updates have completed, returning only once the
431 * journal is in a quiescent state with no updates running.
432 *
433 * The journal lock should not be held on entry.
434 */
435void journal_lock_updates(journal_t *journal)
436{
437 DEFINE_WAIT(wait);
438
439 spin_lock(&journal->j_state_lock);
440 ++journal->j_barrier_count;
441
442 /* Wait until there are no running updates */
443 while (1) {
444 transaction_t *transaction = journal->j_running_transaction;
445
446 if (!transaction)
447 break;
448
449 spin_lock(&transaction->t_handle_lock);
450 if (!transaction->t_updates) {
451 spin_unlock(&transaction->t_handle_lock);
452 break;
453 }
454 prepare_to_wait(&journal->j_wait_updates, &wait,
455 TASK_UNINTERRUPTIBLE);
456 spin_unlock(&transaction->t_handle_lock);
457 spin_unlock(&journal->j_state_lock);
458 schedule();
459 finish_wait(&journal->j_wait_updates, &wait);
460 spin_lock(&journal->j_state_lock);
461 }
462 spin_unlock(&journal->j_state_lock);
463
464 /*
465 * We have now established a barrier against other normal updates, but
466 * we also need to barrier against other journal_lock_updates() calls
467 * to make sure that we serialise special journal-locked operations
468 * too.
469 */
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800470 mutex_lock(&journal->j_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473/**
474 * void journal_unlock_updates (journal_t* journal) - release barrier
475 * @journal: Journal to release the barrier on.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * Release a transaction barrier obtained with journal_lock_updates().
478 *
479 * Should be called without the journal lock held.
480 */
481void journal_unlock_updates (journal_t *journal)
482{
483 J_ASSERT(journal->j_barrier_count != 0);
484
Arjan van de Ven2c68ee72006-03-23 03:00:35 -0800485 mutex_unlock(&journal->j_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 spin_lock(&journal->j_state_lock);
487 --journal->j_barrier_count;
488 spin_unlock(&journal->j_state_lock);
489 wake_up(&journal->j_wait_transaction_locked);
490}
491
492/*
493 * Report any unexpected dirty buffers which turn up. Normally those
494 * indicate an error, but they can occur if the user is running (say)
495 * tune2fs to modify the live filesystem, so we need the option of
496 * continuing as gracefully as possible. #
497 *
498 * The caller should already hold the journal lock and
499 * j_list_lock spinlock: most callers will need those anyway
500 * in order to probe the buffer's journaling state safely.
501 */
502static void jbd_unexpected_dirty_buffer(struct journal_head *jh)
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int jlist;
505
Jan Kara4407c2b2005-09-06 15:19:17 -0700506 /* If this buffer is one which might reasonably be dirty
507 * --- ie. data, or not part of this journal --- then
508 * we're OK to leave it alone, but otherwise we need to
509 * move the dirty bit to the journal's own internal
510 * JBDDirty bit. */
511 jlist = jh->b_jlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Jan Kara4407c2b2005-09-06 15:19:17 -0700513 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
514 jlist == BJ_Shadow || jlist == BJ_Forget) {
515 struct buffer_head *bh = jh2bh(jh);
516
517 if (test_clear_buffer_dirty(bh))
518 set_buffer_jbddirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520}
521
522/*
523 * If the buffer is already part of the current transaction, then there
524 * is nothing we need to do. If it is already part of a prior
525 * transaction which we are still committing to disk, then we need to
526 * make sure that we do not overwrite the old copy: we do copy-out to
527 * preserve the copy going to disk. We also account the buffer against
528 * the handle's metadata buffer credits (unless the buffer is already
529 * part of the transaction, that is).
530 *
531 */
532static int
533do_get_write_access(handle_t *handle, struct journal_head *jh,
534 int force_copy)
535{
536 struct buffer_head *bh;
537 transaction_t *transaction;
538 journal_t *journal;
539 int error;
540 char *frozen_buffer = NULL;
541 int need_copy = 0;
542
543 if (is_handle_aborted(handle))
544 return -EROFS;
545
546 transaction = handle->h_transaction;
547 journal = transaction->t_journal;
548
549 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
550
551 JBUFFER_TRACE(jh, "entry");
552repeat:
553 bh = jh2bh(jh);
554
555 /* @@@ Need to check for errors here at some point. */
556
557 lock_buffer(bh);
558 jbd_lock_bh_state(bh);
559
560 /* We now hold the buffer lock so it is safe to query the buffer
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700561 * state. Is the buffer dirty?
562 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * If so, there are two possibilities. The buffer may be
564 * non-journaled, and undergoing a quite legitimate writeback.
565 * Otherwise, it is journaled, and we don't expect dirty buffers
566 * in that state (the buffers should be marked JBD_Dirty
567 * instead.) So either the IO is being done under our own
568 * control and this is a bug, or it's a third party IO such as
569 * dump(8) (which may leave the buffer scheduled for read ---
570 * ie. locked but not dirty) or tune2fs (which may actually have
571 * the buffer dirtied, ugh.) */
572
573 if (buffer_dirty(bh)) {
574 /*
575 * First question: is this buffer already part of the current
576 * transaction or the existing committing transaction?
577 */
578 if (jh->b_transaction) {
579 J_ASSERT_JH(jh,
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700580 jh->b_transaction == transaction ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 jh->b_transaction ==
582 journal->j_committing_transaction);
583 if (jh->b_next_transaction)
584 J_ASSERT_JH(jh, jh->b_next_transaction ==
585 transaction);
Jan Kara4407c2b2005-09-06 15:19:17 -0700586 }
587 /*
588 * In any case we need to clean the dirty flag and we must
589 * do it under the buffer lock to be sure we don't race
590 * with running write-out.
591 */
592 JBUFFER_TRACE(jh, "Unexpected dirty buffer");
593 jbd_unexpected_dirty_buffer(jh);
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 unlock_buffer(bh);
597
598 error = -EROFS;
599 if (is_handle_aborted(handle)) {
600 jbd_unlock_bh_state(bh);
601 goto out;
602 }
603 error = 0;
604
605 /*
606 * The buffer is already part of this transaction if b_transaction or
607 * b_next_transaction points to it
608 */
609 if (jh->b_transaction == transaction ||
610 jh->b_next_transaction == transaction)
611 goto done;
612
613 /*
Josef Bacik5bc833f2008-04-28 02:16:10 -0700614 * this is the first time this transaction is touching this buffer,
615 * reset the modified flag
616 */
617 jh->b_modified = 0;
618
619 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * If there is already a copy-out version of this buffer, then we don't
621 * need to make another one
622 */
623 if (jh->b_frozen_data) {
624 JBUFFER_TRACE(jh, "has frozen data");
625 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
626 jh->b_next_transaction = transaction;
627 goto done;
628 }
629
630 /* Is there data here we need to preserve? */
631
632 if (jh->b_transaction && jh->b_transaction != transaction) {
633 JBUFFER_TRACE(jh, "owned by older transaction");
634 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
635 J_ASSERT_JH(jh, jh->b_transaction ==
636 journal->j_committing_transaction);
637
638 /* There is one case we have to be very careful about.
639 * If the committing transaction is currently writing
640 * this buffer out to disk and has NOT made a copy-out,
641 * then we cannot modify the buffer contents at all
642 * right now. The essence of copy-out is that it is the
643 * extra copy, not the primary copy, which gets
644 * journaled. If the primary copy is already going to
645 * disk then we cannot do copy-out here. */
646
647 if (jh->b_jlist == BJ_Shadow) {
648 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
649 wait_queue_head_t *wqh;
650
651 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
652
653 JBUFFER_TRACE(jh, "on shadow: sleep");
654 jbd_unlock_bh_state(bh);
655 /* commit wakes up all shadow buffers after IO */
656 for ( ; ; ) {
657 prepare_to_wait(wqh, &wait.wait,
658 TASK_UNINTERRUPTIBLE);
659 if (jh->b_jlist != BJ_Shadow)
660 break;
661 schedule();
662 }
663 finish_wait(wqh, &wait.wait);
664 goto repeat;
665 }
666
667 /* Only do the copy if the currently-owning transaction
668 * still needs it. If it is on the Forget list, the
669 * committing transaction is past that stage. The
670 * buffer had better remain locked during the kmalloc,
671 * but that should be true --- we hold the journal lock
672 * still and the buffer is already on the BUF_JOURNAL
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700673 * list so won't be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 *
675 * Subtle point, though: if this is a get_undo_access,
676 * then we will be relying on the frozen_data to contain
677 * the new value of the committed_data record after the
678 * transaction, so we HAVE to force the frozen_data copy
679 * in that case. */
680
681 if (jh->b_jlist != BJ_Forget || force_copy) {
682 JBUFFER_TRACE(jh, "generate frozen data");
683 if (!frozen_buffer) {
684 JBUFFER_TRACE(jh, "allocate memory for buffer");
685 jbd_unlock_bh_state(bh);
Badari Pulavartyea817392006-08-27 01:23:52 -0700686 frozen_buffer =
Mingming Caoc089d492007-10-16 18:38:25 -0400687 jbd_alloc(jh2bh(jh)->b_size,
Badari Pulavartyea817392006-08-27 01:23:52 -0700688 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!frozen_buffer) {
690 printk(KERN_EMERG
691 "%s: OOM for frozen_buffer\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700692 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 JBUFFER_TRACE(jh, "oom!");
694 error = -ENOMEM;
695 jbd_lock_bh_state(bh);
696 goto done;
697 }
698 goto repeat;
699 }
700 jh->b_frozen_data = frozen_buffer;
701 frozen_buffer = NULL;
702 need_copy = 1;
703 }
704 jh->b_next_transaction = transaction;
705 }
706
707
708 /*
709 * Finally, if the buffer is not journaled right now, we need to make
710 * sure it doesn't get written to disk before the caller actually
711 * commits the new data
712 */
713 if (!jh->b_transaction) {
714 JBUFFER_TRACE(jh, "no transaction");
715 J_ASSERT_JH(jh, !jh->b_next_transaction);
716 jh->b_transaction = transaction;
717 JBUFFER_TRACE(jh, "file as BJ_Reserved");
718 spin_lock(&journal->j_list_lock);
719 __journal_file_buffer(jh, transaction, BJ_Reserved);
720 spin_unlock(&journal->j_list_lock);
721 }
722
723done:
724 if (need_copy) {
725 struct page *page;
726 int offset;
727 char *source;
728
729 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
730 "Possible IO failure.\n");
731 page = jh2bh(jh)->b_page;
732 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
733 source = kmap_atomic(page, KM_USER0);
734 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
735 kunmap_atomic(source, KM_USER0);
736 }
737 jbd_unlock_bh_state(bh);
738
739 /*
740 * If we are about to journal a buffer, then any revoke pending on it is
741 * no longer valid
742 */
743 journal_cancel_revoke(handle, jh);
744
745out:
Andrew Morton304c4c82006-06-23 02:05:31 -0700746 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoc089d492007-10-16 18:38:25 -0400747 jbd_free(frozen_buffer, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 JBUFFER_TRACE(jh, "exit");
750 return error;
751}
752
753/**
754 * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
755 * @handle: transaction to add buffer modifications to
756 * @bh: bh to be used for metadata writes
757 * @credits: variable that will receive credits for the buffer
758 *
759 * Returns an error code or 0 on success.
760 *
761 * In full data journalling mode the buffer may be of type BJ_AsyncData,
762 * because we're write()ing a buffer which is also part of a shared mapping.
763 */
764
765int journal_get_write_access(handle_t *handle, struct buffer_head *bh)
766{
767 struct journal_head *jh = journal_add_journal_head(bh);
768 int rc;
769
770 /* We do not want to get caught playing with fields which the
771 * log thread also manipulates. Make sure that the buffer
772 * completes any outstanding IO before proceeding. */
773 rc = do_get_write_access(handle, jh, 0);
774 journal_put_journal_head(jh);
775 return rc;
776}
777
778
779/*
780 * When the user wants to journal a newly created buffer_head
781 * (ie. getblk() returned a new buffer and we are going to populate it
782 * manually rather than reading off disk), then we need to keep the
783 * buffer_head locked until it has been completely filled with new
784 * data. In this case, we should be able to make the assertion that
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700785 * the bh is not already part of an existing transaction.
786 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * The buffer should already be locked by the caller by this point.
788 * There is no lock ranking violation: it was a newly created,
789 * unlocked buffer beforehand. */
790
791/**
792 * int journal_get_create_access () - notify intent to use newly created bh
793 * @handle: transaction to new buffer to
794 * @bh: new buffer.
795 *
796 * Call this if you create a new bh.
797 */
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700798int journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 transaction_t *transaction = handle->h_transaction;
801 journal_t *journal = transaction->t_journal;
802 struct journal_head *jh = journal_add_journal_head(bh);
803 int err;
804
805 jbd_debug(5, "journal_head %p\n", jh);
806 err = -EROFS;
807 if (is_handle_aborted(handle))
808 goto out;
809 err = 0;
810
811 JBUFFER_TRACE(jh, "entry");
812 /*
813 * The buffer may already belong to this transaction due to pre-zeroing
814 * in the filesystem's new_block code. It may also be on the previous,
815 * committing transaction's lists, but it HAS to be in Forget state in
816 * that case: the transaction must have deleted the buffer for it to be
817 * reused here.
818 */
819 jbd_lock_bh_state(bh);
820 spin_lock(&journal->j_list_lock);
821 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
822 jh->b_transaction == NULL ||
823 (jh->b_transaction == journal->j_committing_transaction &&
824 jh->b_jlist == BJ_Forget)));
825
826 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
827 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
828
829 if (jh->b_transaction == NULL) {
830 jh->b_transaction = transaction;
Josef Bacik5bc833f2008-04-28 02:16:10 -0700831
832 /* first access by this transaction */
833 jh->b_modified = 0;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 JBUFFER_TRACE(jh, "file as BJ_Reserved");
836 __journal_file_buffer(jh, transaction, BJ_Reserved);
837 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik5bc833f2008-04-28 02:16:10 -0700838 /* first access by this transaction */
839 jh->b_modified = 0;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 JBUFFER_TRACE(jh, "set next transaction");
842 jh->b_next_transaction = transaction;
843 }
844 spin_unlock(&journal->j_list_lock);
845 jbd_unlock_bh_state(bh);
846
847 /*
848 * akpm: I added this. ext3_alloc_branch can pick up new indirect
849 * blocks which contain freed but then revoked metadata. We need
850 * to cancel the revoke in case we end up freeing it yet again
851 * and the reallocating as data - this would cause a second revoke,
852 * which hits an assertion error.
853 */
854 JBUFFER_TRACE(jh, "cancelling revoke");
855 journal_cancel_revoke(handle, jh);
856 journal_put_journal_head(jh);
857out:
858 return err;
859}
860
861/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800862 * int journal_get_undo_access() - Notify intent to modify metadata with non-rewindable consequences
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * @handle: transaction
864 * @bh: buffer to undo
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
866 * Sometimes there is a need to distinguish between metadata which has
867 * been committed to disk and that which has not. The ext3fs code uses
868 * this for freeing and allocating space, we have to make sure that we
869 * do not reuse freed space until the deallocation has been committed,
870 * since if we overwrote that space we would make the delete
871 * un-rewindable in case of a crash.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700872 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * To deal with that, journal_get_undo_access requests write access to a
874 * buffer for parts of non-rewindable operations such as delete
875 * operations on the bitmaps. The journaling code must keep a copy of
876 * the buffer's contents prior to the undo_access call until such time
877 * as we know that the buffer has definitely been committed to disk.
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700878 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 * We never need to know which transaction the committed data is part
880 * of, buffers touched here are guaranteed to be dirtied later and so
881 * will be committed to a new transaction in due course, at which point
882 * we can discard the old committed data pointer.
883 *
884 * Returns error number or 0 on success.
885 */
886int journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
887{
888 int err;
889 struct journal_head *jh = journal_add_journal_head(bh);
890 char *committed_data = NULL;
891
892 JBUFFER_TRACE(jh, "entry");
893
894 /*
895 * Do this first --- it can drop the journal lock, so we want to
896 * make sure that obtaining the committed_data is done
897 * atomically wrt. completion of any outstanding commits.
898 */
899 err = do_get_write_access(handle, jh, 1);
900 if (err)
901 goto out;
902
903repeat:
904 if (!jh->b_committed_data) {
Mingming Caoc089d492007-10-16 18:38:25 -0400905 committed_data = jbd_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!committed_data) {
907 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700908 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 err = -ENOMEM;
910 goto out;
911 }
912 }
913
914 jbd_lock_bh_state(bh);
915 if (!jh->b_committed_data) {
916 /* Copy out the current buffer contents into the
917 * preserved, committed copy. */
918 JBUFFER_TRACE(jh, "generate b_committed data");
919 if (!committed_data) {
920 jbd_unlock_bh_state(bh);
921 goto repeat;
922 }
923
924 jh->b_committed_data = committed_data;
925 committed_data = NULL;
926 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
927 }
928 jbd_unlock_bh_state(bh);
929out:
930 journal_put_journal_head(jh);
Andrew Morton304c4c82006-06-23 02:05:31 -0700931 if (unlikely(committed_data))
Mingming Caoc089d492007-10-16 18:38:25 -0400932 jbd_free(committed_data, bh->b_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return err;
934}
935
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700936/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800937 * int journal_dirty_data() - mark a buffer as containing dirty data to be flushed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * @handle: transaction
939 * @bh: bufferhead to mark
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700940 *
Randy Dunlap78a4a502008-02-29 22:02:31 -0800941 * Description:
942 * Mark a buffer as containing dirty data which needs to be flushed before
943 * we can commit the current transaction.
944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * The buffer is placed on the transaction's data list and is marked as
946 * belonging to the transaction.
947 *
948 * Returns error number or 0 on success.
949 *
950 * journal_dirty_data() can be called via page_launder->ext3_writepage
951 * by kswapd.
952 */
953int journal_dirty_data(handle_t *handle, struct buffer_head *bh)
954{
955 journal_t *journal = handle->h_transaction->t_journal;
956 int need_brelse = 0;
957 struct journal_head *jh;
Hidehiro Kawai960a22a2008-10-18 20:27:58 -0700958 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (is_handle_aborted(handle))
Hidehiro Kawai960a22a2008-10-18 20:27:58 -0700961 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 jh = journal_add_journal_head(bh);
964 JBUFFER_TRACE(jh, "entry");
965
966 /*
967 * The buffer could *already* be dirty. Writeout can start
968 * at any time.
969 */
970 jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid);
971
972 /*
973 * What if the buffer is already part of a running transaction?
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700974 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * There are two cases:
976 * 1) It is part of the current running transaction. Refile it,
977 * just in case we have allocated it as metadata, deallocated
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700978 * it, then reallocated it as data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * 2) It is part of the previous, still-committing transaction.
980 * If all we want to do is to guarantee that the buffer will be
981 * written to disk before this new transaction commits, then
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700982 * being sure that the *previous* transaction has this same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * property is sufficient for us! Just leave it on its old
984 * transaction.
985 *
986 * In case (2), the buffer must not already exist as metadata
987 * --- that would violate write ordering (a transaction is free
988 * to write its data at any point, even before the previous
989 * committing transaction has committed). The caller must
990 * never, ever allow this to happen: there's nothing we can do
991 * about it in this layer.
992 */
993 jbd_lock_bh_state(bh);
994 spin_lock(&journal->j_list_lock);
Eric Sandeenf58a74d2006-10-28 10:38:27 -0700995
996 /* Now that we have bh_state locked, are we really still mapped? */
997 if (!buffer_mapped(bh)) {
998 JBUFFER_TRACE(jh, "unmapped buffer, bailing out");
999 goto no_journal;
1000 }
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (jh->b_transaction) {
1003 JBUFFER_TRACE(jh, "has transaction");
1004 if (jh->b_transaction != handle->h_transaction) {
1005 JBUFFER_TRACE(jh, "belongs to older transaction");
1006 J_ASSERT_JH(jh, jh->b_transaction ==
1007 journal->j_committing_transaction);
1008
1009 /* @@@ IS THIS TRUE ? */
1010 /*
1011 * Not any more. Scenario: someone does a write()
1012 * in data=journal mode. The buffer's transaction has
1013 * moved into commit. Then someone does another
1014 * write() to the file. We do the frozen data copyout
1015 * and set b_next_transaction to point to j_running_t.
1016 * And while we're in that state, someone does a
1017 * writepage() in an attempt to pageout the same area
1018 * of the file via a shared mapping. At present that
1019 * calls journal_dirty_data(), and we get right here.
1020 * It may be too late to journal the data. Simply
1021 * falling through to the next test will suffice: the
1022 * data will be dirty and wil be checkpointed. The
1023 * ordering comments in the next comment block still
1024 * apply.
1025 */
1026 //J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1027
1028 /*
1029 * If we're journalling data, and this buffer was
1030 * subject to a write(), it could be metadata, forget
1031 * or shadow against the committing transaction. Now,
1032 * someone has dirtied the same darn page via a mapping
1033 * and it is being writepage()'d.
1034 * We *could* just steal the page from commit, with some
1035 * fancy locking there. Instead, we just skip it -
1036 * don't tie the page's buffers to the new transaction
1037 * at all.
1038 * Implication: if we crash before the writepage() data
1039 * is written into the filesystem, recovery will replay
1040 * the write() data.
1041 */
1042 if (jh->b_jlist != BJ_None &&
1043 jh->b_jlist != BJ_SyncData &&
1044 jh->b_jlist != BJ_Locked) {
1045 JBUFFER_TRACE(jh, "Not stealing");
1046 goto no_journal;
1047 }
1048
1049 /*
1050 * This buffer may be undergoing writeout in commit. We
1051 * can't return from here and let the caller dirty it
1052 * again because that can cause the write-out loop in
1053 * commit to never terminate.
1054 */
1055 if (buffer_dirty(bh)) {
1056 get_bh(bh);
1057 spin_unlock(&journal->j_list_lock);
1058 jbd_unlock_bh_state(bh);
1059 need_brelse = 1;
1060 sync_dirty_buffer(bh);
1061 jbd_lock_bh_state(bh);
1062 spin_lock(&journal->j_list_lock);
Eric Sandeenf58a74d2006-10-28 10:38:27 -07001063 /* Since we dropped the lock... */
1064 if (!buffer_mapped(bh)) {
1065 JBUFFER_TRACE(jh, "buffer got unmapped");
1066 goto no_journal;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* The buffer may become locked again at any
1069 time if it is redirtied */
1070 }
1071
Hidehiro Kawai960a22a2008-10-18 20:27:58 -07001072 /*
1073 * We cannot remove the buffer with io error from the
1074 * committing transaction, because otherwise it would
1075 * miss the error and the commit would not abort.
1076 */
1077 if (unlikely(!buffer_uptodate(bh))) {
1078 ret = -EIO;
1079 goto no_journal;
1080 }
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (jh->b_transaction != NULL) {
1083 JBUFFER_TRACE(jh, "unfile from commit");
1084 __journal_temp_unlink_buffer(jh);
1085 /* It still points to the committing
1086 * transaction; move it to this one so
1087 * that the refile assert checks are
1088 * happy. */
1089 jh->b_transaction = handle->h_transaction;
1090 }
1091 /* The buffer will be refiled below */
1092
1093 }
1094 /*
1095 * Special case --- the buffer might actually have been
1096 * allocated and then immediately deallocated in the previous,
1097 * committing transaction, so might still be left on that
1098 * transaction's metadata lists.
1099 */
1100 if (jh->b_jlist != BJ_SyncData && jh->b_jlist != BJ_Locked) {
1101 JBUFFER_TRACE(jh, "not on correct data list: unfile");
1102 J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);
1103 __journal_temp_unlink_buffer(jh);
1104 jh->b_transaction = handle->h_transaction;
1105 JBUFFER_TRACE(jh, "file as data");
1106 __journal_file_buffer(jh, handle->h_transaction,
1107 BJ_SyncData);
1108 }
1109 } else {
1110 JBUFFER_TRACE(jh, "not on a transaction");
1111 __journal_file_buffer(jh, handle->h_transaction, BJ_SyncData);
1112 }
1113no_journal:
1114 spin_unlock(&journal->j_list_lock);
1115 jbd_unlock_bh_state(bh);
1116 if (need_brelse) {
1117 BUFFER_TRACE(bh, "brelse");
1118 __brelse(bh);
1119 }
1120 JBUFFER_TRACE(jh, "exit");
1121 journal_put_journal_head(jh);
Hidehiro Kawai960a22a2008-10-18 20:27:58 -07001122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001125/**
Randy Dunlap78a4a502008-02-29 22:02:31 -08001126 * int journal_dirty_metadata() - mark a buffer as containing dirty metadata
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * @handle: transaction to add buffer to.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001128 * @bh: buffer to mark
1129 *
Randy Dunlap78a4a502008-02-29 22:02:31 -08001130 * Mark dirty metadata which needs to be journaled as part of the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * transaction.
1132 *
1133 * The buffer is placed on the transaction's metadata list and is marked
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001134 * as belonging to the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001136 * Returns error number or 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 *
1138 * Special care needs to be taken if the buffer already belongs to the
1139 * current committing transaction (in which case we should have frozen
1140 * data present for that commit). In that case, we don't relink the
1141 * buffer: that only gets done when the old transaction finally
1142 * completes its commit.
1143 */
1144int journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1145{
1146 transaction_t *transaction = handle->h_transaction;
1147 journal_t *journal = transaction->t_journal;
1148 struct journal_head *jh = bh2jh(bh);
1149
1150 jbd_debug(5, "journal_head %p\n", jh);
1151 JBUFFER_TRACE(jh, "entry");
1152 if (is_handle_aborted(handle))
1153 goto out;
1154
1155 jbd_lock_bh_state(bh);
1156
1157 if (jh->b_modified == 0) {
1158 /*
1159 * This buffer's got modified and becoming part
1160 * of the transaction. This needs to be done
1161 * once a transaction -bzzz
1162 */
1163 jh->b_modified = 1;
1164 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1165 handle->h_buffer_credits--;
1166 }
1167
1168 /*
1169 * fastpath, to avoid expensive locking. If this buffer is already
1170 * on the running transaction's metadata list there is nothing to do.
1171 * Nobody can take it off again because there is a handle open.
1172 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1173 * result in this test being false, so we go in and take the locks.
1174 */
1175 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1176 JBUFFER_TRACE(jh, "fastpath");
1177 J_ASSERT_JH(jh, jh->b_transaction ==
1178 journal->j_running_transaction);
1179 goto out_unlock_bh;
1180 }
1181
1182 set_buffer_jbddirty(bh);
1183
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001184 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * Metadata already on the current transaction list doesn't
1186 * need to be filed. Metadata on another transaction's list must
1187 * be committing, and will be refiled once the commit completes:
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001188 * leave it alone for now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190 if (jh->b_transaction != transaction) {
1191 JBUFFER_TRACE(jh, "already on other transaction");
1192 J_ASSERT_JH(jh, jh->b_transaction ==
1193 journal->j_committing_transaction);
1194 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1195 /* And this case is illegal: we can't reuse another
1196 * transaction's data buffer, ever. */
1197 goto out_unlock_bh;
1198 }
1199
1200 /* That test should have eliminated the following case: */
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001201 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1204 spin_lock(&journal->j_list_lock);
1205 __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1206 spin_unlock(&journal->j_list_lock);
1207out_unlock_bh:
1208 jbd_unlock_bh_state(bh);
1209out:
1210 JBUFFER_TRACE(jh, "exit");
1211 return 0;
1212}
1213
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * journal_release_buffer: undo a get_write_access without any buffer
1216 * updates, if the update decided in the end that it didn't need access.
1217 *
1218 */
1219void
1220journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1221{
1222 BUFFER_TRACE(bh, "entry");
1223}
1224
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001225/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * void journal_forget() - bforget() for potentially-journaled buffers.
1227 * @handle: transaction handle
1228 * @bh: bh to 'forget'
1229 *
1230 * We can only do the bforget if there are no commits pending against the
1231 * buffer. If the buffer is dirty in the current running transaction we
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001232 * can safely unlink it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 *
1234 * bh may not be a journalled buffer at all - it may be a non-JBD
1235 * buffer which came off the hashtable. Check for this.
1236 *
1237 * Decrements bh->b_count by one.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * Allow this call even if the handle has aborted --- it may be part of
1240 * the caller's cleanup after an abort.
1241 */
1242int journal_forget (handle_t *handle, struct buffer_head *bh)
1243{
1244 transaction_t *transaction = handle->h_transaction;
1245 journal_t *journal = transaction->t_journal;
1246 struct journal_head *jh;
1247 int drop_reserve = 0;
1248 int err = 0;
Josef Bacik5b9a4992008-04-28 02:16:12 -07001249 int was_modified = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 BUFFER_TRACE(bh, "entry");
1252
1253 jbd_lock_bh_state(bh);
1254 spin_lock(&journal->j_list_lock);
1255
1256 if (!buffer_jbd(bh))
1257 goto not_jbd;
1258 jh = bh2jh(bh);
1259
1260 /* Critical error: attempting to delete a bitmap buffer, maybe?
1261 * Don't do any jbd operations, and return an error. */
1262 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1263 "inconsistent data on disk")) {
1264 err = -EIO;
1265 goto not_jbd;
1266 }
1267
Josef Bacik5b9a4992008-04-28 02:16:12 -07001268 /* keep track of wether or not this transaction modified us */
1269 was_modified = jh->b_modified;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * The buffer's going from the transaction, we must drop
1273 * all references -bzzz
1274 */
1275 jh->b_modified = 0;
1276
1277 if (jh->b_transaction == handle->h_transaction) {
1278 J_ASSERT_JH(jh, !jh->b_frozen_data);
1279
1280 /* If we are forgetting a buffer which is already part
1281 * of this transaction, then we can just drop it from
1282 * the transaction immediately. */
1283 clear_buffer_dirty(bh);
1284 clear_buffer_jbddirty(bh);
1285
1286 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1287
Josef Bacik5b9a4992008-04-28 02:16:12 -07001288 /*
1289 * we only want to drop a reference if this transaction
1290 * modified the buffer
1291 */
1292 if (was_modified)
1293 drop_reserve = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001295 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * We are no longer going to journal this buffer.
1297 * However, the commit of this transaction is still
1298 * important to the buffer: the delete that we are now
1299 * processing might obsolete an old log entry, so by
1300 * committing, we can satisfy the buffer's checkpoint.
1301 *
1302 * So, if we have a checkpoint on the buffer, we should
1303 * now refile the buffer on our BJ_Forget list so that
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001304 * we know to remove the checkpoint after we commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
1306
1307 if (jh->b_cp_transaction) {
1308 __journal_temp_unlink_buffer(jh);
1309 __journal_file_buffer(jh, transaction, BJ_Forget);
1310 } else {
1311 __journal_unfile_buffer(jh);
1312 journal_remove_journal_head(bh);
1313 __brelse(bh);
1314 if (!buffer_jbd(bh)) {
1315 spin_unlock(&journal->j_list_lock);
1316 jbd_unlock_bh_state(bh);
1317 __bforget(bh);
1318 goto drop;
1319 }
1320 }
1321 } else if (jh->b_transaction) {
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001322 J_ASSERT_JH(jh, (jh->b_transaction ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 journal->j_committing_transaction));
1324 /* However, if the buffer is still owned by a prior
1325 * (committing) transaction, we can't drop it yet... */
1326 JBUFFER_TRACE(jh, "belongs to older transaction");
1327 /* ... but we CAN drop it from the new transaction if we
1328 * have also modified it since the original commit. */
1329
1330 if (jh->b_next_transaction) {
1331 J_ASSERT(jh->b_next_transaction == transaction);
1332 jh->b_next_transaction = NULL;
Josef Bacik5b9a4992008-04-28 02:16:12 -07001333
1334 /*
1335 * only drop a reference if this transaction modified
1336 * the buffer
1337 */
1338 if (was_modified)
1339 drop_reserve = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 }
1342
1343not_jbd:
1344 spin_unlock(&journal->j_list_lock);
1345 jbd_unlock_bh_state(bh);
1346 __brelse(bh);
1347drop:
1348 if (drop_reserve) {
1349 /* no need to reserve log space for this block -bzzz */
1350 handle->h_buffer_credits++;
1351 }
1352 return err;
1353}
1354
1355/**
1356 * int journal_stop() - complete a transaction
1357 * @handle: tranaction to complete.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * All done for a particular handle.
1360 *
1361 * There is not much action needed here. We just return any remaining
1362 * buffer credits to the transaction and remove the handle. The only
1363 * complication is that we need to start a commit operation if the
1364 * filesystem is marked for synchronous update.
1365 *
1366 * journal_stop itself will not usually return an error, but it may
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001367 * do so in unusual circumstances. In particular, expect it to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * return -EIO if a journal_abort has been executed since the
1369 * transaction began.
1370 */
1371int journal_stop(handle_t *handle)
1372{
1373 transaction_t *transaction = handle->h_transaction;
1374 journal_t *journal = transaction->t_journal;
Josef Bacikf420d4d2009-01-07 18:07:24 -08001375 int err;
Andrew Mortonfe1dcbc2006-02-04 23:27:54 -08001376 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 J_ASSERT(journal_current_handle() == handle);
1379
1380 if (is_handle_aborted(handle))
1381 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001382 else {
1383 J_ASSERT(transaction->t_updates > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if (--handle->h_ref > 0) {
1388 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1389 handle->h_ref);
1390 return err;
1391 }
1392
1393 jbd_debug(4, "Handle %p going down\n", handle);
1394
1395 /*
1396 * Implement synchronous transaction batching. If the handle
1397 * was synchronous, don't force a commit immediately. Let's
1398 * yield and let another thread piggyback onto this transaction.
1399 * Keep doing that while new threads continue to arrive.
1400 * It doesn't cost much - we're about to run a commit and sleep
1401 * on IO anyway. Speeds up many-threaded, many-dir operations
1402 * by 30x or more...
Andrew Mortonfe1dcbc2006-02-04 23:27:54 -08001403 *
Josef Bacikf420d4d2009-01-07 18:07:24 -08001404 * We try and optimize the sleep time against what the underlying disk
1405 * can do, instead of having a static sleep time. This is usefull for
1406 * the case where our storage is so fast that it is more optimal to go
1407 * ahead and force a flush and wait for the transaction to be committed
1408 * than it is to wait for an arbitrary amount of time for new writers to
1409 * join the transaction. We acheive this by measuring how long it takes
1410 * to commit a transaction, and compare it with how long this
1411 * transaction has been running, and if run time < commit time then we
1412 * sleep for the delta and commit. This greatly helps super fast disks
1413 * that would see slowdowns as more threads started doing fsyncs.
1414 *
Andrew Mortonfe1dcbc2006-02-04 23:27:54 -08001415 * But don't do this if this process was the most recent one to
1416 * perform a synchronous write. We do this to detect the case where a
1417 * single process is doing a stream of sync writes. No point in waiting
1418 * for joiners in that case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
Andrew Mortonfe1dcbc2006-02-04 23:27:54 -08001420 pid = current->pid;
1421 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacikf420d4d2009-01-07 18:07:24 -08001422 u64 commit_time, trans_time;
1423
Andrew Mortonfe1dcbc2006-02-04 23:27:54 -08001424 journal->j_last_sync_writer = pid;
Josef Bacikf420d4d2009-01-07 18:07:24 -08001425
1426 spin_lock(&journal->j_state_lock);
1427 commit_time = journal->j_average_commit_time;
1428 spin_unlock(&journal->j_state_lock);
1429
1430 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1431 transaction->t_start_time));
1432
1433 commit_time = min_t(u64, commit_time,
1434 1000*jiffies_to_usecs(1));
1435
1436 if (trans_time < commit_time) {
1437 ktime_t expires = ktime_add_ns(ktime_get(),
1438 commit_time);
1439 set_current_state(TASK_UNINTERRUPTIBLE);
1440 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
1444 current->journal_info = NULL;
1445 spin_lock(&journal->j_state_lock);
1446 spin_lock(&transaction->t_handle_lock);
1447 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1448 transaction->t_updates--;
1449 if (!transaction->t_updates) {
1450 wake_up(&journal->j_wait_updates);
1451 if (journal->j_barrier_count)
1452 wake_up(&journal->j_wait_transaction_locked);
1453 }
1454
1455 /*
1456 * If the handle is marked SYNC, we need to set another commit
1457 * going! We also want to force a commit if the current
1458 * transaction is occupying too much of the log, or if the
1459 * transaction is too old now.
1460 */
1461 if (handle->h_sync ||
1462 transaction->t_outstanding_credits >
1463 journal->j_max_transaction_buffers ||
Dave Kleikampe9ad5622006-09-27 01:49:35 -07001464 time_after_eq(jiffies, transaction->t_expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* Do this even for aborted journals: an abort still
1466 * completes the commit thread, it just doesn't write
1467 * anything to disk. */
1468 tid_t tid = transaction->t_tid;
1469
1470 spin_unlock(&transaction->t_handle_lock);
1471 jbd_debug(2, "transaction too old, requesting commit for "
1472 "handle %p\n", handle);
1473 /* This is non-blocking */
1474 __log_start_commit(journal, transaction->t_tid);
1475 spin_unlock(&journal->j_state_lock);
1476
1477 /*
1478 * Special case: JFS_SYNC synchronous updates require us
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001479 * to wait for the commit to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
1481 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1482 err = log_wait_commit(journal, tid);
1483 } else {
1484 spin_unlock(&transaction->t_handle_lock);
1485 spin_unlock(&journal->j_state_lock);
1486 }
1487
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001488 lock_map_release(&handle->h_lockdep_map);
Peter Zijlstra34a3d1e2007-10-11 22:11:12 +02001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 jbd_free_handle(handle);
1491 return err;
1492}
1493
Randy Dunlap0cf01f62008-03-19 17:00:44 -07001494/**
1495 * int journal_force_commit() - force any uncommitted transactions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * @journal: journal to force
1497 *
1498 * For synchronous operations: force any uncommitted transactions
1499 * to disk. May seem kludgy, but it reuses all the handle batching
1500 * code in a very simple manner.
1501 */
1502int journal_force_commit(journal_t *journal)
1503{
1504 handle_t *handle;
1505 int ret;
1506
1507 handle = journal_start(journal, 1);
1508 if (IS_ERR(handle)) {
1509 ret = PTR_ERR(handle);
1510 } else {
1511 handle->h_sync = 1;
1512 ret = journal_stop(handle);
1513 }
1514 return ret;
1515}
1516
1517/*
1518 *
1519 * List management code snippets: various functions for manipulating the
1520 * transaction buffer lists.
1521 *
1522 */
1523
1524/*
1525 * Append a buffer to a transaction list, given the transaction's list head
1526 * pointer.
1527 *
1528 * j_list_lock is held.
1529 *
1530 * jbd_lock_bh_state(jh2bh(jh)) is held.
1531 */
1532
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001533static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1535{
1536 if (!*list) {
1537 jh->b_tnext = jh->b_tprev = jh;
1538 *list = jh;
1539 } else {
1540 /* Insert at the tail of the list to preserve order */
1541 struct journal_head *first = *list, *last = first->b_tprev;
1542 jh->b_tprev = last;
1543 jh->b_tnext = first;
1544 last->b_tnext = first->b_tprev = jh;
1545 }
1546}
1547
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001548/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * Remove a buffer from a transaction list, given the transaction's list
1550 * head pointer.
1551 *
1552 * Called with j_list_lock held, and the journal may not be locked.
1553 *
1554 * jbd_lock_bh_state(jh2bh(jh)) is held.
1555 */
1556
1557static inline void
1558__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1559{
1560 if (*list == jh) {
1561 *list = jh->b_tnext;
1562 if (*list == jh)
1563 *list = NULL;
1564 }
1565 jh->b_tprev->b_tnext = jh->b_tnext;
1566 jh->b_tnext->b_tprev = jh->b_tprev;
1567}
1568
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001569/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 * Remove a buffer from the appropriate transaction list.
1571 *
1572 * Note that this function can *change* the value of
1573 * bh->b_transaction->t_sync_datalist, t_buffers, t_forget,
1574 * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller
1575 * is holding onto a copy of one of thee pointers, it could go bad.
1576 * Generally the caller needs to re-read the pointer from the transaction_t.
1577 *
1578 * Called under j_list_lock. The journal may not be locked.
1579 */
Adrian Bunkd394e122006-12-06 20:38:26 -08001580static void __journal_temp_unlink_buffer(struct journal_head *jh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct journal_head **list = NULL;
1583 transaction_t *transaction;
1584 struct buffer_head *bh = jh2bh(jh);
1585
1586 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1587 transaction = jh->b_transaction;
1588 if (transaction)
1589 assert_spin_locked(&transaction->t_journal->j_list_lock);
1590
1591 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1592 if (jh->b_jlist != BJ_None)
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001593 J_ASSERT_JH(jh, transaction != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 switch (jh->b_jlist) {
1596 case BJ_None:
1597 return;
1598 case BJ_SyncData:
1599 list = &transaction->t_sync_datalist;
1600 break;
1601 case BJ_Metadata:
1602 transaction->t_nr_buffers--;
1603 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1604 list = &transaction->t_buffers;
1605 break;
1606 case BJ_Forget:
1607 list = &transaction->t_forget;
1608 break;
1609 case BJ_IO:
1610 list = &transaction->t_iobuf_list;
1611 break;
1612 case BJ_Shadow:
1613 list = &transaction->t_shadow_list;
1614 break;
1615 case BJ_LogCtl:
1616 list = &transaction->t_log_list;
1617 break;
1618 case BJ_Reserved:
1619 list = &transaction->t_reserved_list;
1620 break;
1621 case BJ_Locked:
1622 list = &transaction->t_locked_list;
1623 break;
1624 }
1625
1626 __blist_del_buffer(list, jh);
1627 jh->b_jlist = BJ_None;
1628 if (test_clear_buffer_jbddirty(bh))
1629 mark_buffer_dirty(bh); /* Expose it to the VM */
1630}
1631
1632void __journal_unfile_buffer(struct journal_head *jh)
1633{
1634 __journal_temp_unlink_buffer(jh);
1635 jh->b_transaction = NULL;
1636}
1637
1638void journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1639{
1640 jbd_lock_bh_state(jh2bh(jh));
1641 spin_lock(&journal->j_list_lock);
1642 __journal_unfile_buffer(jh);
1643 spin_unlock(&journal->j_list_lock);
1644 jbd_unlock_bh_state(jh2bh(jh));
1645}
1646
1647/*
1648 * Called from journal_try_to_free_buffers().
1649 *
1650 * Called under jbd_lock_bh_state(bh)
1651 */
1652static void
1653__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1654{
1655 struct journal_head *jh;
1656
1657 jh = bh2jh(bh);
1658
1659 if (buffer_locked(bh) || buffer_dirty(bh))
1660 goto out;
1661
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001662 if (jh->b_next_transaction != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto out;
1664
1665 spin_lock(&journal->j_list_lock);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001666 if (jh->b_transaction != NULL && jh->b_cp_transaction == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (jh->b_jlist == BJ_SyncData || jh->b_jlist == BJ_Locked) {
1668 /* A written-back ordered data buffer */
1669 JBUFFER_TRACE(jh, "release data");
1670 __journal_unfile_buffer(jh);
1671 journal_remove_journal_head(bh);
1672 __brelse(bh);
1673 }
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001674 } else if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /* written-back checkpointed metadata buffer */
1676 if (jh->b_jlist == BJ_None) {
1677 JBUFFER_TRACE(jh, "remove from checkpoint list");
1678 __journal_remove_checkpoint(jh);
1679 journal_remove_journal_head(bh);
1680 __brelse(bh);
1681 }
1682 }
1683 spin_unlock(&journal->j_list_lock);
1684out:
1685 return;
1686}
1687
Mingming Cao3f31fdd2008-07-25 01:46:22 -07001688/*
1689 * journal_try_to_free_buffers() could race with journal_commit_transaction()
1690 * The latter might still hold the a count on buffers when inspecting
1691 * them on t_syncdata_list or t_locked_list.
1692 *
1693 * journal_try_to_free_buffers() will call this function to
1694 * wait for the current transaction to finish syncing data buffers, before
1695 * tryinf to free that buffer.
1696 *
1697 * Called with journal->j_state_lock held.
1698 */
1699static void journal_wait_for_transaction_sync_data(journal_t *journal)
1700{
1701 transaction_t *transaction = NULL;
1702 tid_t tid;
1703
1704 spin_lock(&journal->j_state_lock);
1705 transaction = journal->j_committing_transaction;
1706
1707 if (!transaction) {
1708 spin_unlock(&journal->j_state_lock);
1709 return;
1710 }
1711
1712 tid = transaction->t_tid;
1713 spin_unlock(&journal->j_state_lock);
1714 log_wait_commit(journal, tid);
1715}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001717/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 * int journal_try_to_free_buffers() - try to free page buffers.
1719 * @journal: journal for operation
1720 * @page: to try and free
Mingming Cao3f31fdd2008-07-25 01:46:22 -07001721 * @gfp_mask: we use the mask to detect how hard should we try to release
1722 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1723 * release the buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 *
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001725 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 * For all the buffers on this page,
1727 * if they are fully written out ordered data, move them onto BUF_CLEAN
1728 * so try_to_free_buffers() can reap them.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001729 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * This function returns non-zero if we wish try_to_free_buffers()
1731 * to be called. We do this if the page is releasable by try_to_free_buffers().
1732 * We also do it if the page has locked or dirty buffers and the caller wants
1733 * us to perform sync or async writeout.
1734 *
1735 * This complicates JBD locking somewhat. We aren't protected by the
1736 * BKL here. We wish to remove the buffer from its committing or
1737 * running transaction's ->t_datalist via __journal_unfile_buffer.
1738 *
1739 * This may *change* the value of transaction_t->t_datalist, so anyone
1740 * who looks at t_datalist needs to lock against this function.
1741 *
1742 * Even worse, someone may be doing a journal_dirty_data on this
1743 * buffer. So we need to lock against that. journal_dirty_data()
1744 * will come out of the lock with the buffer dirty, which makes it
1745 * ineligible for release here.
1746 *
1747 * Who else is affected by this? hmm... Really the only contender
1748 * is do_get_write_access() - it could be looking at the buffer while
1749 * journal_try_to_free_buffer() is changing its state. But that
1750 * cannot happen because we never reallocate freed data as metadata
1751 * while the data is part of a transaction. Yes?
Mingming Cao3f31fdd2008-07-25 01:46:22 -07001752 *
1753 * Return 0 on failure, 1 on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001755int journal_try_to_free_buffers(journal_t *journal,
Mingming Cao3f31fdd2008-07-25 01:46:22 -07001756 struct page *page, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 struct buffer_head *head;
1759 struct buffer_head *bh;
1760 int ret = 0;
1761
1762 J_ASSERT(PageLocked(page));
1763
1764 head = page_buffers(page);
1765 bh = head;
1766 do {
1767 struct journal_head *jh;
1768
1769 /*
1770 * We take our own ref against the journal_head here to avoid
1771 * having to add tons of locking around each instance of
1772 * journal_remove_journal_head() and journal_put_journal_head().
1773 */
1774 jh = journal_grab_journal_head(bh);
1775 if (!jh)
1776 continue;
1777
1778 jbd_lock_bh_state(bh);
1779 __journal_try_to_free_buffer(journal, bh);
1780 journal_put_journal_head(jh);
1781 jbd_unlock_bh_state(bh);
1782 if (buffer_jbd(bh))
1783 goto busy;
1784 } while ((bh = bh->b_this_page) != head);
Mingming Cao3f31fdd2008-07-25 01:46:22 -07001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 ret = try_to_free_buffers(page);
Mingming Cao3f31fdd2008-07-25 01:46:22 -07001787
1788 /*
1789 * There are a number of places where journal_try_to_free_buffers()
1790 * could race with journal_commit_transaction(), the later still
1791 * holds the reference to the buffers to free while processing them.
1792 * try_to_free_buffers() failed to free those buffers. Some of the
1793 * caller of releasepage() request page buffers to be dropped, otherwise
1794 * treat the fail-to-free as errors (such as generic_file_direct_IO())
1795 *
1796 * So, if the caller of try_to_release_page() wants the synchronous
1797 * behaviour(i.e make sure buffers are dropped upon return),
1798 * let's wait for the current transaction to finish flush of
1799 * dirty data buffers, then try to free those buffers again,
1800 * with the journal locked.
1801 */
1802 if (ret == 0 && (gfp_mask & __GFP_WAIT) && (gfp_mask & __GFP_FS)) {
1803 journal_wait_for_transaction_sync_data(journal);
1804 ret = try_to_free_buffers(page);
1805 }
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807busy:
1808 return ret;
1809}
1810
1811/*
1812 * This buffer is no longer needed. If it is on an older transaction's
1813 * checkpoint list we need to record it on this transaction's forget list
1814 * to pin this buffer (and hence its checkpointing transaction) down until
1815 * this transaction commits. If the buffer isn't on a checkpoint list, we
1816 * release it.
1817 * Returns non-zero if JBD no longer has an interest in the buffer.
1818 *
1819 * Called under j_list_lock.
1820 *
1821 * Called under jbd_lock_bh_state(bh).
1822 */
1823static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1824{
1825 int may_free = 1;
1826 struct buffer_head *bh = jh2bh(jh);
1827
1828 __journal_unfile_buffer(jh);
1829
1830 if (jh->b_cp_transaction) {
1831 JBUFFER_TRACE(jh, "on running+cp transaction");
1832 __journal_file_buffer(jh, transaction, BJ_Forget);
1833 clear_buffer_jbddirty(bh);
1834 may_free = 0;
1835 } else {
1836 JBUFFER_TRACE(jh, "on running transaction");
1837 journal_remove_journal_head(bh);
1838 __brelse(bh);
1839 }
1840 return may_free;
1841}
1842
1843/*
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001844 * journal_invalidatepage
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 *
1846 * This code is tricky. It has a number of cases to deal with.
1847 *
1848 * There are two invariants which this code relies on:
1849 *
1850 * i_size must be updated on disk before we start calling invalidatepage on the
1851 * data.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001852 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * This is done in ext3 by defining an ext3_setattr method which
1854 * updates i_size before truncate gets going. By maintaining this
1855 * invariant, we can be sure that it is safe to throw away any buffers
1856 * attached to the current transaction: once the transaction commits,
1857 * we know that the data will not be needed.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001858 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * Note however that we can *not* throw away data belonging to the
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001860 * previous, committing transaction!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 *
1862 * Any disk blocks which *are* part of the previous, committing
1863 * transaction (and which therefore cannot be discarded immediately) are
1864 * not going to be reused in the new running transaction
1865 *
1866 * The bitmap committed_data images guarantee this: any block which is
1867 * allocated in one transaction and removed in the next will be marked
1868 * as in-use in the committed_data bitmap, so cannot be reused until
1869 * the next transaction to delete the block commits. This means that
1870 * leaving committing buffers dirty is quite safe: the disk blocks
1871 * cannot be reallocated to a different file and so buffer aliasing is
1872 * not possible.
1873 *
1874 *
1875 * The above applies mainly to ordered data mode. In writeback mode we
1876 * don't make guarantees about the order in which data hits disk --- in
1877 * particular we don't guarantee that new dirty data is flushed before
1878 * transaction commit --- so it is always safe just to discard data
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001879 * immediately in that mode. --sct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
1881
1882/*
1883 * The journal_unmap_buffer helper function returns zero if the buffer
1884 * concerned remains pinned as an anonymous buffer belonging to an older
1885 * transaction.
1886 *
1887 * We're outside-transaction here. Either or both of j_running_transaction
1888 * and j_committing_transaction may be NULL.
1889 */
1890static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1891{
1892 transaction_t *transaction;
1893 struct journal_head *jh;
1894 int may_free = 1;
1895 int ret;
1896
1897 BUFFER_TRACE(bh, "entry");
1898
1899 /*
1900 * It is safe to proceed here without the j_list_lock because the
1901 * buffers cannot be stolen by try_to_free_buffers as long as we are
1902 * holding the page lock. --sct
1903 */
1904
1905 if (!buffer_jbd(bh))
1906 goto zap_buffer_unlocked;
1907
1908 spin_lock(&journal->j_state_lock);
1909 jbd_lock_bh_state(bh);
1910 spin_lock(&journal->j_list_lock);
1911
1912 jh = journal_grab_journal_head(bh);
1913 if (!jh)
1914 goto zap_buffer_no_jh;
1915
1916 transaction = jh->b_transaction;
1917 if (transaction == NULL) {
1918 /* First case: not on any transaction. If it
1919 * has no checkpoint link, then we can zap it:
1920 * it's a writeback-mode buffer so we don't care
1921 * if it hits disk safely. */
1922 if (!jh->b_cp_transaction) {
1923 JBUFFER_TRACE(jh, "not on any transaction: zap");
1924 goto zap_buffer;
1925 }
1926
1927 if (!buffer_dirty(bh)) {
1928 /* bdflush has written it. We can drop it now */
1929 goto zap_buffer;
1930 }
1931
1932 /* OK, it must be in the journal but still not
1933 * written fully to disk: it's metadata or
1934 * journaled data... */
1935
1936 if (journal->j_running_transaction) {
1937 /* ... and once the current transaction has
1938 * committed, the buffer won't be needed any
1939 * longer. */
1940 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1941 ret = __dispose_buffer(jh,
1942 journal->j_running_transaction);
1943 journal_put_journal_head(jh);
1944 spin_unlock(&journal->j_list_lock);
1945 jbd_unlock_bh_state(bh);
1946 spin_unlock(&journal->j_state_lock);
1947 return ret;
1948 } else {
1949 /* There is no currently-running transaction. So the
1950 * orphan record which we wrote for this file must have
1951 * passed into commit. We must attach this buffer to
1952 * the committing transaction, if it exists. */
1953 if (journal->j_committing_transaction) {
1954 JBUFFER_TRACE(jh, "give to committing trans");
1955 ret = __dispose_buffer(jh,
1956 journal->j_committing_transaction);
1957 journal_put_journal_head(jh);
1958 spin_unlock(&journal->j_list_lock);
1959 jbd_unlock_bh_state(bh);
1960 spin_unlock(&journal->j_state_lock);
1961 return ret;
1962 } else {
1963 /* The orphan record's transaction has
1964 * committed. We can cleanse this buffer */
1965 clear_buffer_jbddirty(bh);
1966 goto zap_buffer;
1967 }
1968 }
1969 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeenf58a74d2006-10-28 10:38:27 -07001970 JBUFFER_TRACE(jh, "on committing transaction");
akpm@osdl.orgd13df842005-04-16 15:26:36 -07001971 if (jh->b_jlist == BJ_Locked) {
1972 /*
1973 * The buffer is on the committing transaction's locked
1974 * list. We have the buffer locked, so I/O has
1975 * completed. So we can nail the buffer now.
1976 */
1977 may_free = __dispose_buffer(jh, transaction);
1978 goto zap_buffer;
1979 }
1980 /*
1981 * If it is committing, we simply cannot touch it. We
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 * can remove it's next_transaction pointer from the
1983 * running transaction if that is set, but nothing
1984 * else. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 set_buffer_freed(bh);
1986 if (jh->b_next_transaction) {
1987 J_ASSERT(jh->b_next_transaction ==
1988 journal->j_running_transaction);
1989 jh->b_next_transaction = NULL;
1990 }
1991 journal_put_journal_head(jh);
1992 spin_unlock(&journal->j_list_lock);
1993 jbd_unlock_bh_state(bh);
1994 spin_unlock(&journal->j_state_lock);
1995 return 0;
1996 } else {
1997 /* Good, the buffer belongs to the running transaction.
1998 * We are writing our own transaction's data, not any
1999 * previous one's, so it is safe to throw it away
2000 * (remember that we expect the filesystem to have set
2001 * i_size already for this truncate so recovery will not
2002 * expose the disk blocks we are discarding here.) */
2003 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeenf58a74d2006-10-28 10:38:27 -07002004 JBUFFER_TRACE(jh, "on running transaction");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 may_free = __dispose_buffer(jh, transaction);
2006 }
2007
2008zap_buffer:
2009 journal_put_journal_head(jh);
2010zap_buffer_no_jh:
2011 spin_unlock(&journal->j_list_lock);
2012 jbd_unlock_bh_state(bh);
2013 spin_unlock(&journal->j_state_lock);
2014zap_buffer_unlocked:
2015 clear_buffer_dirty(bh);
2016 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2017 clear_buffer_mapped(bh);
2018 clear_buffer_req(bh);
2019 clear_buffer_new(bh);
2020 bh->b_bdev = NULL;
2021 return may_free;
2022}
2023
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002024/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002025 * void journal_invalidatepage() - invalidate a journal page
2026 * @journal: journal to use for flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * @page: page to flush
2028 * @offset: length of page to invalidate.
2029 *
2030 * Reap page buffers containing data after offset in page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 */
NeilBrown2ff28e22006-03-26 01:37:18 -08002032void journal_invalidatepage(journal_t *journal,
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002033 struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 unsigned long offset)
2035{
2036 struct buffer_head *head, *bh, *next;
2037 unsigned int curr_off = 0;
2038 int may_free = 1;
2039
2040 if (!PageLocked(page))
2041 BUG();
2042 if (!page_has_buffers(page))
NeilBrown2ff28e22006-03-26 01:37:18 -08002043 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 /* We will potentially be playing with lists other than just the
2046 * data lists (especially for journaled data mode), so be
2047 * cautious in our locking. */
2048
2049 head = bh = page_buffers(page);
2050 do {
2051 unsigned int next_off = curr_off + bh->b_size;
2052 next = bh->b_this_page;
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (offset <= curr_off) {
Dave Kleikampe9ad5622006-09-27 01:49:35 -07002055 /* This block is wholly outside the truncation point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 lock_buffer(bh);
2057 may_free &= journal_unmap_buffer(journal, bh);
2058 unlock_buffer(bh);
2059 }
2060 curr_off = next_off;
2061 bh = next;
2062
2063 } while (bh != head);
2064
2065 if (!offset) {
NeilBrown2ff28e22006-03-26 01:37:18 -08002066 if (may_free && try_to_free_buffers(page))
2067 J_ASSERT(!page_has_buffers(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002071/*
2072 * File a buffer on the given transaction list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 */
2074void __journal_file_buffer(struct journal_head *jh,
2075 transaction_t *transaction, int jlist)
2076{
2077 struct journal_head **list = NULL;
2078 int was_dirty = 0;
2079 struct buffer_head *bh = jh2bh(jh);
2080
2081 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2082 assert_spin_locked(&transaction->t_journal->j_list_lock);
2083
2084 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2085 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002086 jh->b_transaction == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (jh->b_transaction && jh->b_jlist == jlist)
2089 return;
2090
2091 /* The following list of buffer states needs to be consistent
2092 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
2093 * state. */
2094
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002095 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 jlist == BJ_Shadow || jlist == BJ_Forget) {
2097 if (test_clear_buffer_dirty(bh) ||
2098 test_clear_buffer_jbddirty(bh))
2099 was_dirty = 1;
2100 }
2101
2102 if (jh->b_transaction)
2103 __journal_temp_unlink_buffer(jh);
2104 jh->b_transaction = transaction;
2105
2106 switch (jlist) {
2107 case BJ_None:
2108 J_ASSERT_JH(jh, !jh->b_committed_data);
2109 J_ASSERT_JH(jh, !jh->b_frozen_data);
2110 return;
2111 case BJ_SyncData:
2112 list = &transaction->t_sync_datalist;
2113 break;
2114 case BJ_Metadata:
2115 transaction->t_nr_buffers++;
2116 list = &transaction->t_buffers;
2117 break;
2118 case BJ_Forget:
2119 list = &transaction->t_forget;
2120 break;
2121 case BJ_IO:
2122 list = &transaction->t_iobuf_list;
2123 break;
2124 case BJ_Shadow:
2125 list = &transaction->t_shadow_list;
2126 break;
2127 case BJ_LogCtl:
2128 list = &transaction->t_log_list;
2129 break;
2130 case BJ_Reserved:
2131 list = &transaction->t_reserved_list;
2132 break;
2133 case BJ_Locked:
2134 list = &transaction->t_locked_list;
2135 break;
2136 }
2137
2138 __blist_add_buffer(list, jh);
2139 jh->b_jlist = jlist;
2140
2141 if (was_dirty)
2142 set_buffer_jbddirty(bh);
2143}
2144
2145void journal_file_buffer(struct journal_head *jh,
2146 transaction_t *transaction, int jlist)
2147{
2148 jbd_lock_bh_state(jh2bh(jh));
2149 spin_lock(&transaction->t_journal->j_list_lock);
2150 __journal_file_buffer(jh, transaction, jlist);
2151 spin_unlock(&transaction->t_journal->j_list_lock);
2152 jbd_unlock_bh_state(jh2bh(jh));
2153}
2154
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 * Remove a buffer from its current buffer list in preparation for
2157 * dropping it from its current transaction entirely. If the buffer has
2158 * already started to be used by a subsequent transaction, refile the
2159 * buffer on that transaction's metadata list.
2160 *
2161 * Called under journal->j_list_lock
2162 *
2163 * Called under jbd_lock_bh_state(jh2bh(jh))
2164 */
2165void __journal_refile_buffer(struct journal_head *jh)
2166{
2167 int was_dirty;
2168 struct buffer_head *bh = jh2bh(jh);
2169
2170 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2171 if (jh->b_transaction)
2172 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2173
2174 /* If the buffer is now unused, just drop it. */
2175 if (jh->b_next_transaction == NULL) {
2176 __journal_unfile_buffer(jh);
2177 return;
2178 }
2179
2180 /*
2181 * It has been modified by a later transaction: add it to the new
2182 * transaction's metadata list.
2183 */
2184
2185 was_dirty = test_clear_buffer_jbddirty(bh);
2186 __journal_temp_unlink_buffer(jh);
2187 jh->b_transaction = jh->b_next_transaction;
2188 jh->b_next_transaction = NULL;
Jan Kara9ada7342006-06-23 02:05:25 -07002189 __journal_file_buffer(jh, jh->b_transaction,
Josef Bacik5b9a4992008-04-28 02:16:12 -07002190 jh->b_modified ? BJ_Metadata : BJ_Reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2192
2193 if (was_dirty)
2194 set_buffer_jbddirty(bh);
2195}
2196
2197/*
2198 * For the unlocked version of this call, also make sure that any
2199 * hanging journal_head is cleaned up if necessary.
2200 *
2201 * __journal_refile_buffer is usually called as part of a single locked
2202 * operation on a buffer_head, in which the caller is probably going to
2203 * be hooking the journal_head onto other lists. In that case it is up
2204 * to the caller to remove the journal_head if necessary. For the
2205 * unlocked journal_refile_buffer call, the caller isn't going to be
2206 * doing anything else to the buffer so we need to do the cleanup
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002207 * ourselves to avoid a jh leak.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 *
2209 * *** The journal_head may be freed by this call! ***
2210 */
2211void journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2212{
2213 struct buffer_head *bh = jh2bh(jh);
2214
2215 jbd_lock_bh_state(bh);
2216 spin_lock(&journal->j_list_lock);
2217
2218 __journal_refile_buffer(jh);
2219 jbd_unlock_bh_state(bh);
2220 journal_remove_journal_head(bh);
2221
2222 spin_unlock(&journal->j_list_lock);
2223 __brelse(bh);
2224}