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