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