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