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