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/buffer_head.h>
54 #include <linux/workqueue.h>
55 #include <linux/writeback.h>
56 #include <linux/blkdev.h>
59 /* gets a struct reiserfs_journal_list * from a list head */
60 #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
62 #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
65 /* the number of mounted filesystems. This is used to decide when to
66 ** start and kill the commit workqueue
68 static int reiserfs_mounted_fs_count;
70 static struct workqueue_struct *commit_wq;
72 #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
74 #define BUFNR 64 /*read ahead */
76 /* cnode stat bits. Move these into reiserfs_fs.h */
78 #define BLOCK_FREED 2 /* this block was freed, and can't be written. */
79 #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
81 #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
82 #define BLOCK_DIRTIED 5
85 /* journal list state bits */
86 #define LIST_TOUCHED 1
88 #define LIST_COMMIT_PENDING 4 /* someone will commit this list */
90 /* flags for do_journal_end */
91 #define FLUSH_ALL 1 /* flush commit and real blocks */
92 #define COMMIT_NOW 2 /* end and commit this transaction */
93 #define WAIT 4 /* wait for the log blocks to hit the disk*/
95 static int do_journal_end(struct reiserfs_transaction_handle *,struct super_block *,unsigned long nblocks,int flags) ;
96 static int flush_journal_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
97 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) ;
98 static int can_dirty(struct reiserfs_journal_cnode *cn) ;
99 static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks);
100 static int release_journal_dev( struct super_block *super,
101 struct reiserfs_journal *journal );
102 static int dirty_one_transaction(struct super_block *s,
103 struct reiserfs_journal_list *jl);
104 static void flush_async_commits(void *p);
105 static void queue_log_writer(struct super_block *s);
107 /* values for join in do_journal_begin_r */
109 JBEGIN_REG = 0, /* regular journal begin */
110 JBEGIN_JOIN = 1, /* join the running transaction if at all possible */
111 JBEGIN_ABORT = 2, /* called from cleanup code, ignores aborted flag */
114 static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
115 struct super_block * p_s_sb,
116 unsigned long nblocks,int join);
118 static void init_journal_hash(struct super_block *p_s_sb) {
119 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
120 memset(journal->j_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
124 ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
125 ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
128 static int reiserfs_clean_and_file_buffer(struct buffer_head *bh) {
130 clear_buffer_dirty(bh);
131 clear_buffer_journal_test(bh);
136 static void disable_barrier(struct super_block *s)
138 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_BARRIER_FLUSH);
139 printk("reiserfs: disabling flush barriers on %s\n", reiserfs_bdevname(s));
142 static struct reiserfs_bitmap_node *
143 allocate_bitmap_node(struct super_block *p_s_sb) {
144 struct reiserfs_bitmap_node *bn ;
147 bn = reiserfs_kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS, p_s_sb) ;
151 bn->data = reiserfs_kmalloc(p_s_sb->s_blocksize, GFP_NOFS, p_s_sb) ;
153 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
157 memset(bn->data, 0, p_s_sb->s_blocksize) ;
158 INIT_LIST_HEAD(&bn->list) ;
162 static struct reiserfs_bitmap_node *
163 get_bitmap_node(struct super_block *p_s_sb) {
164 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
165 struct reiserfs_bitmap_node *bn = NULL;
166 struct list_head *entry = journal->j_bitmap_nodes.next ;
168 journal->j_used_bitmap_nodes++ ;
171 if(entry != &journal->j_bitmap_nodes) {
172 bn = list_entry(entry, struct reiserfs_bitmap_node, list) ;
174 memset(bn->data, 0, p_s_sb->s_blocksize) ;
175 journal->j_free_bitmap_nodes-- ;
178 bn = allocate_bitmap_node(p_s_sb) ;
185 static inline void free_bitmap_node(struct super_block *p_s_sb,
186 struct reiserfs_bitmap_node *bn) {
187 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
188 journal->j_used_bitmap_nodes-- ;
189 if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
190 reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
191 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
193 list_add(&bn->list, &journal->j_bitmap_nodes) ;
194 journal->j_free_bitmap_nodes++ ;
198 static void allocate_bitmap_nodes(struct super_block *p_s_sb) {
200 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
201 struct reiserfs_bitmap_node *bn = NULL ;
202 for (i = 0 ; i < REISERFS_MIN_BITMAP_NODES ; i++) {
203 bn = allocate_bitmap_node(p_s_sb) ;
205 list_add(&bn->list, &journal->j_bitmap_nodes) ;
206 journal->j_free_bitmap_nodes++ ;
208 break ; // this is ok, we'll try again when more are needed
213 static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
214 struct reiserfs_list_bitmap *jb) {
215 int bmap_nr = block / (p_s_sb->s_blocksize << 3) ;
216 int bit_nr = block % (p_s_sb->s_blocksize << 3) ;
218 if (!jb->bitmaps[bmap_nr]) {
219 jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb) ;
221 set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data) ;
225 static void cleanup_bitmap_list(struct super_block *p_s_sb,
226 struct reiserfs_list_bitmap *jb) {
228 if (jb->bitmaps == NULL)
231 for (i = 0 ; i < SB_BMAP_NR(p_s_sb) ; i++) {
232 if (jb->bitmaps[i]) {
233 free_bitmap_node(p_s_sb, jb->bitmaps[i]) ;
234 jb->bitmaps[i] = NULL ;
240 ** only call this on FS unmount.
242 static int free_list_bitmaps(struct super_block *p_s_sb,
243 struct reiserfs_list_bitmap *jb_array) {
245 struct reiserfs_list_bitmap *jb ;
246 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
248 jb->journal_list = NULL ;
249 cleanup_bitmap_list(p_s_sb, jb) ;
256 static int free_bitmap_nodes(struct super_block *p_s_sb) {
257 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
258 struct list_head *next = journal->j_bitmap_nodes.next ;
259 struct reiserfs_bitmap_node *bn ;
261 while(next != &journal->j_bitmap_nodes) {
262 bn = list_entry(next, struct reiserfs_bitmap_node, list) ;
264 reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb) ;
265 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb) ;
266 next = journal->j_bitmap_nodes.next ;
267 journal->j_free_bitmap_nodes-- ;
274 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
275 ** jb_array is the array to be filled in.
277 int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
278 struct reiserfs_list_bitmap *jb_array,
282 struct reiserfs_list_bitmap *jb ;
283 int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *) ;
285 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
287 jb->journal_list = NULL ;
288 jb->bitmaps = vmalloc( mem ) ;
290 reiserfs_warning(p_s_sb, "clm-2000, unable to allocate bitmaps for journal lists") ;
294 memset(jb->bitmaps, 0, mem) ;
297 free_list_bitmaps(p_s_sb, jb_array) ;
304 ** find an available list bitmap. If you can't find one, flush a commit list
307 static struct reiserfs_list_bitmap *
308 get_list_bitmap(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
310 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
311 struct reiserfs_list_bitmap *jb = NULL ;
313 for (j = 0 ; j < (JOURNAL_NUM_BITMAPS * 3) ; j++) {
314 i = journal->j_list_bitmap_index ;
315 journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS ;
316 jb = journal->j_list_bitmap + i ;
317 if (journal->j_list_bitmap[i].journal_list) {
318 flush_commit_list(p_s_sb, journal->j_list_bitmap[i].journal_list, 1) ;
319 if (!journal->j_list_bitmap[i].journal_list) {
326 if (jb->journal_list) { /* double check to make sure if flushed correctly */
329 jb->journal_list = jl ;
334 ** allocates a new chunk of X nodes, and links them all together as a list.
335 ** Uses the cnode->next and cnode->prev pointers
336 ** returns NULL on failure
338 static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes) {
339 struct reiserfs_journal_cnode *head ;
341 if (num_cnodes <= 0) {
344 head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
348 memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode)) ;
349 head[0].prev = NULL ;
350 head[0].next = head + 1 ;
351 for (i = 1 ; i < num_cnodes; i++) {
352 head[i].prev = head + (i - 1) ;
353 head[i].next = head + (i + 1) ; /* if last one, overwrite it after the if */
355 head[num_cnodes -1].next = NULL ;
360 ** pulls a cnode off the free list, or returns NULL on failure
362 static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb) {
363 struct reiserfs_journal_cnode *cn ;
364 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
366 reiserfs_check_lock_depth(p_s_sb, "get_cnode") ;
368 if (journal->j_cnode_free <= 0) {
371 journal->j_cnode_used++ ;
372 journal->j_cnode_free-- ;
373 cn = journal->j_cnode_free_list ;
378 cn->next->prev = NULL ;
380 journal->j_cnode_free_list = cn->next ;
381 memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ;
386 ** returns a cnode to the free list
388 static void free_cnode(struct super_block *p_s_sb, struct reiserfs_journal_cnode *cn) {
389 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
391 reiserfs_check_lock_depth(p_s_sb, "free_cnode") ;
393 journal->j_cnode_used-- ;
394 journal->j_cnode_free++ ;
395 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
396 cn->next = journal->j_cnode_free_list ;
397 if (journal->j_cnode_free_list) {
398 journal->j_cnode_free_list->prev = cn ;
400 cn->prev = NULL ; /* not needed with the memset, but I might kill the memset, and forget to do this */
401 journal->j_cnode_free_list = cn ;
404 static void clear_prepared_bits(struct buffer_head *bh) {
405 clear_buffer_journal_prepared (bh);
406 clear_buffer_journal_restore_dirty (bh);
409 /* utility function to force a BUG if it is called without the big
410 ** kernel lock held. caller is the string printed just before calling BUG()
412 void reiserfs_check_lock_depth(struct super_block *sb, char *caller) {
414 if (current->lock_depth < 0) {
415 reiserfs_panic (sb, "%s called without kernel lock held", caller) ;
422 /* return a cnode with same dev, block number and size in table, or null if not found */
423 static inline struct reiserfs_journal_cnode *
424 get_journal_hash_dev(struct super_block *sb,
425 struct reiserfs_journal_cnode **table,
428 struct reiserfs_journal_cnode *cn ;
429 cn = journal_hash(table, sb, bl) ;
431 if (cn->blocknr == bl && cn->sb == sb)
435 return (struct reiserfs_journal_cnode *)0 ;
439 ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
440 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
441 ** being overwritten by a replay after crashing.
443 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
444 ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
445 ** sure you never write the block without logging it.
447 ** next_zero_bit is a suggestion about the next block to try for find_forward.
448 ** when bl is rejected because it is set in a journal list bitmap, we search
449 ** for the next zero bit in the bitmap that rejected bl. Then, we return that
450 ** through next_zero_bit for find_forward to try.
452 ** Just because we return something in next_zero_bit does not mean we won't
453 ** reject it on the next call to reiserfs_in_journal
456 int reiserfs_in_journal(struct super_block *p_s_sb,
457 int bmap_nr, int bit_nr, int search_all,
458 b_blocknr_t *next_zero_bit) {
459 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
460 struct reiserfs_journal_cnode *cn ;
461 struct reiserfs_list_bitmap *jb ;
465 *next_zero_bit = 0 ; /* always start this at zero. */
467 PROC_INFO_INC( p_s_sb, journal.in_journal );
468 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
469 ** if we crash before the transaction that freed it commits, this transaction won't
470 ** have committed either, and the block will never be written
473 for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
474 PROC_INFO_INC( p_s_sb, journal.in_journal_bitmap );
475 jb = journal->j_list_bitmap + i ;
476 if (jb->journal_list && jb->bitmaps[bmap_nr] &&
477 test_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data)) {
478 *next_zero_bit = find_next_zero_bit((unsigned long *)
479 (jb->bitmaps[bmap_nr]->data),
480 p_s_sb->s_blocksize << 3, bit_nr+1) ;
486 bl = bmap_nr * (p_s_sb->s_blocksize << 3) + bit_nr;
487 /* is it in any old transactions? */
488 if (search_all && (cn = get_journal_hash_dev(p_s_sb, journal->j_list_hash_table, bl))) {
492 /* is it in the current transaction. This should never happen */
493 if ((cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, bl))) {
498 PROC_INFO_INC( p_s_sb, journal.in_journal_reusable );
503 /* insert cn into table
505 static inline void insert_journal_hash(struct reiserfs_journal_cnode **table, struct reiserfs_journal_cnode *cn) {
506 struct reiserfs_journal_cnode *cn_orig ;
508 cn_orig = journal_hash(table, cn->sb, cn->blocknr) ;
509 cn->hnext = cn_orig ;
512 cn_orig->hprev = cn ;
514 journal_hash(table, cn->sb, cn->blocknr) = cn ;
517 /* lock the current transaction */
518 inline static void lock_journal(struct super_block *p_s_sb) {
519 PROC_INFO_INC( p_s_sb, journal.lock_journal );
520 down(&SB_JOURNAL(p_s_sb)->j_lock);
523 /* unlock the current transaction */
524 inline static void unlock_journal(struct super_block *p_s_sb) {
525 up(&SB_JOURNAL(p_s_sb)->j_lock);
528 static inline void get_journal_list(struct reiserfs_journal_list *jl)
533 static inline void put_journal_list(struct super_block *s,
534 struct reiserfs_journal_list *jl)
536 if (jl->j_refcount < 1) {
537 reiserfs_panic (s, "trans id %lu, refcount at %d", jl->j_trans_id,
540 if (--jl->j_refcount == 0)
541 reiserfs_kfree(jl, sizeof(struct reiserfs_journal_list), s);
545 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
546 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
549 static void cleanup_freed_for_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl) {
551 struct reiserfs_list_bitmap *jb = jl->j_list_bitmap ;
553 cleanup_bitmap_list(p_s_sb, jb) ;
555 jl->j_list_bitmap->journal_list = NULL ;
556 jl->j_list_bitmap = NULL ;
559 static int journal_list_still_alive(struct super_block *s,
560 unsigned long trans_id)
562 struct reiserfs_journal *journal = SB_JOURNAL (s);
563 struct list_head *entry = &journal->j_journal_list;
564 struct reiserfs_journal_list *jl;
566 if (!list_empty(entry)) {
567 jl = JOURNAL_LIST_ENTRY(entry->next);
568 if (jl->j_trans_id <= trans_id) {
575 static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate) {
576 char b[BDEVNAME_SIZE];
578 if (buffer_journaled(bh)) {
579 reiserfs_warning(NULL, "clm-2084: pinned buffer %lu:%s sent to disk",
580 bh->b_blocknr, bdevname(bh->b_bdev, b)) ;
583 set_buffer_uptodate(bh) ;
585 clear_buffer_uptodate(bh) ;
590 static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate) {
592 set_buffer_uptodate(bh) ;
594 clear_buffer_uptodate(bh) ;
599 static void submit_logged_buffer(struct buffer_head *bh) {
601 bh->b_end_io = reiserfs_end_buffer_io_sync ;
602 clear_buffer_journal_new (bh);
603 clear_buffer_dirty(bh) ;
604 if (!test_clear_buffer_journal_test (bh))
606 if (!buffer_uptodate(bh))
608 submit_bh(WRITE, bh) ;
611 static void submit_ordered_buffer(struct buffer_head *bh) {
613 bh->b_end_io = reiserfs_end_ordered_io;
614 clear_buffer_dirty(bh) ;
615 if (!buffer_uptodate(bh))
617 submit_bh(WRITE, bh) ;
620 static int submit_barrier_buffer(struct buffer_head *bh) {
622 bh->b_end_io = reiserfs_end_ordered_io;
623 clear_buffer_dirty(bh) ;
624 if (!buffer_uptodate(bh))
626 return submit_bh(WRITE_BARRIER, bh) ;
629 static void check_barrier_completion(struct super_block *s,
630 struct buffer_head *bh) {
631 if (buffer_eopnotsupp(bh)) {
632 clear_buffer_eopnotsupp(bh);
634 set_buffer_uptodate(bh);
635 set_buffer_dirty(bh);
636 sync_dirty_buffer(bh);
640 #define CHUNK_SIZE 32
641 struct buffer_chunk {
642 struct buffer_head *bh[CHUNK_SIZE];
646 static void write_chunk(struct buffer_chunk *chunk) {
648 for (i = 0; i < chunk->nr ; i++) {
649 submit_logged_buffer(chunk->bh[i]) ;
654 static void write_ordered_chunk(struct buffer_chunk *chunk) {
656 for (i = 0; i < chunk->nr ; i++) {
657 submit_ordered_buffer(chunk->bh[i]) ;
662 static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
664 void (fn)(struct buffer_chunk *))
667 if (chunk->nr >= CHUNK_SIZE)
669 chunk->bh[chunk->nr++] = bh;
670 if (chunk->nr >= CHUNK_SIZE) {
682 static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
683 static struct reiserfs_jh *alloc_jh(void) {
684 struct reiserfs_jh *jh;
686 jh = kmalloc(sizeof(*jh), GFP_NOFS);
688 atomic_inc(&nr_reiserfs_jh);
696 * we want to free the jh when the buffer has been written
699 void reiserfs_free_jh(struct buffer_head *bh) {
700 struct reiserfs_jh *jh;
704 bh->b_private = NULL;
706 list_del_init(&jh->list);
708 if (atomic_read(&nr_reiserfs_jh) <= 0)
710 atomic_dec(&nr_reiserfs_jh);
715 static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
718 struct reiserfs_jh *jh;
721 spin_lock(&j->j_dirty_buffers_lock);
722 if (!bh->b_private) {
723 spin_unlock(&j->j_dirty_buffers_lock);
727 list_del_init(&jh->list);
732 spin_lock(&j->j_dirty_buffers_lock);
733 /* buffer must be locked for __add_jh, should be able to have
734 * two adds at the same time
741 jh->jl = j->j_current_jl;
743 list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
745 list_add_tail(&jh->list, &jh->jl->j_bh_list);
747 spin_unlock(&j->j_dirty_buffers_lock);
751 int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh) {
752 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
754 int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh) {
755 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
758 #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
759 static int write_ordered_buffers(spinlock_t *lock,
760 struct reiserfs_journal *j,
761 struct reiserfs_journal_list *jl,
762 struct list_head *list)
764 struct buffer_head *bh;
765 struct reiserfs_jh *jh;
766 int ret = j->j_errno;
767 struct buffer_chunk chunk;
768 struct list_head tmp;
769 INIT_LIST_HEAD(&tmp);
773 while(!list_empty(list)) {
774 jh = JH_ENTRY(list->next);
777 if (test_set_buffer_locked(bh)) {
778 if (!buffer_dirty(bh)) {
779 list_del_init(&jh->list);
780 list_add(&jh->list, &tmp);
785 write_ordered_chunk(&chunk);
791 if (buffer_dirty(bh)) {
792 list_del_init(&jh->list);
793 list_add(&jh->list, &tmp);
794 add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
796 reiserfs_free_jh(bh);
801 cond_resched_lock(lock);
805 write_ordered_chunk(&chunk);
808 while(!list_empty(&tmp)) {
809 jh = JH_ENTRY(tmp.prev);
812 reiserfs_free_jh(bh);
814 if (buffer_locked(bh)) {
819 if (!buffer_uptodate(bh)) {
823 cond_resched_lock(lock);
829 static int flush_older_commits(struct super_block *s, struct reiserfs_journal_list *jl) {
830 struct reiserfs_journal *journal = SB_JOURNAL (s);
831 struct reiserfs_journal_list *other_jl;
832 struct reiserfs_journal_list *first_jl;
833 struct list_head *entry;
834 unsigned long trans_id = jl->j_trans_id;
835 unsigned long other_trans_id;
836 unsigned long first_trans_id;
840 * first we walk backwards to find the oldest uncommitted transation
843 entry = jl->j_list.prev;
845 other_jl = JOURNAL_LIST_ENTRY(entry);
846 if (entry == &journal->j_journal_list ||
847 atomic_read(&other_jl->j_older_commits_done))
851 entry = other_jl->j_list.prev;
854 /* if we didn't find any older uncommitted transactions, return now */
855 if (first_jl == jl) {
859 first_trans_id = first_jl->j_trans_id;
861 entry = &first_jl->j_list;
863 other_jl = JOURNAL_LIST_ENTRY(entry);
864 other_trans_id = other_jl->j_trans_id;
866 if (other_trans_id < trans_id) {
867 if (atomic_read(&other_jl->j_commit_left) != 0) {
868 flush_commit_list(s, other_jl, 0);
870 /* list we were called with is gone, return */
871 if (!journal_list_still_alive(s, trans_id))
874 /* the one we just flushed is gone, this means all
875 * older lists are also gone, so first_jl is no longer
876 * valid either. Go back to the beginning.
878 if (!journal_list_still_alive(s, other_trans_id)) {
883 if (entry == &journal->j_journal_list)
891 int reiserfs_async_progress_wait(struct super_block *s) {
893 struct reiserfs_journal *j = SB_JOURNAL(s);
894 if (atomic_read(&j->j_async_throttle))
895 blk_congestion_wait(WRITE, HZ/10);
900 ** if this journal list still has commit blocks unflushed, send them to disk.
902 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
903 ** Before the commit block can by written, every other log block must be safely on disk
906 static int flush_commit_list(struct super_block *s, struct reiserfs_journal_list *jl, int flushall) {
909 struct buffer_head *tbh = NULL ;
910 unsigned long trans_id = jl->j_trans_id;
911 struct reiserfs_journal *journal = SB_JOURNAL (s);
915 reiserfs_check_lock_depth(s, "flush_commit_list") ;
917 if (atomic_read(&jl->j_older_commits_done)) {
921 /* before we can put our commit blocks on disk, we have to make sure everyone older than
924 BUG_ON (jl->j_len <= 0);
925 BUG_ON (trans_id == journal->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 BUG_ON (jl->j_trans_id == 0);
943 /* this commit is done, exit */
944 if (atomic_read(&(jl->j_commit_left)) <= 0) {
946 atomic_set(&(jl->j_older_commits_done), 1) ;
948 up(&jl->j_commit_lock);
952 if (!list_empty(&jl->j_bh_list)) {
954 write_ordered_buffers(&journal->j_dirty_buffers_lock,
955 journal, jl, &jl->j_bh_list);
958 BUG_ON (!list_empty(&jl->j_bh_list));
960 * for the description block and all the log blocks, submit any buffers
961 * that haven't already reached the disk
963 atomic_inc(&journal->j_async_throttle);
964 for (i = 0 ; i < (jl->j_len + 1) ; i++) {
965 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start+i) %
966 SB_ONDISK_JOURNAL_SIZE(s);
967 tbh = journal_find_get_block(s, bn) ;
968 if (buffer_dirty(tbh)) /* redundant, ll_rw_block() checks */
969 ll_rw_block(WRITE, 1, &tbh) ;
972 atomic_dec(&journal->j_async_throttle);
974 /* wait on everything written so far before writing the commit
975 * if we are in barrier mode, send the commit down now
977 barrier = reiserfs_barrier_flush(s);
980 lock_buffer(jl->j_commit_bh);
981 ret = submit_barrier_buffer(jl->j_commit_bh);
982 if (ret == -EOPNOTSUPP) {
983 set_buffer_uptodate(jl->j_commit_bh);
988 for (i = 0 ; i < (jl->j_len + 1) ; i++) {
989 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
990 (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s) ;
991 tbh = journal_find_get_block(s, bn) ;
992 wait_on_buffer(tbh) ;
993 // since we're using ll_rw_blk above, it might have skipped over
994 // a locked buffer. Double check here
996 if (buffer_dirty(tbh)) /* redundant, sync_dirty_buffer() checks */
997 sync_dirty_buffer(tbh);
998 if (unlikely (!buffer_uptodate(tbh))) {
999 #ifdef CONFIG_REISERFS_CHECK
1000 reiserfs_warning(s, "journal-601, buffer write failed") ;
1004 put_bh(tbh) ; /* once for journal_find_get_block */
1005 put_bh(tbh) ; /* once due to original getblk in do_journal_end */
1006 atomic_dec(&(jl->j_commit_left)) ;
1009 BUG_ON (atomic_read(&(jl->j_commit_left)) != 1);
1012 if (buffer_dirty(jl->j_commit_bh))
1014 mark_buffer_dirty(jl->j_commit_bh) ;
1015 sync_dirty_buffer(jl->j_commit_bh) ;
1017 wait_on_buffer(jl->j_commit_bh);
1019 check_barrier_completion(s, jl->j_commit_bh);
1021 /* If there was a write error in the journal - we can't commit this
1022 * transaction - it will be invalid and, if successful, will just end
1023 * up propogating the write error out to the filesystem. */
1024 if (unlikely (!buffer_uptodate(jl->j_commit_bh))) {
1025 #ifdef CONFIG_REISERFS_CHECK
1026 reiserfs_warning(s, "journal-615: buffer write failed") ;
1030 bforget(jl->j_commit_bh) ;
1031 if (journal->j_last_commit_id != 0 &&
1032 (jl->j_trans_id - journal->j_last_commit_id) != 1) {
1033 reiserfs_warning(s, "clm-2200: last commit %lu, current %lu",
1034 journal->j_last_commit_id,
1037 journal->j_last_commit_id = jl->j_trans_id;
1039 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1040 cleanup_freed_for_journal_list(s, jl) ;
1042 retval = retval ? retval : journal->j_errno;
1044 /* mark the metadata dirty */
1046 dirty_one_transaction(s, jl);
1047 atomic_dec(&(jl->j_commit_left)) ;
1050 atomic_set(&(jl->j_older_commits_done), 1) ;
1052 up(&jl->j_commit_lock);
1054 put_journal_list(s, jl);
1057 reiserfs_abort (s, retval, "Journal write error in %s", __FUNCTION__);
1062 ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1063 ** returns NULL if it can't find anything
1065 static struct reiserfs_journal_list *find_newer_jl_for_cn(struct reiserfs_journal_cnode *cn) {
1066 struct super_block *sb = cn->sb;
1067 b_blocknr_t blocknr = cn->blocknr ;
1071 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
1079 static void remove_journal_hash(struct super_block *, struct reiserfs_journal_cnode **,
1080 struct reiserfs_journal_list *, unsigned long, int);
1083 ** once all the real blocks have been flushed, it is safe to remove them from the
1084 ** journal list for this transaction. Aside from freeing the cnode, this also allows the
1085 ** block to be reallocated for data blocks if it had been deleted.
1087 static void remove_all_from_journal_list(struct super_block *p_s_sb, struct reiserfs_journal_list *jl, int debug) {
1088 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
1089 struct reiserfs_journal_cnode *cn, *last ;
1090 cn = jl->j_realblock ;
1092 /* which is better, to lock once around the whole loop, or
1093 ** to lock for each call to remove_journal_hash?
1096 if (cn->blocknr != 0) {
1098 reiserfs_warning (p_s_sb, "block %u, bh is %d, state %ld", cn->blocknr,
1099 cn->bh ? 1: 0, cn->state) ;
1102 remove_journal_hash(p_s_sb, journal->j_list_hash_table, jl, cn->blocknr, 1) ;
1106 free_cnode(p_s_sb, last) ;
1108 jl->j_realblock = NULL ;
1112 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1113 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1114 ** releasing blocks in this transaction for reuse as data blocks.
1115 ** called by flush_journal_list, before it calls remove_all_from_journal_list
1118 static int _update_journal_header_block(struct super_block *p_s_sb, unsigned long offset, unsigned long trans_id) {
1119 struct reiserfs_journal_header *jh ;
1120 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
1122 if (reiserfs_is_journal_aborted (journal))
1125 if (trans_id >= journal->j_last_flush_trans_id) {
1126 if (buffer_locked((journal->j_header_bh))) {
1127 wait_on_buffer((journal->j_header_bh)) ;
1128 if (unlikely (!buffer_uptodate(journal->j_header_bh))) {
1129 #ifdef CONFIG_REISERFS_CHECK
1130 reiserfs_warning (p_s_sb, "journal-699: buffer write failed") ;
1135 journal->j_last_flush_trans_id = trans_id ;
1136 journal->j_first_unflushed_offset = offset ;
1137 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data) ;
1138 jh->j_last_flush_trans_id = cpu_to_le32(trans_id) ;
1139 jh->j_first_unflushed_offset = cpu_to_le32(offset) ;
1140 jh->j_mount_id = cpu_to_le32(journal->j_mount_id) ;
1142 if (reiserfs_barrier_flush(p_s_sb)) {
1144 lock_buffer(journal->j_header_bh);
1145 ret = submit_barrier_buffer(journal->j_header_bh);
1146 if (ret == -EOPNOTSUPP) {
1147 set_buffer_uptodate(journal->j_header_bh);
1148 disable_barrier(p_s_sb);
1151 wait_on_buffer(journal->j_header_bh);
1152 check_barrier_completion(p_s_sb, journal->j_header_bh);
1155 set_buffer_dirty(journal->j_header_bh) ;
1156 sync_dirty_buffer(journal->j_header_bh) ;
1158 if (!buffer_uptodate(journal->j_header_bh)) {
1159 reiserfs_warning (p_s_sb, "journal-837: IO error during journal replay");
1166 static int update_journal_header_block(struct super_block *p_s_sb,
1167 unsigned long offset,
1168 unsigned long trans_id) {
1169 return _update_journal_header_block(p_s_sb, offset, trans_id);
1172 ** flush any and all journal lists older than you are
1173 ** can only be called from flush_journal_list
1175 static int flush_older_journal_lists(struct super_block *p_s_sb,
1176 struct reiserfs_journal_list *jl)
1178 struct list_head *entry;
1179 struct reiserfs_journal_list *other_jl ;
1180 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
1181 unsigned long trans_id = jl->j_trans_id;
1183 /* we know we are the only ones flushing things, no extra race
1184 * protection is required.
1187 entry = journal->j_journal_list.next;
1189 if (entry == &journal->j_journal_list)
1191 other_jl = JOURNAL_LIST_ENTRY(entry);
1192 if (other_jl->j_trans_id < trans_id) {
1193 BUG_ON (other_jl->j_refcount <= 0);
1194 /* do not flush all */
1195 flush_journal_list(p_s_sb, other_jl, 0) ;
1197 /* other_jl is now deleted from the list */
1203 static void del_from_work_list(struct super_block *s,
1204 struct reiserfs_journal_list *jl) {
1205 struct reiserfs_journal *journal = SB_JOURNAL (s);
1206 if (!list_empty(&jl->j_working_list)) {
1207 list_del_init(&jl->j_working_list);
1208 journal->j_num_work_lists--;
1212 /* flush a journal list, both commit and real blocks
1214 ** always set flushall to 1, unless you are calling from inside
1215 ** flush_journal_list
1217 ** IMPORTANT. This can only be called while there are no journal writers,
1218 ** and the journal is locked. That means it can only be called from
1219 ** do_journal_end, or by journal_release
1221 static int flush_journal_list(struct super_block *s,
1222 struct reiserfs_journal_list *jl, int flushall) {
1223 struct reiserfs_journal_list *pjl ;
1224 struct reiserfs_journal_cnode *cn, *last ;
1228 struct buffer_head *saved_bh ;
1229 unsigned long j_len_saved = jl->j_len ;
1230 struct reiserfs_journal *journal = SB_JOURNAL (s);
1233 BUG_ON (j_len_saved <= 0);
1235 if (atomic_read(&journal->j_wcount) != 0) {
1236 reiserfs_warning(s, "clm-2048: flush_journal_list called with wcount %d",
1237 atomic_read(&journal->j_wcount)) ;
1239 BUG_ON (jl->j_trans_id == 0);
1241 /* if flushall == 0, the lock is already held */
1243 down(&journal->j_flush_sem);
1244 } else if (!down_trylock(&journal->j_flush_sem)) {
1249 if (j_len_saved > journal->j_trans_max) {
1250 reiserfs_panic(s, "journal-715: flush_journal_list, length is %lu, trans id %lu\n", j_len_saved, jl->j_trans_id);
1254 /* if all the work is already done, get out of here */
1255 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1256 atomic_read(&(jl->j_commit_left)) <= 0) {
1257 goto flush_older_and_return ;
1260 /* start by putting the commit list on disk. This will also flush
1261 ** the commit lists of any olders transactions
1263 flush_commit_list(s, jl, 1) ;
1265 if (!(jl->j_state & LIST_DIRTY) && !reiserfs_is_journal_aborted (journal))
1268 /* are we done now? */
1269 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1270 atomic_read(&(jl->j_commit_left)) <= 0) {
1271 goto flush_older_and_return ;
1274 /* loop through each cnode, see if we need to write it,
1275 ** or wait on a more recent transaction, or just ignore it
1277 if (atomic_read(&(journal->j_wcount)) != 0) {
1278 reiserfs_panic(s, "journal-844: panic journal list is flushing, wcount is not 0\n") ;
1280 cn = jl->j_realblock ;
1285 /* blocknr of 0 is no longer in the hash, ignore it */
1286 if (cn->blocknr == 0) {
1290 /* This transaction failed commit. Don't write out to the disk */
1291 if (!(jl->j_state & LIST_DIRTY))
1294 pjl = find_newer_jl_for_cn(cn) ;
1295 /* the order is important here. We check pjl to make sure we
1296 ** don't clear BH_JDirty_wait if we aren't the one writing this
1299 if (!pjl && cn->bh) {
1302 /* we do this to make sure nobody releases the buffer while
1303 ** we are working with it
1307 if (buffer_journal_dirty(saved_bh)) {
1308 BUG_ON (!can_dirty (cn));
1311 } else if (can_dirty(cn)) {
1312 /* everything with !pjl && jwait should be writable */
1317 /* if someone has this block in a newer transaction, just make
1318 ** sure they are commited, and don't try writing it to disk
1321 if (atomic_read(&pjl->j_commit_left))
1322 flush_commit_list(s, pjl, 1) ;
1326 /* bh == NULL when the block got to disk on its own, OR,
1327 ** the block got freed in a future transaction
1329 if (saved_bh == NULL) {
1333 /* this should never happen. kupdate_one_transaction has this list
1334 ** locked while it works, so we should never see a buffer here that
1335 ** is not marked JDirty_wait
1337 if ((!was_jwait) && !buffer_locked(saved_bh)) {
1338 reiserfs_warning (s, "journal-813: BAD! buffer %llu %cdirty %cjwait, "
1339 "not in a newer tranasction",
1340 (unsigned long long)saved_bh->b_blocknr,
1341 was_dirty ? ' ' : '!', was_jwait ? ' ' : '!') ;
1344 /* we inc again because saved_bh gets decremented at free_cnode */
1346 set_bit(BLOCK_NEEDS_FLUSH, &cn->state) ;
1347 lock_buffer(saved_bh);
1348 BUG_ON (cn->blocknr != saved_bh->b_blocknr);
1349 if (buffer_dirty(saved_bh))
1350 submit_logged_buffer(saved_bh) ;
1352 unlock_buffer(saved_bh);
1355 reiserfs_warning (s, "clm-2082: Unable to flush buffer %llu in %s",
1356 (unsigned long long)saved_bh->b_blocknr, __FUNCTION__);
1362 /* we incremented this to keep others from taking the buffer head away */
1364 if (atomic_read(&(saved_bh->b_count)) < 0) {
1365 reiserfs_warning (s, "journal-945: saved_bh->b_count < 0");
1370 cn = jl->j_realblock ;
1372 if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1374 reiserfs_panic(s, "journal-1011: cn->bh is NULL\n") ;
1376 wait_on_buffer(cn->bh) ;
1378 reiserfs_panic(s, "journal-1012: cn->bh is NULL\n") ;
1380 if (unlikely (!buffer_uptodate(cn->bh))) {
1381 #ifdef CONFIG_REISERFS_CHECK
1382 reiserfs_warning(s, "journal-949: buffer write failed\n") ;
1386 /* note, we must clear the JDirty_wait bit after the up to date
1387 ** check, otherwise we race against our flushpage routine
1389 BUG_ON (!test_clear_buffer_journal_dirty (cn->bh));
1391 /* undo the inc from journal_mark_dirty */
1400 reiserfs_abort (s, -EIO, "Write error while pushing transaction to disk in %s", __FUNCTION__);
1401 flush_older_and_return:
1404 /* before we can update the journal header block, we _must_ flush all
1405 ** real blocks from all older transactions to disk. This is because
1406 ** once the header block is updated, this transaction will not be
1407 ** replayed after a crash
1410 flush_older_journal_lists(s, jl);
1413 err = journal->j_errno;
1414 /* before we can remove everything from the hash tables for this
1415 ** transaction, we must make sure it can never be replayed
1417 ** since we are only called from do_journal_end, we know for sure there
1418 ** are no allocations going on while we are flushing journal lists. So,
1419 ** we only need to update the journal header block for the last list
1422 if (!err && flushall) {
1423 err = update_journal_header_block(s, (jl->j_start + jl->j_len + 2) % SB_ONDISK_JOURNAL_SIZE(s), jl->j_trans_id) ;
1425 reiserfs_abort (s, -EIO, "Write error while updating journal header in %s", __FUNCTION__);
1427 remove_all_from_journal_list(s, jl, 0) ;
1428 list_del_init(&jl->j_list);
1429 journal->j_num_lists--;
1430 del_from_work_list(s, jl);
1432 if (journal->j_last_flush_id != 0 &&
1433 (jl->j_trans_id - journal->j_last_flush_id) != 1) {
1434 reiserfs_warning(s, "clm-2201: last flush %lu, current %lu",
1435 journal->j_last_flush_id,
1438 journal->j_last_flush_id = jl->j_trans_id;
1440 /* not strictly required since we are freeing the list, but it should
1441 * help find code using dead lists later on
1444 atomic_set(&(jl->j_nonzerolen), 0) ;
1446 jl->j_realblock = NULL ;
1447 jl->j_commit_bh = NULL ;
1448 jl->j_trans_id = 0 ;
1450 put_journal_list(s, jl);
1452 up(&journal->j_flush_sem);
1456 static int write_one_transaction(struct super_block *s,
1457 struct reiserfs_journal_list *jl,
1458 struct buffer_chunk *chunk)
1460 struct reiserfs_journal_cnode *cn;
1463 jl->j_state |= LIST_TOUCHED;
1464 del_from_work_list(s, jl);
1465 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
1469 cn = jl->j_realblock ;
1471 /* if the blocknr == 0, this has been cleared from the hash,
1474 if (cn->blocknr == 0) {
1477 if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
1478 struct buffer_head *tmp_bh;
1479 /* we can race against journal_mark_freed when we try
1480 * to lock_buffer(cn->bh), so we have to inc the buffer
1481 * count, and recheck things after locking
1485 lock_buffer(tmp_bh);
1486 if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
1487 if (!buffer_journal_dirty(tmp_bh) ||
1488 buffer_journal_prepared(tmp_bh))
1490 add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
1493 /* note, cn->bh might be null now */
1494 unlock_buffer(tmp_bh);
1505 /* used by flush_commit_list */
1506 static int dirty_one_transaction(struct super_block *s,
1507 struct reiserfs_journal_list *jl)
1509 struct reiserfs_journal_cnode *cn;
1510 struct reiserfs_journal_list *pjl;
1513 jl->j_state |= LIST_DIRTY;
1514 cn = jl->j_realblock ;
1516 /* look for a more recent transaction that logged this
1517 ** buffer. Only the most recent transaction with a buffer in
1518 ** it is allowed to send that buffer to disk
1520 pjl = find_newer_jl_for_cn(cn) ;
1521 if (!pjl && cn->blocknr && cn->bh && buffer_journal_dirty(cn->bh))
1523 BUG_ON (!can_dirty(cn));
1524 /* if the buffer is prepared, it will either be logged
1525 * or restored. If restored, we need to make sure
1526 * it actually gets marked dirty
1528 clear_buffer_journal_new (cn->bh);
1529 if (buffer_journal_prepared (cn->bh)) {
1530 set_buffer_journal_restore_dirty (cn->bh);
1532 set_buffer_journal_test (cn->bh);
1533 mark_buffer_dirty(cn->bh);
1541 static int kupdate_transactions(struct super_block *s,
1542 struct reiserfs_journal_list *jl,
1543 struct reiserfs_journal_list **next_jl,
1544 unsigned long *next_trans_id,
1549 int transactions_flushed = 0;
1550 unsigned long orig_trans_id = jl->j_trans_id;
1551 struct buffer_chunk chunk;
1552 struct list_head *entry;
1553 struct reiserfs_journal *journal = SB_JOURNAL (s);
1556 down(&journal->j_flush_sem);
1557 if (!journal_list_still_alive(s, orig_trans_id)) {
1561 /* we've got j_flush_sem held, nobody is going to delete any
1562 * of these lists out from underneath us
1564 while((num_trans && transactions_flushed < num_trans) ||
1565 (!num_trans && written < num_blocks)) {
1567 if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
1568 atomic_read(&jl->j_commit_left) || !(jl->j_state & LIST_DIRTY))
1570 del_from_work_list(s, jl);
1573 ret = write_one_transaction(s, jl, &chunk);
1577 transactions_flushed++;
1579 entry = jl->j_list.next;
1582 if (entry == &journal->j_journal_list) {
1585 jl = JOURNAL_LIST_ENTRY(entry);
1587 /* don't bother with older transactions */
1588 if (jl->j_trans_id <= orig_trans_id)
1592 write_chunk(&chunk);
1596 up(&journal->j_flush_sem);
1600 /* for o_sync and fsync heavy applications, they tend to use
1601 ** all the journa list slots with tiny transactions. These
1602 ** trigger lots and lots of calls to update the header block, which
1603 ** adds seeks and slows things down.
1605 ** This function tries to clear out a large chunk of the journal lists
1606 ** at once, which makes everything faster since only the newest journal
1607 ** list updates the header block
1609 static int flush_used_journal_lists(struct super_block *s,
1610 struct reiserfs_journal_list *jl) {
1611 unsigned long len = 0;
1612 unsigned long cur_len;
1616 struct reiserfs_journal_list *tjl;
1617 struct reiserfs_journal_list *flush_jl;
1618 unsigned long trans_id;
1619 struct reiserfs_journal *journal = SB_JOURNAL (s);
1621 flush_jl = tjl = jl;
1623 /* in data logging mode, try harder to flush a lot of blocks */
1624 if (reiserfs_data_log(s))
1626 /* flush for 256 transactions or limit blocks, whichever comes first */
1627 for(i = 0 ; i < 256 && len < limit ; i++) {
1628 if (atomic_read(&tjl->j_commit_left) ||
1629 tjl->j_trans_id < jl->j_trans_id) {
1632 cur_len = atomic_read(&tjl->j_nonzerolen);
1634 tjl->j_state &= ~LIST_TOUCHED;
1638 if (tjl->j_list.next == &journal->j_journal_list)
1640 tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
1642 /* try to find a group of blocks we can flush across all the
1643 ** transactions, but only bother if we've actually spanned
1644 ** across multiple lists
1646 if (flush_jl != jl) {
1647 ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
1649 flush_journal_list(s, flush_jl, 1);
1654 ** removes any nodes in table with name block and dev as bh.
1655 ** only touchs the hnext and hprev pointers.
1657 void remove_journal_hash(struct super_block *sb,
1658 struct reiserfs_journal_cnode **table,
1659 struct reiserfs_journal_list *jl,
1660 unsigned long block, int remove_freed)
1662 struct reiserfs_journal_cnode *cur ;
1663 struct reiserfs_journal_cnode **head ;
1665 head= &(journal_hash(table, sb, block)) ;
1671 if (cur->blocknr == block && cur->sb == sb && (jl == NULL || jl == cur->jlist) &&
1672 (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1674 cur->hnext->hprev = cur->hprev ;
1677 cur->hprev->hnext = cur->hnext ;
1679 *head = cur->hnext ;
1684 if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1685 atomic_dec(&(cur->jlist->j_nonzerolen)) ;
1693 static void free_journal_ram(struct super_block *p_s_sb) {
1694 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1695 reiserfs_kfree(journal->j_current_jl,
1696 sizeof(struct reiserfs_journal_list), p_s_sb);
1697 journal->j_num_lists--;
1699 vfree(journal->j_cnode_free_orig) ;
1700 free_list_bitmaps(p_s_sb, journal->j_list_bitmap) ;
1701 free_bitmap_nodes(p_s_sb) ; /* must be after free_list_bitmaps */
1702 if (journal->j_header_bh) {
1703 brelse(journal->j_header_bh) ;
1705 /* j_header_bh is on the journal dev, make sure not to release the journal
1706 * dev until we brelse j_header_bh
1708 release_journal_dev(p_s_sb, journal);
1713 ** call on unmount. Only set error to 1 if you haven't made your way out
1714 ** of read_super() yet. Any other caller must keep error at 0.
1716 static int do_journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, int error) {
1717 struct reiserfs_transaction_handle myth ;
1719 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1721 /* we only want to flush out transactions if we were called with error == 0
1723 if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
1724 /* end the current trans */
1725 BUG_ON (!th->t_trans_id);
1726 do_journal_end(th, p_s_sb,10, FLUSH_ALL) ;
1728 /* make sure something gets logged to force our way into the flush code */
1729 if (!journal_join(&myth, p_s_sb, 1)) {
1730 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
1731 journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
1732 do_journal_end(&myth, p_s_sb,1, FLUSH_ALL) ;
1737 /* this also catches errors during the do_journal_end above */
1738 if (!error && reiserfs_is_journal_aborted(journal)) {
1739 memset(&myth, 0, sizeof(myth));
1740 if (!journal_join_abort(&myth, p_s_sb, 1)) {
1741 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
1742 journal_mark_dirty(&myth, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
1743 do_journal_end(&myth, p_s_sb, 1, FLUSH_ALL) ;
1747 reiserfs_mounted_fs_count-- ;
1748 /* wait for all commits to finish */
1749 cancel_delayed_work(&SB_JOURNAL(p_s_sb)->j_work);
1750 flush_workqueue(commit_wq);
1751 if (!reiserfs_mounted_fs_count) {
1752 destroy_workqueue(commit_wq);
1756 free_journal_ram(p_s_sb) ;
1762 ** call on unmount. flush all journal trans, release all alloc'd ram
1764 int journal_release(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1765 return do_journal_release(th, p_s_sb, 0) ;
1768 ** only call from an error condition inside reiserfs_read_super!
1770 int journal_release_error(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb) {
1771 return do_journal_release(th, p_s_sb, 1) ;
1774 /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
1775 static int journal_compare_desc_commit(struct super_block *p_s_sb, struct reiserfs_journal_desc *desc,
1776 struct reiserfs_journal_commit *commit) {
1777 if (get_commit_trans_id (commit) != get_desc_trans_id (desc) ||
1778 get_commit_trans_len (commit) != get_desc_trans_len (desc) ||
1779 get_commit_trans_len (commit) > SB_JOURNAL(p_s_sb)->j_trans_max ||
1780 get_commit_trans_len (commit) <= 0
1786 /* returns 0 if it did not find a description block
1787 ** returns -1 if it found a corrupt commit block
1788 ** returns 1 if both desc and commit were valid
1790 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) {
1791 struct reiserfs_journal_desc *desc ;
1792 struct reiserfs_journal_commit *commit ;
1793 struct buffer_head *c_bh ;
1794 unsigned long offset ;
1799 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1800 if (get_desc_trans_len(desc) > 0 && !memcmp(get_journal_desc_magic (d_bh), JOURNAL_DESC_MAGIC, 8)) {
1801 if (oldest_invalid_trans_id && *oldest_invalid_trans_id && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
1802 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-986: transaction "
1803 "is valid returning because trans_id %d is greater than "
1804 "oldest_invalid %lu", get_desc_trans_id(desc),
1805 *oldest_invalid_trans_id);
1808 if (newest_mount_id && *newest_mount_id > get_desc_mount_id (desc)) {
1809 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1087: transaction "
1810 "is valid returning because mount_id %d is less than "
1811 "newest_mount_id %lu", get_desc_mount_id (desc),
1815 if ( get_desc_trans_len(desc) > SB_JOURNAL(p_s_sb)->j_trans_max ) {
1816 reiserfs_warning(p_s_sb, "journal-2018: Bad transaction length %d encountered, ignoring transaction", get_desc_trans_len(desc));
1819 offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1821 /* ok, we have a journal description block, lets see if the transaction was valid */
1822 c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1823 ((offset + get_desc_trans_len(desc) + 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
1826 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1827 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1828 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1829 "journal_transaction_is_valid, commit offset %ld had bad "
1830 "time %d or length %d",
1831 c_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1832 get_commit_trans_id (commit),
1833 get_commit_trans_len(commit));
1835 if (oldest_invalid_trans_id) {
1836 *oldest_invalid_trans_id = get_desc_trans_id(desc) ;
1837 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1004: "
1838 "transaction_is_valid setting oldest invalid trans_id "
1839 "to %d", get_desc_trans_id(desc)) ;
1844 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1006: found valid "
1845 "transaction start offset %llu, len %d id %d",
1846 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1847 get_desc_trans_len(desc), get_desc_trans_id(desc)) ;
1854 static void brelse_array(struct buffer_head **heads, int num) {
1856 for (i = 0 ; i < num ; i++) {
1862 ** given the start, and values for the oldest acceptable transactions,
1863 ** this either reads in a replays a transaction, or returns because the transaction
1864 ** is invalid, or too old.
1866 static int journal_read_transaction(struct super_block *p_s_sb, unsigned long cur_dblock, unsigned long oldest_start,
1867 unsigned long oldest_trans_id, unsigned long newest_mount_id) {
1868 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
1869 struct reiserfs_journal_desc *desc ;
1870 struct reiserfs_journal_commit *commit ;
1871 unsigned long trans_id = 0 ;
1872 struct buffer_head *c_bh ;
1873 struct buffer_head *d_bh ;
1874 struct buffer_head **log_blocks = NULL ;
1875 struct buffer_head **real_blocks = NULL ;
1876 unsigned long trans_offset ;
1880 d_bh = journal_bread(p_s_sb, cur_dblock) ;
1883 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
1884 trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
1885 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
1886 "journal_read_transaction, offset %llu, len %d mount_id %d",
1887 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1888 get_desc_trans_len(desc), get_desc_mount_id(desc)) ;
1889 if (get_desc_trans_id(desc) < oldest_trans_id) {
1890 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
1891 "journal_read_trans skipping because %lu is too old",
1892 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)) ;
1896 if (get_desc_mount_id(desc) != newest_mount_id) {
1897 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
1898 "journal_read_trans skipping because %d is != "
1899 "newest_mount_id %lu", get_desc_mount_id(desc),
1904 c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1905 ((trans_offset + get_desc_trans_len(desc) + 1) %
1906 SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
1911 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
1912 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1913 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal_read_transaction, "
1914 "commit offset %llu had bad time %d or length %d",
1915 c_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1916 get_commit_trans_id(commit), get_commit_trans_len(commit));
1921 trans_id = get_desc_trans_id(desc) ;
1922 /* now we know we've got a good transaction, and it was inside the valid time ranges */
1923 log_blocks = reiserfs_kmalloc(get_desc_trans_len(desc) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
1924 real_blocks = reiserfs_kmalloc(get_desc_trans_len(desc) * sizeof(struct buffer_head *), GFP_NOFS, p_s_sb) ;
1925 if (!log_blocks || !real_blocks) {
1928 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1929 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1930 reiserfs_warning(p_s_sb, "journal-1169: kmalloc failed, unable to mount FS") ;
1933 /* get all the buffer heads */
1934 trans_half = journal_trans_half (p_s_sb->s_blocksize) ;
1935 for(i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1936 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));
1937 if (i < trans_half) {
1938 real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(desc->j_realblock[i])) ;
1940 real_blocks[i] = sb_getblk(p_s_sb, le32_to_cpu(commit->j_realblock[i - trans_half])) ;
1942 if ( real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(p_s_sb) ) {
1943 reiserfs_warning(p_s_sb, "journal-1207: REPLAY FAILURE fsck required! Block to replay is outside of filesystem");
1946 /* make sure we don't try to replay onto log or reserved area */
1947 if (is_block_in_log_or_reserved_area(p_s_sb, real_blocks[i]->b_blocknr)) {
1948 reiserfs_warning(p_s_sb, "journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block") ;
1950 brelse_array(log_blocks, i) ;
1951 brelse_array(real_blocks, i) ;
1954 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1955 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1959 /* read in the log blocks, memcpy to the corresponding real block */
1960 ll_rw_block(READ, get_desc_trans_len(desc), log_blocks) ;
1961 for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1962 wait_on_buffer(log_blocks[i]) ;
1963 if (!buffer_uptodate(log_blocks[i])) {
1964 reiserfs_warning(p_s_sb, "journal-1212: REPLAY FAILURE fsck required! buffer write failed") ;
1965 brelse_array(log_blocks + i, get_desc_trans_len(desc) - i) ;
1966 brelse_array(real_blocks, get_desc_trans_len(desc)) ;
1969 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1970 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1973 memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data, real_blocks[i]->b_size) ;
1974 set_buffer_uptodate(real_blocks[i]) ;
1975 brelse(log_blocks[i]) ;
1977 /* flush out the real blocks */
1978 for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1979 set_buffer_dirty(real_blocks[i]) ;
1980 ll_rw_block(WRITE, 1, real_blocks + i) ;
1982 for (i = 0 ; i < get_desc_trans_len(desc) ; i++) {
1983 wait_on_buffer(real_blocks[i]) ;
1984 if (!buffer_uptodate(real_blocks[i])) {
1985 reiserfs_warning(p_s_sb, "journal-1226: REPLAY FAILURE, fsck required! buffer write failed") ;
1986 brelse_array(real_blocks + i, get_desc_trans_len(desc) - i) ;
1989 reiserfs_kfree(log_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1990 reiserfs_kfree(real_blocks, get_desc_trans_len(desc) * sizeof(struct buffer_head *), p_s_sb) ;
1993 brelse(real_blocks[i]) ;
1995 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)) ;
1996 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1095: setting journal "
1997 "start to offset %ld",
1998 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)) ;
2000 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
2001 journal->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
2002 journal->j_last_flush_trans_id = trans_id ;
2003 journal->j_trans_id = trans_id + 1;
2006 reiserfs_kfree(log_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
2007 reiserfs_kfree(real_blocks, le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *), p_s_sb) ;
2011 /* This function reads blocks starting from block and to max_block of bufsize
2012 size (but no more than BUFNR blocks at a time). This proved to improve
2013 mounting speed on self-rebuilding raid5 arrays at least.
2014 Right now it is only used from journal code. But later we might use it
2016 Note: Do not use journal_getblk/sb_getblk functions here! */
2017 static struct buffer_head * reiserfs_breada (struct block_device *dev, int block, int bufsize,
2018 unsigned int max_block)
2020 struct buffer_head * bhlist[BUFNR];
2021 unsigned int blocks = BUFNR;
2022 struct buffer_head * bh;
2025 bh = __getblk (dev, block, bufsize );
2026 if (buffer_uptodate (bh))
2029 if (block + BUFNR > max_block) {
2030 blocks = max_block - block;
2034 for (i = 1; i < blocks; i++) {
2035 bh = __getblk (dev, block + i, bufsize);
2036 if (buffer_uptodate (bh)) {
2040 else bhlist[j++] = bh;
2042 ll_rw_block (READ, j, bhlist);
2043 for(i = 1; i < j; i++)
2046 wait_on_buffer (bh);
2047 if (buffer_uptodate (bh))
2054 ** read and replay the log
2055 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2056 ** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
2058 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2060 ** On exit, it sets things up so the first transaction will work correctly.
2062 static int journal_read(struct super_block *p_s_sb) {
2063 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
2064 struct reiserfs_journal_desc *desc ;
2065 unsigned long oldest_trans_id = 0;
2066 unsigned long oldest_invalid_trans_id = 0 ;
2068 unsigned long oldest_start = 0;
2069 unsigned long cur_dblock = 0 ;
2070 unsigned long newest_mount_id = 9 ;
2071 struct buffer_head *d_bh ;
2072 struct reiserfs_journal_header *jh ;
2073 int valid_journal_header = 0 ;
2074 int replay_count = 0 ;
2075 int continue_replay = 1 ;
2077 char b[BDEVNAME_SIZE];
2079 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) ;
2080 reiserfs_info (p_s_sb, "checking transaction log (%s)\n",
2081 bdevname(journal->j_dev_bd, b));
2082 start = get_seconds();
2084 /* step 1, read in the journal header block. Check the transaction it says
2085 ** is the first unflushed, and if that transaction is not valid,
2088 journal->j_header_bh = journal_bread(p_s_sb,
2089 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2090 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2091 if (!journal->j_header_bh) {
2094 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data) ;
2095 if (le32_to_cpu(jh->j_first_unflushed_offset) >= 0 &&
2096 le32_to_cpu(jh->j_first_unflushed_offset) < SB_ONDISK_JOURNAL_SIZE(p_s_sb) &&
2097 le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
2098 oldest_start = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2099 le32_to_cpu(jh->j_first_unflushed_offset) ;
2100 oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2101 newest_mount_id = le32_to_cpu(jh->j_mount_id);
2102 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1153: found in "
2103 "header: first_unflushed_offset %d, last_flushed_trans_id "
2104 "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
2105 le32_to_cpu(jh->j_last_flush_trans_id)) ;
2106 valid_journal_header = 1 ;
2108 /* now, we try to read the first unflushed offset. If it is not valid,
2109 ** there is nothing more we can do, and it makes no sense to read
2110 ** through the whole log.
2112 d_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + le32_to_cpu(jh->j_first_unflushed_offset)) ;
2113 ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL) ;
2115 continue_replay = 0 ;
2118 goto start_log_replay;
2121 if (continue_replay && bdev_read_only(p_s_sb->s_bdev)) {
2122 reiserfs_warning (p_s_sb,
2123 "clm-2076: device is readonly, unable to replay log") ;
2127 /* ok, there are transactions that need to be replayed. start with the first log block, find
2128 ** all the valid transactions, and pick out the oldest.
2130 while(continue_replay && cur_dblock < (SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb))) {
2131 /* Note that it is required for blocksize of primary fs device and journal
2132 device to be the same */
2133 d_bh = reiserfs_breada(journal->j_dev_bd, cur_dblock, p_s_sb->s_blocksize,
2134 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb)) ;
2135 ret = journal_transaction_is_valid(p_s_sb, d_bh, &oldest_invalid_trans_id, &newest_mount_id) ;
2137 desc = (struct reiserfs_journal_desc *)d_bh->b_data ;
2138 if (oldest_start == 0) { /* init all oldest_ values */
2139 oldest_trans_id = get_desc_trans_id(desc) ;
2140 oldest_start = d_bh->b_blocknr ;
2141 newest_mount_id = get_desc_mount_id(desc) ;
2142 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1179: Setting "
2143 "oldest_start to offset %llu, trans_id %lu",
2144 oldest_start - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2146 } else if (oldest_trans_id > get_desc_trans_id(desc)) {
2147 /* one we just read was older */
2148 oldest_trans_id = get_desc_trans_id(desc) ;
2149 oldest_start = d_bh->b_blocknr ;
2150 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1180: Resetting "
2151 "oldest_start to offset %lu, trans_id %lu",
2152 oldest_start - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2155 if (newest_mount_id < get_desc_mount_id(desc)) {
2156 newest_mount_id = get_desc_mount_id(desc) ;
2157 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2158 "newest_mount_id to %d", get_desc_mount_id(desc));
2160 cur_dblock += get_desc_trans_len(desc) + 2 ;
2168 cur_dblock = oldest_start ;
2169 if (oldest_trans_id) {
2170 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1206: Starting replay "
2171 "from offset %llu, trans_id %lu",
2172 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2177 while(continue_replay && oldest_trans_id > 0) {
2178 ret = journal_read_transaction(p_s_sb, cur_dblock, oldest_start, oldest_trans_id, newest_mount_id) ;
2181 } else if (ret != 0) {
2184 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + journal->j_start ;
2186 if (cur_dblock == oldest_start)
2190 if (oldest_trans_id == 0) {
2191 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1225: No valid "
2192 "transactions found") ;
2194 /* j_start does not get set correctly if we don't replay any transactions.
2195 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2196 ** copy the trans_id from the header
2198 if (valid_journal_header && replay_count == 0) {
2199 journal->j_start = le32_to_cpu(jh->j_first_unflushed_offset) ;
2200 journal->j_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2201 journal->j_last_flush_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) ;
2202 journal->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
2204 journal->j_mount_id = newest_mount_id + 1 ;
2206 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2207 "newest_mount_id to %lu", journal->j_mount_id) ;
2208 journal->j_first_unflushed_offset = journal->j_start ;
2209 if (replay_count > 0) {
2210 reiserfs_info (p_s_sb, "replayed %d transactions in %lu seconds\n",
2211 replay_count, get_seconds() - start) ;
2213 if (!bdev_read_only(p_s_sb->s_bdev) &&
2214 _update_journal_header_block(p_s_sb, journal->j_start,
2215 journal->j_last_flush_trans_id))
2217 /* replay failed, caller must call free_journal_ram and abort
2225 static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
2227 struct reiserfs_journal_list *jl;
2229 jl = reiserfs_kmalloc(sizeof(struct reiserfs_journal_list), GFP_NOFS, s);
2234 memset(jl, 0, sizeof(*jl));
2235 INIT_LIST_HEAD(&jl->j_list);
2236 INIT_LIST_HEAD(&jl->j_working_list);
2237 INIT_LIST_HEAD(&jl->j_tail_bh_list);
2238 INIT_LIST_HEAD(&jl->j_bh_list);
2239 sema_init(&jl->j_commit_lock, 1);
2240 SB_JOURNAL(s)->j_num_lists++;
2241 get_journal_list(jl);
2245 static void journal_list_init(struct super_block *p_s_sb) {
2246 SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
2249 static int release_journal_dev( struct super_block *super,
2250 struct reiserfs_journal *journal )
2256 if( journal -> j_dev_file != NULL ) {
2257 result = filp_close( journal -> j_dev_file, NULL );
2258 journal -> j_dev_file = NULL;
2259 journal -> j_dev_bd = NULL;
2260 } else if( journal -> j_dev_bd != NULL ) {
2261 result = blkdev_put( journal -> j_dev_bd );
2262 journal -> j_dev_bd = NULL;
2266 reiserfs_warning(super, "sh-457: release_journal_dev: Cannot release journal device: %i", result );
2271 static int journal_init_dev( struct super_block *super,
2272 struct reiserfs_journal *journal,
2273 const char *jdev_name )
2277 int blkdev_mode = FMODE_READ | FMODE_WRITE;
2278 char b[BDEVNAME_SIZE];
2282 journal -> j_dev_bd = NULL;
2283 journal -> j_dev_file = NULL;
2284 jdev = SB_ONDISK_JOURNAL_DEVICE( super ) ?
2285 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
2287 if (bdev_read_only(super->s_bdev))
2288 blkdev_mode = FMODE_READ;
2290 /* there is no "jdev" option and journal is on separate device */
2291 if( ( !jdev_name || !jdev_name[ 0 ] ) ) {
2292 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode);
2293 if (IS_ERR(journal->j_dev_bd)) {
2294 result = PTR_ERR(journal->j_dev_bd);
2295 journal->j_dev_bd = NULL;
2296 reiserfs_warning (super, "sh-458: journal_init_dev: "
2297 "cannot init journal device '%s': %i",
2298 __bdevname(jdev, b), result );
2300 } else if (jdev != super->s_dev)
2301 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2305 journal -> j_dev_file = filp_open( jdev_name, 0, 0 );
2306 if( !IS_ERR( journal -> j_dev_file ) ) {
2307 struct inode *jdev_inode = journal->j_dev_file->f_mapping->host;
2308 if( !S_ISBLK( jdev_inode -> i_mode ) ) {
2309 reiserfs_warning(super, "journal_init_dev: '%s' is "
2310 "not a block device", jdev_name );
2312 release_journal_dev( super, journal );
2315 journal->j_dev_bd = I_BDEV(jdev_inode);
2316 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2317 reiserfs_info(super, "journal_init_dev: journal device: %s\n",
2318 bdevname(journal->j_dev_bd, b));
2321 result = PTR_ERR( journal -> j_dev_file );
2322 journal -> j_dev_file = NULL;
2323 reiserfs_warning (super,
2324 "journal_init_dev: Cannot open '%s': %i",
2325 jdev_name, result );
2331 ** must be called once on fs mount. calls journal_read for you
2333 int journal_init(struct super_block *p_s_sb, const char * j_dev_name, int old_format, unsigned int commit_max_age) {
2334 int num_cnodes = SB_ONDISK_JOURNAL_SIZE(p_s_sb) * 2 ;
2335 struct buffer_head *bhjh;
2336 struct reiserfs_super_block * rs;
2337 struct reiserfs_journal_header *jh;
2338 struct reiserfs_journal *journal;
2339 struct reiserfs_journal_list *jl;
2340 char b[BDEVNAME_SIZE];
2342 journal = SB_JOURNAL(p_s_sb) = vmalloc(sizeof (struct reiserfs_journal)) ;
2344 reiserfs_warning (p_s_sb, "journal-1256: unable to get memory for journal structure") ;
2347 memset(journal, 0, sizeof(struct reiserfs_journal)) ;
2348 INIT_LIST_HEAD(&journal->j_bitmap_nodes) ;
2349 INIT_LIST_HEAD (&journal->j_prealloc_list);
2350 INIT_LIST_HEAD(&journal->j_working_list);
2351 INIT_LIST_HEAD(&journal->j_journal_list);
2352 journal->j_persistent_trans = 0;
2353 if (reiserfs_allocate_list_bitmaps(p_s_sb,
2354 journal->j_list_bitmap,
2355 SB_BMAP_NR(p_s_sb)))
2356 goto free_and_return ;
2357 allocate_bitmap_nodes(p_s_sb) ;
2359 /* reserved for journal area support */
2360 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) = (old_format ?
2361 REISERFS_OLD_DISK_OFFSET_IN_BYTES / p_s_sb->s_blocksize +
2362 SB_BMAP_NR(p_s_sb) + 1 :
2363 REISERFS_DISK_OFFSET_IN_BYTES / p_s_sb->s_blocksize + 2);
2365 /* Sanity check to see is the standard journal fitting withing first bitmap
2366 (actual for small blocksizes) */
2367 if ( !SB_ONDISK_JOURNAL_DEVICE( p_s_sb ) &&
2368 (SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb) > p_s_sb->s_blocksize * 8) ) {
2369 reiserfs_warning (p_s_sb, "journal-1393: journal does not fit for area "
2370 "addressed by first of bitmap blocks. It starts at "
2371 "%u and its size is %u. Block size %ld",
2372 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb),
2373 SB_ONDISK_JOURNAL_SIZE(p_s_sb), p_s_sb->s_blocksize);
2374 goto free_and_return;
2377 if( journal_init_dev( p_s_sb, journal, j_dev_name ) != 0 ) {
2378 reiserfs_warning (p_s_sb, "sh-462: unable to initialize jornal device");
2379 goto free_and_return;
2382 rs = SB_DISK_SUPER_BLOCK(p_s_sb);
2384 /* read journal header */
2385 bhjh = journal_bread(p_s_sb,
2386 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2388 reiserfs_warning (p_s_sb, "sh-459: unable to read journal header");
2389 goto free_and_return;
2391 jh = (struct reiserfs_journal_header *)(bhjh->b_data);
2393 /* make sure that journal matches to the super block */
2394 if (is_reiserfs_jr(rs) && (le32_to_cpu(jh->jh_journal.jp_journal_magic) != sb_jp_journal_magic(rs))) {
2395 reiserfs_warning (p_s_sb, "sh-460: journal header magic %x "
2396 "(device %s) does not match to magic found in super "
2398 jh->jh_journal.jp_journal_magic,
2399 bdevname( journal->j_dev_bd, b),
2400 sb_jp_journal_magic(rs));
2402 goto free_and_return;
2405 journal->j_trans_max = le32_to_cpu (jh->jh_journal.jp_journal_trans_max);
2406 journal->j_max_batch = le32_to_cpu (jh->jh_journal.jp_journal_max_batch);
2407 journal->j_max_commit_age = le32_to_cpu (jh->jh_journal.jp_journal_max_commit_age);
2408 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
2410 if (journal->j_trans_max) {
2411 /* make sure these parameters are available, assign it if they are not */
2412 __u32 initial = journal->j_trans_max;
2415 if (p_s_sb->s_blocksize < 4096)
2416 ratio = 4096 / p_s_sb->s_blocksize;
2418 if (SB_ONDISK_JOURNAL_SIZE(p_s_sb)/journal->j_trans_max < JOURNAL_MIN_RATIO)
2419 journal->j_trans_max = SB_ONDISK_JOURNAL_SIZE(p_s_sb) / JOURNAL_MIN_RATIO;
2420 if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio)
2421 journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT / ratio;
2422 if (journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio)
2423 journal->j_trans_max = JOURNAL_TRANS_MIN_DEFAULT / ratio;
2425 if (journal->j_trans_max != initial)
2426 reiserfs_warning (p_s_sb, "sh-461: journal_init: wrong transaction max size (%u). Changed to %u",
2427 initial, journal->j_trans_max);
2429 journal->j_max_batch = journal->j_trans_max*
2430 JOURNAL_MAX_BATCH_DEFAULT/JOURNAL_TRANS_MAX_DEFAULT;
2433 if (!journal->j_trans_max) {
2434 /*we have the file system was created by old version of mkreiserfs
2435 so this field contains zero value */
2436 journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT ;
2437 journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT ;
2438 journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE ;
2440 /* for blocksize >= 4096 - max transaction size is 1024. For block size < 4096
2441 trans max size is decreased proportionally */
2442 if (p_s_sb->s_blocksize < 4096) {
2443 journal->j_trans_max /= (4096 / p_s_sb->s_blocksize) ;
2444 journal->j_max_batch = (journal->j_trans_max) * 9 / 10 ;
2448 journal->j_default_max_commit_age = journal->j_max_commit_age;
2450 if (commit_max_age != 0) {
2451 journal->j_max_commit_age = commit_max_age;
2452 journal->j_max_trans_age = commit_max_age;
2455 reiserfs_info (p_s_sb, "journal params: device %s, size %u, "
2456 "journal first block %u, max trans len %u, max batch %u, "
2457 "max commit age %u, max trans age %u\n",
2458 bdevname( journal->j_dev_bd, b),
2459 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2460 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2461 journal->j_trans_max,
2462 journal->j_max_batch,
2463 journal->j_max_commit_age,
2464 journal->j_max_trans_age);
2468 journal->j_list_bitmap_index = 0 ;
2469 journal_list_init(p_s_sb) ;
2471 memset(journal->j_list_hash_table, 0, JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)) ;
2473 INIT_LIST_HEAD(&journal->j_dirty_buffers) ;
2474 spin_lock_init(&journal->j_dirty_buffers_lock) ;
2476 journal->j_start = 0 ;
2477 journal->j_len = 0 ;
2478 journal->j_len_alloc = 0 ;
2479 atomic_set(&(journal->j_wcount), 0) ;
2480 atomic_set(&(journal->j_async_throttle), 0) ;
2481 journal->j_bcount = 0 ;
2482 journal->j_trans_start_time = 0 ;
2483 journal->j_last = NULL ;
2484 journal->j_first = NULL ;
2485 init_waitqueue_head(&(journal->j_join_wait)) ;
2486 sema_init(&journal->j_lock, 1);
2487 sema_init(&journal->j_flush_sem, 1);
2489 journal->j_trans_id = 10 ;
2490 journal->j_mount_id = 10 ;
2491 journal->j_state = 0 ;
2492 atomic_set(&(journal->j_jlock), 0) ;
2493 journal->j_cnode_free_list = allocate_cnodes(num_cnodes) ;
2494 journal->j_cnode_free_orig = journal->j_cnode_free_list ;
2495 journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0 ;
2496 journal->j_cnode_used = 0 ;
2497 journal->j_must_wait = 0 ;
2499 init_journal_hash(p_s_sb) ;
2500 jl = journal->j_current_jl;
2501 jl->j_list_bitmap = get_list_bitmap(p_s_sb, jl);
2502 if (!jl->j_list_bitmap) {
2503 reiserfs_warning(p_s_sb, "journal-2005, get_list_bitmap failed for journal list 0") ;
2504 goto free_and_return;
2506 if (journal_read(p_s_sb) < 0) {
2507 reiserfs_warning(p_s_sb, "Replay Failure, unable to mount") ;
2508 goto free_and_return;
2511 reiserfs_mounted_fs_count++ ;
2512 if (reiserfs_mounted_fs_count <= 1)
2513 commit_wq = create_workqueue("reiserfs");
2515 INIT_WORK(&journal->j_work, flush_async_commits, p_s_sb);
2518 free_journal_ram(p_s_sb);
2523 ** test for a polite end of the current transaction. Used by file_write, and should
2524 ** be used by delete to make sure they don't write more than can fit inside a single
2527 int journal_transaction_should_end(struct reiserfs_transaction_handle *th, int new_alloc) {
2528 struct reiserfs_journal *journal = SB_JOURNAL (th->t_super);
2529 time_t now = get_seconds() ;
2530 /* cannot restart while nested */
2531 BUG_ON (!th->t_trans_id);
2532 if (th->t_refcount > 1)
2534 if ( journal->j_must_wait > 0 ||
2535 (journal->j_len_alloc + new_alloc) >= journal->j_max_batch ||
2536 atomic_read(&(journal->j_jlock)) ||
2537 (now - journal->j_trans_start_time) > journal->j_max_trans_age ||
2538 journal->j_cnode_free < (journal->j_trans_max * 3)) {
2544 /* this must be called inside a transaction, and requires the
2545 ** kernel_lock to be held
2547 void reiserfs_block_writes(struct reiserfs_transaction_handle *th) {
2548 struct reiserfs_journal *journal = SB_JOURNAL (th->t_super);
2549 BUG_ON (!th->t_trans_id);
2550 journal->j_must_wait = 1 ;
2551 set_bit(J_WRITERS_BLOCKED, &journal->j_state) ;
2555 /* this must be called without a transaction started, and does not
2558 void reiserfs_allow_writes(struct super_block *s) {
2559 struct reiserfs_journal *journal = SB_JOURNAL (s);
2560 clear_bit(J_WRITERS_BLOCKED, &journal->j_state) ;
2561 wake_up(&journal->j_join_wait) ;
2564 /* this must be called without a transaction started, and does not
2567 void reiserfs_wait_on_write_block(struct super_block *s) {
2568 struct reiserfs_journal *journal = SB_JOURNAL (s);
2569 wait_event(journal->j_join_wait,
2570 !test_bit(J_WRITERS_BLOCKED, &journal->j_state)) ;
2573 static void queue_log_writer(struct super_block *s) {
2575 struct reiserfs_journal *journal = SB_JOURNAL (s);
2576 set_bit(J_WRITERS_QUEUED, &journal->j_state);
2579 * we don't want to use wait_event here because
2580 * we only want to wait once.
2582 init_waitqueue_entry(&wait, current);
2583 add_wait_queue(&journal->j_join_wait, &wait);
2584 set_current_state(TASK_UNINTERRUPTIBLE);
2585 if (test_bit(J_WRITERS_QUEUED, &journal->j_state))
2587 current->state = TASK_RUNNING;
2588 remove_wait_queue(&journal->j_join_wait, &wait);
2591 static void wake_queued_writers(struct super_block *s) {
2592 struct reiserfs_journal *journal = SB_JOURNAL (s);
2593 if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state))
2594 wake_up(&journal->j_join_wait);
2597 static void let_transaction_grow(struct super_block *sb,
2598 unsigned long trans_id)
2600 struct reiserfs_journal *journal = SB_JOURNAL (sb);
2601 unsigned long bcount = journal->j_bcount;
2603 set_current_state(TASK_UNINTERRUPTIBLE);
2604 schedule_timeout(1);
2605 journal->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2606 while ((atomic_read(&journal->j_wcount) > 0 ||
2607 atomic_read(&journal->j_jlock)) &&
2608 journal->j_trans_id == trans_id) {
2609 queue_log_writer(sb);
2611 if (journal->j_trans_id != trans_id)
2613 if (bcount == journal->j_bcount)
2615 bcount = journal->j_bcount;
2619 /* join == true if you must join an existing transaction.
2620 ** join == false if you can deal with waiting for others to finish
2622 ** this will block until the transaction is joinable. send the number of blocks you
2623 ** expect to use in nblocks.
2625 static int do_journal_begin_r(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,unsigned long nblocks,int join) {
2626 time_t now = get_seconds() ;
2628 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2629 struct reiserfs_transaction_handle myth;
2630 int sched_count = 0;
2633 reiserfs_check_lock_depth(p_s_sb, "journal_begin") ;
2635 PROC_INFO_INC( p_s_sb, journal.journal_being );
2636 /* set here for journal_join */
2638 th->t_super = p_s_sb ;
2641 lock_journal(p_s_sb) ;
2642 if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted (journal)) {
2643 unlock_journal (p_s_sb);
2644 retval = journal->j_errno;
2647 journal->j_bcount++;
2649 if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) {
2650 unlock_journal(p_s_sb) ;
2651 reiserfs_wait_on_write_block(p_s_sb) ;
2652 PROC_INFO_INC( p_s_sb, journal.journal_relock_writers );
2655 now = get_seconds();
2657 /* if there is no room in the journal OR
2658 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
2659 ** we don't sleep if there aren't other writers
2662 if ( (!join && journal->j_must_wait > 0) ||
2663 ( !join && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch) ||
2664 (!join && atomic_read(&journal->j_wcount) > 0 && journal->j_trans_start_time > 0 &&
2665 (now - journal->j_trans_start_time) > journal->j_max_trans_age) ||
2666 (!join && atomic_read(&journal->j_jlock)) ||
2667 (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) {
2669 old_trans_id = journal->j_trans_id;
2670 unlock_journal(p_s_sb) ; /* allow others to finish this transaction */
2672 if (!join && (journal->j_len_alloc + nblocks + 2) >=
2673 journal->j_max_batch &&
2674 ((journal->j_len + nblocks + 2) * 100) < (journal->j_len_alloc * 75))
2676 if (atomic_read(&journal->j_wcount) > 10) {
2678 queue_log_writer(p_s_sb);
2682 /* don't mess with joining the transaction if all we have to do is
2683 * wait for someone else to do a commit
2685 if (atomic_read(&journal->j_jlock)) {
2686 while (journal->j_trans_id == old_trans_id &&
2687 atomic_read(&journal->j_jlock)) {
2688 queue_log_writer(p_s_sb);
2692 retval = journal_join(&myth, p_s_sb, 1) ;
2696 /* someone might have ended the transaction while we joined */
2697 if (old_trans_id != journal->j_trans_id) {
2698 retval = do_journal_end(&myth, p_s_sb, 1, 0) ;
2700 retval = do_journal_end(&myth, p_s_sb, 1, COMMIT_NOW) ;
2706 PROC_INFO_INC( p_s_sb, journal.journal_relock_wcount );
2709 /* we are the first writer, set trans_id */
2710 if (journal->j_trans_start_time == 0) {
2711 journal->j_trans_start_time = get_seconds();
2713 atomic_inc(&(journal->j_wcount)) ;
2714 journal->j_len_alloc += nblocks ;
2715 th->t_blocks_logged = 0 ;
2716 th->t_blocks_allocated = nblocks ;
2717 th->t_trans_id = journal->j_trans_id ;
2718 unlock_journal(p_s_sb) ;
2719 INIT_LIST_HEAD (&th->t_list);
2723 memset (th, 0, sizeof (*th));
2724 /* Re-set th->t_super, so we can properly keep track of how many
2725 * persistent transactions there are. We need to do this so if this
2726 * call is part of a failed restart_transaction, we can free it later */
2727 th->t_super = p_s_sb;
2731 struct reiserfs_transaction_handle *
2732 reiserfs_persistent_transaction(struct super_block *s, int nblocks) {
2734 struct reiserfs_transaction_handle *th ;
2736 /* if we're nesting into an existing transaction. It will be
2737 ** persistent on its own
2739 if (reiserfs_transaction_running(s)) {
2740 th = current->journal_info ;
2742 if (th->t_refcount < 2) {
2747 th = reiserfs_kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS, s) ;
2750 ret = journal_begin(th, s, nblocks) ;
2752 reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle), s) ;
2756 SB_JOURNAL(s)->j_persistent_trans++;
2761 reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th) {
2762 struct super_block *s = th->t_super;
2765 ret = journal_end(th, th->t_super, th->t_blocks_allocated);
2768 if (th->t_refcount == 0) {
2769 SB_JOURNAL(s)->j_persistent_trans--;
2770 reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle), s) ;
2775 static int journal_join(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2776 struct reiserfs_transaction_handle *cur_th = current->journal_info;
2778 /* this keeps do_journal_end from NULLing out the current->journal_info
2781 th->t_handle_save = cur_th ;
2782 if (cur_th && cur_th->t_refcount > 1) {
2785 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_JOIN) ;
2788 int journal_join_abort(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2789 struct reiserfs_transaction_handle *cur_th = current->journal_info;
2791 /* this keeps do_journal_end from NULLing out the current->journal_info
2794 th->t_handle_save = cur_th ;
2795 if (cur_th && cur_th->t_refcount > 1) {
2798 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_ABORT) ;
2801 int journal_begin(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb, unsigned long nblocks) {
2802 struct reiserfs_transaction_handle *cur_th = current->journal_info ;
2805 th->t_handle_save = NULL ;
2807 /* we are nesting into the current transaction */
2808 if (cur_th->t_super == p_s_sb) {
2809 BUG_ON (!cur_th->t_refcount);
2810 cur_th->t_refcount++ ;
2811 memcpy(th, cur_th, sizeof(*th));
2812 if (th->t_refcount <= 1)
2813 reiserfs_warning (p_s_sb, "BAD: refcount <= 1, but journal_info != 0");
2816 /* we've ended up with a handle from a different filesystem.
2817 ** save it and restore on journal_end. This should never
2820 reiserfs_warning(p_s_sb, "clm-2100: nesting info a different FS") ;
2821 th->t_handle_save = current->journal_info ;
2822 current->journal_info = th;
2825 current->journal_info = th;
2827 ret = do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_REG) ;
2828 if (current->journal_info != th)
2831 /* I guess this boils down to being the reciprocal of clm-2100 above.
2832 * If do_journal_begin_r fails, we need to put it back, since journal_end
2833 * won't be called to do it. */
2835 current->journal_info = th->t_handle_save;
2837 BUG_ON (!th->t_refcount);
2843 ** puts bh into the current transaction. If it was already there, reorders removes the
2844 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
2846 ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
2847 ** transaction is committed.
2849 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
2851 int journal_mark_dirty(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, struct buffer_head *bh) {
2852 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
2853 struct reiserfs_journal_cnode *cn = NULL;
2854 int count_already_incd = 0 ;
2856 BUG_ON (!th->t_trans_id);
2858 PROC_INFO_INC( p_s_sb, journal.mark_dirty );
2859 if (th->t_trans_id != journal->j_trans_id) {
2860 reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ld\n",
2861 th->t_trans_id, journal->j_trans_id);
2866 prepared = test_clear_buffer_journal_prepared (bh);
2867 clear_buffer_journal_restore_dirty (bh);
2868 /* already in this transaction, we are done */
2869 if (buffer_journaled(bh)) {
2870 PROC_INFO_INC( p_s_sb, journal.mark_dirty_already );
2874 /* this must be turned into a panic instead of a warning. We can't allow
2875 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
2876 ** could get to disk too early. NOT GOOD.
2878 if (!prepared || buffer_dirty(bh)) {
2879 reiserfs_warning (p_s_sb, "journal-1777: buffer %llu bad state "
2880 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
2881 (unsigned long long)bh->b_blocknr, prepared ? ' ' : '!',
2882 buffer_locked(bh) ? ' ' : '!',
2883 buffer_dirty(bh) ? ' ' : '!',
2884 buffer_journal_dirty(bh) ? ' ' : '!') ;
2887 if (atomic_read(&(journal->j_wcount)) <= 0) {
2888 reiserfs_warning (p_s_sb, "journal-1409: journal_mark_dirty returning because j_wcount was %d", atomic_read(&(journal->j_wcount))) ;
2891 /* this error means I've screwed up, and we've overflowed the transaction.
2892 ** Nothing can be done here, except make the FS readonly or panic.
2894 if (journal->j_len >= journal->j_trans_max) {
2895 reiserfs_panic(th->t_super, "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n", journal->j_len) ;
2898 if (buffer_journal_dirty(bh)) {
2899 count_already_incd = 1 ;
2900 PROC_INFO_INC( p_s_sb, journal.mark_dirty_notjournal );
2901 clear_buffer_journal_dirty (bh);
2904 if (journal->j_len > journal->j_len_alloc) {
2905 journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT ;
2908 set_buffer_journaled (bh);
2910 /* now put this guy on the end */
2912 cn = get_cnode(p_s_sb) ;
2914 reiserfs_panic(p_s_sb, "get_cnode failed!\n");
2917 if (th->t_blocks_logged == th->t_blocks_allocated) {
2918 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT ;
2919 journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT ;
2921 th->t_blocks_logged++ ;
2925 cn->blocknr = bh->b_blocknr ;
2928 insert_journal_hash(journal->j_hash_table, cn) ;
2929 if (!count_already_incd) {
2934 cn->prev = journal->j_last ;
2936 if (journal->j_last) {
2937 journal->j_last->next = cn ;
2938 journal->j_last = cn ;
2940 journal->j_first = cn ;
2941 journal->j_last = cn ;
2946 int journal_end(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
2947 if (!current->journal_info && th->t_refcount > 1)
2948 reiserfs_warning (p_s_sb, "REISER-NESTING: th NULL, refcount %d",
2951 if (!th->t_trans_id) {
2957 if (th->t_refcount > 0) {
2958 struct reiserfs_transaction_handle *cur_th = current->journal_info ;
2960 /* we aren't allowed to close a nested transaction on a different
2961 ** filesystem from the one in the task struct
2963 if (cur_th->t_super != th->t_super)
2967 memcpy(current->journal_info, th, sizeof(*th));
2972 return do_journal_end(th, p_s_sb, nblocks, 0) ;
2976 /* removes from the current transaction, relsing and descrementing any counters.
2977 ** also files the removed buffer directly onto the clean list
2979 ** called by journal_mark_freed when a block has been deleted
2981 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
2983 static int remove_from_transaction(struct super_block *p_s_sb, b_blocknr_t blocknr, int already_cleaned) {
2984 struct buffer_head *bh ;
2985 struct reiserfs_journal_cnode *cn ;
2986 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
2989 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr) ;
2990 if (!cn || !cn->bh) {
2995 cn->prev->next = cn->next ;
2998 cn->next->prev = cn->prev ;
3000 if (cn == journal->j_first) {
3001 journal->j_first = cn->next ;
3003 if (cn == journal->j_last) {
3004 journal->j_last = cn->prev ;
3007 remove_journal_hash(p_s_sb, journal->j_hash_table, NULL, bh->b_blocknr, 0) ;
3008 clear_buffer_journaled (bh); /* don't log this one */
3010 if (!already_cleaned) {
3011 clear_buffer_journal_dirty (bh);
3012 clear_buffer_dirty(bh);
3013 clear_buffer_journal_test (bh);
3015 if (atomic_read(&(bh->b_count)) < 0) {
3016 reiserfs_warning (p_s_sb, "journal-1752: remove from trans, b_count < 0");
3021 journal->j_len_alloc-- ;
3022 free_cnode(p_s_sb, cn) ;
3027 ** for any cnode in a journal list, it can only be dirtied of all the
3028 ** transactions that include it are commited to disk.
3029 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
3030 ** and 0 if you aren't
3032 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3033 ** blocks for a given transaction on disk
3036 static int can_dirty(struct reiserfs_journal_cnode *cn) {
3037 struct super_block *sb = cn->sb;
3038 b_blocknr_t blocknr = cn->blocknr ;
3039 struct reiserfs_journal_cnode *cur = cn->hprev ;
3042 /* first test hprev. These are all newer than cn, so any node here
3043 ** with the same block number and dev means this node can't be sent
3044 ** to disk right now.
3046 while(cur && can_dirty) {
3047 if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
3048 cur->blocknr == blocknr) {
3053 /* then test hnext. These are all older than cn. As long as they
3054 ** are committed to the log, it is safe to write cn to disk
3057 while(cur && can_dirty) {
3058 if (cur->jlist && cur->jlist->j_len > 0 &&
3059 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
3060 cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
3068 /* syncs the commit blocks, but does not force the real buffers to disk
3069 ** will wait until the current transaction is done/commited before returning
3071 int journal_end_sync(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, unsigned long nblocks) {
3072 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
3074 BUG_ON (!th->t_trans_id);
3075 /* you can sync while nested, very, very bad */
3076 if (th->t_refcount > 1) {
3079 if (journal->j_len == 0) {
3080 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
3081 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
3083 return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT) ;
3087 ** writeback the pending async commits to disk
3089 static void flush_async_commits(void *p) {
3090 struct super_block *p_s_sb = p;
3091 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
3092 struct reiserfs_journal_list *jl;
3093 struct list_head *entry;
3096 if (!list_empty(&journal->j_journal_list)) {
3097 /* last entry is the youngest, commit it and you get everything */
3098 entry = journal->j_journal_list.prev;
3099 jl = JOURNAL_LIST_ENTRY(entry);
3100 flush_commit_list(p_s_sb, jl, 1);
3104 * this is a little racey, but there's no harm in missing
3105 * the filemap_fdata_write
3107 if (!atomic_read(&journal->j_async_throttle) && !reiserfs_is_journal_aborted (journal)) {
3108 atomic_inc(&journal->j_async_throttle);
3109 filemap_fdatawrite(p_s_sb->s_bdev->bd_inode->i_mapping);
3110 atomic_dec(&journal->j_async_throttle);
3115 ** flushes any old transactions to disk
3116 ** ends the current transaction if it is too old
3118 int reiserfs_flush_old_commits(struct super_block *p_s_sb) {
3120 struct reiserfs_transaction_handle th ;
3121 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
3123 now = get_seconds();
3124 /* safety check so we don't flush while we are replaying the log during
3127 if (list_empty(&journal->j_journal_list)) {
3131 /* check the current transaction. If there are no writers, and it is
3132 * too old, finish it, and force the commit blocks to disk
3134 if (atomic_read(&journal->j_wcount) <= 0 &&
3135 journal->j_trans_start_time > 0 &&
3136 journal->j_len > 0 &&
3137 (now - journal->j_trans_start_time) > journal->j_max_trans_age)
3139 if (!journal_join(&th, p_s_sb, 1)) {
3140 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
3141 journal_mark_dirty(&th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
3143 /* we're only being called from kreiserfsd, it makes no sense to do
3144 ** an async commit so that kreiserfsd can do it later
3146 do_journal_end(&th, p_s_sb,1, COMMIT_NOW | WAIT) ;
3149 return p_s_sb->s_dirt;
3153 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
3155 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
3156 ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3157 ** flushes the commit list and returns 0.
3159 ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
3161 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3163 static int check_journal_end(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb,
3164 unsigned long nblocks, int flags) {
3167 int flush = flags & FLUSH_ALL ;
3168 int commit_now = flags & COMMIT_NOW ;
3169 int wait_on_commit = flags & WAIT ;
3170 struct reiserfs_journal_list *jl;
3171 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
3173 BUG_ON (!th->t_trans_id);
3175 if (th->t_trans_id != journal->j_trans_id) {
3176 reiserfs_panic(th->t_super, "journal-1577: handle trans id %ld != current trans id %ld\n",
3177 th->t_trans_id, journal->j_trans_id);
3180 journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged) ;
3181 if (atomic_read(&(journal->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3182 atomic_dec(&(journal->j_wcount)) ;
3185 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
3186 ** will be dealt with by next transaction that actually writes something, but should be taken
3187 ** care of in this trans
3189 if (journal->j_len == 0) {
3192 /* if wcount > 0, and we are called to with flush or commit_now,
3193 ** we wait on j_join_wait. We will wake up when the last writer has
3194 ** finished the transaction, and started it on its way to the disk.
3195 ** Then, we flush the commit or journal list, and just return 0
3196 ** because the rest of journal end was already done for this transaction.
3198 if (atomic_read(&(journal->j_wcount)) > 0) {
3199 if (flush || commit_now) {
3202 jl = journal->j_current_jl;
3203 trans_id = jl->j_trans_id;
3205 jl->j_state |= LIST_COMMIT_PENDING;
3206 atomic_set(&(journal->j_jlock), 1) ;
3208 journal->j_next_full_flush = 1 ;
3210 unlock_journal(p_s_sb) ;
3212 /* sleep while the current transaction is still j_jlocked */
3213 while(journal->j_trans_id == trans_id) {
3214 if (atomic_read(&journal->j_jlock)) {
3215 queue_log_writer(p_s_sb);
3217 lock_journal(p_s_sb);
3218 if (journal->j_trans_id == trans_id) {
3219 atomic_set(&(journal->j_jlock), 1) ;
3221 unlock_journal(p_s_sb);
3224 if (journal->j_trans_id == trans_id) {
3227 if (commit_now && journal_list_still_alive(p_s_sb, trans_id) &&
3230 flush_commit_list(p_s_sb, jl, 1) ;
3234 unlock_journal(p_s_sb) ;
3238 /* deal with old transactions where we are the last writers */
3239 now = get_seconds();
3240 if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3242 journal->j_next_async_flush = 1 ;
3244 /* don't batch when someone is waiting on j_join_wait */
3245 /* don't batch when syncing the commit or flushing the whole trans */
3246 if (!(journal->j_must_wait > 0) && !(atomic_read(&(journal->j_jlock))) && !flush && !commit_now &&
3247 (journal->j_len < journal->j_max_batch) &&
3248 journal->j_len_alloc < journal->j_max_batch && journal->j_cnode_free > (journal->j_trans_max * 3)) {
3249 journal->j_bcount++ ;
3250 unlock_journal(p_s_sb) ;
3254 if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
3255 reiserfs_panic(p_s_sb, "journal-003: journal_end: j_start (%ld) is too high\n", journal->j_start) ;
3261 ** Does all the work that makes deleting blocks safe.
3262 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3265 ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3266 ** before this transaction has finished.
3268 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3269 ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3270 ** the block can't be reallocated yet.
3272 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3274 int journal_mark_freed(struct reiserfs_transaction_handle *th, struct super_block *p_s_sb, b_blocknr_t blocknr) {
3275 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
3276 struct reiserfs_journal_cnode *cn = NULL ;
3277 struct buffer_head *bh = NULL ;
3278 struct reiserfs_list_bitmap *jb = NULL ;
3280 BUG_ON (!th->t_trans_id);
3282 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr);
3287 /* if it is journal new, we just remove it from this transaction */
3288 if (bh && buffer_journal_new(bh)) {
3289 clear_buffer_journal_new (bh);
3290 clear_prepared_bits(bh) ;
3291 reiserfs_clean_and_file_buffer(bh) ;
3292 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
3294 /* set the bit for this block in the journal bitmap for this transaction */
3295 jb = journal->j_current_jl->j_list_bitmap;
3297 reiserfs_panic(p_s_sb, "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n") ;
3299 set_bit_in_list_bitmap(p_s_sb, blocknr, jb) ;
3301 /* Note, the entire while loop is not allowed to schedule. */
3304 clear_prepared_bits(bh) ;
3305 reiserfs_clean_and_file_buffer(bh) ;
3307 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned) ;
3309 /* find all older transactions with this block, make sure they don't try to write it out */
3310 cn = get_journal_hash_dev(p_s_sb,journal->j_list_hash_table, blocknr) ;
3312 if (p_s_sb == cn->sb && blocknr == cn->blocknr) {
3313 set_bit(BLOCK_FREED, &cn->state) ;
3316 /* remove_from_transaction will brelse the buffer if it was
3317 ** in the current trans
3319 clear_buffer_journal_dirty (cn->bh);
3320 clear_buffer_dirty(cn->bh);
3321 clear_buffer_journal_test(cn->bh);
3324 if (atomic_read(&(cn->bh->b_count)) < 0) {
3325 reiserfs_warning (p_s_sb, "journal-2138: cn->bh->b_count < 0");
3328 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3329 atomic_dec(&(cn->jlist->j_nonzerolen)) ;
3339 put_bh(bh) ; /* get_hash grabs the buffer */
3340 if (atomic_read(&(bh->b_count)) < 0) {
3341 reiserfs_warning (p_s_sb, "journal-2165: bh->b_count < 0");
3347 void reiserfs_update_inode_transaction(struct inode *inode) {
3348 struct reiserfs_journal *journal = SB_JOURNAL (inode->i_sb);
3349 REISERFS_I(inode)->i_jl = journal->j_current_jl;
3350 REISERFS_I(inode)->i_trans_id = journal->j_trans_id ;
3354 * returns -1 on error, 0 if no commits/barriers were done and 1
3355 * if a transaction was actually committed and the barrier was done
3357 static int __commit_trans_jl(struct inode *inode, unsigned long id,
3358 struct reiserfs_journal_list *jl)
3360 struct reiserfs_transaction_handle th ;
3361 struct super_block *sb = inode->i_sb ;
3362 struct reiserfs_journal *journal = SB_JOURNAL (sb);
3365 /* is it from the current transaction, or from an unknown transaction? */
3366 if (id == journal->j_trans_id) {
3367 jl = journal->j_current_jl;
3368 /* try to let other writers come in and grow this transaction */
3369 let_transaction_grow(sb, id);
3370 if (journal->j_trans_id != id) {
3371 goto flush_commit_only;
3374 ret = journal_begin(&th, sb, 1) ;
3378 /* someone might have ended this transaction while we joined */
3379 if (journal->j_trans_id != id) {
3380 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb), 1) ;
3381 journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb)) ;
3382 ret = journal_end(&th, sb, 1) ;
3383 goto flush_commit_only;
3386 ret = journal_end_sync(&th, sb, 1) ;
3391 /* this gets tricky, we have to make sure the journal list in
3392 * the inode still exists. We know the list is still around
3393 * if we've got a larger transaction id than the oldest list
3396 if (journal_list_still_alive(inode->i_sb, id)) {
3398 * we only set ret to 1 when we know for sure
3399 * the barrier hasn't been started yet on the commit
3402 if (atomic_read(&jl->j_commit_left) > 1)
3404 flush_commit_list(sb, jl, 1) ;
3405 if (journal->j_errno)
3406 ret = journal->j_errno;
3409 /* otherwise the list is gone, and long since committed */
3413 int reiserfs_commit_for_inode(struct inode *inode) {
3414 unsigned long id = REISERFS_I(inode)->i_trans_id;
3415 struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
3417 /* for the whole inode, assume unset id means it was
3418 * changed in the current transaction. More conservative
3421 reiserfs_update_inode_transaction(inode) ;
3422 id = REISERFS_I(inode)->i_trans_id;
3423 /* jl will be updated in __commit_trans_jl */
3426 return __commit_trans_jl(inode, id, jl);
3429 void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb,
3430 struct buffer_head *bh) {
3431 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
3432 PROC_INFO_INC( p_s_sb, journal.restore_prepared );
3436 if (test_clear_buffer_journal_restore_dirty (bh) &&
3437 buffer_journal_dirty(bh)) {
3438 struct reiserfs_journal_cnode *cn;
3439 cn = get_journal_hash_dev(p_s_sb,
3440 journal->j_list_hash_table,
3442 if (cn && can_dirty(cn)) {
3443 set_buffer_journal_test (bh);
3444 mark_buffer_dirty(bh);
3447 clear_buffer_journal_prepared (bh);
3450 extern struct tree_balance *cur_tb ;
3452 ** before we can change a metadata block, we have to make sure it won't
3453 ** be written to disk while we are altering it. So, we must:
3458 int reiserfs_prepare_for_journal(struct super_block *p_s_sb,
3459 struct buffer_head *bh, int wait) {
3460 PROC_INFO_INC( p_s_sb, journal.prepare );
3462 if (test_set_buffer_locked(bh)) {
3467 set_buffer_journal_prepared (bh);
3468 if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
3469 clear_buffer_journal_test (bh);
3470 set_buffer_journal_restore_dirty (bh);
3476 static void flush_old_journal_lists(struct super_block *s) {
3477 struct reiserfs_journal *journal = SB_JOURNAL (s);
3478 struct reiserfs_journal_list *jl;
3479 struct list_head *entry;
3480 time_t now = get_seconds();
3482 while(!list_empty(&journal->j_journal_list)) {
3483 entry = journal->j_journal_list.next;
3484 jl = JOURNAL_LIST_ENTRY(entry);
3485 /* this check should always be run, to send old lists to disk */
3486 if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4))) {
3487 flush_used_journal_lists(s, jl);
3495 ** long and ugly. If flush, will not return until all commit
3496 ** blocks and all real buffers in the trans are on disk.
3497 ** If no_async, won't return until all commit blocks are on disk.
3499 ** keep reading, there are comments as you go along
3501 ** If the journal is aborted, we just clean up. Things like flushing
3502 ** journal lists, etc just won't happen.
3504 static int do_journal_end(struct reiserfs_transaction_handle *th, struct super_block * p_s_sb, unsigned long nblocks,
3506 struct reiserfs_journal *journal = SB_JOURNAL (p_s_sb);
3507 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
3508 struct reiserfs_journal_cnode *last_cn = NULL;
3509 struct reiserfs_journal_desc *desc ;
3510 struct reiserfs_journal_commit *commit ;
3511 struct buffer_head *c_bh ; /* commit bh */
3512 struct buffer_head *d_bh ; /* desc bh */
3513 int cur_write_start = 0 ; /* start index of current log write */
3516 int flush = flags & FLUSH_ALL ;
3517 int wait_on_commit = flags & WAIT ;
3518 struct reiserfs_journal_list *jl, *temp_jl;
3519 struct list_head *entry, *safe;
3520 unsigned long jindex;
3521 unsigned long commit_trans_id;
3524 BUG_ON (th->t_refcount > 1);
3525 BUG_ON (!th->t_trans_id);
3527 current->journal_info = th->t_handle_save;
3528 reiserfs_check_lock_depth(p_s_sb, "journal end");
3529 if (journal->j_len == 0) {
3530 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb), 1) ;
3531 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb)) ;
3534 lock_journal(p_s_sb) ;
3535 if (journal->j_next_full_flush) {
3536 flags |= FLUSH_ALL ;
3539 if (journal->j_next_async_flush) {
3540 flags |= COMMIT_NOW | WAIT;
3544 /* check_journal_end locks the journal, and unlocks if it does not return 1
3545 ** it tells us if we should continue with the journal_end, or just return
3547 if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
3549 wake_queued_writers(p_s_sb);
3550 reiserfs_async_progress_wait(p_s_sb);
3554 /* check_journal_end might set these, check again */
3555 if (journal->j_next_full_flush) {
3560 ** j must wait means we have to flush the log blocks, and the real blocks for
3563 if (journal->j_must_wait > 0) {
3567 #ifdef REISERFS_PREALLOCATE
3568 /* quota ops might need to nest, setup the journal_info pointer for them */
3569 current->journal_info = th ;
3570 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
3571 * the transaction */
3572 current->journal_info = th->t_handle_save ;
3575 /* setup description block */
3576 d_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + journal->j_start) ;
3577 set_buffer_uptodate(d_bh);
3578 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data ;
3579 memset(d_bh->b_data, 0, d_bh->b_size) ;
3580 memcpy(get_journal_desc_magic (d_bh), JOURNAL_DESC_MAGIC, 8) ;
3581 set_desc_trans_id(desc, journal->j_trans_id) ;
3583 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
3584 c_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3585 ((journal->j_start + journal->j_len + 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
3586 commit = (struct reiserfs_journal_commit *)c_bh->b_data ;
3587 memset(c_bh->b_data, 0, c_bh->b_size) ;
3588 set_commit_trans_id(commit, journal->j_trans_id) ;
3589 set_buffer_uptodate(c_bh) ;
3591 /* init this journal list */
3592 jl = journal->j_current_jl;
3594 /* we lock the commit before doing anything because
3595 * we want to make sure nobody tries to run flush_commit_list until
3596 * the new transaction is fully setup, and we've already flushed the
3599 down(&jl->j_commit_lock);
3601 /* save the transaction id in case we need to commit it later */
3602 commit_trans_id = jl->j_trans_id;
3604 atomic_set(&jl->j_older_commits_done, 0) ;
3605 jl->j_trans_id = journal->j_trans_id ;
3606 jl->j_timestamp = journal->j_trans_start_time ;
3607 jl->j_commit_bh = c_bh ;
3608 jl->j_start = journal->j_start ;
3609 jl->j_len = journal->j_len ;
3610 atomic_set(&jl->j_nonzerolen, journal->j_len) ;
3611 atomic_set(&jl->j_commit_left, journal->j_len + 2);
3612 jl->j_realblock = NULL ;
3614 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
3615 ** for each real block, add it to the journal list hash,
3616 ** copy into real block index array in the commit or desc block
3618 trans_half = journal_trans_half(p_s_sb->s_blocksize);
3619 for (i = 0, cn = journal->j_first ; cn ; cn = cn->next, i++) {
3620 if (buffer_journaled (cn->bh)) {
3621 jl_cn = get_cnode(p_s_sb) ;
3623 reiserfs_panic(p_s_sb, "journal-1676, get_cnode returned NULL\n") ;
3626 jl->j_realblock = jl_cn ;
3628 jl_cn->prev = last_cn ;
3629 jl_cn->next = NULL ;
3631 last_cn->next = jl_cn ;
3634 /* make sure the block we are trying to log is not a block
3635 of journal or reserved area */
3637 if (is_block_in_log_or_reserved_area(p_s_sb, cn->bh->b_blocknr)) {
3638 reiserfs_panic(p_s_sb, "journal-2332: Trying to log block %lu, which is a log block\n", cn->bh->b_blocknr) ;
3640 jl_cn->blocknr = cn->bh->b_blocknr ;
3643 jl_cn->bh = cn->bh ;
3645 insert_journal_hash(journal->j_list_hash_table, jl_cn) ;
3646 if (i < trans_half) {
3647 desc->j_realblock[i] = cpu_to_le32(cn->bh->b_blocknr) ;
3649 commit->j_realblock[i - trans_half] = cpu_to_le32(cn->bh->b_blocknr) ;
3655 set_desc_trans_len(desc, journal->j_len) ;
3656 set_desc_mount_id(desc, journal->j_mount_id) ;
3657 set_desc_trans_id(desc, journal->j_trans_id) ;
3658 set_commit_trans_len(commit, journal->j_len);
3660 /* special check in case all buffers in the journal were marked for not logging */
3661 if (journal->j_len == 0) {
3665 /* we're about to dirty all the log blocks, mark the description block
3666 * dirty now too. Don't mark the commit block dirty until all the
3667 * others are on disk
3669 mark_buffer_dirty(d_bh);
3671 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
3672 cur_write_start = journal->j_start ;
3673 cn = journal->j_first ;
3674 jindex = 1 ; /* start at one so we don't get the desc again */
3676 clear_buffer_journal_new (cn->bh);
3677 /* copy all the real blocks into log area. dirty log blocks */
3678 if (buffer_journaled (cn->bh)) {
3679 struct buffer_head *tmp_bh ;
3682 tmp_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3683 ((cur_write_start + jindex) % SB_ONDISK_JOURNAL_SIZE(p_s_sb))) ;
3684 set_buffer_uptodate(tmp_bh);
3685 page = cn->bh->b_page;
3687 memcpy(tmp_bh->b_data, addr + offset_in_page(cn->bh->b_data),
3690 mark_buffer_dirty(tmp_bh);
3692 set_buffer_journal_dirty (cn->bh);
3693 clear_buffer_journaled (cn->bh);
3695 /* JDirty cleared sometime during transaction. don't log this one */
3696 reiserfs_warning(p_s_sb, "journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!") ;
3700 free_cnode(p_s_sb, cn) ;
3705 /* we are done with both the c_bh and d_bh, but
3706 ** c_bh must be written after all other commit blocks,
3707 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
3710 journal->j_current_jl = alloc_journal_list(p_s_sb);
3712 /* now it is safe to insert this transaction on the main list */
3713 list_add_tail(&jl->j_list, &journal->j_journal_list);
3714 list_add_tail(&jl->j_working_list, &journal->j_working_list);
3715 journal->j_num_work_lists++;
3717 /* reset journal values for the next transaction */
3718 old_start = journal->j_start ;
3719 journal->j_start = (journal->j_start + journal->j_len + 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb);
3720 atomic_set(&(journal->j_wcount), 0) ;
3721 journal->j_bcount = 0 ;
3722 journal->j_last = NULL ;
3723 journal->j_first = NULL ;
3724 journal->j_len = 0 ;
3725 journal->j_trans_start_time = 0 ;
3726 journal->j_trans_id++ ;
3727 journal->j_current_jl->j_trans_id = journal->j_trans_id;
3728 journal->j_must_wait = 0 ;
3729 journal->j_len_alloc = 0 ;
3730 journal->j_next_full_flush = 0 ;
3731 journal->j_next_async_flush = 0 ;
3732 init_journal_hash(p_s_sb) ;
3734 // make sure reiserfs_add_jh sees the new current_jl before we
3735 // write out the tails
3738 /* tail conversion targets have to hit the disk before we end the
3739 * transaction. Otherwise a later transaction might repack the tail
3740 * before this transaction commits, leaving the data block unflushed and
3741 * clean, if we crash before the later transaction commits, the data block
3744 if (!list_empty(&jl->j_tail_bh_list)) {
3746 write_ordered_buffers(&journal->j_dirty_buffers_lock,
3747 journal, jl, &jl->j_tail_bh_list);
3750 if (!list_empty(&jl->j_tail_bh_list))
3752 up(&jl->j_commit_lock);
3754 /* honor the flush wishes from the caller, simple commits can
3755 ** be done outside the journal lock, they are done below
3757 ** if we don't flush the commit list right now, we put it into
3758 ** the work queue so the people waiting on the async progress work
3759 ** queue don't wait for this proc to flush journal lists and such.
3762 flush_commit_list(p_s_sb, jl, 1) ;
3763 flush_journal_list(p_s_sb, jl, 1) ;
3764 } else if (!(jl->j_state & LIST_COMMIT_PENDING))
3765 queue_delayed_work(commit_wq, &journal->j_work, HZ/10);
3768 /* if the next transaction has any chance of wrapping, flush
3769 ** transactions that might get overwritten. If any journal lists are very
3770 ** old flush them as well.
3773 list_for_each_safe(entry, safe, &journal->j_journal_list) {
3774 temp_jl = JOURNAL_LIST_ENTRY(entry);
3775 if (journal->j_start <= temp_jl->j_start) {
3776 if ((journal->j_start + journal->j_trans_max + 1) >=
3779 flush_used_journal_lists(p_s_sb, temp_jl);
3781 } else if ((journal->j_start +
3782 journal->j_trans_max + 1) <
3783 SB_ONDISK_JOURNAL_SIZE(p_s_sb))
3785 /* if we don't cross into the next transaction and we don't
3786 * wrap, there is no way we can overlap any later transactions
3791 } else if ((journal->j_start +
3792 journal->j_trans_max + 1) >
3793 SB_ONDISK_JOURNAL_SIZE(p_s_sb))
3795 if (((journal->j_start + journal->j_trans_max + 1) %
3796 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) >= temp_jl->j_start)
3798 flush_used_journal_lists(p_s_sb, temp_jl);
3801 /* we don't overlap anything from out start to the end of the
3802 * log, and our wrapped portion doesn't overlap anything at
3803 * the start of the log. We can break
3809 flush_old_journal_lists(p_s_sb);
3811 journal->j_current_jl->j_list_bitmap = get_list_bitmap(p_s_sb, journal->j_current_jl) ;
3813 if (!(journal->j_current_jl->j_list_bitmap)) {
3814 reiserfs_panic(p_s_sb, "journal-1996: do_journal_end, could not get a list bitmap\n") ;
3817 atomic_set(&(journal->j_jlock), 0) ;
3818 unlock_journal(p_s_sb) ;
3819 /* wake up any body waiting to join. */
3820 clear_bit(J_WRITERS_QUEUED, &journal->j_state);
3821 wake_up(&(journal->j_join_wait)) ;
3823 if (!flush && wait_on_commit &&
3824 journal_list_still_alive(p_s_sb, commit_trans_id)) {
3825 flush_commit_list(p_s_sb, jl, 1) ;
3828 reiserfs_check_lock_depth(p_s_sb, "journal end2");
3830 memset (th, 0, sizeof (*th));
3831 /* Re-set th->t_super, so we can properly keep track of how many
3832 * persistent transactions there are. We need to do this so if this
3833 * call is part of a failed restart_transaction, we can free it later */
3834 th->t_super = p_s_sb;
3836 return journal->j_errno;
3840 __reiserfs_journal_abort_hard (struct super_block *sb)
3842 struct reiserfs_journal *journal = SB_JOURNAL (sb);
3843 if (test_bit (J_ABORTED, &journal->j_state))
3846 printk (KERN_CRIT "REISERFS: Aborting journal for filesystem on %s\n",
3847 reiserfs_bdevname (sb));
3849 sb->s_flags |= MS_RDONLY;
3850 set_bit (J_ABORTED, &journal->j_state);
3852 #ifdef CONFIG_REISERFS_CHECK
3858 __reiserfs_journal_abort_soft (struct super_block *sb, int errno)
3860 struct reiserfs_journal *journal = SB_JOURNAL (sb);
3861 if (test_bit (J_ABORTED, &journal->j_state))
3864 if (!journal->j_errno)
3865 journal->j_errno = errno;
3867 __reiserfs_journal_abort_hard (sb);
3871 reiserfs_journal_abort (struct super_block *sb, int errno)
3873 return __reiserfs_journal_abort_soft (sb, errno);