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