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