File: /usr/src/linux/fs/reiserfs/journal.c
1 /*
2 ** Write ahead logging implementation copyright Chris Mason 2000
3 **
4 ** The background commits make this code very interelated, and
5 ** overly complex. I need to rethink things a bit....The major players:
6 **
7 ** journal_begin -- call with the number of blocks you expect to log.
8 ** If the current transaction is too
9 ** old, it will block until the current transaction is
10 ** finished, and then start a new one.
11 ** Usually, your transaction will get joined in with
12 ** previous ones for speed.
13 **
14 ** journal_join -- same as journal_begin, but won't block on the current
15 ** transaction regardless of age. Don't ever call
16 ** this. Ever. There are only two places it should be
17 ** called from, and they are both inside this file.
18 **
19 ** journal_mark_dirty -- adds blocks into this transaction. clears any flags
20 ** that might make them get sent to disk
21 ** and then marks them BH_JDirty. Puts the buffer head
22 ** into the current transaction hash.
23 **
24 ** journal_end -- if the current transaction is batchable, it does nothing
25 ** otherwise, it could do an async/synchronous commit, or
26 ** a full flush of all log and real blocks in the
27 ** transaction.
28 **
29 ** flush_old_commits -- if the current transaction is too old, it is ended and
30 ** commit blocks are sent to disk. Forces commit blocks
31 ** to disk for all backgrounded commits that have been
32 ** around too long.
33 ** -- Note, if you call this as an immediate flush from
34 ** from within kupdate, it will ignore the immediate flag
35 **
36 ** The commit thread -- a writer process for async commits. It allows a
37 ** a process to request a log flush on a task queue.
38 ** the commit will happen once the commit thread wakes up.
39 ** The benefit here is the writer (with whatever
40 ** related locks it has) doesn't have to wait for the
41 ** log blocks to hit disk if it doesn't want to.
42 */
43
44 #include <linux/config.h>
45 #include <asm/uaccess.h>
46 #include <asm/system.h>
47
48 #include <linux/sched.h>
49 #include <asm/semaphore.h>
50
51 #include <linux/vmalloc.h>
52 #include <linux/reiserfs_fs.h>
53
54 #include <linux/kernel.h>
55 #include <linux/errno.h>
56 #include <linux/fcntl.h>
57 #include <linux/locks.h>
58 #include <linux/stat.h>
59 #include <linux/string.h>
60 #include <linux/smp_lock.h>
61
62 /* the number of mounted filesystems. This is used to decide when to
63 ** start and kill the commit thread
64 */
65 static int reiserfs_mounted_fs_count = 0 ;
66
67 /* wake this up when you add something to the commit thread task queue */
68 DECLARE_WAIT_QUEUE_HEAD(reiserfs_commit_thread_wait) ;
69
70 /* wait on this if you need to be sure you task queue entries have been run */
71 static DECLARE_WAIT_QUEUE_HEAD(reiserfs_commit_thread_done) ;
72 DECLARE_TASK_QUEUE(reiserfs_commit_thread_tq) ;
73
74 #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit structs at 4k */
75
76 /* cnode stat bits. Move these into reiserfs_fs.h */
77
78 #define BLOCK_FREED 2 /* this block was freed, and can't be written. */
79 #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
80
81 #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
82
83 /* flags for do_journal_end */
84 #define FLUSH_ALL 1 /* flush commit and real blocks */
85 #define COMMIT_NOW 2 /* end and commit this transaction */
86 #define WAIT 4 /* wait for the log blocks to hit the disk*/
87
88 /* state bits for the journal */
89 #define WRITERS_BLOCKED 1 /* set when new writers not allowed */
90
91 static int do_journal_end(struct reiserfs_transaction_handle *,struct super_block *,unsigned long nblocks,int flags) ;
92 static int flush_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
93 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
94 static int can_dirty(struct reiserfs_journal_cnode *cn) ;
95
96 static void init_journal_hash(struct super_block *p_s_sb) {
97 memset(SB_JOURNAL(p_s_sb)->j_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
98 }
99
100 /*
101 ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
102 ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
103 ** more details.
104 */
105 static int reiserfs_clean_and_file_buffer(struct buffer_head *bh) {
106 if (bh) {
107 clear_bit(BH_Dirty, &bh->b_state) ;
108 refile_buffer(bh) ;
109 }
110 return 0 ;
111 }
112
113 static struct reiserfs_bitmap_node *
114 allocate_bitmap_node(struct super_block *p_s_sb) {
115 struct reiserfs_bitmap_node *bn ;
116 static int id = 0 ;
117
118 bn = kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS) ;
119 if (!bn) {
120 return NULL ;
121 }
122 bn->data = kmalloc(p_s_sb->s_blocksize, GFP_NOFS) ;
123 if (!bn->data) {
124 kfree(bn) ;
125 return NULL ;
126 }
127 bn->id = id++ ;
128 memset(bn->data, 0, p_s_sb->s_blocksize) ;
129 INIT_LIST_HEAD(&bn->list) ;
130 return bn ;
131 }
132
133 static struct reiserfs_bitmap_node *
134 get_bitmap_node(struct super_block *p_s_sb) {
135 struct reiserfs_bitmap_node *bn = NULL;
136 struct list_head *entry = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
137
138 SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes++ ;
139 repeat:
140
141 if(entry != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
142 bn = list_entry(entry, struct reiserfs_bitmap_node, list) ;
143 list_del(entry) ;
144 memset(bn->data, 0, p_s_sb->s_blocksize) ;
145 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
146 return bn ;
147 }
148 bn = allocate_bitmap_node(p_s_sb) ;
149 if (!bn) {
150 current->policy = SCHED_YIELD ;
151 schedule() ;
152 goto repeat ;
153 }
154 return bn ;
155 }
156 static inline void free_bitmap_node(struct super_block *p_s_sb,
157 struct reiserfs_bitmap_node *bn) {
158 SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes-- ;
159 if (SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
160 kfree(bn->data) ;
161 kfree(bn) ;
162 } else {
163 list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
164 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
165 }
166 }
167
168 static void allocate_bitmap_nodes(struct super_block *p_s_sb) {
169 int i ;
170 struct reiserfs_bitmap_node *bn = NULL ;
171 for (i = 0 ; i < REISERFS_MIN_BITMAP_NODES ; i++) {
172 bn = allocate_bitmap_node(p_s_sb) ;
173 if (bn) {
174 list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
175 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
176 } else {
177 break ; // this is ok, we'll try again when more are needed
178 }
179 }
180 }
181
182 static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
183 struct reiserfs_list_bitmap *jb) {
184 int bmap_nr = block / (p_s_sb->s_blocksize << 3) ;
185 int bit_nr = block % (p_s_sb->s_blocksize << 3) ;
186
187 if (!jb->bitmaps[bmap_nr]) {
188 jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb) ;
189 }
190 set_bit(bit_nr, jb->bitmaps[bmap_nr]->data) ;
191 return 0 ;
192 }
193
194 static void cleanup_bitmap_list(struct super_block *p_s_sb,
195 struct reiserfs_list_bitmap *jb) {
196 int i;
197 for (i = 0 ; i < SB_BMAP_NR(p_s_sb) ; i++) {
198 if (jb->bitmaps[i]) {
199 free_bitmap_node(p_s_sb, jb->bitmaps[i]) ;
200 jb->bitmaps[i] = NULL ;
201 }
202 }
203 }
204
205 /*
206 ** only call this on FS unmount.
207 */
208 static int free_list_bitmaps(struct super_block *p_s_sb,
209 struct reiserfs_list_bitmap *jb_array) {
210 int i ;
211 struct reiserfs_list_bitmap *jb ;
212 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
213 jb = jb_array + i ;
214 jb->journal_list = NULL ;
215 cleanup_bitmap_list(p_s_sb, jb) ;
216 vfree(jb->bitmaps) ;
217 jb->bitmaps = NULL ;
218 }
219 return 0;
220 }
221
222 static int free_bitmap_nodes(struct super_block *p_s_sb) {
223 struct list_head *next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
224 struct reiserfs_bitmap_node *bn ;
225
226 while(next != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
227 bn = list_entry(next, struct reiserfs_bitmap_node, list) ;
228 list_del(next) ;
229 kfree(bn->data) ;
230 kfree(bn) ;
231 next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
232 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
233 }
234
235 return 0 ;
236 }
237
238 /*
239 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
240 ** jb_array is the array to be filled in.
241 */
242 int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
243 struct reiserfs_list_bitmap *jb_array,
244 int bmap_nr) {
245 int i ;
246 int failed = 0 ;
247 struct reiserfs_list_bitmap *jb ;
248 int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *) ;
249
250 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
251 jb = jb_array + i ;
252 jb->journal_list = NULL ;
253 jb->bitmaps = vmalloc( mem ) ;
254 if (!jb->bitmaps) {
255 reiserfs_warning("clm-2000, unable to allocate bitmaps for journal lists\n") ;
256 failed = 1;
257 break ;
258 }
259 memset(jb->bitmaps, 0, mem) ;
260 }
261 if (failed) {
262 free_list_bitmaps(p_s_sb, jb_array) ;
263 return -1 ;
264 }
265 return 0 ;
266 }
267
268 /*
269 ** find an available list bitmap. If you can't find one, flush a commit list
270 ** and try again
271 */
272 static struct reiserfs_list_bitmap *
273 get_list_bitmap(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
274 int i,j ;
275 struct reiserfs_list_bitmap *jb = NULL ;
276
277 for (j = 0 ; j < (JOURNAL_NUM_BITMAPS * 3) ; j++) {
278 i = SB_JOURNAL(p_s_sb)->j_list_bitmap_index ;
279 SB_JOURNAL(p_s_sb)->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS ;
280 jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
281 if (SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
282 flush_commit_list(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list, 1) ;
283 if (!SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
284 break ;
285 }
286 } else {
287 break ;
288 }
289 }
290 if (jb->journal_list) { /* double check to make sure if flushed correctly */
291 return NULL ;
292 }
293 jb->journal_list = jl ;
294 return jb ;
295 }
296
297 /*
298 ** allocates a new chunk of X nodes, and links them all together as a list.
299 ** Uses the cnode->next and cnode->prev pointers
300 ** returns NULL on failure
301 */
302 static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes) {
303 struct reiserfs_journal_cnode *head ;
304 int i ;
305 if (num_cnodes <= 0) {
306 return NULL ;
307 }
308 head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
309 if (!head) {
310 return NULL ;
311 }
312 memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
313 head[0].prev = NULL ;
314 head[0].next = head + 1 ;
315 for (i = 1 ; i < num_cnodes; i++) {
316 head[i].prev = head + (i - 1) ;
317 head[i].next = head + (i + 1) ; /* if last one, overwrite it after the if */
318 }
319 head[num_cnodes -1].next = NULL ;
320 return head ;
321 }
322
323 /*
324 ** pulls a cnode off the free list, or returns NULL on failure
325 */
326 static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb) {
327 struct reiserfs_journal_cnode *cn ;
328
329 reiserfs_check_lock_depth("get_cnode") ;
330
331 if (SB_JOURNAL(p_s_sb)->j_cnode_free <= 0) {
332 return NULL ;
333 }
334 SB_JOURNAL(p_s_sb)->j_cnode_used++ ;
335 SB_JOURNAL(p_s_sb)->j_cnode_free-- ;
336 cn = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
337 if (!cn) {
338 return cn ;
339 }
340 if (cn->next) {
341 cn->next->prev = NULL ;
342 }
343 SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn->next ;
344 memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ;
345 return cn ;
346 }
347
348 /*
349 ** returns a cnode to the free list
350 */
351 static void free_cnode(struct super_block *p_s_sb, struct reiserfs_journal_cnode *cn) {
352
353 reiserfs_check_lock_depth("free_cnode") ;
354
355 SB_JOURNAL(p_s_sb)->j_cnode_used-- ;
356 SB_JOURNAL(p_s_sb)->j_cnode_free++ ;
357 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
358 cn->next = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
359 if (SB_JOURNAL(p_s_sb)->j_cnode_free_list) {
360 SB_JOURNAL(p_s_sb)->j_cnode_free_list->prev = cn ;
361 }
362 cn->prev = NULL ; /* not needed with the memset, but I might kill the memset, and forget to do this */
363 SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn ;
364 }
365
366 static int clear_prepared_bits(struct buffer_head *bh) {
367 clear_bit(BH_JPrepared, &bh->b_state) ;
368 return 0 ;
369 }
370
371 /* buffer is in current transaction */
372 inline int buffer_journaled(struct buffer_head *bh) {
373 if (bh)
374 return test_bit(BH_JDirty, &bh->b_state) ;
375 else
376 return 0 ;
377 }
378
379 /* disk block was taken off free list before being in a finished transation, or written to disk
380 ** journal_new blocks can be reused immediately, for any purpose
381 */
382 inline int buffer_journal_new(struct buffer_head *bh) {
383 if (bh)
384 return test_bit(BH_JNew, &bh->b_state) ;
385 else
386 return 0 ;
387 }
388
389 inline int mark_buffer_journal_new(struct buffer_head *bh) {
390 if (bh) {
391 set_bit(BH_JNew, &bh->b_state) ;
392 }
393 return 0 ;
394 }
395
396 inline int mark_buffer_not_journaled(struct buffer_head *bh) {
397 if (bh)
398 clear_bit(BH_JDirty, &bh->b_state) ;
399 return 0 ;
400 }
401
402 /* utility function to force a BUG if it is called without the big
403 ** kernel lock held. caller is the string printed just before calling BUG()
404 */
405 void reiserfs_check_lock_depth(char *caller) {
406 #ifdef CONFIG_SMP
407 if (current->lock_depth < 0) {
408 printk("%s called without kernel lock held\n", caller) ;
409 show_reiserfs_locks() ;
410 BUG() ;
411 }
412 #else
413 ;
414 #endif
415 }
416
417 /* return a cnode with same dev, block number and size in table, or null if not found */
418 static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct reiserfs_journal_cnode **table,
419 kdev_t dev,long bl,int size) {
420 struct reiserfs_journal_cnode *cn ;
421 cn = journal_hash(table, dev, bl) ;
422 while(cn) {
423 if ((cn->blocknr == bl) && (cn->dev == dev))
424 return cn ;
425 cn = cn->hnext ;
426 }
427 return (struct reiserfs_journal_cnode *)0 ;
428 }
429
430 /* returns a cnode with same size, block number and dev as bh in the current transaction hash. NULL if not found */
431 static inline struct reiserfs_journal_cnode *get_journal_hash(struct super_block *p_s_sb, struct buffer_head *bh) {
432 struct reiserfs_journal_cnode *cn ;
433 if (bh) {
434 cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_hash_table, bh->b_dev, bh->b_blocknr, bh->b_size) ;
435 }
436 else {
437 return (struct reiserfs_journal_cnode *)0 ;
438 }
439 return cn ;
440 }
441
442 /* once upon a time, the journal would deadlock. a lot. Now, when
443 ** CONFIG_REISERFS_CHECK is defined, anytime someone enters a
444 ** transaction, it pushes itself into this ugly static list, and pops
445 ** itself off before calling journal_end. I made a SysRq key to dump
446 ** the list, and tell me what the writers are when I'm deadlocked. */
447
448 /* are you depending on the compiler
449 to optimize this function away
450 everywhere it is called? It is not
451 obvious how this works, but I
452 suppose debugging code need not be
453 clear. -Hans */
454 static char *journal_writers[512] ;
455 int push_journal_writer(char *s) {
456 #ifdef CONFIG_REISERFS_CHECK
457 int i ;
458 for (i = 0 ; i < 512 ; i++) {
459 if (!journal_writers[i]) {
460 journal_writers[i] = s ;
461 return i ;
462 }
463 }
464 return -1 ;
465 #else
466 return 0 ;
467 #endif
468 }
469 int pop_journal_writer(int index) {
470 #ifdef CONFIG_REISERFS_CHECK
471 if (index >= 0) {
472 journal_writers[index] = NULL ;
473 }
474 #endif
475 return 0 ;
476 }
477
478 int dump_journal_writers(void) {
479 int i ;
480 for (i = 0 ; i < 512 ; i++) {
481 if (journal_writers[i]) {
482 printk("%d: %s\n", i, journal_writers[i]) ;
483 }
484 }
485 return 0 ;
486 }
487
488 /*
489 ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
490 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
491 ** being overwritten by a replay after crashing.
492 **
493 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
494 ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
495 ** sure you never write the block without logging it.
496 **
497 ** next_zero_bit is a suggestion about the next block to try for find_forward.
498 ** when bl is rejected because it is set in a journal list bitmap, we search
499 ** for the next zero bit in the bitmap that rejected bl. Then, we return that
500 ** through next_zero_bit for find_forward to try.
501 **
502 ** Just because we return something in next_zero_bit does not mean we won't
503 ** reject it on the next call to reiserfs_in_journal
504 **
505 */
506 int reiserfs_in_journal(struct super_block *p_s_sb, kdev_t dev,
507 unsigned long bl, int size, int search_all,
508 unsigned long *next_zero_bit) {
509 struct reiserfs_journal_cnode *cn ;
510 struct reiserfs_list_bitmap *jb ;
511 int i ;
512 int bmap_nr = bl / (p_s_sb->s_blocksize << 3) ;
513 int bit_nr = bl % (p_s_sb->s_blocksize << 3) ;
514 int tmp_bit ;
515
516 *next_zero_bit = 0 ; /* always start this at zero. */
517
518 /* we aren't logging all blocks are safe for reuse */
519 if (reiserfs_dont_log(p_s_sb)) {
520 return 0 ;
521 }
522
523 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
524 ** if we crash before the transaction that freed it commits, this transaction won't
525 ** have committed either, and the block will never be written
526 */
527 if (search_all) {
528 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
529 jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
530 if (jb->journal_list && jb->bitmaps[bmap_nr] &&
531 test_bit(bit_nr, jb->bitmaps[bmap_nr]->data)) {
532 tmp_bit = find_next_zero_bit((unsigned long *)
533 (jb->bitmaps[bmap_nr]->data),
534 p_s_sb->s_blocksize << 3, bit_nr+1) ;
535 *next_zero_bit = bmap_nr * (p_s_sb->s_blocksize << 3) + tmp_bit ;
536 return 1 ;
537 }
538 }
539 }
540
541 /* is it in any old transactions? */
542 if (search_all && (cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_list_hash_table, dev,bl,size))) {
543 return 1;
544 }
545
546 /* is it in the current transaction. This should never happen */
547 if ((cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_hash_table, dev,bl,size))) {
548 return 1;
549 }
550
551 /* safe for reuse */
552 return 0 ;
553 }
554
555 /* insert cn into table
556 */
557 inline void insert_journal_hash(struct reiserfs_journal_cnode **table, struct reiserfs_journal_cnode *cn) {
558 struct reiserfs_journal_cnode *cn_orig ;
559
560 cn_orig = journal_hash(table, cn->dev, cn->blocknr) ;
561 cn->hnext = cn_orig ;
562 cn->hprev = NULL ;
563 if (cn_orig) {
564 cn_orig->hprev = cn ;
565 }
566 journal_hash(table, cn->dev, cn->blocknr) = cn ;
567 }
568
569 /* lock the current transaction */
570 inline static void lock_journal(struct super_block *p_s_sb) {
571 while(atomic_read(&(SB_JOURNAL(p_s_sb)->j_wlock)) > 0) {
572 sleep_on(&(SB_JOURNAL(p_s_sb)->j_wait)) ;
573 }
574 atomic_set(&(SB_JOURNAL(p_s_sb)->j_wlock), 1) ;
575 }
576
577 /* unlock the current transaction */
578 inline static void unlock_journal(struct super_block *p_s_sb) {
579 atomic_dec(&(SB_JOURNAL(p_s_sb)->j_wlock)) ;
580 wake_up(&(SB_JOURNAL(p_s_sb)->j_wait)) ;
581 }
582
583 /*
584 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
585 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
586 ** transaction.
587 */
588 static void cleanup_freed_for_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
589
590 struct reiserfs_list_bitmap *jb = jl->j_list_bitmap ;
591 if (jb) {
592 cleanup_bitmap_list(p_s_sb, jb) ;
593 }
594 jl->j_list_bitmap->journal_list = NULL ;
595 jl->j_list_bitmap = NULL ;
596 }
597
598 /*
599 ** if this journal list still has commit blocks unflushed, send them to disk.
600 **
601 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
602 ** Before the commit block can by written, every other log block must be safely on disk
603 **
604 */
605 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) {
606 int i, count ;
607 int index = 0 ;
608 int bn ;
609 int retry_count = 0 ;
610 int orig_commit_left = 0 ;
611 struct buffer_head *tbh = NULL ;
612 struct reiserfs_journal_list *other_jl ;
613
614 reiserfs_check_lock_depth("flush_commit_list") ;
615
616 if (atomic_read(&jl->j_older_commits_done)) {
617 return 0 ;
618 }
619
620 /* before we can put our commit blocks on disk, we have to make sure everyone older than
621 ** us is on disk too
622 */
623 if (jl->j_len <= 0) {
624 return 0 ;
625 }
626 if (flushall) {
627 /* we _must_ make sure the transactions are committed in order. Start with the
628 ** index after this one, wrap all the way around
629 */
630 index = (jl - SB_JOURNAL_LIST(s)) + 1 ;
631 for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
632 other_jl = SB_JOURNAL_LIST(s) + ( (index + i) % JOURNAL_LIST_COUNT) ;
633 if (other_jl && other_jl != jl && other_jl->j_len > 0 && other_jl->j_trans_id > 0 &&
634 other_jl->j_trans_id <= jl->j_trans_id && (atomic_read(&(jl->j_older_commits_done)) == 0)) {
635 flush_commit_list(s, other_jl, 0) ;
636 }
637 }
638 }
639
640 count = 0 ;
641 /* don't flush the commit list for the current transactoin */
642 if (jl == ((SB_JOURNAL_LIST(s) + SB_JOURNAL_LIST_INDEX(s)))) {
643 return 0 ;
644 }
645
646 /* make sure nobody is trying to flush this one at the same time */
647 if (atomic_read(&(jl->j_commit_flushing))) {
648 sleep_on(&(jl->j_commit_wait)) ;
649 if (flushall) {
650 atomic_set(&(jl->j_older_commits_done), 1) ;
651 }
652 return 0 ;
653 }
654
655 /* this commit is done, exit */
656 if (atomic_read(&(jl->j_commit_left)) <= 0) {
657 if (flushall) {
658 atomic_set(&(jl->j_older_commits_done), 1) ;
659 }
660 return 0 ;
661 }
662 /* keeps others from flushing while we are flushing */
663 atomic_set(&(jl->j_commit_flushing), 1) ;
664
665
666 if (jl->j_len > JOURNAL_TRANS_MAX) {
667 reiserfs_panic(s, "journal-512: flush_commit_list: length is %lu, list number %d\n", jl->j_len, jl - SB_JOURNAL_LIST(s)) ;
668 return 0 ;
669 }
670
671 orig_commit_left = atomic_read(&(jl->j_commit_left)) ;
672
673 /* start by checking all the commit blocks in this transaction.
674 ** Add anyone not on disk into tbh. Stop checking once commit_left <= 1, because that means we
675 ** only have the commit block left
676 */
677 retry:
678 count = 0 ;
679 for (i = 0 ; atomic_read(&(jl->j_commit_left)) > 1 && i < (jl->j_len + 1) ; i++) { /* everything but commit_bh */
680 bn = reiserfs_get_journal_block(s) + (jl->j_start+i) % JOURNAL_BLOCK_COUNT;
681 tbh = get_hash_table(s->s_dev, bn, s->s_blocksize) ;
682
683 /* kill this sanity check */
684 if (count > (orig_commit_left + 2)) {
685 reiserfs_panic(s, "journal-539: flush_commit_list: BAD count(%d) > orig_commit_left(%d)!\n", count, orig_commit_left) ;
686 }
687 if (tbh) {
688 if (buffer_locked(tbh)) { /* wait on it, redo it just to make sure */
689 wait_on_buffer(tbh) ;
690 if (!buffer_uptodate(tbh)) {
691 reiserfs_panic(s, "journal-584, buffer write failed\n") ;
692 }
693 }
694 if (buffer_dirty(tbh)) {
695 printk("journal-569: flush_commit_list, block already dirty!\n") ;
696 } else {
697 mark_buffer_dirty(tbh) ;
698 }
699 ll_rw_block(WRITE, 1, &tbh) ;
700 count++ ;
701 put_bh(tbh) ; /* once for our get_hash */
702 }
703 }
704
705 /* wait on everyone in tbh before writing commit block*/
706 if (count > 0) {
707 for (i = 0 ; atomic_read(&(jl->j_commit_left)) > 1 &&
708 i < (jl->j_len + 1) ; i++) { /* everything but commit_bh */
709 bn = reiserfs_get_journal_block(s) + (jl->j_start + i) % JOURNAL_BLOCK_COUNT ;
710 tbh = get_hash_table(s->s_dev, bn, s->s_blocksize) ;
711
712 wait_on_buffer(tbh) ;
713 if (!buffer_uptodate(tbh)) {
714 reiserfs_panic(s, "journal-601, buffer write failed\n") ;
715 }
716 put_bh(tbh) ; /* once for our get_hash */
717 bforget(tbh) ; /* once due to original getblk in do_journal_end */
718 atomic_dec(&(jl->j_commit_left)) ;
719 }
720 }
721
722 if (atomic_read(&(jl->j_commit_left)) != 1) { /* just the commit_bh left, flush it without calling getblk for everyone */
723 if (retry_count < 2) {
724 printk("journal-582: flush_commit_list, not all log blocks on disk yet, trying again\n") ;
725 retry_count++ ;
726 goto retry;
727 }
728 reiserfs_panic(s, "journal-563: flush_commit_list: BAD, j_commit_left is %lu, should be 1\n",
729 atomic_read(&(jl->j_commit_left)));
730 }
731
732 mark_buffer_dirty(jl->j_commit_bh) ;
733 ll_rw_block(WRITE, 1, &(jl->j_commit_bh)) ;
734 wait_on_buffer(jl->j_commit_bh) ;
735 if (!buffer_uptodate(jl->j_commit_bh)) {
736 reiserfs_panic(s, "journal-615: buffer write failed\n") ;
737 }
738 atomic_dec(&(jl->j_commit_left)) ;
739 bforget(jl->j_commit_bh) ;
740
741 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
742 cleanup_freed_for_journal_list(s, jl) ;
743
744 if (flushall) {
745 atomic_set(&(jl->j_older_commits_done), 1) ;
746 }
747 atomic_set(&(jl->j_commit_flushing), 0) ;
748 wake_up(&(jl->j_commit_wait)) ;
749
750 s->s_dirt = 1 ;
751 return 0 ;
752 }
753
754 /*
755 ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
756 ** returns NULL if it can't find anything
757 */
758 static struct reiserfs_journal_list *find_newer_jl_for_cn(struct reiserfs_journal_cnode *cn) {
759 kdev_t dev = cn->dev;
760 unsigned long blocknr = cn->blocknr ;
761
762 cn = cn->hprev ;
763 while(cn) {
764 if (cn->dev == dev && cn->blocknr == blocknr && cn->jlist) {
765 return cn->jlist ;
766 }
767 cn = cn->hprev ;
768 }
769 return NULL ;
770 }
771
772
773 /*
774 ** once all the real blocks have been flushed, it is safe to remove them from the
775 ** journal list for this transaction. Aside from freeing the cnode, this also allows the
776 ** block to be reallocated for data blocks if it had been deleted.
777 */
778 static void remove_all_from_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl, int debug) {
779 struct buffer_head fake_bh ;
780 struct reiserfs_journal_cnode *cn, *last ;
781 cn = jl->j_realblock ;
782
783 /* which is better, to lock once around the whole loop, or
784 ** to lock for each call to remove_from_journal_list?
785 */
786 while(cn) {
787 if (cn->blocknr != 0) {
788 if (debug) {
789 printk("block %lu, bh is %d, state %d\n", cn->blocknr, cn->bh ? 1: 0,
790 cn->state) ;
791 }
792 fake_bh.b_blocknr = cn->blocknr ;
793 fake_bh.b_dev = cn->dev ;
794 cn->state = 0 ;
795 remove_from_journal_list(p_s_sb, jl, &fake_bh, 1) ;
796 }
797 last = cn ;
798 cn = cn->next ;
799 free_cnode(p_s_sb, last) ;
800 }
801 jl->j_realblock = NULL ;
802 }
803
804 /*
805 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
806 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
807 ** releasing blocks in this transaction for reuse as data blocks.
808 ** called by flush_journal_list, before it calls remove_all_from_journal_list
809 **
810 */
811 static int _update_journal_header_block(struct super_block *p_s_sb, unsigned long offset, unsigned long trans_id) {
812 struct reiserfs_journal_header *jh ;
813 if (trans_id >= SB_JOURNAL(p_s_sb)->j_last_flush_trans_id) {
814 if (buffer_locked((SB_JOURNAL(p_s_sb)->j_header_bh))) {
815 wait_on_buffer((SB_JOURNAL(p_s_sb)->j_header_bh)) ;
816 if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
817 reiserfs_panic(p_s_sb, "journal-699: buffer write failed\n") ;
818 }
819 }
820 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
821 SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = offset ;
822 jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
823 jh->j_last_flush_trans_id = cpu_to_le32(trans_id) ;
824 jh->j_first_unflushed_offset = cpu_to_le32(offset) ;
825 jh->j_mount_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_mount_id) ;
826 set_bit(BH_Dirty, &(SB_JOURNAL(p_s_sb)->j_header_bh->b_state)) ;
827 ll_rw_block(WRITE, 1, &(SB_JOURNAL(p_s_sb)->j_header_bh)) ;
828 wait_on_buffer((SB_JOURNAL(p_s_sb)->j_header_bh)) ;
829 if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
830 printk( "reiserfs: journal-837: IO error during journal replay\n" );
831 return -EIO ;
832 }
833 }
834 return 0 ;
835 }
836
837 static int update_journal_header_block(struct super_block *p_s_sb,
838 unsigned long offset,
839 unsigned long trans_id) {
840 if (_update_journal_header_block(p_s_sb, offset, trans_id)) {
841 reiserfs_panic(p_s_sb, "journal-712: buffer write failed\n") ;
842 }
843 return 0 ;
844 }
845 /*
846 ** flush any and all journal lists older than you are
847 ** can only be called from flush_journal_list
848 */
849 static int flush_older_journal_lists(struct super_block *p_s_sb, struct reiserfs_journal_list *jl, unsigned long trans_id) {
850 int i, index ;
851 struct reiserfs_journal_list *other_jl ;
852
853 index = jl - SB_JOURNAL_LIST(p_s_sb) ;
854 for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
855 other_jl = SB_JOURNAL_LIST(p_s_sb) + ((index + i) % JOURNAL_LIST_COUNT) ;
856 if (other_jl && other_jl->j_len > 0 &&
857 other_jl->j_trans_id > 0 &&
858 other_jl->j_trans_id < trans_id &&
859 other_jl != jl) {
860 /* do not flush all */
861 flush_journal_list(p_s_sb, other_jl, 0) ;
862 }
863 }
864 return 0 ;
865 }
866
867 static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate) {
868 if (buffer_journaled(bh)) {
869 reiserfs_warning("clm-2084: pinned buffer %u:%s sent to disk\n",
870 bh->b_blocknr, kdevname(bh->b_dev)) ;
871 }
872 mark_buffer_uptodate(bh, uptodate) ;
873 unlock_buffer(bh) ;
874 put_bh(bh) ;
875 }
876 static void submit_logged_buffer(struct buffer_head *bh) {
877 lock_buffer(bh) ;
878 get_bh(bh) ;
879 bh->b_end_io = reiserfs_end_buffer_io_sync ;
880 mark_buffer_notjournal_new(bh) ;
881 clear_bit(BH_Dirty, &bh->b_state) ;
882 submit_bh(WRITE, bh) ;
883 }
884
885 /* flush a journal list, both commit and real blocks
886 **
887 ** always set flushall to 1, unless you are calling from inside
888 ** flush_journal_list
889 **
890 ** IMPORTANT. This can only be called while there are no journal writers,
891 ** and the journal is locked. That means it can only be called from
892 ** do_journal_end, or by journal_release
893 */
894 static int flush_journal_list(struct super_block *s,
895 struct reiserfs_journal_list *jl, int flushall) {
896 struct reiserfs_journal_list *pjl ;
897 struct reiserfs_journal_cnode *cn, *last ;
898 int count ;
899 int was_jwait = 0 ;
900 int was_dirty = 0 ;
901 struct buffer_head *saved_bh ;
902 unsigned long j_len_saved = jl->j_len ;
903
904 if (j_len_saved <= 0) {
905 return 0 ;
906 }
907
908 if (atomic_read(&SB_JOURNAL(s)->j_wcount) != 0) {
909 reiserfs_warning("clm-2048: flush_journal_list called with wcount %d\n",
910 atomic_read(&SB_JOURNAL(s)->j_wcount)) ;
911 }
912 /* if someone is getting the commit list, we must wait for them */
913 while (atomic_read(&(jl->j_commit_flushing))) {
914 sleep_on(&(jl->j_commit_wait)) ;
915 }
916 /* if someone is flushing this list, we must wait for them */
917 while (atomic_read(&(jl->j_flushing))) {
918 sleep_on(&(jl->j_flush_wait)) ;
919 }
920
921 /* this list is now ours, we can change anything we want */
922 atomic_set(&(jl->j_flushing), 1) ;
923
924 count = 0 ;
925 if (j_len_saved > JOURNAL_TRANS_MAX) {
926 reiserfs_panic(s, "journal-715: flush_journal_list, length is %lu, list number %d\n", j_len_saved, jl - SB_JOURNAL_LIST(s)) ;
927 atomic_dec(&(jl->j_flushing)) ;
928 return 0 ;
929 }
930
931 /* if all the work is already done, get out of here */
932 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
933 atomic_read(&(jl->j_commit_left)) <= 0) {
934 goto flush_older_and_return ;
935 }
936
937 /* start by putting the commit list on disk. This will also flush
938 ** the commit lists of any olders transactions
939 */
940 flush_commit_list(s, jl, 1) ;
941
942 /* are we done now? */
943 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
944 atomic_read(&(jl->j_commit_left)) <= 0) {
945 goto flush_older_and_return ;
946 }
947
948 /* loop through each cnode, see if we need to write it,
949 ** or wait on a more recent transaction, or just ignore it
950 */
951 if (atomic_read(&(SB_JOURNAL(s)->j_wcount)) != 0) {
952 reiserfs_panic(s, "journal-844: panic journal list is flushing, wcount is not 0\n") ;
953 }
954 cn = jl->j_realblock ;
955 while(cn) {
956 was_jwait = 0 ;
957 was_dirty = 0 ;
958 saved_bh = NULL ;
959 /* blocknr of 0 is no longer in the hash, ignore it */
960 if (cn->blocknr == 0) {
961 goto free_cnode ;
962 }
963 pjl = find_newer_jl_for_cn(cn) ;
964 /* the order is important here. We check pjl to make sure we
965 ** don't clear BH_JDirty_wait if we aren't the one writing this
966 ** block to disk
967 */
968 if (!pjl && cn->bh) {
969 saved_bh = cn->bh ;
970
971 /* we do this to make sure nobody releases the buffer while
972 ** we are working with it
973 */
974 get_bh(saved_bh) ;
975
976 if (buffer_journal_dirty(saved_bh)) {
977 was_jwait = 1 ;
978 mark_buffer_notjournal_dirty(saved_bh) ;
979 /* undo the inc from journal_mark_dirty */
980 put_bh(saved_bh) ;
981 }
982 if (can_dirty(cn)) {
983 was_dirty = 1 ;
984 }
985 }
986
987 /* if someone has this block in a newer transaction, just make
988 ** sure they are commited, and don't try writing it to disk
989 */
990 if (pjl) {
991 flush_commit_list(s, pjl, 1) ;
992 goto free_cnode ;
993 }
994
995 /* bh == NULL when the block got to disk on its own, OR,
996 ** the block got freed in a future transaction
997 */
998 if (saved_bh == NULL) {
999 goto free_cnode ;
1000 }
1001
1002 /* this should never happen. kupdate_one_transaction has this list
1003 ** locked while it works, so we should never see a buffer here that
1004 ** is not marked JDirty_wait
1005 */
1006 if ((!was_jwait) && !buffer_locked(saved_bh)) {
1007 printk("journal-813: BAD! buffer %lu %cdirty %cjwait, not in a newer tranasction\n", saved_bh->b_blocknr,
1008 was_dirty ? ' ' : '!', was_jwait ? ' ' : '!') ;
1009 }
1010 /* kupdate_one_transaction waits on the buffers it is writing, so we
1011 ** should never see locked buffers here
1012 */
1013 if (buffer_locked(saved_bh)) {
1014 printk("clm-2083: locked buffer %lu in flush_journal_list\n",
1015 saved_bh->b_blocknr) ;
1016 wait_on_buffer(saved_bh) ;
1017 if (!buffer_uptodate(saved_bh)) {
1018 reiserfs_panic(s, "journal-923: buffer write failed\n") ;
1019 }
1020 }
1021 if (was_dirty) {
1022 /* we inc again because saved_bh gets decremented at free_cnode */
1023 get_bh(saved_bh) ;
1024 set_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
1025 submit_logged_buffer(saved_bh) ;
1026 count++ ;
1027 } else {
1028 printk("clm-2082: Unable to flush buffer %lu in flush_journal_list\n",
1029 saved_bh->b_blocknr) ;
1030 }
1031 free_cnode:
1032 last = cn ;
1033 cn = cn->next ;
1034 if (saved_bh) {
1035 /* we incremented this to keep others from taking the buffer head away */
1036 put_bh(saved_bh) ;
1037 if (atomic_read(&(saved_bh->b_count)) < 0) {
1038 printk("journal-945: saved_bh->b_count < 0") ;
1039 }
1040 }
1041 }
1042 if (count > 0) {
1043 cn = jl->j_realblock ;
1044 while(cn) {
1045 if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1046 if (!cn->bh) {
1047 reiserfs_panic(s, "journal-1011: cn->bh is NULL\n") ;
1048 }
1049 wait_on_buffer(cn->bh) ;
1050 if (!cn->bh) {
1051 reiserfs_panic(s, "journal-1012: cn->bh is NULL\n") ;
1052 }
1053 if (!buffer_uptodate(cn->bh)) {
1054 reiserfs_panic(s, "journal-949: buffer write failed\n") ;
1055 }
1056 refile_buffer(cn->bh) ;
1057 brelse(cn->bh) ;
1058 }
1059 cn = cn->next ;
1060 }
1061 }
1062
1063 flush_older_and_return:
1064 /* before we can update the journal header block, we _must_ flush all
1065 ** real blocks from all older transactions to disk. This is because
1066 ** once the header block is updated, this transaction will not be
1067 ** replayed after a crash
1068 */
1069 if (flushall) {
1070 flush_older_journal_lists(s, jl, jl->j_trans_id) ;
1071 }
1072
1073 /* before we can remove everything from the hash tables for this
1074 ** transaction, we must make sure it can never be replayed
1075 **
1076 ** since we are only called from do_journal_end, we know for sure there
1077 ** are no allocations going on while we are flushing journal lists. So,
1078 ** we only need to update the journal header block for the last list
1079 ** being flushed
1080 */
1081 if (flushall) {
1082 update_journal_header_block(s, (jl->j_start + jl->j_len + 2) % JOURNAL_BLOCK_COUNT, jl->j_trans_id) ;
1083 }
1084 remove_all_from_journal_list(s, jl, 0) ;
1085 jl->j_len = 0 ;
1086 atomic_set(&(jl->j_nonzerolen), 0) ;
1087 jl->j_start = 0 ;
1088 jl->j_realblock = NULL ;
1089 jl->j_commit_bh = NULL ;
1090 jl->j_trans_id = 0 ;
1091 atomic_dec(&(jl->j_flushing)) ;
1092 wake_up(&(jl->j_flush_wait)) ;
1093 return 0 ;
1094 }
1095
1096
1097 static int kupdate_one_transaction(struct super_block *s,
1098 struct reiserfs_journal_list *jl)
1099 {
1100 struct reiserfs_journal_list *pjl ; /* previous list for this cn */
1101 struct reiserfs_journal_cnode *cn, *walk_cn ;
1102 unsigned long blocknr ;
1103 int run = 0 ;
1104 int orig_trans_id = jl->j_trans_id ;
1105 struct buffer_head *saved_bh ;
1106 int ret = 0 ;
1107
1108 /* if someone is getting the commit list, we must wait for them */
1109 while (atomic_read(&(jl->j_commit_flushing))) {
1110 sleep_on(&(jl->j_commit_wait)) ;
1111 }
1112 /* if someone is flushing this list, we must wait for them */
1113 while (atomic_read(&(jl->j_flushing))) {
1114 sleep_on(&(jl->j_flush_wait)) ;
1115 }
1116 /* was it flushed while we slept? */
1117 if (jl->j_len <= 0 || jl->j_trans_id != orig_trans_id) {
1118 return 0 ;
1119 }
1120
1121 /* this list is now ours, we can change anything we want */
1122 atomic_set(&(jl->j_flushing), 1) ;
1123
1124 loop_start:
1125 cn = jl->j_realblock ;
1126 while(cn) {
1127 saved_bh = NULL ;
1128 /* if the blocknr == 0, this has been cleared from the hash,
1129 ** skip it
1130 */
1131 if (cn->blocknr == 0) {
1132 goto next ;
1133 }
1134 /* look for a more recent transaction that logged this
1135 ** buffer. Only the most recent transaction with a buffer in
1136 ** it is allowed to send that buffer to disk
1137 */
1138 pjl = find_newer_jl_for_cn(cn) ;
1139 if (run == 0 && !pjl && cn->bh && buffer_journal_dirty(cn->bh) &&
1140 can_dirty(cn))
1141 {
1142 if (!test_bit(BH_JPrepared, &cn->bh->b_state)) {
1143 set_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
1144 submit_logged_buffer(cn->bh) ;
1145 } else {
1146 /* someone else is using this buffer. We can't
1147 ** send it to disk right now because they might
1148 ** be changing/logging it.
1149 */
1150 ret = 1 ;
1151 }
1152 } else if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1153 clear_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
1154 if (!pjl && cn->bh) {
1155 wait_on_buffer(cn->bh) ;
1156 }
1157 /* check again, someone could have logged while we scheduled */
1158 pjl = find_newer_jl_for_cn(cn) ;
1159
1160 /* before the JDirty_wait bit is set, the
1161 ** buffer is added to the hash list. So, if we are
1162 ** run in the middle of a do_journal_end, we will notice
1163 ** if this buffer was logged and added from the latest
1164 ** transaction. In this case, we don't want to decrement
1165 ** b_count
1166 */
1167 if (!pjl && cn->bh && buffer_journal_dirty(cn->bh)) {
1168 blocknr = cn->blocknr ;
1169 walk_cn = cn ;
1170 saved_bh= cn->bh ;
1171 /* update all older transactions to show this block
1172 ** was flushed
1173 */
1174 mark_buffer_notjournal_dirty(cn->bh) ;
1175 while(walk_cn) {
1176 if (walk_cn->bh && walk_cn->blocknr == blocknr &&
1177 walk_cn->dev == cn->dev) {
1178 if (walk_cn->jlist) {
1179 atomic_dec(&(walk_cn->jlist->j_nonzerolen)) ;
1180 }
1181 walk_cn->bh = NULL ;
1182 }
1183 walk_cn = walk_cn->hnext ;
1184 }
1185 if (atomic_read(&saved_bh->b_count) < 1) {
1186 reiserfs_warning("clm-2081: bad count on %lu\n",
1187 saved_bh->b_blocknr) ;
1188 }
1189 brelse(saved_bh) ;
1190 }
1191 }
1192 /*
1193 ** if the more recent transaction is committed to the log,
1194 ** this buffer can be considered flushed. Decrement our
1195 ** counters to reflect one less buffer that needs writing.
1196 **
1197 ** note, this relies on all of the above code being
1198 ** schedule free once pjl comes back non-null.
1199 */
1200 if (pjl && cn->bh && atomic_read(&pjl->j_commit_left) == 0) {
1201 atomic_dec(&cn->jlist->j_nonzerolen) ;
1202 cn->bh = NULL ;
1203 }
1204 next:
1205 cn = cn->next ;
1206 }
1207 /* the first run through the loop sends all the dirty buffers to
1208 ** ll_rw_block.
1209 ** the second run through the loop does all the accounting
1210 */
1211 if (run++ == 0) {
1212 goto loop_start ;
1213 }
1214
1215 atomic_set(&(jl->j_flushing), 0) ;
1216 wake_up(&(jl->j_flush_wait)) ;
1217 return ret ;
1218 }
1219 /* since we never give dirty buffers to bdflush/kupdate, we have to
1220 ** flush them ourselves. This runs through the journal lists, finds
1221 ** old metadata in need of flushing and sends it to disk.
1222 ** this does not end transactions, commit anything, or free
1223 ** cnodes.
1224 **
1225 ** returns the highest transaction id that was flushed last time
1226 */
1227 static unsigned long reiserfs_journal_kupdate(struct super_block *s) {
1228 struct reiserfs_journal_list *jl ;
1229 int i ;
1230 int start ;
1231 time_t age ;
1232 int ret = 0 ;
1233
1234 start = SB_JOURNAL_LIST_INDEX(s) ;
1235
1236 /* safety check to prevent flush attempts during a mount */
1237 if (start < 0) {
1238 return 0 ;
1239 }
1240 i = (start + 1) % JOURNAL_LIST_COUNT ;
1241 while(i != start) {
1242 jl = SB_JOURNAL_LIST(s) + i ;
1243 age = CURRENT_TIME - jl->j_timestamp ;
1244 if (jl->j_len > 0 && // age >= (JOURNAL_MAX_COMMIT_AGE * 2) &&
1245 atomic_read(&(jl->j_nonzerolen)) > 0 &&
1246 atomic_read(&(jl->j_commit_left)) == 0) {
1247
1248 if (jl->j_trans_id == SB_JOURNAL(s)->j_trans_id) {
1249 break ;
1250 }
1251 /* if ret was already 1, we want to preserve that */
1252 ret |= kupdate_one_transaction(s, jl) ;
1253 }
1254 if (atomic_read(&(jl->j_nonzerolen)) > 0) {
1255 ret |= 1 ;
1256 }
1257 i = (i + 1) % JOURNAL_LIST_COUNT ;
1258 }
1259 return ret ;
1260 }
1261
1262 /*
1263 ** removes any nodes in table with name block and dev as bh.
1264 ** only touchs the hnext and hprev pointers.
1265 */
1266 void remove_journal_hash(struct reiserfs_journal_cnode **table, struct reiserfs_journal_list *jl,struct buffer_head *bh,
1267 int remove_freed){
1268 struct reiserfs_journal_cnode *cur ;
1269 struct reiserfs_journal_cnode **head ;
1270
1271 if (!bh)
1272 return ;
1273
1274 head= &(journal_hash(table, bh->b_dev, bh->b_blocknr)) ;
1275 if (!head) {
1276 return ;
1277 }
1278 cur = *head ;
1279 while(cur) {
1280 if (cur->blocknr == bh->b_blocknr && cur->dev == bh->b_dev && (jl == NULL || jl == cur->jlist) &&
1281 (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1282 if (cur->hnext) {
1283 cur->hnext->hprev = cur->hprev ;
1284 }
1285 if (cur->hprev) {
1286 cur->hprev->hnext = cur->hnext ;
1287 } else {
1288 *head = cur->hnext ;
1289 }
1290 cur->blocknr = 0 ;
1291 cur->dev = 0 ;
1292 cur->state = 0 ;
1293 if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1294 atomic_dec(&(cur->jlist->j_nonzerolen)) ;
1295 cur->bh = NULL ;
1296 cur->jlist = NULL ;
1297 }
1298 cur = cur->hnext ;
1299 }
1300 }
1301
1302 static void free_journal_ram(struct super_block *p_s_sb) {
1303 vfree(SB_JOURNAL(p_s_sb)->j_cnode_free_orig) ;
1304 free_list_bitmaps(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap) ;
1305 free_bitmap_nodes(p_s_sb) ; /* must be after free_list_bitmaps */
1306 if (SB_JOURNAL(p_s_sb)->j_header_bh) {
1307 brelse(SB_JOURNAL(p_s_sb)->j_header_bh) ;
1308 }
1309 vfree(SB_JOURNAL(p_s_sb)) ;
1310 }
1311
1312 /*
1313 ** call on unmount. Only set error to 1 if you haven't made your way out
1314 ** of read_super() yet. Any other caller must keep error at 0.
1315 */
1316 static int do_journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, int error) {
1317 struct reiserfs_transaction_handle myth ;
1318
1319 /* we only want to flush out transactions if we were called with error == 0
1320 */
1321 if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
1322 /* end the current trans */
1323 do_journal_end(th, p_s_sb,10, FLUSH_ALL) ;
1324
1325 /* make sure something gets logged to force our way into the flush code */
1326 journal_join(&myth, p_s_sb, 1) ;
1327 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
1328 journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
1329 do_journal_end(&myth, p_s_sb,1, FLUSH_ALL) ;
1330 }
1331
1332 /* we decrement before we wake up, because the commit thread dies off
1333 ** when it has been woken up and the count is <= 0
1334 */
1335 reiserfs_mounted_fs_count-- ;
1336 wake_up(&reiserfs_commit_thread_wait) ;
1337 sleep_on(&reiserfs_commit_thread_done) ;
1338
1339 free_journal_ram(p_s_sb) ;
1340
1341 return 0 ;
1342 }
1343
1344 /*
1345 ** call on unmount. flush all journal trans, release all alloc'd ram
1346 */
1347 int journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1348 return do_journal_release(th, p_s_sb, 0) ;
1349 }
1350 /*
1351 ** only call from an error condition inside reiserfs_read_super!
1352 */
1353 int journal_release_error(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1354 return do_journal_release(th, p_s_sb, 1) ;
1355 }
1356
1357 /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
1358 static int journal_compare_desc_commit(struct super_block *p_s_sb, struct reiserfs_journal_desc *desc,
1359 struct reiserfs_journal_commit *commit) {
1360 if (le32_to_cpu(commit->j_trans_id) != le32_to_cpu(desc->j_trans_id) ||
1361 le32_to_cpu(commit->j_len) != le32_to_cpu(desc->j_len) ||
1362 le32_to_cpu(commit->j_len) > JOURNAL_TRANS_MAX ||
1363 le32_to_cpu(commit->j_len) <= 0
1364 ) {
1365 return 1 ;
1366 }
1367 return 0 ;
1368 }
1369 /* returns 0 if it did not find a description block
1370 ** returns -1 if it found a corrupt commit block
1371 ** returns 1 if both desc and commit were valid
1372 */
1373 static int journal_transaction_is_valid(struct super_block *p_s_sb, struct buffer_head *d_bh, unsigned long *oldest_invalid_trans_id, unsigned long *newest_mount_id) {
1374 struct reiserfs_journal_desc *desc ;
1375 struct reiserfs_journal_commit *commit ;
1376 struct buffer_head *c_bh ;
1377 unsigned long offset ;
1378
1379 if (!d_bh)
1380 return 0 ;
1381
1382 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1383 if (le32_to_cpu(desc->j_len) > 0 && !memcmp(desc->j_magic, JOURNAL_DESC_MAGIC, 8)) {
1384 if (oldest_invalid_trans_id && *oldest_invalid_trans_id && le32_to_cpu(desc->j_trans_id) > *oldest_invalid_trans_id) {
1385 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-986: transaction "
1386 "is valid returning because trans_id %d is greater than "
1387 "oldest_invalid %lu\n", le32_to_cpu(desc->j_trans_id),
1388 *oldest_invalid_trans_id);
1389 return 0 ;
1390 }
1391 if (newest_mount_id && *newest_mount_id > le32_to_cpu(desc->j_mount_id)) {
1392 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1087: transaction "
1393 "is valid returning because mount_id %d is less than "
1394 "newest_mount_id %lu\n", desc->j_mount_id,
1395 *newest_mount_id) ;
1396 return -1 ;
1397 }
1398 offset = d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb) ;
1399
1400 /* ok, we have a journal description block, lets see if the transaction was valid */
1401 c_bh = bread(p_s_sb->s_dev, reiserfs_get_journal_block(p_s_sb) + ((offset + le32_to_cpu(desc->j_len) + 1) % JOURNAL_BLOCK_COUNT),
1402 p_s_sb->s_blocksize) ;
1403 if (!c_bh)
1404 return 0 ;
1405 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1406 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1407 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1408 "journal_transaction_is_valid, commit offset %ld had bad "
1409 "time %d or length %d\n",
1410 c_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb),
1411 le32_to_cpu(commit->j_trans_id),
1412 le32_to_cpu(commit->j_len));
1413 brelse(c_bh) ;
1414 if (oldest_invalid_trans_id)
1415 *oldest_invalid_trans_id = le32_to_cpu(desc->j_trans_id) ;
1416 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1004: "
1417 "transaction_is_valid setting oldest invalid trans_id "
1418 "to %d\n", le32_to_cpu(desc->j_trans_id)) ;
1419 return -1;
1420 }
1421 brelse(c_bh) ;
1422 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1006: found valid "
1423 "transaction start offset %lu, len %d id %d\n",
1424 d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb),
1425 le32_to_cpu(desc->j_len), le32_to_cpu(desc->j_trans_id)) ;
1426 return 1 ;
1427 } else {
1428 return 0 ;
1429 }
1430 }
1431
1432 static void brelse_array(struct buffer_head **heads, int num) {
1433 int i ;
1434 for (i = 0 ; i < num ; i++) {
1435 brelse(heads[i]) ;
1436 }
1437 }
1438
1439 /*
1440 ** given the start, and values for the oldest acceptable transactions,
1441 ** this either reads in a replays a transaction, or returns because the transaction
1442 ** is invalid, or too old.
1443 */
1444 static int journal_read_transaction(struct super_block *p_s_sb, unsigned long cur_dblock, unsigned long oldest_start,
1445 unsigned long oldest_trans_id, unsigned long newest_mount_id) {
1446 struct reiserfs_journal_desc *desc ;
1447 struct reiserfs_journal_commit *commit ;
1448 unsigned long trans_id = 0 ;
1449 struct buffer_head *c_bh ;
1450 struct buffer_head *d_bh ;
1451 struct buffer_head **log_blocks = NULL ;
1452 struct buffer_head **real_blocks = NULL ;
1453 unsigned long trans_offset ;
1454 int i;
1455
1456 d_bh = bread(p_s_sb->s_dev, cur_dblock, p_s_sb->s_blocksize) ;
1457 if (!d_bh)
1458 return 1 ;
1459 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1460 trans_offset = d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb) ;
1461 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
1462 "journal_read_transaction, offset %lu, len %d mount_id %d\n",
1463 d_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb),
1464 le32_to_cpu(desc->j_len), le32_to_cpu(desc->j_mount_id)) ;
1465 if (le32_to_cpu(desc->j_trans_id) < oldest_trans_id) {
1466 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
1467 "journal_read_trans skipping because %lu is too old\n",
1468 cur_dblock - reiserfs_get_journal_block(p_s_sb)) ;
1469 brelse(d_bh) ;
1470 return 1 ;
1471 }
1472 if (le32_to_cpu(desc->j_mount_id) != newest_mount_id) {
1473 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
1474 "journal_read_trans skipping because %d is != "
1475 "newest_mount_id %lu\n", le32_to_cpu(desc->j_mount_id),
1476 newest_mount_id) ;
1477 brelse(d_bh) ;
1478 return 1 ;
1479 }
1480 c_bh = bread(p_s_sb->s_dev, reiserfs_get_journal_block(p_s_sb) + ((trans_offset + le32_to_cpu(desc->j_len) + 1) % JOURNAL_BLOCK_COUNT),
1481 p_s_sb->s_blocksize) ;
1482 if (!c_bh) {
1483 brelse(d_bh) ;
1484 return 1 ;
1485 }
1486 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1487 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1488 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal_read_transaction, "
1489 "commit offset %ld had bad time %d or length %d\n",
1490 c_bh->b_blocknr - reiserfs_get_journal_block(p_s_sb),
1491 le32_to_cpu(commit->j_trans_id), le32_to_cpu(commit->j_len));
1492 brelse(c_bh) ;
1493 brelse(d_bh) ;
1494 return 1;
1495 }
1496 trans_id = le32_to_cpu(desc->j_trans_id) ;
1497 /* now we know we've got a good transaction, and it was inside the valid time ranges */
1498 log_blocks = kmalloc(le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), GFP_NOFS) ;
1499 real_blocks = kmalloc(le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), GFP_NOFS) ;
1500 if (!log_blocks || !real_blocks) {
1501 brelse(c_bh) ;
1502 brelse(d_bh) ;
1503 kfree(log_blocks) ;
1504 kfree(real_blocks) ;
1505 reiserfs_warning("journal-1169: kmalloc failed, unable to mount FS\n") ;
1506 return -1 ;
1507 }
1508 /* get all the buffer heads */
1509 for(i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
1510 log_blocks[i] = getblk(p_s_sb->s_dev, reiserfs_get_journal_block(p_s_sb) + (trans_offset + 1 + i) % JOURNAL_BLOCK_COUNT, p_s_sb->s_blocksize);
1511 if (i < JOURNAL_TRANS_HALF) {
1512 real_blocks[i] = getblk(p_s_sb->s_dev, le32_to_cpu(desc->j_realblock[i]), p_s_sb->s_blocksize) ;
1513 } else {
1514 real_blocks[i] = getblk(p_s_sb->s_dev, le32_to_cpu(commit->j_realblock[i - JOURNAL_TRANS_HALF]), p_s_sb->s_blocksize) ;
1515 }
1516 if (real_blocks[i]->b_blocknr >= reiserfs_get_journal_block(p_s_sb) &&
1517 real_blocks[i]->b_blocknr < (reiserfs_get_journal_block(p_s_sb)+JOURNAL_BLOCK_COUNT)) {
1518 reiserfs_warning("journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block\n") ;
1519 brelse_array(log_blocks, i) ;
1520 brelse_array(real_blocks, i) ;
1521 brelse(c_bh) ;
1522 brelse(d_bh) ;
1523 kfree(log_blocks) ;
1524 kfree(real_blocks) ;
1525 return -1 ;
1526 }
1527 }
1528 /* read in the log blocks, memcpy to the corresponding real block */
1529 ll_rw_block(READ, le32_to_cpu(desc->j_len), log_blocks) ;
1530 for (i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
1531 wait_on_buffer(log_blocks[i]) ;
1532 if (!buffer_uptodate(log_blocks[i])) {
1533 reiserfs_warning("journal-1212: REPLAY FAILURE fsck required! buffer write failed\n") ;
1534 brelse_array(log_blocks + i, le32_to_cpu(desc->j_len) - i) ;
1535 brelse_array(real_blocks, le32_to_cpu(desc->j_len)) ;
1536 brelse(c_bh) ;
1537 brelse(d_bh) ;
1538 kfree(log_blocks) ;
1539 kfree(real_blocks) ;
1540 return -1 ;
1541 }
1542 memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data, real_blocks[i]->b_size) ;
1543 mark_buffer_uptodate(real_blocks[i], 1) ;
1544 brelse(log_blocks[i]) ;
1545 }
1546 /* flush out the real blocks */
1547 for (i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
1548 set_bit(BH_Dirty, &(real_blocks[i]->b_state)) ;
1549 ll_rw_block(WRITE, 1, real_blocks + i) ;
1550 }
1551 for (i = 0 ; i < le32_to_cpu(desc->j_len) ; i++) {
1552 wait_on_buffer(real_blocks[i]) ;
1553 if (!buffer_uptodate(real_blocks[i])) {
1554 reiserfs_warning("journal-1226: REPLAY FAILURE, fsck required! buffer write failed\n") ;
1555 brelse_array(real_blocks + i, le32_to_cpu(desc->j_len) - i) ;
1556 brelse(c_bh) ;
1557 brelse(d_bh) ;
1558 kfree(log_blocks) ;
1559 kfree(real_blocks) ;
1560 return -1 ;
1561 }
1562 brelse(real_blocks[i]) ;
1563 }
1564 cur_dblock = reiserfs_get_journal_block(p_s_sb) + ((trans_offset + le32_to_cpu(desc->j_len) + 2) % JOURNAL_BLOCK_COUNT) ;
1565 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1095: setting journal "
1566 "start to offset %ld\n",
1567 cur_dblock - reiserfs_get_journal_block(p_s_sb)) ;
1568
1569 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
1570 SB_JOURNAL(p_s_sb)->j_start = cur_dblock - reiserfs_get_journal_block(p_s_sb) ;
1571 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
1572 SB_JOURNAL(p_s_sb)->j_trans_id = trans_id + 1;
1573 brelse(c_bh) ;
1574 brelse(d_bh) ;
1575 kfree(log_blocks) ;
1576 kfree(real_blocks) ;
1577 return 0 ;
1578 }
1579
1580 /*
1581 ** read and replay the log
1582 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
1583 ** transaction. This tests that before finding all the transactions in the log, whic makes normal mount times fast.
1584 **
1585 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
1586 **
1587 ** On exit, it sets things up so the first transaction will work correctly.
1588 */
1589 static int journal_read(struct super_block *p_s_sb) {
1590 struct reiserfs_journal_desc *desc ;
1591 unsigned long last_flush_trans_id = 0 ;
1592 unsigned long oldest_trans_id = 0;
1593 unsigned long oldest_invalid_trans_id = 0 ;
1594 time_t start ;
1595 unsigned long last_flush_start = 0;
1596 unsigned long oldest_start = 0;
1597 unsigned long cur_dblock = 0 ;
1598 unsigned long newest_mount_id = 9 ;
1599 struct buffer_head *d_bh ;
1600 struct reiserfs_journal_header *jh ;
1601 int valid_journal_header = 0 ;
1602 int replay_count = 0 ;
1603 int continue_replay = 1 ;
1604 int ret ;
1605
1606 cur_dblock = reiserfs_get_journal_block(p_s_sb) ;
1607 printk("reiserfs: checking transaction log (device %s) ...\n",
1608 kdevname(p_s_sb->s_dev)) ;
1609 start = CURRENT_TIME ;
1610
1611 /* step 1, read in the journal header block. Check the transaction it says
1612 ** is the first unflushed, and if that transaction is not valid,
1613 ** replay is done
1614 */
1615 SB_JOURNAL(p_s_sb)->j_header_bh = bread(p_s_sb->s_dev,
1616 reiserfs_get_journal_block(p_s_sb) +
1617 JOURNAL_BLOCK_COUNT,
1618 p_s_sb->s_blocksize) ;
1619 if (!SB_JOURNAL(p_s_sb)->j_header_bh) {
1620 return 1 ;
1621 }
1622 jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
1623 if (le32_to_cpu(jh->j_first_unflushed_offset) >= 0 &&
1624 le32_to_cpu(jh->j_first_unflushed_offset) < JOURNAL_BLOCK_COUNT &&
1625 le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
1626 last_flush_start = reiserfs_get_journal_block(p_s_sb) +
1627 le32_to_cpu(jh->j_first_unflushed_offset) ;
1628 last_flush_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) ;
1629 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1153: found in "
1630 "header: first_unflushed_offset %d, last_flushed_trans_id "
1631 "%lu\n", le32_to_cpu(jh->j_first_unflushed_offset),
1632 last_flush_trans_id) ;
1633 valid_journal_header = 1 ;
1634
1635 /* now, we try to read the first unflushed offset. If it is not valid,
1636 ** there is nothing more we can do, and it makes no sense to read
1637 ** through the whole log.
1638 */
1639 d_bh = bread(p_s_sb->s_dev, reiserfs_get_journal_block(p_s_sb) + le32_to_cpu(jh->j_first_unflushed_offset), p_s_sb->s_blocksize) ;
1640 ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL) ;
1641 if (!ret) {
1642 continue_replay = 0 ;
1643 }
1644 brelse(d_bh) ;
1645 }
1646
1647 if (continue_replay && is_read_only(p_s_sb->s_dev)) {
1648 printk("clm-2076: device is readonly, unable to replay log\n") ;
1649 return -1 ;
1650 }
1651 if (continue_replay && (p_s_sb->s_flags & MS_RDONLY)) {
1652 printk("Warning, log replay starting on readonly filesystem\n") ;
1653 }
1654
1655 /* ok, there are transactions that need to be replayed. start with the first log block, find
1656 ** all the valid transactions, and pick out the oldest.
1657 */
1658 while(continue_replay && cur_dblock < (reiserfs_get_journal_block(p_s_sb) + JOURNAL_BLOCK_COUNT)) {
1659 d_bh = bread(p_s_sb->s_dev, cur_dblock, p_s_sb->s_blocksize) ;
1660 ret = journal_transaction_is_valid(p_s_sb, d_bh, &oldest_invalid_trans_id, &newest_mount_id) ;
1661 if (ret == 1) {
1662 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1663 if (oldest_start == 0) { /* init all oldest_ values */
1664 oldest_trans_id = le32_to_cpu(desc->j_trans_id) ;
1665 oldest_start = d_bh->b_blocknr ;
1666 newest_mount_id = le32_to_cpu(desc->j_mount_id) ;
1667 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1179: Setting "
1668 "oldest_start to offset %lu, trans_id %lu\n",
1669 oldest_start - reiserfs_get_journal_block(p_s_sb),
1670 oldest_trans_id) ;
1671 } else if (oldest_trans_id > le32_to_cpu(desc->j_trans_id)) {
1672 /* one we just read was older */
1673 oldest_trans_id = le32_to_cpu(desc->j_trans_id) ;
1674 oldest_start = d_bh->b_blocknr ;
1675 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1180: Resetting "
1676 "oldest_start to offset %lu, trans_id %lu\n",
1677 oldest_start - reiserfs_get_journal_block(p_s_sb),
1678 oldest_trans_id) ;
1679 }
1680 if (newest_mount_id < le32_to_cpu(desc->j_mount_id)) {
1681 newest_mount_id = le32_to_cpu(desc->j_mount_id) ;
1682 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
1683 "newest_mount_id to %d\n", le32_to_cpu(desc->j_mount_id));
1684 }
1685 cur_dblock += le32_to_cpu(desc->j_len) + 2 ;
1686 }
1687 else {
1688 cur_dblock++ ;
1689 }
1690 brelse(d_bh) ;
1691 }
1692 /* step three, starting at the oldest transaction, replay */
1693 if (last_flush_start > 0) {
1694 oldest_start = last_flush_start ;
1695 oldest_trans_id = last_flush_trans_id ;
1696 }
1697 cur_dblock = oldest_start ;
1698 if (oldest_trans_id) {
1699 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1206: Starting replay "
1700 "from offset %lu, trans_id %lu\n",
1701 cur_dblock - reiserfs_get_journal_block(p_s_sb),
1702 oldest_trans_id) ;
1703
1704 }
1705 replay_count = 0 ;
1706 while(continue_replay && oldest_trans_id > 0) {
1707 ret = journal_read_transaction(p_s_sb, cur_dblock, oldest_start, oldest_trans_id, newest_mount_id) ;
1708 if (ret < 0) {
1709 return ret ;
1710 } else if (ret != 0) {
1711 break ;
1712 }
1713 cur_dblock = reiserfs_get_journal_block(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start ;
1714 replay_count++ ;
1715 }
1716
1717 if (oldest_trans_id == 0) {
1718 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1225: No valid "
1719 "transactions found\n") ;
1720 }
1721 /* j_start does not get set correctly if we don't replay any transactions.
1722 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
1723 ** copy the trans_id from the header
1724 */
1725 if (valid_journal_header && replay_count == 0) {
1726 SB_JOURNAL(p_s_sb)->j_start = le32_to_cpu(jh->j_first_unflushed_offset) ;
1727 SB_JOURNAL(p_s_sb)->j_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
1728 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) ;
1729 SB_JOURNAL(p_s_sb)->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
1730 } else {
1731 SB_JOURNAL(p_s_sb)->j_mount_id = newest_mount_id + 1 ;
1732 }
1733 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
1734 "newest_mount_id to %lu\n", SB_JOURNAL(p_s_sb)->j_mount_id) ;
1735 SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = SB_JOURNAL(p_s_sb)->j_start ;
1736 if (replay_count > 0) {
1737 printk("reiserfs: replayed %d transactions in %lu seconds\n", replay_count,
1738 CURRENT_TIME - start) ;
1739 }
1740 if (!is_read_only(p_s_sb->s_dev) &&
1741 _update_journal_header_block(p_s_sb, SB_JOURNAL(p_s_sb)->j_start,
1742 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id))
1743 {
1744 /* replay failed, caller must call free_journal_ram and abort
1745 ** the mount
1746 */
1747 return -1 ;
1748 }
1749 return 0 ;
1750 }
1751
1752
1753 struct reiserfs_journal_commit_task {
1754 struct super_block *p_s_sb ;
1755 int jindex ;
1756 int wake_on_finish ; /* if this is one, we wake the task_done queue, if it
1757 ** is zero, we free the whole struct on finish
1758 */
1759 struct reiserfs_journal_commit_task *self ;
1760 struct wait_queue *task_done ;
1761 struct tq_struct task ;
1762 } ;
1763
1764 static void reiserfs_journal_commit_task_func(struct reiserfs_journal_commit_task *ct) {
1765
1766 struct reiserfs_journal_list *jl ;
1767 jl = SB_JOURNAL_LIST(ct->p_s_sb) + ct->jindex ;
1768
1769 flush_commit_list(ct->p_s_sb, SB_JOURNAL_LIST(ct->p_s_sb) + ct->jindex, 1) ;
1770 if (jl->j_len > 0 && atomic_read(&(jl->j_nonzerolen)) > 0 &&
1771 atomic_read(&(jl->j_commit_left)) == 0) {
1772 kupdate_one_transaction(ct->p_s_sb, jl) ;
1773 }
1774 kfree(ct->self) ;
1775 }
1776
1777 static void setup_commit_task_arg(struct reiserfs_journal_commit_task *ct,
1778 struct super_block *p_s_sb,
1779 int jindex) {
1780 if (!ct) {
1781 reiserfs_panic(NULL, "journal-1360: setup_commit_task_arg called with NULL struct\n") ;
1782 }
1783 ct->p_s_sb = p_s_sb ;
1784 ct->jindex = jindex ;
1785 ct->task_done = NULL ;
1786 INIT_LIST_HEAD(&ct->task.list) ;
1787 ct->task.sync = 0 ;
1788 ct->task.routine = (void *)(void *)reiserfs_journal_commit_task_func ;
1789 ct->self = ct ;
1790 ct->task.data = (void *)ct ;
1791 }
1792
1793 static void commit_flush_async(struct super_block *p_s_sb, int jindex) {
1794 struct reiserfs_journal_commit_task *ct ;
1795 /* using GFP_NOFS, GFP_KERNEL could try to flush inodes, which will try
1796 ** to start/join a transaction, which will deadlock
1797 */
1798 ct = kmalloc(sizeof(struct reiserfs_journal_commit_task), GFP_NOFS) ;
1799 if (ct) {
1800 setup_commit_task_arg(ct, p_s_sb, jindex) ;
1801 queue_task(&(ct->task), &reiserfs_commit_thread_tq);
1802 wake_up(&reiserfs_commit_thread_wait) ;
1803 } else {
1804 #ifdef CONFIG_REISERFS_CHECK
1805 reiserfs_warning("journal-1540: kmalloc failed, doing sync commit\n") ;
1806 #endif
1807 flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1) ;
1808 }
1809 }
1810
1811 /*
1812 ** this is the commit thread. It is started with kernel_thread on
1813 ** FS mount, and journal_release() waits for it to exit.
1814 **
1815 ** It could do a periodic commit, but there is a lot code for that
1816 ** elsewhere right now, and I only wanted to implement this little
1817 ** piece for starters.
1818 **
1819 ** All we do here is sleep on the j_commit_thread_wait wait queue, and
1820 ** then run the per filesystem commit task queue when we wakeup.
1821 */
1822 static int reiserfs_journal_commit_thread(void *nullp) {
1823
1824 daemonize() ;
1825
1826 spin_lock_irq(¤t->sigmask_lock);
1827 sigfillset(¤t->blocked);
1828 recalc_sigpending(current);
1829 spin_unlock_irq(¤t->sigmask_lock);
1830
1831 sprintf(current->comm, "kreiserfsd") ;
1832 lock_kernel() ;
1833 while(1) {
1834
1835 while(TQ_ACTIVE(reiserfs_commit_thread_tq)) {
1836 run_task_queue(&reiserfs_commit_thread_tq) ;
1837 }
1838
1839 /* if there aren't any more filesystems left, break */
1840 if (reiserfs_mounted_fs_count <= 0) {
1841 run_task_queue(&reiserfs_commit_thread_tq) ;
1842 break ;
1843 }
1844 wake_up(&reiserfs_commit_thread_done) ;
1845 interruptible_sleep_on_timeout(&reiserfs_commit_thread_wait, 5) ;
1846 }
1847 unlock_kernel() ;
1848 wake_up(&reiserfs_commit_thread_done) ;
1849 return 0 ;
1850 }
1851
1852 static void journal_list_init(struct super_block *p_s_sb) {
1853 int i ;
1854 for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
1855 init_waitqueue_head(&(SB_JOURNAL_LIST(p_s_sb)[i].j_commit_wait)) ;
1856 init_waitqueue_head(&(SB_JOURNAL_LIST(p_s_sb)[i].j_flush_wait)) ;
1857 }
1858 }
1859
1860 /*
1861 ** must be called once on fs mount. calls journal_read for you
1862 */
1863 int journal_init(struct super_block *p_s_sb) {
1864 int num_cnodes = JOURNAL_BLOCK_COUNT * 2 ;
1865
1866 if (sizeof(struct reiserfs_journal_commit) != 4096 ||
1867 sizeof(struct reiserfs_journal_desc) != 4096
1868 ) {
1869 printk("journal-1249: commit or desc struct not 4096 %Zd %Zd\n", sizeof(struct reiserfs_journal_commit),
1870 sizeof(struct reiserfs_journal_desc)) ;
1871 return 1 ;
1872 }
1873 /* sanity check to make sure they don't overflow the journal */
1874 if (JOURNAL_BLOCK_COUNT > reiserfs_get_journal_orig_size(p_s_sb)) {
1875 printk("journal-1393: current JOURNAL_BLOCK_COUNT (%d) is too big. This FS was created with a journal size of %lu blocks\n",
1876 JOURNAL_BLOCK_COUNT, reiserfs_get_journal_orig_size(p_s_sb)) ;
1877 return 1 ;
1878 }
1879 SB_JOURNAL(p_s_sb) = vmalloc(sizeof (struct reiserfs_journal)) ;
1880
1881 if (!SB_JOURNAL(p_s_sb)) {
1882 printk("journal-1256: unable to get memory for journal structure\n") ;
1883 return 1 ;
1884 }
1885 memset(SB_JOURNAL(p_s_sb), 0, sizeof(struct reiserfs_journal)) ;
1886
1887 SB_JOURNAL(p_s_sb)->j_list_bitmap_index = 0 ;
1888 SB_JOURNAL_LIST_INDEX(p_s_sb) = -10000 ; /* make sure flush_old_commits does not try to flush a list while replay is on */
1889
1890 /* clear out the journal list array */
1891 memset(SB_JOURNAL_LIST(p_s_sb), 0, sizeof(struct reiserfs_journal_list) * JOURNAL_LIST_COUNT) ;
1892 journal_list_init(p_s_sb) ;
1893
1894 memset(SB_JOURNAL(p_s_sb)->j_list_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
1895 memset(journal_writers, 0, sizeof(char *) * 512) ; /* debug code */
1896
1897 INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
1898 INIT_LIST_HEAD(&(SB_JOURNAL(p_s_sb)->j_dummy_inode.i_dirty_buffers)) ;
1899 reiserfs_allocate_list_bitmaps(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap,
1900 SB_BMAP_NR(p_s_sb)) ;
1901 allocate_bitmap_nodes(p_s_sb) ;
1902
1903 SB_JOURNAL(p_s_sb)->j_start = 0 ;
1904 SB_JOURNAL(p_s_sb)->j_len = 0 ;
1905 SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
1906 atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
1907 SB_JOURNAL(p_s_sb)->j_bcount = 0 ;
1908 SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;
1909 SB_JOURNAL(p_s_sb)->j_last = NULL ;
1910 SB_JOURNAL(p_s_sb)->j_first = NULL ;
1911 init_waitqueue_head(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
1912 init_waitqueue_head(&(SB_JOURNAL(p_s_sb)->j_wait)) ;
1913
1914 SB_JOURNAL(p_s_sb)->j_trans_id = 10 ;
1915 SB_JOURNAL(p_s_sb)->j_mount_id = 10 ;
1916 SB_JOURNAL(p_s_sb)->j_state = 0 ;
1917 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
1918 atomic_set(&(SB_JOURNAL(p_s_sb)->j_wlock), 0) ;
1919 SB_JOURNAL(p_s_sb)->j_cnode_free_list = allocate_cnodes(num_cnodes) ;
1920 SB_JOURNAL(p_s_sb)->j_cnode_free_orig = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
1921 SB_JOURNAL(p_s_sb)->j_cnode_free = SB_JOURNAL(p_s_sb)->j_cnode_free_list ? num_cnodes : 0 ;
1922 SB_JOURNAL(p_s_sb)->j_cnode_used = 0 ;
1923 SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
1924 init_journal_hash(p_s_sb) ;
1925 SB_JOURNAL_LIST(p_s_sb)[0].j_list_bitmap = get_list_bitmap(p_s_sb, SB_JOURNAL_LIST(p_s_sb)) ;
1926 if (!(SB_JOURNAL_LIST(p_s_sb)[0].j_list_bitmap)) {
1927 reiserfs_warning("journal-2005, get_list_bitmap failed for journal list 0\n") ;
1928 return 1 ;
1929 }
1930 if (journal_read(p_s_sb) < 0) {
1931 reiserfs_warning("Replay Failure, unable to mount\n") ;
1932 free_journal_ram(p_s_sb) ;
1933 return 1 ;
1934 }
1935 SB_JOURNAL_LIST_INDEX(p_s_sb) = 0 ; /* once the read is done, we can set this
1936 where it belongs */
1937
1938 INIT_LIST_HEAD (&SB_JOURNAL(p_s_sb)->j_prealloc_list);
1939
1940 if (reiserfs_dont_log (p_s_sb))
1941 return 0;
1942
1943 reiserfs_mounted_fs_count++ ;
1944 if (reiserfs_mounted_fs_count <= 1) {
1945 kernel_thread((void *)(void *)reiserfs_journal_commit_thread, NULL,
1946 CLONE_FS | CLONE_FILES | CLONE_VM) ;
1947 }
1948 return 0 ;
1949 }
1950
1951 /*
1952 ** test for a polite end of the current transaction. Used by file_write, and should
1953 ** be used by delete to make sure they don't write more than can fit inside a single
1954 ** transaction
1955 */
1956 int journal_transaction_should_end(struct reiserfs_transaction_handle *th, int new_alloc) {
1957 time_t now = CURRENT_TIME ;
1958 if (reiserfs_dont_log(th->t_super))
1959 return 0 ;
1960 if ( SB_JOURNAL(th->t_super)->j_must_wait > 0 ||
1961 (SB_JOURNAL(th->t_super)->j_len_alloc + new_alloc) >= JOURNAL_MAX_BATCH ||
1962 atomic_read(&(SB_JOURNAL(th->t_super)->j_jlock)) ||
1963 (now - SB_JOURNAL(th->t_super)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE ||
1964 SB_JOURNAL(th->t_super)->j_cnode_free < (JOURNAL_TRANS_MAX * 3)) {
1965 return 1 ;
1966 }
1967 return 0 ;
1968 }
1969
1970 /* this must be called inside a transaction, and requires the
1971 ** kernel_lock to be held
1972 */
1973 void reiserfs_block_writes(struct reiserfs_transaction_handle *th) {
1974 struct super_block *s = th->t_super ;
1975 SB_JOURNAL(s)->j_must_wait = 1 ;
1976 set_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
1977 return ;
1978 }
1979
1980 /* this must be called without a transaction started, and does not
1981 ** require BKL
1982 */
1983 void reiserfs_allow_writes(struct super_block *s) {
1984 clear_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
1985 wake_up(&SB_JOURNAL(s)->j_join_wait) ;
1986 }
1987
1988 /* this must be called without a transaction started, and does not
1989 ** require BKL
1990 */
1991 void reiserfs_wait_on_write_block(struct super_block *s) {
1992 wait_event(SB_JOURNAL(s)->j_join_wait,
1993 !test_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state)) ;
1994 }
1995
1996 /* join == true if you must join an existing transaction.
1997 ** join == false if you can deal with waiting for others to finish
1998 **
1999 ** this will block until the transaction is joinable. send the number of blocks you
2000 ** expect to use in nblocks.
2001 */
2002 static int do_journal_begin_r(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,unsigned long nblocks,int join) {
2003 time_t now = CURRENT_TIME ;
2004 int old_trans_id ;
2005
2006 reiserfs_check_lock_depth("journal_begin") ;
2007 #ifdef CONFIG_REISERFS_CHECK
2008 if (p_s_sb->s_flags & MS_RDONLY) {
2009 printk("clm-2078: calling journal_begin on readonly FS\n") ;
2010 BUG() ;
2011 }
2012 #endif
2013
2014 if (reiserfs_dont_log(p_s_sb)) {
2015 th->t_super = p_s_sb ; /* others will check this for the don't log flag */
2016 return 0 ;
2017 }
2018
2019 relock:
2020 lock_journal(p_s_sb) ;
2021
2022 if (test_bit(WRITERS_BLOCKED, &SB_JOURNAL(p_s_sb)->j_state)) {
2023 unlock_journal(p_s_sb) ;
2024 reiserfs_wait_on_write_block(p_s_sb) ;
2025 goto relock ;
2026 }
2027
2028 /* if there is no room in the journal OR
2029 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
2030 ** we don't sleep if there aren't other writers
2031 */
2032
2033
2034 if ( (!join && SB_JOURNAL(p_s_sb)->j_must_wait > 0) ||
2035 ( !join && (SB_JOURNAL(p_s_sb)->j_len_alloc + nblocks + 2) >= JOURNAL_MAX_BATCH) ||
2036 (!join && atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0 && SB_JOURNAL(p_s_sb)->j_trans_start_time > 0 &&
2037 (now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE) ||
2038 (!join && atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)) ) ||
2039 (!join && SB_JOURNAL(p_s_sb)->j_cnode_free < (JOURNAL_TRANS_MAX * 3))) {
2040
2041 unlock_journal(p_s_sb) ; /* allow others to finish this transaction */
2042
2043 /* if writer count is 0, we can just force this transaction to end, and start
2044 ** a new one afterwards.
2045 */
2046 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0) {
2047 struct reiserfs_transaction_handle myth ;
2048 journal_join(&myth, p_s_sb, 1) ;
2049 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
2050 journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
2051 do_journal_end(&myth, p_s_sb,1,COMMIT_NOW) ;
2052 } else {
2053 /* but if the writer count isn't zero, we have to wait for the current writers to finish.
2054 ** They won't batch on transaction end once we set j_jlock
2055 */
2056 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
2057 old_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
2058 while(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)) &&
2059 SB_JOURNAL(p_s_sb)->j_trans_id == old_trans_id) {
2060 sleep_on(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
2061 }
2062 }
2063 goto relock ;
2064 }
2065
2066 if (SB_JOURNAL(p_s_sb)->j_trans_start_time == 0) { /* we are the first writer, set trans_id */
2067 SB_JOURNAL(p_s_sb)->j_trans_start_time = now ;
2068 }
2069 atomic_inc(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
2070 SB_JOURNAL(p_s_sb)->j_len_alloc += nblocks ;
2071 th->t_blocks_logged = 0 ;
2072 th->t_blocks_allocated = nblocks ;
2073 th->t_super = p_s_sb ;
2074 th->t_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
2075 th->t_caller = "Unknown" ;
2076 unlock_journal(p_s_sb) ;
2077 p_s_sb->s_dirt = 1;
2078 return 0 ;
2079 }
2080
2081
2082 int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2083 return do_journal_begin_r(th, p_s_sb, nblocks, 1) ;
2084 }
2085
2086 int journal_begin(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb, unsigned long nblocks) {
2087 return do_journal_begin_r(th, p_s_sb, nblocks, 0) ;
2088 }
2089
2090 /* not used at all */
2091 int journal_prepare(struct super_block * p_s_sb, struct buffer_head *bh) {
2092 return 0 ;
2093 }
2094
2095 /*
2096 ** puts bh into the current transaction. If it was already there, reorders removes the
2097 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
2098 **
2099 ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
2100 ** transaction is committed.
2101 **
2102 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
2103 */
2104 int journal_mark_dirty(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, struct buffer_head *bh) {
2105 struct reiserfs_journal_cnode *cn = NULL;
2106 int count_already_incd = 0 ;
2107 int prepared = 0 ;
2108
2109 if (reiserfs_dont_log(th->t_super)) {
2110 mark_buffer_dirty(bh) ;
2111 return 0 ;
2112 }
2113
2114 if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
2115 reiserfs_panic(th->t_super, "journal-1577: handle trans id %d != current trans id %d\n",
2116 th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
2117 }
2118 p_s_sb->s_dirt = 1 ;
2119
2120 prepared = test_and_clear_bit(BH_JPrepared, &bh->b_state) ;
2121 /* already in this transaction, we are done */
2122 if (buffer_journaled(bh)) {
2123 return 0 ;
2124 }
2125
2126 /* this must be turned into a panic instead of a warning. We can't allow
2127 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
2128 ** could get to disk too early. NOT GOOD.
2129 */
2130 if (!prepared || buffer_locked(bh)) {
2131 printk("journal-1777: buffer %lu bad state %cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT\n", bh->b_blocknr, prepared ? ' ' : '!',
2132 buffer_locked(bh) ? ' ' : '!',
2133 buffer_dirty(bh) ? ' ' : '!',
2134 buffer_journal_dirty(bh) ? ' ' : '!') ;
2135 show_reiserfs_locks() ;
2136 }
2137 count_already_incd = clear_prepared_bits(bh) ;
2138
2139 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0) {
2140 printk("journal-1409: journal_mark_dirty returning because j_wcount was %d\n", atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount))) ;
2141 return 1 ;
2142 }
2143 /* this error means I've screwed up, and we've overflowed the transaction.
2144 ** Nothing can be done here, except make the FS readonly or panic.
2145 */
2146 if (SB_JOURNAL(p_s_sb)->j_len >= JOURNAL_TRANS_MAX) {
2147 reiserfs_panic(th->t_super, "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n", SB_JOURNAL(p_s_sb)->j_len) ;
2148 }
2149
2150 if (buffer_journal_dirty(bh)) {
2151 count_already_incd = 1 ;
2152 mark_buffer_notjournal_dirty(bh) ;
2153 }
2154
2155 if (buffer_dirty(bh)) {
2156 clear_bit(BH_Dirty, &bh->b_state) ;
2157 }
2158
2159 if (buffer_journaled(bh)) { /* must double check after getting lock */
2160 goto done ;
2161 }
2162
2163 if (SB_JOURNAL(p_s_sb)->j_len > SB_JOURNAL(p_s_sb)->j_len_alloc) {
2164 SB_JOURNAL(p_s_sb)->j_len_alloc = SB_JOURNAL(p_s_sb)->j_len + JOURNAL_PER_BALANCE_CNT ;
2165 }
2166
2167 set_bit(BH_JDirty, &bh->b_state) ;
2168
2169 /* now put this guy on the end */
2170 if (!cn) {
2171 cn = get_cnode(p_s_sb) ;
2172 if (!cn) {
2173 reiserfs_panic(p_s_sb, "get_cnode failed!\n");
2174 }
2175
2176 if (th->t_blocks_logged == th->t_blocks_allocated) {
2177 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT ;
2178 SB_JOURNAL(p_s_sb)->j_len_alloc += JOURNAL_PER_BALANCE_CNT ;
2179 }
2180 th->t_blocks_logged++ ;
2181 SB_JOURNAL(p_s_sb)->j_len++ ;
2182
2183 cn->bh = bh ;
2184 cn->blocknr = bh->b_blocknr ;
2185 cn->dev = bh->b_dev ;
2186 cn->jlist = NULL ;
2187 insert_journal_hash(SB_JOURNAL(p_s_sb)->j_hash_table, cn) ;
2188 if (!count_already_incd) {
2189 get_bh(bh) ;
2190 }
2191 }
2192 cn->next = NULL ;
2193 cn->prev = SB_JOURNAL(p_s_sb)->j_last ;
2194 cn->bh = bh ;
2195 if (SB_JOURNAL(p_s_sb)->j_last) {
2196 SB_JOURNAL(p_s_sb)->j_last->next = cn ;
2197 SB_JOURNAL(p_s_sb)->j_last = cn ;
2198 } else {
2199 SB_JOURNAL(p_s_sb)->j_first = cn ;
2200 SB_JOURNAL(p_s_sb)->j_last = cn ;
2201 }
2202 done:
2203 return 0 ;
2204 }
2205
2206 /*
2207 ** if buffer already in current transaction, do a journal_mark_dirty
2208 ** otherwise, just mark it dirty and move on. Used for writes to meta blocks
2209 ** that don't need journaling
2210 */
2211 int journal_mark_dirty_nolog(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, struct buffer_head *bh) {
2212 if (reiserfs_dont_log(th->t_super) || buffer_journaled(bh) ||
2213 buffer_journal_dirty(bh)) {
2214 return journal_mark_dirty(th, p_s_sb, bh) ;
2215 }
2216 if (get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_list_hash_table, bh->b_dev,bh->b_blocknr,bh->b_size)) {
2217 return journal_mark_dirty(th, p_s_sb, bh) ;
2218 }
2219 mark_buffer_dirty(bh) ;
2220 return 0 ;
2221 }
2222
2223 int journal_end(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2224 return do_journal_end(th, p_s_sb, nblocks, 0) ;
2225 }
2226
2227 /* removes from the current transaction, relsing and descrementing any counters.
2228 ** also files the removed buffer directly onto the clean list
2229 **
2230 ** called by journal_mark_freed when a block has been deleted
2231 **
2232 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
2233 */
2234 int remove_from_transaction(struct super_block *p_s_sb, unsigned long blocknr, int already_cleaned) {
2235 struct buffer_head *bh ;
2236 struct reiserfs_journal_cnode *cn ;
2237 int ret = 0;
2238
2239 cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_hash_table, p_s_sb->s_dev, blocknr, p_s_sb->s_blocksize) ;
2240 if (!cn || !cn->bh) {
2241 return ret ;
2242 }
2243 bh = cn->bh ;
2244 if (cn->prev) {
2245 cn->prev->next = cn->next ;
2246 }
2247 if (cn->next) {
2248 cn->next->prev = cn->prev ;
2249 }
2250 if (cn == SB_JOURNAL(p_s_sb)->j_first) {
2251 SB_JOURNAL(p_s_sb)->j_first = cn->next ;
2252 }
2253 if (cn == SB_JOURNAL(p_s_sb)->j_last) {
2254 SB_JOURNAL(p_s_sb)->j_last = cn->prev ;
2255 }
2256 remove_journal_hash(SB_JOURNAL(p_s_sb)->j_hash_table, NULL, bh, 0) ;
2257 mark_buffer_not_journaled(bh) ; /* don't log this one */
2258
2259 if (!already_cleaned) {
2260 mark_buffer_notjournal_dirty(bh) ;
2261 put_bh(bh) ;
2262 if (atomic_read(&(bh->b_count)) < 0) {
2263 printk("journal-1752: remove from trans, b_count < 0\n") ;
2264 }
2265 if (!buffer_locked(bh)) reiserfs_clean_and_file_buffer(bh) ;
2266 ret = 1 ;
2267 }
2268 SB_JOURNAL(p_s_sb)->j_len-- ;
2269 SB_JOURNAL(p_s_sb)->j_len_alloc-- ;
2270 free_cnode(p_s_sb, cn) ;
2271 return ret ;
2272 }
2273
2274 /* removes from a specific journal list hash */
2275 int remove_from_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, struct buffer_head *bh, int remove_freed) {
2276 remove_journal_hash(SB_JOURNAL(s)->j_list_hash_table, jl, bh, remove_freed) ;
2277 return 0 ;
2278 }
2279
2280 /*
2281 ** for any cnode in a journal list, it can only be dirtied of all the
2282 ** transactions that include it are commited to disk.
2283 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
2284 ** and 0 if you aren't
2285 **
2286 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
2287 ** blocks for a given transaction on disk
2288 **
2289 */
2290 static int can_dirty(struct reiserfs_journal_cnode *cn) {
2291 kdev_t dev = cn->dev ;
2292 unsigned long blocknr = cn->blocknr ;
2293 struct reiserfs_journal_cnode *cur = cn->hprev ;
2294 int can_dirty = 1 ;
2295
2296 /* first test hprev. These are all newer than cn, so any node here
2297 ** with the name block number and dev means this node can't be sent
2298 ** to disk right now.
2299 */
2300 while(cur && can_dirty) {
2301 if (cur->jlist && cur->bh && cur->blocknr && cur->dev == dev &&
2302 cur->blocknr == blocknr) {
2303 can_dirty = 0 ;
2304 }
2305 cur = cur->hprev ;
2306 }
2307 /* then test hnext. These are all older than cn. As long as they
2308 ** are committed to the log, it is safe to write cn to disk
2309 */
2310 cur = cn->hnext ;
2311 while(cur && can_dirty) {
2312 if (cur->jlist && cur->jlist->j_len > 0 &&
2313 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
2314 cur->blocknr && cur->dev == dev && cur->blocknr == blocknr) {
2315 can_dirty = 0 ;
2316 }
2317 cur = cur->hnext ;
2318 }
2319 return can_dirty ;
2320 }
2321
2322 /* syncs the commit blocks, but does not force the real buffers to disk
2323 ** will wait until the current transaction is done/commited before returning
2324 */
2325 int journal_end_sync(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2326 return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT) ;
2327 }
2328
2329 int show_reiserfs_locks(void) {
2330
2331 dump_journal_writers() ;
2332 #if 0 /* debugging code for when we are compiled static don't delete */
2333 p_s_sb = sb_entry(super_blocks.next);
2334 while (p_s_sb != sb_entry(&super_blocks)) {
2335 if (reiserfs_is_super(p_s_sb)) {
2336 printk("journal lock is %d, join lock is %d, writers %d must wait is %d\n",
2337 atomic_read(&(SB_JOURNAL(p_s_sb)->j_wlock)),
2338 atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)),
2339 atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)),
2340 SB_JOURNAL(p_s_sb)->j_must_wait) ;
2341 printk("used cnodes %d, free cnodes %d\n", SB_JOURNAL(p_s_sb)->j_cnode_used, SB_JOURNAL(p_s_sb)->j_cnode_free) ;
2342 }
2343 p_s_sb = sb_entry(p_s_sb->s_list.next);
2344 }
2345 #endif
2346 return 0 ;
2347 }
2348
2349 /*
2350 ** used to get memory back from async commits that are floating around
2351 ** and to reclaim any blocks deleted but unusable because their commits
2352 ** haven't hit disk yet. called from bitmap.c
2353 **
2354 ** if it starts flushing things, it ors SCHEDULE_OCCURRED into repeat.
2355 ** note, this is just if schedule has a chance of occuring. I need to
2356 ** change flush_commit_lists to have a repeat parameter too.
2357 **
2358 */
2359 void flush_async_commits(struct super_block *p_s_sb) {
2360 int i ;
2361
2362 for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
2363 if (i != SB_JOURNAL_LIST_INDEX(p_s_sb)) {
2364 flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + i, 1) ;
2365 }
2366 }
2367 }
2368
2369 /*
2370 ** flushes any old transactions to disk
2371 ** ends the current transaction if it is too old
2372 **
2373 ** also calls flush_journal_list with old_only == 1, which allows me to reclaim
2374 ** memory and such from the journal lists whose real blocks are all on disk.
2375 **
2376 ** called by sync_dev_journal from buffer.c
2377 */
2378 int flush_old_commits(struct super_block *p_s_sb, int immediate) {
2379 int i ;
2380 int count = 0;
2381 int start ;
2382 time_t now ;
2383 struct reiserfs_transaction_handle th ;
2384
2385 start = SB_JOURNAL_LIST_INDEX(p_s_sb) ;
2386 now = CURRENT_TIME ;
2387
2388 /* safety check so we don't flush while we are replaying the log during mount */
2389 if (SB_JOURNAL_LIST_INDEX(p_s_sb) < 0) {
2390 return 0 ;
2391 }
2392 /* starting with oldest, loop until we get to the start */
2393 i = (SB_JOURNAL_LIST_INDEX(p_s_sb) + 1) % JOURNAL_LIST_COUNT ;
2394 while(i != start) {
2395 if (SB_JOURNAL_LIST(p_s_sb)[i].j_len > 0 && ((now - SB_JOURNAL_LIST(p_s_sb)[i].j_timestamp) > JOURNAL_MAX_COMMIT_AGE ||
2396 immediate)) {
2397 /* we have to check again to be sure the current transaction did not change */
2398 if (i != SB_JOURNAL_LIST_INDEX(p_s_sb)) {
2399 flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + i, 1) ;
2400 }
2401 }
2402 i = (i + 1) % JOURNAL_LIST_COUNT ;
2403 count++ ;
2404 }
2405 /* now, check the current transaction. If there are no writers, and it is too old, finish it, and
2406 ** force the commit blocks to disk
2407 */
2408 if (!immediate && atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0 &&
2409 SB_JOURNAL(p_s_sb)->j_trans_start_time > 0 &&
2410 SB_JOURNAL(p_s_sb)->j_len > 0 &&
2411 (now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE) {
2412 journal_join(&th, p_s_sb, 1) ;
2413 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
2414 journal_mark_dirty(&th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
2415 do_journal_end(&th, p_s_sb,1, COMMIT_NOW) ;
2416 } else if (immediate) { /* belongs above, but I wanted this to be very explicit as a special case. If they say to
2417 flush, we must be sure old transactions hit the disk too. */
2418 journal_join(&th, p_s_sb, 1) ;
2419 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
2420 journal_mark_dirty(&th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
2421 do_journal_end(&th, p_s_sb,1, COMMIT_NOW | WAIT) ;
2422 }
2423 reiserfs_journal_kupdate(p_s_sb) ;
2424 return 0 ;
2425 }
2426
2427 /*
2428 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
2429 **
2430 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
2431 ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
2432 ** flushes the commit list and returns 0.
2433 **
2434 ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
2435 **
2436 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
2437 */
2438 static int check_journal_end(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,
2439 unsigned long nblocks, int flags) {
2440
2441 time_t now ;
2442 int flush = flags & FLUSH_ALL ;
2443 int commit_now = flags & COMMIT_NOW ;
2444 int wait_on_commit = flags & WAIT ;
2445
2446 if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
2447 reiserfs_panic(th->t_super, "journal-1577: handle trans id %d != current trans id %d\n",
2448 th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
2449 }
2450
2451 SB_JOURNAL(p_s_sb)->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged) ;
2452 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
2453 atomic_dec(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
2454 }
2455
2456 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
2457 ** will be dealt with by next transaction that actually writes something, but should be taken
2458 ** care of in this trans
2459 */
2460 if (SB_JOURNAL(p_s_sb)->j_len == 0) {
2461 int wcount = atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
2462 unlock_journal(p_s_sb) ;
2463 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)) > 0 && wcount <= 0) {
2464 atomic_dec(&(SB_JOURNAL(p_s_sb)->j_jlock)) ;
2465 wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
2466 }
2467 return 0 ;
2468 }
2469 /* if wcount > 0, and we are called to with flush or commit_now,
2470 ** we wait on j_join_wait. We will wake up when the last writer has
2471 ** finished the transaction, and started it on its way to the disk.
2472 ** Then, we flush the commit or journal list, and just return 0
2473 ** because the rest of journal end was already done for this transaction.
2474 */
2475 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) {
2476 if (flush || commit_now) {
2477 int orig_jindex = SB_JOURNAL_LIST_INDEX(p_s_sb) ;
2478 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
2479 if (flush) {
2480 SB_JOURNAL(p_s_sb)->j_next_full_flush = 1 ;
2481 }
2482 unlock_journal(p_s_sb) ;
2483 /* sleep while the current transaction is still j_jlocked */
2484 while(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock)) &&
2485 SB_JOURNAL(p_s_sb)->j_trans_id == th->t_trans_id) {
2486 sleep_on(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
2487 }
2488 if (commit_now) {
2489 if (wait_on_commit) {
2490 flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + orig_jindex, 1) ;
2491 } else {
2492 commit_flush_async(p_s_sb, orig_jindex) ;
2493 }
2494 }
2495 return 0 ;
2496 }
2497 unlock_journal(p_s_sb) ;
2498 return 0 ;
2499 }
2500
2501 /* deal with old transactions where we are the last writers */
2502 now = CURRENT_TIME ;
2503 if ((now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > JOURNAL_MAX_TRANS_AGE) {
2504 commit_now = 1 ;
2505 SB_JOURNAL(p_s_sb)->j_next_async_flush = 1 ;
2506 }
2507 /* don't batch when someone is waiting on j_join_wait */
2508 /* don't batch when syncing the commit or flushing the whole trans */
2509 if (!(SB_JOURNAL(p_s_sb)->j_must_wait > 0) && !(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock))) && !flush && !commit_now &&
2510 (SB_JOURNAL(p_s_sb)->j_len < JOURNAL_MAX_BATCH) &&
2511 SB_JOURNAL(p_s_sb)->j_len_alloc < JOURNAL_MAX_BATCH && SB_JOURNAL(p_s_sb)->j_cnode_free > (JOURNAL_TRANS_MAX * 3)) {
2512 SB_JOURNAL(p_s_sb)->j_bcount++ ;
2513 unlock_journal(p_s_sb) ;
2514 return 0 ;
2515 }
2516
2517 if (SB_JOURNAL(p_s_sb)->j_start > JOURNAL_BLOCK_COUNT) {
2518 reiserfs_panic(p_s_sb, "journal-003: journal_end: j_start (%d) is too high\n", SB_JOURNAL(p_s_sb)->j_start) ;
2519 }
2520 return 1 ;
2521 }
2522
2523 /*
2524 ** Does all the work that makes deleting blocks safe.
2525 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
2526 **
2527 ** otherwise:
2528 ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
2529 ** before this transaction has finished.
2530 **
2531 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
2532 ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
2533 ** the block can't be reallocated yet.
2534 **
2535 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
2536 */
2537 int journal_mark_freed(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long blocknr) {
2538 struct reiserfs_journal_cnode *cn = NULL ;
2539 struct buffer_head *bh = NULL ;
2540 struct reiserfs_list_bitmap *jb = NULL ;
2541 int cleaned = 0 ;
2542
2543 if (reiserfs_dont_log(th->t_super)) {
2544 bh = get_hash_table(p_s_sb->s_dev, blocknr, p_s_sb->s_blocksize) ;
2545 if (bh && buffer_dirty (bh)) {
2546 printk ("journal_mark_freed(dont_log): dirty buffer on hash list: %lx %ld\n", bh->b_state, blocknr);
2547 BUG ();
2548 }
2549 brelse (bh);
2550 return 0 ;
2551 }
2552 bh = get_hash_table(p_s_sb->s_dev, blocknr, p_s_sb->s_blocksize) ;
2553 /* if it is journal new, we just remove it from this transaction */
2554 if (bh && buffer_journal_new(bh)) {
2555 mark_buffer_notjournal_new(bh) ;
2556 clear_prepared_bits(bh) ;
2557 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
2558 } else {
2559 /* set the bit for this block in the journal bitmap for this transaction */
2560 jb = SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_list_bitmap ;
2561 if (!jb) {
2562 reiserfs_panic(p_s_sb, "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n") ;
2563 }
2564 set_bit_in_list_bitmap(p_s_sb, blocknr, jb) ;
2565
2566 /* Note, the entire while loop is not allowed to schedule. */
2567
2568 if (bh) {
2569 clear_prepared_bits(bh) ;
2570 }
2571 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
2572
2573 /* find all older transactions with this block, make sure they don't try to write it out */
2574 cn = get_journal_hash_dev(SB_JOURNAL(p_s_sb)->j_list_hash_table, p_s_sb->s_dev, blocknr, p_s_sb->s_blocksize) ;
2575 while (cn) {
2576 if (p_s_sb->s_dev == cn->dev && blocknr == cn->blocknr) {
2577 set_bit(BLOCK_FREED, &cn->state) ;
2578 if (cn->bh) {
2579 if (!cleaned) {
2580 /* remove_from_transaction will brelse the buffer if it was
2581 ** in the current trans
2582 */
2583 mark_buffer_notjournal_dirty(cn->bh) ;
2584 cleaned = 1 ;
2585 put_bh(cn->bh) ;
2586 if (atomic_read(&(cn->bh->b_count)) < 0) {
2587 printk("journal-2138: cn->bh->b_count < 0\n") ;
2588 }
2589 }
2590 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
2591 atomic_dec(&(cn->jlist->j_nonzerolen)) ;
2592 }
2593 cn->bh = NULL ;
2594 }
2595 }
2596 cn = cn->hnext ;
2597 }
2598 }
2599
2600 if (bh) {
2601 reiserfs_clean_and_file_buffer(bh) ;
2602 put_bh(bh) ; /* get_hash grabs the buffer */
2603 if (atomic_read(&(bh->b_count)) < 0) {
2604 printk("journal-2165: bh->b_count < 0\n") ;
2605 }
2606 }
2607 return 0 ;
2608 }
2609
2610 void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb,
2611 struct buffer_head *bh) {
2612 if (reiserfs_dont_log (p_s_sb))
2613 return;
2614
2615 if (!bh) {
2616 return ;
2617 }
2618 clear_bit(BH_JPrepared, &bh->b_state) ;
2619 }
2620
2621 extern struct tree_balance *cur_tb ;
2622 /*
2623 ** before we can change a metadata block, we have to make sure it won't
2624 ** be written to disk while we are altering it. So, we must:
2625 ** clean it
2626 ** wait on it.
2627 **
2628 */
2629 void reiserfs_prepare_for_journal(struct super_block *p_s_sb,
2630 struct buffer_head *bh, int wait) {
2631 int retry_count = 0 ;
2632
2633 if (reiserfs_dont_log (p_s_sb))
2634 return;
2635
2636 while(!test_bit(BH_JPrepared, &bh->b_state) ||
2637 (wait && buffer_locked(bh))) {
2638 if (buffer_journaled(bh)) {
2639 set_bit(BH_JPrepared, &bh->b_state) ;
2640 return ;
2641 }
2642 set_bit(BH_JPrepared, &bh->b_state) ;
2643 if (wait) {
2644 #ifdef CONFIG_REISERFS_CHECK
2645 if (buffer_locked(bh) && cur_tb != NULL) {
2646 printk("reiserfs_prepare_for_journal, waiting while do_balance was running\n") ;
2647 BUG() ;
2648 }
2649 #endif
2650 wait_on_buffer(bh) ;
2651 }
2652 retry_count++ ;
2653 }
2654 }
2655
2656 /*
2657 ** long and ugly. If flush, will not return until all commit
2658 ** blocks and all real buffers in the trans are on disk.
2659 ** If no_async, won't return until all commit blocks are on disk.
2660 **
2661 ** keep reading, there are comments as you go along
2662 */
2663 static int do_journal_end(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb, unsigned long nblocks,
2664 int flags) {
2665 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
2666 struct reiserfs_journal_cnode *last_cn = NULL;
2667 struct reiserfs_journal_desc *desc ;
2668 struct reiserfs_journal_commit *commit ;
2669 struct buffer_head *c_bh ; /* commit bh */
2670 struct buffer_head *d_bh ; /* desc bh */
2671 int cur_write_start = 0 ; /* start index of current log write */
2672 int cur_blocks_left = 0 ; /* number of journal blocks left to write */
2673 int old_start ;
2674 int i ;
2675 int jindex ;
2676 int orig_jindex ;
2677 int flush = flags & FLUSH_ALL ;
2678 int commit_now = flags & COMMIT_NOW ;
2679 int wait_on_commit = flags & WAIT ;
2680 struct reiserfs_super_block *rs ;
2681
2682 if (reiserfs_dont_log(th->t_super)) {
2683 return 0 ;
2684 }
2685
2686 lock_journal(p_s_sb) ;
2687 if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
2688 flags |= FLUSH_ALL ;
2689 flush = 1 ;
2690 }
2691 if (SB_JOURNAL(p_s_sb)->j_next_async_flush) {
2692 flags |= COMMIT_NOW ;
2693 commit_now = 1 ;
2694 }
2695
2696 /* check_journal_end locks the journal, and unlocks if it does not return 1
2697 ** it tells us if we should continue with the journal_end, or just return
2698 */
2699 if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
2700 return 0 ;
2701 }
2702
2703 /* check_journal_end might set these, check again */
2704 if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
2705 flush = 1 ;
2706 }
2707 if (SB_JOURNAL(p_s_sb)->j_next_async_flush) {
2708 commit_now = 1 ;
2709 }
2710 /*
2711 ** j must wait means we have to flush the log blocks, and the real blocks for
2712 ** this transaction
2713 */
2714 if (SB_JOURNAL(p_s_sb)->j_must_wait > 0) {
2715 flush = 1 ;
2716 }
2717
2718 #ifdef REISERFS_PREALLOCATE
2719 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
2720 * the transaction */
2721 #endif
2722
2723 rs = SB_DISK_SUPER_BLOCK(p_s_sb) ;
2724 /* setup description block */
2725 d_bh = getblk(p_s_sb->s_dev, reiserfs_get_journal_block(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start, p_s_sb->s_blocksize) ;
2726 mark_buffer_uptodate(d_bh, 1) ;
2727 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data ;
2728 memset(desc, 0, sizeof(struct reiserfs_journal_desc)) ;
2729 memcpy(desc->j_magic, JOURNAL_DESC_MAGIC, 8) ;
2730 desc->j_trans_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_trans_id) ;
2731
2732 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
2733 c_bh = getblk(p_s_sb->s_dev, reiserfs_get_journal_block(p_s_sb) +
2734 ((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 1) % JOURNAL_BLOCK_COUNT),
2735 p_s_sb->s_blocksize) ;
2736 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
2737 memset(commit, 0, sizeof(struct reiserfs_journal_commit)) ;
2738 commit->j_trans_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_trans_id) ;
2739 mark_buffer_uptodate(c_bh, 1) ;
2740
2741 /* init this journal list */
2742 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_older_commits_done), 0) ;
2743 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
2744 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_timestamp = SB_JOURNAL(p_s_sb)->j_trans_start_time ;
2745 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_bh = c_bh ;
2746 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_start = SB_JOURNAL(p_s_sb)->j_start ;
2747 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_len = SB_JOURNAL(p_s_sb)->j_len ;
2748 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_nonzerolen), SB_JOURNAL(p_s_sb)->j_len) ;
2749 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_left), SB_JOURNAL(p_s_sb)->j_len + 2);
2750 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_realblock = NULL ;
2751 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_flushing), 1) ;
2752 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_flushing), 1) ;
2753
2754 /* which is faster, locking/unlocking at the start and end of the for
2755 ** or locking once per iteration around the insert_journal_hash?
2756 ** eitherway, we are write locking insert_journal_hash. The ENTIRE FOR
2757 ** LOOP MUST not cause schedule to occur.
2758 */
2759
2760 /* for each real block, add it to the journal list hash,
2761 ** copy into real block index array in the commit or desc block
2762 */
2763 for (i = 0, cn = SB_JOURNAL(p_s_sb)->j_first ; cn ; cn = cn->next, i++) {
2764 if (test_bit(BH_JDirty, &cn->bh->b_state) ) {
2765 jl_cn = get_cnode(p_s_sb) ;
2766 if (!jl_cn) {
2767 reiserfs_panic(p_s_sb, "journal-1676, get_cnode returned NULL\n") ;
2768 }
2769 if (i == 0) {
2770 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_realblock = jl_cn ;
2771 }
2772 jl_cn->prev = last_cn ;
2773 jl_cn->next = NULL ;
2774 if (last_cn) {
2775 last_cn->next = jl_cn ;
2776 }
2777 last_cn = jl_cn ;
2778 if (cn->bh->b_blocknr >= reiserfs_get_journal_block(p_s_sb) &&
2779 cn->bh->b_blocknr < (reiserfs_get_journal_block(p_s_sb) + JOURNAL_BLOCK_COUNT)) {
2780 reiserfs_panic(p_s_sb, "journal-2332: Trying to log block %lu, which is a log block\n", cn->bh->b_blocknr) ;
2781 }
2782 jl_cn->blocknr = cn->bh->b_blocknr ;
2783 jl_cn->state = 0 ;
2784 jl_cn->dev = cn->bh->b_dev ;
2785 jl_cn->bh = cn->bh ;
2786 jl_cn->jlist = SB_JOURNAL_LIST(p_s_sb) + SB_JOURNAL_LIST_INDEX(p_s_sb) ;
2787 insert_journal_hash(SB_JOURNAL(p_s_sb)->j_list_hash_table, jl_cn) ;
2788 if (i < JOURNAL_TRANS_HALF) {
2789 desc->j_realblock[i] = cpu_to_le32(cn->bh->b_blocknr) ;
2790 } else {
2791 commit->j_realblock[i - JOURNAL_TRANS_HALF] = cpu_to_le32(cn->bh->b_blocknr) ;
2792 }
2793 } else {
2794 i-- ;
2795 }
2796 }
2797
2798 desc->j_len = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_len) ;
2799 desc->j_mount_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_mount_id) ;
2800 desc->j_trans_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_trans_id) ;
2801 commit->j_len = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_len) ;
2802
2803 /* special check in case all buffers in the journal were marked for not logging */
2804 if (SB_JOURNAL(p_s_sb)->j_len == 0) {
2805 brelse(d_bh) ;
2806 brelse(c_bh) ;
2807 unlock_journal(p_s_sb) ;
2808 printk("journal-2020: do_journal_end: BAD desc->j_len is ZERO\n") ;
2809 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
2810 wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
2811 return 0 ;
2812 }
2813
2814 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
2815 cur_write_start = SB_JOURNAL(p_s_sb)->j_start ;
2816 cur_blocks_left = SB_JOURNAL(p_s_sb)->j_len ;
2817 cn = SB_JOURNAL(p_s_sb)->j_first ;
2818 jindex = 1 ; /* start at one so we don't get the desc again */
2819 while(cur_blocks_left > 0) {
2820 /* copy all the real blocks into log area. dirty log blocks */
2821 if (test_bit(BH_JDirty, &cn->bh->b_state)) {
2822 struct buffer_head *tmp_bh ;
2823 tmp_bh = getblk(p_s_sb->s_dev, reiserfs_get_journal_block(p_s_sb) +
2824 ((cur_write_start + jindex) % JOURNAL_BLOCK_COUNT),
2825 p_s_sb->s_blocksize) ;
2826 mark_buffer_uptodate(tmp_bh, 1) ;
2827 memcpy(tmp_bh->b_data, cn->bh->b_data, cn->bh->b_size) ;
2828 jindex++ ;
2829 } else {
2830 /* JDirty cleared sometime during transaction. don't log this one */
2831 printk("journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!\n") ;
2832 }
2833 cn = cn->next ;
2834 cur_blocks_left-- ;
2835 }
2836
2837 /* we are done with both the c_bh and d_bh, but
2838 ** c_bh must be written after all other commit blocks,
2839 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
2840 */
2841
2842 /* now loop through and mark all buffers from this transaction as JDirty_wait
2843 ** clear the JDirty bit, clear BH_JNew too.
2844 ** if they weren't JDirty, they weren't logged, just relse them and move on
2845 */
2846 cn = SB_JOURNAL(p_s_sb)->j_first ;
2847 while(cn) {
2848 clear_bit(BH_JNew, &(cn->bh->b_state)) ;
2849 if (test_bit(BH_JDirty, &(cn->bh->b_state))) {
2850 set_bit(BH_JDirty_wait, &(cn->bh->b_state)) ;
2851 clear_bit(BH_JDirty, &(cn->bh->b_state)) ;
2852 } else {
2853 brelse(cn->bh) ;
2854 }
2855 next = cn->next ;
2856 free_cnode(p_s_sb, cn) ;
2857 cn = next ;
2858 }
2859
2860 /* unlock the journal list for committing and flushing */
2861 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_flushing), 0) ;
2862 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_flushing), 0) ;
2863
2864 orig_jindex = SB_JOURNAL_LIST_INDEX(p_s_sb) ;
2865 jindex = (SB_JOURNAL_LIST_INDEX(p_s_sb) + 1) % JOURNAL_LIST_COUNT ;
2866 SB_JOURNAL_LIST_INDEX(p_s_sb) = jindex ;
2867
2868 /* write any buffers that must hit disk before this commit is done */
2869 fsync_inode_buffers(&(SB_JOURNAL(p_s_sb)->j_dummy_inode)) ;
2870
2871 /* honor the flush and async wishes from the caller */
2872 if (flush) {
2873
2874 flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + orig_jindex, 1) ;
2875 flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + orig_jindex , 1) ;
2876 } else if (commit_now) {
2877 if (wait_on_commit) {
2878 flush_commit_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + orig_jindex, 1) ;
2879 } else {
2880 commit_flush_async(p_s_sb, orig_jindex) ;
2881 }
2882 }
2883
2884 /* reset journal values for the next transaction */
2885 old_start = SB_JOURNAL(p_s_sb)->j_start ;
2886 SB_JOURNAL(p_s_sb)->j_start = (SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 2) % JOURNAL_BLOCK_COUNT;
2887 atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
2888 SB_JOURNAL(p_s_sb)->j_bcount = 0 ;
2889 SB_JOURNAL(p_s_sb)->j_last = NULL ;
2890 SB_JOURNAL(p_s_sb)->j_first = NULL ;
2891 SB_JOURNAL(p_s_sb)->j_len = 0 ;
2892 SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;
2893 SB_JOURNAL(p_s_sb)->j_trans_id++ ;
2894 SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
2895 SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
2896 SB_JOURNAL(p_s_sb)->j_next_full_flush = 0 ;
2897 SB_JOURNAL(p_s_sb)->j_next_async_flush = 0 ;
2898 init_journal_hash(p_s_sb) ;
2899
2900 /* if the next transaction has any chance of wrapping, flush
2901 ** transactions that might get overwritten. If any journal lists are very
2902 ** old flush them as well.
2903 */
2904 for (i = 0 ; i < JOURNAL_LIST_COUNT ; i++) {
2905 jindex = i ;
2906 if (SB_JOURNAL_LIST(p_s_sb)[jindex].j_len > 0 && SB_JOURNAL(p_s_sb)->j_start <= SB_JOURNAL_LIST(p_s_sb)[jindex].j_start) {
2907 if ((SB_JOURNAL(p_s_sb)->j_start + JOURNAL_TRANS_MAX + 1) >= SB_JOURNAL_LIST(p_s_sb)[jindex].j_start) {
2908 flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1) ;
2909 }
2910 } else if (SB_JOURNAL_LIST(p_s_sb)[jindex].j_len > 0 &&
2911 (SB_JOURNAL(p_s_sb)->j_start + JOURNAL_TRANS_MAX + 1) > JOURNAL_BLOCK_COUNT) {
2912 if (((SB_JOURNAL(p_s_sb)->j_start + JOURNAL_TRANS_MAX + 1) % JOURNAL_BLOCK_COUNT) >=
2913 SB_JOURNAL_LIST(p_s_sb)[jindex].j_start) {
2914 flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1 ) ;
2915 }
2916 }
2917 /* this check should always be run, to send old lists to disk */
2918 if (SB_JOURNAL_LIST(p_s_sb)[jindex].j_len > 0 &&
2919 SB_JOURNAL_LIST(p_s_sb)[jindex].j_timestamp <
2920 (CURRENT_TIME - (JOURNAL_MAX_TRANS_AGE * 4))) {
2921 flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + jindex, 1 ) ;
2922 }
2923 }
2924
2925 /* if the next journal_list is still in use, flush it */
2926 if (SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_len != 0) {
2927 flush_journal_list(p_s_sb, SB_JOURNAL_LIST(p_s_sb) + SB_JOURNAL_LIST_INDEX(p_s_sb), 1) ;
2928 }
2929
2930 /* we don't want anyone flushing the new transaction's list */
2931 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_commit_flushing), 1) ;
2932 atomic_set(&(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_flushing), 1) ;
2933 SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_list_bitmap = get_list_bitmap(p_s_sb, SB_JOURNAL_LIST(p_s_sb) +
2934 SB_JOURNAL_LIST_INDEX(p_s_sb)) ;
2935
2936 if (!(SB_JOURNAL_LIST(p_s_sb)[SB_JOURNAL_LIST_INDEX(p_s_sb)].j_list_bitmap)) {
2937 reiserfs_panic(p_s_sb, "journal-1996: do_journal_end, could not get a list bitmap\n") ;
2938 }
2939 unlock_journal(p_s_sb) ;
2940 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
2941 /* wake up any body waiting to join. */
2942 wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
2943 return 0 ;
2944 }
2945
2946
2947
2948