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