2 ** Write ahead logging implementation copyright Chris Mason 2000
4 ** The background commits make this code very interelated, and
5 ** overly complex. I need to rethink things a bit....The major players:
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.
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.
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.
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
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
33 ** -- Note, if you call this as an immediate flush from
34 ** from within kupdate, it will ignore the immediate flag
37 #include <linux/config.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 #include <linux/time.h>
42 #include <asm/semaphore.h>
44 #include <linux/vmalloc.h>
45 #include <linux/reiserfs_fs.h>
47 #include <linux/kernel.h>
48 #include <linux/errno.h>
49 #include <linux/fcntl.h>
50 #include <linux/stat.h>
51 #include <linux/string.h>
52 #include <linux/smp_lock.h>
53 #include <linux/suspend.h>
54 #include <linux/buffer_head.h>
55 #include <linux/workqueue.h>
56 #include <linux/writeback.h>
57 #include <linux/blkdev.h>
60 /* gets a struct reiserfs_journal_list * from a list head */
61 #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
63 #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
66 /* the number of mounted filesystems. This is used to decide when to
67 ** start and kill the commit workqueue
69 static int reiserfs_mounted_fs_count;
71 static struct workqueue_struct *commit_wq;
73 #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
75 #define BUFNR 64 /*read ahead */
77 /* cnode stat bits. Move these into reiserfs_fs.h */
79 #define BLOCK_FREED 2 /* this block was freed, and can't be written. */
80 #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
82 #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
83 #define BLOCK_DIRTIED 5
86 /* journal list state bits */
87 #define LIST_TOUCHED 1
89 #define LIST_COMMIT_PENDING 4 /* someone will commit this list */
91 /* flags for do_journal_end */
92 #define FLUSH_ALL 1 /* flush commit and real blocks */
93 #define COMMIT_NOW 2 /* end and commit this transaction */
94 #define WAIT 4 /* wait for the log blocks to hit the disk*/
96 /* state bits for the journal */
97 #define WRITERS_BLOCKED 1 /* set when new writers not allowed */
98 #define WRITERS_QUEUED 2 /* set when log is full due to too many
102 static int do_journal_end(struct reiserfs_transaction_handle *,struct super_block *,unsigned long nblocks,int flags) ;
103 static int flush_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
104 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
105 static int can_dirty(struct reiserfs_journal_cnode *cn) ;
106 static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks);
107 static int release_journal_dev( struct super_block *super,
108 struct reiserfs_journal *journal );
109 static int dirty_one_transaction(struct super_block *s,
110 struct reiserfs_journal_list *jl);
111 static void flush_async_commits(void *p);
113 static void init_journal_hash(struct super_block *p_s_sb) {
114 memset(SB_JOURNAL(p_s_sb)->j_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
118 ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
119 ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
122 static int reiserfs_clean_and_file_buffer(struct buffer_head *bh) {
124 clear_buffer_dirty(bh);
125 clear_bit(BH_JTest, &bh->b_state);
130 static struct reiserfs_bitmap_node *
131 allocate_bitmap_node(struct super_block *p_s_sb) {
132 struct reiserfs_bitmap_node *bn ;
135 bn = reiserfs_kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS, p_s_sb) ;
139 bn->data = reiserfs_kmalloc(p_s_sb->s_blocksize, GFP_NOFS, p_s_sb) ;
141 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
145 memset(bn->data, 0, p_s_sb->s_blocksize) ;
146 INIT_LIST_HEAD(&bn->list) ;
150 static struct reiserfs_bitmap_node *
151 get_bitmap_node(struct super_block *p_s_sb) {
152 struct reiserfs_bitmap_node *bn = NULL;
153 struct list_head *entry = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
155 SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes++ ;
158 if(entry != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
159 bn = list_entry(entry, struct reiserfs_bitmap_node, list) ;
161 memset(bn->data, 0, p_s_sb->s_blocksize) ;
162 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
165 bn = allocate_bitmap_node(p_s_sb) ;
172 static inline void free_bitmap_node(struct super_block *p_s_sb,
173 struct reiserfs_bitmap_node *bn) {
174 SB_JOURNAL(p_s_sb)->j_used_bitmap_nodes-- ;
175 if (SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
176 reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
177 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
179 list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
180 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
184 static void allocate_bitmap_nodes(struct super_block *p_s_sb) {
186 struct reiserfs_bitmap_node *bn = NULL ;
187 for (i = 0 ; i < REISERFS_MIN_BITMAP_NODES ; i++) {
188 bn = allocate_bitmap_node(p_s_sb) ;
190 list_add(&bn->list, &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
191 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes++ ;
193 break ; // this is ok, we'll try again when more are needed
198 static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
199 struct reiserfs_list_bitmap *jb) {
200 int bmap_nr = block / (p_s_sb->s_blocksize << 3) ;
201 int bit_nr = block % (p_s_sb->s_blocksize << 3) ;
203 if (!jb->bitmaps[bmap_nr]) {
204 jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb) ;
206 set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data) ;
210 static void cleanup_bitmap_list(struct super_block *p_s_sb,
211 struct reiserfs_list_bitmap *jb) {
213 if (jb->bitmaps == NULL)
216 for (i = 0 ; i < SB_BMAP_NR(p_s_sb) ; i++) {
217 if (jb->bitmaps[i]) {
218 free_bitmap_node(p_s_sb, jb->bitmaps[i]) ;
219 jb->bitmaps[i] = NULL ;
225 ** only call this on FS unmount.
227 static int free_list_bitmaps(struct super_block *p_s_sb,
228 struct reiserfs_list_bitmap *jb_array) {
230 struct reiserfs_list_bitmap *jb ;
231 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
233 jb->journal_list = NULL ;
234 cleanup_bitmap_list(p_s_sb, jb) ;
241 static int free_bitmap_nodes(struct super_block *p_s_sb) {
242 struct list_head *next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
243 struct reiserfs_bitmap_node *bn ;
245 while(next != &SB_JOURNAL(p_s_sb)->j_bitmap_nodes) {
246 bn = list_entry(next, struct reiserfs_bitmap_node, list) ;
248 reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
249 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
250 next = SB_JOURNAL(p_s_sb)->j_bitmap_nodes.next ;
251 SB_JOURNAL(p_s_sb)->j_free_bitmap_nodes-- ;
258 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
259 ** jb_array is the array to be filled in.
261 int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
262 struct reiserfs_list_bitmap *jb_array,
266 struct reiserfs_list_bitmap *jb ;
267 int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *) ;
269 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
271 jb->journal_list = NULL ;
272 jb->bitmaps = vmalloc( mem ) ;
274 reiserfs_warning(p_s_sb, "clm-2000, unable to allocate bitmaps for journal lists") ;
278 memset(jb->bitmaps, 0, mem) ;
281 free_list_bitmaps(p_s_sb, jb_array) ;
288 ** find an available list bitmap. If you can't find one, flush a commit list
291 static struct reiserfs_list_bitmap *
292 get_list_bitmap(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
294 struct reiserfs_list_bitmap *jb = NULL ;
296 for (j = 0 ; j < (JOURNAL_NUM_BITMAPS * 3) ; j++) {
297 i = SB_JOURNAL(p_s_sb)->j_list_bitmap_index ;
298 SB_JOURNAL(p_s_sb)->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS ;
299 jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
300 if (SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
301 flush_commit_list(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list, 1) ;
302 if (!SB_JOURNAL(p_s_sb)->j_list_bitmap[i].journal_list) {
309 if (jb->journal_list) { /* double check to make sure if flushed correctly */
312 jb->journal_list = jl ;
317 ** allocates a new chunk of X nodes, and links them all together as a list.
318 ** Uses the cnode->next and cnode->prev pointers
319 ** returns NULL on failure
321 static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes) {
322 struct reiserfs_journal_cnode *head ;
324 if (num_cnodes <= 0) {
327 head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
331 memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
332 head[0].prev = NULL ;
333 head[0].next = head + 1 ;
334 for (i = 1 ; i < num_cnodes; i++) {
335 head[i].prev = head + (i - 1) ;
336 head[i].next = head + (i + 1) ; /* if last one, overwrite it after the if */
338 head[num_cnodes -1].next = NULL ;
343 ** pulls a cnode off the free list, or returns NULL on failure
345 static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb) {
346 struct reiserfs_journal_cnode *cn ;
348 reiserfs_check_lock_depth(p_s_sb, "get_cnode") ;
350 if (SB_JOURNAL(p_s_sb)->j_cnode_free <= 0) {
353 SB_JOURNAL(p_s_sb)->j_cnode_used++ ;
354 SB_JOURNAL(p_s_sb)->j_cnode_free-- ;
355 cn = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
360 cn->next->prev = NULL ;
362 SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn->next ;
363 memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ;
368 ** returns a cnode to the free list
370 static void free_cnode(struct super_block *p_s_sb, struct reiserfs_journal_cnode *cn) {
372 reiserfs_check_lock_depth(p_s_sb, "free_cnode") ;
374 SB_JOURNAL(p_s_sb)->j_cnode_used-- ;
375 SB_JOURNAL(p_s_sb)->j_cnode_free++ ;
376 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
377 cn->next = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
378 if (SB_JOURNAL(p_s_sb)->j_cnode_free_list) {
379 SB_JOURNAL(p_s_sb)->j_cnode_free_list->prev = cn ;
381 cn->prev = NULL ; /* not needed with the memset, but I might kill the memset, and forget to do this */
382 SB_JOURNAL(p_s_sb)->j_cnode_free_list = cn ;
385 static int clear_prepared_bits(struct buffer_head *bh) {
386 clear_bit(BH_JPrepared, &bh->b_state) ;
387 clear_bit(BH_JRestore_dirty, &bh->b_state) ;
391 /* buffer is in current transaction */
392 inline int buffer_journaled(const struct buffer_head *bh) {
394 return test_bit(BH_JDirty, &bh->b_state) ;
399 /* disk block was taken off free list before being in a finished transation, or written to disk
400 ** journal_new blocks can be reused immediately, for any purpose
402 inline int buffer_journal_new(const struct buffer_head *bh) {
404 return test_bit(BH_JNew, &bh->b_state) ;
409 inline int mark_buffer_journal_new(struct buffer_head *bh) {
411 set_bit(BH_JNew, &bh->b_state) ;
416 inline int mark_buffer_not_journaled(struct buffer_head *bh) {
418 clear_bit(BH_JDirty, &bh->b_state) ;
422 /* utility function to force a BUG if it is called without the big
423 ** kernel lock held. caller is the string printed just before calling BUG()
425 void reiserfs_check_lock_depth(struct super_block *sb, char *caller) {
427 if (current->lock_depth < 0) {
428 reiserfs_panic (sb, "%s called without kernel lock held", caller) ;
435 /* return a cnode with same dev, block number and size in table, or null if not found */
436 static inline struct reiserfs_journal_cnode *
437 get_journal_hash_dev(struct super_block *sb,
438 struct reiserfs_journal_cnode **table,
441 struct reiserfs_journal_cnode *cn ;
442 cn = journal_hash(table, sb, bl) ;
444 if (cn->blocknr == bl && cn->sb == sb)
448 return (struct reiserfs_journal_cnode *)0 ;
451 /* returns a cnode with same size, block number and dev as bh in the current transaction hash. NULL if not found */
452 static inline struct reiserfs_journal_cnode *get_journal_hash(struct super_block *p_s_sb, struct buffer_head *bh) {
453 struct reiserfs_journal_cnode *cn ;
455 cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, bh->b_blocknr);
458 return (struct reiserfs_journal_cnode *)0 ;
464 ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
465 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
466 ** being overwritten by a replay after crashing.
468 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
469 ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
470 ** sure you never write the block without logging it.
472 ** next_zero_bit is a suggestion about the next block to try for find_forward.
473 ** when bl is rejected because it is set in a journal list bitmap, we search
474 ** for the next zero bit in the bitmap that rejected bl. Then, we return that
475 ** through next_zero_bit for find_forward to try.
477 ** Just because we return something in next_zero_bit does not mean we won't
478 ** reject it on the next call to reiserfs_in_journal
481 int reiserfs_in_journal(struct super_block *p_s_sb,
482 int bmap_nr, int bit_nr, int search_all,
483 b_blocknr_t *next_zero_bit) {
484 struct reiserfs_journal_cnode *cn ;
485 struct reiserfs_list_bitmap *jb ;
489 *next_zero_bit = 0 ; /* always start this at zero. */
491 PROC_INFO_INC( p_s_sb, journal.in_journal );
492 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
493 ** if we crash before the transaction that freed it commits, this transaction won't
494 ** have committed either, and the block will never be written
497 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
498 PROC_INFO_INC( p_s_sb, journal.in_journal_bitmap );
499 jb = SB_JOURNAL(p_s_sb)->j_list_bitmap + i ;
500 if (jb->journal_list && jb->bitmaps[bmap_nr] &&
501 test_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data)) {
502 *next_zero_bit = find_next_zero_bit((unsigned long *)
503 (jb->bitmaps[bmap_nr]->data),
504 p_s_sb->s_blocksize << 3, bit_nr+1) ;
510 bl = bmap_nr * (p_s_sb->s_blocksize << 3) + bit_nr;
511 /* is it in any old transactions? */
512 if (search_all && (cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_hash_table, bl))) {
516 /* is it in the current transaction. This should never happen */
517 if ((cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, bl))) {
522 PROC_INFO_INC( p_s_sb, journal.in_journal_reusable );
527 /* insert cn into table
529 inline void insert_journal_hash(struct reiserfs_journal_cnode **table, struct reiserfs_journal_cnode *cn) {
530 struct reiserfs_journal_cnode *cn_orig ;
532 cn_orig = journal_hash(table, cn->sb, cn->blocknr) ;
533 cn->hnext = cn_orig ;
536 cn_orig->hprev = cn ;
538 journal_hash(table, cn->sb, cn->blocknr) = cn ;
541 /* lock the current transaction */
542 inline static void lock_journal(struct super_block *p_s_sb) {
543 PROC_INFO_INC( p_s_sb, journal.lock_journal );
544 down(&SB_JOURNAL(p_s_sb)->j_lock);
547 /* unlock the current transaction */
548 inline static void unlock_journal(struct super_block *p_s_sb) {
549 up(&SB_JOURNAL(p_s_sb)->j_lock);
552 static inline void get_journal_list(struct reiserfs_journal_list *jl)
557 static inline void put_journal_list(struct super_block *s,
558 struct reiserfs_journal_list *jl)
560 if (jl->j_refcount < 1) {
561 reiserfs_panic (s, "trans id %lu, refcount at %d", jl->j_trans_id,
564 if (--jl->j_refcount == 0)
565 reiserfs_kfree(jl, sizeof(struct reiserfs_journal_list), s);
569 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
570 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
573 static void cleanup_freed_for_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
575 struct reiserfs_list_bitmap *jb = jl->j_list_bitmap ;
577 cleanup_bitmap_list(p_s_sb, jb) ;
579 jl->j_list_bitmap->journal_list = NULL ;
580 jl->j_list_bitmap = NULL ;
583 static int journal_list_still_alive(struct super_block *s,
584 unsigned long trans_id)
586 struct list_head *entry = &SB_JOURNAL(s)->j_journal_list;
587 struct reiserfs_journal_list *jl;
589 if (!list_empty(entry)) {
590 jl = JOURNAL_LIST_ENTRY(entry->next);
591 if (jl->j_trans_id <= trans_id) {
598 static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate) {
599 char b[BDEVNAME_SIZE];
601 if (buffer_journaled(bh)) {
602 reiserfs_warning(NULL, "clm-2084: pinned buffer %lu:%s sent to disk",
603 bh->b_blocknr, bdevname(bh->b_bdev, b)) ;
606 set_buffer_uptodate(bh) ;
608 clear_buffer_uptodate(bh) ;
613 static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate) {
615 set_buffer_uptodate(bh) ;
617 clear_buffer_uptodate(bh) ;
622 static void submit_logged_buffer(struct buffer_head *bh) {
624 bh->b_end_io = reiserfs_end_buffer_io_sync ;
625 mark_buffer_notjournal_new(bh) ;
626 clear_buffer_dirty(bh) ;
627 if (!test_and_clear_bit(BH_JTest, &bh->b_state))
629 if (!buffer_uptodate(bh))
631 submit_bh(WRITE, bh) ;
634 static void submit_ordered_buffer(struct buffer_head *bh) {
636 bh->b_end_io = reiserfs_end_ordered_io;
637 clear_buffer_dirty(bh) ;
638 if (!buffer_uptodate(bh))
640 submit_bh(WRITE, bh) ;
643 #define CHUNK_SIZE 32
644 struct buffer_chunk {
645 struct buffer_head *bh[CHUNK_SIZE];
649 static void write_chunk(struct buffer_chunk *chunk) {
651 for (i = 0; i < chunk->nr ; i++) {
652 submit_logged_buffer(chunk->bh[i]) ;
657 static void write_ordered_chunk(struct buffer_chunk *chunk) {
659 for (i = 0; i < chunk->nr ; i++) {
660 submit_ordered_buffer(chunk->bh[i]) ;
665 static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
667 void (fn)(struct buffer_chunk *))
670 if (chunk->nr >= CHUNK_SIZE)
672 chunk->bh[chunk->nr++] = bh;
673 if (chunk->nr >= CHUNK_SIZE) {
685 atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
686 static struct reiserfs_jh *alloc_jh(void) {
687 struct reiserfs_jh *jh;
689 jh = kmalloc(sizeof(*jh), GFP_NOFS);
691 atomic_inc(&nr_reiserfs_jh);
699 * we want to free the jh when the buffer has been written
702 void reiserfs_free_jh(struct buffer_head *bh) {
703 struct reiserfs_jh *jh;
707 bh->b_private = NULL;
709 list_del_init(&jh->list);
711 if (atomic_read(&nr_reiserfs_jh) <= 0)
713 atomic_dec(&nr_reiserfs_jh);
718 static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
721 struct reiserfs_jh *jh;
724 spin_lock(&j->j_dirty_buffers_lock);
725 if (!bh->b_private) {
726 spin_unlock(&j->j_dirty_buffers_lock);
730 list_del_init(&jh->list);
735 spin_lock(&j->j_dirty_buffers_lock);
736 /* buffer must be locked for __add_jh, should be able to have
737 * two adds at the same time
744 jh->jl = j->j_current_jl;
746 list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
748 list_add_tail(&jh->list, &jh->jl->j_bh_list);
750 spin_unlock(&j->j_dirty_buffers_lock);
754 int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh) {
755 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
757 int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh) {
758 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
761 #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
762 static int write_ordered_buffers(spinlock_t *lock,
763 struct reiserfs_journal *j,
764 struct reiserfs_journal_list *jl,
765 struct list_head *list)
767 struct buffer_head *bh;
768 struct reiserfs_jh *jh;
770 struct buffer_chunk chunk;
771 struct list_head tmp;
772 INIT_LIST_HEAD(&tmp);
776 while(!list_empty(list)) {
777 jh = JH_ENTRY(list->next);
780 if (test_set_buffer_locked(bh)) {
781 if (!buffer_dirty(bh)) {
782 list_del_init(&jh->list);
783 list_add(&jh->list, &tmp);
788 write_ordered_chunk(&chunk);
794 if (buffer_dirty(bh)) {
795 list_del_init(&jh->list);
796 list_add(&jh->list, &tmp);
797 add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
799 reiserfs_free_jh(bh);
804 cond_resched_lock(lock);
808 write_ordered_chunk(&chunk);
811 while(!list_empty(&tmp)) {
812 jh = JH_ENTRY(tmp.prev);
815 reiserfs_free_jh(bh);
817 if (buffer_locked(bh)) {
822 if (!buffer_uptodate(bh))
825 cond_resched_lock(lock);
831 static int flush_older_commits(struct super_block *s, struct reiserfs_journal_list *jl) {
832 struct reiserfs_journal_list *other_jl;
833 struct reiserfs_journal_list *first_jl;
834 struct list_head *entry;
835 unsigned long trans_id = jl->j_trans_id;
836 unsigned long other_trans_id;
837 unsigned long first_trans_id;
841 * first we walk backwards to find the oldest uncommitted transation
844 entry = jl->j_list.prev;
846 other_jl = JOURNAL_LIST_ENTRY(entry);
847 if (entry == &SB_JOURNAL(s)->j_journal_list ||
848 atomic_read(&other_jl->j_older_commits_done))
852 entry = other_jl->j_list.prev;
855 /* if we didn't find any older uncommitted transactions, return now */
856 if (first_jl == jl) {
860 first_trans_id = first_jl->j_trans_id;
862 entry = &first_jl->j_list;
864 other_jl = JOURNAL_LIST_ENTRY(entry);
865 other_trans_id = other_jl->j_trans_id;
867 if (other_trans_id < trans_id) {
868 if (atomic_read(&other_jl->j_commit_left) != 0) {
869 flush_commit_list(s, other_jl, 0);
871 /* list we were called with is gone, return */
872 if (!journal_list_still_alive(s, trans_id))
875 /* the one we just flushed is gone, this means all
876 * older lists are also gone, so first_jl is no longer
877 * valid either. Go back to the beginning.
879 if (!journal_list_still_alive(s, other_trans_id)) {
884 if (entry == &SB_JOURNAL(s)->j_journal_list)
892 int reiserfs_async_progress_wait(struct super_block *s) {
894 struct reiserfs_journal *j = SB_JOURNAL(s);
895 if (atomic_read(&j->j_async_throttle))
896 blk_congestion_wait(WRITE, HZ/10);
901 ** if this journal list still has commit blocks unflushed, send them to disk.
903 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
904 ** Before the commit block can by written, every other log block must be safely on disk
907 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) {
910 struct buffer_head *tbh = NULL ;
911 unsigned long trans_id = jl->j_trans_id;
913 reiserfs_check_lock_depth(s, "flush_commit_list") ;
915 if (atomic_read(&jl->j_older_commits_done)) {
919 /* before we can put our commit blocks on disk, we have to make sure everyone older than
924 if (trans_id == SB_JOURNAL(s)->j_trans_id)
927 get_journal_list(jl);
929 if (flush_older_commits(s, jl) == 1) {
930 /* list disappeared during flush_older_commits. return */
935 /* make sure nobody is trying to flush this one at the same time */
936 down(&jl->j_commit_lock);
937 if (!journal_list_still_alive(s, trans_id)) {
938 up(&jl->j_commit_lock);
941 if (jl->j_trans_id == 0)
944 /* this commit is done, exit */
945 if (atomic_read(&(jl->j_commit_left)) <= 0) {
947 atomic_set(&(jl->j_older_commits_done), 1) ;
949 up(&jl->j_commit_lock);
953 if (!list_empty(&jl->j_bh_list)) {
955 write_ordered_buffers(&SB_JOURNAL(s)->j_dirty_buffers_lock,
956 SB_JOURNAL(s), jl, &jl->j_bh_list);
959 if (!list_empty(&jl->j_bh_list))
962 * for the description block and all the log blocks, submit any buffers
963 * that haven't already reached the disk
965 atomic_inc(&SB_JOURNAL(s)->j_async_throttle);
966 for (i = 0 ; i < (jl->j_len + 1) ; i++) {
967 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start+i) %
968 SB_ONDISK_JOURNAL_SIZE(s);
969 tbh = journal_find_get_block(s, bn) ;
970 if (buffer_dirty(tbh))
971 ll_rw_block(WRITE, 1, &tbh) ;
974 atomic_dec(&SB_JOURNAL(s)->j_async_throttle);
976 /* wait on everything written so far before writing the commit */
977 for (i = 0 ; i < (jl->j_len + 1) ; i++) {
978 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
979 (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s) ;
980 tbh = journal_find_get_block(s, bn) ;
981 wait_on_buffer(tbh) ;
982 // since we're using ll_rw_blk above, it might have skipped over
983 // a locked buffer. Double check here
985 if (buffer_dirty(tbh))
986 sync_dirty_buffer(tbh);
987 if (!buffer_uptodate(tbh)) {
988 reiserfs_panic(s, "journal-601, buffer write failed\n") ;
990 put_bh(tbh) ; /* once for journal_find_get_block */
991 put_bh(tbh) ; /* once due to original getblk in do_journal_end */
992 atomic_dec(&(jl->j_commit_left)) ;
995 if (atomic_read(&(jl->j_commit_left)) != 1)
998 if (buffer_dirty(jl->j_commit_bh))
1000 mark_buffer_dirty(jl->j_commit_bh) ;
1001 sync_dirty_buffer(jl->j_commit_bh) ;
1002 if (!buffer_uptodate(jl->j_commit_bh)) {
1003 reiserfs_panic(s, "journal-615: buffer write failed\n") ;
1005 bforget(jl->j_commit_bh) ;
1006 if (SB_JOURNAL(s)->j_last_commit_id != 0 &&
1007 (jl->j_trans_id - SB_JOURNAL(s)->j_last_commit_id) != 1) {
1008 reiserfs_warning(s, "clm-2200: last commit %lu, current %lu",
1009 SB_JOURNAL(s)->j_last_commit_id,
1012 SB_JOURNAL(s)->j_last_commit_id = jl->j_trans_id;
1014 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1015 cleanup_freed_for_journal_list(s, jl) ;
1017 /* mark the metadata dirty */
1018 dirty_one_transaction(s, jl);
1019 atomic_dec(&(jl->j_commit_left)) ;
1022 atomic_set(&(jl->j_older_commits_done), 1) ;
1024 up(&jl->j_commit_lock);
1026 put_journal_list(s, jl);
1032 ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1033 ** returns NULL if it can't find anything
1035 static struct reiserfs_journal_list *find_newer_jl_for_cn(struct reiserfs_journal_cnode *cn) {
1036 struct super_block *sb = cn->sb;
1037 b_blocknr_t blocknr = cn->blocknr ;
1041 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
1049 void remove_journal_hash(struct super_block *, struct reiserfs_journal_cnode **,
1050 struct reiserfs_journal_list *, unsigned long, int);
1053 ** once all the real blocks have been flushed, it is safe to remove them from the
1054 ** journal list for this transaction. Aside from freeing the cnode, this also allows the
1055 ** block to be reallocated for data blocks if it had been deleted.
1057 static void remove_all_from_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl, int debug) {
1058 struct reiserfs_journal_cnode *cn, *last ;
1059 cn = jl->j_realblock ;
1061 /* which is better, to lock once around the whole loop, or
1062 ** to lock for each call to remove_journal_hash?
1065 if (cn->blocknr != 0) {
1067 reiserfs_warning (p_s_sb, "block %u, bh is %d, state %ld", cn->blocknr,
1068 cn->bh ? 1: 0, cn->state) ;
1071 remove_journal_hash(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_hash_table, jl, cn->blocknr, 1) ;
1075 free_cnode(p_s_sb, last) ;
1077 jl->j_realblock = NULL ;
1081 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1082 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1083 ** releasing blocks in this transaction for reuse as data blocks.
1084 ** called by flush_journal_list, before it calls remove_all_from_journal_list
1087 static int _update_journal_header_block(struct super_block *p_s_sb, unsigned long offset, unsigned long trans_id) {
1088 struct reiserfs_journal_header *jh ;
1089 if (trans_id >= SB_JOURNAL(p_s_sb)->j_last_flush_trans_id) {
1090 if (buffer_locked((SB_JOURNAL(p_s_sb)->j_header_bh))) {
1091 wait_on_buffer((SB_JOURNAL(p_s_sb)->j_header_bh)) ;
1092 if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
1093 reiserfs_panic(p_s_sb, "journal-699: buffer write failed\n") ;
1096 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
1097 SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = offset ;
1098 jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
1099 jh->j_last_flush_trans_id = cpu_to_le32(trans_id) ;
1100 jh->j_first_unflushed_offset = cpu_to_le32(offset) ;
1101 jh->j_mount_id = cpu_to_le32(SB_JOURNAL(p_s_sb)->j_mount_id) ;
1102 set_buffer_dirty(SB_JOURNAL(p_s_sb)->j_header_bh) ;
1103 sync_dirty_buffer(SB_JOURNAL(p_s_sb)->j_header_bh) ;
1104 if (!buffer_uptodate(SB_JOURNAL(p_s_sb)->j_header_bh)) {
1105 reiserfs_warning (p_s_sb, "journal-837: IO error during journal replay");
1112 static int update_journal_header_block(struct super_block *p_s_sb,
1113 unsigned long offset,
1114 unsigned long trans_id) {
1115 if (_update_journal_header_block(p_s_sb, offset, trans_id)) {
1116 reiserfs_panic(p_s_sb, "journal-712: buffer write failed\n") ;
1121 ** flush any and all journal lists older than you are
1122 ** can only be called from flush_journal_list
1124 static int flush_older_journal_lists(struct super_block *p_s_sb,
1125 struct reiserfs_journal_list *jl)
1127 struct list_head *entry;
1128 struct reiserfs_journal_list *other_jl ;
1129 unsigned long trans_id = jl->j_trans_id;
1131 /* we know we are the only ones flushing things, no extra race
1132 * protection is required.
1135 entry = SB_JOURNAL(p_s_sb)->j_journal_list.next;
1136 other_jl = JOURNAL_LIST_ENTRY(entry);
1137 if (other_jl->j_trans_id < trans_id) {
1138 /* do not flush all */
1139 flush_journal_list(p_s_sb, other_jl, 0) ;
1141 /* other_jl is now deleted from the list */
1147 static void del_from_work_list(struct super_block *s,
1148 struct reiserfs_journal_list *jl) {
1149 if (!list_empty(&jl->j_working_list)) {
1150 list_del_init(&jl->j_working_list);
1151 SB_JOURNAL(s)->j_num_work_lists--;
1155 /* flush a journal list, both commit and real blocks
1157 ** always set flushall to 1, unless you are calling from inside
1158 ** flush_journal_list
1160 ** IMPORTANT. This can only be called while there are no journal writers,
1161 ** and the journal is locked. That means it can only be called from
1162 ** do_journal_end, or by journal_release
1164 static int flush_journal_list(struct super_block *s,
1165 struct reiserfs_journal_list *jl, int flushall) {
1166 struct reiserfs_journal_list *pjl ;
1167 struct reiserfs_journal_cnode *cn, *last ;
1171 struct buffer_head *saved_bh ;
1172 unsigned long j_len_saved = jl->j_len ;
1174 if (j_len_saved <= 0) {
1178 if (atomic_read(&SB_JOURNAL(s)->j_wcount) != 0) {
1179 reiserfs_warning(s, "clm-2048: flush_journal_list called with wcount %d",
1180 atomic_read(&SB_JOURNAL(s)->j_wcount)) ;
1182 if (jl->j_trans_id == 0)
1185 /* if flushall == 0, the lock is already held */
1187 down(&SB_JOURNAL(s)->j_flush_sem);
1188 } else if (!down_trylock(&SB_JOURNAL(s)->j_flush_sem)) {
1193 if (j_len_saved > SB_JOURNAL_TRANS_MAX(s)) {
1194 reiserfs_panic(s, "journal-715: flush_journal_list, length is %lu, trans id %lu\n", j_len_saved, jl->j_trans_id);
1198 /* if all the work is already done, get out of here */
1199 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1200 atomic_read(&(jl->j_commit_left)) <= 0) {
1201 goto flush_older_and_return ;
1204 /* start by putting the commit list on disk. This will also flush
1205 ** the commit lists of any olders transactions
1207 flush_commit_list(s, jl, 1) ;
1209 if (!(jl->j_state & LIST_DIRTY))
1212 /* are we done now? */
1213 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1214 atomic_read(&(jl->j_commit_left)) <= 0) {
1215 goto flush_older_and_return ;
1218 /* loop through each cnode, see if we need to write it,
1219 ** or wait on a more recent transaction, or just ignore it
1221 if (atomic_read(&(SB_JOURNAL(s)->j_wcount)) != 0) {
1222 reiserfs_panic(s, "journal-844: panic journal list is flushing, wcount is not 0\n") ;
1224 cn = jl->j_realblock ;
1229 /* blocknr of 0 is no longer in the hash, ignore it */
1230 if (cn->blocknr == 0) {
1233 pjl = find_newer_jl_for_cn(cn) ;
1234 /* the order is important here. We check pjl to make sure we
1235 ** don't clear BH_JDirty_wait if we aren't the one writing this
1238 if (!pjl && cn->bh) {
1241 /* we do this to make sure nobody releases the buffer while
1242 ** we are working with it
1246 if (buffer_journal_dirty(saved_bh)) {
1251 } else if (can_dirty(cn)) {
1252 /* everything with !pjl && jwait should be writable */
1257 /* if someone has this block in a newer transaction, just make
1258 ** sure they are commited, and don't try writing it to disk
1261 if (atomic_read(&pjl->j_commit_left))
1262 flush_commit_list(s, pjl, 1) ;
1266 /* bh == NULL when the block got to disk on its own, OR,
1267 ** the block got freed in a future transaction
1269 if (saved_bh == NULL) {
1273 /* this should never happen. kupdate_one_transaction has this list
1274 ** locked while it works, so we should never see a buffer here that
1275 ** is not marked JDirty_wait
1277 if ((!was_jwait) && !buffer_locked(saved_bh)) {
1278 reiserfs_warning (s, "journal-813: BAD! buffer %llu %cdirty %cjwait, "
1279 "not in a newer tranasction",
1280 (unsigned long long)saved_bh->b_blocknr,
1281 was_dirty ? ' ' : '!', was_jwait ? ' ' : '!') ;
1284 /* we inc again because saved_bh gets decremented at free_cnode */
1286 set_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
1287 lock_buffer(saved_bh);
1288 if (cn->blocknr != saved_bh->b_blocknr)
1290 if (buffer_dirty(saved_bh))
1291 submit_logged_buffer(saved_bh) ;
1293 unlock_buffer(saved_bh);
1296 reiserfs_warning (s, "clm-2082: Unable to flush buffer %llu in %s",
1297 (unsigned long long)saved_bh->b_blocknr, __FUNCTION__);
1303 /* we incremented this to keep others from taking the buffer head away */
1305 if (atomic_read(&(saved_bh->b_count)) < 0) {
1306 reiserfs_warning (s, "journal-945: saved_bh->b_count < 0");
1311 cn = jl->j_realblock ;
1313 if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1315 reiserfs_panic(s, "journal-1011: cn->bh is NULL\n") ;
1317 wait_on_buffer(cn->bh) ;
1319 reiserfs_panic(s, "journal-1012: cn->bh is NULL\n") ;
1321 if (!buffer_uptodate(cn->bh)) {
1322 reiserfs_panic(s, "journal-949: buffer write failed\n") ;
1324 /* note, we must clear the JDirty_wait bit after the up to date
1325 ** check, otherwise we race against our flushpage routine
1327 if (!test_and_clear_bit(BH_JDirty_wait, &cn->bh->b_state))
1330 /* undo the inc from journal_mark_dirty */
1338 flush_older_and_return:
1339 /* before we can update the journal header block, we _must_ flush all
1340 ** real blocks from all older transactions to disk. This is because
1341 ** once the header block is updated, this transaction will not be
1342 ** replayed after a crash
1345 flush_older_journal_lists(s, jl);
1348 /* before we can remove everything from the hash tables for this
1349 ** transaction, we must make sure it can never be replayed
1351 ** since we are only called from do_journal_end, we know for sure there
1352 ** are no allocations going on while we are flushing journal lists. So,
1353 ** we only need to update the journal header block for the last list
1357 update_journal_header_block(s, (jl->j_start + jl->j_len + 2) % SB_ONDISK_JOURNAL_SIZE(s), jl->j_trans_id) ;
1359 remove_all_from_journal_list(s, jl, 0) ;
1360 list_del(&jl->j_list);
1361 SB_JOURNAL(s)->j_num_lists--;
1362 del_from_work_list(s, jl);
1364 if (SB_JOURNAL(s)->j_last_flush_id != 0 &&
1365 (jl->j_trans_id - SB_JOURNAL(s)->j_last_flush_id) != 1) {
1366 reiserfs_warning(s, "clm-2201: last flush %lu, current %lu",
1367 SB_JOURNAL(s)->j_last_flush_id,
1370 SB_JOURNAL(s)->j_last_flush_id = jl->j_trans_id;
1372 /* not strictly required since we are freeing the list, but it should
1373 * help find code using dead lists later on
1376 atomic_set(&(jl->j_nonzerolen), 0) ;
1378 jl->j_realblock = NULL ;
1379 jl->j_commit_bh = NULL ;
1380 jl->j_trans_id = 0 ;
1382 put_journal_list(s, jl);
1384 up(&SB_JOURNAL(s)->j_flush_sem);
1388 static int write_one_transaction(struct super_block *s,
1389 struct reiserfs_journal_list *jl,
1390 struct buffer_chunk *chunk)
1392 struct reiserfs_journal_cnode *cn;
1395 jl->j_state |= LIST_TOUCHED;
1396 del_from_work_list(s, jl);
1397 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
1401 cn = jl->j_realblock ;
1403 /* if the blocknr == 0, this has been cleared from the hash,
1406 if (cn->blocknr == 0) {
1409 if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
1410 struct buffer_head *tmp_bh;
1411 /* we can race against journal_mark_freed when we try
1412 * to lock_buffer(cn->bh), so we have to inc the buffer
1413 * count, and recheck things after locking
1417 lock_buffer(tmp_bh);
1418 if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
1419 if (!buffer_journal_dirty(tmp_bh) ||
1420 reiserfs_buffer_prepared(tmp_bh))
1422 add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
1425 /* note, cn->bh might be null now */
1426 unlock_buffer(tmp_bh);
1437 /* used by flush_commit_list */
1438 static int dirty_one_transaction(struct super_block *s,
1439 struct reiserfs_journal_list *jl)
1441 struct reiserfs_journal_cnode *cn;
1442 struct reiserfs_journal_list *pjl;
1445 jl->j_state |= LIST_DIRTY;
1446 cn = jl->j_realblock ;
1448 /* look for a more recent transaction that logged this
1449 ** buffer. Only the most recent transaction with a buffer in
1450 ** it is allowed to send that buffer to disk
1452 pjl = find_newer_jl_for_cn(cn) ;
1453 if (!pjl && cn->blocknr && cn->bh && buffer_journal_dirty(cn->bh))
1457 /* if the buffer is prepared, it will either be logged
1458 * or restored. If restored, we need to make sure
1459 * it actually gets marked dirty
1461 mark_buffer_notjournal_new(cn->bh) ;
1462 if (test_bit(BH_JPrepared, &cn->bh->b_state)) {
1463 set_bit(BH_JRestore_dirty, &cn->bh->b_state);
1465 set_bit(BH_JTest, &cn->bh->b_state);
1466 mark_buffer_dirty(cn->bh);
1474 static int kupdate_transactions(struct super_block *s,
1475 struct reiserfs_journal_list *jl,
1476 struct reiserfs_journal_list **next_jl,
1477 unsigned long *next_trans_id,
1482 int transactions_flushed = 0;
1483 unsigned long orig_trans_id = jl->j_trans_id;
1484 struct buffer_chunk chunk;
1485 struct list_head *entry;
1488 down(&SB_JOURNAL(s)->j_flush_sem);
1489 if (!journal_list_still_alive(s, orig_trans_id)) {
1493 /* we've got j_flush_sem held, nobody is going to delete any
1494 * of these lists out from underneath us
1496 while((num_trans && transactions_flushed < num_trans) ||
1497 (!num_trans && written < num_blocks)) {
1499 if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
1500 atomic_read(&jl->j_commit_left))
1502 del_from_work_list(s, jl);
1505 ret = write_one_transaction(s, jl, &chunk);
1509 transactions_flushed++;
1511 entry = jl->j_list.next;
1514 if (entry == &SB_JOURNAL(s)->j_journal_list) {
1517 jl = JOURNAL_LIST_ENTRY(entry);
1519 /* don't bother with older transactions */
1520 if (jl->j_trans_id <= orig_trans_id)
1524 write_chunk(&chunk);
1528 up(&SB_JOURNAL(s)->j_flush_sem);
1532 /* for o_sync and fsync heavy applications, they tend to use
1533 ** all the journa list slots with tiny transactions. These
1534 ** trigger lots and lots of calls to update the header block, which
1535 ** adds seeks and slows things down.
1537 ** This function tries to clear out a large chunk of the journal lists
1538 ** at once, which makes everything faster since only the newest journal
1539 ** list updates the header block
1541 static int flush_used_journal_lists(struct super_block *s,
1542 struct reiserfs_journal_list *jl) {
1543 unsigned long len = 0;
1544 unsigned long cur_len;
1547 struct reiserfs_journal_list *tjl;
1548 struct reiserfs_journal_list *flush_jl;
1549 unsigned long trans_id;
1551 flush_jl = tjl = jl;
1553 /* flush for 256 transactions or 256 blocks, whichever comes first */
1554 for(i = 0 ; i < 256 && len < 256 ; i++) {
1555 if (atomic_read(&tjl->j_commit_left) ||
1556 tjl->j_trans_id < jl->j_trans_id) {
1559 cur_len = atomic_read(&tjl->j_nonzerolen);
1561 tjl->j_state &= ~LIST_TOUCHED;
1565 if (tjl->j_list.next == &SB_JOURNAL(s)->j_journal_list)
1567 tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
1569 /* try to find a group of blocks we can flush across all the
1570 ** transactions, but only bother if we've actually spanned
1571 ** across multiple lists
1573 if (flush_jl != jl) {
1574 ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
1576 flush_journal_list(s, flush_jl, 1);
1581 ** removes any nodes in table with name block and dev as bh.
1582 ** only touchs the hnext and hprev pointers.
1584 void remove_journal_hash(struct super_block *sb,
1585 struct reiserfs_journal_cnode **table,
1586 struct reiserfs_journal_list *jl,
1587 unsigned long block, int remove_freed)
1589 struct reiserfs_journal_cnode *cur ;
1590 struct reiserfs_journal_cnode **head ;
1592 head= &(journal_hash(table, sb, block)) ;
1598 if (cur->blocknr == block && cur->sb == sb && (jl == NULL || jl == cur->jlist) &&
1599 (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1601 cur->hnext->hprev = cur->hprev ;
1604 cur->hprev->hnext = cur->hnext ;
1606 *head = cur->hnext ;
1611 if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1612 atomic_dec(&(cur->jlist->j_nonzerolen)) ;
1620 static void free_journal_ram(struct super_block *p_s_sb) {
1621 reiserfs_kfree(SB_JOURNAL(p_s_sb)->j_current_jl,
1622 sizeof(struct reiserfs_journal_list), p_s_sb);
1623 SB_JOURNAL(p_s_sb)->j_num_lists--;
1625 vfree(SB_JOURNAL(p_s_sb)->j_cnode_free_orig) ;
1626 free_list_bitmaps(p_s_sb, SB_JOURNAL(p_s_sb)->j_list_bitmap) ;
1627 free_bitmap_nodes(p_s_sb) ; /* must be after free_list_bitmaps */
1628 if (SB_JOURNAL(p_s_sb)->j_header_bh) {
1629 brelse(SB_JOURNAL(p_s_sb)->j_header_bh) ;
1631 /* j_header_bh is on the journal dev, make sure not to release the journal
1632 * dev until we brelse j_header_bh
1634 release_journal_dev(p_s_sb, SB_JOURNAL(p_s_sb));
1635 vfree(SB_JOURNAL(p_s_sb)) ;
1639 ** call on unmount. Only set error to 1 if you haven't made your way out
1640 ** of read_super() yet. Any other caller must keep error at 0.
1642 static int do_journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, int error) {
1643 struct reiserfs_transaction_handle myth ;
1645 /* we only want to flush out transactions if we were called with error == 0
1647 if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
1648 /* end the current trans */
1649 do_journal_end(th, p_s_sb,10, FLUSH_ALL) ;
1651 /* make sure something gets logged to force our way into the flush code */
1652 journal_join(&myth, p_s_sb, 1) ;
1653 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
1654 journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
1655 do_journal_end(&myth, p_s_sb,1, FLUSH_ALL) ;
1658 reiserfs_mounted_fs_count-- ;
1659 /* wait for all commits to finish */
1660 cancel_delayed_work(&SB_JOURNAL(p_s_sb)->j_work);
1661 flush_workqueue(commit_wq);
1662 if (!reiserfs_mounted_fs_count) {
1663 destroy_workqueue(commit_wq);
1667 free_journal_ram(p_s_sb) ;
1673 ** call on unmount. flush all journal trans, release all alloc'd ram
1675 int journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1676 return do_journal_release(th, p_s_sb, 0) ;
1679 ** only call from an error condition inside reiserfs_read_super!
1681 int journal_release_error(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1682 return do_journal_release(th, p_s_sb, 1) ;
1685 /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
1686 static int journal_compare_desc_commit(struct super_block *p_s_sb, struct reiserfs_journal_desc *desc,
1687 struct reiserfs_journal_commit *commit) {
1688 if (get_commit_trans_id (commit) != get_desc_trans_id (desc) ||
1689 get_commit_trans_len (commit) != get_desc_trans_len (desc) ||
1690 get_commit_trans_len (commit) > SB_JOURNAL_TRANS_MAX(p_s_sb) ||
1691 get_commit_trans_len (commit) <= 0
1697 /* returns 0 if it did not find a description block
1698 ** returns -1 if it found a corrupt commit block
1699 ** returns 1 if both desc and commit were valid
1701 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) {
1702 struct reiserfs_journal_desc *desc ;
1703 struct reiserfs_journal_commit *commit ;
1704 struct buffer_head *c_bh ;
1705 unsigned long offset ;
1710 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1711 if (get_desc_trans_len(desc) > 0 && !memcmp(get_journal_desc_magic (d_bh), JOURNAL_DESC_MAGIC, 8)) {
1712 if (oldest_invalid_trans_id && *oldest_invalid_trans_id && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
1713 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-986: transaction "
1714 "is valid returning because trans_id %d is greater than "
1715 "oldest_invalid %lu", get_desc_trans_id(desc),
1716 *oldest_invalid_trans_id);
1719 if (newest_mount_id && *newest_mount_id > get_desc_mount_id (desc)) {
1720 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1087: transaction "
1721 "is valid returning because mount_id %d is less than "
1722 "newest_mount_id %lu", get_desc_mount_id (desc),
1726 if ( get_desc_trans_len(desc) > SB_JOURNAL_TRANS_MAX(p_s_sb) ) {
1727 reiserfs_warning(p_s_sb, "journal-2018: Bad transaction length %d encountered, ignoring transaction", get_desc_trans_len(desc));
1730 offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1732 /* ok, we have a journal description block, lets see if the transaction was valid */
1733 c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1734 ((offset + get_desc_trans_len(desc) + 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
1737 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1738 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1739 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1740 "journal_transaction_is_valid, commit offset %ld had bad "
1741 "time %d or length %d",
1742 c_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1743 get_commit_trans_id (commit),
1744 get_commit_trans_len(commit));
1746 if (oldest_invalid_trans_id) {
1747 *oldest_invalid_trans_id = get_desc_trans_id(desc) ;
1748 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1004: "
1749 "transaction_is_valid setting oldest invalid trans_id "
1750 "to %d", get_desc_trans_id(desc)) ;
1755 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1006: found valid "
1756 "transaction start offset %llu, len %d id %d",
1757 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1758 get_desc_trans_len(desc), get_desc_trans_id(desc)) ;
1765 static void brelse_array(struct buffer_head **heads, int num) {
1767 for (i = 0 ; i < num ; i++) {
1773 ** given the start, and values for the oldest acceptable transactions,
1774 ** this either reads in a replays a transaction, or returns because the transaction
1775 ** is invalid, or too old.
1777 static int journal_read_transaction(struct super_block *p_s_sb, unsigned long cur_dblock, unsigned long oldest_start,
1778 unsigned long oldest_trans_id, unsigned long newest_mount_id) {
1779 struct reiserfs_journal_desc *desc ;
1780 struct reiserfs_journal_commit *commit ;
1781 unsigned long trans_id = 0 ;
1782 struct buffer_head *c_bh ;
1783 struct buffer_head *d_bh ;
1784 struct buffer_head **log_blocks = NULL ;
1785 struct buffer_head **real_blocks = NULL ;
1786 unsigned long trans_offset ;
1790 d_bh = journal_bread(p_s_sb, cur_dblock) ;
1793 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1794 trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1795 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
1796 "journal_read_transaction, offset %llu, len %d mount_id %d",
1797 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1798 get_desc_trans_len(desc), get_desc_mount_id(desc)) ;
1799 if (get_desc_trans_id(desc) < oldest_trans_id) {
1800 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
1801 "journal_read_trans skipping because %lu is too old",
1802 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)) ;
1806 if (get_desc_mount_id(desc) != newest_mount_id) {
1807 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
1808 "journal_read_trans skipping because %d is != "
1809 "newest_mount_id %lu", get_desc_mount_id(desc),
1814 c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1815 ((trans_offset + get_desc_trans_len(desc) + 1) %
1816 SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
1821 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1822 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1823 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal_read_transaction, "
1824 "commit offset %llu had bad time %d or length %d",
1825 c_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1826 get_commit_trans_id(commit), get_commit_trans_len(commit));
1831 trans_id = get_desc_trans_id(desc) ;
1832 /* now we know we've got a good transaction, and it was inside the valid time ranges */
1833 log_blocks = reiserfs_kmalloc(get_desc_trans_len(desc) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
1834 real_blocks = reiserfs_kmalloc(get_desc_trans_len(desc) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
1835 if (!log_blocks || !real_blocks) {
1838 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1839 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1840 reiserfs_warning(p_s_sb, "journal-1169: kmalloc failed, unable to mount FS") ;
1843 /* get all the buffer heads */
1844 trans_half = journal_trans_half (p_s_sb->s_blocksize) ;
1845 for(i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1846 log_blocks[i] = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + (trans_offset + 1 + i) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
1847 if (i < trans_half) {
1848 real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(desc->j_realblock[i])) ;
1850 real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(commit->j_realblock[i - trans_half])) ;
1852 if ( real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(p_s_sb) ) {
1853 reiserfs_warning(p_s_sb, "journal-1207: REPLAY FAILURE fsck required! Block to replay is outside of filesystem");
1856 /* make sure we don't try to replay onto log or reserved area */
1857 if (is_block_in_log_or_reserved_area(p_s_sb, real_blocks[i]->b_blocknr)) {
1858 reiserfs_warning(p_s_sb, "journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block") ;
1860 brelse_array(log_blocks, i) ;
1861 brelse_array(real_blocks, i) ;
1864 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1865 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1869 /* read in the log blocks, memcpy to the corresponding real block */
1870 ll_rw_block(READ, get_desc_trans_len(desc), log_blocks) ;
1871 for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1872 wait_on_buffer(log_blocks[i]) ;
1873 if (!buffer_uptodate(log_blocks[i])) {
1874 reiserfs_warning(p_s_sb, "journal-1212: REPLAY FAILURE fsck required! buffer write failed") ;
1875 brelse_array(log_blocks + i, get_desc_trans_len(desc) - i) ;
1876 brelse_array(real_blocks, get_desc_trans_len(desc)) ;
1879 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1880 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1883 memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data, real_blocks[i]->b_size) ;
1884 set_buffer_uptodate(real_blocks[i]) ;
1885 brelse(log_blocks[i]) ;
1887 /* flush out the real blocks */
1888 for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1889 set_buffer_dirty(real_blocks[i]) ;
1890 ll_rw_block(WRITE, 1, real_blocks + i) ;
1892 for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1893 wait_on_buffer(real_blocks[i]) ;
1894 if (!buffer_uptodate(real_blocks[i])) {
1895 reiserfs_warning(p_s_sb, "journal-1226: REPLAY FAILURE, fsck required! buffer write failed") ;
1896 brelse_array(real_blocks + i, get_desc_trans_len(desc) - i) ;
1899 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1900 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1903 brelse(real_blocks[i]) ;
1905 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + ((trans_offset + get_desc_trans_len(desc) + 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)) ;
1906 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1095: setting journal "
1907 "start to offset %ld",
1908 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)) ;
1910 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
1911 SB_JOURNAL(p_s_sb)->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1912 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = trans_id ;
1913 SB_JOURNAL(p_s_sb)->j_trans_id = trans_id + 1;
1916 reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
1917 reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
1921 /* This function reads blocks starting from block and to max_block of bufsize
1922 size (but no more than BUFNR blocks at a time). This proved to improve
1923 mounting speed on self-rebuilding raid5 arrays at least.
1924 Right now it is only used from journal code. But later we might use it
1926 Note: Do not use journal_getblk/sb_getblk functions here! */
1927 struct buffer_head * reiserfs_breada (struct block_device *dev, int block, int bufsize,
1928 unsigned int max_block)
1930 struct buffer_head * bhlist[BUFNR];
1931 unsigned int blocks = BUFNR;
1932 struct buffer_head * bh;
1935 bh = __getblk (dev, block, bufsize );
1936 if (buffer_uptodate (bh))
1939 if (block + BUFNR > max_block) {
1940 blocks = max_block - block;
1944 for (i = 1; i < blocks; i++) {
1945 bh = __getblk (dev, block + i, bufsize);
1946 if (buffer_uptodate (bh)) {
1950 else bhlist[j++] = bh;
1952 ll_rw_block (READ, j, bhlist);
1953 for(i = 1; i < j; i++)
1956 wait_on_buffer (bh);
1957 if (buffer_uptodate (bh))
1964 ** read and replay the log
1965 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
1966 ** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
1968 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
1970 ** On exit, it sets things up so the first transaction will work correctly.
1972 static int journal_read(struct super_block *p_s_sb) {
1973 struct reiserfs_journal_desc *desc ;
1974 unsigned long oldest_trans_id = 0;
1975 unsigned long oldest_invalid_trans_id = 0 ;
1977 unsigned long oldest_start = 0;
1978 unsigned long cur_dblock = 0 ;
1979 unsigned long newest_mount_id = 9 ;
1980 struct buffer_head *d_bh ;
1981 struct reiserfs_journal_header *jh ;
1982 int valid_journal_header = 0 ;
1983 int replay_count = 0 ;
1984 int continue_replay = 1 ;
1986 char b[BDEVNAME_SIZE];
1988 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1989 reiserfs_info (p_s_sb, "checking transaction log (%s)\n",
1990 bdevname(SB_JOURNAL(p_s_sb)->j_dev_bd, b));
1991 start = get_seconds();
1993 /* step 1, read in the journal header block. Check the transaction it says
1994 ** is the first unflushed, and if that transaction is not valid,
1997 SB_JOURNAL(p_s_sb)->j_header_bh = journal_bread(p_s_sb,
1998 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1999 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2000 if (!SB_JOURNAL(p_s_sb)->j_header_bh) {
2003 jh = (struct reiserfs_journal_header *)(SB_JOURNAL(p_s_sb)->j_header_bh->b_data) ;
2004 if (le32_to_cpu(jh->j_first_unflushed_offset) >= 0 &&
2005 le32_to_cpu(jh->j_first_unflushed_offset) < SB_ONDISK_JOURNAL_SIZE(p_s_sb) &&
2006 le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
2007 oldest_start = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2008 le32_to_cpu(jh->j_first_unflushed_offset) ;
2009 oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2010 newest_mount_id = le32_to_cpu(jh->j_mount_id);
2011 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1153: found in "
2012 "header: first_unflushed_offset %d, last_flushed_trans_id "
2013 "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
2014 le32_to_cpu(jh->j_last_flush_trans_id)) ;
2015 valid_journal_header = 1 ;
2017 /* now, we try to read the first unflushed offset. If it is not valid,
2018 ** there is nothing more we can do, and it makes no sense to read
2019 ** through the whole log.
2021 d_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + le32_to_cpu(jh->j_first_unflushed_offset)) ;
2022 ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL) ;
2024 continue_replay = 0 ;
2027 goto start_log_replay;
2030 if (continue_replay && bdev_read_only(p_s_sb->s_bdev)) {
2031 reiserfs_warning (p_s_sb,
2032 "clm-2076: device is readonly, unable to replay log") ;
2036 /* ok, there are transactions that need to be replayed. start with the first log block, find
2037 ** all the valid transactions, and pick out the oldest.
2039 while(continue_replay && cur_dblock < (SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb))) {
2040 /* Note that it is required for blocksize of primary fs device and journal
2041 device to be the same */
2042 d_bh = reiserfs_breada(SB_JOURNAL(p_s_sb)->j_dev_bd, cur_dblock, p_s_sb->s_blocksize,
2043 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb)) ;
2044 ret = journal_transaction_is_valid(p_s_sb, d_bh, &oldest_invalid_trans_id, &newest_mount_id) ;
2046 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
2047 if (oldest_start == 0) { /* init all oldest_ values */
2048 oldest_trans_id = get_desc_trans_id(desc) ;
2049 oldest_start = d_bh->b_blocknr ;
2050 newest_mount_id = get_desc_mount_id(desc) ;
2051 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1179: Setting "
2052 "oldest_start to offset %llu, trans_id %lu",
2053 oldest_start - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2055 } else if (oldest_trans_id > get_desc_trans_id(desc)) {
2056 /* one we just read was older */
2057 oldest_trans_id = get_desc_trans_id(desc) ;
2058 oldest_start = d_bh->b_blocknr ;
2059 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1180: Resetting "
2060 "oldest_start to offset %lu, trans_id %lu",
2061 oldest_start - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2064 if (newest_mount_id < get_desc_mount_id(desc)) {
2065 newest_mount_id = get_desc_mount_id(desc) ;
2066 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2067 "newest_mount_id to %d", get_desc_mount_id(desc));
2069 cur_dblock += get_desc_trans_len(desc) + 2 ;
2077 cur_dblock = oldest_start ;
2078 if (oldest_trans_id) {
2079 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1206: Starting replay "
2080 "from offset %llu, trans_id %lu",
2081 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2086 while(continue_replay && oldest_trans_id > 0) {
2087 ret = journal_read_transaction(p_s_sb, cur_dblock, oldest_start, oldest_trans_id, newest_mount_id) ;
2090 } else if (ret != 0) {
2093 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start ;
2095 if (cur_dblock == oldest_start)
2099 if (oldest_trans_id == 0) {
2100 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1225: No valid "
2101 "transactions found") ;
2103 /* j_start does not get set correctly if we don't replay any transactions.
2104 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2105 ** copy the trans_id from the header
2107 if (valid_journal_header && replay_count == 0) {
2108 SB_JOURNAL(p_s_sb)->j_start = le32_to_cpu(jh->j_first_unflushed_offset) ;
2109 SB_JOURNAL(p_s_sb)->j_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2110 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) ;
2111 SB_JOURNAL(p_s_sb)->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
2113 SB_JOURNAL(p_s_sb)->j_mount_id = newest_mount_id + 1 ;
2115 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2116 "newest_mount_id to %lu", SB_JOURNAL(p_s_sb)->j_mount_id) ;
2117 SB_JOURNAL(p_s_sb)->j_first_unflushed_offset = SB_JOURNAL(p_s_sb)->j_start ;
2118 if (replay_count > 0) {
2119 reiserfs_info (p_s_sb, "replayed %d transactions in %lu seconds\n",
2120 replay_count, get_seconds() - start) ;
2122 if (!bdev_read_only(p_s_sb->s_bdev) &&
2123 _update_journal_header_block(p_s_sb, SB_JOURNAL(p_s_sb)->j_start,
2124 SB_JOURNAL(p_s_sb)->j_last_flush_trans_id))
2126 /* replay failed, caller must call free_journal_ram and abort
2134 static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
2136 struct reiserfs_journal_list *jl;
2138 jl = reiserfs_kmalloc(sizeof(struct reiserfs_journal_list), GFP_NOFS, s);
2143 memset(jl, 0, sizeof(*jl));
2144 INIT_LIST_HEAD(&jl->j_list);
2145 INIT_LIST_HEAD(&jl->j_working_list);
2146 INIT_LIST_HEAD(&jl->j_tail_bh_list);
2147 INIT_LIST_HEAD(&jl->j_bh_list);
2148 sema_init(&jl->j_commit_lock, 1);
2149 SB_JOURNAL(s)->j_num_lists++;
2150 get_journal_list(jl);
2154 static void journal_list_init(struct super_block *p_s_sb) {
2155 SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
2158 static int release_journal_dev( struct super_block *super,
2159 struct reiserfs_journal *journal )
2165 if( journal -> j_dev_file != NULL ) {
2166 result = filp_close( journal -> j_dev_file, NULL );
2167 journal -> j_dev_file = NULL;
2168 journal -> j_dev_bd = NULL;
2169 } else if( journal -> j_dev_bd != NULL ) {
2170 result = blkdev_put( journal -> j_dev_bd );
2171 journal -> j_dev_bd = NULL;
2175 reiserfs_warning(super, "sh-457: release_journal_dev: Cannot release journal device: %i", result );
2180 static int journal_init_dev( struct super_block *super,
2181 struct reiserfs_journal *journal,
2182 const char *jdev_name )
2186 int blkdev_mode = FMODE_READ | FMODE_WRITE;
2187 char b[BDEVNAME_SIZE];
2191 journal -> j_dev_bd = NULL;
2192 journal -> j_dev_file = NULL;
2193 jdev = SB_ONDISK_JOURNAL_DEVICE( super ) ?
2194 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
2196 if (bdev_read_only(super->s_bdev))
2197 blkdev_mode = FMODE_READ;
2199 /* there is no "jdev" option and journal is on separate device */
2200 if( ( !jdev_name || !jdev_name[ 0 ] ) ) {
2201 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode);
2202 if (IS_ERR(journal->j_dev_bd)) {
2203 result = PTR_ERR(journal->j_dev_bd);
2204 journal->j_dev_bd = NULL;
2205 reiserfs_warning (super, "sh-458: journal_init_dev: "
2206 "cannot init journal device '%s': %i",
2207 __bdevname(jdev, b), result );
2209 } else if (jdev != super->s_dev)
2210 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2214 journal -> j_dev_file = filp_open( jdev_name, 0, 0 );
2215 if( !IS_ERR( journal -> j_dev_file ) ) {
2216 struct inode *jdev_inode = journal->j_dev_file->f_mapping->host;
2217 if( !S_ISBLK( jdev_inode -> i_mode ) ) {
2218 reiserfs_warning (super, "journal_init_dev: '%s' is "
2219 "not a block device", jdev_name );
2223 journal->j_dev_bd = I_BDEV(jdev_inode);
2224 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2227 result = PTR_ERR( journal -> j_dev_file );
2228 journal -> j_dev_file = NULL;
2229 reiserfs_warning (super,
2230 "journal_init_dev: Cannot open '%s': %i",
2231 jdev_name, result );
2234 release_journal_dev( super, journal );
2236 reiserfs_info(super, "journal_init_dev: journal device: %s\n",
2237 bdevname(journal->j_dev_bd, b));
2242 ** must be called once on fs mount. calls journal_read for you
2244 int journal_init(struct super_block *p_s_sb, const char * j_dev_name, int old_format, unsigned int commit_max_age) {
2245 int num_cnodes = SB_ONDISK_JOURNAL_SIZE(p_s_sb) * 2 ;
2246 struct buffer_head *bhjh;
2247 struct reiserfs_super_block * rs;
2248 struct reiserfs_journal_header *jh;
2249 struct reiserfs_journal *journal;
2250 struct reiserfs_journal_list *jl;
2251 char b[BDEVNAME_SIZE];
2253 journal = SB_JOURNAL(p_s_sb) = vmalloc(sizeof (struct reiserfs_journal)) ;
2255 reiserfs_warning (p_s_sb, "journal-1256: unable to get memory for journal structure") ;
2258 memset(journal, 0, sizeof(struct reiserfs_journal)) ;
2259 INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_bitmap_nodes) ;
2260 INIT_LIST_HEAD (&SB_JOURNAL(p_s_sb)->j_prealloc_list);
2261 INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_working_list);
2262 INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_journal_list);
2263 if (reiserfs_allocate_list_bitmaps(p_s_sb,
2264 SB_JOURNAL(p_s_sb)->j_list_bitmap,
2265 SB_BMAP_NR(p_s_sb)))
2266 goto free_and_return ;
2267 allocate_bitmap_nodes(p_s_sb) ;
2269 /* reserved for journal area support */
2270 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) = (old_format ?
2271 REISERFS_OLD_DISK_OFFSET_IN_BYTES / p_s_sb->s_blocksize +
2272 SB_BMAP_NR(p_s_sb) + 1 :
2273 REISERFS_DISK_OFFSET_IN_BYTES / p_s_sb->s_blocksize + 2);
2275 /* Sanity check to see is the standard journal fitting withing first bitmap
2276 (actual for small blocksizes) */
2277 if ( !SB_ONDISK_JOURNAL_DEVICE( p_s_sb ) &&
2278 (SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb) > p_s_sb->s_blocksize * 8) ) {
2279 reiserfs_warning (p_s_sb, "journal-1393: journal does not fit for area "
2280 "addressed by first of bitmap blocks. It starts at "
2281 "%u and its size is %u. Block size %ld",
2282 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb),
2283 SB_ONDISK_JOURNAL_SIZE(p_s_sb), p_s_sb->s_blocksize);
2284 goto free_and_return;
2287 if( journal_init_dev( p_s_sb, journal, j_dev_name ) != 0 ) {
2288 reiserfs_warning (p_s_sb, "sh-462: unable to initialize jornal device");
2289 goto free_and_return;
2292 rs = SB_DISK_SUPER_BLOCK(p_s_sb);
2294 /* read journal header */
2295 bhjh = journal_bread(p_s_sb,
2296 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2298 reiserfs_warning (p_s_sb, "sh-459: unable to read journal header");
2299 goto free_and_return;
2301 jh = (struct reiserfs_journal_header *)(bhjh->b_data);
2303 /* make sure that journal matches to the super block */
2304 if (is_reiserfs_jr(rs) && (jh->jh_journal.jp_journal_magic != sb_jp_journal_magic(rs))) {
2305 reiserfs_warning (p_s_sb, "sh-460: journal header magic %x "
2306 "(device %s) does not match to magic found in super "
2308 jh->jh_journal.jp_journal_magic,
2309 bdevname( SB_JOURNAL(p_s_sb)->j_dev_bd, b),
2310 sb_jp_journal_magic(rs));
2312 goto free_and_return;
2315 SB_JOURNAL_TRANS_MAX(p_s_sb) = le32_to_cpu (jh->jh_journal.jp_journal_trans_max);
2316 SB_JOURNAL_MAX_BATCH(p_s_sb) = le32_to_cpu (jh->jh_journal.jp_journal_max_batch);
2317 SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb) = le32_to_cpu (jh->jh_journal.jp_journal_max_commit_age);
2318 SB_JOURNAL_MAX_TRANS_AGE(p_s_sb) = JOURNAL_MAX_TRANS_AGE;
2320 if (SB_JOURNAL_TRANS_MAX(p_s_sb)) {
2321 /* make sure these parameters are available, assign it if they are not */
2322 __u32 initial = SB_JOURNAL_TRANS_MAX(p_s_sb);
2325 if (p_s_sb->s_blocksize < 4096)
2326 ratio = 4096 / p_s_sb->s_blocksize;
2328 if (SB_ONDISK_JOURNAL_SIZE(p_s_sb)/SB_JOURNAL_TRANS_MAX(p_s_sb) < JOURNAL_MIN_RATIO)
2329 SB_JOURNAL_TRANS_MAX(p_s_sb) = SB_ONDISK_JOURNAL_SIZE(p_s_sb) / JOURNAL_MIN_RATIO;
2330 if (SB_JOURNAL_TRANS_MAX(p_s_sb) > JOURNAL_TRANS_MAX_DEFAULT / ratio)
2331 SB_JOURNAL_TRANS_MAX(p_s_sb) = JOURNAL_TRANS_MAX_DEFAULT / ratio;
2332 if (SB_JOURNAL_TRANS_MAX(p_s_sb) < JOURNAL_TRANS_MIN_DEFAULT / ratio)
2333 SB_JOURNAL_TRANS_MAX(p_s_sb) = JOURNAL_TRANS_MIN_DEFAULT / ratio;
2335 if (SB_JOURNAL_TRANS_MAX(p_s_sb) != initial)
2336 reiserfs_warning (p_s_sb, "sh-461: journal_init: wrong transaction max size (%u). Changed to %u",
2337 initial, SB_JOURNAL_TRANS_MAX(p_s_sb));
2339 SB_JOURNAL_MAX_BATCH(p_s_sb) = SB_JOURNAL_TRANS_MAX(p_s_sb)*
2340 JOURNAL_MAX_BATCH_DEFAULT/JOURNAL_TRANS_MAX_DEFAULT;
2343 if (!SB_JOURNAL_TRANS_MAX(p_s_sb)) {
2344 /*we have the file system was created by old version of mkreiserfs
2345 so this field contains zero value */
2346 SB_JOURNAL_TRANS_MAX(p_s_sb) = JOURNAL_TRANS_MAX_DEFAULT ;
2347 SB_JOURNAL_MAX_BATCH(p_s_sb) = JOURNAL_MAX_BATCH_DEFAULT ;
2348 SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb) = JOURNAL_MAX_COMMIT_AGE ;
2350 /* for blocksize >= 4096 - max transaction size is 1024. For block size < 4096
2351 trans max size is decreased proportionally */
2352 if (p_s_sb->s_blocksize < 4096) {
2353 SB_JOURNAL_TRANS_MAX(p_s_sb) /= (4096 / p_s_sb->s_blocksize) ;
2354 SB_JOURNAL_MAX_BATCH(p_s_sb) = (SB_JOURNAL_TRANS_MAX(p_s_sb)) * 9 / 10 ;
2358 SB_JOURNAL_DEFAULT_MAX_COMMIT_AGE(p_s_sb) = SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb);
2360 if (commit_max_age != 0) {
2361 SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb) = commit_max_age;
2362 SB_JOURNAL_MAX_TRANS_AGE(p_s_sb) = commit_max_age;
2365 reiserfs_info (p_s_sb, "journal params: device %s, size %u, "
2366 "journal first block %u, max trans len %u, max batch %u, "
2367 "max commit age %u, max trans age %u\n",
2368 bdevname( SB_JOURNAL(p_s_sb)->j_dev_bd, b),
2369 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2370 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2371 SB_JOURNAL_TRANS_MAX(p_s_sb),
2372 SB_JOURNAL_MAX_BATCH(p_s_sb),
2373 SB_JOURNAL_MAX_COMMIT_AGE(p_s_sb),
2374 SB_JOURNAL_MAX_TRANS_AGE(p_s_sb));
2378 SB_JOURNAL(p_s_sb)->j_list_bitmap_index = 0 ;
2379 journal_list_init(p_s_sb) ;
2381 memset(SB_JOURNAL(p_s_sb)->j_list_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
2383 INIT_LIST_HEAD(&SB_JOURNAL(p_s_sb)->j_dirty_buffers) ;
2384 spin_lock_init(&SB_JOURNAL(p_s_sb)->j_dirty_buffers_lock) ;
2386 SB_JOURNAL(p_s_sb)->j_start = 0 ;
2387 SB_JOURNAL(p_s_sb)->j_len = 0 ;
2388 SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
2389 atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
2390 atomic_set(&(SB_JOURNAL(p_s_sb)->j_async_throttle), 0) ;
2391 SB_JOURNAL(p_s_sb)->j_bcount = 0 ;
2392 SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;
2393 SB_JOURNAL(p_s_sb)->j_last = NULL ;
2394 SB_JOURNAL(p_s_sb)->j_first = NULL ;
2395 init_waitqueue_head(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
2396 sema_init(&SB_JOURNAL(p_s_sb)->j_lock, 1);
2397 sema_init(&SB_JOURNAL(p_s_sb)->j_flush_sem, 1);
2399 SB_JOURNAL(p_s_sb)->j_trans_id = 10 ;
2400 SB_JOURNAL(p_s_sb)->j_mount_id = 10 ;
2401 SB_JOURNAL(p_s_sb)->j_state = 0 ;
2402 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
2403 SB_JOURNAL(p_s_sb)->j_cnode_free_list = allocate_cnodes(num_cnodes) ;
2404 SB_JOURNAL(p_s_sb)->j_cnode_free_orig = SB_JOURNAL(p_s_sb)->j_cnode_free_list ;
2405 SB_JOURNAL(p_s_sb)->j_cnode_free = SB_JOURNAL(p_s_sb)->j_cnode_free_list ? num_cnodes : 0 ;
2406 SB_JOURNAL(p_s_sb)->j_cnode_used = 0 ;
2407 SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
2409 init_journal_hash(p_s_sb) ;
2410 jl = SB_JOURNAL(p_s_sb)->j_current_jl;
2411 jl->j_list_bitmap = get_list_bitmap(p_s_sb, jl);
2412 if (!jl->j_list_bitmap) {
2413 reiserfs_warning(p_s_sb, "journal-2005, get_list_bitmap failed for journal list 0") ;
2414 goto free_and_return;
2416 if (journal_read(p_s_sb) < 0) {
2417 reiserfs_warning(p_s_sb, "Replay Failure, unable to mount") ;
2418 goto free_and_return;
2421 reiserfs_mounted_fs_count++ ;
2422 if (reiserfs_mounted_fs_count <= 1)
2423 commit_wq = create_workqueue("reiserfs");
2425 INIT_WORK(&journal->j_work, flush_async_commits, p_s_sb);
2428 free_journal_ram(p_s_sb);
2433 ** test for a polite end of the current transaction. Used by file_write, and should
2434 ** be used by delete to make sure they don't write more than can fit inside a single
2437 int journal_transaction_should_end(struct reiserfs_transaction_handle *th, int new_alloc) {
2438 time_t now = get_seconds() ;
2439 /* cannot restart while nested */
2440 if (th->t_refcount > 1)
2442 if ( SB_JOURNAL(th->t_super)->j_must_wait > 0 ||
2443 (SB_JOURNAL(th->t_super)->j_len_alloc + new_alloc) >= SB_JOURNAL_MAX_BATCH(th->t_super) ||
2444 atomic_read(&(SB_JOURNAL(th->t_super)->j_jlock)) ||
2445 (now - SB_JOURNAL(th->t_super)->j_trans_start_time) > SB_JOURNAL_MAX_TRANS_AGE(th->t_super) ||
2446 SB_JOURNAL(th->t_super)->j_cnode_free < (SB_JOURNAL_TRANS_MAX(th->t_super) * 3)) {
2452 /* this must be called inside a transaction, and requires the
2453 ** kernel_lock to be held
2455 void reiserfs_block_writes(struct reiserfs_transaction_handle *th) {
2456 struct super_block *s = th->t_super ;
2457 SB_JOURNAL(s)->j_must_wait = 1 ;
2458 set_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
2462 /* this must be called without a transaction started, and does not
2465 void reiserfs_allow_writes(struct super_block *s) {
2466 clear_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state) ;
2467 wake_up(&SB_JOURNAL(s)->j_join_wait) ;
2470 /* this must be called without a transaction started, and does not
2473 void reiserfs_wait_on_write_block(struct super_block *s) {
2474 wait_event(SB_JOURNAL(s)->j_join_wait,
2475 !test_bit(WRITERS_BLOCKED, &SB_JOURNAL(s)->j_state)) ;
2478 static void queue_log_writer(struct super_block *s) {
2480 set_bit(WRITERS_QUEUED, &SB_JOURNAL(s)->j_state);
2483 * we don't want to use wait_event here because
2484 * we only want to wait once.
2486 init_waitqueue_entry(&wait, current);
2487 add_wait_queue(&SB_JOURNAL(s)->j_join_wait, &wait);
2488 set_current_state(TASK_UNINTERRUPTIBLE);
2489 if (test_bit(WRITERS_QUEUED, &SB_JOURNAL(s)->j_state))
2491 current->state = TASK_RUNNING;
2492 remove_wait_queue(&SB_JOURNAL(s)->j_join_wait, &wait);
2495 static void wake_queued_writers(struct super_block *s) {
2496 if (test_and_clear_bit(WRITERS_QUEUED, &SB_JOURNAL(s)->j_state))
2497 wake_up(&SB_JOURNAL(s)->j_join_wait);
2500 static void let_transaction_grow(struct super_block *sb,
2501 unsigned long trans_id)
2503 unsigned long bcount = SB_JOURNAL(sb)->j_bcount;
2505 set_current_state(TASK_UNINTERRUPTIBLE);
2506 schedule_timeout(1);
2507 SB_JOURNAL(sb)->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2508 while ((atomic_read(&SB_JOURNAL(sb)->j_wcount) > 0 ||
2509 atomic_read(&SB_JOURNAL(sb)->j_jlock)) &&
2510 SB_JOURNAL(sb)->j_trans_id == trans_id) {
2511 queue_log_writer(sb);
2513 if (SB_JOURNAL(sb)->j_trans_id != trans_id)
2515 if (bcount == SB_JOURNAL(sb)->j_bcount)
2517 bcount = SB_JOURNAL(sb)->j_bcount;
2521 /* join == true if you must join an existing transaction.
2522 ** join == false if you can deal with waiting for others to finish
2524 ** this will block until the transaction is joinable. send the number of blocks you
2525 ** expect to use in nblocks.
2527 static int do_journal_begin_r(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,unsigned long nblocks,int join) {
2528 time_t now = get_seconds() ;
2530 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2531 struct reiserfs_transaction_handle myth;
2532 int sched_count = 0;
2534 reiserfs_check_lock_depth(p_s_sb, "journal_begin") ;
2535 RFALSE( p_s_sb->s_flags & MS_RDONLY,
2536 "clm-2078: calling journal_begin on readonly FS") ;
2538 PROC_INFO_INC( p_s_sb, journal.journal_being );
2539 /* set here for journal_join */
2541 th->t_super = p_s_sb ;
2544 lock_journal(p_s_sb) ;
2545 journal->j_bcount++;
2547 if (test_bit(WRITERS_BLOCKED, &journal->j_state)) {
2548 unlock_journal(p_s_sb) ;
2549 reiserfs_wait_on_write_block(p_s_sb) ;
2550 PROC_INFO_INC( p_s_sb, journal.journal_relock_writers );
2553 now = get_seconds();
2555 /* if there is no room in the journal OR
2556 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
2557 ** we don't sleep if there aren't other writers
2560 if ( (!join && journal->j_must_wait > 0) ||
2561 ( !join && (journal->j_len_alloc + nblocks + 2) >= SB_JOURNAL_MAX_BATCH(p_s_sb)) ||
2562 (!join && atomic_read(&journal->j_wcount) > 0 && journal->j_trans_start_time > 0 &&
2563 (now - journal->j_trans_start_time) > SB_JOURNAL_MAX_TRANS_AGE(p_s_sb)) ||
2564 (!join && atomic_read(&journal->j_jlock)) ||
2565 (!join && journal->j_cnode_free < (SB_JOURNAL_TRANS_MAX(p_s_sb) * 3))) {
2567 old_trans_id = journal->j_trans_id;
2568 unlock_journal(p_s_sb) ; /* allow others to finish this transaction */
2570 if (!join && (journal->j_len_alloc + nblocks + 2) >=
2571 SB_JOURNAL_MAX_BATCH(p_s_sb) &&
2572 ((journal->j_len + nblocks + 2) * 100) < (journal->j_len_alloc * 75))
2574 if (atomic_read(&journal->j_wcount) > 10) {
2576 queue_log_writer(p_s_sb);
2580 /* don't mess with joining the transaction if all we have to do is
2581 * wait for someone else to do a commit
2583 if (atomic_read(&journal->j_jlock)) {
2584 while (journal->j_trans_id == old_trans_id &&
2585 atomic_read(&journal->j_jlock)) {
2586 queue_log_writer(p_s_sb);
2590 journal_join(&myth, p_s_sb, 1) ;
2592 /* someone might have ended the transaction while we joined */
2593 if (old_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
2594 do_journal_end(&myth, p_s_sb, 1, 0) ;
2596 do_journal_end(&myth, p_s_sb, 1, COMMIT_NOW) ;
2599 PROC_INFO_INC( p_s_sb, journal.journal_relock_wcount );
2602 /* we are the first writer, set trans_id */
2603 if (journal->j_trans_start_time == 0) {
2604 journal->j_trans_start_time = get_seconds();
2606 atomic_inc(&(journal->j_wcount)) ;
2607 journal->j_len_alloc += nblocks ;
2608 th->t_blocks_logged = 0 ;
2609 th->t_blocks_allocated = nblocks ;
2610 th->t_trans_id = journal->j_trans_id ;
2611 unlock_journal(p_s_sb) ;
2615 struct reiserfs_transaction_handle *
2616 reiserfs_persistent_transaction(struct super_block *s, int nblocks) {
2618 struct reiserfs_transaction_handle *th ;
2620 /* if we're nesting into an existing transaction. It will be
2621 ** persistent on its own
2623 if (reiserfs_transaction_running(s)) {
2624 th = current->journal_info ;
2626 if (th->t_refcount < 2) {
2631 th = reiserfs_kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS, s) ;
2634 ret = journal_begin(th, s, nblocks) ;
2636 reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle), s) ;
2643 reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th) {
2644 struct super_block *s = th->t_super;
2646 ret = journal_end(th, th->t_super, th->t_blocks_allocated);
2647 if (th->t_refcount == 0)
2648 reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle), s) ;
2652 static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2653 struct reiserfs_transaction_handle *cur_th = current->journal_info;
2655 /* this keeps do_journal_end from NULLing out the current->journal_info
2658 th->t_handle_save = cur_th ;
2659 if (cur_th && cur_th->t_refcount > 1) {
2662 return do_journal_begin_r(th, p_s_sb, nblocks, 1) ;
2665 int journal_begin(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb, unsigned long nblocks) {
2666 struct reiserfs_transaction_handle *cur_th = current->journal_info ;
2669 th->t_handle_save = NULL ;
2671 /* we are nesting into the current transaction */
2672 if (cur_th->t_super == p_s_sb) {
2673 cur_th->t_refcount++ ;
2674 memcpy(th, cur_th, sizeof(*th));
2675 if (th->t_refcount <= 1)
2676 reiserfs_warning (p_s_sb, "BAD: refcount <= 1, but journal_info != 0");
2679 /* we've ended up with a handle from a different filesystem.
2680 ** save it and restore on journal_end. This should never
2683 reiserfs_warning(p_s_sb, "clm-2100: nesting info a different FS") ;
2684 th->t_handle_save = current->journal_info ;
2685 current->journal_info = th;
2688 current->journal_info = th;
2690 ret = do_journal_begin_r(th, p_s_sb, nblocks, 0) ;
2691 if (current->journal_info != th)
2697 ** puts bh into the current transaction. If it was already there, reorders removes the
2698 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
2700 ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
2701 ** transaction is committed.
2703 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
2705 int journal_mark_dirty(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, struct buffer_head *bh) {
2706 struct reiserfs_journal_cnode *cn = NULL;
2707 int count_already_incd = 0 ;
2710 PROC_INFO_INC( p_s_sb, journal.mark_dirty );
2711 if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
2712 reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ld\n",
2713 th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
2717 prepared = test_and_clear_bit(BH_JPrepared, &bh->b_state) ;
2718 clear_bit(BH_JRestore_dirty, &bh->b_state);
2719 /* already in this transaction, we are done */
2720 if (buffer_journaled(bh)) {
2721 PROC_INFO_INC( p_s_sb, journal.mark_dirty_already );
2725 /* this must be turned into a panic instead of a warning. We can't allow
2726 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
2727 ** could get to disk too early. NOT GOOD.
2729 if (!prepared || buffer_dirty(bh)) {
2730 reiserfs_warning (p_s_sb, "journal-1777: buffer %llu bad state "
2731 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
2732 (unsigned long long)bh->b_blocknr, prepared ? ' ' : '!',
2733 buffer_locked(bh) ? ' ' : '!',
2734 buffer_dirty(bh) ? ' ' : '!',
2735 buffer_journal_dirty(bh) ? ' ' : '!') ;
2738 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0) {
2739 reiserfs_warning (p_s_sb, "journal-1409: journal_mark_dirty returning because j_wcount was %d", atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount))) ;
2742 /* this error means I've screwed up, and we've overflowed the transaction.
2743 ** Nothing can be done here, except make the FS readonly or panic.
2745 if (SB_JOURNAL(p_s_sb)->j_len >= SB_JOURNAL_TRANS_MAX(p_s_sb)) {
2746 reiserfs_panic(th->t_super, "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n", SB_JOURNAL(p_s_sb)->j_len) ;
2749 if (buffer_journal_dirty(bh)) {
2750 count_already_incd = 1 ;
2751 PROC_INFO_INC( p_s_sb, journal.mark_dirty_notjournal );
2752 mark_buffer_notjournal_dirty(bh) ;
2755 if (SB_JOURNAL(p_s_sb)->j_len > SB_JOURNAL(p_s_sb)->j_len_alloc) {
2756 SB_JOURNAL(p_s_sb)->j_len_alloc = SB_JOURNAL(p_s_sb)->j_len + JOURNAL_PER_BALANCE_CNT ;
2759 set_bit(BH_JDirty, &bh->b_state) ;
2761 /* now put this guy on the end */
2763 cn = get_cnode(p_s_sb) ;
2765 reiserfs_panic(p_s_sb, "get_cnode failed!\n");
2768 if (th->t_blocks_logged == th->t_blocks_allocated) {
2769 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT ;
2770 SB_JOURNAL(p_s_sb)->j_len_alloc += JOURNAL_PER_BALANCE_CNT ;
2772 th->t_blocks_logged++ ;
2773 SB_JOURNAL(p_s_sb)->j_len++ ;
2776 cn->blocknr = bh->b_blocknr ;
2779 insert_journal_hash(SB_JOURNAL(p_s_sb)->j_hash_table, cn) ;
2780 if (!count_already_incd) {
2785 cn->prev = SB_JOURNAL(p_s_sb)->j_last ;
2787 if (SB_JOURNAL(p_s_sb)->j_last) {
2788 SB_JOURNAL(p_s_sb)->j_last->next = cn ;
2789 SB_JOURNAL(p_s_sb)->j_last = cn ;
2791 SB_JOURNAL(p_s_sb)->j_first = cn ;
2792 SB_JOURNAL(p_s_sb)->j_last = cn ;
2797 int journal_end(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2798 if (!current->journal_info && th->t_refcount > 1)
2799 reiserfs_warning (p_s_sb, "REISER-NESTING: th NULL, refcount %d",
2803 if (th->t_refcount > 0) {
2804 struct reiserfs_transaction_handle *cur_th = current->journal_info ;
2806 /* we aren't allowed to close a nested transaction on a different
2807 ** filesystem from the one in the task struct
2809 if (cur_th->t_super != th->t_super)
2813 memcpy(current->journal_info, th, sizeof(*th));
2818 return do_journal_end(th, p_s_sb, nblocks, 0) ;
2822 /* removes from the current transaction, relsing and descrementing any counters.
2823 ** also files the removed buffer directly onto the clean list
2825 ** called by journal_mark_freed when a block has been deleted
2827 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
2829 static int remove_from_transaction(struct super_block *p_s_sb, b_blocknr_t blocknr, int already_cleaned) {
2830 struct buffer_head *bh ;
2831 struct reiserfs_journal_cnode *cn ;
2834 cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, blocknr) ;
2835 if (!cn || !cn->bh) {
2840 cn->prev->next = cn->next ;
2843 cn->next->prev = cn->prev ;
2845 if (cn == SB_JOURNAL(p_s_sb)->j_first) {
2846 SB_JOURNAL(p_s_sb)->j_first = cn->next ;
2848 if (cn == SB_JOURNAL(p_s_sb)->j_last) {
2849 SB_JOURNAL(p_s_sb)->j_last = cn->prev ;
2852 remove_journal_hash(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, NULL, bh->b_blocknr, 0) ;
2853 mark_buffer_not_journaled(bh) ; /* don't log this one */
2855 if (!already_cleaned) {
2856 mark_buffer_notjournal_dirty(bh) ;
2858 if (atomic_read(&(bh->b_count)) < 0) {
2859 reiserfs_warning (p_s_sb, "journal-1752: remove from trans, b_count < 0");
2863 SB_JOURNAL(p_s_sb)->j_len-- ;
2864 SB_JOURNAL(p_s_sb)->j_len_alloc-- ;
2865 free_cnode(p_s_sb, cn) ;
2870 ** for any cnode in a journal list, it can only be dirtied of all the
2871 ** transactions that include it are commited to disk.
2872 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
2873 ** and 0 if you aren't
2875 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
2876 ** blocks for a given transaction on disk
2879 static int can_dirty(struct reiserfs_journal_cnode *cn) {
2880 struct super_block *sb = cn->sb;
2881 b_blocknr_t blocknr = cn->blocknr ;
2882 struct reiserfs_journal_cnode *cur = cn->hprev ;
2885 /* first test hprev. These are all newer than cn, so any node here
2886 ** with the same block number and dev means this node can't be sent
2887 ** to disk right now.
2889 while(cur && can_dirty) {
2890 if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
2891 cur->blocknr == blocknr) {
2896 /* then test hnext. These are all older than cn. As long as they
2897 ** are committed to the log, it is safe to write cn to disk
2900 while(cur && can_dirty) {
2901 if (cur->jlist && cur->jlist->j_len > 0 &&
2902 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
2903 cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
2911 /* syncs the commit blocks, but does not force the real buffers to disk
2912 ** will wait until the current transaction is done/commited before returning
2914 int journal_end_sync(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2916 /* you can sync while nested, very, very bad */
2917 if (th->t_refcount > 1) {
2920 if (SB_JOURNAL(p_s_sb)->j_len == 0) {
2921 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
2922 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
2924 return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT) ;
2928 ** writeback the pending async commits to disk
2930 static void flush_async_commits(void *p) {
2931 struct super_block *p_s_sb = p;
2932 struct reiserfs_journal_list *jl;
2933 struct list_head *entry;
2936 if (!list_empty(&SB_JOURNAL(p_s_sb)->j_journal_list)) {
2937 /* last entry is the youngest, commit it and you get everything */
2938 entry = SB_JOURNAL(p_s_sb)->j_journal_list.prev;
2939 jl = JOURNAL_LIST_ENTRY(entry);
2940 flush_commit_list(p_s_sb, jl, 1);
2944 * this is a little racey, but there's no harm in missing
2945 * the filemap_fdata_write
2947 if (!atomic_read(&SB_JOURNAL(p_s_sb)->j_async_throttle)) {
2948 atomic_inc(&SB_JOURNAL(p_s_sb)->j_async_throttle);
2949 filemap_fdatawrite(p_s_sb->s_bdev->bd_inode->i_mapping);
2950 atomic_dec(&SB_JOURNAL(p_s_sb)->j_async_throttle);
2955 ** flushes any old transactions to disk
2956 ** ends the current transaction if it is too old
2958 int reiserfs_flush_old_commits(struct super_block *p_s_sb) {
2960 struct reiserfs_transaction_handle th ;
2962 now = get_seconds();
2963 /* safety check so we don't flush while we are replaying the log during
2966 if (list_empty(&SB_JOURNAL(p_s_sb)->j_journal_list)) {
2970 /* check the current transaction. If there are no writers, and it is
2971 * too old, finish it, and force the commit blocks to disk
2973 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) <= 0 &&
2974 SB_JOURNAL(p_s_sb)->j_trans_start_time > 0 &&
2975 SB_JOURNAL(p_s_sb)->j_len > 0 &&
2976 (now - SB_JOURNAL(p_s_sb)->j_trans_start_time) >
2977 SB_JOURNAL_MAX_TRANS_AGE(p_s_sb))
2979 journal_join(&th, p_s_sb, 1) ;
2980 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
2981 journal_mark_dirty(&th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
2983 /* we're only being called from kreiserfsd, it makes no sense to do
2984 ** an async commit so that kreiserfsd can do it later
2986 do_journal_end(&th, p_s_sb,1, COMMIT_NOW | WAIT) ;
2988 return p_s_sb->s_dirt;
2992 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
2994 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
2995 ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
2996 ** flushes the commit list and returns 0.
2998 ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
3000 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3002 static int check_journal_end(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,
3003 unsigned long nblocks, int flags) {
3006 int flush = flags & FLUSH_ALL ;
3007 int commit_now = flags & COMMIT_NOW ;
3008 int wait_on_commit = flags & WAIT ;
3009 struct reiserfs_journal_list *jl;
3011 if (th->t_trans_id != SB_JOURNAL(p_s_sb)->j_trans_id) {
3012 reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ld\n",
3013 th->t_trans_id, SB_JOURNAL(p_s_sb)->j_trans_id);
3016 SB_JOURNAL(p_s_sb)->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged) ;
3017 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3018 atomic_dec(&(SB_JOURNAL(p_s_sb)->j_wcount)) ;
3021 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
3022 ** will be dealt with by next transaction that actually writes something, but should be taken
3023 ** care of in this trans
3025 if (SB_JOURNAL(p_s_sb)->j_len == 0) {
3028 /* if wcount > 0, and we are called to with flush or commit_now,
3029 ** we wait on j_join_wait. We will wake up when the last writer has
3030 ** finished the transaction, and started it on its way to the disk.
3031 ** Then, we flush the commit or journal list, and just return 0
3032 ** because the rest of journal end was already done for this transaction.
3034 if (atomic_read(&(SB_JOURNAL(p_s_sb)->j_wcount)) > 0) {
3035 if (flush || commit_now) {
3038 jl = SB_JOURNAL(p_s_sb)->j_current_jl;
3039 trans_id = jl->j_trans_id;
3041 jl->j_state |= LIST_COMMIT_PENDING;
3042 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
3044 SB_JOURNAL(p_s_sb)->j_next_full_flush = 1 ;
3046 unlock_journal(p_s_sb) ;
3048 /* sleep while the current transaction is still j_jlocked */
3049 while(SB_JOURNAL(p_s_sb)->j_trans_id == trans_id) {
3050 if (atomic_read(&SB_JOURNAL(p_s_sb)->j_jlock)) {
3051 queue_log_writer(p_s_sb);
3053 lock_journal(p_s_sb);
3054 if (SB_JOURNAL(p_s_sb)->j_trans_id == trans_id) {
3055 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 1) ;
3057 unlock_journal(p_s_sb);
3060 if (SB_JOURNAL(p_s_sb)->j_trans_id == trans_id) {
3063 if (commit_now && journal_list_still_alive(p_s_sb, trans_id) &&
3066 flush_commit_list(p_s_sb, jl, 1) ;
3070 unlock_journal(p_s_sb) ;
3074 /* deal with old transactions where we are the last writers */
3075 now = get_seconds();
3076 if ((now - SB_JOURNAL(p_s_sb)->j_trans_start_time) > SB_JOURNAL_MAX_TRANS_AGE(p_s_sb)) {
3078 SB_JOURNAL(p_s_sb)->j_next_async_flush = 1 ;
3080 /* don't batch when someone is waiting on j_join_wait */
3081 /* don't batch when syncing the commit or flushing the whole trans */
3082 if (!(SB_JOURNAL(p_s_sb)->j_must_wait > 0) && !(atomic_read(&(SB_JOURNAL(p_s_sb)->j_jlock))) && !flush && !commit_now &&
3083 (SB_JOURNAL(p_s_sb)->j_len < SB_JOURNAL_MAX_BATCH(p_s_sb)) &&
3084 SB_JOURNAL(p_s_sb)->j_len_alloc < SB_JOURNAL_MAX_BATCH(p_s_sb) && SB_JOURNAL(p_s_sb)->j_cnode_free > (SB_JOURNAL_TRANS_MAX(p_s_sb) * 3)) {
3085 SB_JOURNAL(p_s_sb)->j_bcount++ ;
3086 unlock_journal(p_s_sb) ;
3090 if (SB_JOURNAL(p_s_sb)->j_start > SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
3091 reiserfs_panic(p_s_sb, "journal-003: journal_end: j_start (%ld) is too high\n", SB_JOURNAL(p_s_sb)->j_start) ;
3097 ** Does all the work that makes deleting blocks safe.
3098 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3101 ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3102 ** before this transaction has finished.
3104 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3105 ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3106 ** the block can't be reallocated yet.
3108 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3110 int journal_mark_freed(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, b_blocknr_t blocknr) {
3111 struct reiserfs_journal_cnode *cn = NULL ;
3112 struct buffer_head *bh = NULL ;
3113 struct reiserfs_list_bitmap *jb = NULL ;
3116 cn = get_journal_hash_dev(p_s_sb, SB_JOURNAL(p_s_sb)->j_hash_table, blocknr);
3121 /* if it is journal new, we just remove it from this transaction */
3122 if (bh && buffer_journal_new(bh)) {
3123 mark_buffer_notjournal_new(bh) ;
3124 clear_prepared_bits(bh) ;
3125 reiserfs_clean_and_file_buffer(bh) ;
3126 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
3128 /* set the bit for this block in the journal bitmap for this transaction */
3129 jb = SB_JOURNAL(p_s_sb)->j_current_jl->j_list_bitmap;
3131 reiserfs_panic(p_s_sb, "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n") ;
3133 set_bit_in_list_bitmap(p_s_sb, blocknr, jb) ;
3135 /* Note, the entire while loop is not allowed to schedule. */
3138 clear_prepared_bits(bh) ;
3139 reiserfs_clean_and_file_buffer(bh) ;
3141 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
3143 /* find all older transactions with this block, make sure they don't try to write it out */
3144 cn = get_journal_hash_dev(p_s_sb,SB_JOURNAL(p_s_sb)->j_list_hash_table, blocknr) ;
3146 if (p_s_sb == cn->sb && blocknr == cn->blocknr) {
3147 set_bit(BLOCK_FREED, &cn->state) ;
3150 /* remove_from_transaction will brelse the buffer if it was
3151 ** in the current trans
3153 mark_buffer_notjournal_dirty(cn->bh) ;
3156 if (atomic_read(&(cn->bh->b_count)) < 0) {
3157 reiserfs_warning (p_s_sb, "journal-2138: cn->bh->b_count < 0");
3160 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3161 atomic_dec(&(cn->jlist->j_nonzerolen)) ;
3171 put_bh(bh) ; /* get_hash grabs the buffer */
3172 if (atomic_read(&(bh->b_count)) < 0) {
3173 reiserfs_warning (p_s_sb, "journal-2165: bh->b_count < 0");
3179 void reiserfs_update_inode_transaction(struct inode *inode) {
3180 REISERFS_I(inode)->i_jl = SB_JOURNAL(inode->i_sb)->j_current_jl;
3181 REISERFS_I(inode)->i_trans_id = SB_JOURNAL(inode->i_sb)->j_trans_id ;
3184 static void __commit_trans_jl(struct inode *inode, unsigned long id,
3185 struct reiserfs_journal_list *jl)
3187 struct reiserfs_transaction_handle th ;
3188 struct super_block *sb = inode->i_sb ;
3190 /* is it from the current transaction, or from an unknown transaction? */
3191 if (id == SB_JOURNAL(sb)->j_trans_id) {
3192 jl = SB_JOURNAL(sb)->j_current_jl;
3193 /* try to let other writers come in and grow this transaction */
3194 let_transaction_grow(sb, id);
3195 if (SB_JOURNAL(sb)->j_trans_id != id) {
3196 goto flush_commit_only;
3199 journal_begin(&th, sb, 1) ;
3201 /* someone might have ended this transaction while we joined */
3202 if (SB_JOURNAL(sb)->j_trans_id != id) {
3203 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb), 1) ;
3204 journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb)) ;
3205 journal_end(&th, sb, 1) ;
3206 goto flush_commit_only;
3209 journal_end_sync(&th, sb, 1) ;
3212 /* this gets tricky, we have to make sure the journal list in
3213 * the inode still exists. We know the list is still around
3214 * if we've got a larger transaction id than the oldest list
3217 if (journal_list_still_alive(inode->i_sb, id)) {
3218 flush_commit_list(sb, jl, 1) ;
3221 /* otherwise the list is gone, and long since committed */
3224 void reiserfs_commit_for_inode(struct inode *inode) {
3225 unsigned long id = REISERFS_I(inode)->i_trans_id;
3226 struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
3228 /* for the whole inode, assume unset id means it was
3229 * changed in the current transaction. More conservative
3232 reiserfs_update_inode_transaction(inode) ;
3233 id = REISERFS_I(inode)->i_trans_id;
3234 /* jl will be updated in __commit_trans_jl */
3237 __commit_trans_jl(inode, id, jl);
3240 void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb,
3241 struct buffer_head *bh) {
3242 PROC_INFO_INC( p_s_sb, journal.restore_prepared );
3246 if (test_and_clear_bit(BH_JRestore_dirty, &bh->b_state) &&
3247 buffer_journal_dirty(bh)) {
3248 struct reiserfs_journal_cnode *cn;
3249 cn = get_journal_hash_dev(p_s_sb,
3250 SB_JOURNAL(p_s_sb)->j_list_hash_table,
3252 if (cn && can_dirty(cn)) {
3253 set_bit(BH_JTest, &bh->b_state);
3254 mark_buffer_dirty(bh);
3257 clear_bit(BH_JPrepared, &bh->b_state) ;
3260 extern struct tree_balance *cur_tb ;
3262 ** before we can change a metadata block, we have to make sure it won't
3263 ** be written to disk while we are altering it. So, we must:
3268 int reiserfs_prepare_for_journal(struct super_block *p_s_sb,
3269 struct buffer_head *bh, int wait) {
3270 PROC_INFO_INC( p_s_sb, journal.prepare );
3272 if (test_set_buffer_locked(bh)) {
3277 set_bit(BH_JPrepared, &bh->b_state);
3278 if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
3279 clear_bit(BH_JTest, &bh->b_state);
3280 set_bit(BH_JRestore_dirty, &bh->b_state);
3286 static void flush_old_journal_lists(struct super_block *s) {
3287 struct reiserfs_journal_list *jl;
3288 struct list_head *entry;
3289 time_t now = get_seconds();
3291 while(!list_empty(&SB_JOURNAL(s)->j_journal_list)) {
3292 entry = SB_JOURNAL(s)->j_journal_list.next;
3293 jl = JOURNAL_LIST_ENTRY(entry);
3294 /* this check should always be run, to send old lists to disk */
3295 if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4))) {
3296 flush_used_journal_lists(s, jl);
3304 ** long and ugly. If flush, will not return until all commit
3305 ** blocks and all real buffers in the trans are on disk.
3306 ** If no_async, won't return until all commit blocks are on disk.
3308 ** keep reading, there are comments as you go along
3310 static int do_journal_end(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb, unsigned long nblocks,
3312 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
3313 struct reiserfs_journal_cnode *last_cn = NULL;
3314 struct reiserfs_journal_desc *desc ;
3315 struct reiserfs_journal_commit *commit ;
3316 struct buffer_head *c_bh ; /* commit bh */
3317 struct buffer_head *d_bh ; /* desc bh */
3318 int cur_write_start = 0 ; /* start index of current log write */
3321 int flush = flags & FLUSH_ALL ;
3322 int wait_on_commit = flags & WAIT ;
3323 struct reiserfs_journal_list *jl, *temp_jl;
3324 struct list_head *entry, *safe;
3325 unsigned long jindex;
3326 unsigned long commit_trans_id;
3329 if (th->t_refcount > 1)
3332 current->journal_info = th->t_handle_save;
3333 reiserfs_check_lock_depth(p_s_sb, "journal end");
3334 if (SB_JOURNAL(p_s_sb)->j_len == 0) {
3335 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
3336 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
3339 lock_journal(p_s_sb) ;
3340 if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
3341 flags |= FLUSH_ALL ;
3344 if (SB_JOURNAL(p_s_sb)->j_next_async_flush) {
3345 flags |= COMMIT_NOW | WAIT;
3349 /* check_journal_end locks the journal, and unlocks if it does not return 1
3350 ** it tells us if we should continue with the journal_end, or just return
3352 if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
3354 wake_queued_writers(p_s_sb);
3355 reiserfs_async_progress_wait(p_s_sb);
3359 /* check_journal_end might set these, check again */
3360 if (SB_JOURNAL(p_s_sb)->j_next_full_flush) {
3365 ** j must wait means we have to flush the log blocks, and the real blocks for
3368 if (SB_JOURNAL(p_s_sb)->j_must_wait > 0) {
3372 #ifdef REISERFS_PREALLOCATE
3373 /* quota ops might need to nest, setup the journal_info pointer for them */
3374 current->journal_info = th ;
3375 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
3376 * the transaction */
3377 current->journal_info = th->t_handle_save ;
3380 /* setup description block */
3381 d_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_JOURNAL(p_s_sb)->j_start) ;
3382 set_buffer_uptodate(d_bh);
3383 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data ;
3384 memset(d_bh->b_data, 0, d_bh->b_size) ;
3385 memcpy(get_journal_desc_magic (d_bh), JOURNAL_DESC_MAGIC, 8) ;
3386 set_desc_trans_id(desc, SB_JOURNAL(p_s_sb)->j_trans_id) ;
3388 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
3389 c_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3390 ((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
3391 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
3392 memset(c_bh->b_data, 0, c_bh->b_size) ;
3393 set_commit_trans_id(commit, SB_JOURNAL(p_s_sb)->j_trans_id) ;
3394 set_buffer_uptodate(c_bh) ;
3396 /* init this journal list */
3397 jl = SB_JOURNAL(p_s_sb)->j_current_jl;
3399 /* we lock the commit before doing anything because
3400 * we want to make sure nobody tries to run flush_commit_list until
3401 * the new transaction is fully setup, and we've already flushed the
3404 down(&jl->j_commit_lock);
3406 /* save the transaction id in case we need to commit it later */
3407 commit_trans_id = jl->j_trans_id;
3409 atomic_set(&jl->j_older_commits_done, 0) ;
3410 jl->j_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id ;
3411 jl->j_timestamp = SB_JOURNAL(p_s_sb)->j_trans_start_time ;
3412 jl->j_commit_bh = c_bh ;
3413 jl->j_start = SB_JOURNAL(p_s_sb)->j_start ;
3414 jl->j_len = SB_JOURNAL(p_s_sb)->j_len ;
3415 atomic_set(&jl->j_nonzerolen, SB_JOURNAL(p_s_sb)->j_len) ;
3416 atomic_set(&jl->j_commit_left, SB_JOURNAL(p_s_sb)->j_len + 2);
3417 jl->j_realblock = NULL ;
3419 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
3420 ** for each real block, add it to the journal list hash,
3421 ** copy into real block index array in the commit or desc block
3423 trans_half = journal_trans_half(p_s_sb->s_blocksize);
3424 for (i = 0, cn = SB_JOURNAL(p_s_sb)->j_first ; cn ; cn = cn->next, i++) {
3425 if (test_bit(BH_JDirty, &cn->bh->b_state) ) {
3426 jl_cn = get_cnode(p_s_sb) ;
3428 reiserfs_panic(p_s_sb, "journal-1676, get_cnode returned NULL\n") ;
3431 jl->j_realblock = jl_cn ;
3433 jl_cn->prev = last_cn ;
3434 jl_cn->next = NULL ;
3436 last_cn->next = jl_cn ;
3439 /* make sure the block we are trying to log is not a block
3440 of journal or reserved area */
3442 if (is_block_in_log_or_reserved_area(p_s_sb, cn->bh->b_blocknr)) {
3443 reiserfs_panic(p_s_sb, "journal-2332: Trying to log block %lu, which is a log block\n", cn->bh->b_blocknr) ;
3445 jl_cn->blocknr = cn->bh->b_blocknr ;
3448 jl_cn->bh = cn->bh ;
3450 insert_journal_hash(SB_JOURNAL(p_s_sb)->j_list_hash_table, jl_cn) ;
3451 if (i < trans_half) {
3452 desc->j_realblock[i] = cpu_to_le32(cn->bh->b_blocknr) ;
3454 commit->j_realblock[i - trans_half] = cpu_to_le32(cn->bh->b_blocknr) ;
3460 set_desc_trans_len(desc, SB_JOURNAL(p_s_sb)->j_len) ;
3461 set_desc_mount_id(desc, SB_JOURNAL(p_s_sb)->j_mount_id) ;
3462 set_desc_trans_id(desc, SB_JOURNAL(p_s_sb)->j_trans_id) ;
3463 set_commit_trans_len(commit, SB_JOURNAL(p_s_sb)->j_len);
3465 /* special check in case all buffers in the journal were marked for not logging */
3466 if (SB_JOURNAL(p_s_sb)->j_len == 0) {
3470 /* we're about to dirty all the log blocks, mark the description block
3471 * dirty now too. Don't mark the commit block dirty until all the
3472 * others are on disk
3474 mark_buffer_dirty(d_bh);
3476 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
3477 cur_write_start = SB_JOURNAL(p_s_sb)->j_start ;
3478 cn = SB_JOURNAL(p_s_sb)->j_first ;
3479 jindex = 1 ; /* start at one so we don't get the desc again */
3481 clear_bit(BH_JNew, &(cn->bh->b_state)) ;
3482 /* copy all the real blocks into log area. dirty log blocks */
3483 if (test_bit(BH_JDirty, &cn->bh->b_state)) {
3484 struct buffer_head *tmp_bh ;
3485 tmp_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3486 ((cur_write_start + jindex) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
3487 set_buffer_uptodate(tmp_bh);
3488 memcpy(tmp_bh->b_data, cn->bh->b_data, cn->bh->b_size) ;
3489 mark_buffer_dirty(tmp_bh);
3491 set_bit(BH_JDirty_wait, &(cn->bh->b_state)) ;
3492 clear_bit(BH_JDirty, &(cn->bh->b_state)) ;
3494 /* JDirty cleared sometime during transaction. don't log this one */
3495 reiserfs_warning(p_s_sb, "journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!") ;
3499 free_cnode(p_s_sb, cn) ;
3504 /* we are done with both the c_bh and d_bh, but
3505 ** c_bh must be written after all other commit blocks,
3506 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
3509 SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
3511 /* now it is safe to insert this transaction on the main list */
3512 list_add_tail(&jl->j_list, &SB_JOURNAL(p_s_sb)->j_journal_list);
3513 list_add_tail(&jl->j_working_list, &SB_JOURNAL(p_s_sb)->j_working_list);
3514 SB_JOURNAL(p_s_sb)->j_num_work_lists++;
3516 /* reset journal values for the next transaction */
3517 old_start = SB_JOURNAL(p_s_sb)->j_start ;
3518 SB_JOURNAL(p_s_sb)->j_start = (SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL(p_s_sb)->j_len + 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb);
3519 atomic_set(&(SB_JOURNAL(p_s_sb)->j_wcount), 0) ;
3520 SB_JOURNAL(p_s_sb)->j_bcount = 0 ;
3521 SB_JOURNAL(p_s_sb)->j_last = NULL ;
3522 SB_JOURNAL(p_s_sb)->j_first = NULL ;
3523 SB_JOURNAL(p_s_sb)->j_len = 0 ;
3524 SB_JOURNAL(p_s_sb)->j_trans_start_time = 0 ;
3525 SB_JOURNAL(p_s_sb)->j_trans_id++ ;
3526 SB_JOURNAL(p_s_sb)->j_current_jl->j_trans_id = SB_JOURNAL(p_s_sb)->j_trans_id;
3527 SB_JOURNAL(p_s_sb)->j_must_wait = 0 ;
3528 SB_JOURNAL(p_s_sb)->j_len_alloc = 0 ;
3529 SB_JOURNAL(p_s_sb)->j_next_full_flush = 0 ;
3530 SB_JOURNAL(p_s_sb)->j_next_async_flush = 0 ;
3531 init_journal_hash(p_s_sb) ;
3533 // make sure reiserfs_add_jh sees the new current_jl before we
3534 // write out the tails
3537 /* tail conversion targets have to hit the disk before we end the
3538 * transaction. Otherwise a later transaction might repack the tail
3539 * before this transaction commits, leaving the data block unflushed and
3540 * clean, if we crash before the later transaction commits, the data block
3543 if (!list_empty(&jl->j_tail_bh_list)) {
3545 write_ordered_buffers(&SB_JOURNAL(p_s_sb)->j_dirty_buffers_lock,
3546 SB_JOURNAL(p_s_sb), jl, &jl->j_tail_bh_list);
3549 if (!list_empty(&jl->j_tail_bh_list))
3551 up(&jl->j_commit_lock);
3553 /* honor the flush wishes from the caller, simple commits can
3554 ** be done outside the journal lock, they are done below
3556 ** if we don't flush the commit list right now, we put it into
3557 ** the work queue so the people waiting on the async progress work
3558 ** queue don't wait for this proc to flush journal lists and such.
3561 flush_commit_list(p_s_sb, jl, 1) ;
3562 flush_journal_list(p_s_sb, jl, 1) ;
3563 } else if (!(jl->j_state & LIST_COMMIT_PENDING))
3564 queue_delayed_work(commit_wq, &SB_JOURNAL(p_s_sb)->j_work, HZ/10);
3567 /* if the next transaction has any chance of wrapping, flush
3568 ** transactions that might get overwritten. If any journal lists are very
3569 ** old flush them as well.
3572 list_for_each_safe(entry, safe, &SB_JOURNAL(p_s_sb)->j_journal_list) {
3573 temp_jl = JOURNAL_LIST_ENTRY(entry);
3574 if (SB_JOURNAL(p_s_sb)->j_start <= temp_jl->j_start) {
3575 if ((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) >=
3578 flush_used_journal_lists(p_s_sb, temp_jl);
3580 } else if ((SB_JOURNAL(p_s_sb)->j_start +
3581 SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) <
3582 SB_ONDISK_JOURNAL_SIZE(p_s_sb))
3584 /* if we don't cross into the next transaction and we don't
3585 * wrap, there is no way we can overlap any later transactions
3590 } else if ((SB_JOURNAL(p_s_sb)->j_start +
3591 SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) >
3592 SB_ONDISK_JOURNAL_SIZE(p_s_sb))
3594 if (((SB_JOURNAL(p_s_sb)->j_start + SB_JOURNAL_TRANS_MAX(p_s_sb) + 1) %
3595 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) >= temp_jl->j_start)
3597 flush_used_journal_lists(p_s_sb, temp_jl);
3600 /* we don't overlap anything from out start to the end of the
3601 * log, and our wrapped portion doesn't overlap anything at
3602 * the start of the log. We can break
3608 flush_old_journal_lists(p_s_sb);
3610 SB_JOURNAL(p_s_sb)->j_current_jl->j_list_bitmap = get_list_bitmap(p_s_sb, SB_JOURNAL(p_s_sb)->j_current_jl) ;
3612 if (!(SB_JOURNAL(p_s_sb)->j_current_jl->j_list_bitmap)) {
3613 reiserfs_panic(p_s_sb, "journal-1996: do_journal_end, could not get a list bitmap\n") ;
3616 atomic_set(&(SB_JOURNAL(p_s_sb)->j_jlock), 0) ;
3617 unlock_journal(p_s_sb) ;
3618 /* wake up any body waiting to join. */
3619 clear_bit(WRITERS_QUEUED, &SB_JOURNAL(p_s_sb)->j_state);
3620 wake_up(&(SB_JOURNAL(p_s_sb)->j_join_wait)) ;
3622 if (!flush && wait_on_commit &&
3623 journal_list_still_alive(p_s_sb, commit_trans_id)) {
3624 flush_commit_list(p_s_sb, jl, 1) ;
3627 reiserfs_check_lock_depth(p_s_sb, "journal end2");