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