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