]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/jbd2/transaction.c
ext4: Add mount options in superblock
[net-next-2.6.git] / fs / jbd2 / transaction.c
CommitLineData
470decc6 1/*
58862699 2 * linux/fs/jbd2/transaction.c
470decc6
DK
3 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem transaction handling code; part of the ext2fs
13 * journaling system.
14 *
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>
f7f4bccb 22#include <linux/jbd2.h>
470decc6
DK
23#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
470decc6
DK
26#include <linux/mm.h>
27#include <linux/highmem.h>
e07f7183 28#include <linux/hrtimer.h>
47def826
TT
29#include <linux/backing-dev.h>
30#include <linux/module.h>
470decc6 31
7ddae860
AB
32static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
33
470decc6 34/*
f7f4bccb 35 * jbd2_get_transaction: obtain a new transaction_t object.
470decc6
DK
36 *
37 * Simply allocate and initialise a new transaction. Create it in
38 * RUNNING state and add it to the current journal (which should not
39 * have an existing running transaction: we only make a new transaction
40 * once we have started to commit the old one).
41 *
42 * Preconditions:
43 * The journal MUST be locked. We don't perform atomic mallocs on the
44 * new transaction and we can't block without protecting against other
45 * processes trying to touch the journal while it is in transition.
46 *
470decc6
DK
47 */
48
49static transaction_t *
f7f4bccb 50jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
470decc6
DK
51{
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
e07f7183 54 transaction->t_start_time = ktime_get();
470decc6
DK
55 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires = jiffies + journal->j_commit_interval;
57 spin_lock_init(&transaction->t_handle_lock);
c851ed54 58 INIT_LIST_HEAD(&transaction->t_inode_list);
3e624fc7 59 INIT_LIST_HEAD(&transaction->t_private_list);
470decc6
DK
60
61 /* Set up the commit timer for the new transaction. */
b1f485f2 62 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
470decc6
DK
63 add_timer(&journal->j_commit_timer);
64
65 J_ASSERT(journal->j_running_transaction == NULL);
66 journal->j_running_transaction = transaction;
8e85fb3f
JL
67 transaction->t_max_wait = 0;
68 transaction->t_start = jiffies;
470decc6
DK
69
70 return transaction;
71}
72
73/*
74 * Handle management.
75 *
76 * A handle_t is an object which represents a single atomic update to a
77 * filesystem, and which tracks all of the modifications which form part
78 * of that one update.
79 */
80
81/*
82 * start_this_handle: Given a handle, deal with any locking or stalling
83 * needed to make sure that there is enough journal space for the handle
84 * to begin. Attach the handle to a transaction and set up the
85 * transaction's buffer credits.
86 */
87
47def826
TT
88static int start_this_handle(journal_t *journal, handle_t *handle,
89 int gfp_mask)
470decc6
DK
90{
91 transaction_t *transaction;
92 int needed;
93 int nblocks = handle->h_buffer_credits;
94 transaction_t *new_transaction = NULL;
8e85fb3f 95 unsigned long ts = jiffies;
470decc6
DK
96
97 if (nblocks > journal->j_max_transaction_buffers) {
98 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
99 current->comm, nblocks,
100 journal->j_max_transaction_buffers);
47def826 101 return -ENOSPC;
470decc6
DK
102 }
103
104alloc_transaction:
105 if (!journal->j_running_transaction) {
47def826 106 new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
470decc6 107 if (!new_transaction) {
47def826
TT
108 /*
109 * If __GFP_FS is not present, then we may be
110 * being called from inside the fs writeback
111 * layer, so we MUST NOT fail. Since
112 * __GFP_NOFAIL is going away, we will arrange
113 * to retry the allocation ourselves.
114 */
115 if ((gfp_mask & __GFP_FS) == 0) {
116 congestion_wait(BLK_RW_ASYNC, HZ/50);
117 goto alloc_transaction;
118 }
119 return -ENOMEM;
470decc6 120 }
470decc6
DK
121 }
122
123 jbd_debug(3, "New handle %p going live.\n", handle);
124
125repeat:
126
127 /*
128 * We need to hold j_state_lock until t_updates has been incremented,
129 * for proper journal barrier handling
130 */
131 spin_lock(&journal->j_state_lock);
132repeat_locked:
133 if (is_journal_aborted(journal) ||
f7f4bccb 134 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
470decc6 135 spin_unlock(&journal->j_state_lock);
47def826
TT
136 kfree(new_transaction);
137 return -EROFS;
470decc6
DK
138 }
139
140 /* Wait on the journal's transaction barrier if necessary */
141 if (journal->j_barrier_count) {
142 spin_unlock(&journal->j_state_lock);
143 wait_event(journal->j_wait_transaction_locked,
144 journal->j_barrier_count == 0);
145 goto repeat;
146 }
147
148 if (!journal->j_running_transaction) {
149 if (!new_transaction) {
150 spin_unlock(&journal->j_state_lock);
151 goto alloc_transaction;
152 }
f7f4bccb 153 jbd2_get_transaction(journal, new_transaction);
470decc6
DK
154 new_transaction = NULL;
155 }
156
157 transaction = journal->j_running_transaction;
158
159 /*
160 * If the current transaction is locked down for commit, wait for the
161 * lock to be released.
162 */
163 if (transaction->t_state == T_LOCKED) {
164 DEFINE_WAIT(wait);
165
166 prepare_to_wait(&journal->j_wait_transaction_locked,
167 &wait, TASK_UNINTERRUPTIBLE);
168 spin_unlock(&journal->j_state_lock);
169 schedule();
170 finish_wait(&journal->j_wait_transaction_locked, &wait);
171 goto repeat;
172 }
173
174 /*
175 * If there is not enough space left in the log to write all potential
176 * buffers requested by this operation, we need to stall pending a log
177 * checkpoint to free some more log space.
178 */
179 spin_lock(&transaction->t_handle_lock);
180 needed = transaction->t_outstanding_credits + nblocks;
181
182 if (needed > journal->j_max_transaction_buffers) {
183 /*
184 * If the current transaction is already too large, then start
185 * to commit it: we can then go back and attach this handle to
186 * a new transaction.
187 */
188 DEFINE_WAIT(wait);
189
190 jbd_debug(2, "Handle %p starting new commit...\n", handle);
191 spin_unlock(&transaction->t_handle_lock);
192 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
193 TASK_UNINTERRUPTIBLE);
f7f4bccb 194 __jbd2_log_start_commit(journal, transaction->t_tid);
470decc6
DK
195 spin_unlock(&journal->j_state_lock);
196 schedule();
197 finish_wait(&journal->j_wait_transaction_locked, &wait);
198 goto repeat;
199 }
200
201 /*
202 * The commit code assumes that it can get enough log space
203 * without forcing a checkpoint. This is *critical* for
204 * correctness: a checkpoint of a buffer which is also
205 * associated with a committing transaction creates a deadlock,
206 * so commit simply cannot force through checkpoints.
207 *
208 * We must therefore ensure the necessary space in the journal
209 * *before* starting to dirty potentially checkpointed buffers
210 * in the new transaction.
211 *
212 * The worst part is, any transaction currently committing can
213 * reduce the free space arbitrarily. Be careful to account for
214 * those buffers when checkpointing.
215 */
216
217 /*
218 * @@@ AKPM: This seems rather over-defensive. We're giving commit
219 * a _lot_ of headroom: 1/4 of the journal plus the size of
220 * the committing transaction. Really, we only need to give it
221 * committing_transaction->t_outstanding_credits plus "enough" for
222 * the log control blocks.
223 * Also, this test is inconsitent with the matching one in
f7f4bccb 224 * jbd2_journal_extend().
470decc6 225 */
f7f4bccb 226 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
470decc6
DK
227 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
228 spin_unlock(&transaction->t_handle_lock);
f7f4bccb 229 __jbd2_log_wait_for_space(journal);
470decc6
DK
230 goto repeat_locked;
231 }
232
233 /* OK, account for the buffers that this operation expects to
234 * use and add the handle to the running transaction. */
235
8e85fb3f
JL
236 if (time_after(transaction->t_start, ts)) {
237 ts = jbd2_time_diff(ts, transaction->t_start);
238 if (ts > transaction->t_max_wait)
239 transaction->t_max_wait = ts;
240 }
241
470decc6
DK
242 handle->h_transaction = transaction;
243 transaction->t_outstanding_credits += nblocks;
244 transaction->t_updates++;
245 transaction->t_handle_count++;
246 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
247 handle, nblocks, transaction->t_outstanding_credits,
f7f4bccb 248 __jbd2_log_space_left(journal));
470decc6
DK
249 spin_unlock(&transaction->t_handle_lock);
250 spin_unlock(&journal->j_state_lock);
9599b0e5
JK
251
252 lock_map_acquire(&handle->h_lockdep_map);
47def826
TT
253 kfree(new_transaction);
254 return 0;
470decc6
DK
255}
256
7b751066
MC
257static struct lock_class_key jbd2_handle_key;
258
470decc6
DK
259/* Allocate a new handle. This should probably be in a slab... */
260static handle_t *new_handle(int nblocks)
261{
af1e76d6 262 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
470decc6
DK
263 if (!handle)
264 return NULL;
265 memset(handle, 0, sizeof(*handle));
266 handle->h_buffer_credits = nblocks;
267 handle->h_ref = 1;
268
7b751066
MC
269 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
270 &jbd2_handle_key, 0);
271
470decc6
DK
272 return handle;
273}
274
275/**
f7f4bccb 276 * handle_t *jbd2_journal_start() - Obtain a new handle.
470decc6
DK
277 * @journal: Journal to start transaction on.
278 * @nblocks: number of block buffer we might modify
279 *
280 * We make sure that the transaction can guarantee at least nblocks of
281 * modified buffers in the log. We block until the log can guarantee
282 * that much space.
283 *
284 * This function is visible to journal users (like ext3fs), so is not
285 * called with the journal already locked.
286 *
287 * Return a pointer to a newly allocated handle, or NULL on failure
288 */
47def826 289handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
470decc6
DK
290{
291 handle_t *handle = journal_current_handle();
292 int err;
293
294 if (!journal)
295 return ERR_PTR(-EROFS);
296
297 if (handle) {
298 J_ASSERT(handle->h_transaction->t_journal == journal);
299 handle->h_ref++;
300 return handle;
301 }
302
303 handle = new_handle(nblocks);
304 if (!handle)
305 return ERR_PTR(-ENOMEM);
306
307 current->journal_info = handle;
308
47def826 309 err = start_this_handle(journal, handle, gfp_mask);
470decc6 310 if (err < 0) {
af1e76d6 311 jbd2_free_handle(handle);
470decc6
DK
312 current->journal_info = NULL;
313 handle = ERR_PTR(err);
7b751066 314 goto out;
470decc6 315 }
7b751066 316out:
470decc6
DK
317 return handle;
318}
47def826
TT
319EXPORT_SYMBOL(jbd2__journal_start);
320
321
322handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
323{
324 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
325}
326EXPORT_SYMBOL(jbd2_journal_start);
327
470decc6
DK
328
329/**
f7f4bccb 330 * int jbd2_journal_extend() - extend buffer credits.
470decc6
DK
331 * @handle: handle to 'extend'
332 * @nblocks: nr blocks to try to extend by.
333 *
334 * Some transactions, such as large extends and truncates, can be done
335 * atomically all at once or in several stages. The operation requests
336 * a credit for a number of buffer modications in advance, but can
337 * extend its credit if it needs more.
338 *
f7f4bccb 339 * jbd2_journal_extend tries to give the running handle more buffer credits.
470decc6
DK
340 * It does not guarantee that allocation - this is a best-effort only.
341 * The calling process MUST be able to deal cleanly with a failure to
342 * extend here.
343 *
344 * Return 0 on success, non-zero on failure.
345 *
346 * return code < 0 implies an error
347 * return code > 0 implies normal transaction-full status.
348 */
f7f4bccb 349int jbd2_journal_extend(handle_t *handle, int nblocks)
470decc6
DK
350{
351 transaction_t *transaction = handle->h_transaction;
352 journal_t *journal = transaction->t_journal;
353 int result;
354 int wanted;
355
356 result = -EIO;
357 if (is_handle_aborted(handle))
358 goto out;
359
360 result = 1;
361
362 spin_lock(&journal->j_state_lock);
363
364 /* Don't extend a locked-down transaction! */
365 if (handle->h_transaction->t_state != T_RUNNING) {
366 jbd_debug(3, "denied handle %p %d blocks: "
367 "transaction not running\n", handle, nblocks);
368 goto error_out;
369 }
370
371 spin_lock(&transaction->t_handle_lock);
372 wanted = transaction->t_outstanding_credits + nblocks;
373
374 if (wanted > journal->j_max_transaction_buffers) {
375 jbd_debug(3, "denied handle %p %d blocks: "
376 "transaction too large\n", handle, nblocks);
377 goto unlock;
378 }
379
f7f4bccb 380 if (wanted > __jbd2_log_space_left(journal)) {
470decc6
DK
381 jbd_debug(3, "denied handle %p %d blocks: "
382 "insufficient log space\n", handle, nblocks);
383 goto unlock;
384 }
385
386 handle->h_buffer_credits += nblocks;
387 transaction->t_outstanding_credits += nblocks;
388 result = 0;
389
390 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
391unlock:
392 spin_unlock(&transaction->t_handle_lock);
393error_out:
394 spin_unlock(&journal->j_state_lock);
395out:
396 return result;
397}
398
399
400/**
f7f4bccb 401 * int jbd2_journal_restart() - restart a handle .
470decc6
DK
402 * @handle: handle to restart
403 * @nblocks: nr credits requested
404 *
405 * Restart a handle for a multi-transaction filesystem
406 * operation.
407 *
f7f4bccb
MC
408 * If the jbd2_journal_extend() call above fails to grant new buffer credits
409 * to a running handle, a call to jbd2_journal_restart will commit the
470decc6
DK
410 * handle's transaction so far and reattach the handle to a new
411 * transaction capabable of guaranteeing the requested number of
412 * credits.
413 */
47def826 414int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
470decc6
DK
415{
416 transaction_t *transaction = handle->h_transaction;
417 journal_t *journal = transaction->t_journal;
418 int ret;
419
420 /* If we've had an abort of any type, don't even think about
421 * actually doing the restart! */
422 if (is_handle_aborted(handle))
423 return 0;
424
425 /*
426 * First unlink the handle from its current transaction, and start the
427 * commit on that.
428 */
429 J_ASSERT(transaction->t_updates > 0);
430 J_ASSERT(journal_current_handle() == handle);
431
432 spin_lock(&journal->j_state_lock);
433 spin_lock(&transaction->t_handle_lock);
434 transaction->t_outstanding_credits -= handle->h_buffer_credits;
435 transaction->t_updates--;
436
437 if (!transaction->t_updates)
438 wake_up(&journal->j_wait_updates);
439 spin_unlock(&transaction->t_handle_lock);
440
441 jbd_debug(2, "restarting handle %p\n", handle);
f7f4bccb 442 __jbd2_log_start_commit(journal, transaction->t_tid);
470decc6
DK
443 spin_unlock(&journal->j_state_lock);
444
9599b0e5 445 lock_map_release(&handle->h_lockdep_map);
470decc6 446 handle->h_buffer_credits = nblocks;
47def826 447 ret = start_this_handle(journal, handle, gfp_mask);
470decc6
DK
448 return ret;
449}
47def826 450EXPORT_SYMBOL(jbd2__journal_restart);
470decc6
DK
451
452
47def826
TT
453int jbd2_journal_restart(handle_t *handle, int nblocks)
454{
455 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
456}
457EXPORT_SYMBOL(jbd2_journal_restart);
458
470decc6 459/**
f7f4bccb 460 * void jbd2_journal_lock_updates () - establish a transaction barrier.
470decc6
DK
461 * @journal: Journal to establish a barrier on.
462 *
463 * This locks out any further updates from being started, and blocks
464 * until all existing updates have completed, returning only once the
465 * journal is in a quiescent state with no updates running.
466 *
467 * The journal lock should not be held on entry.
468 */
f7f4bccb 469void jbd2_journal_lock_updates(journal_t *journal)
470decc6
DK
470{
471 DEFINE_WAIT(wait);
472
473 spin_lock(&journal->j_state_lock);
474 ++journal->j_barrier_count;
475
476 /* Wait until there are no running updates */
477 while (1) {
478 transaction_t *transaction = journal->j_running_transaction;
479
480 if (!transaction)
481 break;
482
483 spin_lock(&transaction->t_handle_lock);
484 if (!transaction->t_updates) {
485 spin_unlock(&transaction->t_handle_lock);
486 break;
487 }
488 prepare_to_wait(&journal->j_wait_updates, &wait,
489 TASK_UNINTERRUPTIBLE);
490 spin_unlock(&transaction->t_handle_lock);
491 spin_unlock(&journal->j_state_lock);
492 schedule();
493 finish_wait(&journal->j_wait_updates, &wait);
494 spin_lock(&journal->j_state_lock);
495 }
496 spin_unlock(&journal->j_state_lock);
497
498 /*
499 * We have now established a barrier against other normal updates, but
f7f4bccb 500 * we also need to barrier against other jbd2_journal_lock_updates() calls
470decc6
DK
501 * to make sure that we serialise special journal-locked operations
502 * too.
503 */
504 mutex_lock(&journal->j_barrier);
505}
506
507/**
f7f4bccb 508 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
470decc6
DK
509 * @journal: Journal to release the barrier on.
510 *
f7f4bccb 511 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
470decc6
DK
512 *
513 * Should be called without the journal lock held.
514 */
f7f4bccb 515void jbd2_journal_unlock_updates (journal_t *journal)
470decc6
DK
516{
517 J_ASSERT(journal->j_barrier_count != 0);
518
519 mutex_unlock(&journal->j_barrier);
520 spin_lock(&journal->j_state_lock);
521 --journal->j_barrier_count;
522 spin_unlock(&journal->j_state_lock);
523 wake_up(&journal->j_wait_transaction_locked);
524}
525
f91d1d04 526static void warn_dirty_buffer(struct buffer_head *bh)
470decc6 527{
f91d1d04 528 char b[BDEVNAME_SIZE];
470decc6 529
f91d1d04
JK
530 printk(KERN_WARNING
531 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
532 "There's a risk of filesystem corruption in case of system "
533 "crash.\n",
534 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
470decc6
DK
535}
536
537/*
538 * If the buffer is already part of the current transaction, then there
539 * is nothing we need to do. If it is already part of a prior
540 * transaction which we are still committing to disk, then we need to
541 * make sure that we do not overwrite the old copy: we do copy-out to
542 * preserve the copy going to disk. We also account the buffer against
543 * the handle's metadata buffer credits (unless the buffer is already
544 * part of the transaction, that is).
545 *
546 */
547static int
548do_get_write_access(handle_t *handle, struct journal_head *jh,
549 int force_copy)
550{
551 struct buffer_head *bh;
552 transaction_t *transaction;
553 journal_t *journal;
554 int error;
555 char *frozen_buffer = NULL;
556 int need_copy = 0;
557
558 if (is_handle_aborted(handle))
559 return -EROFS;
560
561 transaction = handle->h_transaction;
562 journal = transaction->t_journal;
563
564 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
565
566 JBUFFER_TRACE(jh, "entry");
567repeat:
568 bh = jh2bh(jh);
569
570 /* @@@ Need to check for errors here at some point. */
571
572 lock_buffer(bh);
573 jbd_lock_bh_state(bh);
574
575 /* We now hold the buffer lock so it is safe to query the buffer
576 * state. Is the buffer dirty?
577 *
578 * If so, there are two possibilities. The buffer may be
579 * non-journaled, and undergoing a quite legitimate writeback.
580 * Otherwise, it is journaled, and we don't expect dirty buffers
581 * in that state (the buffers should be marked JBD_Dirty
582 * instead.) So either the IO is being done under our own
583 * control and this is a bug, or it's a third party IO such as
584 * dump(8) (which may leave the buffer scheduled for read ---
585 * ie. locked but not dirty) or tune2fs (which may actually have
586 * the buffer dirtied, ugh.) */
587
588 if (buffer_dirty(bh)) {
589 /*
590 * First question: is this buffer already part of the current
591 * transaction or the existing committing transaction?
592 */
593 if (jh->b_transaction) {
594 J_ASSERT_JH(jh,
595 jh->b_transaction == transaction ||
596 jh->b_transaction ==
597 journal->j_committing_transaction);
598 if (jh->b_next_transaction)
599 J_ASSERT_JH(jh, jh->b_next_transaction ==
600 transaction);
f91d1d04 601 warn_dirty_buffer(bh);
470decc6
DK
602 }
603 /*
604 * In any case we need to clean the dirty flag and we must
605 * do it under the buffer lock to be sure we don't race
606 * with running write-out.
607 */
f91d1d04
JK
608 JBUFFER_TRACE(jh, "Journalling dirty buffer");
609 clear_buffer_dirty(bh);
610 set_buffer_jbddirty(bh);
470decc6
DK
611 }
612
613 unlock_buffer(bh);
614
615 error = -EROFS;
616 if (is_handle_aborted(handle)) {
617 jbd_unlock_bh_state(bh);
618 goto out;
619 }
620 error = 0;
621
622 /*
623 * The buffer is already part of this transaction if b_transaction or
624 * b_next_transaction points to it
625 */
626 if (jh->b_transaction == transaction ||
627 jh->b_next_transaction == transaction)
628 goto done;
629
9fc7c63a
JB
630 /*
631 * this is the first time this transaction is touching this buffer,
632 * reset the modified flag
633 */
634 jh->b_modified = 0;
635
470decc6
DK
636 /*
637 * If there is already a copy-out version of this buffer, then we don't
638 * need to make another one
639 */
640 if (jh->b_frozen_data) {
641 JBUFFER_TRACE(jh, "has frozen data");
642 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
643 jh->b_next_transaction = transaction;
644 goto done;
645 }
646
647 /* Is there data here we need to preserve? */
648
649 if (jh->b_transaction && jh->b_transaction != transaction) {
650 JBUFFER_TRACE(jh, "owned by older transaction");
651 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
652 J_ASSERT_JH(jh, jh->b_transaction ==
653 journal->j_committing_transaction);
654
655 /* There is one case we have to be very careful about.
656 * If the committing transaction is currently writing
657 * this buffer out to disk and has NOT made a copy-out,
658 * then we cannot modify the buffer contents at all
659 * right now. The essence of copy-out is that it is the
660 * extra copy, not the primary copy, which gets
661 * journaled. If the primary copy is already going to
662 * disk then we cannot do copy-out here. */
663
664 if (jh->b_jlist == BJ_Shadow) {
665 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
666 wait_queue_head_t *wqh;
667
668 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
669
670 JBUFFER_TRACE(jh, "on shadow: sleep");
671 jbd_unlock_bh_state(bh);
672 /* commit wakes up all shadow buffers after IO */
673 for ( ; ; ) {
674 prepare_to_wait(wqh, &wait.wait,
675 TASK_UNINTERRUPTIBLE);
676 if (jh->b_jlist != BJ_Shadow)
677 break;
678 schedule();
679 }
680 finish_wait(wqh, &wait.wait);
681 goto repeat;
682 }
683
684 /* Only do the copy if the currently-owning transaction
685 * still needs it. If it is on the Forget list, the
686 * committing transaction is past that stage. The
687 * buffer had better remain locked during the kmalloc,
688 * but that should be true --- we hold the journal lock
689 * still and the buffer is already on the BUF_JOURNAL
690 * list so won't be flushed.
691 *
692 * Subtle point, though: if this is a get_undo_access,
693 * then we will be relying on the frozen_data to contain
694 * the new value of the committed_data record after the
695 * transaction, so we HAVE to force the frozen_data copy
696 * in that case. */
697
698 if (jh->b_jlist != BJ_Forget || force_copy) {
699 JBUFFER_TRACE(jh, "generate frozen data");
700 if (!frozen_buffer) {
701 JBUFFER_TRACE(jh, "allocate memory for buffer");
702 jbd_unlock_bh_state(bh);
703 frozen_buffer =
af1e76d6 704 jbd2_alloc(jh2bh(jh)->b_size,
470decc6
DK
705 GFP_NOFS);
706 if (!frozen_buffer) {
707 printk(KERN_EMERG
708 "%s: OOM for frozen_buffer\n",
329d291f 709 __func__);
470decc6
DK
710 JBUFFER_TRACE(jh, "oom!");
711 error = -ENOMEM;
712 jbd_lock_bh_state(bh);
713 goto done;
714 }
715 goto repeat;
716 }
717 jh->b_frozen_data = frozen_buffer;
718 frozen_buffer = NULL;
719 need_copy = 1;
720 }
721 jh->b_next_transaction = transaction;
722 }
723
724
725 /*
726 * Finally, if the buffer is not journaled right now, we need to make
727 * sure it doesn't get written to disk before the caller actually
728 * commits the new data
729 */
730 if (!jh->b_transaction) {
731 JBUFFER_TRACE(jh, "no transaction");
732 J_ASSERT_JH(jh, !jh->b_next_transaction);
733 jh->b_transaction = transaction;
734 JBUFFER_TRACE(jh, "file as BJ_Reserved");
735 spin_lock(&journal->j_list_lock);
f7f4bccb 736 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
470decc6
DK
737 spin_unlock(&journal->j_list_lock);
738 }
739
740done:
741 if (need_copy) {
742 struct page *page;
743 int offset;
744 char *source;
745
746 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
747 "Possible IO failure.\n");
748 page = jh2bh(jh)->b_page;
749 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
750 source = kmap_atomic(page, KM_USER0);
751 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
752 kunmap_atomic(source, KM_USER0);
e06c8227
JB
753
754 /*
755 * Now that the frozen data is saved off, we need to store
756 * any matching triggers.
757 */
758 jh->b_frozen_triggers = jh->b_triggers;
470decc6
DK
759 }
760 jbd_unlock_bh_state(bh);
761
762 /*
763 * If we are about to journal a buffer, then any revoke pending on it is
764 * no longer valid
765 */
f7f4bccb 766 jbd2_journal_cancel_revoke(handle, jh);
470decc6
DK
767
768out:
769 if (unlikely(frozen_buffer)) /* It's usually NULL */
af1e76d6 770 jbd2_free(frozen_buffer, bh->b_size);
470decc6
DK
771
772 JBUFFER_TRACE(jh, "exit");
773 return error;
774}
775
776/**
f7f4bccb 777 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
470decc6
DK
778 * @handle: transaction to add buffer modifications to
779 * @bh: bh to be used for metadata writes
780 * @credits: variable that will receive credits for the buffer
781 *
782 * Returns an error code or 0 on success.
783 *
784 * In full data journalling mode the buffer may be of type BJ_AsyncData,
785 * because we're write()ing a buffer which is also part of a shared mapping.
786 */
787
f7f4bccb 788int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
470decc6 789{
f7f4bccb 790 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
470decc6
DK
791 int rc;
792
793 /* We do not want to get caught playing with fields which the
794 * log thread also manipulates. Make sure that the buffer
795 * completes any outstanding IO before proceeding. */
796 rc = do_get_write_access(handle, jh, 0);
f7f4bccb 797 jbd2_journal_put_journal_head(jh);
470decc6
DK
798 return rc;
799}
800
801
802/*
803 * When the user wants to journal a newly created buffer_head
804 * (ie. getblk() returned a new buffer and we are going to populate it
805 * manually rather than reading off disk), then we need to keep the
806 * buffer_head locked until it has been completely filled with new
807 * data. In this case, we should be able to make the assertion that
808 * the bh is not already part of an existing transaction.
809 *
810 * The buffer should already be locked by the caller by this point.
811 * There is no lock ranking violation: it was a newly created,
812 * unlocked buffer beforehand. */
813
814/**
f7f4bccb 815 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
470decc6
DK
816 * @handle: transaction to new buffer to
817 * @bh: new buffer.
818 *
819 * Call this if you create a new bh.
820 */
f7f4bccb 821int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
470decc6
DK
822{
823 transaction_t *transaction = handle->h_transaction;
824 journal_t *journal = transaction->t_journal;
f7f4bccb 825 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
470decc6
DK
826 int err;
827
828 jbd_debug(5, "journal_head %p\n", jh);
829 err = -EROFS;
830 if (is_handle_aborted(handle))
831 goto out;
832 err = 0;
833
834 JBUFFER_TRACE(jh, "entry");
835 /*
836 * The buffer may already belong to this transaction due to pre-zeroing
837 * in the filesystem's new_block code. It may also be on the previous,
838 * committing transaction's lists, but it HAS to be in Forget state in
839 * that case: the transaction must have deleted the buffer for it to be
840 * reused here.
841 */
842 jbd_lock_bh_state(bh);
843 spin_lock(&journal->j_list_lock);
844 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
845 jh->b_transaction == NULL ||
846 (jh->b_transaction == journal->j_committing_transaction &&
847 jh->b_jlist == BJ_Forget)));
848
849 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
850 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
851
852 if (jh->b_transaction == NULL) {
f91d1d04
JK
853 /*
854 * Previous jbd2_journal_forget() could have left the buffer
855 * with jbddirty bit set because it was being committed. When
856 * the commit finished, we've filed the buffer for
857 * checkpointing and marked it dirty. Now we are reallocating
858 * the buffer so the transaction freeing it must have
859 * committed and so it's safe to clear the dirty bit.
860 */
861 clear_buffer_dirty(jh2bh(jh));
470decc6 862 jh->b_transaction = transaction;
9fc7c63a
JB
863
864 /* first access by this transaction */
865 jh->b_modified = 0;
866
470decc6 867 JBUFFER_TRACE(jh, "file as BJ_Reserved");
f7f4bccb 868 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
470decc6 869 } else if (jh->b_transaction == journal->j_committing_transaction) {
9fc7c63a
JB
870 /* first access by this transaction */
871 jh->b_modified = 0;
872
470decc6
DK
873 JBUFFER_TRACE(jh, "set next transaction");
874 jh->b_next_transaction = transaction;
875 }
876 spin_unlock(&journal->j_list_lock);
877 jbd_unlock_bh_state(bh);
878
879 /*
880 * akpm: I added this. ext3_alloc_branch can pick up new indirect
881 * blocks which contain freed but then revoked metadata. We need
882 * to cancel the revoke in case we end up freeing it yet again
883 * and the reallocating as data - this would cause a second revoke,
884 * which hits an assertion error.
885 */
886 JBUFFER_TRACE(jh, "cancelling revoke");
f7f4bccb
MC
887 jbd2_journal_cancel_revoke(handle, jh);
888 jbd2_journal_put_journal_head(jh);
470decc6
DK
889out:
890 return err;
891}
892
893/**
f7f4bccb 894 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
470decc6
DK
895 * non-rewindable consequences
896 * @handle: transaction
897 * @bh: buffer to undo
898 * @credits: store the number of taken credits here (if not NULL)
899 *
900 * Sometimes there is a need to distinguish between metadata which has
901 * been committed to disk and that which has not. The ext3fs code uses
902 * this for freeing and allocating space, we have to make sure that we
903 * do not reuse freed space until the deallocation has been committed,
904 * since if we overwrote that space we would make the delete
905 * un-rewindable in case of a crash.
906 *
f7f4bccb 907 * To deal with that, jbd2_journal_get_undo_access requests write access to a
470decc6
DK
908 * buffer for parts of non-rewindable operations such as delete
909 * operations on the bitmaps. The journaling code must keep a copy of
910 * the buffer's contents prior to the undo_access call until such time
911 * as we know that the buffer has definitely been committed to disk.
912 *
913 * We never need to know which transaction the committed data is part
914 * of, buffers touched here are guaranteed to be dirtied later and so
915 * will be committed to a new transaction in due course, at which point
916 * we can discard the old committed data pointer.
917 *
918 * Returns error number or 0 on success.
919 */
f7f4bccb 920int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
470decc6
DK
921{
922 int err;
f7f4bccb 923 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
470decc6
DK
924 char *committed_data = NULL;
925
926 JBUFFER_TRACE(jh, "entry");
927
928 /*
929 * Do this first --- it can drop the journal lock, so we want to
930 * make sure that obtaining the committed_data is done
931 * atomically wrt. completion of any outstanding commits.
932 */
933 err = do_get_write_access(handle, jh, 1);
934 if (err)
935 goto out;
936
937repeat:
938 if (!jh->b_committed_data) {
af1e76d6 939 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
470decc6
DK
940 if (!committed_data) {
941 printk(KERN_EMERG "%s: No memory for committed data\n",
329d291f 942 __func__);
470decc6
DK
943 err = -ENOMEM;
944 goto out;
945 }
946 }
947
948 jbd_lock_bh_state(bh);
949 if (!jh->b_committed_data) {
950 /* Copy out the current buffer contents into the
951 * preserved, committed copy. */
952 JBUFFER_TRACE(jh, "generate b_committed data");
953 if (!committed_data) {
954 jbd_unlock_bh_state(bh);
955 goto repeat;
956 }
957
958 jh->b_committed_data = committed_data;
959 committed_data = NULL;
960 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
961 }
962 jbd_unlock_bh_state(bh);
963out:
f7f4bccb 964 jbd2_journal_put_journal_head(jh);
470decc6 965 if (unlikely(committed_data))
af1e76d6 966 jbd2_free(committed_data, bh->b_size);
470decc6
DK
967 return err;
968}
969
e06c8227
JB
970/**
971 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
972 * @bh: buffer to trigger on
973 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
974 *
975 * Set any triggers on this journal_head. This is always safe, because
976 * triggers for a committing buffer will be saved off, and triggers for
977 * a running transaction will match the buffer in that transaction.
978 *
979 * Call with NULL to clear the triggers.
980 */
981void jbd2_journal_set_triggers(struct buffer_head *bh,
982 struct jbd2_buffer_trigger_type *type)
983{
984 struct journal_head *jh = bh2jh(bh);
985
986 jh->b_triggers = type;
987}
988
989void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
990 struct jbd2_buffer_trigger_type *triggers)
991{
992 struct buffer_head *bh = jh2bh(jh);
993
994 if (!triggers || !triggers->t_commit)
995 return;
996
997 triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
998}
999
1000void jbd2_buffer_abort_trigger(struct journal_head *jh,
1001 struct jbd2_buffer_trigger_type *triggers)
1002{
1003 if (!triggers || !triggers->t_abort)
1004 return;
1005
1006 triggers->t_abort(triggers, jh2bh(jh));
1007}
1008
1009
1010
470decc6 1011/**
f7f4bccb 1012 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
470decc6
DK
1013 * @handle: transaction to add buffer to.
1014 * @bh: buffer to mark
1015 *
1016 * mark dirty metadata which needs to be journaled as part of the current
1017 * transaction.
1018 *
1019 * The buffer is placed on the transaction's metadata list and is marked
1020 * as belonging to the transaction.
1021 *
1022 * Returns error number or 0 on success.
1023 *
1024 * Special care needs to be taken if the buffer already belongs to the
1025 * current committing transaction (in which case we should have frozen
1026 * data present for that commit). In that case, we don't relink the
1027 * buffer: that only gets done when the old transaction finally
1028 * completes its commit.
1029 */
f7f4bccb 1030int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
470decc6
DK
1031{
1032 transaction_t *transaction = handle->h_transaction;
1033 journal_t *journal = transaction->t_journal;
1034 struct journal_head *jh = bh2jh(bh);
1035
1036 jbd_debug(5, "journal_head %p\n", jh);
1037 JBUFFER_TRACE(jh, "entry");
1038 if (is_handle_aborted(handle))
1039 goto out;
1040
1041 jbd_lock_bh_state(bh);
1042
1043 if (jh->b_modified == 0) {
1044 /*
1045 * This buffer's got modified and becoming part
1046 * of the transaction. This needs to be done
1047 * once a transaction -bzzz
1048 */
1049 jh->b_modified = 1;
1050 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1051 handle->h_buffer_credits--;
1052 }
1053
1054 /*
1055 * fastpath, to avoid expensive locking. If this buffer is already
1056 * on the running transaction's metadata list there is nothing to do.
1057 * Nobody can take it off again because there is a handle open.
1058 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1059 * result in this test being false, so we go in and take the locks.
1060 */
1061 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1062 JBUFFER_TRACE(jh, "fastpath");
1063 J_ASSERT_JH(jh, jh->b_transaction ==
1064 journal->j_running_transaction);
1065 goto out_unlock_bh;
1066 }
1067
1068 set_buffer_jbddirty(bh);
1069
1070 /*
1071 * Metadata already on the current transaction list doesn't
1072 * need to be filed. Metadata on another transaction's list must
1073 * be committing, and will be refiled once the commit completes:
1074 * leave it alone for now.
1075 */
1076 if (jh->b_transaction != transaction) {
1077 JBUFFER_TRACE(jh, "already on other transaction");
1078 J_ASSERT_JH(jh, jh->b_transaction ==
1079 journal->j_committing_transaction);
1080 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1081 /* And this case is illegal: we can't reuse another
1082 * transaction's data buffer, ever. */
1083 goto out_unlock_bh;
1084 }
1085
1086 /* That test should have eliminated the following case: */
4019191b 1087 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
470decc6
DK
1088
1089 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1090 spin_lock(&journal->j_list_lock);
f7f4bccb 1091 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
470decc6
DK
1092 spin_unlock(&journal->j_list_lock);
1093out_unlock_bh:
1094 jbd_unlock_bh_state(bh);
1095out:
1096 JBUFFER_TRACE(jh, "exit");
1097 return 0;
1098}
1099
1100/*
f7f4bccb 1101 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
470decc6
DK
1102 * updates, if the update decided in the end that it didn't need access.
1103 *
1104 */
1105void
f7f4bccb 1106jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
470decc6
DK
1107{
1108 BUFFER_TRACE(bh, "entry");
1109}
1110
1111/**
f7f4bccb 1112 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
470decc6
DK
1113 * @handle: transaction handle
1114 * @bh: bh to 'forget'
1115 *
1116 * We can only do the bforget if there are no commits pending against the
1117 * buffer. If the buffer is dirty in the current running transaction we
1118 * can safely unlink it.
1119 *
1120 * bh may not be a journalled buffer at all - it may be a non-JBD
1121 * buffer which came off the hashtable. Check for this.
1122 *
1123 * Decrements bh->b_count by one.
1124 *
1125 * Allow this call even if the handle has aborted --- it may be part of
1126 * the caller's cleanup after an abort.
1127 */
f7f4bccb 1128int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
470decc6
DK
1129{
1130 transaction_t *transaction = handle->h_transaction;
1131 journal_t *journal = transaction->t_journal;
1132 struct journal_head *jh;
1133 int drop_reserve = 0;
1134 int err = 0;
1dfc3220 1135 int was_modified = 0;
470decc6
DK
1136
1137 BUFFER_TRACE(bh, "entry");
1138
1139 jbd_lock_bh_state(bh);
1140 spin_lock(&journal->j_list_lock);
1141
1142 if (!buffer_jbd(bh))
1143 goto not_jbd;
1144 jh = bh2jh(bh);
1145
1146 /* Critical error: attempting to delete a bitmap buffer, maybe?
1147 * Don't do any jbd operations, and return an error. */
1148 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1149 "inconsistent data on disk")) {
1150 err = -EIO;
1151 goto not_jbd;
1152 }
1153
1dfc3220
JB
1154 /* keep track of wether or not this transaction modified us */
1155 was_modified = jh->b_modified;
1156
470decc6
DK
1157 /*
1158 * The buffer's going from the transaction, we must drop
1159 * all references -bzzz
1160 */
1161 jh->b_modified = 0;
1162
1163 if (jh->b_transaction == handle->h_transaction) {
1164 J_ASSERT_JH(jh, !jh->b_frozen_data);
1165
1166 /* If we are forgetting a buffer which is already part
1167 * of this transaction, then we can just drop it from
1168 * the transaction immediately. */
1169 clear_buffer_dirty(bh);
1170 clear_buffer_jbddirty(bh);
1171
1172 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1173
1dfc3220
JB
1174 /*
1175 * we only want to drop a reference if this transaction
1176 * modified the buffer
1177 */
1178 if (was_modified)
1179 drop_reserve = 1;
470decc6
DK
1180
1181 /*
1182 * We are no longer going to journal this buffer.
1183 * However, the commit of this transaction is still
1184 * important to the buffer: the delete that we are now
1185 * processing might obsolete an old log entry, so by
1186 * committing, we can satisfy the buffer's checkpoint.
1187 *
1188 * So, if we have a checkpoint on the buffer, we should
1189 * now refile the buffer on our BJ_Forget list so that
1190 * we know to remove the checkpoint after we commit.
1191 */
1192
1193 if (jh->b_cp_transaction) {
f7f4bccb
MC
1194 __jbd2_journal_temp_unlink_buffer(jh);
1195 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
470decc6 1196 } else {
f7f4bccb
MC
1197 __jbd2_journal_unfile_buffer(jh);
1198 jbd2_journal_remove_journal_head(bh);
470decc6
DK
1199 __brelse(bh);
1200 if (!buffer_jbd(bh)) {
1201 spin_unlock(&journal->j_list_lock);
1202 jbd_unlock_bh_state(bh);
1203 __bforget(bh);
1204 goto drop;
1205 }
1206 }
1207 } else if (jh->b_transaction) {
1208 J_ASSERT_JH(jh, (jh->b_transaction ==
1209 journal->j_committing_transaction));
1210 /* However, if the buffer is still owned by a prior
1211 * (committing) transaction, we can't drop it yet... */
1212 JBUFFER_TRACE(jh, "belongs to older transaction");
1213 /* ... but we CAN drop it from the new transaction if we
1214 * have also modified it since the original commit. */
1215
1216 if (jh->b_next_transaction) {
1217 J_ASSERT(jh->b_next_transaction == transaction);
1218 jh->b_next_transaction = NULL;
1dfc3220
JB
1219
1220 /*
1221 * only drop a reference if this transaction modified
1222 * the buffer
1223 */
1224 if (was_modified)
1225 drop_reserve = 1;
470decc6
DK
1226 }
1227 }
1228
1229not_jbd:
1230 spin_unlock(&journal->j_list_lock);
1231 jbd_unlock_bh_state(bh);
1232 __brelse(bh);
1233drop:
1234 if (drop_reserve) {
1235 /* no need to reserve log space for this block -bzzz */
1236 handle->h_buffer_credits++;
1237 }
1238 return err;
1239}
1240
1241/**
f7f4bccb 1242 * int jbd2_journal_stop() - complete a transaction
470decc6
DK
1243 * @handle: tranaction to complete.
1244 *
1245 * All done for a particular handle.
1246 *
1247 * There is not much action needed here. We just return any remaining
1248 * buffer credits to the transaction and remove the handle. The only
1249 * complication is that we need to start a commit operation if the
1250 * filesystem is marked for synchronous update.
1251 *
f7f4bccb 1252 * jbd2_journal_stop itself will not usually return an error, but it may
470decc6 1253 * do so in unusual circumstances. In particular, expect it to
f7f4bccb 1254 * return -EIO if a jbd2_journal_abort has been executed since the
470decc6
DK
1255 * transaction began.
1256 */
f7f4bccb 1257int jbd2_journal_stop(handle_t *handle)
470decc6
DK
1258{
1259 transaction_t *transaction = handle->h_transaction;
1260 journal_t *journal = transaction->t_journal;
e07f7183 1261 int err;
470decc6
DK
1262 pid_t pid;
1263
470decc6
DK
1264 J_ASSERT(journal_current_handle() == handle);
1265
1266 if (is_handle_aborted(handle))
1267 err = -EIO;
3e2a532b
OH
1268 else {
1269 J_ASSERT(transaction->t_updates > 0);
470decc6 1270 err = 0;
3e2a532b 1271 }
470decc6
DK
1272
1273 if (--handle->h_ref > 0) {
1274 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1275 handle->h_ref);
1276 return err;
1277 }
1278
1279 jbd_debug(4, "Handle %p going down\n", handle);
1280
1281 /*
1282 * Implement synchronous transaction batching. If the handle
1283 * was synchronous, don't force a commit immediately. Let's
e07f7183
JB
1284 * yield and let another thread piggyback onto this
1285 * transaction. Keep doing that while new threads continue to
1286 * arrive. It doesn't cost much - we're about to run a commit
1287 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1288 * operations by 30x or more...
1289 *
1290 * We try and optimize the sleep time against what the
1291 * underlying disk can do, instead of having a static sleep
1292 * time. This is useful for the case where our storage is so
1293 * fast that it is more optimal to go ahead and force a flush
1294 * and wait for the transaction to be committed than it is to
1295 * wait for an arbitrary amount of time for new writers to
1296 * join the transaction. We achieve this by measuring how
1297 * long it takes to commit a transaction, and compare it with
1298 * how long this transaction has been running, and if run time
1299 * < commit time then we sleep for the delta and commit. This
1300 * greatly helps super fast disks that would see slowdowns as
1301 * more threads started doing fsyncs.
470decc6 1302 *
e07f7183
JB
1303 * But don't do this if this process was the most recent one
1304 * to perform a synchronous write. We do this to detect the
1305 * case where a single process is doing a stream of sync
1306 * writes. No point in waiting for joiners in that case.
470decc6
DK
1307 */
1308 pid = current->pid;
1309 if (handle->h_sync && journal->j_last_sync_writer != pid) {
e07f7183
JB
1310 u64 commit_time, trans_time;
1311
470decc6 1312 journal->j_last_sync_writer = pid;
e07f7183
JB
1313
1314 spin_lock(&journal->j_state_lock);
1315 commit_time = journal->j_average_commit_time;
1316 spin_unlock(&journal->j_state_lock);
1317
1318 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1319 transaction->t_start_time));
1320
30773840
TT
1321 commit_time = max_t(u64, commit_time,
1322 1000*journal->j_min_batch_time);
e07f7183 1323 commit_time = min_t(u64, commit_time,
30773840 1324 1000*journal->j_max_batch_time);
e07f7183
JB
1325
1326 if (trans_time < commit_time) {
1327 ktime_t expires = ktime_add_ns(ktime_get(),
1328 commit_time);
1329 set_current_state(TASK_UNINTERRUPTIBLE);
1330 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1331 }
470decc6
DK
1332 }
1333
7058548c
TT
1334 if (handle->h_sync)
1335 transaction->t_synchronous_commit = 1;
470decc6 1336 current->journal_info = NULL;
470decc6
DK
1337 spin_lock(&transaction->t_handle_lock);
1338 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1339 transaction->t_updates--;
1340 if (!transaction->t_updates) {
1341 wake_up(&journal->j_wait_updates);
1342 if (journal->j_barrier_count)
1343 wake_up(&journal->j_wait_transaction_locked);
1344 }
1345
1346 /*
1347 * If the handle is marked SYNC, we need to set another commit
1348 * going! We also want to force a commit if the current
1349 * transaction is occupying too much of the log, or if the
1350 * transaction is too old now.
1351 */
1352 if (handle->h_sync ||
1353 transaction->t_outstanding_credits >
1354 journal->j_max_transaction_buffers ||
1355 time_after_eq(jiffies, transaction->t_expires)) {
1356 /* Do this even for aborted journals: an abort still
1357 * completes the commit thread, it just doesn't write
1358 * anything to disk. */
1359 tid_t tid = transaction->t_tid;
1360
1361 spin_unlock(&transaction->t_handle_lock);
1362 jbd_debug(2, "transaction too old, requesting commit for "
1363 "handle %p\n", handle);
1364 /* This is non-blocking */
c35a56a0 1365 jbd2_log_start_commit(journal, transaction->t_tid);
470decc6
DK
1366
1367 /*
f7f4bccb 1368 * Special case: JBD2_SYNC synchronous updates require us
470decc6
DK
1369 * to wait for the commit to complete.
1370 */
1371 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
f7f4bccb 1372 err = jbd2_log_wait_commit(journal, tid);
470decc6
DK
1373 } else {
1374 spin_unlock(&transaction->t_handle_lock);
470decc6
DK
1375 }
1376
3295f0ef 1377 lock_map_release(&handle->h_lockdep_map);
7b751066 1378
af1e76d6 1379 jbd2_free_handle(handle);
470decc6
DK
1380 return err;
1381}
1382
5648ba5b
RD
1383/**
1384 * int jbd2_journal_force_commit() - force any uncommitted transactions
470decc6
DK
1385 * @journal: journal to force
1386 *
1387 * For synchronous operations: force any uncommitted transactions
1388 * to disk. May seem kludgy, but it reuses all the handle batching
1389 * code in a very simple manner.
1390 */
f7f4bccb 1391int jbd2_journal_force_commit(journal_t *journal)
470decc6
DK
1392{
1393 handle_t *handle;
1394 int ret;
1395
f7f4bccb 1396 handle = jbd2_journal_start(journal, 1);
470decc6
DK
1397 if (IS_ERR(handle)) {
1398 ret = PTR_ERR(handle);
1399 } else {
1400 handle->h_sync = 1;
f7f4bccb 1401 ret = jbd2_journal_stop(handle);
470decc6
DK
1402 }
1403 return ret;
1404}
1405
1406/*
1407 *
1408 * List management code snippets: various functions for manipulating the
1409 * transaction buffer lists.
1410 *
1411 */
1412
1413/*
1414 * Append a buffer to a transaction list, given the transaction's list head
1415 * pointer.
1416 *
1417 * j_list_lock is held.
1418 *
1419 * jbd_lock_bh_state(jh2bh(jh)) is held.
1420 */
1421
1422static inline void
1423__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1424{
1425 if (!*list) {
1426 jh->b_tnext = jh->b_tprev = jh;
1427 *list = jh;
1428 } else {
1429 /* Insert at the tail of the list to preserve order */
1430 struct journal_head *first = *list, *last = first->b_tprev;
1431 jh->b_tprev = last;
1432 jh->b_tnext = first;
1433 last->b_tnext = first->b_tprev = jh;
1434 }
1435}
1436
1437/*
1438 * Remove a buffer from a transaction list, given the transaction's list
1439 * head pointer.
1440 *
1441 * Called with j_list_lock held, and the journal may not be locked.
1442 *
1443 * jbd_lock_bh_state(jh2bh(jh)) is held.
1444 */
1445
1446static inline void
1447__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1448{
1449 if (*list == jh) {
1450 *list = jh->b_tnext;
1451 if (*list == jh)
1452 *list = NULL;
1453 }
1454 jh->b_tprev->b_tnext = jh->b_tnext;
1455 jh->b_tnext->b_tprev = jh->b_tprev;
1456}
1457
1458/*
1459 * Remove a buffer from the appropriate transaction list.
1460 *
1461 * Note that this function can *change* the value of
87c89c23
JK
1462 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1463 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1464 * of these pointers, it could go bad. Generally the caller needs to re-read
1465 * the pointer from the transaction_t.
470decc6
DK
1466 *
1467 * Called under j_list_lock. The journal may not be locked.
1468 */
f7f4bccb 1469void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
470decc6
DK
1470{
1471 struct journal_head **list = NULL;
1472 transaction_t *transaction;
1473 struct buffer_head *bh = jh2bh(jh);
1474
1475 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1476 transaction = jh->b_transaction;
1477 if (transaction)
1478 assert_spin_locked(&transaction->t_journal->j_list_lock);
1479
1480 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1481 if (jh->b_jlist != BJ_None)
4019191b 1482 J_ASSERT_JH(jh, transaction != NULL);
470decc6
DK
1483
1484 switch (jh->b_jlist) {
1485 case BJ_None:
1486 return;
470decc6
DK
1487 case BJ_Metadata:
1488 transaction->t_nr_buffers--;
1489 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1490 list = &transaction->t_buffers;
1491 break;
1492 case BJ_Forget:
1493 list = &transaction->t_forget;
1494 break;
1495 case BJ_IO:
1496 list = &transaction->t_iobuf_list;
1497 break;
1498 case BJ_Shadow:
1499 list = &transaction->t_shadow_list;
1500 break;
1501 case BJ_LogCtl:
1502 list = &transaction->t_log_list;
1503 break;
1504 case BJ_Reserved:
1505 list = &transaction->t_reserved_list;
1506 break;
470decc6
DK
1507 }
1508
1509 __blist_del_buffer(list, jh);
1510 jh->b_jlist = BJ_None;
1511 if (test_clear_buffer_jbddirty(bh))
1512 mark_buffer_dirty(bh); /* Expose it to the VM */
1513}
1514
f7f4bccb 1515void __jbd2_journal_unfile_buffer(struct journal_head *jh)
470decc6 1516{
f7f4bccb 1517 __jbd2_journal_temp_unlink_buffer(jh);
470decc6
DK
1518 jh->b_transaction = NULL;
1519}
1520
f7f4bccb 1521void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
470decc6
DK
1522{
1523 jbd_lock_bh_state(jh2bh(jh));
1524 spin_lock(&journal->j_list_lock);
f7f4bccb 1525 __jbd2_journal_unfile_buffer(jh);
470decc6
DK
1526 spin_unlock(&journal->j_list_lock);
1527 jbd_unlock_bh_state(jh2bh(jh));
1528}
1529
1530/*
f7f4bccb 1531 * Called from jbd2_journal_try_to_free_buffers().
470decc6
DK
1532 *
1533 * Called under jbd_lock_bh_state(bh)
1534 */
1535static void
1536__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1537{
1538 struct journal_head *jh;
1539
1540 jh = bh2jh(bh);
1541
1542 if (buffer_locked(bh) || buffer_dirty(bh))
1543 goto out;
1544
4019191b 1545 if (jh->b_next_transaction != NULL)
470decc6
DK
1546 goto out;
1547
1548 spin_lock(&journal->j_list_lock);
87c89c23 1549 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
470decc6
DK
1550 /* written-back checkpointed metadata buffer */
1551 if (jh->b_jlist == BJ_None) {
1552 JBUFFER_TRACE(jh, "remove from checkpoint list");
f7f4bccb
MC
1553 __jbd2_journal_remove_checkpoint(jh);
1554 jbd2_journal_remove_journal_head(bh);
470decc6
DK
1555 __brelse(bh);
1556 }
1557 }
1558 spin_unlock(&journal->j_list_lock);
1559out:
1560 return;
1561}
1562
470decc6 1563/**
f7f4bccb 1564 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
470decc6
DK
1565 * @journal: journal for operation
1566 * @page: to try and free
530576bb
MC
1567 * @gfp_mask: we use the mask to detect how hard should we try to release
1568 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1569 * release the buffers.
470decc6
DK
1570 *
1571 *
1572 * For all the buffers on this page,
1573 * if they are fully written out ordered data, move them onto BUF_CLEAN
1574 * so try_to_free_buffers() can reap them.
1575 *
1576 * This function returns non-zero if we wish try_to_free_buffers()
1577 * to be called. We do this if the page is releasable by try_to_free_buffers().
1578 * We also do it if the page has locked or dirty buffers and the caller wants
1579 * us to perform sync or async writeout.
1580 *
1581 * This complicates JBD locking somewhat. We aren't protected by the
1582 * BKL here. We wish to remove the buffer from its committing or
f7f4bccb 1583 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
470decc6
DK
1584 *
1585 * This may *change* the value of transaction_t->t_datalist, so anyone
1586 * who looks at t_datalist needs to lock against this function.
1587 *
f7f4bccb
MC
1588 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1589 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
470decc6
DK
1590 * will come out of the lock with the buffer dirty, which makes it
1591 * ineligible for release here.
1592 *
1593 * Who else is affected by this? hmm... Really the only contender
1594 * is do_get_write_access() - it could be looking at the buffer while
1595 * journal_try_to_free_buffer() is changing its state. But that
1596 * cannot happen because we never reallocate freed data as metadata
1597 * while the data is part of a transaction. Yes?
530576bb
MC
1598 *
1599 * Return 0 on failure, 1 on success
470decc6 1600 */
f7f4bccb 1601int jbd2_journal_try_to_free_buffers(journal_t *journal,
530576bb 1602 struct page *page, gfp_t gfp_mask)
470decc6
DK
1603{
1604 struct buffer_head *head;
1605 struct buffer_head *bh;
1606 int ret = 0;
1607
1608 J_ASSERT(PageLocked(page));
1609
1610 head = page_buffers(page);
1611 bh = head;
1612 do {
1613 struct journal_head *jh;
1614
1615 /*
1616 * We take our own ref against the journal_head here to avoid
1617 * having to add tons of locking around each instance of
530576bb
MC
1618 * jbd2_journal_remove_journal_head() and
1619 * jbd2_journal_put_journal_head().
470decc6 1620 */
f7f4bccb 1621 jh = jbd2_journal_grab_journal_head(bh);
470decc6
DK
1622 if (!jh)
1623 continue;
1624
1625 jbd_lock_bh_state(bh);
1626 __journal_try_to_free_buffer(journal, bh);
f7f4bccb 1627 jbd2_journal_put_journal_head(jh);
470decc6
DK
1628 jbd_unlock_bh_state(bh);
1629 if (buffer_jbd(bh))
1630 goto busy;
1631 } while ((bh = bh->b_this_page) != head);
530576bb 1632
470decc6 1633 ret = try_to_free_buffers(page);
530576bb 1634
470decc6
DK
1635busy:
1636 return ret;
1637}
1638
1639/*
1640 * This buffer is no longer needed. If it is on an older transaction's
1641 * checkpoint list we need to record it on this transaction's forget list
1642 * to pin this buffer (and hence its checkpointing transaction) down until
1643 * this transaction commits. If the buffer isn't on a checkpoint list, we
1644 * release it.
1645 * Returns non-zero if JBD no longer has an interest in the buffer.
1646 *
1647 * Called under j_list_lock.
1648 *
1649 * Called under jbd_lock_bh_state(bh).
1650 */
1651static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1652{
1653 int may_free = 1;
1654 struct buffer_head *bh = jh2bh(jh);
1655
f7f4bccb 1656 __jbd2_journal_unfile_buffer(jh);
470decc6
DK
1657
1658 if (jh->b_cp_transaction) {
1659 JBUFFER_TRACE(jh, "on running+cp transaction");
f91d1d04
JK
1660 /*
1661 * We don't want to write the buffer anymore, clear the
1662 * bit so that we don't confuse checks in
1663 * __journal_file_buffer
1664 */
1665 clear_buffer_dirty(bh);
f7f4bccb 1666 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
470decc6
DK
1667 may_free = 0;
1668 } else {
1669 JBUFFER_TRACE(jh, "on running transaction");
f7f4bccb 1670 jbd2_journal_remove_journal_head(bh);
470decc6
DK
1671 __brelse(bh);
1672 }
1673 return may_free;
1674}
1675
1676/*
f7f4bccb 1677 * jbd2_journal_invalidatepage
470decc6
DK
1678 *
1679 * This code is tricky. It has a number of cases to deal with.
1680 *
1681 * There are two invariants which this code relies on:
1682 *
1683 * i_size must be updated on disk before we start calling invalidatepage on the
1684 * data.
1685 *
1686 * This is done in ext3 by defining an ext3_setattr method which
1687 * updates i_size before truncate gets going. By maintaining this
1688 * invariant, we can be sure that it is safe to throw away any buffers
1689 * attached to the current transaction: once the transaction commits,
1690 * we know that the data will not be needed.
1691 *
1692 * Note however that we can *not* throw away data belonging to the
1693 * previous, committing transaction!
1694 *
1695 * Any disk blocks which *are* part of the previous, committing
1696 * transaction (and which therefore cannot be discarded immediately) are
1697 * not going to be reused in the new running transaction
1698 *
1699 * The bitmap committed_data images guarantee this: any block which is
1700 * allocated in one transaction and removed in the next will be marked
1701 * as in-use in the committed_data bitmap, so cannot be reused until
1702 * the next transaction to delete the block commits. This means that
1703 * leaving committing buffers dirty is quite safe: the disk blocks
1704 * cannot be reallocated to a different file and so buffer aliasing is
1705 * not possible.
1706 *
1707 *
1708 * The above applies mainly to ordered data mode. In writeback mode we
1709 * don't make guarantees about the order in which data hits disk --- in
1710 * particular we don't guarantee that new dirty data is flushed before
1711 * transaction commit --- so it is always safe just to discard data
1712 * immediately in that mode. --sct
1713 */
1714
1715/*
1716 * The journal_unmap_buffer helper function returns zero if the buffer
1717 * concerned remains pinned as an anonymous buffer belonging to an older
1718 * transaction.
1719 *
1720 * We're outside-transaction here. Either or both of j_running_transaction
1721 * and j_committing_transaction may be NULL.
1722 */
1723static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1724{
1725 transaction_t *transaction;
1726 struct journal_head *jh;
1727 int may_free = 1;
1728 int ret;
1729
1730 BUFFER_TRACE(bh, "entry");
1731
1732 /*
1733 * It is safe to proceed here without the j_list_lock because the
1734 * buffers cannot be stolen by try_to_free_buffers as long as we are
1735 * holding the page lock. --sct
1736 */
1737
1738 if (!buffer_jbd(bh))
1739 goto zap_buffer_unlocked;
1740
87c89c23 1741 /* OK, we have data buffer in journaled mode */
470decc6
DK
1742 spin_lock(&journal->j_state_lock);
1743 jbd_lock_bh_state(bh);
1744 spin_lock(&journal->j_list_lock);
1745
f7f4bccb 1746 jh = jbd2_journal_grab_journal_head(bh);
470decc6
DK
1747 if (!jh)
1748 goto zap_buffer_no_jh;
1749
ba869023 1750 /*
1751 * We cannot remove the buffer from checkpoint lists until the
1752 * transaction adding inode to orphan list (let's call it T)
1753 * is committed. Otherwise if the transaction changing the
1754 * buffer would be cleaned from the journal before T is
1755 * committed, a crash will cause that the correct contents of
1756 * the buffer will be lost. On the other hand we have to
1757 * clear the buffer dirty bit at latest at the moment when the
1758 * transaction marking the buffer as freed in the filesystem
1759 * structures is committed because from that moment on the
1760 * buffer can be reallocated and used by a different page.
1761 * Since the block hasn't been freed yet but the inode has
1762 * already been added to orphan list, it is safe for us to add
1763 * the buffer to BJ_Forget list of the newest transaction.
1764 */
470decc6
DK
1765 transaction = jh->b_transaction;
1766 if (transaction == NULL) {
1767 /* First case: not on any transaction. If it
1768 * has no checkpoint link, then we can zap it:
1769 * it's a writeback-mode buffer so we don't care
1770 * if it hits disk safely. */
1771 if (!jh->b_cp_transaction) {
1772 JBUFFER_TRACE(jh, "not on any transaction: zap");
1773 goto zap_buffer;
1774 }
1775
1776 if (!buffer_dirty(bh)) {
1777 /* bdflush has written it. We can drop it now */
1778 goto zap_buffer;
1779 }
1780
1781 /* OK, it must be in the journal but still not
1782 * written fully to disk: it's metadata or
1783 * journaled data... */
1784
1785 if (journal->j_running_transaction) {
1786 /* ... and once the current transaction has
1787 * committed, the buffer won't be needed any
1788 * longer. */
1789 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1790 ret = __dispose_buffer(jh,
1791 journal->j_running_transaction);
f7f4bccb 1792 jbd2_journal_put_journal_head(jh);
470decc6
DK
1793 spin_unlock(&journal->j_list_lock);
1794 jbd_unlock_bh_state(bh);
1795 spin_unlock(&journal->j_state_lock);
1796 return ret;
1797 } else {
1798 /* There is no currently-running transaction. So the
1799 * orphan record which we wrote for this file must have
1800 * passed into commit. We must attach this buffer to
1801 * the committing transaction, if it exists. */
1802 if (journal->j_committing_transaction) {
1803 JBUFFER_TRACE(jh, "give to committing trans");
1804 ret = __dispose_buffer(jh,
1805 journal->j_committing_transaction);
f7f4bccb 1806 jbd2_journal_put_journal_head(jh);
470decc6
DK
1807 spin_unlock(&journal->j_list_lock);
1808 jbd_unlock_bh_state(bh);
1809 spin_unlock(&journal->j_state_lock);
1810 return ret;
1811 } else {
1812 /* The orphan record's transaction has
1813 * committed. We can cleanse this buffer */
1814 clear_buffer_jbddirty(bh);
1815 goto zap_buffer;
1816 }
1817 }
1818 } else if (transaction == journal->j_committing_transaction) {
9b57988d 1819 JBUFFER_TRACE(jh, "on committing transaction");
470decc6 1820 /*
ba869023 1821 * The buffer is committing, we simply cannot touch
1822 * it. So we just set j_next_transaction to the
1823 * running transaction (if there is one) and mark
1824 * buffer as freed so that commit code knows it should
1825 * clear dirty bits when it is done with the buffer.
1826 */
470decc6 1827 set_buffer_freed(bh);
ba869023 1828 if (journal->j_running_transaction && buffer_jbddirty(bh))
1829 jh->b_next_transaction = journal->j_running_transaction;
f7f4bccb 1830 jbd2_journal_put_journal_head(jh);
470decc6
DK
1831 spin_unlock(&journal->j_list_lock);
1832 jbd_unlock_bh_state(bh);
1833 spin_unlock(&journal->j_state_lock);
1834 return 0;
1835 } else {
1836 /* Good, the buffer belongs to the running transaction.
1837 * We are writing our own transaction's data, not any
1838 * previous one's, so it is safe to throw it away
1839 * (remember that we expect the filesystem to have set
1840 * i_size already for this truncate so recovery will not
1841 * expose the disk blocks we are discarding here.) */
1842 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
9b57988d 1843 JBUFFER_TRACE(jh, "on running transaction");
470decc6
DK
1844 may_free = __dispose_buffer(jh, transaction);
1845 }
1846
1847zap_buffer:
f7f4bccb 1848 jbd2_journal_put_journal_head(jh);
470decc6
DK
1849zap_buffer_no_jh:
1850 spin_unlock(&journal->j_list_lock);
1851 jbd_unlock_bh_state(bh);
1852 spin_unlock(&journal->j_state_lock);
1853zap_buffer_unlocked:
1854 clear_buffer_dirty(bh);
1855 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1856 clear_buffer_mapped(bh);
1857 clear_buffer_req(bh);
1858 clear_buffer_new(bh);
1859 bh->b_bdev = NULL;
1860 return may_free;
1861}
1862
1863/**
f7f4bccb 1864 * void jbd2_journal_invalidatepage()
470decc6
DK
1865 * @journal: journal to use for flush...
1866 * @page: page to flush
1867 * @offset: length of page to invalidate.
1868 *
1869 * Reap page buffers containing data after offset in page.
1870 *
1871 */
f7f4bccb 1872void jbd2_journal_invalidatepage(journal_t *journal,
470decc6
DK
1873 struct page *page,
1874 unsigned long offset)
1875{
1876 struct buffer_head *head, *bh, *next;
1877 unsigned int curr_off = 0;
1878 int may_free = 1;
1879
1880 if (!PageLocked(page))
1881 BUG();
1882 if (!page_has_buffers(page))
1883 return;
1884
1885 /* We will potentially be playing with lists other than just the
1886 * data lists (especially for journaled data mode), so be
1887 * cautious in our locking. */
1888
1889 head = bh = page_buffers(page);
1890 do {
1891 unsigned int next_off = curr_off + bh->b_size;
1892 next = bh->b_this_page;
1893
1894 if (offset <= curr_off) {
1895 /* This block is wholly outside the truncation point */
1896 lock_buffer(bh);
1897 may_free &= journal_unmap_buffer(journal, bh);
1898 unlock_buffer(bh);
1899 }
1900 curr_off = next_off;
1901 bh = next;
1902
1903 } while (bh != head);
1904
1905 if (!offset) {
1906 if (may_free && try_to_free_buffers(page))
1907 J_ASSERT(!page_has_buffers(page));
1908 }
1909}
1910
1911/*
1912 * File a buffer on the given transaction list.
1913 */
f7f4bccb 1914void __jbd2_journal_file_buffer(struct journal_head *jh,
470decc6
DK
1915 transaction_t *transaction, int jlist)
1916{
1917 struct journal_head **list = NULL;
1918 int was_dirty = 0;
1919 struct buffer_head *bh = jh2bh(jh);
1920
1921 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1922 assert_spin_locked(&transaction->t_journal->j_list_lock);
1923
1924 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1925 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
4019191b 1926 jh->b_transaction == NULL);
470decc6
DK
1927
1928 if (jh->b_transaction && jh->b_jlist == jlist)
1929 return;
1930
470decc6
DK
1931 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1932 jlist == BJ_Shadow || jlist == BJ_Forget) {
f91d1d04
JK
1933 /*
1934 * For metadata buffers, we track dirty bit in buffer_jbddirty
1935 * instead of buffer_dirty. We should not see a dirty bit set
1936 * here because we clear it in do_get_write_access but e.g.
1937 * tune2fs can modify the sb and set the dirty bit at any time
1938 * so we try to gracefully handle that.
1939 */
1940 if (buffer_dirty(bh))
1941 warn_dirty_buffer(bh);
470decc6
DK
1942 if (test_clear_buffer_dirty(bh) ||
1943 test_clear_buffer_jbddirty(bh))
1944 was_dirty = 1;
1945 }
1946
1947 if (jh->b_transaction)
f7f4bccb 1948 __jbd2_journal_temp_unlink_buffer(jh);
470decc6
DK
1949 jh->b_transaction = transaction;
1950
1951 switch (jlist) {
1952 case BJ_None:
1953 J_ASSERT_JH(jh, !jh->b_committed_data);
1954 J_ASSERT_JH(jh, !jh->b_frozen_data);
1955 return;
470decc6
DK
1956 case BJ_Metadata:
1957 transaction->t_nr_buffers++;
1958 list = &transaction->t_buffers;
1959 break;
1960 case BJ_Forget:
1961 list = &transaction->t_forget;
1962 break;
1963 case BJ_IO:
1964 list = &transaction->t_iobuf_list;
1965 break;
1966 case BJ_Shadow:
1967 list = &transaction->t_shadow_list;
1968 break;
1969 case BJ_LogCtl:
1970 list = &transaction->t_log_list;
1971 break;
1972 case BJ_Reserved:
1973 list = &transaction->t_reserved_list;
1974 break;
470decc6
DK
1975 }
1976
1977 __blist_add_buffer(list, jh);
1978 jh->b_jlist = jlist;
1979
1980 if (was_dirty)
1981 set_buffer_jbddirty(bh);
1982}
1983
f7f4bccb 1984void jbd2_journal_file_buffer(struct journal_head *jh,
470decc6
DK
1985 transaction_t *transaction, int jlist)
1986{
1987 jbd_lock_bh_state(jh2bh(jh));
1988 spin_lock(&transaction->t_journal->j_list_lock);
f7f4bccb 1989 __jbd2_journal_file_buffer(jh, transaction, jlist);
470decc6
DK
1990 spin_unlock(&transaction->t_journal->j_list_lock);
1991 jbd_unlock_bh_state(jh2bh(jh));
1992}
1993
1994/*
1995 * Remove a buffer from its current buffer list in preparation for
1996 * dropping it from its current transaction entirely. If the buffer has
1997 * already started to be used by a subsequent transaction, refile the
1998 * buffer on that transaction's metadata list.
1999 *
2000 * Called under journal->j_list_lock
2001 *
2002 * Called under jbd_lock_bh_state(jh2bh(jh))
2003 */
f7f4bccb 2004void __jbd2_journal_refile_buffer(struct journal_head *jh)
470decc6 2005{
ba869023 2006 int was_dirty, jlist;
470decc6
DK
2007 struct buffer_head *bh = jh2bh(jh);
2008
2009 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2010 if (jh->b_transaction)
2011 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2012
2013 /* If the buffer is now unused, just drop it. */
2014 if (jh->b_next_transaction == NULL) {
f7f4bccb 2015 __jbd2_journal_unfile_buffer(jh);
470decc6
DK
2016 return;
2017 }
2018
2019 /*
2020 * It has been modified by a later transaction: add it to the new
2021 * transaction's metadata list.
2022 */
2023
2024 was_dirty = test_clear_buffer_jbddirty(bh);
f7f4bccb 2025 __jbd2_journal_temp_unlink_buffer(jh);
470decc6
DK
2026 jh->b_transaction = jh->b_next_transaction;
2027 jh->b_next_transaction = NULL;
ba869023 2028 if (buffer_freed(bh))
2029 jlist = BJ_Forget;
2030 else if (jh->b_modified)
2031 jlist = BJ_Metadata;
2032 else
2033 jlist = BJ_Reserved;
2034 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
470decc6
DK
2035 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2036
2037 if (was_dirty)
2038 set_buffer_jbddirty(bh);
2039}
2040
2041/*
2042 * For the unlocked version of this call, also make sure that any
2043 * hanging journal_head is cleaned up if necessary.
2044 *
f7f4bccb 2045 * __jbd2_journal_refile_buffer is usually called as part of a single locked
470decc6
DK
2046 * operation on a buffer_head, in which the caller is probably going to
2047 * be hooking the journal_head onto other lists. In that case it is up
2048 * to the caller to remove the journal_head if necessary. For the
f7f4bccb 2049 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
470decc6
DK
2050 * doing anything else to the buffer so we need to do the cleanup
2051 * ourselves to avoid a jh leak.
2052 *
2053 * *** The journal_head may be freed by this call! ***
2054 */
f7f4bccb 2055void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
470decc6
DK
2056{
2057 struct buffer_head *bh = jh2bh(jh);
2058
2059 jbd_lock_bh_state(bh);
2060 spin_lock(&journal->j_list_lock);
2061
f7f4bccb 2062 __jbd2_journal_refile_buffer(jh);
470decc6 2063 jbd_unlock_bh_state(bh);
f7f4bccb 2064 jbd2_journal_remove_journal_head(bh);
470decc6
DK
2065
2066 spin_unlock(&journal->j_list_lock);
2067 __brelse(bh);
2068}
c851ed54
JK
2069
2070/*
2071 * File inode in the inode list of the handle's transaction
2072 */
2073int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2074{
2075 transaction_t *transaction = handle->h_transaction;
2076 journal_t *journal = transaction->t_journal;
2077
2078 if (is_handle_aborted(handle))
2079 return -EIO;
2080
2081 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2082 transaction->t_tid);
2083
2084 /*
2085 * First check whether inode isn't already on the transaction's
2086 * lists without taking the lock. Note that this check is safe
2087 * without the lock as we cannot race with somebody removing inode
2088 * from the transaction. The reason is that we remove inode from the
2089 * transaction only in journal_release_jbd_inode() and when we commit
2090 * the transaction. We are guarded from the first case by holding
2091 * a reference to the inode. We are safe against the second case
2092 * because if jinode->i_transaction == transaction, commit code
2093 * cannot touch the transaction because we hold reference to it,
2094 * and if jinode->i_next_transaction == transaction, commit code
2095 * will only file the inode where we want it.
2096 */
2097 if (jinode->i_transaction == transaction ||
2098 jinode->i_next_transaction == transaction)
2099 return 0;
2100
2101 spin_lock(&journal->j_list_lock);
2102
2103 if (jinode->i_transaction == transaction ||
2104 jinode->i_next_transaction == transaction)
2105 goto done;
2106
2107 /* On some different transaction's list - should be
2108 * the committing one */
2109 if (jinode->i_transaction) {
2110 J_ASSERT(jinode->i_next_transaction == NULL);
2111 J_ASSERT(jinode->i_transaction ==
2112 journal->j_committing_transaction);
2113 jinode->i_next_transaction = transaction;
2114 goto done;
2115 }
2116 /* Not on any transaction list... */
2117 J_ASSERT(!jinode->i_next_transaction);
2118 jinode->i_transaction = transaction;
2119 list_add(&jinode->i_list, &transaction->t_inode_list);
2120done:
2121 spin_unlock(&journal->j_list_lock);
2122
2123 return 0;
2124}
2125
2126/*
7f5aa215
JK
2127 * File truncate and transaction commit interact with each other in a
2128 * non-trivial way. If a transaction writing data block A is
2129 * committing, we cannot discard the data by truncate until we have
2130 * written them. Otherwise if we crashed after the transaction with
2131 * write has committed but before the transaction with truncate has
2132 * committed, we could see stale data in block A. This function is a
2133 * helper to solve this problem. It starts writeout of the truncated
2134 * part in case it is in the committing transaction.
2135 *
2136 * Filesystem code must call this function when inode is journaled in
2137 * ordered mode before truncation happens and after the inode has been
2138 * placed on orphan list with the new inode size. The second condition
2139 * avoids the race that someone writes new data and we start
2140 * committing the transaction after this function has been called but
2141 * before a transaction for truncate is started (and furthermore it
2142 * allows us to optimize the case where the addition to orphan list
2143 * happens in the same transaction as write --- we don't have to write
2144 * any data in such case).
c851ed54 2145 */
7f5aa215
JK
2146int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2147 struct jbd2_inode *jinode,
c851ed54
JK
2148 loff_t new_size)
2149{
7f5aa215 2150 transaction_t *inode_trans, *commit_trans;
c851ed54
JK
2151 int ret = 0;
2152
7f5aa215
JK
2153 /* This is a quick check to avoid locking if not necessary */
2154 if (!jinode->i_transaction)
c851ed54 2155 goto out;
7f5aa215
JK
2156 /* Locks are here just to force reading of recent values, it is
2157 * enough that the transaction was not committing before we started
2158 * a transaction adding the inode to orphan list */
c851ed54
JK
2159 spin_lock(&journal->j_state_lock);
2160 commit_trans = journal->j_committing_transaction;
2161 spin_unlock(&journal->j_state_lock);
7f5aa215
JK
2162 spin_lock(&journal->j_list_lock);
2163 inode_trans = jinode->i_transaction;
2164 spin_unlock(&journal->j_list_lock);
2165 if (inode_trans == commit_trans) {
2166 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
c851ed54
JK
2167 new_size, LLONG_MAX);
2168 if (ret)
2169 jbd2_journal_abort(journal, ret);
2170 }
2171out:
2172 return ret;
2173}