vserver 1.9.3
[linux-2.6.git] / fs / reiserfs / journal.c
1 /*
2 ** Write ahead logging implementation copyright Chris Mason 2000
3 **
4 ** The background commits make this code very interelated, and 
5 ** overly complex.  I need to rethink things a bit....The major players:
6 **
7 ** journal_begin -- call with the number of blocks you expect to log.  
8 **                  If the current transaction is too
9 **                  old, it will block until the current transaction is 
10 **                  finished, and then start a new one.
11 **                  Usually, your transaction will get joined in with 
12 **                  previous ones for speed.
13 **
14 ** journal_join  -- same as journal_begin, but won't block on the current 
15 **                  transaction regardless of age.  Don't ever call
16 **                  this.  Ever.  There are only two places it should be 
17 **                  called from, and they are both inside this file.
18 **
19 ** journal_mark_dirty -- adds blocks into this transaction.  clears any flags 
20 **                       that might make them get sent to disk
21 **                       and then marks them BH_JDirty.  Puts the buffer head 
22 **                       into the current transaction hash.  
23 **
24 ** journal_end -- if the current transaction is batchable, it does nothing
25 **                   otherwise, it could do an async/synchronous commit, or
26 **                   a full flush of all log and real blocks in the 
27 **                   transaction.
28 **
29 ** flush_old_commits -- if the current transaction is too old, it is ended and 
30 **                      commit blocks are sent to disk.  Forces commit blocks 
31 **                      to disk for all backgrounded commits that have been 
32 **                      around too long.
33 **                   -- Note, if you call this as an immediate flush from 
34 **                      from within kupdate, it will ignore the immediate flag
35 */
36
37 #include <linux/config.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40
41 #include <linux/time.h>
42 #include <asm/semaphore.h>
43
44 #include <linux/vmalloc.h>
45 #include <linux/reiserfs_fs.h>
46
47 #include <linux/kernel.h>
48 #include <linux/errno.h>
49 #include <linux/fcntl.h>
50 #include <linux/stat.h>
51 #include <linux/string.h>
52 #include <linux/smp_lock.h>
53 #include <linux/suspend.h>
54 #include <linux/buffer_head.h>
55 #include <linux/workqueue.h>
56 #include <linux/writeback.h>
57 #include <linux/blkdev.h>
58
59
60 /* gets a struct reiserfs_journal_list * from a list head */
61 #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
62                                j_list))
63 #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
64                                j_working_list))
65
66 /* the number of mounted filesystems.  This is used to decide when to
67 ** start and kill the commit workqueue
68 */
69 static int reiserfs_mounted_fs_count;
70
71 static struct workqueue_struct *commit_wq;
72
73 #define JOURNAL_TRANS_HALF 1018   /* must be correct to keep the desc and commit
74                                      structs at 4k */
75 #define BUFNR 64 /*read ahead */
76
77 /* cnode stat bits.  Move these into reiserfs_fs.h */
78
79 #define BLOCK_FREED 2           /* this block was freed, and can't be written.  */
80 #define BLOCK_FREED_HOLDER 3    /* this block was freed during this transaction, and can't be written */
81
82 #define BLOCK_NEEDS_FLUSH 4     /* used in flush_journal_list */
83 #define BLOCK_DIRTIED 5
84
85
86 /* journal list state bits */
87 #define LIST_TOUCHED 1
88 #define LIST_DIRTY   2
89 #define LIST_COMMIT_PENDING  4          /* someone will commit this list */
90
91 /* flags for do_journal_end */
92 #define FLUSH_ALL   1           /* flush commit and real blocks */
93 #define COMMIT_NOW  2           /* end and commit this transaction */
94 #define WAIT        4           /* wait for the log blocks to hit the disk*/
95
96 /* state bits for the journal */
97 #define WRITERS_BLOCKED 1      /* set when new writers not allowed */
98 #define WRITERS_QUEUED 2       /* set when log is full due to too many
99                                 * writers
100                                 */
101
102 static int do_journal_end(struct reiserfs_transaction_handle *,struct super_block *,unsigned long nblocks,int flags) ;
103 static int flush_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
104 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall)  ;
105 static int can_dirty(struct reiserfs_journal_cnode *cn) ;
106 static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks);
107 static int release_journal_dev( struct super_block *super,
108                                 struct reiserfs_journal *journal );
109 static int dirty_one_transaction(struct super_block *s,
110                                  struct reiserfs_journal_list *jl);
111 static void flush_async_commits(void *p);
112
113 static void init_journal_hash(struct super_block *p_s_sb) {
114   memset(SB_JOURNAL(p_s_sb)->j_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
115 }
116
117 /*
118 ** clears BH_Dirty and sticks the buffer on the clean list.  Called because I can't allow refile_buffer to
119 ** make schedule happen after I've freed a block.  Look at remove_from_transaction and journal_mark_freed for
120 ** more details.
121 */
122 static int reiserfs_clean_and_file_buffer(struct buffer_head *bh) {
123   if (bh) {
124     clear_buffer_dirty(bh);
125     clear_bit(BH_JTest, &bh->b_state);
126   }
127   return 0 ;
128 }
129
130 static void disable_barrier(struct super_block *s)
131 {
132     REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_BARRIER_FLUSH);
133     printk("reiserfs: disabling flush barriers on %s\n", reiserfs_bdevname(s));
134 }
135
136 static struct reiserfs_bitmap_node *
137 allocate_bitmap_node(struct super_block *p_s_sb) {
138   struct reiserfs_bitmap_node *bn ;
139   static int id;
140
141   bn = reiserfs_kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS, p_s_sb) ;
142   if (!bn) {
143     return NULL ;
144   }
145   bn->data = reiserfs_kmalloc(p_s_sb->s_blocksize, GFP_NOFS, p_s_sb) ;
146   if (!bn->data) {
147     reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
148     return NULL ;
149   }
150   bn->id = id++ ;
151   memset(bn->data, 0, p_s_sb->s_blocksize) ;
152   INIT_LIST_HEAD(&bn->list) ;
153   return bn ;
154 }
155
156 static struct reiserfs_bitmap_node *
157 get_bitmap_node(struct super_block *p_s_sb) {
158   struct reiserfs_bitmap_node *bn = NULL;
159   struct list_head *entry = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
160
161   SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes++ ;
162 repeat:
163
164   if(entry != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
165     bn = list_entry(entry, struct reiserfs_bitmap_node, list) ;
166     list_del(entry) ;
167     memset(bn->data, 0, p_s_sb->s_blocksize) ;
168     SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
169     return bn ;
170   }
171   bn = allocate_bitmap_node(p_s_sb) ;
172   if (!bn) {
173     yield();
174     goto repeat ;
175   }
176   return bn ;
177 }
178 static inline void free_bitmap_node(struct super_block *p_s_sb,
179                                     struct reiserfs_bitmap_node *bn) {
180   SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes-- ;
181   if (SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
182     reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
183     reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
184   } else {
185     list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
186     SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
187   }
188 }
189
190 static void allocate_bitmap_nodes(struct super_block *p_s_sb) {
191   int i ;
192   struct reiserfs_bitmap_node *bn = NULL ;
193   for (i = 0 ; i < REISERFS_MIN_BITMAP_NODES ; i++) {
194     bn = allocate_bitmap_node(p_s_sb) ;
195     if (bn) {
196       list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
197       SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
198     } else {
199       break ; // this is ok, we'll try again when more are needed 
200     }
201   }
202 }
203
204 static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
205                                   struct reiserfs_list_bitmap *jb) {
206   int bmap_nr = block / (p_s_sb->s_blocksize << 3) ;
207   int bit_nr = block % (p_s_sb->s_blocksize << 3) ;
208
209   if (!jb->bitmaps[bmap_nr]) {
210     jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb) ;
211   }
212   set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data) ;
213   return 0 ;
214 }
215
216 static void cleanup_bitmap_list(struct super_block *p_s_sb,
217                                 struct reiserfs_list_bitmap *jb) {
218   int i;
219   if (jb->bitmaps == NULL)
220     return;
221
222   for (i = 0 ; i < SB_BMAP_NR(p_s_sb) ; i++) {
223     if (jb->bitmaps[i]) {
224       free_bitmap_node(p_s_sb, jb->bitmaps[i]) ;
225       jb->bitmaps[i] = NULL ;
226     }
227   }
228 }
229
230 /*
231 ** only call this on FS unmount.
232 */
233 static int free_list_bitmaps(struct super_block *p_s_sb,
234                              struct reiserfs_list_bitmap *jb_array) {
235   int i ;
236   struct reiserfs_list_bitmap *jb ;
237   for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
238     jb = jb_array + i ;
239     jb->journal_list = NULL ;
240     cleanup_bitmap_list(p_s_sb, jb) ;
241     vfree(jb->bitmaps) ;
242     jb->bitmaps = NULL ;
243   }
244   return 0;
245 }
246
247 static int free_bitmap_nodes(struct super_block *p_s_sb) {
248   struct list_head *next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
249   struct reiserfs_bitmap_node *bn ;
250
251   while(next != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
252     bn = list_entry(next, struct reiserfs_bitmap_node, list) ;
253     list_del(next) ;
254     reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
255     reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
256     next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
257     SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
258   }
259
260   return 0 ;
261 }
262
263 /*
264 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps. 
265 ** jb_array is the array to be filled in.
266 */
267 int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
268                                    struct reiserfs_list_bitmap *jb_array,
269                                    int bmap_nr) {
270   int i ;
271   int failed = 0 ;
272   struct reiserfs_list_bitmap *jb ;
273   int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *) ;
274
275   for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
276     jb = jb_array + i ;
277     jb->journal_list = NULL ;
278     jb->bitmaps = vmalloc( mem ) ;
279     if (!jb->bitmaps) {
280       reiserfs_warning(p_s_sb, "clm-2000, unable to allocate bitmaps for journal lists") ;
281       failed = 1;   
282       break ;
283     }
284     memset(jb->bitmaps, 0, mem) ;
285   }
286   if (failed) {
287     free_list_bitmaps(p_s_sb, jb_array) ;
288     return -1 ;
289   }
290   return 0 ;
291 }
292
293 /*
294 ** find an available list bitmap.  If you can't find one, flush a commit list 
295 ** and try again
296 */
297 static struct reiserfs_list_bitmap *
298 get_list_bitmap(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
299   int i,j ; 
300   struct reiserfs_list_bitmap *jb = NULL ;
301
302   for (j = 0 ; j < (JOURNAL_NUM_BITMAPS * 3) ; j++) {
303     i = SB_JOURNAL(p_s_sb)->j_list_bitmap_index ;
304     SB_JOURNAL(p_s_sb)->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS ;
305     jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
306     if (SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
307       flush_commit_list(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list, 1) ;
308       if (!SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
309         break ;
310       }
311     } else {
312       break ;
313     }
314   }
315   if (jb->journal_list) { /* double check to make sure if flushed correctly */
316     return NULL ;
317   }
318   jb->journal_list = jl ;
319   return jb ;
320 }
321
322 /* 
323 ** allocates a new chunk of X nodes, and links them all together as a list.
324 ** Uses the cnode->next and cnode->prev pointers
325 ** returns NULL on failure
326 */
327 static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes) {
328   struct reiserfs_journal_cnode *head ;
329   int i ;
330   if (num_cnodes <= 0) {
331     return NULL ;
332   }
333   head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
334   if (!head) {
335     return NULL ;
336   }
337   memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
338   head[0].prev = NULL ;
339   head[0].next = head + 1 ;
340   for (i = 1 ; i < num_cnodes; i++) {
341     head[i].prev = head + (i - 1) ;
342     head[i].next = head + (i + 1) ; /* if last one, overwrite it after the if */
343   }
344   head[num_cnodes -1].next = NULL ;
345   return head ;
346 }
347
348 /*
349 ** pulls a cnode off the free list, or returns NULL on failure 
350 */
351 static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb) {
352   struct reiserfs_journal_cnode *cn ;
353
354   reiserfs_check_lock_depth(p_s_sb, "get_cnode") ;
355
356   if (SB_JOURNAL(p_s_sb)->j_cnode_free <= 0) {
357     return NULL ;
358   }
359   SB_JOURNAL(p_s_sb)->j_cnode_used++ ;
360   SB_JOURNAL(p_s_sb)->j_cnode_free-- ;
361   cn = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
362   if (!cn) {
363     return cn ;
364   }
365   if (cn->next) {
366     cn->next->prev = NULL ;
367   }
368   SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn->next ;
369   memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ;
370   return cn ;
371 }
372
373 /*
374 ** returns a cnode to the free list 
375 */
376 static void free_cnode(struct super_block *p_s_sb, struct reiserfs_journal_cnode *cn) {
377
378   reiserfs_check_lock_depth(p_s_sb, "free_cnode") ;
379
380   SB_JOURNAL(p_s_sb)->j_cnode_used-- ;
381   SB_JOURNAL(p_s_sb)->j_cnode_free++ ;
382   /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
383   cn->next = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
384   if (SB_JOURNAL(p_s_sb)->j_cnode_free_list) {
385     SB_JOURNAL(p_s_sb)->j_cnode_free_list->prev = cn ;
386   }
387   cn->prev = NULL ; /* not needed with the memset, but I might kill the memset, and forget to do this */
388   SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn ;
389 }
390
391 static int clear_prepared_bits(struct buffer_head *bh) {
392   clear_bit(BH_JPrepared, &bh->b_state) ;
393   clear_bit(BH_JRestore_dirty, &bh->b_state) ;
394   return 0 ;
395 }
396
397 /* buffer is in current transaction */
398 inline int buffer_journaled(const struct buffer_head *bh) {
399   if (bh)
400     return test_bit(BH_JDirty, &bh->b_state) ;
401   else
402     return 0 ;
403 }
404
405 /* disk block was taken off free list before being in a finished transation, or written to disk
406 ** journal_new blocks can be reused immediately, for any purpose
407 */ 
408 inline int buffer_journal_new(const struct buffer_head *bh) {
409   if (bh) 
410     return test_bit(BH_JNew, &bh->b_state) ;
411   else
412     return 0 ;
413 }
414
415 inline int mark_buffer_journal_new(struct buffer_head *bh) {
416   if (bh) {
417     set_bit(BH_JNew, &bh->b_state) ;
418   }
419   return 0 ;
420 }
421
422 inline int mark_buffer_not_journaled(struct buffer_head *bh) {
423   if (bh) 
424     clear_bit(BH_JDirty, &bh->b_state) ;
425   return 0 ;
426 }
427
428 /* utility function to force a BUG if it is called without the big
429 ** kernel lock held.  caller is the string printed just before calling BUG()
430 */
431 void reiserfs_check_lock_depth(struct super_block *sb, char *caller) {
432 #ifdef CONFIG_SMP
433   if (current->lock_depth < 0) {
434     reiserfs_panic (sb, "%s called without kernel lock held", caller) ;
435   }
436 #else
437   ;
438 #endif
439 }
440
441 /* return a cnode with same dev, block number and size in table, or null if not found */
442 static inline struct reiserfs_journal_cnode *
443 get_journal_hash_dev(struct super_block *sb,
444                      struct reiserfs_journal_cnode **table,
445                      long bl)
446 {
447   struct reiserfs_journal_cnode *cn ;
448   cn = journal_hash(table, sb, bl) ;
449   while(cn) {
450     if (cn->blocknr == bl && cn->sb == sb)
451       return cn ;
452     cn = cn->hnext ;
453   }
454   return (struct reiserfs_journal_cnode *)0 ;
455 }
456
457 /* returns a cnode with same size, block number and dev as bh in the current transaction hash.  NULL if not found */
458 static inline struct reiserfs_journal_cnode *get_journal_hash(struct super_block *p_s_sb, struct buffer_head *bh) {
459   struct reiserfs_journal_cnode *cn ;
460   if (bh) {
461     cn =  get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, bh->b_blocknr);
462   }
463   else {
464     return (struct reiserfs_journal_cnode *)0 ;
465   }
466   return cn ;
467 }
468
469 /*
470 ** this actually means 'can this block be reallocated yet?'.  If you set search_all, a block can only be allocated
471 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
472 ** being overwritten by a replay after crashing.
473 **
474 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction.  Since deleting
475 ** a block removes it from the current transaction, this case should never happen.  If you don't set search_all, make
476 ** sure you never write the block without logging it.
477 **
478 ** next_zero_bit is a suggestion about the next block to try for find_forward.
479 ** when bl is rejected because it is set in a journal list bitmap, we search
480 ** for the next zero bit in the bitmap that rejected bl.  Then, we return that
481 ** through next_zero_bit for find_forward to try.
482 **
483 ** Just because we return something in next_zero_bit does not mean we won't
484 ** reject it on the next call to reiserfs_in_journal
485 **
486 */
487 int reiserfs_in_journal(struct super_block *p_s_sb,
488                         int bmap_nr, int bit_nr, int search_all, 
489                         b_blocknr_t *next_zero_bit) {
490   struct reiserfs_journal_cnode *cn ;
491   struct reiserfs_list_bitmap *jb ;
492   int i ;
493   unsigned long bl;
494
495   *next_zero_bit = 0 ; /* always start this at zero. */
496
497   PROC_INFO_INC( p_s_sb, journal.in_journal );
498   /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
499   ** if we crash before the transaction that freed it commits,  this transaction won't
500   ** have committed either, and the block will never be written
501   */
502   if (search_all) {
503     for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
504       PROC_INFO_INC( p_s_sb, journal.in_journal_bitmap );
505       jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
506       if (jb->journal_list && jb->bitmaps[bmap_nr] &&
507           test_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data)) {
508         *next_zero_bit = find_next_zero_bit((unsigned long *)
509                                      (jb->bitmaps[bmap_nr]->data),
510                                      p_s_sb->s_blocksize << 3, bit_nr+1) ; 
511         return 1 ;
512       }
513     }
514   }
515
516   bl = bmap_nr * (p_s_sb->s_blocksize << 3) + bit_nr;
517   /* is it in any old transactions? */
518   if (search_all && (cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_hash_table, bl))) {
519     return 1; 
520   }
521
522   /* is it in the current transaction.  This should never happen */
523   if ((cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, bl))) {
524     BUG();
525     return 1; 
526   }
527
528   PROC_INFO_INC( p_s_sb, journal.in_journal_reusable );
529   /* safe for reuse */
530   return 0 ;
531 }
532
533 /* insert cn into table
534 */
535 inline void insert_journal_hash(struct reiserfs_journal_cnode **table, struct reiserfs_journal_cnode *cn) {
536   struct reiserfs_journal_cnode *cn_orig ;
537
538   cn_orig = journal_hash(table, cn->sb, cn->blocknr) ;
539   cn->hnext = cn_orig ;
540   cn->hprev = NULL ;
541   if (cn_orig) {
542     cn_orig->hprev = cn ;
543   }
544   journal_hash(table, cn->sb, cn->blocknr) =  cn ;
545 }
546
547 /* lock the current transaction */
548 inline static void lock_journal(struct super_block *p_s_sb) {
549     PROC_INFO_INC( p_s_sb, journal.lock_journal );
550     down(&SB_JOURNAL(p_s_sb)->j_lock);
551 }
552
553 /* unlock the current transaction */
554 inline static void unlock_journal(struct super_block *p_s_sb) {
555     up(&SB_JOURNAL(p_s_sb)->j_lock);
556 }
557
558 static inline void get_journal_list(struct reiserfs_journal_list *jl)
559 {
560     jl->j_refcount++;
561 }
562
563 static inline void put_journal_list(struct super_block *s,
564                                    struct reiserfs_journal_list *jl)
565 {
566     if (jl->j_refcount < 1) {
567         reiserfs_panic (s, "trans id %lu, refcount at %d", jl->j_trans_id,
568                                                  jl->j_refcount);
569     }
570     if (--jl->j_refcount == 0)
571         reiserfs_kfree(jl, sizeof(struct reiserfs_journal_list), s);
572 }
573
574 /*
575 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
576 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
577 ** transaction.
578 */
579 static void cleanup_freed_for_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
580
581   struct reiserfs_list_bitmap *jb = jl->j_list_bitmap ;
582   if (jb) {
583     cleanup_bitmap_list(p_s_sb, jb) ;
584   }
585   jl->j_list_bitmap->journal_list = NULL ;
586   jl->j_list_bitmap = NULL ;
587 }
588
589 static int journal_list_still_alive(struct super_block *s,
590                                     unsigned long trans_id)
591 {
592     struct list_head *entry = &SB_JOURNAL(s)->j_journal_list;
593     struct reiserfs_journal_list *jl;
594
595     if (!list_empty(entry)) {
596         jl = JOURNAL_LIST_ENTRY(entry->next);
597         if (jl->j_trans_id <= trans_id) {
598             return 1;
599         }
600     }
601     return 0;
602 }
603
604 static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate) {
605     char b[BDEVNAME_SIZE];
606
607     if (buffer_journaled(bh)) {
608         reiserfs_warning(NULL, "clm-2084: pinned buffer %lu:%s sent to disk",
609                          bh->b_blocknr, bdevname(bh->b_bdev, b)) ;
610     }
611     if (uptodate)
612         set_buffer_uptodate(bh) ;
613     else
614         clear_buffer_uptodate(bh) ;
615     unlock_buffer(bh) ;
616     put_bh(bh) ;
617 }
618
619 static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate) {
620     if (uptodate)
621         set_buffer_uptodate(bh) ;
622     else
623         clear_buffer_uptodate(bh) ;
624     unlock_buffer(bh) ;
625     put_bh(bh) ;
626 }
627
628 static void submit_logged_buffer(struct buffer_head *bh) {
629     get_bh(bh) ;
630     bh->b_end_io = reiserfs_end_buffer_io_sync ;
631     mark_buffer_notjournal_new(bh) ;
632     clear_buffer_dirty(bh) ;
633     if (!test_and_clear_bit(BH_JTest, &bh->b_state))
634         BUG();
635     if (!buffer_uptodate(bh))
636         BUG();
637     submit_bh(WRITE, bh) ;
638 }
639
640 static void submit_ordered_buffer(struct buffer_head *bh) {
641     get_bh(bh) ;
642     bh->b_end_io = reiserfs_end_ordered_io;
643     clear_buffer_dirty(bh) ;
644     if (!buffer_uptodate(bh))
645         BUG();
646     submit_bh(WRITE, bh) ;
647 }
648
649 static int submit_barrier_buffer(struct buffer_head *bh) {
650     get_bh(bh) ;
651     bh->b_end_io = reiserfs_end_ordered_io;
652     clear_buffer_dirty(bh) ;
653     if (!buffer_uptodate(bh))
654         BUG();
655     return submit_bh(WRITE_BARRIER, bh) ;
656 }
657
658 static void check_barrier_completion(struct super_block *s,
659                                      struct buffer_head *bh) {
660     if (buffer_eopnotsupp(bh)) {
661         clear_buffer_eopnotsupp(bh);
662         disable_barrier(s);
663         set_buffer_uptodate(bh);
664         set_buffer_dirty(bh);
665         sync_dirty_buffer(bh);
666     }
667 }
668
669 #define CHUNK_SIZE 32
670 struct buffer_chunk {
671     struct buffer_head *bh[CHUNK_SIZE];
672     int nr;
673 };
674
675 static void write_chunk(struct buffer_chunk *chunk) {
676     int i;
677     for (i = 0; i < chunk->nr ; i++) {
678         submit_logged_buffer(chunk->bh[i]) ;
679     }
680     chunk->nr = 0;
681 }
682
683 static void write_ordered_chunk(struct buffer_chunk *chunk) {
684     int i;
685     for (i = 0; i < chunk->nr ; i++) {
686         submit_ordered_buffer(chunk->bh[i]) ;
687     }
688     chunk->nr = 0;
689 }
690
691 static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
692                          spinlock_t *lock,
693                          void (fn)(struct buffer_chunk *))
694 {
695     int ret = 0;
696     if (chunk->nr >= CHUNK_SIZE)
697         BUG();
698     chunk->bh[chunk->nr++] = bh;
699     if (chunk->nr >= CHUNK_SIZE) {
700         ret = 1;
701         if (lock)
702             spin_unlock(lock);
703         fn(chunk);
704         if (lock)
705             spin_lock(lock);
706     }
707     return ret;
708 }
709
710
711 atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
712 static struct reiserfs_jh *alloc_jh(void) {
713     struct reiserfs_jh *jh;
714     while(1) {
715         jh = kmalloc(sizeof(*jh), GFP_NOFS);
716         if (jh) {
717             atomic_inc(&nr_reiserfs_jh);
718             return jh;
719         }
720         yield();
721     }
722 }
723
724 /*
725  * we want to free the jh when the buffer has been written
726  * and waited on
727  */
728 void reiserfs_free_jh(struct buffer_head *bh) {
729     struct reiserfs_jh *jh;
730
731     jh = bh->b_private;
732     if (jh) {
733         bh->b_private = NULL;
734         jh->bh = NULL;
735         list_del_init(&jh->list);
736         kfree(jh);
737         if (atomic_read(&nr_reiserfs_jh) <= 0)
738             BUG();
739         atomic_dec(&nr_reiserfs_jh);
740         put_bh(bh);
741     }
742 }
743
744 static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
745                            int tail)
746 {
747     struct reiserfs_jh *jh;
748
749     if (bh->b_private) {
750         spin_lock(&j->j_dirty_buffers_lock);
751         if (!bh->b_private) {
752             spin_unlock(&j->j_dirty_buffers_lock);
753             goto no_jh;
754         }
755         jh = bh->b_private;
756         list_del_init(&jh->list);
757     } else {
758 no_jh:
759         get_bh(bh);
760         jh = alloc_jh();
761         spin_lock(&j->j_dirty_buffers_lock);
762         /* buffer must be locked for __add_jh, should be able to have
763          * two adds at the same time
764          */
765         if (bh->b_private)
766             BUG();
767         jh->bh = bh;
768         bh->b_private = jh;
769     }
770     jh->jl = j->j_current_jl;
771     if (tail)
772         list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
773     else {
774         list_add_tail(&jh->list, &jh->jl->j_bh_list);
775     }
776     spin_unlock(&j->j_dirty_buffers_lock);
777     return 0;
778 }
779
780 int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh) {
781     return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
782 }
783 int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh) {
784     return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
785 }
786
787 #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
788 static int write_ordered_buffers(spinlock_t *lock,
789                                  struct reiserfs_journal *j,
790                                  struct reiserfs_journal_list *jl,
791                                  struct list_head *list)
792 {
793     struct buffer_head *bh;
794     struct reiserfs_jh *jh;
795     int ret = 0;
796     struct buffer_chunk chunk;
797     struct list_head tmp;
798     INIT_LIST_HEAD(&tmp);
799
800     chunk.nr = 0;
801     spin_lock(lock);
802     while(!list_empty(list)) {
803         jh = JH_ENTRY(list->next);
804         bh = jh->bh;
805         get_bh(bh);
806         if (test_set_buffer_locked(bh)) {
807             if (!buffer_dirty(bh)) {
808                 list_del_init(&jh->list);
809                 list_add(&jh->list, &tmp);
810                 goto loop_next;
811             }
812             spin_unlock(lock);
813             if (chunk.nr)
814                 write_ordered_chunk(&chunk);
815             wait_on_buffer(bh);
816             cond_resched();
817             spin_lock(lock);
818             goto loop_next;
819         }
820         if (buffer_dirty(bh)) {
821             list_del_init(&jh->list);
822             list_add(&jh->list, &tmp);
823             add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
824         } else {
825             reiserfs_free_jh(bh);
826             unlock_buffer(bh);
827         }
828 loop_next:
829         put_bh(bh);
830         cond_resched_lock(lock);
831     }
832     if (chunk.nr) {
833         spin_unlock(lock);
834         write_ordered_chunk(&chunk);
835         spin_lock(lock);
836     }
837     while(!list_empty(&tmp)) {
838         jh = JH_ENTRY(tmp.prev);
839         bh = jh->bh;
840         get_bh(bh);
841         reiserfs_free_jh(bh);
842
843         if (buffer_locked(bh)) {
844             spin_unlock(lock);
845             wait_on_buffer(bh);
846             spin_lock(lock);
847         }
848         if (!buffer_uptodate(bh))
849             ret = -EIO;
850         put_bh(bh);
851         cond_resched_lock(lock);
852     }
853     spin_unlock(lock);
854     return ret;
855 }
856
857 static int flush_older_commits(struct super_block *s, struct reiserfs_journal_list *jl) {
858     struct reiserfs_journal_list *other_jl;
859     struct reiserfs_journal_list *first_jl;
860     struct list_head *entry;
861     unsigned long trans_id = jl->j_trans_id;
862     unsigned long other_trans_id;
863     unsigned long first_trans_id;
864
865 find_first:
866     /*
867      * first we walk backwards to find the oldest uncommitted transation
868      */
869     first_jl = jl;
870     entry = jl->j_list.prev;
871     while(1) {
872         other_jl = JOURNAL_LIST_ENTRY(entry);
873         if (entry == &SB_JOURNAL(s)->j_journal_list ||
874             atomic_read(&other_jl->j_older_commits_done))
875             break;
876
877         first_jl = other_jl;
878         entry = other_jl->j_list.prev;
879     }
880
881     /* if we didn't find any older uncommitted transactions, return now */
882     if (first_jl == jl) {
883         return 0;
884     }
885
886     first_trans_id = first_jl->j_trans_id;
887
888     entry = &first_jl->j_list;
889     while(1) {
890         other_jl = JOURNAL_LIST_ENTRY(entry);
891         other_trans_id = other_jl->j_trans_id;
892
893         if (other_trans_id < trans_id) {
894             if (atomic_read(&other_jl->j_commit_left) != 0) {
895                 flush_commit_list(s, other_jl, 0);
896
897                 /* list we were called with is gone, return */
898                 if (!journal_list_still_alive(s, trans_id))
899                     return 1;
900
901                 /* the one we just flushed is gone, this means all
902                  * older lists are also gone, so first_jl is no longer
903                  * valid either.  Go back to the beginning.
904                  */
905                 if (!journal_list_still_alive(s, other_trans_id)) {
906                     goto find_first;
907                 }
908             }
909             entry = entry->next;
910             if (entry == &SB_JOURNAL(s)->j_journal_list)
911                 return 0;
912         } else {
913             return 0;
914         }
915     }
916     return 0;
917 }
918 int reiserfs_async_progress_wait(struct super_block *s) {
919     DEFINE_WAIT(wait);
920     struct reiserfs_journal *j = SB_JOURNAL(s);
921     if (atomic_read(&j->j_async_throttle))
922         blk_congestion_wait(WRITE, HZ/10);
923     return 0;
924 }
925
926 /*
927 ** if this journal list still has commit blocks unflushed, send them to disk.
928 **
929 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
930 ** Before the commit block can by written, every other log block must be safely on disk
931 **
932 */
933 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) {
934   int i;
935   int bn ;
936   struct buffer_head *tbh = NULL ;
937   unsigned long trans_id = jl->j_trans_id;
938   int barrier = 0;
939
940   reiserfs_check_lock_depth(s, "flush_commit_list") ;
941
942   if (atomic_read(&jl->j_older_commits_done)) {
943     return 0 ;
944   }
945
946   /* before we can put our commit blocks on disk, we have to make sure everyone older than
947   ** us is on disk too
948   */
949   if (jl->j_len <= 0)
950     BUG();
951   if (trans_id == SB_JOURNAL(s)->j_trans_id)
952     BUG();
953
954   get_journal_list(jl);
955   if (flushall) {
956     if (flush_older_commits(s, jl) == 1) {
957       /* list disappeared during flush_older_commits.  return */
958       goto put_jl;
959     }
960   }
961
962   /* make sure nobody is trying to flush this one at the same time */
963   down(&jl->j_commit_lock);
964   if (!journal_list_still_alive(s, trans_id)) {
965     up(&jl->j_commit_lock);
966     goto put_jl;
967   }
968   if (jl->j_trans_id == 0)
969     BUG();
970
971   /* this commit is done, exit */
972   if (atomic_read(&(jl->j_commit_left)) <= 0) {
973     if (flushall) {
974       atomic_set(&(jl->j_older_commits_done), 1) ;
975     }
976     up(&jl->j_commit_lock);
977     goto put_jl;
978   }
979
980   if (!list_empty(&jl->j_bh_list)) {
981       unlock_kernel();
982       write_ordered_buffers(&SB_JOURNAL(s)->j_dirty_buffers_lock,
983                             SB_JOURNAL(s), jl, &jl->j_bh_list);
984       lock_kernel();
985   }
986   if (!list_empty(&jl->j_bh_list))
987       BUG();
988   /*
989    * for the description block and all the log blocks, submit any buffers
990    * that haven't already reached the disk
991    */
992   atomic_inc(&SB_JOURNAL(s)->j_async_throttle);
993   for (i = 0 ; i < (jl->j_len + 1) ; i++) {
994     bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start+i) %
995          SB_ONDISK_JOURNAL_SIZE(s);
996     tbh = journal_find_get_block(s, bn) ;
997     if (buffer_dirty(tbh))
998         ll_rw_block(WRITE, 1, &tbh) ;
999     put_bh(tbh) ;
1000   }
1001   atomic_dec(&SB_JOURNAL(s)->j_async_throttle);
1002
1003   /* wait on everything written so far before writing the commit
1004    * if we are in barrier mode, send the commit down now
1005    */
1006   barrier = reiserfs_barrier_flush(s);
1007   if (barrier) {
1008       int ret;
1009       lock_buffer(jl->j_commit_bh);
1010       ret = submit_barrier_buffer(jl->j_commit_bh);
1011       if (ret == -EOPNOTSUPP) {
1012           set_buffer_uptodate(jl->j_commit_bh);
1013           disable_barrier(s);
1014           barrier = 0;
1015       }
1016   }
1017   for (i = 0 ;  i < (jl->j_len + 1) ; i++) {
1018     bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
1019          (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s) ;
1020     tbh = journal_find_get_block(s, bn) ;
1021     wait_on_buffer(tbh) ;
1022     // since we're using ll_rw_blk above, it might have skipped over
1023     // a locked buffer.  Double check here
1024     //
1025     if (buffer_dirty(tbh))
1026       sync_dirty_buffer(tbh);
1027     if (!buffer_uptodate(tbh)) {
1028       reiserfs_panic(s, "journal-601, buffer write failed\n") ;
1029     }
1030     put_bh(tbh) ; /* once for journal_find_get_block */
1031     put_bh(tbh) ;    /* once due to original getblk in do_journal_end */
1032     atomic_dec(&(jl->j_commit_left)) ;
1033   }
1034
1035   if (atomic_read(&(jl->j_commit_left)) != 1)
1036     BUG();
1037
1038   if (!barrier) {
1039       if (buffer_dirty(jl->j_commit_bh))
1040         BUG();
1041       mark_buffer_dirty(jl->j_commit_bh) ;
1042       sync_dirty_buffer(jl->j_commit_bh) ;
1043   } else
1044       wait_on_buffer(jl->j_commit_bh);
1045
1046   check_barrier_completion(s, jl->j_commit_bh);
1047   if (!buffer_uptodate(jl->j_commit_bh)) {
1048     reiserfs_panic(s, "journal-615: buffer write failed\n") ;
1049   }
1050   bforget(jl->j_commit_bh) ;
1051   if (SB_JOURNAL(s)->j_last_commit_id != 0 &&
1052      (jl->j_trans_id - SB_JOURNAL(s)->j_last_commit_id) != 1) {
1053       reiserfs_warning(s, "clm-2200: last commit %lu, current %lu",
1054                        SB_JOURNAL(s)->j_last_commit_id,
1055                        jl->j_trans_id);
1056   }
1057   SB_JOURNAL(s)->j_last_commit_id = jl->j_trans_id;
1058
1059   /* now, every commit block is on the disk.  It is safe to allow blocks freed during this transaction to be reallocated */
1060   cleanup_freed_for_journal_list(s, jl) ;
1061
1062   /* mark the metadata dirty */
1063   dirty_one_transaction(s, jl);
1064   atomic_dec(&(jl->j_commit_left)) ;
1065
1066   if (flushall) {
1067     atomic_set(&(jl->j_older_commits_done), 1) ;
1068   }
1069   up(&jl->j_commit_lock);
1070 put_jl:
1071   put_journal_list(s, jl);
1072   return 0 ;
1073 }
1074
1075 /*
1076 ** flush_journal_list frequently needs to find a newer transaction for a given block.  This does that, or 
1077 ** returns NULL if it can't find anything 
1078 */
1079 static struct reiserfs_journal_list *find_newer_jl_for_cn(struct reiserfs_journal_cnode *cn) {
1080   struct super_block *sb = cn->sb;
1081   b_blocknr_t blocknr = cn->blocknr ;
1082
1083   cn = cn->hprev ;
1084   while(cn) {
1085     if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
1086       return cn->jlist ;
1087     }
1088     cn = cn->hprev ;
1089   }
1090   return NULL ;
1091 }
1092
1093 void remove_journal_hash(struct super_block *, struct reiserfs_journal_cnode **,
1094 struct reiserfs_journal_list *, unsigned long, int);
1095
1096 /*
1097 ** once all the real blocks have been flushed, it is safe to remove them from the
1098 ** journal list for this transaction.  Aside from freeing the cnode, this also allows the
1099 ** block to be reallocated for data blocks if it had been deleted.
1100 */
1101 static void remove_all_from_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl, int debug) {
1102   struct reiserfs_journal_cnode *cn, *last ;
1103   cn = jl->j_realblock ;
1104
1105   /* which is better, to lock once around the whole loop, or
1106   ** to lock for each call to remove_journal_hash?
1107   */
1108   while(cn) {
1109     if (cn->blocknr != 0) {
1110       if (debug) {
1111        reiserfs_warning (p_s_sb, "block %u, bh is %d, state %ld", cn->blocknr,
1112                          cn->bh ? 1: 0, cn->state) ;
1113       }
1114       cn->state = 0 ;
1115       remove_journal_hash(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_hash_table, jl, cn->blocknr, 1) ;
1116     }
1117     last = cn ;
1118     cn = cn->next ;
1119     free_cnode(p_s_sb, last) ;
1120   }
1121   jl->j_realblock = NULL ;
1122 }
1123
1124 /*
1125 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1126 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1127 ** releasing blocks in this transaction for reuse as data blocks.
1128 ** called by flush_journal_list, before it calls remove_all_from_journal_list
1129 **
1130 */
1131 static int _update_journal_header_block(struct super_block *p_s_sb, unsigned long offset, unsigned long trans_id) {
1132   struct reiserfs_journal_header *jh ;
1133   if (trans_id >= SB_JOURNAL(p_s_sb)->j_last_flush_trans_id) {
1134     if (buffer_locked((SB_JOURNAL(p_s_sb)->j_header_bh)))  {
1135       wait_on_buffer((SB_JOURNAL(p_s_sb)->j_header_bh)) ;
1136       if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
1137         reiserfs_panic(p_s_sb, "journal-699: buffer write failed\n") ;
1138       }
1139     }
1140     SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
1141     SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = offset ;
1142     jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
1143     jh->j_last_flush_trans_id = cpu_to_le32(trans_id) ;
1144     jh->j_first_unflushed_offset = cpu_to_le32(offset) ;
1145     jh->j_mount_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_mount_id) ;
1146
1147     if (reiserfs_barrier_flush(p_s_sb)) {
1148         int ret;
1149         lock_buffer(SB_JOURNAL(p_s_sb)->j_header_bh);
1150         ret = submit_barrier_buffer(SB_JOURNAL(p_s_sb)->j_header_bh);
1151         if (ret == -EOPNOTSUPP) {
1152             set_buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh);
1153             disable_barrier(p_s_sb);
1154             goto sync;
1155         }
1156         wait_on_buffer(SB_JOURNAL(p_s_sb)->j_header_bh);
1157         check_barrier_completion(p_s_sb, SB_JOURNAL(p_s_sb)->j_header_bh);
1158     } else {
1159 sync:
1160         set_buffer_dirty(SB_JOURNAL(p_s_sb)->j_header_bh) ;
1161         sync_dirty_buffer(SB_JOURNAL(p_s_sb)->j_header_bh) ;
1162     }
1163     if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
1164       reiserfs_warning (p_s_sb, "journal-837: IO error during journal replay");
1165       return -EIO ;
1166     }
1167   }
1168   return 0 ;
1169 }
1170
1171 static int update_journal_header_block(struct super_block *p_s_sb, 
1172                                        unsigned long offset, 
1173                                        unsigned long trans_id) {
1174     if (_update_journal_header_block(p_s_sb, offset, trans_id)) {
1175         reiserfs_panic(p_s_sb, "journal-712: buffer write failed\n") ;
1176     }
1177     return 0 ;
1178 }
1179 /* 
1180 ** flush any and all journal lists older than you are 
1181 ** can only be called from flush_journal_list
1182 */
1183 static int flush_older_journal_lists(struct super_block *p_s_sb,
1184                                      struct reiserfs_journal_list *jl)
1185 {
1186     struct list_head *entry;
1187     struct reiserfs_journal_list *other_jl ;
1188     unsigned long trans_id = jl->j_trans_id;
1189
1190     /* we know we are the only ones flushing things, no extra race
1191      * protection is required.
1192      */
1193 restart:
1194     entry = SB_JOURNAL(p_s_sb)->j_journal_list.next;
1195     other_jl = JOURNAL_LIST_ENTRY(entry);
1196     if (other_jl->j_trans_id < trans_id) {
1197         /* do not flush all */
1198         flush_journal_list(p_s_sb, other_jl, 0) ;
1199
1200         /* other_jl is now deleted from the list */
1201         goto restart;
1202     }
1203     return 0 ;
1204 }
1205
1206 static void del_from_work_list(struct super_block *s,
1207                                struct reiserfs_journal_list *jl) {
1208     if (!list_empty(&jl->j_working_list)) {
1209         list_del_init(&jl->j_working_list);
1210         SB_JOURNAL(s)->j_num_work_lists--;
1211     }
1212 }
1213
1214 /* flush a journal list, both commit and real blocks
1215 **
1216 ** always set flushall to 1, unless you are calling from inside
1217 ** flush_journal_list
1218 **
1219 ** IMPORTANT.  This can only be called while there are no journal writers, 
1220 ** and the journal is locked.  That means it can only be called from 
1221 ** do_journal_end, or by journal_release
1222 */
1223 static int flush_journal_list(struct super_block *s, 
1224                               struct reiserfs_journal_list *jl, int flushall) {
1225   struct reiserfs_journal_list *pjl ;
1226   struct reiserfs_journal_cnode *cn, *last ;
1227   int count ;
1228   int was_jwait = 0 ;
1229   int was_dirty = 0 ;
1230   struct buffer_head *saved_bh ; 
1231   unsigned long j_len_saved = jl->j_len ;
1232
1233   if (j_len_saved <= 0) {
1234     BUG();
1235   }
1236
1237   if (atomic_read(&SB_JOURNAL(s)->j_wcount) != 0) {
1238     reiserfs_warning(s, "clm-2048: flush_journal_list called with wcount %d",
1239                       atomic_read(&SB_JOURNAL(s)->j_wcount)) ;
1240   }
1241   if (jl->j_trans_id == 0)
1242     BUG();
1243
1244   /* if flushall == 0, the lock is already held */
1245   if (flushall) {
1246       down(&SB_JOURNAL(s)->j_flush_sem);
1247   } else if (!down_trylock(&SB_JOURNAL(s)->j_flush_sem)) {
1248       BUG();
1249   }
1250
1251   count = 0 ;
1252   if (j_len_saved > SB_JOURNAL_TRANS_MAX(s)) {
1253     reiserfs_panic(s, "journal-715: flush_journal_list, length is %lu, trans id %lu\n", j_len_saved, jl->j_trans_id);
1254     return 0 ;
1255   }
1256
1257   /* if all the work is already done, get out of here */
1258   if (atomic_read(&(jl->j_nonzerolen)) <= 0 && 
1259       atomic_read(&(jl->j_commit_left)) <= 0) {
1260     goto flush_older_and_return ;
1261   } 
1262
1263   /* start by putting the commit list on disk.  This will also flush 
1264   ** the commit lists of any olders transactions
1265   */
1266   flush_commit_list(s, jl, 1) ;
1267
1268   if (!(jl->j_state & LIST_DIRTY))
1269       BUG();
1270
1271   /* are we done now? */
1272   if (atomic_read(&(jl->j_nonzerolen)) <= 0 && 
1273       atomic_read(&(jl->j_commit_left)) <= 0) {
1274     goto flush_older_and_return ;
1275   }
1276
1277   /* loop through each cnode, see if we need to write it, 
1278   ** or wait on a more recent transaction, or just ignore it 
1279   */
1280   if (atomic_read(&(SB_JOURNAL(s)->j_wcount)) != 0) {
1281     reiserfs_panic(s, "journal-844: panic journal list is flushing, wcount is not 0\n") ;
1282   }
1283   cn = jl->j_realblock ;
1284   while(cn) {
1285     was_jwait = 0 ;
1286     was_dirty = 0 ;
1287     saved_bh = NULL ;
1288     /* blocknr of 0 is no longer in the hash, ignore it */
1289     if (cn->blocknr == 0) {
1290       goto free_cnode ;
1291     }
1292     pjl = find_newer_jl_for_cn(cn) ;
1293     /* the order is important here.  We check pjl to make sure we
1294     ** don't clear BH_JDirty_wait if we aren't the one writing this
1295     ** block to disk
1296     */
1297     if (!pjl && cn->bh) {
1298       saved_bh = cn->bh ;
1299
1300       /* we do this to make sure nobody releases the buffer while 
1301       ** we are working with it 
1302       */
1303       get_bh(saved_bh) ;
1304
1305       if (buffer_journal_dirty(saved_bh)) {
1306         if (!can_dirty(cn))
1307           BUG();
1308         was_jwait = 1 ;
1309         was_dirty = 1 ;
1310       } else if (can_dirty(cn)) {
1311         /* everything with !pjl && jwait should be writable */
1312         BUG();
1313       }
1314     }
1315
1316     /* if someone has this block in a newer transaction, just make
1317     ** sure they are commited, and don't try writing it to disk
1318     */
1319     if (pjl) {
1320       if (atomic_read(&pjl->j_commit_left))
1321         flush_commit_list(s, pjl, 1) ;
1322       goto free_cnode ;
1323     }
1324
1325     /* bh == NULL when the block got to disk on its own, OR, 
1326     ** the block got freed in a future transaction 
1327     */
1328     if (saved_bh == NULL) {
1329       goto free_cnode ;
1330     }
1331
1332     /* this should never happen.  kupdate_one_transaction has this list
1333     ** locked while it works, so we should never see a buffer here that
1334     ** is not marked JDirty_wait
1335     */
1336     if ((!was_jwait) && !buffer_locked(saved_bh)) {
1337         reiserfs_warning (s, "journal-813: BAD! buffer %llu %cdirty %cjwait, "
1338                           "not in a newer tranasction",
1339                           (unsigned long long)saved_bh->b_blocknr,
1340                           was_dirty ? ' ' : '!', was_jwait ? ' ' : '!') ;
1341     }
1342     if (was_dirty) { 
1343       /* we inc again because saved_bh gets decremented at free_cnode */
1344       get_bh(saved_bh) ;
1345       set_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
1346       lock_buffer(saved_bh);
1347       if (cn->blocknr != saved_bh->b_blocknr)
1348         BUG();
1349       if (buffer_dirty(saved_bh))
1350         submit_logged_buffer(saved_bh) ;
1351       else
1352         unlock_buffer(saved_bh);
1353       count++ ;
1354     } else {
1355       reiserfs_warning (s, "clm-2082: Unable to flush buffer %llu in %s",
1356                         (unsigned long long)saved_bh->b_blocknr, __FUNCTION__);
1357     }
1358 free_cnode:
1359     last = cn ;
1360     cn = cn->next ;
1361     if (saved_bh) {
1362       /* we incremented this to keep others from taking the buffer head away */
1363       put_bh(saved_bh) ;
1364       if (atomic_read(&(saved_bh->b_count)) < 0) {
1365         reiserfs_warning (s, "journal-945: saved_bh->b_count < 0");
1366       }
1367     }
1368   }
1369   if (count > 0) {
1370     cn = jl->j_realblock ;
1371     while(cn) {
1372       if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1373         if (!cn->bh) {
1374           reiserfs_panic(s, "journal-1011: cn->bh is NULL\n") ;
1375         }
1376         wait_on_buffer(cn->bh) ;
1377         if (!cn->bh) {
1378           reiserfs_panic(s, "journal-1012: cn->bh is NULL\n") ;
1379         }
1380         if (!buffer_uptodate(cn->bh)) {
1381           reiserfs_panic(s, "journal-949: buffer write failed\n") ;
1382         }
1383         /* note, we must clear the JDirty_wait bit after the up to date
1384         ** check, otherwise we race against our flushpage routine
1385         */
1386         if (!test_and_clear_bit(BH_JDirty_wait, &cn->bh->b_state))
1387             BUG();
1388
1389         /* undo the inc from journal_mark_dirty */
1390         put_bh(cn->bh) ;
1391         brelse(cn->bh) ;
1392       }
1393       cn = cn->next ;
1394     }
1395   }
1396
1397 flush_older_and_return:
1398   /* before we can update the journal header block, we _must_ flush all 
1399   ** real blocks from all older transactions to disk.  This is because
1400   ** once the header block is updated, this transaction will not be
1401   ** replayed after a crash
1402   */
1403   if (flushall) {
1404     flush_older_journal_lists(s, jl);
1405   } 
1406   
1407   /* before we can remove everything from the hash tables for this 
1408   ** transaction, we must make sure it can never be replayed
1409   **
1410   ** since we are only called from do_journal_end, we know for sure there
1411   ** are no allocations going on while we are flushing journal lists.  So,
1412   ** we only need to update the journal header block for the last list
1413   ** being flushed
1414   */
1415   if (flushall) {
1416     update_journal_header_block(s, (jl->j_start + jl->j_len + 2) % SB_ONDISK_JOURNAL_SIZE(s), jl->j_trans_id) ;
1417   }
1418   remove_all_from_journal_list(s, jl, 0) ;
1419   list_del(&jl->j_list);
1420   SB_JOURNAL(s)->j_num_lists--;
1421   del_from_work_list(s, jl);
1422
1423   if (SB_JOURNAL(s)->j_last_flush_id != 0 &&
1424      (jl->j_trans_id - SB_JOURNAL(s)->j_last_flush_id) != 1) {
1425       reiserfs_warning(s, "clm-2201: last flush %lu, current %lu",
1426                        SB_JOURNAL(s)->j_last_flush_id,
1427                        jl->j_trans_id);
1428   }
1429   SB_JOURNAL(s)->j_last_flush_id = jl->j_trans_id;
1430
1431   /* not strictly required since we are freeing the list, but it should
1432    * help find code using dead lists later on
1433    */
1434   jl->j_len = 0 ;
1435   atomic_set(&(jl->j_nonzerolen), 0) ;
1436   jl->j_start = 0 ;
1437   jl->j_realblock = NULL ;
1438   jl->j_commit_bh = NULL ;
1439   jl->j_trans_id = 0 ;
1440   jl->j_state = 0;
1441   put_journal_list(s, jl);
1442   if (flushall)
1443     up(&SB_JOURNAL(s)->j_flush_sem);
1444   return 0 ;
1445
1446
1447 static int write_one_transaction(struct super_block *s,
1448                                  struct reiserfs_journal_list *jl,
1449                                  struct buffer_chunk *chunk)
1450 {
1451     struct reiserfs_journal_cnode *cn;
1452     int ret = 0 ;
1453
1454     jl->j_state |= LIST_TOUCHED;
1455     del_from_work_list(s, jl);
1456     if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
1457         return 0;
1458     }
1459
1460     cn = jl->j_realblock ;
1461     while(cn) {
1462         /* if the blocknr == 0, this has been cleared from the hash,
1463         ** skip it
1464         */
1465         if (cn->blocknr == 0) {
1466             goto next ;
1467         }
1468         if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
1469             struct buffer_head *tmp_bh;
1470             /* we can race against journal_mark_freed when we try
1471              * to lock_buffer(cn->bh), so we have to inc the buffer
1472              * count, and recheck things after locking
1473              */
1474             tmp_bh = cn->bh;
1475             get_bh(tmp_bh);
1476             lock_buffer(tmp_bh);
1477             if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
1478                 if (!buffer_journal_dirty(tmp_bh) ||
1479                     reiserfs_buffer_prepared(tmp_bh))
1480                     BUG();
1481                 add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
1482                 ret++;
1483             } else {
1484                 /* note, cn->bh might be null now */
1485                 unlock_buffer(tmp_bh);
1486             }
1487             put_bh(tmp_bh);
1488         }
1489 next:
1490         cn = cn->next ;
1491         cond_resched();
1492     }
1493     return ret ;
1494 }
1495
1496 /* used by flush_commit_list */
1497 static int dirty_one_transaction(struct super_block *s,
1498                                  struct reiserfs_journal_list *jl)
1499 {
1500     struct reiserfs_journal_cnode *cn;
1501     struct reiserfs_journal_list *pjl;
1502     int ret = 0 ;
1503
1504     jl->j_state |= LIST_DIRTY;
1505     cn = jl->j_realblock ;
1506     while(cn) {
1507         /* look for a more recent transaction that logged this
1508         ** buffer.  Only the most recent transaction with a buffer in
1509         ** it is allowed to send that buffer to disk
1510         */
1511         pjl = find_newer_jl_for_cn(cn) ;
1512         if (!pjl && cn->blocknr && cn->bh && buffer_journal_dirty(cn->bh))
1513         {
1514             if (!can_dirty(cn))
1515                 BUG();
1516             /* if the buffer is prepared, it will either be logged
1517              * or restored.  If restored, we need to make sure
1518              * it actually gets marked dirty
1519              */
1520             mark_buffer_notjournal_new(cn->bh) ;
1521             if (test_bit(BH_JPrepared, &cn->bh->b_state)) {
1522                 set_bit(BH_JRestore_dirty, &cn->bh->b_state);
1523             } else {
1524                 set_bit(BH_JTest, &cn->bh->b_state);
1525                 mark_buffer_dirty(cn->bh);
1526             }
1527         } 
1528         cn = cn->next ;
1529     }
1530     return ret ;
1531 }
1532
1533 static int kupdate_transactions(struct super_block *s,
1534                                    struct reiserfs_journal_list *jl,
1535                                    struct reiserfs_journal_list **next_jl,
1536                                    unsigned long *next_trans_id,
1537                                    int num_blocks,
1538                                    int num_trans) {
1539     int ret = 0;
1540     int written = 0 ;
1541     int transactions_flushed = 0;
1542     unsigned long orig_trans_id = jl->j_trans_id;
1543     struct buffer_chunk chunk;
1544     struct list_head *entry;
1545     chunk.nr = 0;
1546
1547     down(&SB_JOURNAL(s)->j_flush_sem);
1548     if (!journal_list_still_alive(s, orig_trans_id)) {
1549         goto done;
1550     }
1551
1552     /* we've got j_flush_sem held, nobody is going to delete any
1553      * of these lists out from underneath us
1554      */
1555     while((num_trans && transactions_flushed < num_trans) ||
1556           (!num_trans && written < num_blocks)) {
1557
1558         if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
1559             atomic_read(&jl->j_commit_left))
1560         {
1561             del_from_work_list(s, jl);
1562             break;
1563         }
1564         ret = write_one_transaction(s, jl, &chunk);
1565
1566         if (ret < 0)
1567             goto done;
1568         transactions_flushed++;
1569         written += ret;
1570         entry = jl->j_list.next;
1571
1572         /* did we wrap? */
1573         if (entry == &SB_JOURNAL(s)->j_journal_list) {
1574             break;
1575         }
1576         jl = JOURNAL_LIST_ENTRY(entry);
1577
1578         /* don't bother with older transactions */
1579         if (jl->j_trans_id <= orig_trans_id)
1580             break;
1581     }
1582     if (chunk.nr) {
1583         write_chunk(&chunk);
1584     }
1585
1586 done:
1587     up(&SB_JOURNAL(s)->j_flush_sem);
1588     return ret;
1589 }
1590
1591 /* for o_sync and fsync heavy applications, they tend to use
1592 ** all the journa list slots with tiny transactions.  These
1593 ** trigger lots and lots of calls to update the header block, which
1594 ** adds seeks and slows things down.
1595 **
1596 ** This function tries to clear out a large chunk of the journal lists
1597 ** at once, which makes everything faster since only the newest journal
1598 ** list updates the header block
1599 */
1600 static int flush_used_journal_lists(struct super_block *s,
1601                                     struct reiserfs_journal_list *jl) {
1602     unsigned long len = 0;
1603     unsigned long cur_len;
1604     int ret;
1605     int i;
1606     int limit = 256;
1607     struct reiserfs_journal_list *tjl;
1608     struct reiserfs_journal_list *flush_jl;
1609     unsigned long trans_id;
1610
1611     flush_jl = tjl = jl;
1612
1613     /* in data logging mode, try harder to flush a lot of blocks */
1614     if (reiserfs_data_log(s))
1615         limit = 1024;
1616     /* flush for 256 transactions or limit blocks, whichever comes first */
1617     for(i = 0 ; i < 256 && len < limit ; i++) {
1618         if (atomic_read(&tjl->j_commit_left) ||
1619             tjl->j_trans_id < jl->j_trans_id) {
1620             break;
1621         }
1622         cur_len = atomic_read(&tjl->j_nonzerolen);
1623         if (cur_len > 0) {
1624             tjl->j_state &= ~LIST_TOUCHED;
1625         }
1626         len += cur_len;
1627         flush_jl = tjl;
1628         if (tjl->j_list.next == &SB_JOURNAL(s)->j_journal_list)
1629             break;
1630         tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
1631     }
1632     /* try to find a group of blocks we can flush across all the
1633     ** transactions, but only bother if we've actually spanned
1634     ** across multiple lists
1635     */
1636     if (flush_jl != jl) {
1637         ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
1638     }
1639     flush_journal_list(s, flush_jl, 1);
1640     return 0;
1641 }
1642
1643 /*
1644 ** removes any nodes in table with name block and dev as bh.
1645 ** only touchs the hnext and hprev pointers.
1646 */
1647 void remove_journal_hash(struct super_block *sb,
1648                         struct reiserfs_journal_cnode **table,
1649                         struct reiserfs_journal_list *jl,
1650                         unsigned long block, int remove_freed)
1651 {
1652   struct reiserfs_journal_cnode *cur ;
1653   struct reiserfs_journal_cnode **head ;
1654
1655   head= &(journal_hash(table, sb, block)) ;
1656   if (!head) {
1657     return ;
1658   }
1659   cur = *head ;
1660   while(cur) {
1661     if (cur->blocknr == block && cur->sb == sb && (jl == NULL || jl == cur->jlist) && 
1662         (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1663       if (cur->hnext) {
1664         cur->hnext->hprev = cur->hprev ;
1665       }
1666       if (cur->hprev) {
1667         cur->hprev->hnext = cur->hnext ;
1668       } else {
1669         *head = cur->hnext ;
1670       }
1671       cur->blocknr = 0 ;
1672       cur->sb = NULL ;
1673       cur->state = 0 ;
1674       if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1675         atomic_dec(&(cur->jlist->j_nonzerolen)) ;
1676       cur->bh = NULL ;
1677       cur->jlist = NULL ;
1678     } 
1679     cur = cur->hnext ;
1680   }
1681 }
1682
1683 static void free_journal_ram(struct super_block *p_s_sb) {
1684   reiserfs_kfree(SB_JOURNAL(p_s_sb)->j_current_jl,
1685                  sizeof(struct reiserfs_journal_list), p_s_sb);
1686   SB_JOURNAL(p_s_sb)->j_num_lists--;
1687
1688   vfree(SB_JOURNAL(p_s_sb)->j_cnode_free_orig) ;
1689   free_list_bitmaps(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap) ;
1690   free_bitmap_nodes(p_s_sb) ; /* must be after free_list_bitmaps */
1691   if (SB_JOURNAL(p_s_sb)->j_header_bh) {
1692     brelse(SB_JOURNAL(p_s_sb)->j_header_bh) ;
1693   }
1694   /* j_header_bh is on the journal dev, make sure not to release the journal
1695    * dev until we brelse j_header_bh
1696    */
1697   release_journal_dev(p_s_sb, SB_JOURNAL(p_s_sb));
1698   vfree(SB_JOURNAL(p_s_sb)) ;
1699 }
1700
1701 /*
1702 ** call on unmount.  Only set error to 1 if you haven't made your way out
1703 ** of read_super() yet.  Any other caller must keep error at 0.
1704 */
1705 static int do_journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, int error) {
1706   struct reiserfs_transaction_handle myth ;
1707
1708   /* we only want to flush out transactions if we were called with error == 0
1709   */
1710   if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
1711     /* end the current trans */
1712     do_journal_end(th, p_s_sb,10, FLUSH_ALL) ;
1713
1714     /* make sure something gets logged to force our way into the flush code */
1715     journal_join(&myth, p_s_sb, 1) ;
1716     reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
1717     journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
1718     do_journal_end(&myth, p_s_sb,1, FLUSH_ALL) ;
1719   }
1720
1721   reiserfs_mounted_fs_count-- ;
1722   /* wait for all commits to finish */
1723   cancel_delayed_work(&SB_JOURNAL(p_s_sb)->j_work);
1724   flush_workqueue(commit_wq);
1725   if (!reiserfs_mounted_fs_count) {
1726     destroy_workqueue(commit_wq);
1727     commit_wq = NULL;
1728   }
1729
1730   free_journal_ram(p_s_sb) ;
1731
1732   return 0 ;
1733 }
1734
1735 /*
1736 ** call on unmount.  flush all journal trans, release all alloc'd ram
1737 */
1738 int journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1739   return do_journal_release(th, p_s_sb, 0) ;
1740 }
1741 /*
1742 ** only call from an error condition inside reiserfs_read_super!
1743 */
1744 int journal_release_error(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1745   return do_journal_release(th, p_s_sb, 1) ;
1746 }
1747
1748 /* compares description block with commit block.  returns 1 if they differ, 0 if they are the same */
1749 static int journal_compare_desc_commit(struct super_block *p_s_sb, struct reiserfs_journal_desc *desc, 
1750                                        struct reiserfs_journal_commit *commit) {
1751   if (get_commit_trans_id (commit) != get_desc_trans_id (desc) || 
1752       get_commit_trans_len (commit) != get_desc_trans_len (desc) || 
1753       get_commit_trans_len (commit) > SB_JOURNAL_TRANS_MAX(p_s_sb) || 
1754       get_commit_trans_len (commit) <= 0 
1755   ) {
1756     return 1 ;
1757   }
1758   return 0 ;
1759 }
1760 /* returns 0 if it did not find a description block  
1761 ** returns -1 if it found a corrupt commit block
1762 ** returns 1 if both desc and commit were valid 
1763 */
1764 static int journal_transaction_is_valid(struct super_block *p_s_sb, struct buffer_head *d_bh, unsigned long *oldest_invalid_trans_id, unsigned long *newest_mount_id) {
1765   struct reiserfs_journal_desc *desc ;
1766   struct reiserfs_journal_commit *commit ;
1767   struct buffer_head *c_bh ;
1768   unsigned long offset ;
1769
1770   if (!d_bh)
1771       return 0 ;
1772
1773   desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1774   if (get_desc_trans_len(desc) > 0 && !memcmp(get_journal_desc_magic (d_bh), JOURNAL_DESC_MAGIC, 8)) {
1775     if (oldest_invalid_trans_id && *oldest_invalid_trans_id && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
1776       reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-986: transaction "
1777                       "is valid returning because trans_id %d is greater than "
1778                       "oldest_invalid %lu", get_desc_trans_id(desc),
1779                        *oldest_invalid_trans_id);
1780       return 0 ;
1781     }
1782     if (newest_mount_id && *newest_mount_id > get_desc_mount_id (desc)) {
1783       reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1087: transaction "
1784                      "is valid returning because mount_id %d is less than "
1785                      "newest_mount_id %lu", get_desc_mount_id (desc),
1786                      *newest_mount_id) ;
1787       return -1 ;
1788     }
1789     if ( get_desc_trans_len(desc) > SB_JOURNAL_TRANS_MAX(p_s_sb) ) {
1790       reiserfs_warning(p_s_sb, "journal-2018: Bad transaction length %d encountered, ignoring transaction", get_desc_trans_len(desc));
1791       return -1 ;
1792     }
1793     offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1794
1795     /* ok, we have a journal description block, lets see if the transaction was valid */
1796     c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1797                  ((offset + get_desc_trans_len(desc) + 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
1798     if (!c_bh)
1799       return 0 ;
1800     commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1801     if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1802       reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, 
1803                      "journal_transaction_is_valid, commit offset %ld had bad "
1804                      "time %d or length %d",
1805                      c_bh->b_blocknr -  SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1806                      get_commit_trans_id (commit), 
1807                      get_commit_trans_len(commit));
1808       brelse(c_bh) ;
1809       if (oldest_invalid_trans_id) {
1810         *oldest_invalid_trans_id = get_desc_trans_id(desc) ;
1811         reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1004: "
1812                        "transaction_is_valid setting oldest invalid trans_id "
1813                        "to %d", get_desc_trans_id(desc)) ;
1814       }
1815       return -1; 
1816     }
1817     brelse(c_bh) ;
1818     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1006: found valid "
1819                    "transaction start offset %llu, len %d id %d",
1820                    d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb), 
1821                    get_desc_trans_len(desc), get_desc_trans_id(desc)) ;
1822     return 1 ;
1823   } else {
1824     return 0 ;
1825   }
1826 }
1827
1828 static void brelse_array(struct buffer_head **heads, int num) {
1829   int i ;
1830   for (i = 0 ; i < num ; i++) {
1831     brelse(heads[i]) ;
1832   }
1833 }
1834
1835 /*
1836 ** given the start, and values for the oldest acceptable transactions,
1837 ** this either reads in a replays a transaction, or returns because the transaction
1838 ** is invalid, or too old.
1839 */
1840 static int journal_read_transaction(struct super_block *p_s_sb, unsigned long cur_dblock, unsigned long oldest_start, 
1841                                     unsigned long oldest_trans_id, unsigned long newest_mount_id) {
1842   struct reiserfs_journal_desc *desc ;
1843   struct reiserfs_journal_commit *commit ;
1844   unsigned long trans_id = 0 ;
1845   struct buffer_head *c_bh ;
1846   struct buffer_head *d_bh ;
1847   struct buffer_head **log_blocks = NULL ;
1848   struct buffer_head **real_blocks = NULL ;
1849   unsigned long trans_offset ;
1850   int i;
1851   int trans_half;
1852
1853   d_bh = journal_bread(p_s_sb, cur_dblock) ;
1854   if (!d_bh)
1855     return 1 ;
1856   desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1857   trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1858   reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
1859                  "journal_read_transaction, offset %llu, len %d mount_id %d",
1860                  d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb), 
1861                  get_desc_trans_len(desc), get_desc_mount_id(desc)) ;
1862   if (get_desc_trans_id(desc) < oldest_trans_id) {
1863     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
1864                    "journal_read_trans skipping because %lu is too old",
1865                    cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)) ;
1866     brelse(d_bh) ;
1867     return 1 ;
1868   }
1869   if (get_desc_mount_id(desc) != newest_mount_id) {
1870     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
1871                    "journal_read_trans skipping because %d is != "
1872                    "newest_mount_id %lu", get_desc_mount_id(desc),
1873                     newest_mount_id) ;
1874     brelse(d_bh) ;
1875     return 1 ;
1876   }
1877   c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1878                 ((trans_offset + get_desc_trans_len(desc) + 1) % 
1879                  SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
1880   if (!c_bh) {
1881     brelse(d_bh) ;
1882     return 1 ;
1883   }
1884   commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1885   if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1886     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal_read_transaction, "
1887                    "commit offset %llu had bad time %d or length %d",
1888                    c_bh->b_blocknr -  SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb), 
1889                    get_commit_trans_id(commit), get_commit_trans_len(commit));
1890     brelse(c_bh) ;
1891     brelse(d_bh) ;
1892     return 1; 
1893   }
1894   trans_id = get_desc_trans_id(desc) ;
1895   /* now we know we've got a good transaction, and it was inside the valid time ranges */
1896   log_blocks = reiserfs_kmalloc(get_desc_trans_len(desc) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
1897   real_blocks = reiserfs_kmalloc(get_desc_trans_len(desc) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
1898   if (!log_blocks  || !real_blocks) {
1899     brelse(c_bh) ;
1900     brelse(d_bh) ;
1901     reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1902     reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1903     reiserfs_warning(p_s_sb, "journal-1169: kmalloc failed, unable to mount FS") ;
1904     return -1 ;
1905   }
1906   /* get all the buffer heads */
1907   trans_half = journal_trans_half (p_s_sb->s_blocksize) ;
1908   for(i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1909     log_blocks[i] =  journal_getblk(p_s_sb,  SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + (trans_offset + 1 + i) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
1910     if (i < trans_half) {
1911       real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(desc->j_realblock[i])) ;
1912     } else {
1913       real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(commit->j_realblock[i - trans_half])) ;
1914     }
1915     if ( real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(p_s_sb) ) {
1916       reiserfs_warning(p_s_sb, "journal-1207: REPLAY FAILURE fsck required! Block to replay is outside of filesystem");
1917       goto abort_replay;
1918     }
1919     /* make sure we don't try to replay onto log or reserved area */
1920     if (is_block_in_log_or_reserved_area(p_s_sb, real_blocks[i]->b_blocknr)) {
1921       reiserfs_warning(p_s_sb, "journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block") ;
1922 abort_replay:
1923       brelse_array(log_blocks, i) ;
1924       brelse_array(real_blocks, i) ;
1925       brelse(c_bh) ;
1926       brelse(d_bh) ;
1927       reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1928       reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1929       return -1 ;
1930     }
1931   }
1932   /* read in the log blocks, memcpy to the corresponding real block */
1933   ll_rw_block(READ, get_desc_trans_len(desc), log_blocks) ;
1934   for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1935     wait_on_buffer(log_blocks[i]) ;
1936     if (!buffer_uptodate(log_blocks[i])) {
1937       reiserfs_warning(p_s_sb, "journal-1212: REPLAY FAILURE fsck required! buffer write failed") ;
1938       brelse_array(log_blocks + i, get_desc_trans_len(desc) - i) ;
1939       brelse_array(real_blocks, get_desc_trans_len(desc)) ;
1940       brelse(c_bh) ;
1941       brelse(d_bh) ;
1942       reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1943       reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1944       return -1 ;
1945     }
1946     memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data, real_blocks[i]->b_size) ;
1947     set_buffer_uptodate(real_blocks[i]) ;
1948     brelse(log_blocks[i]) ;
1949   }
1950   /* flush out the real blocks */
1951   for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1952     set_buffer_dirty(real_blocks[i]) ;
1953     ll_rw_block(WRITE, 1, real_blocks + i) ;
1954   }
1955   for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1956     wait_on_buffer(real_blocks[i]) ; 
1957     if (!buffer_uptodate(real_blocks[i])) {
1958       reiserfs_warning(p_s_sb, "journal-1226: REPLAY FAILURE, fsck required! buffer write failed") ;
1959       brelse_array(real_blocks + i, get_desc_trans_len(desc) - i) ;
1960       brelse(c_bh) ;
1961       brelse(d_bh) ;
1962       reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1963       reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1964       return -1 ;
1965     }
1966     brelse(real_blocks[i]) ;
1967   }
1968   cur_dblock =  SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + ((trans_offset + get_desc_trans_len(desc) + 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)) ;
1969   reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1095: setting journal "
1970                  "start to offset %ld",
1971                  cur_dblock -  SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)) ;
1972   
1973   /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
1974   SB_JOURNAL(p_s_sb)->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1975   SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
1976   SB_JOURNAL(p_s_sb)->j_trans_id = trans_id + 1;
1977   brelse(c_bh) ;
1978   brelse(d_bh) ;
1979   reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
1980   reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
1981   return 0 ;
1982 }
1983
1984 /* This function reads blocks starting from block and to max_block of bufsize
1985    size (but no more than BUFNR blocks at a time). This proved to improve
1986    mounting speed on self-rebuilding raid5 arrays at least.
1987    Right now it is only used from journal code. But later we might use it
1988    from other places.
1989    Note: Do not use journal_getblk/sb_getblk functions here! */
1990 struct buffer_head * reiserfs_breada (struct block_device *dev, int block, int bufsize,
1991                             unsigned int max_block)
1992 {
1993         struct buffer_head * bhlist[BUFNR];
1994         unsigned int blocks = BUFNR;
1995         struct buffer_head * bh;
1996         int i, j;
1997         
1998         bh = __getblk (dev, block, bufsize );
1999         if (buffer_uptodate (bh))
2000                 return (bh);   
2001                 
2002         if (block + BUFNR > max_block) {
2003                 blocks = max_block - block;
2004         }
2005         bhlist[0] = bh;
2006         j = 1;
2007         for (i = 1; i < blocks; i++) {
2008                 bh = __getblk (dev, block + i, bufsize);
2009                 if (buffer_uptodate (bh)) {
2010                         brelse (bh);
2011                         break;
2012                 }
2013                 else bhlist[j++] = bh;
2014         }
2015         ll_rw_block (READ, j, bhlist);
2016         for(i = 1; i < j; i++) 
2017                 brelse (bhlist[i]);
2018         bh = bhlist[0];
2019         wait_on_buffer (bh);
2020         if (buffer_uptodate (bh))
2021                 return bh;
2022         brelse (bh);
2023         return NULL;
2024 }
2025
2026 /*
2027 ** read and replay the log
2028 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2029 ** transaction.  This tests that before finding all the transactions in the log, which makes normal mount times fast.
2030 **
2031 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2032 **
2033 ** On exit, it sets things up so the first transaction will work correctly.
2034 */
2035 static int journal_read(struct super_block *p_s_sb) {
2036   struct reiserfs_journal_desc *desc ;
2037   unsigned long oldest_trans_id = 0;
2038   unsigned long oldest_invalid_trans_id = 0 ;
2039   time_t start ;
2040   unsigned long oldest_start = 0;
2041   unsigned long cur_dblock = 0 ;
2042   unsigned long newest_mount_id = 9 ;
2043   struct buffer_head *d_bh ;
2044   struct reiserfs_journal_header *jh ;
2045   int valid_journal_header = 0 ;
2046   int replay_count = 0 ;
2047   int continue_replay = 1 ;
2048   int ret ;
2049   char b[BDEVNAME_SIZE];
2050
2051   cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
2052   reiserfs_info (p_s_sb, "checking transaction log (%s)\n",
2053          bdevname(SB_JOURNAL(p_s_sb)->j_dev_bd, b));
2054   start = get_seconds();
2055
2056   /* step 1, read in the journal header block.  Check the transaction it says 
2057   ** is the first unflushed, and if that transaction is not valid, 
2058   ** replay is done
2059   */
2060   SB_JOURNAL(p_s_sb)->j_header_bh = journal_bread(p_s_sb,
2061                                            SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + 
2062                                            SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2063   if (!SB_JOURNAL(p_s_sb)->j_header_bh) {
2064     return 1 ;
2065   }
2066   jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
2067   if (le32_to_cpu(jh->j_first_unflushed_offset) >= 0 && 
2068       le32_to_cpu(jh->j_first_unflushed_offset) < SB_ONDISK_JOURNAL_SIZE(p_s_sb) && 
2069       le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
2070     oldest_start = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + 
2071                        le32_to_cpu(jh->j_first_unflushed_offset) ;
2072     oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2073     newest_mount_id = le32_to_cpu(jh->j_mount_id);
2074     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1153: found in "
2075                    "header: first_unflushed_offset %d, last_flushed_trans_id "
2076                    "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
2077                    le32_to_cpu(jh->j_last_flush_trans_id)) ;
2078     valid_journal_header = 1 ;
2079
2080     /* now, we try to read the first unflushed offset.  If it is not valid, 
2081     ** there is nothing more we can do, and it makes no sense to read 
2082     ** through the whole log.
2083     */
2084     d_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + le32_to_cpu(jh->j_first_unflushed_offset)) ;
2085     ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL) ;
2086     if (!ret) {
2087       continue_replay = 0 ;
2088     }
2089     brelse(d_bh) ;
2090     goto start_log_replay;
2091   }
2092
2093   if (continue_replay && bdev_read_only(p_s_sb->s_bdev)) {
2094     reiserfs_warning (p_s_sb,
2095                       "clm-2076: device is readonly, unable to replay log") ;
2096     return -1 ;
2097   }
2098
2099   /* ok, there are transactions that need to be replayed.  start with the first log block, find
2100   ** all the valid transactions, and pick out the oldest.
2101   */
2102   while(continue_replay && cur_dblock < (SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb))) {
2103     /* Note that it is required for blocksize of primary fs device and journal
2104        device to be the same */
2105     d_bh = reiserfs_breada(SB_JOURNAL(p_s_sb)->j_dev_bd, cur_dblock, p_s_sb->s_blocksize,
2106                            SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb)) ;
2107     ret = journal_transaction_is_valid(p_s_sb, d_bh, &oldest_invalid_trans_id, &newest_mount_id) ;
2108     if (ret == 1) {
2109       desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
2110       if (oldest_start == 0) { /* init all oldest_ values */
2111         oldest_trans_id = get_desc_trans_id(desc) ;
2112         oldest_start = d_bh->b_blocknr ;
2113         newest_mount_id = get_desc_mount_id(desc) ;
2114         reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1179: Setting "
2115                        "oldest_start to offset %llu, trans_id %lu",
2116                        oldest_start - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb), 
2117                        oldest_trans_id) ;
2118       } else if (oldest_trans_id > get_desc_trans_id(desc)) { 
2119         /* one we just read was older */
2120         oldest_trans_id = get_desc_trans_id(desc) ;
2121         oldest_start = d_bh->b_blocknr ;
2122         reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1180: Resetting "
2123                        "oldest_start to offset %lu, trans_id %lu",
2124                         oldest_start - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb), 
2125                         oldest_trans_id) ;
2126       }
2127       if (newest_mount_id < get_desc_mount_id(desc)) {
2128         newest_mount_id = get_desc_mount_id(desc) ;
2129         reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2130                       "newest_mount_id to %d", get_desc_mount_id(desc));
2131       }
2132       cur_dblock += get_desc_trans_len(desc) + 2 ;
2133     } else {
2134       cur_dblock++ ;
2135     }
2136     brelse(d_bh) ;
2137   }
2138
2139 start_log_replay:
2140   cur_dblock = oldest_start ;
2141   if (oldest_trans_id)  {
2142     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1206: Starting replay "
2143                    "from offset %llu, trans_id %lu",
2144                    cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb), 
2145                    oldest_trans_id) ;
2146
2147   }
2148   replay_count = 0 ;
2149   while(continue_replay && oldest_trans_id > 0) {
2150     ret = journal_read_transaction(p_s_sb, cur_dblock, oldest_start, oldest_trans_id, newest_mount_id) ;
2151     if (ret < 0) {
2152       return ret ;
2153     } else if (ret != 0) {
2154       break ;
2155     }
2156     cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start ;
2157     replay_count++ ;
2158    if (cur_dblock == oldest_start)
2159         break;
2160   }
2161
2162   if (oldest_trans_id == 0) {
2163     reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1225: No valid "
2164                    "transactions found") ;
2165   }
2166   /* j_start does not get set correctly if we don't replay any transactions.
2167   ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2168   ** copy the trans_id from the header
2169   */
2170   if (valid_journal_header && replay_count == 0) { 
2171     SB_JOURNAL(p_s_sb)->j_start = le32_to_cpu(jh->j_first_unflushed_offset) ;
2172     SB_JOURNAL(p_s_sb)->j_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2173     SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) ;
2174     SB_JOURNAL(p_s_sb)->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
2175   } else {
2176     SB_JOURNAL(p_s_sb)->j_mount_id = newest_mount_id + 1 ;
2177   }
2178   reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2179                  "newest_mount_id to %lu", SB_JOURNAL(p_s_sb)->j_mount_id) ;
2180   SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = SB_JOURNAL(p_s_sb)->j_start ; 
2181   if (replay_count > 0) {
2182     reiserfs_info (p_s_sb, "replayed %d transactions in %lu seconds\n",
2183                    replay_count, get_seconds() - start) ;
2184   }
2185   if (!bdev_read_only(p_s_sb->s_bdev) && 
2186        _update_journal_header_block(p_s_sb, SB_JOURNAL(p_s_sb)->j_start, 
2187                                    SB_JOURNAL(p_s_sb)->j_last_flush_trans_id))
2188   {
2189       /* replay failed, caller must call free_journal_ram and abort
2190       ** the mount
2191       */
2192       return -1 ;
2193   }
2194   return 0 ;
2195 }
2196
2197 static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
2198 {
2199     struct reiserfs_journal_list *jl;
2200 retry:
2201     jl = reiserfs_kmalloc(sizeof(struct reiserfs_journal_list), GFP_NOFS, s);
2202     if (!jl) {
2203         yield();
2204         goto retry;
2205     }
2206     memset(jl, 0, sizeof(*jl));
2207     INIT_LIST_HEAD(&jl->j_list);
2208     INIT_LIST_HEAD(&jl->j_working_list);
2209     INIT_LIST_HEAD(&jl->j_tail_bh_list);
2210     INIT_LIST_HEAD(&jl->j_bh_list);
2211     sema_init(&jl->j_commit_lock, 1);
2212     SB_JOURNAL(s)->j_num_lists++;
2213     get_journal_list(jl);
2214     return jl;
2215 }
2216
2217 static void journal_list_init(struct super_block *p_s_sb) {
2218     SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
2219 }
2220
2221 static int release_journal_dev( struct super_block *super,
2222                                 struct reiserfs_journal *journal )
2223 {
2224     int result;
2225     
2226     result = 0;
2227
2228     if( journal -> j_dev_file != NULL ) {
2229         result = filp_close( journal -> j_dev_file, NULL );
2230         journal -> j_dev_file = NULL;
2231         journal -> j_dev_bd = NULL;
2232     } else if( journal -> j_dev_bd != NULL ) {
2233         result = blkdev_put( journal -> j_dev_bd );
2234         journal -> j_dev_bd = NULL;
2235     }
2236
2237     if( result != 0 ) {
2238         reiserfs_warning(super, "sh-457: release_journal_dev: Cannot release journal device: %i", result );
2239     }
2240     return result;
2241 }
2242
2243 static int journal_init_dev( struct super_block *super, 
2244                              struct reiserfs_journal *journal, 
2245                              const char *jdev_name )
2246 {
2247         int result;
2248         dev_t jdev;
2249         int blkdev_mode = FMODE_READ | FMODE_WRITE;
2250         char b[BDEVNAME_SIZE];
2251
2252         result = 0;
2253
2254         journal -> j_dev_bd = NULL;
2255         journal -> j_dev_file = NULL;
2256         jdev = SB_ONDISK_JOURNAL_DEVICE( super ) ?
2257                 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev; 
2258
2259         if (bdev_read_only(super->s_bdev))
2260             blkdev_mode = FMODE_READ;
2261
2262         /* there is no "jdev" option and journal is on separate device */
2263         if( ( !jdev_name || !jdev_name[ 0 ] ) ) {
2264                 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode);
2265                 if (IS_ERR(journal->j_dev_bd)) {
2266                         result = PTR_ERR(journal->j_dev_bd);
2267                         journal->j_dev_bd = NULL;
2268                         reiserfs_warning (super, "sh-458: journal_init_dev: "
2269                                           "cannot init journal device '%s': %i",
2270                                           __bdevname(jdev, b), result );
2271                         return result;
2272                 } else if (jdev != super->s_dev)
2273                         set_blocksize(journal->j_dev_bd, super->s_blocksize);
2274                 return 0;
2275         }
2276
2277         journal -> j_dev_file = filp_open( jdev_name, 0, 0 );
2278         if( !IS_ERR( journal -> j_dev_file ) ) {
2279                 struct inode *jdev_inode = journal->j_dev_file->f_mapping->host;
2280                 if( !S_ISBLK( jdev_inode -> i_mode ) ) {
2281                         reiserfs_warning  (super, "journal_init_dev: '%s' is "
2282                                            "not a block device", jdev_name );
2283                         result = -ENOTBLK;
2284                 } else  {
2285                         /* ok */
2286                         journal->j_dev_bd = I_BDEV(jdev_inode);
2287                         set_blocksize(journal->j_dev_bd, super->s_blocksize);
2288                 }
2289         } else {
2290                 result = PTR_ERR( journal -> j_dev_file );
2291                 journal -> j_dev_file = NULL;
2292                 reiserfs_warning (super,
2293                                   "journal_init_dev: Cannot open '%s': %i",
2294                                   jdev_name, result );
2295         }
2296         if( result != 0 ) {
2297                 release_journal_dev( super, journal );
2298         }
2299         reiserfs_info(super, "journal_init_dev: journal device: %s\n",
2300                 bdevname(journal->j_dev_bd, b));
2301         return result;
2302 }
2303
2304 /*
2305 ** must be called once on fs mount.  calls journal_read for you
2306 */
2307 int journal_init(struct super_block *p_s_sb, const char * j_dev_name, int old_format, unsigned int commit_max_age) {
2308     int num_cnodes = SB_ONDISK_JOURNAL_SIZE(p_s_sb) * 2 ;
2309     struct buffer_head *bhjh;
2310     struct reiserfs_super_block * rs;
2311     struct reiserfs_journal_header *jh;
2312     struct reiserfs_journal *journal;
2313     struct reiserfs_journal_list *jl;
2314     char b[BDEVNAME_SIZE];
2315
2316     journal = SB_JOURNAL(p_s_sb) = vmalloc(sizeof (struct reiserfs_journal)) ;
2317     if (!journal) {
2318         reiserfs_warning (p_s_sb, "journal-1256: unable to get memory for journal structure") ;
2319         return 1 ;
2320     }
2321     memset(journal, 0, sizeof(struct reiserfs_journal)) ;
2322     INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
2323     INIT_LIST_HEAD (&SB_JOURNAL(p_s_sb)->j_prealloc_list);
2324     INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_working_list);
2325     INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_journal_list);
2326     if (reiserfs_allocate_list_bitmaps(p_s_sb,
2327                                        SB_JOURNAL(p_s_sb)->j_list_bitmap,
2328                                        SB_BMAP_NR(p_s_sb)))
2329         goto free_and_return ;
2330     allocate_bitmap_nodes(p_s_sb) ;
2331
2332     /* reserved for journal area support */
2333     SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) = (old_format ?
2334                                              REISERFS_OLD_DISK_OFFSET_IN_BYTES / p_s_sb->s_blocksize +
2335                                              SB_BMAP_NR(p_s_sb) + 1 :
2336                                              REISERFS_DISK_OFFSET_IN_BYTES / p_s_sb->s_blocksize + 2); 
2337     
2338     /* Sanity check to see is the standard journal fitting withing first bitmap
2339        (actual for small blocksizes) */
2340     if ( !SB_ONDISK_JOURNAL_DEVICE( p_s_sb ) &&
2341          (SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb) > p_s_sb->s_blocksize * 8) ) {
2342         reiserfs_warning (p_s_sb, "journal-1393: journal does not fit for area "
2343                           "addressed by first of bitmap blocks. It starts at "
2344                           "%u and its size is %u. Block size %ld",
2345                           SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb),
2346                           SB_ONDISK_JOURNAL_SIZE(p_s_sb), p_s_sb->s_blocksize);
2347         goto free_and_return;
2348     }
2349
2350     if( journal_init_dev( p_s_sb, journal, j_dev_name ) != 0 ) {
2351       reiserfs_warning (p_s_sb, "sh-462: unable to initialize jornal device");
2352       goto free_and_return;
2353     }
2354
2355      rs = SB_DISK_SUPER_BLOCK(p_s_sb);
2356      
2357      /* read journal header */
2358      bhjh = journal_bread(p_s_sb,
2359                    SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2360      if (!bhjh) {
2361          reiserfs_warning (p_s_sb, "sh-459: unable to read journal header");
2362          goto free_and_return;
2363      }
2364      jh = (struct reiserfs_journal_header *)(bhjh->b_data);
2365      
2366      /* make sure that journal matches to the super block */
2367      if (is_reiserfs_jr(rs) && (jh->jh_journal.jp_journal_magic != sb_jp_journal_magic(rs))) {
2368          reiserfs_warning (p_s_sb, "sh-460: journal header magic %x "
2369                            "(device %s) does not match to magic found in super "
2370                            "block %x",
2371                            jh->jh_journal.jp_journal_magic,
2372                            bdevname( SB_JOURNAL(p_s_sb)->j_dev_bd, b),
2373                            sb_jp_journal_magic(rs));
2374          brelse (bhjh);
2375          goto free_and_return;
2376   }
2377      
2378   SB_JOURNAL_TRANS_MAX(p_s_sb)      = le32_to_cpu (jh->jh_journal.jp_journal_trans_max);
2379   SB_JOURNAL_MAX_BATCH(p_s_sb)      = le32_to_cpu (jh->jh_journal.jp_journal_max_batch);
2380   SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb) = le32_to_cpu (jh->jh_journal.jp_journal_max_commit_age);
2381   SB_JOURNAL_MAX_TRANS_AGE(p_s_sb)  = JOURNAL_MAX_TRANS_AGE;
2382
2383   if (SB_JOURNAL_TRANS_MAX(p_s_sb)) {
2384     /* make sure these parameters are available, assign it if they are not */
2385     __u32 initial = SB_JOURNAL_TRANS_MAX(p_s_sb);
2386     __u32 ratio = 1;
2387     
2388     if (p_s_sb->s_blocksize < 4096)
2389       ratio = 4096 / p_s_sb->s_blocksize;
2390     
2391     if (SB_ONDISK_JOURNAL_SIZE(p_s_sb)/SB_JOURNAL_TRANS_MAX(p_s_sb) < JOURNAL_MIN_RATIO)
2392       SB_JOURNAL_TRANS_MAX(p_s_sb) = SB_ONDISK_JOURNAL_SIZE(p_s_sb) / JOURNAL_MIN_RATIO;
2393     if (SB_JOURNAL_TRANS_MAX(p_s_sb) > JOURNAL_TRANS_MAX_DEFAULT / ratio)
2394       SB_JOURNAL_TRANS_MAX(p_s_sb) = JOURNAL_TRANS_MAX_DEFAULT / ratio;
2395     if (SB_JOURNAL_TRANS_MAX(p_s_sb) < JOURNAL_TRANS_MIN_DEFAULT / ratio)
2396       SB_JOURNAL_TRANS_MAX(p_s_sb) = JOURNAL_TRANS_MIN_DEFAULT / ratio;
2397     
2398     if (SB_JOURNAL_TRANS_MAX(p_s_sb) != initial)
2399       reiserfs_warning (p_s_sb, "sh-461: journal_init: wrong transaction max size (%u). Changed to %u",
2400               initial, SB_JOURNAL_TRANS_MAX(p_s_sb));
2401
2402     SB_JOURNAL_MAX_BATCH(p_s_sb) = SB_JOURNAL_TRANS_MAX(p_s_sb)*
2403       JOURNAL_MAX_BATCH_DEFAULT/JOURNAL_TRANS_MAX_DEFAULT;
2404   }  
2405   
2406   if (!SB_JOURNAL_TRANS_MAX(p_s_sb)) {
2407     /*we have the file system was created by old version of mkreiserfs 
2408       so this field contains zero value */
2409     SB_JOURNAL_TRANS_MAX(p_s_sb)      = JOURNAL_TRANS_MAX_DEFAULT ;
2410     SB_JOURNAL_MAX_BATCH(p_s_sb)      = JOURNAL_MAX_BATCH_DEFAULT ;  
2411     SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb) = JOURNAL_MAX_COMMIT_AGE ;
2412     
2413     /* for blocksize >= 4096 - max transaction size is 1024. For block size < 4096
2414        trans max size is decreased proportionally */
2415     if (p_s_sb->s_blocksize < 4096) {
2416       SB_JOURNAL_TRANS_MAX(p_s_sb) /= (4096 / p_s_sb->s_blocksize) ;
2417       SB_JOURNAL_MAX_BATCH(p_s_sb) = (SB_JOURNAL_TRANS_MAX(p_s_sb)) * 9 / 10 ;
2418     }
2419   }
2420
2421   SB_JOURNAL_DEFAULT_MAX_COMMIT_AGE(p_s_sb) = SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb);
2422
2423   if (commit_max_age != 0) {
2424       SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb) = commit_max_age;
2425       SB_JOURNAL_MAX_TRANS_AGE(p_s_sb) = commit_max_age;
2426   }
2427
2428   reiserfs_info (p_s_sb, "journal params: device %s, size %u, "
2429                  "journal first block %u, max trans len %u, max batch %u, "
2430                  "max commit age %u, max trans age %u\n",
2431                  bdevname( SB_JOURNAL(p_s_sb)->j_dev_bd, b),
2432                  SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2433                  SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2434                  SB_JOURNAL_TRANS_MAX(p_s_sb),
2435                  SB_JOURNAL_MAX_BATCH(p_s_sb),
2436                  SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb),
2437                  SB_JOURNAL_MAX_TRANS_AGE(p_s_sb));
2438
2439   brelse (bhjh);
2440      
2441   SB_JOURNAL(p_s_sb)->j_list_bitmap_index = 0 ;
2442   journal_list_init(p_s_sb) ;
2443
2444   memset(SB_JOURNAL(p_s_sb)->j_list_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
2445
2446   INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_dirty_buffers) ;
2447   spin_lock_init(&SB_JOURNAL(p_s_sb)->j_dirty_buffers_lock) ;
2448
2449   SB_JOURNAL(p_s_sb)->j_start = 0 ;
2450   SB_JOURNAL(p_s_sb)->j_len = 0 ;
2451   SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
2452   atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
2453   atomic_set(&(SB_JOURNAL(p_s_sb)->j_async_throttle), 0) ;
2454   SB_JOURNAL(p_s_sb)->j_bcount = 0 ;      
2455   SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;    
2456   SB_JOURNAL(p_s_sb)->j_last = NULL ;     
2457   SB_JOURNAL(p_s_sb)->j_first = NULL ;     
2458   init_waitqueue_head(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
2459   sema_init(&SB_JOURNAL(p_s_sb)->j_lock, 1);
2460   sema_init(&SB_JOURNAL(p_s_sb)->j_flush_sem, 1);
2461
2462   SB_JOURNAL(p_s_sb)->j_trans_id = 10 ;  
2463   SB_JOURNAL(p_s_sb)->j_mount_id = 10 ; 
2464   SB_JOURNAL(p_s_sb)->j_state = 0 ;
2465   atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
2466   SB_JOURNAL(p_s_sb)->j_cnode_free_list = allocate_cnodes(num_cnodes) ;
2467   SB_JOURNAL(p_s_sb)->j_cnode_free_orig = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
2468   SB_JOURNAL(p_s_sb)->j_cnode_free = SB_JOURNAL(p_s_sb)->j_cnode_free_list ? num_cnodes : 0 ;
2469   SB_JOURNAL(p_s_sb)->j_cnode_used = 0 ;
2470   SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
2471
2472   init_journal_hash(p_s_sb) ;
2473   jl = SB_JOURNAL(p_s_sb)->j_current_jl;
2474   jl->j_list_bitmap = get_list_bitmap(p_s_sb, jl);
2475   if (!jl->j_list_bitmap) {
2476     reiserfs_warning(p_s_sb, "journal-2005, get_list_bitmap failed for journal list 0") ;
2477     goto free_and_return;
2478   }
2479   if (journal_read(p_s_sb) < 0) {
2480     reiserfs_warning(p_s_sb, "Replay Failure, unable to mount") ;
2481     goto free_and_return;
2482   }
2483
2484   reiserfs_mounted_fs_count++ ;
2485   if (reiserfs_mounted_fs_count <= 1)
2486     commit_wq = create_workqueue("reiserfs");
2487
2488   INIT_WORK(&journal->j_work, flush_async_commits, p_s_sb);
2489   return 0 ;
2490 free_and_return:
2491   free_journal_ram(p_s_sb);
2492   return 1;
2493 }
2494
2495 /*
2496 ** test for a polite end of the current transaction.  Used by file_write, and should
2497 ** be used by delete to make sure they don't write more than can fit inside a single
2498 ** transaction
2499 */
2500 int journal_transaction_should_end(struct reiserfs_transaction_handle *th, int new_alloc) {
2501   time_t now = get_seconds() ;
2502   /* cannot restart while nested */
2503   if (th->t_refcount > 1)
2504     return 0 ;
2505   if ( SB_JOURNAL(th->t_super)->j_must_wait > 0 ||
2506        (SB_JOURNAL(th->t_super)->j_len_alloc + new_alloc) >= SB_JOURNAL_MAX_BATCH(th->t_super) || 
2507        atomic_read(&(SB_JOURNAL(th->t_super)->j_jlock)) ||
2508       (now - SB_JOURNAL(th->t_super)->j_trans_start_time) > SB_JOURNAL_MAX_TRANS_AGE(th->t_super) ||
2509        SB_JOURNAL(th->t_super)->j_cnode_free < (SB_JOURNAL_TRANS_MAX(th->t_super) * 3)) { 
2510     return 1 ;
2511   }
2512   return 0 ;
2513 }
2514
2515 /* this must be called inside a transaction, and requires the 
2516 ** kernel_lock to be held
2517 */
2518 void reiserfs_block_writes(struct reiserfs_transaction_handle *th) {
2519     struct super_block *s = th->t_super ;
2520     SB_JOURNAL(s)->j_must_wait = 1 ;
2521     set_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
2522     return ;
2523 }
2524
2525 /* this must be called without a transaction started, and does not
2526 ** require BKL
2527 */
2528 void reiserfs_allow_writes(struct super_block *s) {
2529     clear_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
2530     wake_up(&SB_JOURNAL(s)->j_join_wait) ;
2531 }
2532
2533 /* this must be called without a transaction started, and does not
2534 ** require BKL
2535 */
2536 void reiserfs_wait_on_write_block(struct super_block *s) {
2537     wait_event(SB_JOURNAL(s)->j_join_wait, 
2538                !test_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state)) ;
2539 }
2540
2541 static void queue_log_writer(struct super_block *s) {
2542     wait_queue_t wait;
2543     set_bit(WRITERS_QUEUED, &SB_JOURNAL(s)->j_state);
2544
2545     /*
2546      * we don't want to use wait_event here because
2547      * we only want to wait once.
2548      */
2549     init_waitqueue_entry(&wait, current);
2550     add_wait_queue(&SB_JOURNAL(s)->j_join_wait, &wait);
2551     set_current_state(TASK_UNINTERRUPTIBLE);
2552     if (test_bit(WRITERS_QUEUED, &SB_JOURNAL(s)->j_state))
2553         schedule();
2554     current->state = TASK_RUNNING;
2555     remove_wait_queue(&SB_JOURNAL(s)->j_join_wait, &wait);
2556 }
2557
2558 static void wake_queued_writers(struct super_block *s) {
2559     if (test_and_clear_bit(WRITERS_QUEUED, &SB_JOURNAL(s)->j_state))
2560         wake_up(&SB_JOURNAL(s)->j_join_wait);
2561 }
2562
2563 static void let_transaction_grow(struct super_block *sb,
2564                                  unsigned long trans_id)
2565 {
2566     unsigned long bcount = SB_JOURNAL(sb)->j_bcount;
2567     while(1) {
2568         set_current_state(TASK_UNINTERRUPTIBLE);
2569         schedule_timeout(1);
2570         SB_JOURNAL(sb)->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2571         while ((atomic_read(&SB_JOURNAL(sb)->j_wcount) > 0 ||
2572                 atomic_read(&SB_JOURNAL(sb)->j_jlock)) &&
2573                SB_JOURNAL(sb)->j_trans_id == trans_id) {
2574             queue_log_writer(sb);
2575         }
2576         if (SB_JOURNAL(sb)->j_trans_id != trans_id)
2577             break;
2578         if (bcount == SB_JOURNAL(sb)->j_bcount)
2579             break;
2580         bcount = SB_JOURNAL(sb)->j_bcount;
2581     }
2582 }
2583
2584 /* join == true if you must join an existing transaction.
2585 ** join == false if you can deal with waiting for others to finish
2586 **
2587 ** this will block until the transaction is joinable.  send the number of blocks you
2588 ** expect to use in nblocks.
2589 */
2590 static int do_journal_begin_r(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,unsigned long nblocks,int join) {
2591   time_t now = get_seconds() ;
2592   int old_trans_id  ;
2593   struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2594   struct reiserfs_transaction_handle myth;
2595   int sched_count = 0;
2596
2597   reiserfs_check_lock_depth(p_s_sb, "journal_begin") ;
2598   RFALSE( p_s_sb->s_flags & MS_RDONLY, 
2599           "clm-2078: calling journal_begin on readonly FS") ;
2600
2601   PROC_INFO_INC( p_s_sb, journal.journal_being );
2602   /* set here for journal_join */
2603   th->t_refcount = 1;
2604   th->t_super = p_s_sb ;
2605
2606 relock:
2607   lock_journal(p_s_sb) ;
2608   journal->j_bcount++;
2609
2610   if (test_bit(WRITERS_BLOCKED, &journal->j_state)) {
2611     unlock_journal(p_s_sb) ;
2612     reiserfs_wait_on_write_block(p_s_sb) ;
2613     PROC_INFO_INC( p_s_sb, journal.journal_relock_writers );
2614     goto relock ;
2615   }
2616   now = get_seconds();
2617
2618   /* if there is no room in the journal OR
2619   ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning 
2620   ** we don't sleep if there aren't other writers
2621   */
2622
2623   if ( (!join && journal->j_must_wait > 0) ||
2624      ( !join && (journal->j_len_alloc + nblocks + 2) >= SB_JOURNAL_MAX_BATCH(p_s_sb)) ||
2625      (!join && atomic_read(&journal->j_wcount) > 0 && journal->j_trans_start_time > 0 &&
2626       (now - journal->j_trans_start_time) > SB_JOURNAL_MAX_TRANS_AGE(p_s_sb)) ||
2627      (!join && atomic_read(&journal->j_jlock)) ||
2628      (!join && journal->j_cnode_free < (SB_JOURNAL_TRANS_MAX(p_s_sb) * 3))) {
2629
2630     old_trans_id = journal->j_trans_id;
2631     unlock_journal(p_s_sb) ; /* allow others to finish this transaction */
2632
2633     if (!join && (journal->j_len_alloc + nblocks + 2) >=
2634         SB_JOURNAL_MAX_BATCH(p_s_sb) &&
2635         ((journal->j_len + nblocks + 2) * 100) < (journal->j_len_alloc * 75))
2636     {
2637         if (atomic_read(&journal->j_wcount) > 10) {
2638             sched_count++;
2639             queue_log_writer(p_s_sb);
2640             goto relock;
2641         }
2642     }
2643     /* don't mess with joining the transaction if all we have to do is
2644      * wait for someone else to do a commit
2645      */
2646     if (atomic_read(&journal->j_jlock)) {
2647         while (journal->j_trans_id == old_trans_id &&
2648                atomic_read(&journal->j_jlock)) {
2649             queue_log_writer(p_s_sb);
2650         }
2651         goto relock;
2652     }
2653     journal_join(&myth, p_s_sb, 1) ;
2654
2655     /* someone might have ended the transaction while we joined */
2656     if (old_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
2657         do_journal_end(&myth, p_s_sb, 1, 0) ;
2658     } else {
2659         do_journal_end(&myth, p_s_sb, 1, COMMIT_NOW) ;
2660     }
2661
2662     PROC_INFO_INC( p_s_sb, journal.journal_relock_wcount );
2663     goto relock ;
2664   }
2665   /* we are the first writer, set trans_id */
2666   if (journal->j_trans_start_time == 0) {
2667     journal->j_trans_start_time = get_seconds();
2668   }
2669   atomic_inc(&(journal->j_wcount)) ;
2670   journal->j_len_alloc += nblocks ;
2671   th->t_blocks_logged = 0 ;
2672   th->t_blocks_allocated = nblocks ;
2673   th->t_trans_id = journal->j_trans_id ;
2674   unlock_journal(p_s_sb) ;
2675   return 0 ;
2676 }
2677
2678 struct reiserfs_transaction_handle *
2679 reiserfs_persistent_transaction(struct super_block *s, int nblocks) {
2680     int ret ;
2681     struct reiserfs_transaction_handle *th ;
2682
2683     /* if we're nesting into an existing transaction.  It will be
2684     ** persistent on its own
2685     */
2686     if (reiserfs_transaction_running(s)) {
2687         th = current->journal_info ;
2688         th->t_refcount++ ;
2689         if (th->t_refcount < 2) {
2690             BUG() ;
2691         }
2692         return th ;
2693     }
2694     th = reiserfs_kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS, s) ;
2695     if (!th)
2696        return NULL;
2697     ret = journal_begin(th, s, nblocks) ;
2698     if (ret) {
2699         reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle), s) ;
2700         return NULL;
2701     }
2702     return th ;
2703 }
2704
2705 int
2706 reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th) {
2707     struct super_block *s = th->t_super;
2708     int ret;
2709     ret = journal_end(th, th->t_super, th->t_blocks_allocated);
2710     if (th->t_refcount == 0)
2711         reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle), s) ;
2712     return ret;
2713 }
2714
2715 static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2716   struct reiserfs_transaction_handle *cur_th = current->journal_info;
2717
2718   /* this keeps do_journal_end from NULLing out the current->journal_info
2719   ** pointer
2720   */
2721   th->t_handle_save = cur_th ;
2722   if (cur_th && cur_th->t_refcount > 1) {
2723       BUG() ;
2724   }
2725   return do_journal_begin_r(th, p_s_sb, nblocks, 1) ;
2726 }
2727
2728 int journal_begin(struct reiserfs_transaction_handle *th, struct super_block  * p_s_sb, unsigned long nblocks) {
2729     struct reiserfs_transaction_handle *cur_th = current->journal_info ;
2730     int ret ;
2731
2732     th->t_handle_save = NULL ;
2733     if (cur_th) {
2734         /* we are nesting into the current transaction */
2735         if (cur_th->t_super == p_s_sb) {
2736               cur_th->t_refcount++ ;
2737               memcpy(th, cur_th, sizeof(*th));
2738               if (th->t_refcount <= 1)
2739                       reiserfs_warning (p_s_sb, "BAD: refcount <= 1, but journal_info != 0");
2740               return 0;
2741         } else {
2742             /* we've ended up with a handle from a different filesystem.
2743             ** save it and restore on journal_end.  This should never
2744             ** really happen...
2745             */
2746             reiserfs_warning(p_s_sb, "clm-2100: nesting info a different FS") ;
2747             th->t_handle_save = current->journal_info ;
2748             current->journal_info = th;
2749         }
2750     } else {
2751         current->journal_info = th;
2752     }
2753     ret = do_journal_begin_r(th, p_s_sb, nblocks, 0) ;
2754     if (current->journal_info != th)
2755         BUG() ;
2756     return ret ;
2757 }
2758
2759 /*
2760 ** puts bh into the current transaction.  If it was already there, reorders removes the
2761 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
2762 **
2763 ** if it was dirty, cleans and files onto the clean list.  I can't let it be dirty again until the
2764 ** transaction is committed.
2765 ** 
2766 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
2767 */
2768 int journal_mark_dirty(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, struct buffer_head *bh) {
2769   struct reiserfs_journal_cnode *cn = NULL;
2770   int count_already_incd = 0 ;
2771   int prepared = 0 ;
2772
2773   PROC_INFO_INC( p_s_sb, journal.mark_dirty );
2774   if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
2775     reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ld\n", 
2776                    th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
2777   }
2778   p_s_sb->s_dirt = 1;
2779
2780   prepared = test_and_clear_bit(BH_JPrepared, &bh->b_state) ;
2781   clear_bit(BH_JRestore_dirty, &bh->b_state);
2782   /* already in this transaction, we are done */
2783   if (buffer_journaled(bh)) {
2784     PROC_INFO_INC( p_s_sb, journal.mark_dirty_already );
2785     return 0 ;
2786   }
2787
2788   /* this must be turned into a panic instead of a warning.  We can't allow
2789   ** a dirty or journal_dirty or locked buffer to be logged, as some changes
2790   ** could get to disk too early.  NOT GOOD.
2791   */
2792   if (!prepared || buffer_dirty(bh)) {
2793     reiserfs_warning (p_s_sb, "journal-1777: buffer %llu bad state "
2794                       "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
2795                       (unsigned long long)bh->b_blocknr, prepared ? ' ' : '!',
2796                             buffer_locked(bh) ? ' ' : '!',
2797                             buffer_dirty(bh) ? ' ' : '!',
2798                             buffer_journal_dirty(bh) ? ' ' : '!') ;
2799   }
2800
2801   if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0) {
2802     reiserfs_warning (p_s_sb, "journal-1409: journal_mark_dirty returning because j_wcount was %d", atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount))) ;
2803     return 1 ;
2804   }
2805   /* this error means I've screwed up, and we've overflowed the transaction.  
2806   ** Nothing can be done here, except make the FS readonly or panic.
2807   */ 
2808   if (SB_JOURNAL(p_s_sb)->j_len >= SB_JOURNAL_TRANS_MAX(p_s_sb)) { 
2809     reiserfs_panic(th->t_super, "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n", SB_JOURNAL(p_s_sb)->j_len) ;
2810   }
2811
2812   if (buffer_journal_dirty(bh)) {
2813     count_already_incd = 1 ;
2814     PROC_INFO_INC( p_s_sb, journal.mark_dirty_notjournal );
2815     mark_buffer_notjournal_dirty(bh) ;
2816   }
2817
2818   if (SB_JOURNAL(p_s_sb)->j_len > SB_JOURNAL(p_s_sb)->j_len_alloc) {
2819     SB_JOURNAL(p_s_sb)->j_len_alloc = SB_JOURNAL(p_s_sb)->j_len + JOURNAL_PER_BALANCE_CNT ;
2820   }
2821
2822   set_bit(BH_JDirty, &bh->b_state) ;
2823
2824   /* now put this guy on the end */
2825   if (!cn) {
2826     cn = get_cnode(p_s_sb) ;
2827     if (!cn) {
2828       reiserfs_panic(p_s_sb, "get_cnode failed!\n"); 
2829     }
2830
2831     if (th->t_blocks_logged == th->t_blocks_allocated) {
2832       th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT ;
2833       SB_JOURNAL(p_s_sb)->j_len_alloc += JOURNAL_PER_BALANCE_CNT ;
2834     }
2835     th->t_blocks_logged++ ;
2836     SB_JOURNAL(p_s_sb)->j_len++ ;
2837
2838     cn->bh = bh ;
2839     cn->blocknr = bh->b_blocknr ;
2840     cn->sb = p_s_sb;
2841     cn->jlist = NULL ;
2842     insert_journal_hash(SB_JOURNAL(p_s_sb)->j_hash_table, cn) ;
2843     if (!count_already_incd) {
2844       get_bh(bh) ;
2845     }
2846   }
2847   cn->next = NULL ;
2848   cn->prev = SB_JOURNAL(p_s_sb)->j_last ;
2849   cn->bh = bh ;
2850   if (SB_JOURNAL(p_s_sb)->j_last) {
2851     SB_JOURNAL(p_s_sb)->j_last->next = cn ;
2852     SB_JOURNAL(p_s_sb)->j_last = cn ;
2853   } else {
2854     SB_JOURNAL(p_s_sb)->j_first = cn ;
2855     SB_JOURNAL(p_s_sb)->j_last = cn ;
2856   }
2857   return 0 ;
2858 }
2859
2860 int journal_end(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2861   if (!current->journal_info && th->t_refcount > 1)
2862     reiserfs_warning (p_s_sb, "REISER-NESTING: th NULL, refcount %d",
2863                       th->t_refcount);
2864
2865   th->t_refcount--;
2866   if (th->t_refcount > 0) {
2867     struct reiserfs_transaction_handle *cur_th = current->journal_info ;
2868
2869     /* we aren't allowed to close a nested transaction on a different
2870     ** filesystem from the one in the task struct
2871     */
2872     if (cur_th->t_super != th->t_super)
2873       BUG() ;
2874
2875     if (th != cur_th) {
2876       memcpy(current->journal_info, th, sizeof(*th));
2877       th->t_trans_id = 0;
2878     }
2879     return 0;
2880   } else {
2881     return do_journal_end(th, p_s_sb, nblocks, 0) ;
2882   }
2883 }
2884
2885 /* removes from the current transaction, relsing and descrementing any counters.  
2886 ** also files the removed buffer directly onto the clean list
2887 **
2888 ** called by journal_mark_freed when a block has been deleted
2889 **
2890 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
2891 */
2892 static int remove_from_transaction(struct super_block *p_s_sb, b_blocknr_t blocknr, int already_cleaned) {
2893   struct buffer_head *bh ;
2894   struct reiserfs_journal_cnode *cn ;
2895   int ret = 0;
2896
2897   cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, blocknr) ;
2898   if (!cn || !cn->bh) {
2899     return ret ;
2900   }
2901   bh = cn->bh ;
2902   if (cn->prev) {
2903     cn->prev->next = cn->next ;
2904   }
2905   if (cn->next) {
2906     cn->next->prev = cn->prev ;
2907   }
2908   if (cn == SB_JOURNAL(p_s_sb)->j_first) {
2909     SB_JOURNAL(p_s_sb)->j_first = cn->next ;  
2910   }
2911   if (cn == SB_JOURNAL(p_s_sb)->j_last) {
2912     SB_JOURNAL(p_s_sb)->j_last = cn->prev ;
2913   }
2914   if (bh)
2915         remove_journal_hash(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, NULL, bh->b_blocknr, 0) ; 
2916   mark_buffer_not_journaled(bh) ; /* don't log this one */
2917
2918   if (!already_cleaned) {
2919     mark_buffer_notjournal_dirty(bh) ; 
2920     put_bh(bh) ;
2921     if (atomic_read(&(bh->b_count)) < 0) {
2922       reiserfs_warning (p_s_sb, "journal-1752: remove from trans, b_count < 0");
2923     }
2924     ret = 1 ;
2925   }
2926   SB_JOURNAL(p_s_sb)->j_len-- ;
2927   SB_JOURNAL(p_s_sb)->j_len_alloc-- ;
2928   free_cnode(p_s_sb, cn) ;
2929   return ret ;
2930 }
2931
2932 /*
2933 ** for any cnode in a journal list, it can only be dirtied of all the
2934 ** transactions that include it are commited to disk.
2935 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
2936 ** and 0 if you aren't
2937 **
2938 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
2939 ** blocks for a given transaction on disk
2940 **
2941 */
2942 static int can_dirty(struct reiserfs_journal_cnode *cn) {
2943   struct super_block *sb = cn->sb;
2944   b_blocknr_t blocknr = cn->blocknr  ;
2945   struct reiserfs_journal_cnode *cur = cn->hprev ;
2946   int can_dirty = 1 ;
2947   
2948   /* first test hprev.  These are all newer than cn, so any node here
2949   ** with the same block number and dev means this node can't be sent
2950   ** to disk right now.
2951   */
2952   while(cur && can_dirty) {
2953     if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb && 
2954         cur->blocknr == blocknr) {
2955       can_dirty = 0 ;
2956     }
2957     cur = cur->hprev ;
2958   }
2959   /* then test hnext.  These are all older than cn.  As long as they
2960   ** are committed to the log, it is safe to write cn to disk
2961   */
2962   cur = cn->hnext ;
2963   while(cur && can_dirty) {
2964     if (cur->jlist && cur->jlist->j_len > 0 && 
2965         atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh && 
2966         cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
2967       can_dirty = 0 ;
2968     }
2969     cur = cur->hnext ;
2970   }
2971   return can_dirty ;
2972 }
2973
2974 /* syncs the commit blocks, but does not force the real buffers to disk
2975 ** will wait until the current transaction is done/commited before returning 
2976 */
2977 int journal_end_sync(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2978
2979   /* you can sync while nested, very, very bad */
2980   if (th->t_refcount > 1) {
2981     BUG() ;
2982   }
2983   if (SB_JOURNAL(p_s_sb)->j_len == 0) {
2984     reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
2985     journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
2986   }
2987   return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT) ;
2988 }
2989
2990 /*
2991 ** writeback the pending async commits to disk
2992 */
2993 static void flush_async_commits(void *p) {
2994   struct super_block *p_s_sb = p;
2995   struct reiserfs_journal_list *jl;
2996   struct list_head *entry;
2997
2998   lock_kernel();
2999   if (!list_empty(&SB_JOURNAL(p_s_sb)->j_journal_list)) {
3000       /* last entry is the youngest, commit it and you get everything */
3001       entry = SB_JOURNAL(p_s_sb)->j_journal_list.prev;
3002       jl = JOURNAL_LIST_ENTRY(entry);
3003       flush_commit_list(p_s_sb, jl, 1);
3004   }
3005   unlock_kernel();
3006   /*
3007    * this is a little racey, but there's no harm in missing
3008    * the filemap_fdata_write
3009    */
3010   if (!atomic_read(&SB_JOURNAL(p_s_sb)->j_async_throttle)) {
3011       atomic_inc(&SB_JOURNAL(p_s_sb)->j_async_throttle);
3012       filemap_fdatawrite(p_s_sb->s_bdev->bd_inode->i_mapping);
3013       atomic_dec(&SB_JOURNAL(p_s_sb)->j_async_throttle);
3014   }
3015 }
3016
3017 /*
3018 ** flushes any old transactions to disk
3019 ** ends the current transaction if it is too old
3020 */
3021 int reiserfs_flush_old_commits(struct super_block *p_s_sb) {
3022     time_t now ;
3023     struct reiserfs_transaction_handle th ;
3024
3025     now = get_seconds();
3026     /* safety check so we don't flush while we are replaying the log during
3027      * mount
3028      */
3029     if (list_empty(&SB_JOURNAL(p_s_sb)->j_journal_list)) {
3030         return 0  ;
3031     }
3032
3033     /* check the current transaction.  If there are no writers, and it is
3034      * too old, finish it, and force the commit blocks to disk
3035      */
3036     if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0 &&
3037         SB_JOURNAL(p_s_sb)->j_trans_start_time > 0 &&
3038         SB_JOURNAL(p_s_sb)->j_len > 0 &&
3039         (now - SB_JOURNAL(p_s_sb)->j_trans_start_time) >
3040         SB_JOURNAL_MAX_TRANS_AGE(p_s_sb))
3041     {
3042         journal_join(&th, p_s_sb, 1) ;
3043         reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
3044         journal_mark_dirty(&th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
3045
3046         /* we're only being called from kreiserfsd, it makes no sense to do
3047         ** an async commit so that kreiserfsd can do it later
3048         */
3049         do_journal_end(&th, p_s_sb,1, COMMIT_NOW | WAIT) ;
3050     }
3051     return p_s_sb->s_dirt;
3052 }
3053
3054 /*
3055 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
3056 ** 
3057 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all 
3058 ** the writers are done.  By the time it wakes up, the transaction it was called has already ended, so it just
3059 ** flushes the commit list and returns 0.
3060 **
3061 ** Won't batch when flush or commit_now is set.  Also won't batch when others are waiting on j_join_wait.
3062 ** 
3063 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3064 */
3065 static int check_journal_end(struct reiserfs_transaction_handle *th, struct super_block  * p_s_sb, 
3066                              unsigned long nblocks, int flags) {
3067
3068   time_t now ;
3069   int flush = flags & FLUSH_ALL ;
3070   int commit_now = flags & COMMIT_NOW ;
3071   int wait_on_commit = flags & WAIT ;
3072   struct reiserfs_journal_list *jl;
3073
3074   if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
3075     reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ld\n", 
3076                    th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
3077   }
3078
3079   SB_JOURNAL(p_s_sb)->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged) ;
3080   if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) { /* <= 0 is allowed.  unmounting might not call begin */
3081     atomic_dec(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
3082   }
3083
3084   /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released 
3085   ** will be dealt with by next transaction that actually writes something, but should be taken
3086   ** care of in this trans
3087   */
3088   if (SB_JOURNAL(p_s_sb)->j_len == 0) {
3089     BUG();
3090   }
3091   /* if wcount > 0, and we are called to with flush or commit_now,
3092   ** we wait on j_join_wait.  We will wake up when the last writer has
3093   ** finished the transaction, and started it on its way to the disk.
3094   ** Then, we flush the commit or journal list, and just return 0 
3095   ** because the rest of journal end was already done for this transaction.
3096   */
3097   if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) {
3098     if (flush || commit_now) {
3099       unsigned trans_id ;
3100
3101       jl = SB_JOURNAL(p_s_sb)->j_current_jl;
3102       trans_id = jl->j_trans_id;
3103       if (wait_on_commit)
3104         jl->j_state |= LIST_COMMIT_PENDING;
3105       atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
3106       if (flush) {
3107         SB_JOURNAL(p_s_sb)->j_next_full_flush = 1 ;
3108       }
3109       unlock_journal(p_s_sb) ;
3110
3111       /* sleep while the current transaction is still j_jlocked */
3112       while(SB_JOURNAL(p_s_sb)->j_trans_id == trans_id) {
3113         if (atomic_read(&SB_JOURNAL(p_s_sb)->j_jlock)) {
3114             queue_log_writer(p_s_sb);
3115         } else {
3116             lock_journal(p_s_sb);
3117             if (SB_JOURNAL(p_s_sb)->j_trans_id == trans_id) {
3118                 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
3119             }
3120             unlock_journal(p_s_sb);
3121         }
3122       }
3123       if (SB_JOURNAL(p_s_sb)->j_trans_id == trans_id) {
3124           BUG();
3125       }
3126       if (commit_now && journal_list_still_alive(p_s_sb, trans_id) &&
3127           wait_on_commit)
3128       {
3129           flush_commit_list(p_s_sb, jl, 1) ;
3130       }
3131       return 0 ;
3132     } 
3133     unlock_journal(p_s_sb) ;
3134     return 0 ;
3135   }
3136
3137   /* deal with old transactions where we are the last writers */
3138   now = get_seconds();
3139   if ((now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > SB_JOURNAL_MAX_TRANS_AGE(p_s_sb)) {
3140     commit_now = 1 ;
3141     SB_JOURNAL(p_s_sb)->j_next_async_flush = 1 ;
3142   }
3143   /* don't batch when someone is waiting on j_join_wait */
3144   /* don't batch when syncing the commit or flushing the whole trans */
3145   if (!(SB_JOURNAL(p_s_sb)->j_must_wait > 0) && !(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock))) && !flush && !commit_now && 
3146       (SB_JOURNAL(p_s_sb)->j_len < SB_JOURNAL_MAX_BATCH(p_s_sb))  && 
3147       SB_JOURNAL(p_s_sb)->j_len_alloc < SB_JOURNAL_MAX_BATCH(p_s_sb) && SB_JOURNAL(p_s_sb)->j_cnode_free > (SB_JOURNAL_TRANS_MAX(p_s_sb) * 3)) {
3148     SB_JOURNAL(p_s_sb)->j_bcount++ ;
3149     unlock_journal(p_s_sb) ;
3150     return 0 ;
3151   }
3152
3153   if (SB_JOURNAL(p_s_sb)->j_start > SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
3154     reiserfs_panic(p_s_sb, "journal-003: journal_end: j_start (%ld) is too high\n", SB_JOURNAL(p_s_sb)->j_start) ;
3155   }
3156   return 1 ;
3157 }
3158
3159 /*
3160 ** Does all the work that makes deleting blocks safe.
3161 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3162 ** 
3163 ** otherwise:
3164 ** set a bit for the block in the journal bitmap.  That will prevent it from being allocated for unformatted nodes
3165 ** before this transaction has finished.
3166 **
3167 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers.  That will prevent any old transactions with
3168 ** this block from trying to flush to the real location.  Since we aren't removing the cnode from the journal_list_hash,
3169 ** the block can't be reallocated yet.
3170 **
3171 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3172 */
3173 int journal_mark_freed(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, b_blocknr_t blocknr) {
3174   struct reiserfs_journal_cnode *cn = NULL ;
3175   struct buffer_head *bh = NULL ;
3176   struct reiserfs_list_bitmap *jb = NULL ;
3177   int cleaned = 0 ;
3178
3179   cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, blocknr);
3180   if (cn && cn->bh) {
3181       bh = cn->bh ;
3182       get_bh(bh) ;
3183   }
3184   /* if it is journal new, we just remove it from this transaction */
3185   if (bh && buffer_journal_new(bh)) {
3186     mark_buffer_notjournal_new(bh) ;
3187     clear_prepared_bits(bh) ;
3188     reiserfs_clean_and_file_buffer(bh) ;
3189     cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
3190   } else {
3191     /* set the bit for this block in the journal bitmap for this transaction */
3192     jb = SB_JOURNAL(p_s_sb)->j_current_jl->j_list_bitmap;
3193     if (!jb) {
3194       reiserfs_panic(p_s_sb, "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n") ;
3195     }
3196     set_bit_in_list_bitmap(p_s_sb, blocknr, jb) ;
3197
3198     /* Note, the entire while loop is not allowed to schedule.  */
3199
3200     if (bh) {
3201       clear_prepared_bits(bh) ;
3202       reiserfs_clean_and_file_buffer(bh) ;
3203     }
3204     cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
3205
3206     /* find all older transactions with this block, make sure they don't try to write it out */
3207     cn = get_journal_hash_dev(p_s_sb,SB_JOURNAL(p_s_sb)->j_list_hash_table,  blocknr) ;
3208     while (cn) {
3209       if (p_s_sb == cn->sb && blocknr == cn->blocknr) {
3210         set_bit(BLOCK_FREED, &cn->state) ;
3211         if (cn->bh) {
3212           if (!cleaned) {
3213             /* remove_from_transaction will brelse the buffer if it was 
3214             ** in the current trans
3215             */
3216             mark_buffer_notjournal_dirty(cn->bh) ;
3217             cleaned = 1 ;
3218             put_bh(cn->bh) ;
3219             if (atomic_read(&(cn->bh->b_count)) < 0) {
3220               reiserfs_warning (p_s_sb, "journal-2138: cn->bh->b_count < 0");
3221             }
3222           }
3223           if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3224             atomic_dec(&(cn->jlist->j_nonzerolen)) ;
3225           }
3226           cn->bh = NULL ; 
3227         } 
3228       }
3229       cn = cn->hnext ;
3230     }
3231   }
3232
3233   if (bh) {
3234     put_bh(bh) ; /* get_hash grabs the buffer */
3235     if (atomic_read(&(bh->b_count)) < 0) {
3236       reiserfs_warning (p_s_sb, "journal-2165: bh->b_count < 0");
3237     }
3238   }
3239   return 0 ;
3240 }
3241
3242 void reiserfs_update_inode_transaction(struct inode *inode) {
3243   REISERFS_I(inode)->i_jl = SB_JOURNAL(inode->i_sb)->j_current_jl;
3244   REISERFS_I(inode)->i_trans_id = SB_JOURNAL(inode->i_sb)->j_trans_id ;
3245 }
3246
3247 /*
3248  * returns -1 on error, 0 if no commits/barriers were done and 1
3249  * if a transaction was actually committed and the barrier was done
3250  */
3251 static int __commit_trans_jl(struct inode *inode, unsigned long id,
3252                                  struct reiserfs_journal_list *jl)
3253 {
3254     struct reiserfs_transaction_handle th ;
3255     struct super_block *sb = inode->i_sb ;
3256     int ret = 0;
3257
3258     /* is it from the current transaction, or from an unknown transaction? */
3259     if (id == SB_JOURNAL(sb)->j_trans_id) {
3260         jl = SB_JOURNAL(sb)->j_current_jl;
3261         /* try to let other writers come in and grow this transaction */
3262         let_transaction_grow(sb, id);
3263         if (SB_JOURNAL(sb)->j_trans_id != id) {
3264             goto flush_commit_only;
3265         }
3266
3267         journal_begin(&th, sb, 1) ;
3268
3269         /* someone might have ended this transaction while we joined */
3270         if (SB_JOURNAL(sb)->j_trans_id != id) {
3271             reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb), 1) ;
3272             journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb)) ;
3273             journal_end(&th, sb, 1) ;
3274             goto flush_commit_only;
3275         }
3276
3277         journal_end_sync(&th, sb, 1) ;
3278         ret = 1;
3279
3280     } else {
3281         /* this gets tricky, we have to make sure the journal list in
3282          * the inode still exists.  We know the list is still around
3283          * if we've got a larger transaction id than the oldest list
3284          */
3285 flush_commit_only:
3286         if (journal_list_still_alive(inode->i_sb, id)) {
3287             /*
3288              * we only set ret to 1 when we know for sure
3289              * the barrier hasn't been started yet on the commit
3290              * block.
3291              */
3292             if (atomic_read(&jl->j_commit_left) > 1)
3293                 ret = 1;
3294             flush_commit_list(sb, jl, 1) ;
3295         }
3296     }
3297     /* otherwise the list is gone, and long since committed */
3298     return ret;
3299 }
3300
3301 int reiserfs_commit_for_inode(struct inode *inode) {
3302     unsigned long id = REISERFS_I(inode)->i_trans_id;
3303     struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
3304
3305     /* for the whole inode, assume unset id means it was
3306      * changed in the current transaction.  More conservative
3307      */
3308     if (!id || !jl) {
3309         reiserfs_update_inode_transaction(inode) ;
3310         id = REISERFS_I(inode)->i_trans_id;
3311         /* jl will be updated in __commit_trans_jl */
3312     }
3313
3314    return __commit_trans_jl(inode, id, jl);
3315 }
3316
3317 void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb, 
3318                                       struct buffer_head *bh) {
3319     PROC_INFO_INC( p_s_sb, journal.restore_prepared );
3320     if (!bh) {
3321         return ;
3322     }
3323     if (test_and_clear_bit(BH_JRestore_dirty, &bh->b_state) &&
3324         buffer_journal_dirty(bh)) {
3325         struct reiserfs_journal_cnode *cn;
3326         cn = get_journal_hash_dev(p_s_sb,
3327                                   SB_JOURNAL(p_s_sb)->j_list_hash_table,
3328                                   bh->b_blocknr);
3329         if (cn && can_dirty(cn)) {
3330             set_bit(BH_JTest, &bh->b_state);
3331             mark_buffer_dirty(bh);
3332         }
3333     }
3334     clear_bit(BH_JPrepared, &bh->b_state) ;
3335 }
3336
3337 extern struct tree_balance *cur_tb ;
3338 /*
3339 ** before we can change a metadata block, we have to make sure it won't
3340 ** be written to disk while we are altering it.  So, we must:
3341 ** clean it
3342 ** wait on it.
3343 ** 
3344 */
3345 int reiserfs_prepare_for_journal(struct super_block *p_s_sb,
3346                                   struct buffer_head *bh, int wait) {
3347   PROC_INFO_INC( p_s_sb, journal.prepare );
3348
3349     if (test_set_buffer_locked(bh)) {
3350         if (!wait)
3351             return 0;
3352         lock_buffer(bh);
3353     }
3354     set_bit(BH_JPrepared, &bh->b_state);
3355     if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh))  {
3356         clear_bit(BH_JTest, &bh->b_state);
3357         set_bit(BH_JRestore_dirty, &bh->b_state);
3358     }
3359     unlock_buffer(bh);
3360     return 1;
3361 }
3362
3363 static void flush_old_journal_lists(struct super_block *s) {
3364     struct reiserfs_journal_list *jl;
3365     struct list_head *entry;
3366     time_t now = get_seconds();
3367
3368     while(!list_empty(&SB_JOURNAL(s)->j_journal_list)) {
3369         entry = SB_JOURNAL(s)->j_journal_list.next;
3370         jl = JOURNAL_LIST_ENTRY(entry);
3371         /* this check should always be run, to send old lists to disk */
3372         if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4))) {
3373             flush_used_journal_lists(s, jl);
3374         } else {
3375             break;
3376         }
3377     }
3378 }
3379
3380 /* 
3381 ** long and ugly.  If flush, will not return until all commit
3382 ** blocks and all real buffers in the trans are on disk.
3383 ** If no_async, won't return until all commit blocks are on disk.
3384 **
3385 ** keep reading, there are comments as you go along
3386 */
3387 static int do_journal_end(struct reiserfs_transaction_handle *th, struct super_block  * p_s_sb, unsigned long nblocks, 
3388                           int flags) {
3389   struct reiserfs_journal_cnode *cn, *next, *jl_cn; 
3390   struct reiserfs_journal_cnode *last_cn = NULL;
3391   struct reiserfs_journal_desc *desc ; 
3392   struct reiserfs_journal_commit *commit ; 
3393   struct buffer_head *c_bh ; /* commit bh */
3394   struct buffer_head *d_bh ; /* desc bh */
3395   int cur_write_start = 0 ; /* start index of current log write */
3396   int old_start ;
3397   int i ;
3398   int flush = flags & FLUSH_ALL ;
3399   int wait_on_commit = flags & WAIT ;
3400   struct reiserfs_journal_list *jl, *temp_jl;
3401   struct list_head *entry, *safe;
3402   unsigned long jindex;
3403   unsigned long commit_trans_id;
3404   int trans_half;
3405
3406   if (th->t_refcount > 1)
3407     BUG() ;
3408
3409   current->journal_info = th->t_handle_save;
3410   reiserfs_check_lock_depth(p_s_sb, "journal end");
3411   if (SB_JOURNAL(p_s_sb)->j_len == 0) {
3412       reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
3413       journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
3414   }
3415
3416   lock_journal(p_s_sb) ;
3417   if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
3418     flags |= FLUSH_ALL ;
3419     flush = 1 ;
3420   }
3421   if (SB_JOURNAL(p_s_sb)->j_next_async_flush) {
3422     flags |= COMMIT_NOW | WAIT;
3423     wait_on_commit = 1;
3424   }
3425
3426   /* check_journal_end locks the journal, and unlocks if it does not return 1 
3427   ** it tells us if we should continue with the journal_end, or just return
3428   */
3429   if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
3430     p_s_sb->s_dirt = 1;
3431     wake_queued_writers(p_s_sb);
3432     reiserfs_async_progress_wait(p_s_sb);
3433     goto out ;
3434   }
3435
3436   /* check_journal_end might set these, check again */
3437   if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
3438     flush = 1 ;
3439   }
3440
3441   /*
3442   ** j must wait means we have to flush the log blocks, and the real blocks for
3443   ** this transaction
3444   */
3445   if (SB_JOURNAL(p_s_sb)->j_must_wait > 0) {
3446     flush = 1 ;
3447   }
3448
3449 #ifdef REISERFS_PREALLOCATE
3450   /* quota ops might need to nest, setup the journal_info pointer for them */
3451   current->journal_info = th ;
3452   reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
3453                                       * the transaction */
3454   current->journal_info = th->t_handle_save ;
3455 #endif
3456   
3457   /* setup description block */
3458   d_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start) ; 
3459   set_buffer_uptodate(d_bh);
3460   desc = (struct reiserfs_journal_desc *)(d_bh)->b_data ;
3461   memset(d_bh->b_data, 0, d_bh->b_size) ;
3462   memcpy(get_journal_desc_magic (d_bh), JOURNAL_DESC_MAGIC, 8) ;
3463   set_desc_trans_id(desc, SB_JOURNAL(p_s_sb)->j_trans_id) ;
3464
3465   /* setup commit block.  Don't write (keep it clean too) this one until after everyone else is written */
3466   c_bh =  journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + 
3467                  ((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
3468   commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
3469   memset(c_bh->b_data, 0, c_bh->b_size) ;
3470   set_commit_trans_id(commit, SB_JOURNAL(p_s_sb)->j_trans_id) ;
3471   set_buffer_uptodate(c_bh) ;
3472
3473   /* init this journal list */
3474   jl = SB_JOURNAL(p_s_sb)->j_current_jl;
3475
3476   /* we lock the commit before doing anything because
3477    * we want to make sure nobody tries to run flush_commit_list until
3478    * the new transaction is fully setup, and we've already flushed the
3479    * ordered bh list
3480    */
3481   down(&jl->j_commit_lock);
3482
3483   /* save the transaction id in case we need to commit it later */
3484   commit_trans_id = jl->j_trans_id;
3485
3486   atomic_set(&jl->j_older_commits_done, 0) ;
3487   jl->j_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
3488   jl->j_timestamp = SB_JOURNAL(p_s_sb)->j_trans_start_time ;
3489   jl->j_commit_bh = c_bh ;
3490   jl->j_start = SB_JOURNAL(p_s_sb)->j_start ;
3491   jl->j_len = SB_JOURNAL(p_s_sb)->j_len ;
3492   atomic_set(&jl->j_nonzerolen, SB_JOURNAL(p_s_sb)->j_len) ;
3493   atomic_set(&jl->j_commit_left, SB_JOURNAL(p_s_sb)->j_len + 2);
3494   jl->j_realblock = NULL ;
3495
3496   /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
3497   **  for each real block, add it to the journal list hash,
3498   ** copy into real block index array in the commit or desc block
3499   */
3500   trans_half = journal_trans_half(p_s_sb->s_blocksize);
3501   for (i = 0, cn = SB_JOURNAL(p_s_sb)->j_first ; cn ; cn = cn->next, i++) {
3502     if (test_bit(BH_JDirty, &cn->bh->b_state) ) {
3503       jl_cn = get_cnode(p_s_sb) ;
3504       if (!jl_cn) {
3505         reiserfs_panic(p_s_sb, "journal-1676, get_cnode returned NULL\n") ;
3506       }
3507       if (i == 0) {
3508         jl->j_realblock = jl_cn ;
3509       }
3510       jl_cn->prev = last_cn ;
3511       jl_cn->next = NULL ;
3512       if (last_cn) {
3513         last_cn->next = jl_cn ;
3514       }
3515       last_cn = jl_cn ;
3516       /* make sure the block we are trying to log is not a block 
3517          of journal or reserved area */
3518
3519       if (is_block_in_log_or_reserved_area(p_s_sb, cn->bh->b_blocknr)) {
3520         reiserfs_panic(p_s_sb, "journal-2332: Trying to log block %lu, which is a log block\n", cn->bh->b_blocknr) ;
3521       }
3522       jl_cn->blocknr = cn->bh->b_blocknr ; 
3523       jl_cn->state = 0 ;
3524       jl_cn->sb = p_s_sb;
3525       jl_cn->bh = cn->bh ;
3526       jl_cn->jlist = jl;
3527       insert_journal_hash(SB_JOURNAL(p_s_sb)->j_list_hash_table, jl_cn) ; 
3528       if (i < trans_half) {
3529         desc->j_realblock[i] = cpu_to_le32(cn->bh->b_blocknr) ;
3530       } else {
3531         commit->j_realblock[i - trans_half] = cpu_to_le32(cn->bh->b_blocknr) ;
3532       }
3533     } else {
3534       i-- ;
3535     }
3536   }
3537   set_desc_trans_len(desc, SB_JOURNAL(p_s_sb)->j_len) ;
3538   set_desc_mount_id(desc, SB_JOURNAL(p_s_sb)->j_mount_id) ;
3539   set_desc_trans_id(desc, SB_JOURNAL(p_s_sb)->j_trans_id) ;
3540   set_commit_trans_len(commit, SB_JOURNAL(p_s_sb)->j_len);
3541
3542   /* special check in case all buffers in the journal were marked for not logging */
3543   if (SB_JOURNAL(p_s_sb)->j_len == 0) {
3544     BUG();
3545   }
3546
3547   /* we're about to dirty all the log blocks, mark the description block
3548    * dirty now too.  Don't mark the commit block dirty until all the
3549    * others are on disk
3550    */
3551   mark_buffer_dirty(d_bh);
3552
3553   /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
3554   cur_write_start = SB_JOURNAL(p_s_sb)->j_start ;
3555   cn = SB_JOURNAL(p_s_sb)->j_first ;
3556   jindex = 1 ; /* start at one so we don't get the desc again */
3557   while(cn) {
3558     clear_bit(BH_JNew, &(cn->bh->b_state)) ;
3559     /* copy all the real blocks into log area.  dirty log blocks */
3560     if (test_bit(BH_JDirty, &cn->bh->b_state)) {
3561       struct buffer_head *tmp_bh ;
3562       char *addr;
3563       struct page *page;
3564       tmp_bh =  journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + 
3565                        ((cur_write_start + jindex) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
3566       set_buffer_uptodate(tmp_bh);
3567       page = cn->bh->b_page;
3568       addr = kmap(page);
3569       memcpy(tmp_bh->b_data, addr + offset_in_page(cn->bh->b_data),
3570              cn->bh->b_size);
3571       kunmap(page);
3572       mark_buffer_dirty(tmp_bh);
3573       jindex++ ;
3574       set_bit(BH_JDirty_wait, &(cn->bh->b_state)) ; 
3575       clear_bit(BH_JDirty, &(cn->bh->b_state)) ;
3576     } else {
3577       /* JDirty cleared sometime during transaction.  don't log this one */
3578       reiserfs_warning(p_s_sb, "journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!") ;
3579       brelse(cn->bh) ;
3580     }
3581     next = cn->next ;
3582     free_cnode(p_s_sb, cn) ;
3583     cn = next ;
3584     cond_resched();
3585   }
3586
3587   /* we are done  with both the c_bh and d_bh, but
3588   ** c_bh must be written after all other commit blocks,
3589   ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
3590   */
3591
3592   SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
3593
3594   /* now it is safe to insert this transaction on the main list */
3595   list_add_tail(&jl->j_list, &SB_JOURNAL(p_s_sb)->j_journal_list);
3596   list_add_tail(&jl->j_working_list, &SB_JOURNAL(p_s_sb)->j_working_list);
3597   SB_JOURNAL(p_s_sb)->j_num_work_lists++;
3598
3599   /* reset journal values for the next transaction */
3600   old_start = SB_JOURNAL(p_s_sb)->j_start ;
3601   SB_JOURNAL(p_s_sb)->j_start = (SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb);
3602   atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
3603   SB_JOURNAL(p_s_sb)->j_bcount = 0 ;
3604   SB_JOURNAL(p_s_sb)->j_last = NULL ;
3605   SB_JOURNAL(p_s_sb)->j_first = NULL ;
3606   SB_JOURNAL(p_s_sb)->j_len = 0 ;
3607   SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;
3608   SB_JOURNAL(p_s_sb)->j_trans_id++ ;
3609   SB_JOURNAL(p_s_sb)->j_current_jl->j_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id;
3610   SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
3611   SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
3612   SB_JOURNAL(p_s_sb)->j_next_full_flush = 0 ;
3613   SB_JOURNAL(p_s_sb)->j_next_async_flush = 0 ;
3614   init_journal_hash(p_s_sb) ; 
3615
3616   // make sure reiserfs_add_jh sees the new current_jl before we
3617   // write out the tails
3618   smp_mb();
3619
3620   /* tail conversion targets have to hit the disk before we end the
3621    * transaction.  Otherwise a later transaction might repack the tail
3622    * before this transaction commits, leaving the data block unflushed and
3623    * clean, if we crash before the later transaction commits, the data block
3624    * is lost.
3625    */
3626   if (!list_empty(&jl->j_tail_bh_list)) {
3627       unlock_kernel();
3628       write_ordered_buffers(&SB_JOURNAL(p_s_sb)->j_dirty_buffers_lock,
3629                             SB_JOURNAL(p_s_sb), jl, &jl->j_tail_bh_list);
3630       lock_kernel();
3631   }
3632   if (!list_empty(&jl->j_tail_bh_list))
3633       BUG();
3634   up(&jl->j_commit_lock);
3635
3636   /* honor the flush wishes from the caller, simple commits can
3637   ** be done outside the journal lock, they are done below
3638   **
3639   ** if we don't flush the commit list right now, we put it into
3640   ** the work queue so the people waiting on the async progress work
3641   ** queue don't wait for this proc to flush journal lists and such.
3642   */
3643   if (flush) {
3644     flush_commit_list(p_s_sb, jl, 1) ;
3645     flush_journal_list(p_s_sb, jl, 1) ;
3646   } else if (!(jl->j_state & LIST_COMMIT_PENDING))
3647     queue_delayed_work(commit_wq, &SB_JOURNAL(p_s_sb)->j_work, HZ/10);
3648
3649
3650   /* if the next transaction has any chance of wrapping, flush 
3651   ** transactions that might get overwritten.  If any journal lists are very 
3652   ** old flush them as well.  
3653   */
3654 first_jl:
3655   list_for_each_safe(entry, safe, &SB_JOURNAL(p_s_sb)->j_journal_list) {
3656     temp_jl = JOURNAL_LIST_ENTRY(entry);
3657     if (SB_JOURNAL(p_s_sb)->j_start <= temp_jl->j_start) {
3658       if ((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) >=
3659           temp_jl->j_start)
3660       {
3661         flush_used_journal_lists(p_s_sb, temp_jl);
3662         goto first_jl;
3663       } else if ((SB_JOURNAL(p_s_sb)->j_start +
3664                   SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) <
3665                   SB_ONDISK_JOURNAL_SIZE(p_s_sb))
3666       {
3667           /* if we don't cross into the next transaction and we don't
3668            * wrap, there is no way we can overlap any later transactions
3669            * break now
3670            */
3671           break;
3672       }
3673     } else if ((SB_JOURNAL(p_s_sb)->j_start +
3674                 SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) >
3675                 SB_ONDISK_JOURNAL_SIZE(p_s_sb))
3676     {
3677       if (((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) %
3678             SB_ONDISK_JOURNAL_SIZE(p_s_sb)) >= temp_jl->j_start)
3679       {
3680         flush_used_journal_lists(p_s_sb, temp_jl);
3681         goto first_jl;
3682       } else {
3683           /* we don't overlap anything from out start to the end of the
3684            * log, and our wrapped portion doesn't overlap anything at
3685            * the start of the log.  We can break
3686            */
3687           break;
3688       }
3689     }
3690   }
3691   flush_old_journal_lists(p_s_sb);
3692
3693   SB_JOURNAL(p_s_sb)->j_current_jl->j_list_bitmap = get_list_bitmap(p_s_sb, SB_JOURNAL(p_s_sb)->j_current_jl) ;
3694
3695   if (!(SB_JOURNAL(p_s_sb)->j_current_jl->j_list_bitmap)) {
3696     reiserfs_panic(p_s_sb, "journal-1996: do_journal_end, could not get a list bitmap\n") ;
3697   }
3698
3699   atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
3700   unlock_journal(p_s_sb) ;
3701   /* wake up any body waiting to join. */
3702   clear_bit(WRITERS_QUEUED, &SB_JOURNAL(p_s_sb)->j_state);
3703   wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
3704
3705   if (!flush && wait_on_commit &&
3706       journal_list_still_alive(p_s_sb, commit_trans_id)) {
3707           flush_commit_list(p_s_sb, jl, 1) ;
3708   }
3709 out:
3710   reiserfs_check_lock_depth(p_s_sb, "journal end2");
3711   th->t_trans_id = 0;
3712   return 0 ;
3713 }