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