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