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>
58 /* gets a struct reiserfs_journal_list * from a list head */
59 #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
61 #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
64 /* the number of mounted filesystems. This is used to decide when to
65 ** start and kill the commit workqueue
67 static int reiserfs_mounted_fs_count
;
69 static struct workqueue_struct
*commit_wq
;
71 #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
73 #define BUFNR 64 /*read ahead */
75 /* cnode stat bits. Move these into reiserfs_fs.h */
77 #define BLOCK_FREED 2 /* this block was freed, and can't be written. */
78 #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
80 #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
81 #define BLOCK_DIRTIED 5
83 /* journal list state bits */
84 #define LIST_TOUCHED 1
86 #define LIST_COMMIT_PENDING 4 /* someone will commit this list */
88 /* flags for do_journal_end */
89 #define FLUSH_ALL 1 /* flush commit and real blocks */
90 #define COMMIT_NOW 2 /* end and commit this transaction */
91 #define WAIT 4 /* wait for the log blocks to hit the disk */
93 static int do_journal_end(struct reiserfs_transaction_handle
*,
94 struct super_block
*, unsigned long nblocks
,
96 static int flush_journal_list(struct super_block
*s
,
97 struct reiserfs_journal_list
*jl
, int flushall
);
98 static int flush_commit_list(struct super_block
*s
,
99 struct reiserfs_journal_list
*jl
, int flushall
);
100 static int can_dirty(struct reiserfs_journal_cnode
*cn
);
101 static int journal_join(struct reiserfs_transaction_handle
*th
,
102 struct super_block
*p_s_sb
, unsigned long nblocks
);
103 static int release_journal_dev(struct super_block
*super
,
104 struct reiserfs_journal
*journal
);
105 static int dirty_one_transaction(struct super_block
*s
,
106 struct reiserfs_journal_list
*jl
);
107 static void flush_async_commits(void *p
);
108 static void queue_log_writer(struct super_block
*s
);
110 /* values for join in do_journal_begin_r */
112 JBEGIN_REG
= 0, /* regular journal begin */
113 JBEGIN_JOIN
= 1, /* join the running transaction if at all possible */
114 JBEGIN_ABORT
= 2, /* called from cleanup code, ignores aborted flag */
117 static int do_journal_begin_r(struct reiserfs_transaction_handle
*th
,
118 struct super_block
*p_s_sb
,
119 unsigned long nblocks
, int join
);
121 static void init_journal_hash(struct super_block
*p_s_sb
)
123 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
124 memset(journal
->j_hash_table
, 0,
125 JOURNAL_HASH_SIZE
* sizeof(struct reiserfs_journal_cnode
*));
129 ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
130 ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
133 static int reiserfs_clean_and_file_buffer(struct buffer_head
*bh
)
136 clear_buffer_dirty(bh
);
137 clear_buffer_journal_test(bh
);
142 static void disable_barrier(struct super_block
*s
)
144 REISERFS_SB(s
)->s_mount_opt
&= ~(1 << REISERFS_BARRIER_FLUSH
);
145 printk("reiserfs: disabling flush barriers on %s\n",
146 reiserfs_bdevname(s
));
149 static struct reiserfs_bitmap_node
*allocate_bitmap_node(struct super_block
152 struct reiserfs_bitmap_node
*bn
;
155 bn
= kmalloc(sizeof(struct reiserfs_bitmap_node
), GFP_NOFS
);
159 bn
->data
= kzalloc(p_s_sb
->s_blocksize
, GFP_NOFS
);
165 INIT_LIST_HEAD(&bn
->list
);
169 static struct reiserfs_bitmap_node
*get_bitmap_node(struct super_block
*p_s_sb
)
171 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
172 struct reiserfs_bitmap_node
*bn
= NULL
;
173 struct list_head
*entry
= journal
->j_bitmap_nodes
.next
;
175 journal
->j_used_bitmap_nodes
++;
178 if (entry
!= &journal
->j_bitmap_nodes
) {
179 bn
= list_entry(entry
, struct reiserfs_bitmap_node
, list
);
181 memset(bn
->data
, 0, p_s_sb
->s_blocksize
);
182 journal
->j_free_bitmap_nodes
--;
185 bn
= allocate_bitmap_node(p_s_sb
);
192 static inline void free_bitmap_node(struct super_block
*p_s_sb
,
193 struct reiserfs_bitmap_node
*bn
)
195 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
196 journal
->j_used_bitmap_nodes
--;
197 if (journal
->j_free_bitmap_nodes
> REISERFS_MAX_BITMAP_NODES
) {
201 list_add(&bn
->list
, &journal
->j_bitmap_nodes
);
202 journal
->j_free_bitmap_nodes
++;
206 static void allocate_bitmap_nodes(struct super_block
*p_s_sb
)
209 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
210 struct reiserfs_bitmap_node
*bn
= NULL
;
211 for (i
= 0; i
< REISERFS_MIN_BITMAP_NODES
; i
++) {
212 bn
= allocate_bitmap_node(p_s_sb
);
214 list_add(&bn
->list
, &journal
->j_bitmap_nodes
);
215 journal
->j_free_bitmap_nodes
++;
217 break; // this is ok, we'll try again when more are needed
222 static int set_bit_in_list_bitmap(struct super_block
*p_s_sb
, int block
,
223 struct reiserfs_list_bitmap
*jb
)
225 int bmap_nr
= block
/ (p_s_sb
->s_blocksize
<< 3);
226 int bit_nr
= block
% (p_s_sb
->s_blocksize
<< 3);
228 if (!jb
->bitmaps
[bmap_nr
]) {
229 jb
->bitmaps
[bmap_nr
] = get_bitmap_node(p_s_sb
);
231 set_bit(bit_nr
, (unsigned long *)jb
->bitmaps
[bmap_nr
]->data
);
235 static void cleanup_bitmap_list(struct super_block
*p_s_sb
,
236 struct reiserfs_list_bitmap
*jb
)
239 if (jb
->bitmaps
== NULL
)
242 for (i
= 0; i
< SB_BMAP_NR(p_s_sb
); i
++) {
243 if (jb
->bitmaps
[i
]) {
244 free_bitmap_node(p_s_sb
, jb
->bitmaps
[i
]);
245 jb
->bitmaps
[i
] = NULL
;
251 ** only call this on FS unmount.
253 static int free_list_bitmaps(struct super_block
*p_s_sb
,
254 struct reiserfs_list_bitmap
*jb_array
)
257 struct reiserfs_list_bitmap
*jb
;
258 for (i
= 0; i
< JOURNAL_NUM_BITMAPS
; i
++) {
260 jb
->journal_list
= NULL
;
261 cleanup_bitmap_list(p_s_sb
, jb
);
268 static int free_bitmap_nodes(struct super_block
*p_s_sb
)
270 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
271 struct list_head
*next
= journal
->j_bitmap_nodes
.next
;
272 struct reiserfs_bitmap_node
*bn
;
274 while (next
!= &journal
->j_bitmap_nodes
) {
275 bn
= list_entry(next
, struct reiserfs_bitmap_node
, list
);
279 next
= journal
->j_bitmap_nodes
.next
;
280 journal
->j_free_bitmap_nodes
--;
287 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
288 ** jb_array is the array to be filled in.
290 int reiserfs_allocate_list_bitmaps(struct super_block
*p_s_sb
,
291 struct reiserfs_list_bitmap
*jb_array
,
296 struct reiserfs_list_bitmap
*jb
;
297 int mem
= bmap_nr
* sizeof(struct reiserfs_bitmap_node
*);
299 for (i
= 0; i
< JOURNAL_NUM_BITMAPS
; i
++) {
301 jb
->journal_list
= NULL
;
302 jb
->bitmaps
= vmalloc(mem
);
304 reiserfs_warning(p_s_sb
,
305 "clm-2000, unable to allocate bitmaps for journal lists");
309 memset(jb
->bitmaps
, 0, mem
);
312 free_list_bitmaps(p_s_sb
, jb_array
);
319 ** find an available list bitmap. If you can't find one, flush a commit list
322 static struct reiserfs_list_bitmap
*get_list_bitmap(struct super_block
*p_s_sb
,
323 struct reiserfs_journal_list
327 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
328 struct reiserfs_list_bitmap
*jb
= NULL
;
330 for (j
= 0; j
< (JOURNAL_NUM_BITMAPS
* 3); j
++) {
331 i
= journal
->j_list_bitmap_index
;
332 journal
->j_list_bitmap_index
= (i
+ 1) % JOURNAL_NUM_BITMAPS
;
333 jb
= journal
->j_list_bitmap
+ i
;
334 if (journal
->j_list_bitmap
[i
].journal_list
) {
335 flush_commit_list(p_s_sb
,
336 journal
->j_list_bitmap
[i
].
338 if (!journal
->j_list_bitmap
[i
].journal_list
) {
345 if (jb
->journal_list
) { /* double check to make sure if flushed correctly */
348 jb
->journal_list
= jl
;
353 ** allocates a new chunk of X nodes, and links them all together as a list.
354 ** Uses the cnode->next and cnode->prev pointers
355 ** returns NULL on failure
357 static struct reiserfs_journal_cnode
*allocate_cnodes(int num_cnodes
)
359 struct reiserfs_journal_cnode
*head
;
361 if (num_cnodes
<= 0) {
364 head
= vmalloc(num_cnodes
* sizeof(struct reiserfs_journal_cnode
));
368 memset(head
, 0, num_cnodes
* sizeof(struct reiserfs_journal_cnode
));
370 head
[0].next
= head
+ 1;
371 for (i
= 1; i
< num_cnodes
; i
++) {
372 head
[i
].prev
= head
+ (i
- 1);
373 head
[i
].next
= head
+ (i
+ 1); /* if last one, overwrite it after the if */
375 head
[num_cnodes
- 1].next
= NULL
;
380 ** pulls a cnode off the free list, or returns NULL on failure
382 static struct reiserfs_journal_cnode
*get_cnode(struct super_block
*p_s_sb
)
384 struct reiserfs_journal_cnode
*cn
;
385 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
387 reiserfs_check_lock_depth(p_s_sb
, "get_cnode");
389 if (journal
->j_cnode_free
<= 0) {
392 journal
->j_cnode_used
++;
393 journal
->j_cnode_free
--;
394 cn
= journal
->j_cnode_free_list
;
399 cn
->next
->prev
= NULL
;
401 journal
->j_cnode_free_list
= cn
->next
;
402 memset(cn
, 0, sizeof(struct reiserfs_journal_cnode
));
407 ** returns a cnode to the free list
409 static void free_cnode(struct super_block
*p_s_sb
,
410 struct reiserfs_journal_cnode
*cn
)
412 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
414 reiserfs_check_lock_depth(p_s_sb
, "free_cnode");
416 journal
->j_cnode_used
--;
417 journal
->j_cnode_free
++;
418 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
419 cn
->next
= journal
->j_cnode_free_list
;
420 if (journal
->j_cnode_free_list
) {
421 journal
->j_cnode_free_list
->prev
= cn
;
423 cn
->prev
= NULL
; /* not needed with the memset, but I might kill the memset, and forget to do this */
424 journal
->j_cnode_free_list
= cn
;
427 static void clear_prepared_bits(struct buffer_head
*bh
)
429 clear_buffer_journal_prepared(bh
);
430 clear_buffer_journal_restore_dirty(bh
);
433 /* utility function to force a BUG if it is called without the big
434 ** kernel lock held. caller is the string printed just before calling BUG()
436 void reiserfs_check_lock_depth(struct super_block
*sb
, char *caller
)
439 if (current
->lock_depth
< 0) {
440 reiserfs_panic(sb
, "%s called without kernel lock held",
448 /* return a cnode with same dev, block number and size in table, or null if not found */
449 static inline struct reiserfs_journal_cnode
*get_journal_hash_dev(struct
453 reiserfs_journal_cnode
457 struct reiserfs_journal_cnode
*cn
;
458 cn
= journal_hash(table
, sb
, bl
);
460 if (cn
->blocknr
== bl
&& cn
->sb
== sb
)
464 return (struct reiserfs_journal_cnode
*)0;
468 ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
469 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
470 ** being overwritten by a replay after crashing.
472 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
473 ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
474 ** sure you never write the block without logging it.
476 ** next_zero_bit is a suggestion about the next block to try for find_forward.
477 ** when bl is rejected because it is set in a journal list bitmap, we search
478 ** for the next zero bit in the bitmap that rejected bl. Then, we return that
479 ** through next_zero_bit for find_forward to try.
481 ** Just because we return something in next_zero_bit does not mean we won't
482 ** reject it on the next call to reiserfs_in_journal
485 int reiserfs_in_journal(struct super_block
*p_s_sb
,
486 int bmap_nr
, int bit_nr
, int search_all
,
487 b_blocknr_t
* next_zero_bit
)
489 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
490 struct reiserfs_journal_cnode
*cn
;
491 struct reiserfs_list_bitmap
*jb
;
495 *next_zero_bit
= 0; /* always start this at zero. */
497 PROC_INFO_INC(p_s_sb
, journal
.in_journal
);
498 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
499 ** if we crash before the transaction that freed it commits, this transaction won't
500 ** have committed either, and the block will never be written
503 for (i
= 0; i
< JOURNAL_NUM_BITMAPS
; i
++) {
504 PROC_INFO_INC(p_s_sb
, journal
.in_journal_bitmap
);
505 jb
= journal
->j_list_bitmap
+ i
;
506 if (jb
->journal_list
&& jb
->bitmaps
[bmap_nr
] &&
508 (unsigned long *)jb
->bitmaps
[bmap_nr
]->
511 find_next_zero_bit((unsigned long *)
512 (jb
->bitmaps
[bmap_nr
]->
514 p_s_sb
->s_blocksize
<< 3,
521 bl
= bmap_nr
* (p_s_sb
->s_blocksize
<< 3) + bit_nr
;
522 /* is it in any old transactions? */
525 get_journal_hash_dev(p_s_sb
, journal
->j_list_hash_table
, bl
))) {
529 /* is it in the current transaction. This should never happen */
530 if ((cn
= get_journal_hash_dev(p_s_sb
, journal
->j_hash_table
, bl
))) {
535 PROC_INFO_INC(p_s_sb
, journal
.in_journal_reusable
);
540 /* insert cn into table
542 static inline void insert_journal_hash(struct reiserfs_journal_cnode
**table
,
543 struct reiserfs_journal_cnode
*cn
)
545 struct reiserfs_journal_cnode
*cn_orig
;
547 cn_orig
= journal_hash(table
, cn
->sb
, cn
->blocknr
);
553 journal_hash(table
, cn
->sb
, cn
->blocknr
) = cn
;
556 /* lock the current transaction */
557 static inline void lock_journal(struct super_block
*p_s_sb
)
559 PROC_INFO_INC(p_s_sb
, journal
.lock_journal
);
560 down(&SB_JOURNAL(p_s_sb
)->j_lock
);
563 /* unlock the current transaction */
564 static inline void unlock_journal(struct super_block
*p_s_sb
)
566 up(&SB_JOURNAL(p_s_sb
)->j_lock
);
569 static inline void get_journal_list(struct reiserfs_journal_list
*jl
)
574 static inline void put_journal_list(struct super_block
*s
,
575 struct reiserfs_journal_list
*jl
)
577 if (jl
->j_refcount
< 1) {
578 reiserfs_panic(s
, "trans id %lu, refcount at %d",
579 jl
->j_trans_id
, jl
->j_refcount
);
581 if (--jl
->j_refcount
== 0)
586 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
587 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
590 static void cleanup_freed_for_journal_list(struct super_block
*p_s_sb
,
591 struct reiserfs_journal_list
*jl
)
594 struct reiserfs_list_bitmap
*jb
= jl
->j_list_bitmap
;
596 cleanup_bitmap_list(p_s_sb
, jb
);
598 jl
->j_list_bitmap
->journal_list
= NULL
;
599 jl
->j_list_bitmap
= NULL
;
602 static int journal_list_still_alive(struct super_block
*s
,
603 unsigned long trans_id
)
605 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
606 struct list_head
*entry
= &journal
->j_journal_list
;
607 struct reiserfs_journal_list
*jl
;
609 if (!list_empty(entry
)) {
610 jl
= JOURNAL_LIST_ENTRY(entry
->next
);
611 if (jl
->j_trans_id
<= trans_id
) {
618 static void reiserfs_end_buffer_io_sync(struct buffer_head
*bh
, int uptodate
)
620 char b
[BDEVNAME_SIZE
];
622 if (buffer_journaled(bh
)) {
623 reiserfs_warning(NULL
,
624 "clm-2084: pinned buffer %lu:%s sent to disk",
625 bh
->b_blocknr
, bdevname(bh
->b_bdev
, b
));
628 set_buffer_uptodate(bh
);
630 clear_buffer_uptodate(bh
);
635 static void reiserfs_end_ordered_io(struct buffer_head
*bh
, int uptodate
)
638 set_buffer_uptodate(bh
);
640 clear_buffer_uptodate(bh
);
645 static void submit_logged_buffer(struct buffer_head
*bh
)
648 bh
->b_end_io
= reiserfs_end_buffer_io_sync
;
649 clear_buffer_journal_new(bh
);
650 clear_buffer_dirty(bh
);
651 if (!test_clear_buffer_journal_test(bh
))
653 if (!buffer_uptodate(bh
))
655 submit_bh(WRITE
, bh
);
658 static void submit_ordered_buffer(struct buffer_head
*bh
)
661 bh
->b_end_io
= reiserfs_end_ordered_io
;
662 clear_buffer_dirty(bh
);
663 if (!buffer_uptodate(bh
))
665 submit_bh(WRITE
, bh
);
668 static int submit_barrier_buffer(struct buffer_head
*bh
)
671 bh
->b_end_io
= reiserfs_end_ordered_io
;
672 clear_buffer_dirty(bh
);
673 if (!buffer_uptodate(bh
))
675 return submit_bh(WRITE_BARRIER
, bh
);
678 static void check_barrier_completion(struct super_block
*s
,
679 struct buffer_head
*bh
)
681 if (buffer_eopnotsupp(bh
)) {
682 clear_buffer_eopnotsupp(bh
);
684 set_buffer_uptodate(bh
);
685 set_buffer_dirty(bh
);
686 sync_dirty_buffer(bh
);
690 #define CHUNK_SIZE 32
691 struct buffer_chunk
{
692 struct buffer_head
*bh
[CHUNK_SIZE
];
696 static void write_chunk(struct buffer_chunk
*chunk
)
700 for (i
= 0; i
< chunk
->nr
; i
++) {
701 submit_logged_buffer(chunk
->bh
[i
]);
707 static void write_ordered_chunk(struct buffer_chunk
*chunk
)
711 for (i
= 0; i
< chunk
->nr
; i
++) {
712 submit_ordered_buffer(chunk
->bh
[i
]);
718 static int add_to_chunk(struct buffer_chunk
*chunk
, struct buffer_head
*bh
,
719 spinlock_t
* lock
, void (fn
) (struct buffer_chunk
*))
722 if (chunk
->nr
>= CHUNK_SIZE
)
724 chunk
->bh
[chunk
->nr
++] = bh
;
725 if (chunk
->nr
>= CHUNK_SIZE
) {
736 static atomic_t nr_reiserfs_jh
= ATOMIC_INIT(0);
737 static struct reiserfs_jh
*alloc_jh(void)
739 struct reiserfs_jh
*jh
;
741 jh
= kmalloc(sizeof(*jh
), GFP_NOFS
);
743 atomic_inc(&nr_reiserfs_jh
);
751 * we want to free the jh when the buffer has been written
754 void reiserfs_free_jh(struct buffer_head
*bh
)
756 struct reiserfs_jh
*jh
;
760 bh
->b_private
= NULL
;
762 list_del_init(&jh
->list
);
764 if (atomic_read(&nr_reiserfs_jh
) <= 0)
766 atomic_dec(&nr_reiserfs_jh
);
771 static inline int __add_jh(struct reiserfs_journal
*j
, struct buffer_head
*bh
,
774 struct reiserfs_jh
*jh
;
777 spin_lock(&j
->j_dirty_buffers_lock
);
778 if (!bh
->b_private
) {
779 spin_unlock(&j
->j_dirty_buffers_lock
);
783 list_del_init(&jh
->list
);
788 spin_lock(&j
->j_dirty_buffers_lock
);
789 /* buffer must be locked for __add_jh, should be able to have
790 * two adds at the same time
797 jh
->jl
= j
->j_current_jl
;
799 list_add_tail(&jh
->list
, &jh
->jl
->j_tail_bh_list
);
801 list_add_tail(&jh
->list
, &jh
->jl
->j_bh_list
);
803 spin_unlock(&j
->j_dirty_buffers_lock
);
807 int reiserfs_add_tail_list(struct inode
*inode
, struct buffer_head
*bh
)
809 return __add_jh(SB_JOURNAL(inode
->i_sb
), bh
, 1);
811 int reiserfs_add_ordered_list(struct inode
*inode
, struct buffer_head
*bh
)
813 return __add_jh(SB_JOURNAL(inode
->i_sb
), bh
, 0);
816 #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
817 static int write_ordered_buffers(spinlock_t
* lock
,
818 struct reiserfs_journal
*j
,
819 struct reiserfs_journal_list
*jl
,
820 struct list_head
*list
)
822 struct buffer_head
*bh
;
823 struct reiserfs_jh
*jh
;
824 int ret
= j
->j_errno
;
825 struct buffer_chunk chunk
;
826 struct list_head tmp
;
827 INIT_LIST_HEAD(&tmp
);
831 while (!list_empty(list
)) {
832 jh
= JH_ENTRY(list
->next
);
835 if (test_set_buffer_locked(bh
)) {
836 if (!buffer_dirty(bh
)) {
837 list_del_init(&jh
->list
);
838 list_add(&jh
->list
, &tmp
);
843 write_ordered_chunk(&chunk
);
849 if (buffer_dirty(bh
)) {
850 list_del_init(&jh
->list
);
851 list_add(&jh
->list
, &tmp
);
852 add_to_chunk(&chunk
, bh
, lock
, write_ordered_chunk
);
854 reiserfs_free_jh(bh
);
859 cond_resched_lock(lock
);
863 write_ordered_chunk(&chunk
);
866 while (!list_empty(&tmp
)) {
867 jh
= JH_ENTRY(tmp
.prev
);
870 reiserfs_free_jh(bh
);
872 if (buffer_locked(bh
)) {
877 if (!buffer_uptodate(bh
)) {
880 /* ugly interaction with invalidatepage here.
881 * reiserfs_invalidate_page will pin any buffer that has a valid
882 * journal head from an older transaction. If someone else sets
883 * our buffer dirty after we write it in the first loop, and
884 * then someone truncates the page away, nobody will ever write
885 * the buffer. We're safe if we write the page one last time
886 * after freeing the journal header.
888 if (buffer_dirty(bh
) && unlikely(bh
->b_page
->mapping
== NULL
)) {
890 ll_rw_block(WRITE
, 1, &bh
);
894 cond_resched_lock(lock
);
900 static int flush_older_commits(struct super_block
*s
,
901 struct reiserfs_journal_list
*jl
)
903 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
904 struct reiserfs_journal_list
*other_jl
;
905 struct reiserfs_journal_list
*first_jl
;
906 struct list_head
*entry
;
907 unsigned long trans_id
= jl
->j_trans_id
;
908 unsigned long other_trans_id
;
909 unsigned long first_trans_id
;
913 * first we walk backwards to find the oldest uncommitted transation
916 entry
= jl
->j_list
.prev
;
918 other_jl
= JOURNAL_LIST_ENTRY(entry
);
919 if (entry
== &journal
->j_journal_list
||
920 atomic_read(&other_jl
->j_older_commits_done
))
924 entry
= other_jl
->j_list
.prev
;
927 /* if we didn't find any older uncommitted transactions, return now */
928 if (first_jl
== jl
) {
932 first_trans_id
= first_jl
->j_trans_id
;
934 entry
= &first_jl
->j_list
;
936 other_jl
= JOURNAL_LIST_ENTRY(entry
);
937 other_trans_id
= other_jl
->j_trans_id
;
939 if (other_trans_id
< trans_id
) {
940 if (atomic_read(&other_jl
->j_commit_left
) != 0) {
941 flush_commit_list(s
, other_jl
, 0);
943 /* list we were called with is gone, return */
944 if (!journal_list_still_alive(s
, trans_id
))
947 /* the one we just flushed is gone, this means all
948 * older lists are also gone, so first_jl is no longer
949 * valid either. Go back to the beginning.
951 if (!journal_list_still_alive
952 (s
, other_trans_id
)) {
957 if (entry
== &journal
->j_journal_list
)
965 int reiserfs_async_progress_wait(struct super_block
*s
)
968 struct reiserfs_journal
*j
= SB_JOURNAL(s
);
969 if (atomic_read(&j
->j_async_throttle
))
970 blk_congestion_wait(WRITE
, HZ
/ 10);
975 ** if this journal list still has commit blocks unflushed, send them to disk.
977 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
978 ** Before the commit block can by written, every other log block must be safely on disk
981 static int flush_commit_list(struct super_block
*s
,
982 struct reiserfs_journal_list
*jl
, int flushall
)
986 struct buffer_head
*tbh
= NULL
;
987 unsigned long trans_id
= jl
->j_trans_id
;
988 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
992 reiserfs_check_lock_depth(s
, "flush_commit_list");
994 if (atomic_read(&jl
->j_older_commits_done
)) {
1000 /* before we can put our commit blocks on disk, we have to make sure everyone older than
1001 ** us is on disk too
1003 BUG_ON(jl
->j_len
<= 0);
1004 BUG_ON(trans_id
== journal
->j_trans_id
);
1006 get_journal_list(jl
);
1008 if (flush_older_commits(s
, jl
) == 1) {
1009 /* list disappeared during flush_older_commits. return */
1014 /* make sure nobody is trying to flush this one at the same time */
1015 down(&jl
->j_commit_lock
);
1016 if (!journal_list_still_alive(s
, trans_id
)) {
1017 up(&jl
->j_commit_lock
);
1020 BUG_ON(jl
->j_trans_id
== 0);
1022 /* this commit is done, exit */
1023 if (atomic_read(&(jl
->j_commit_left
)) <= 0) {
1025 atomic_set(&(jl
->j_older_commits_done
), 1);
1027 up(&jl
->j_commit_lock
);
1031 if (!list_empty(&jl
->j_bh_list
)) {
1033 write_ordered_buffers(&journal
->j_dirty_buffers_lock
,
1034 journal
, jl
, &jl
->j_bh_list
);
1037 BUG_ON(!list_empty(&jl
->j_bh_list
));
1039 * for the description block and all the log blocks, submit any buffers
1040 * that haven't already reached the disk
1042 atomic_inc(&journal
->j_async_throttle
);
1043 for (i
= 0; i
< (jl
->j_len
+ 1); i
++) {
1044 bn
= SB_ONDISK_JOURNAL_1st_BLOCK(s
) + (jl
->j_start
+ i
) %
1045 SB_ONDISK_JOURNAL_SIZE(s
);
1046 tbh
= journal_find_get_block(s
, bn
);
1047 if (buffer_dirty(tbh
)) /* redundant, ll_rw_block() checks */
1048 ll_rw_block(SWRITE
, 1, &tbh
);
1051 atomic_dec(&journal
->j_async_throttle
);
1053 /* We're skipping the commit if there's an error */
1054 if (retval
|| reiserfs_is_journal_aborted(journal
))
1057 /* wait on everything written so far before writing the commit
1058 * if we are in barrier mode, send the commit down now
1060 barrier
= reiserfs_barrier_flush(s
);
1063 lock_buffer(jl
->j_commit_bh
);
1064 ret
= submit_barrier_buffer(jl
->j_commit_bh
);
1065 if (ret
== -EOPNOTSUPP
) {
1066 set_buffer_uptodate(jl
->j_commit_bh
);
1071 for (i
= 0; i
< (jl
->j_len
+ 1); i
++) {
1072 bn
= SB_ONDISK_JOURNAL_1st_BLOCK(s
) +
1073 (jl
->j_start
+ i
) % SB_ONDISK_JOURNAL_SIZE(s
);
1074 tbh
= journal_find_get_block(s
, bn
);
1075 wait_on_buffer(tbh
);
1076 // since we're using ll_rw_blk above, it might have skipped over
1077 // a locked buffer. Double check here
1079 if (buffer_dirty(tbh
)) /* redundant, sync_dirty_buffer() checks */
1080 sync_dirty_buffer(tbh
);
1081 if (unlikely(!buffer_uptodate(tbh
))) {
1082 #ifdef CONFIG_REISERFS_CHECK
1083 reiserfs_warning(s
, "journal-601, buffer write failed");
1087 put_bh(tbh
); /* once for journal_find_get_block */
1088 put_bh(tbh
); /* once due to original getblk in do_journal_end */
1089 atomic_dec(&(jl
->j_commit_left
));
1092 BUG_ON(atomic_read(&(jl
->j_commit_left
)) != 1);
1095 /* If there was a write error in the journal - we can't commit
1096 * this transaction - it will be invalid and, if successful,
1097 * will just end up propogating the write error out to
1098 * the file system. */
1099 if (likely(!retval
&& !reiserfs_is_journal_aborted (journal
))) {
1100 if (buffer_dirty(jl
->j_commit_bh
))
1102 mark_buffer_dirty(jl
->j_commit_bh
) ;
1103 sync_dirty_buffer(jl
->j_commit_bh
) ;
1106 wait_on_buffer(jl
->j_commit_bh
);
1108 check_barrier_completion(s
, jl
->j_commit_bh
);
1110 /* If there was a write error in the journal - we can't commit this
1111 * transaction - it will be invalid and, if successful, will just end
1112 * up propogating the write error out to the filesystem. */
1113 if (unlikely(!buffer_uptodate(jl
->j_commit_bh
))) {
1114 #ifdef CONFIG_REISERFS_CHECK
1115 reiserfs_warning(s
, "journal-615: buffer write failed");
1119 bforget(jl
->j_commit_bh
);
1120 if (journal
->j_last_commit_id
!= 0 &&
1121 (jl
->j_trans_id
- journal
->j_last_commit_id
) != 1) {
1122 reiserfs_warning(s
, "clm-2200: last commit %lu, current %lu",
1123 journal
->j_last_commit_id
, jl
->j_trans_id
);
1125 journal
->j_last_commit_id
= jl
->j_trans_id
;
1127 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1128 cleanup_freed_for_journal_list(s
, jl
);
1130 retval
= retval
? retval
: journal
->j_errno
;
1132 /* mark the metadata dirty */
1134 dirty_one_transaction(s
, jl
);
1135 atomic_dec(&(jl
->j_commit_left
));
1138 atomic_set(&(jl
->j_older_commits_done
), 1);
1140 up(&jl
->j_commit_lock
);
1142 put_journal_list(s
, jl
);
1145 reiserfs_abort(s
, retval
, "Journal write error in %s",
1152 ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1153 ** returns NULL if it can't find anything
1155 static struct reiserfs_journal_list
*find_newer_jl_for_cn(struct
1156 reiserfs_journal_cnode
1159 struct super_block
*sb
= cn
->sb
;
1160 b_blocknr_t blocknr
= cn
->blocknr
;
1164 if (cn
->sb
== sb
&& cn
->blocknr
== blocknr
&& cn
->jlist
) {
1172 static void remove_journal_hash(struct super_block
*,
1173 struct reiserfs_journal_cnode
**,
1174 struct reiserfs_journal_list
*, unsigned long,
1178 ** once all the real blocks have been flushed, it is safe to remove them from the
1179 ** journal list for this transaction. Aside from freeing the cnode, this also allows the
1180 ** block to be reallocated for data blocks if it had been deleted.
1182 static void remove_all_from_journal_list(struct super_block
*p_s_sb
,
1183 struct reiserfs_journal_list
*jl
,
1186 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
1187 struct reiserfs_journal_cnode
*cn
, *last
;
1188 cn
= jl
->j_realblock
;
1190 /* which is better, to lock once around the whole loop, or
1191 ** to lock for each call to remove_journal_hash?
1194 if (cn
->blocknr
!= 0) {
1196 reiserfs_warning(p_s_sb
,
1197 "block %u, bh is %d, state %ld",
1198 cn
->blocknr
, cn
->bh
? 1 : 0,
1202 remove_journal_hash(p_s_sb
, journal
->j_list_hash_table
,
1203 jl
, cn
->blocknr
, 1);
1207 free_cnode(p_s_sb
, last
);
1209 jl
->j_realblock
= NULL
;
1213 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1214 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1215 ** releasing blocks in this transaction for reuse as data blocks.
1216 ** called by flush_journal_list, before it calls remove_all_from_journal_list
1219 static int _update_journal_header_block(struct super_block
*p_s_sb
,
1220 unsigned long offset
,
1221 unsigned long trans_id
)
1223 struct reiserfs_journal_header
*jh
;
1224 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
1226 if (reiserfs_is_journal_aborted(journal
))
1229 if (trans_id
>= journal
->j_last_flush_trans_id
) {
1230 if (buffer_locked((journal
->j_header_bh
))) {
1231 wait_on_buffer((journal
->j_header_bh
));
1232 if (unlikely(!buffer_uptodate(journal
->j_header_bh
))) {
1233 #ifdef CONFIG_REISERFS_CHECK
1234 reiserfs_warning(p_s_sb
,
1235 "journal-699: buffer write failed");
1240 journal
->j_last_flush_trans_id
= trans_id
;
1241 journal
->j_first_unflushed_offset
= offset
;
1242 jh
= (struct reiserfs_journal_header
*)(journal
->j_header_bh
->
1244 jh
->j_last_flush_trans_id
= cpu_to_le32(trans_id
);
1245 jh
->j_first_unflushed_offset
= cpu_to_le32(offset
);
1246 jh
->j_mount_id
= cpu_to_le32(journal
->j_mount_id
);
1248 if (reiserfs_barrier_flush(p_s_sb
)) {
1250 lock_buffer(journal
->j_header_bh
);
1251 ret
= submit_barrier_buffer(journal
->j_header_bh
);
1252 if (ret
== -EOPNOTSUPP
) {
1253 set_buffer_uptodate(journal
->j_header_bh
);
1254 disable_barrier(p_s_sb
);
1257 wait_on_buffer(journal
->j_header_bh
);
1258 check_barrier_completion(p_s_sb
, journal
->j_header_bh
);
1261 set_buffer_dirty(journal
->j_header_bh
);
1262 sync_dirty_buffer(journal
->j_header_bh
);
1264 if (!buffer_uptodate(journal
->j_header_bh
)) {
1265 reiserfs_warning(p_s_sb
,
1266 "journal-837: IO error during journal replay");
1273 static int update_journal_header_block(struct super_block
*p_s_sb
,
1274 unsigned long offset
,
1275 unsigned long trans_id
)
1277 return _update_journal_header_block(p_s_sb
, offset
, trans_id
);
1281 ** flush any and all journal lists older than you are
1282 ** can only be called from flush_journal_list
1284 static int flush_older_journal_lists(struct super_block
*p_s_sb
,
1285 struct reiserfs_journal_list
*jl
)
1287 struct list_head
*entry
;
1288 struct reiserfs_journal_list
*other_jl
;
1289 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
1290 unsigned long trans_id
= jl
->j_trans_id
;
1292 /* we know we are the only ones flushing things, no extra race
1293 * protection is required.
1296 entry
= journal
->j_journal_list
.next
;
1298 if (entry
== &journal
->j_journal_list
)
1300 other_jl
= JOURNAL_LIST_ENTRY(entry
);
1301 if (other_jl
->j_trans_id
< trans_id
) {
1302 BUG_ON(other_jl
->j_refcount
<= 0);
1303 /* do not flush all */
1304 flush_journal_list(p_s_sb
, other_jl
, 0);
1306 /* other_jl is now deleted from the list */
1312 static void del_from_work_list(struct super_block
*s
,
1313 struct reiserfs_journal_list
*jl
)
1315 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1316 if (!list_empty(&jl
->j_working_list
)) {
1317 list_del_init(&jl
->j_working_list
);
1318 journal
->j_num_work_lists
--;
1322 /* flush a journal list, both commit and real blocks
1324 ** always set flushall to 1, unless you are calling from inside
1325 ** flush_journal_list
1327 ** IMPORTANT. This can only be called while there are no journal writers,
1328 ** and the journal is locked. That means it can only be called from
1329 ** do_journal_end, or by journal_release
1331 static int flush_journal_list(struct super_block
*s
,
1332 struct reiserfs_journal_list
*jl
, int flushall
)
1334 struct reiserfs_journal_list
*pjl
;
1335 struct reiserfs_journal_cnode
*cn
, *last
;
1339 struct buffer_head
*saved_bh
;
1340 unsigned long j_len_saved
= jl
->j_len
;
1341 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1344 BUG_ON(j_len_saved
<= 0);
1346 if (atomic_read(&journal
->j_wcount
) != 0) {
1348 "clm-2048: flush_journal_list called with wcount %d",
1349 atomic_read(&journal
->j_wcount
));
1351 BUG_ON(jl
->j_trans_id
== 0);
1353 /* if flushall == 0, the lock is already held */
1355 down(&journal
->j_flush_sem
);
1356 } else if (!down_trylock(&journal
->j_flush_sem
)) {
1361 if (j_len_saved
> journal
->j_trans_max
) {
1363 "journal-715: flush_journal_list, length is %lu, trans id %lu\n",
1364 j_len_saved
, jl
->j_trans_id
);
1370 /* if all the work is already done, get out of here */
1371 if (atomic_read(&(jl
->j_nonzerolen
)) <= 0 &&
1372 atomic_read(&(jl
->j_commit_left
)) <= 0) {
1373 goto flush_older_and_return
;
1376 /* start by putting the commit list on disk. This will also flush
1377 ** the commit lists of any olders transactions
1379 flush_commit_list(s
, jl
, 1);
1381 if (!(jl
->j_state
& LIST_DIRTY
)
1382 && !reiserfs_is_journal_aborted(journal
))
1385 /* are we done now? */
1386 if (atomic_read(&(jl
->j_nonzerolen
)) <= 0 &&
1387 atomic_read(&(jl
->j_commit_left
)) <= 0) {
1388 goto flush_older_and_return
;
1391 /* loop through each cnode, see if we need to write it,
1392 ** or wait on a more recent transaction, or just ignore it
1394 if (atomic_read(&(journal
->j_wcount
)) != 0) {
1396 "journal-844: panic journal list is flushing, wcount is not 0\n");
1398 cn
= jl
->j_realblock
;
1403 /* blocknr of 0 is no longer in the hash, ignore it */
1404 if (cn
->blocknr
== 0) {
1408 /* This transaction failed commit. Don't write out to the disk */
1409 if (!(jl
->j_state
& LIST_DIRTY
))
1412 pjl
= find_newer_jl_for_cn(cn
);
1413 /* the order is important here. We check pjl to make sure we
1414 ** don't clear BH_JDirty_wait if we aren't the one writing this
1417 if (!pjl
&& cn
->bh
) {
1420 /* we do this to make sure nobody releases the buffer while
1421 ** we are working with it
1425 if (buffer_journal_dirty(saved_bh
)) {
1426 BUG_ON(!can_dirty(cn
));
1429 } else if (can_dirty(cn
)) {
1430 /* everything with !pjl && jwait should be writable */
1435 /* if someone has this block in a newer transaction, just make
1436 ** sure they are commited, and don't try writing it to disk
1439 if (atomic_read(&pjl
->j_commit_left
))
1440 flush_commit_list(s
, pjl
, 1);
1444 /* bh == NULL when the block got to disk on its own, OR,
1445 ** the block got freed in a future transaction
1447 if (saved_bh
== NULL
) {
1451 /* this should never happen. kupdate_one_transaction has this list
1452 ** locked while it works, so we should never see a buffer here that
1453 ** is not marked JDirty_wait
1455 if ((!was_jwait
) && !buffer_locked(saved_bh
)) {
1457 "journal-813: BAD! buffer %llu %cdirty %cjwait, "
1458 "not in a newer tranasction",
1459 (unsigned long long)saved_bh
->
1460 b_blocknr
, was_dirty
? ' ' : '!',
1461 was_jwait
? ' ' : '!');
1464 /* we inc again because saved_bh gets decremented at free_cnode */
1466 set_bit(BLOCK_NEEDS_FLUSH
, &cn
->state
);
1467 lock_buffer(saved_bh
);
1468 BUG_ON(cn
->blocknr
!= saved_bh
->b_blocknr
);
1469 if (buffer_dirty(saved_bh
))
1470 submit_logged_buffer(saved_bh
);
1472 unlock_buffer(saved_bh
);
1476 "clm-2082: Unable to flush buffer %llu in %s",
1477 (unsigned long long)saved_bh
->
1478 b_blocknr
, __FUNCTION__
);
1484 /* we incremented this to keep others from taking the buffer head away */
1486 if (atomic_read(&(saved_bh
->b_count
)) < 0) {
1488 "journal-945: saved_bh->b_count < 0");
1493 cn
= jl
->j_realblock
;
1495 if (test_bit(BLOCK_NEEDS_FLUSH
, &cn
->state
)) {
1498 "journal-1011: cn->bh is NULL\n");
1500 wait_on_buffer(cn
->bh
);
1503 "journal-1012: cn->bh is NULL\n");
1505 if (unlikely(!buffer_uptodate(cn
->bh
))) {
1506 #ifdef CONFIG_REISERFS_CHECK
1508 "journal-949: buffer write failed\n");
1512 /* note, we must clear the JDirty_wait bit after the up to date
1513 ** check, otherwise we race against our flushpage routine
1515 BUG_ON(!test_clear_buffer_journal_dirty
1518 /* undo the inc from journal_mark_dirty */
1527 reiserfs_abort(s
, -EIO
,
1528 "Write error while pushing transaction to disk in %s",
1530 flush_older_and_return
:
1532 /* before we can update the journal header block, we _must_ flush all
1533 ** real blocks from all older transactions to disk. This is because
1534 ** once the header block is updated, this transaction will not be
1535 ** replayed after a crash
1538 flush_older_journal_lists(s
, jl
);
1541 err
= journal
->j_errno
;
1542 /* before we can remove everything from the hash tables for this
1543 ** transaction, we must make sure it can never be replayed
1545 ** since we are only called from do_journal_end, we know for sure there
1546 ** are no allocations going on while we are flushing journal lists. So,
1547 ** we only need to update the journal header block for the last list
1550 if (!err
&& flushall
) {
1552 update_journal_header_block(s
,
1553 (jl
->j_start
+ jl
->j_len
+
1554 2) % SB_ONDISK_JOURNAL_SIZE(s
),
1557 reiserfs_abort(s
, -EIO
,
1558 "Write error while updating journal header in %s",
1561 remove_all_from_journal_list(s
, jl
, 0);
1562 list_del_init(&jl
->j_list
);
1563 journal
->j_num_lists
--;
1564 del_from_work_list(s
, jl
);
1566 if (journal
->j_last_flush_id
!= 0 &&
1567 (jl
->j_trans_id
- journal
->j_last_flush_id
) != 1) {
1568 reiserfs_warning(s
, "clm-2201: last flush %lu, current %lu",
1569 journal
->j_last_flush_id
, jl
->j_trans_id
);
1571 journal
->j_last_flush_id
= jl
->j_trans_id
;
1573 /* not strictly required since we are freeing the list, but it should
1574 * help find code using dead lists later on
1577 atomic_set(&(jl
->j_nonzerolen
), 0);
1579 jl
->j_realblock
= NULL
;
1580 jl
->j_commit_bh
= NULL
;
1583 put_journal_list(s
, jl
);
1585 up(&journal
->j_flush_sem
);
1590 static int write_one_transaction(struct super_block
*s
,
1591 struct reiserfs_journal_list
*jl
,
1592 struct buffer_chunk
*chunk
)
1594 struct reiserfs_journal_cnode
*cn
;
1597 jl
->j_state
|= LIST_TOUCHED
;
1598 del_from_work_list(s
, jl
);
1599 if (jl
->j_len
== 0 || atomic_read(&jl
->j_nonzerolen
) == 0) {
1603 cn
= jl
->j_realblock
;
1605 /* if the blocknr == 0, this has been cleared from the hash,
1608 if (cn
->blocknr
== 0) {
1611 if (cn
->bh
&& can_dirty(cn
) && buffer_dirty(cn
->bh
)) {
1612 struct buffer_head
*tmp_bh
;
1613 /* we can race against journal_mark_freed when we try
1614 * to lock_buffer(cn->bh), so we have to inc the buffer
1615 * count, and recheck things after locking
1619 lock_buffer(tmp_bh
);
1620 if (cn
->bh
&& can_dirty(cn
) && buffer_dirty(tmp_bh
)) {
1621 if (!buffer_journal_dirty(tmp_bh
) ||
1622 buffer_journal_prepared(tmp_bh
))
1624 add_to_chunk(chunk
, tmp_bh
, NULL
, write_chunk
);
1627 /* note, cn->bh might be null now */
1628 unlock_buffer(tmp_bh
);
1639 /* used by flush_commit_list */
1640 static int dirty_one_transaction(struct super_block
*s
,
1641 struct reiserfs_journal_list
*jl
)
1643 struct reiserfs_journal_cnode
*cn
;
1644 struct reiserfs_journal_list
*pjl
;
1647 jl
->j_state
|= LIST_DIRTY
;
1648 cn
= jl
->j_realblock
;
1650 /* look for a more recent transaction that logged this
1651 ** buffer. Only the most recent transaction with a buffer in
1652 ** it is allowed to send that buffer to disk
1654 pjl
= find_newer_jl_for_cn(cn
);
1655 if (!pjl
&& cn
->blocknr
&& cn
->bh
1656 && buffer_journal_dirty(cn
->bh
)) {
1657 BUG_ON(!can_dirty(cn
));
1658 /* if the buffer is prepared, it will either be logged
1659 * or restored. If restored, we need to make sure
1660 * it actually gets marked dirty
1662 clear_buffer_journal_new(cn
->bh
);
1663 if (buffer_journal_prepared(cn
->bh
)) {
1664 set_buffer_journal_restore_dirty(cn
->bh
);
1666 set_buffer_journal_test(cn
->bh
);
1667 mark_buffer_dirty(cn
->bh
);
1675 static int kupdate_transactions(struct super_block
*s
,
1676 struct reiserfs_journal_list
*jl
,
1677 struct reiserfs_journal_list
**next_jl
,
1678 unsigned long *next_trans_id
,
1679 int num_blocks
, int num_trans
)
1683 int transactions_flushed
= 0;
1684 unsigned long orig_trans_id
= jl
->j_trans_id
;
1685 struct buffer_chunk chunk
;
1686 struct list_head
*entry
;
1687 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1690 down(&journal
->j_flush_sem
);
1691 if (!journal_list_still_alive(s
, orig_trans_id
)) {
1695 /* we've got j_flush_sem held, nobody is going to delete any
1696 * of these lists out from underneath us
1698 while ((num_trans
&& transactions_flushed
< num_trans
) ||
1699 (!num_trans
&& written
< num_blocks
)) {
1701 if (jl
->j_len
== 0 || (jl
->j_state
& LIST_TOUCHED
) ||
1702 atomic_read(&jl
->j_commit_left
)
1703 || !(jl
->j_state
& LIST_DIRTY
)) {
1704 del_from_work_list(s
, jl
);
1707 ret
= write_one_transaction(s
, jl
, &chunk
);
1711 transactions_flushed
++;
1713 entry
= jl
->j_list
.next
;
1716 if (entry
== &journal
->j_journal_list
) {
1719 jl
= JOURNAL_LIST_ENTRY(entry
);
1721 /* don't bother with older transactions */
1722 if (jl
->j_trans_id
<= orig_trans_id
)
1726 write_chunk(&chunk
);
1730 up(&journal
->j_flush_sem
);
1734 /* for o_sync and fsync heavy applications, they tend to use
1735 ** all the journa list slots with tiny transactions. These
1736 ** trigger lots and lots of calls to update the header block, which
1737 ** adds seeks and slows things down.
1739 ** This function tries to clear out a large chunk of the journal lists
1740 ** at once, which makes everything faster since only the newest journal
1741 ** list updates the header block
1743 static int flush_used_journal_lists(struct super_block
*s
,
1744 struct reiserfs_journal_list
*jl
)
1746 unsigned long len
= 0;
1747 unsigned long cur_len
;
1751 struct reiserfs_journal_list
*tjl
;
1752 struct reiserfs_journal_list
*flush_jl
;
1753 unsigned long trans_id
;
1754 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1756 flush_jl
= tjl
= jl
;
1758 /* in data logging mode, try harder to flush a lot of blocks */
1759 if (reiserfs_data_log(s
))
1761 /* flush for 256 transactions or limit blocks, whichever comes first */
1762 for (i
= 0; i
< 256 && len
< limit
; i
++) {
1763 if (atomic_read(&tjl
->j_commit_left
) ||
1764 tjl
->j_trans_id
< jl
->j_trans_id
) {
1767 cur_len
= atomic_read(&tjl
->j_nonzerolen
);
1769 tjl
->j_state
&= ~LIST_TOUCHED
;
1773 if (tjl
->j_list
.next
== &journal
->j_journal_list
)
1775 tjl
= JOURNAL_LIST_ENTRY(tjl
->j_list
.next
);
1777 /* try to find a group of blocks we can flush across all the
1778 ** transactions, but only bother if we've actually spanned
1779 ** across multiple lists
1781 if (flush_jl
!= jl
) {
1782 ret
= kupdate_transactions(s
, jl
, &tjl
, &trans_id
, len
, i
);
1784 flush_journal_list(s
, flush_jl
, 1);
1789 ** removes any nodes in table with name block and dev as bh.
1790 ** only touchs the hnext and hprev pointers.
1792 void remove_journal_hash(struct super_block
*sb
,
1793 struct reiserfs_journal_cnode
**table
,
1794 struct reiserfs_journal_list
*jl
,
1795 unsigned long block
, int remove_freed
)
1797 struct reiserfs_journal_cnode
*cur
;
1798 struct reiserfs_journal_cnode
**head
;
1800 head
= &(journal_hash(table
, sb
, block
));
1806 if (cur
->blocknr
== block
&& cur
->sb
== sb
1807 && (jl
== NULL
|| jl
== cur
->jlist
)
1808 && (!test_bit(BLOCK_FREED
, &cur
->state
) || remove_freed
)) {
1810 cur
->hnext
->hprev
= cur
->hprev
;
1813 cur
->hprev
->hnext
= cur
->hnext
;
1820 if (cur
->bh
&& cur
->jlist
) /* anybody who clears the cur->bh will also dec the nonzerolen */
1821 atomic_dec(&(cur
->jlist
->j_nonzerolen
));
1829 static void free_journal_ram(struct super_block
*p_s_sb
)
1831 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
1832 kfree(journal
->j_current_jl
);
1833 journal
->j_num_lists
--;
1835 vfree(journal
->j_cnode_free_orig
);
1836 free_list_bitmaps(p_s_sb
, journal
->j_list_bitmap
);
1837 free_bitmap_nodes(p_s_sb
); /* must be after free_list_bitmaps */
1838 if (journal
->j_header_bh
) {
1839 brelse(journal
->j_header_bh
);
1841 /* j_header_bh is on the journal dev, make sure not to release the journal
1842 * dev until we brelse j_header_bh
1844 release_journal_dev(p_s_sb
, journal
);
1849 ** call on unmount. Only set error to 1 if you haven't made your way out
1850 ** of read_super() yet. Any other caller must keep error at 0.
1852 static int do_journal_release(struct reiserfs_transaction_handle
*th
,
1853 struct super_block
*p_s_sb
, int error
)
1855 struct reiserfs_transaction_handle myth
;
1857 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
1859 /* we only want to flush out transactions if we were called with error == 0
1861 if (!error
&& !(p_s_sb
->s_flags
& MS_RDONLY
)) {
1862 /* end the current trans */
1863 BUG_ON(!th
->t_trans_id
);
1864 do_journal_end(th
, p_s_sb
, 10, FLUSH_ALL
);
1866 /* make sure something gets logged to force our way into the flush code */
1867 if (!journal_join(&myth
, p_s_sb
, 1)) {
1868 reiserfs_prepare_for_journal(p_s_sb
,
1869 SB_BUFFER_WITH_SB(p_s_sb
),
1871 journal_mark_dirty(&myth
, p_s_sb
,
1872 SB_BUFFER_WITH_SB(p_s_sb
));
1873 do_journal_end(&myth
, p_s_sb
, 1, FLUSH_ALL
);
1878 /* this also catches errors during the do_journal_end above */
1879 if (!error
&& reiserfs_is_journal_aborted(journal
)) {
1880 memset(&myth
, 0, sizeof(myth
));
1881 if (!journal_join_abort(&myth
, p_s_sb
, 1)) {
1882 reiserfs_prepare_for_journal(p_s_sb
,
1883 SB_BUFFER_WITH_SB(p_s_sb
),
1885 journal_mark_dirty(&myth
, p_s_sb
,
1886 SB_BUFFER_WITH_SB(p_s_sb
));
1887 do_journal_end(&myth
, p_s_sb
, 1, FLUSH_ALL
);
1891 reiserfs_mounted_fs_count
--;
1892 /* wait for all commits to finish */
1893 cancel_delayed_work(&SB_JOURNAL(p_s_sb
)->j_work
);
1894 flush_workqueue(commit_wq
);
1895 if (!reiserfs_mounted_fs_count
) {
1896 destroy_workqueue(commit_wq
);
1900 free_journal_ram(p_s_sb
);
1906 ** call on unmount. flush all journal trans, release all alloc'd ram
1908 int journal_release(struct reiserfs_transaction_handle
*th
,
1909 struct super_block
*p_s_sb
)
1911 return do_journal_release(th
, p_s_sb
, 0);
1915 ** only call from an error condition inside reiserfs_read_super!
1917 int journal_release_error(struct reiserfs_transaction_handle
*th
,
1918 struct super_block
*p_s_sb
)
1920 return do_journal_release(th
, p_s_sb
, 1);
1923 /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
1924 static int journal_compare_desc_commit(struct super_block
*p_s_sb
,
1925 struct reiserfs_journal_desc
*desc
,
1926 struct reiserfs_journal_commit
*commit
)
1928 if (get_commit_trans_id(commit
) != get_desc_trans_id(desc
) ||
1929 get_commit_trans_len(commit
) != get_desc_trans_len(desc
) ||
1930 get_commit_trans_len(commit
) > SB_JOURNAL(p_s_sb
)->j_trans_max
||
1931 get_commit_trans_len(commit
) <= 0) {
1937 /* returns 0 if it did not find a description block
1938 ** returns -1 if it found a corrupt commit block
1939 ** returns 1 if both desc and commit were valid
1941 static int journal_transaction_is_valid(struct super_block
*p_s_sb
,
1942 struct buffer_head
*d_bh
,
1943 unsigned long *oldest_invalid_trans_id
,
1944 unsigned long *newest_mount_id
)
1946 struct reiserfs_journal_desc
*desc
;
1947 struct reiserfs_journal_commit
*commit
;
1948 struct buffer_head
*c_bh
;
1949 unsigned long offset
;
1954 desc
= (struct reiserfs_journal_desc
*)d_bh
->b_data
;
1955 if (get_desc_trans_len(desc
) > 0
1956 && !memcmp(get_journal_desc_magic(d_bh
), JOURNAL_DESC_MAGIC
, 8)) {
1957 if (oldest_invalid_trans_id
&& *oldest_invalid_trans_id
1958 && get_desc_trans_id(desc
) > *oldest_invalid_trans_id
) {
1959 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
1960 "journal-986: transaction "
1961 "is valid returning because trans_id %d is greater than "
1962 "oldest_invalid %lu",
1963 get_desc_trans_id(desc
),
1964 *oldest_invalid_trans_id
);
1968 && *newest_mount_id
> get_desc_mount_id(desc
)) {
1969 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
1970 "journal-1087: transaction "
1971 "is valid returning because mount_id %d is less than "
1972 "newest_mount_id %lu",
1973 get_desc_mount_id(desc
),
1977 if (get_desc_trans_len(desc
) > SB_JOURNAL(p_s_sb
)->j_trans_max
) {
1978 reiserfs_warning(p_s_sb
,
1979 "journal-2018: Bad transaction length %d encountered, ignoring transaction",
1980 get_desc_trans_len(desc
));
1983 offset
= d_bh
->b_blocknr
- SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
);
1985 /* ok, we have a journal description block, lets see if the transaction was valid */
1987 journal_bread(p_s_sb
,
1988 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
1989 ((offset
+ get_desc_trans_len(desc
) +
1990 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb
)));
1993 commit
= (struct reiserfs_journal_commit
*)c_bh
->b_data
;
1994 if (journal_compare_desc_commit(p_s_sb
, desc
, commit
)) {
1995 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
1996 "journal_transaction_is_valid, commit offset %ld had bad "
1997 "time %d or length %d",
1999 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
),
2000 get_commit_trans_id(commit
),
2001 get_commit_trans_len(commit
));
2003 if (oldest_invalid_trans_id
) {
2004 *oldest_invalid_trans_id
=
2005 get_desc_trans_id(desc
);
2006 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2008 "transaction_is_valid setting oldest invalid trans_id "
2010 get_desc_trans_id(desc
));
2015 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2016 "journal-1006: found valid "
2017 "transaction start offset %llu, len %d id %d",
2019 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
),
2020 get_desc_trans_len(desc
),
2021 get_desc_trans_id(desc
));
2028 static void brelse_array(struct buffer_head
**heads
, int num
)
2031 for (i
= 0; i
< num
; i
++) {
2037 ** given the start, and values for the oldest acceptable transactions,
2038 ** this either reads in a replays a transaction, or returns because the transaction
2039 ** is invalid, or too old.
2041 static int journal_read_transaction(struct super_block
*p_s_sb
,
2042 unsigned long cur_dblock
,
2043 unsigned long oldest_start
,
2044 unsigned long oldest_trans_id
,
2045 unsigned long newest_mount_id
)
2047 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
2048 struct reiserfs_journal_desc
*desc
;
2049 struct reiserfs_journal_commit
*commit
;
2050 unsigned long trans_id
= 0;
2051 struct buffer_head
*c_bh
;
2052 struct buffer_head
*d_bh
;
2053 struct buffer_head
**log_blocks
= NULL
;
2054 struct buffer_head
**real_blocks
= NULL
;
2055 unsigned long trans_offset
;
2059 d_bh
= journal_bread(p_s_sb
, cur_dblock
);
2062 desc
= (struct reiserfs_journal_desc
*)d_bh
->b_data
;
2063 trans_offset
= d_bh
->b_blocknr
- SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
);
2064 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
, "journal-1037: "
2065 "journal_read_transaction, offset %llu, len %d mount_id %d",
2066 d_bh
->b_blocknr
- SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
),
2067 get_desc_trans_len(desc
), get_desc_mount_id(desc
));
2068 if (get_desc_trans_id(desc
) < oldest_trans_id
) {
2069 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
, "journal-1039: "
2070 "journal_read_trans skipping because %lu is too old",
2072 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
));
2076 if (get_desc_mount_id(desc
) != newest_mount_id
) {
2077 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
, "journal-1146: "
2078 "journal_read_trans skipping because %d is != "
2079 "newest_mount_id %lu", get_desc_mount_id(desc
),
2084 c_bh
= journal_bread(p_s_sb
, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2085 ((trans_offset
+ get_desc_trans_len(desc
) + 1) %
2086 SB_ONDISK_JOURNAL_SIZE(p_s_sb
)));
2091 commit
= (struct reiserfs_journal_commit
*)c_bh
->b_data
;
2092 if (journal_compare_desc_commit(p_s_sb
, desc
, commit
)) {
2093 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2094 "journal_read_transaction, "
2095 "commit offset %llu had bad time %d or length %d",
2097 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
),
2098 get_commit_trans_id(commit
),
2099 get_commit_trans_len(commit
));
2104 trans_id
= get_desc_trans_id(desc
);
2105 /* now we know we've got a good transaction, and it was inside the valid time ranges */
2106 log_blocks
= kmalloc(get_desc_trans_len(desc
) *
2107 sizeof(struct buffer_head
*), GFP_NOFS
);
2108 real_blocks
= kmalloc(get_desc_trans_len(desc
) *
2109 sizeof(struct buffer_head
*), GFP_NOFS
);
2110 if (!log_blocks
|| !real_blocks
) {
2115 reiserfs_warning(p_s_sb
,
2116 "journal-1169: kmalloc failed, unable to mount FS");
2119 /* get all the buffer heads */
2120 trans_half
= journal_trans_half(p_s_sb
->s_blocksize
);
2121 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2123 journal_getblk(p_s_sb
,
2124 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2126 i
) % SB_ONDISK_JOURNAL_SIZE(p_s_sb
));
2127 if (i
< trans_half
) {
2130 le32_to_cpu(desc
->j_realblock
[i
]));
2134 le32_to_cpu(commit
->
2135 j_realblock
[i
- trans_half
]));
2137 if (real_blocks
[i
]->b_blocknr
> SB_BLOCK_COUNT(p_s_sb
)) {
2138 reiserfs_warning(p_s_sb
,
2139 "journal-1207: REPLAY FAILURE fsck required! Block to replay is outside of filesystem");
2142 /* make sure we don't try to replay onto log or reserved area */
2143 if (is_block_in_log_or_reserved_area
2144 (p_s_sb
, real_blocks
[i
]->b_blocknr
)) {
2145 reiserfs_warning(p_s_sb
,
2146 "journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block");
2148 brelse_array(log_blocks
, i
);
2149 brelse_array(real_blocks
, i
);
2157 /* read in the log blocks, memcpy to the corresponding real block */
2158 ll_rw_block(READ
, get_desc_trans_len(desc
), log_blocks
);
2159 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2160 wait_on_buffer(log_blocks
[i
]);
2161 if (!buffer_uptodate(log_blocks
[i
])) {
2162 reiserfs_warning(p_s_sb
,
2163 "journal-1212: REPLAY FAILURE fsck required! buffer write failed");
2164 brelse_array(log_blocks
+ i
,
2165 get_desc_trans_len(desc
) - i
);
2166 brelse_array(real_blocks
, get_desc_trans_len(desc
));
2173 memcpy(real_blocks
[i
]->b_data
, log_blocks
[i
]->b_data
,
2174 real_blocks
[i
]->b_size
);
2175 set_buffer_uptodate(real_blocks
[i
]);
2176 brelse(log_blocks
[i
]);
2178 /* flush out the real blocks */
2179 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2180 set_buffer_dirty(real_blocks
[i
]);
2181 ll_rw_block(SWRITE
, 1, real_blocks
+ i
);
2183 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2184 wait_on_buffer(real_blocks
[i
]);
2185 if (!buffer_uptodate(real_blocks
[i
])) {
2186 reiserfs_warning(p_s_sb
,
2187 "journal-1226: REPLAY FAILURE, fsck required! buffer write failed");
2188 brelse_array(real_blocks
+ i
,
2189 get_desc_trans_len(desc
) - i
);
2196 brelse(real_blocks
[i
]);
2199 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2200 ((trans_offset
+ get_desc_trans_len(desc
) +
2201 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb
));
2202 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2203 "journal-1095: setting journal " "start to offset %ld",
2204 cur_dblock
- SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
));
2206 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
2207 journal
->j_start
= cur_dblock
- SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
);
2208 journal
->j_last_flush_trans_id
= trans_id
;
2209 journal
->j_trans_id
= trans_id
+ 1;
2217 /* This function reads blocks starting from block and to max_block of bufsize
2218 size (but no more than BUFNR blocks at a time). This proved to improve
2219 mounting speed on self-rebuilding raid5 arrays at least.
2220 Right now it is only used from journal code. But later we might use it
2222 Note: Do not use journal_getblk/sb_getblk functions here! */
2223 static struct buffer_head
*reiserfs_breada(struct block_device
*dev
, int block
,
2224 int bufsize
, unsigned int max_block
)
2226 struct buffer_head
*bhlist
[BUFNR
];
2227 unsigned int blocks
= BUFNR
;
2228 struct buffer_head
*bh
;
2231 bh
= __getblk(dev
, block
, bufsize
);
2232 if (buffer_uptodate(bh
))
2235 if (block
+ BUFNR
> max_block
) {
2236 blocks
= max_block
- block
;
2240 for (i
= 1; i
< blocks
; i
++) {
2241 bh
= __getblk(dev
, block
+ i
, bufsize
);
2242 if (buffer_uptodate(bh
)) {
2248 ll_rw_block(READ
, j
, bhlist
);
2249 for (i
= 1; i
< j
; i
++)
2253 if (buffer_uptodate(bh
))
2260 ** read and replay the log
2261 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2262 ** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
2264 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2266 ** On exit, it sets things up so the first transaction will work correctly.
2268 static int journal_read(struct super_block
*p_s_sb
)
2270 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
2271 struct reiserfs_journal_desc
*desc
;
2272 unsigned long oldest_trans_id
= 0;
2273 unsigned long oldest_invalid_trans_id
= 0;
2275 unsigned long oldest_start
= 0;
2276 unsigned long cur_dblock
= 0;
2277 unsigned long newest_mount_id
= 9;
2278 struct buffer_head
*d_bh
;
2279 struct reiserfs_journal_header
*jh
;
2280 int valid_journal_header
= 0;
2281 int replay_count
= 0;
2282 int continue_replay
= 1;
2284 char b
[BDEVNAME_SIZE
];
2286 cur_dblock
= SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
);
2287 reiserfs_info(p_s_sb
, "checking transaction log (%s)\n",
2288 bdevname(journal
->j_dev_bd
, b
));
2289 start
= get_seconds();
2291 /* step 1, read in the journal header block. Check the transaction it says
2292 ** is the first unflushed, and if that transaction is not valid,
2295 journal
->j_header_bh
= journal_bread(p_s_sb
,
2296 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
)
2297 + SB_ONDISK_JOURNAL_SIZE(p_s_sb
));
2298 if (!journal
->j_header_bh
) {
2301 jh
= (struct reiserfs_journal_header
*)(journal
->j_header_bh
->b_data
);
2302 if (le32_to_cpu(jh
->j_first_unflushed_offset
) >= 0 &&
2303 le32_to_cpu(jh
->j_first_unflushed_offset
) <
2304 SB_ONDISK_JOURNAL_SIZE(p_s_sb
)
2305 && le32_to_cpu(jh
->j_last_flush_trans_id
) > 0) {
2307 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2308 le32_to_cpu(jh
->j_first_unflushed_offset
);
2309 oldest_trans_id
= le32_to_cpu(jh
->j_last_flush_trans_id
) + 1;
2310 newest_mount_id
= le32_to_cpu(jh
->j_mount_id
);
2311 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2312 "journal-1153: found in "
2313 "header: first_unflushed_offset %d, last_flushed_trans_id "
2314 "%lu", le32_to_cpu(jh
->j_first_unflushed_offset
),
2315 le32_to_cpu(jh
->j_last_flush_trans_id
));
2316 valid_journal_header
= 1;
2318 /* now, we try to read the first unflushed offset. If it is not valid,
2319 ** there is nothing more we can do, and it makes no sense to read
2320 ** through the whole log.
2323 journal_bread(p_s_sb
,
2324 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2325 le32_to_cpu(jh
->j_first_unflushed_offset
));
2326 ret
= journal_transaction_is_valid(p_s_sb
, d_bh
, NULL
, NULL
);
2328 continue_replay
= 0;
2331 goto start_log_replay
;
2334 if (continue_replay
&& bdev_read_only(p_s_sb
->s_bdev
)) {
2335 reiserfs_warning(p_s_sb
,
2336 "clm-2076: device is readonly, unable to replay log");
2340 /* ok, there are transactions that need to be replayed. start with the first log block, find
2341 ** all the valid transactions, and pick out the oldest.
2343 while (continue_replay
2345 (SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2346 SB_ONDISK_JOURNAL_SIZE(p_s_sb
))) {
2347 /* Note that it is required for blocksize of primary fs device and journal
2348 device to be the same */
2350 reiserfs_breada(journal
->j_dev_bd
, cur_dblock
,
2351 p_s_sb
->s_blocksize
,
2352 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2353 SB_ONDISK_JOURNAL_SIZE(p_s_sb
));
2355 journal_transaction_is_valid(p_s_sb
, d_bh
,
2356 &oldest_invalid_trans_id
,
2359 desc
= (struct reiserfs_journal_desc
*)d_bh
->b_data
;
2360 if (oldest_start
== 0) { /* init all oldest_ values */
2361 oldest_trans_id
= get_desc_trans_id(desc
);
2362 oldest_start
= d_bh
->b_blocknr
;
2363 newest_mount_id
= get_desc_mount_id(desc
);
2364 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2365 "journal-1179: Setting "
2366 "oldest_start to offset %llu, trans_id %lu",
2368 SB_ONDISK_JOURNAL_1st_BLOCK
2369 (p_s_sb
), oldest_trans_id
);
2370 } else if (oldest_trans_id
> get_desc_trans_id(desc
)) {
2371 /* one we just read was older */
2372 oldest_trans_id
= get_desc_trans_id(desc
);
2373 oldest_start
= d_bh
->b_blocknr
;
2374 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2375 "journal-1180: Resetting "
2376 "oldest_start to offset %lu, trans_id %lu",
2378 SB_ONDISK_JOURNAL_1st_BLOCK
2379 (p_s_sb
), oldest_trans_id
);
2381 if (newest_mount_id
< get_desc_mount_id(desc
)) {
2382 newest_mount_id
= get_desc_mount_id(desc
);
2383 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2384 "journal-1299: Setting "
2385 "newest_mount_id to %d",
2386 get_desc_mount_id(desc
));
2388 cur_dblock
+= get_desc_trans_len(desc
) + 2;
2396 cur_dblock
= oldest_start
;
2397 if (oldest_trans_id
) {
2398 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2399 "journal-1206: Starting replay "
2400 "from offset %llu, trans_id %lu",
2401 cur_dblock
- SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
),
2406 while (continue_replay
&& oldest_trans_id
> 0) {
2408 journal_read_transaction(p_s_sb
, cur_dblock
, oldest_start
,
2409 oldest_trans_id
, newest_mount_id
);
2412 } else if (ret
!= 0) {
2416 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) + journal
->j_start
;
2418 if (cur_dblock
== oldest_start
)
2422 if (oldest_trans_id
== 0) {
2423 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
,
2424 "journal-1225: No valid " "transactions found");
2426 /* j_start does not get set correctly if we don't replay any transactions.
2427 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2428 ** copy the trans_id from the header
2430 if (valid_journal_header
&& replay_count
== 0) {
2431 journal
->j_start
= le32_to_cpu(jh
->j_first_unflushed_offset
);
2432 journal
->j_trans_id
=
2433 le32_to_cpu(jh
->j_last_flush_trans_id
) + 1;
2434 journal
->j_last_flush_trans_id
=
2435 le32_to_cpu(jh
->j_last_flush_trans_id
);
2436 journal
->j_mount_id
= le32_to_cpu(jh
->j_mount_id
) + 1;
2438 journal
->j_mount_id
= newest_mount_id
+ 1;
2440 reiserfs_debug(p_s_sb
, REISERFS_DEBUG_CODE
, "journal-1299: Setting "
2441 "newest_mount_id to %lu", journal
->j_mount_id
);
2442 journal
->j_first_unflushed_offset
= journal
->j_start
;
2443 if (replay_count
> 0) {
2444 reiserfs_info(p_s_sb
,
2445 "replayed %d transactions in %lu seconds\n",
2446 replay_count
, get_seconds() - start
);
2448 if (!bdev_read_only(p_s_sb
->s_bdev
) &&
2449 _update_journal_header_block(p_s_sb
, journal
->j_start
,
2450 journal
->j_last_flush_trans_id
)) {
2451 /* replay failed, caller must call free_journal_ram and abort
2459 static struct reiserfs_journal_list
*alloc_journal_list(struct super_block
*s
)
2461 struct reiserfs_journal_list
*jl
;
2462 jl
= kzalloc(sizeof(struct reiserfs_journal_list
),
2463 GFP_NOFS
| __GFP_NOFAIL
);
2464 INIT_LIST_HEAD(&jl
->j_list
);
2465 INIT_LIST_HEAD(&jl
->j_working_list
);
2466 INIT_LIST_HEAD(&jl
->j_tail_bh_list
);
2467 INIT_LIST_HEAD(&jl
->j_bh_list
);
2468 sema_init(&jl
->j_commit_lock
, 1);
2469 SB_JOURNAL(s
)->j_num_lists
++;
2470 get_journal_list(jl
);
2474 static void journal_list_init(struct super_block
*p_s_sb
)
2476 SB_JOURNAL(p_s_sb
)->j_current_jl
= alloc_journal_list(p_s_sb
);
2479 static int release_journal_dev(struct super_block
*super
,
2480 struct reiserfs_journal
*journal
)
2486 if (journal
->j_dev_file
!= NULL
) {
2487 result
= filp_close(journal
->j_dev_file
, NULL
);
2488 journal
->j_dev_file
= NULL
;
2489 journal
->j_dev_bd
= NULL
;
2490 } else if (journal
->j_dev_bd
!= NULL
) {
2491 result
= blkdev_put(journal
->j_dev_bd
);
2492 journal
->j_dev_bd
= NULL
;
2496 reiserfs_warning(super
,
2497 "sh-457: release_journal_dev: Cannot release journal device: %i",
2503 static int journal_init_dev(struct super_block
*super
,
2504 struct reiserfs_journal
*journal
,
2505 const char *jdev_name
)
2509 int blkdev_mode
= FMODE_READ
| FMODE_WRITE
;
2510 char b
[BDEVNAME_SIZE
];
2514 journal
->j_dev_bd
= NULL
;
2515 journal
->j_dev_file
= NULL
;
2516 jdev
= SB_ONDISK_JOURNAL_DEVICE(super
) ?
2517 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super
)) : super
->s_dev
;
2519 if (bdev_read_only(super
->s_bdev
))
2520 blkdev_mode
= FMODE_READ
;
2522 /* there is no "jdev" option and journal is on separate device */
2523 if ((!jdev_name
|| !jdev_name
[0])) {
2524 journal
->j_dev_bd
= open_by_devnum(jdev
, blkdev_mode
);
2525 if (IS_ERR(journal
->j_dev_bd
)) {
2526 result
= PTR_ERR(journal
->j_dev_bd
);
2527 journal
->j_dev_bd
= NULL
;
2528 reiserfs_warning(super
, "sh-458: journal_init_dev: "
2529 "cannot init journal device '%s': %i",
2530 __bdevname(jdev
, b
), result
);
2532 } else if (jdev
!= super
->s_dev
)
2533 set_blocksize(journal
->j_dev_bd
, super
->s_blocksize
);
2537 journal
->j_dev_file
= filp_open(jdev_name
, 0, 0);
2538 if (!IS_ERR(journal
->j_dev_file
)) {
2539 struct inode
*jdev_inode
= journal
->j_dev_file
->f_mapping
->host
;
2540 if (!S_ISBLK(jdev_inode
->i_mode
)) {
2541 reiserfs_warning(super
, "journal_init_dev: '%s' is "
2542 "not a block device", jdev_name
);
2544 release_journal_dev(super
, journal
);
2547 journal
->j_dev_bd
= I_BDEV(jdev_inode
);
2548 set_blocksize(journal
->j_dev_bd
, super
->s_blocksize
);
2549 reiserfs_info(super
,
2550 "journal_init_dev: journal device: %s\n",
2551 bdevname(journal
->j_dev_bd
, b
));
2554 result
= PTR_ERR(journal
->j_dev_file
);
2555 journal
->j_dev_file
= NULL
;
2556 reiserfs_warning(super
,
2557 "journal_init_dev: Cannot open '%s': %i",
2564 ** must be called once on fs mount. calls journal_read for you
2566 int journal_init(struct super_block
*p_s_sb
, const char *j_dev_name
,
2567 int old_format
, unsigned int commit_max_age
)
2569 int num_cnodes
= SB_ONDISK_JOURNAL_SIZE(p_s_sb
) * 2;
2570 struct buffer_head
*bhjh
;
2571 struct reiserfs_super_block
*rs
;
2572 struct reiserfs_journal_header
*jh
;
2573 struct reiserfs_journal
*journal
;
2574 struct reiserfs_journal_list
*jl
;
2575 char b
[BDEVNAME_SIZE
];
2577 journal
= SB_JOURNAL(p_s_sb
) = vmalloc(sizeof(struct reiserfs_journal
));
2579 reiserfs_warning(p_s_sb
,
2580 "journal-1256: unable to get memory for journal structure");
2583 memset(journal
, 0, sizeof(struct reiserfs_journal
));
2584 INIT_LIST_HEAD(&journal
->j_bitmap_nodes
);
2585 INIT_LIST_HEAD(&journal
->j_prealloc_list
);
2586 INIT_LIST_HEAD(&journal
->j_working_list
);
2587 INIT_LIST_HEAD(&journal
->j_journal_list
);
2588 journal
->j_persistent_trans
= 0;
2589 if (reiserfs_allocate_list_bitmaps(p_s_sb
,
2590 journal
->j_list_bitmap
,
2591 SB_BMAP_NR(p_s_sb
)))
2592 goto free_and_return
;
2593 allocate_bitmap_nodes(p_s_sb
);
2595 /* reserved for journal area support */
2596 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb
) = (old_format
?
2597 REISERFS_OLD_DISK_OFFSET_IN_BYTES
2598 / p_s_sb
->s_blocksize
+
2599 SB_BMAP_NR(p_s_sb
) +
2601 REISERFS_DISK_OFFSET_IN_BYTES
/
2602 p_s_sb
->s_blocksize
+ 2);
2604 /* Sanity check to see is the standard journal fitting withing first bitmap
2605 (actual for small blocksizes) */
2606 if (!SB_ONDISK_JOURNAL_DEVICE(p_s_sb
) &&
2607 (SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb
) +
2608 SB_ONDISK_JOURNAL_SIZE(p_s_sb
) > p_s_sb
->s_blocksize
* 8)) {
2609 reiserfs_warning(p_s_sb
,
2610 "journal-1393: journal does not fit for area "
2611 "addressed by first of bitmap blocks. It starts at "
2612 "%u and its size is %u. Block size %ld",
2613 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb
),
2614 SB_ONDISK_JOURNAL_SIZE(p_s_sb
),
2615 p_s_sb
->s_blocksize
);
2616 goto free_and_return
;
2619 if (journal_init_dev(p_s_sb
, journal
, j_dev_name
) != 0) {
2620 reiserfs_warning(p_s_sb
,
2621 "sh-462: unable to initialize jornal device");
2622 goto free_and_return
;
2625 rs
= SB_DISK_SUPER_BLOCK(p_s_sb
);
2627 /* read journal header */
2628 bhjh
= journal_bread(p_s_sb
,
2629 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
2630 SB_ONDISK_JOURNAL_SIZE(p_s_sb
));
2632 reiserfs_warning(p_s_sb
,
2633 "sh-459: unable to read journal header");
2634 goto free_and_return
;
2636 jh
= (struct reiserfs_journal_header
*)(bhjh
->b_data
);
2638 /* make sure that journal matches to the super block */
2639 if (is_reiserfs_jr(rs
)
2640 && (le32_to_cpu(jh
->jh_journal
.jp_journal_magic
) !=
2641 sb_jp_journal_magic(rs
))) {
2642 reiserfs_warning(p_s_sb
,
2643 "sh-460: journal header magic %x "
2644 "(device %s) does not match to magic found in super "
2645 "block %x", jh
->jh_journal
.jp_journal_magic
,
2646 bdevname(journal
->j_dev_bd
, b
),
2647 sb_jp_journal_magic(rs
));
2649 goto free_and_return
;
2652 journal
->j_trans_max
= le32_to_cpu(jh
->jh_journal
.jp_journal_trans_max
);
2653 journal
->j_max_batch
= le32_to_cpu(jh
->jh_journal
.jp_journal_max_batch
);
2654 journal
->j_max_commit_age
=
2655 le32_to_cpu(jh
->jh_journal
.jp_journal_max_commit_age
);
2656 journal
->j_max_trans_age
= JOURNAL_MAX_TRANS_AGE
;
2658 if (journal
->j_trans_max
) {
2659 /* make sure these parameters are available, assign it if they are not */
2660 __u32 initial
= journal
->j_trans_max
;
2663 if (p_s_sb
->s_blocksize
< 4096)
2664 ratio
= 4096 / p_s_sb
->s_blocksize
;
2666 if (SB_ONDISK_JOURNAL_SIZE(p_s_sb
) / journal
->j_trans_max
<
2668 journal
->j_trans_max
=
2669 SB_ONDISK_JOURNAL_SIZE(p_s_sb
) / JOURNAL_MIN_RATIO
;
2670 if (journal
->j_trans_max
> JOURNAL_TRANS_MAX_DEFAULT
/ ratio
)
2671 journal
->j_trans_max
=
2672 JOURNAL_TRANS_MAX_DEFAULT
/ ratio
;
2673 if (journal
->j_trans_max
< JOURNAL_TRANS_MIN_DEFAULT
/ ratio
)
2674 journal
->j_trans_max
=
2675 JOURNAL_TRANS_MIN_DEFAULT
/ ratio
;
2677 if (journal
->j_trans_max
!= initial
)
2678 reiserfs_warning(p_s_sb
,
2679 "sh-461: journal_init: wrong transaction max size (%u). Changed to %u",
2680 initial
, journal
->j_trans_max
);
2682 journal
->j_max_batch
= journal
->j_trans_max
*
2683 JOURNAL_MAX_BATCH_DEFAULT
/ JOURNAL_TRANS_MAX_DEFAULT
;
2686 if (!journal
->j_trans_max
) {
2687 /*we have the file system was created by old version of mkreiserfs
2688 so this field contains zero value */
2689 journal
->j_trans_max
= JOURNAL_TRANS_MAX_DEFAULT
;
2690 journal
->j_max_batch
= JOURNAL_MAX_BATCH_DEFAULT
;
2691 journal
->j_max_commit_age
= JOURNAL_MAX_COMMIT_AGE
;
2693 /* for blocksize >= 4096 - max transaction size is 1024. For block size < 4096
2694 trans max size is decreased proportionally */
2695 if (p_s_sb
->s_blocksize
< 4096) {
2696 journal
->j_trans_max
/= (4096 / p_s_sb
->s_blocksize
);
2697 journal
->j_max_batch
= (journal
->j_trans_max
) * 9 / 10;
2701 journal
->j_default_max_commit_age
= journal
->j_max_commit_age
;
2703 if (commit_max_age
!= 0) {
2704 journal
->j_max_commit_age
= commit_max_age
;
2705 journal
->j_max_trans_age
= commit_max_age
;
2708 reiserfs_info(p_s_sb
, "journal params: device %s, size %u, "
2709 "journal first block %u, max trans len %u, max batch %u, "
2710 "max commit age %u, max trans age %u\n",
2711 bdevname(journal
->j_dev_bd
, b
),
2712 SB_ONDISK_JOURNAL_SIZE(p_s_sb
),
2713 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
),
2714 journal
->j_trans_max
,
2715 journal
->j_max_batch
,
2716 journal
->j_max_commit_age
, journal
->j_max_trans_age
);
2720 journal
->j_list_bitmap_index
= 0;
2721 journal_list_init(p_s_sb
);
2723 memset(journal
->j_list_hash_table
, 0,
2724 JOURNAL_HASH_SIZE
* sizeof(struct reiserfs_journal_cnode
*));
2726 INIT_LIST_HEAD(&journal
->j_dirty_buffers
);
2727 spin_lock_init(&journal
->j_dirty_buffers_lock
);
2729 journal
->j_start
= 0;
2731 journal
->j_len_alloc
= 0;
2732 atomic_set(&(journal
->j_wcount
), 0);
2733 atomic_set(&(journal
->j_async_throttle
), 0);
2734 journal
->j_bcount
= 0;
2735 journal
->j_trans_start_time
= 0;
2736 journal
->j_last
= NULL
;
2737 journal
->j_first
= NULL
;
2738 init_waitqueue_head(&(journal
->j_join_wait
));
2739 sema_init(&journal
->j_lock
, 1);
2740 sema_init(&journal
->j_flush_sem
, 1);
2742 journal
->j_trans_id
= 10;
2743 journal
->j_mount_id
= 10;
2744 journal
->j_state
= 0;
2745 atomic_set(&(journal
->j_jlock
), 0);
2746 journal
->j_cnode_free_list
= allocate_cnodes(num_cnodes
);
2747 journal
->j_cnode_free_orig
= journal
->j_cnode_free_list
;
2748 journal
->j_cnode_free
= journal
->j_cnode_free_list
? num_cnodes
: 0;
2749 journal
->j_cnode_used
= 0;
2750 journal
->j_must_wait
= 0;
2752 if (journal
->j_cnode_free
== 0) {
2753 reiserfs_warning(p_s_sb
, "journal-2004: Journal cnode memory "
2754 "allocation failed (%ld bytes). Journal is "
2755 "too large for available memory. Usually "
2756 "this is due to a journal that is too large.",
2757 sizeof (struct reiserfs_journal_cnode
) * num_cnodes
);
2758 goto free_and_return
;
2761 init_journal_hash(p_s_sb
);
2762 jl
= journal
->j_current_jl
;
2763 jl
->j_list_bitmap
= get_list_bitmap(p_s_sb
, jl
);
2764 if (!jl
->j_list_bitmap
) {
2765 reiserfs_warning(p_s_sb
,
2766 "journal-2005, get_list_bitmap failed for journal list 0");
2767 goto free_and_return
;
2769 if (journal_read(p_s_sb
) < 0) {
2770 reiserfs_warning(p_s_sb
, "Replay Failure, unable to mount");
2771 goto free_and_return
;
2774 reiserfs_mounted_fs_count
++;
2775 if (reiserfs_mounted_fs_count
<= 1)
2776 commit_wq
= create_workqueue("reiserfs");
2778 INIT_WORK(&journal
->j_work
, flush_async_commits
, p_s_sb
);
2781 free_journal_ram(p_s_sb
);
2786 ** test for a polite end of the current transaction. Used by file_write, and should
2787 ** be used by delete to make sure they don't write more than can fit inside a single
2790 int journal_transaction_should_end(struct reiserfs_transaction_handle
*th
,
2793 struct reiserfs_journal
*journal
= SB_JOURNAL(th
->t_super
);
2794 time_t now
= get_seconds();
2795 /* cannot restart while nested */
2796 BUG_ON(!th
->t_trans_id
);
2797 if (th
->t_refcount
> 1)
2799 if (journal
->j_must_wait
> 0 ||
2800 (journal
->j_len_alloc
+ new_alloc
) >= journal
->j_max_batch
||
2801 atomic_read(&(journal
->j_jlock
)) ||
2802 (now
- journal
->j_trans_start_time
) > journal
->j_max_trans_age
||
2803 journal
->j_cnode_free
< (journal
->j_trans_max
* 3)) {
2809 /* this must be called inside a transaction, and requires the
2810 ** kernel_lock to be held
2812 void reiserfs_block_writes(struct reiserfs_transaction_handle
*th
)
2814 struct reiserfs_journal
*journal
= SB_JOURNAL(th
->t_super
);
2815 BUG_ON(!th
->t_trans_id
);
2816 journal
->j_must_wait
= 1;
2817 set_bit(J_WRITERS_BLOCKED
, &journal
->j_state
);
2821 /* this must be called without a transaction started, and does not
2824 void reiserfs_allow_writes(struct super_block
*s
)
2826 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2827 clear_bit(J_WRITERS_BLOCKED
, &journal
->j_state
);
2828 wake_up(&journal
->j_join_wait
);
2831 /* this must be called without a transaction started, and does not
2834 void reiserfs_wait_on_write_block(struct super_block
*s
)
2836 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2837 wait_event(journal
->j_join_wait
,
2838 !test_bit(J_WRITERS_BLOCKED
, &journal
->j_state
));
2841 static void queue_log_writer(struct super_block
*s
)
2844 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2845 set_bit(J_WRITERS_QUEUED
, &journal
->j_state
);
2848 * we don't want to use wait_event here because
2849 * we only want to wait once.
2851 init_waitqueue_entry(&wait
, current
);
2852 add_wait_queue(&journal
->j_join_wait
, &wait
);
2853 set_current_state(TASK_UNINTERRUPTIBLE
);
2854 if (test_bit(J_WRITERS_QUEUED
, &journal
->j_state
))
2856 current
->state
= TASK_RUNNING
;
2857 remove_wait_queue(&journal
->j_join_wait
, &wait
);
2860 static void wake_queued_writers(struct super_block
*s
)
2862 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2863 if (test_and_clear_bit(J_WRITERS_QUEUED
, &journal
->j_state
))
2864 wake_up(&journal
->j_join_wait
);
2867 static void let_transaction_grow(struct super_block
*sb
, unsigned long trans_id
)
2869 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
2870 unsigned long bcount
= journal
->j_bcount
;
2872 schedule_timeout_uninterruptible(1);
2873 journal
->j_current_jl
->j_state
|= LIST_COMMIT_PENDING
;
2874 while ((atomic_read(&journal
->j_wcount
) > 0 ||
2875 atomic_read(&journal
->j_jlock
)) &&
2876 journal
->j_trans_id
== trans_id
) {
2877 queue_log_writer(sb
);
2879 if (journal
->j_trans_id
!= trans_id
)
2881 if (bcount
== journal
->j_bcount
)
2883 bcount
= journal
->j_bcount
;
2887 /* join == true if you must join an existing transaction.
2888 ** join == false if you can deal with waiting for others to finish
2890 ** this will block until the transaction is joinable. send the number of blocks you
2891 ** expect to use in nblocks.
2893 static int do_journal_begin_r(struct reiserfs_transaction_handle
*th
,
2894 struct super_block
*p_s_sb
, unsigned long nblocks
,
2897 time_t now
= get_seconds();
2899 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
2900 struct reiserfs_transaction_handle myth
;
2901 int sched_count
= 0;
2904 reiserfs_check_lock_depth(p_s_sb
, "journal_begin");
2905 if (nblocks
> journal
->j_trans_max
)
2908 PROC_INFO_INC(p_s_sb
, journal
.journal_being
);
2909 /* set here for journal_join */
2911 th
->t_super
= p_s_sb
;
2914 lock_journal(p_s_sb
);
2915 if (join
!= JBEGIN_ABORT
&& reiserfs_is_journal_aborted(journal
)) {
2916 unlock_journal(p_s_sb
);
2917 retval
= journal
->j_errno
;
2920 journal
->j_bcount
++;
2922 if (test_bit(J_WRITERS_BLOCKED
, &journal
->j_state
)) {
2923 unlock_journal(p_s_sb
);
2924 reiserfs_wait_on_write_block(p_s_sb
);
2925 PROC_INFO_INC(p_s_sb
, journal
.journal_relock_writers
);
2928 now
= get_seconds();
2930 /* if there is no room in the journal OR
2931 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
2932 ** we don't sleep if there aren't other writers
2935 if ((!join
&& journal
->j_must_wait
> 0) ||
2937 && (journal
->j_len_alloc
+ nblocks
+ 2) >= journal
->j_max_batch
)
2938 || (!join
&& atomic_read(&journal
->j_wcount
) > 0
2939 && journal
->j_trans_start_time
> 0
2940 && (now
- journal
->j_trans_start_time
) >
2941 journal
->j_max_trans_age
) || (!join
2942 && atomic_read(&journal
->j_jlock
))
2943 || (!join
&& journal
->j_cnode_free
< (journal
->j_trans_max
* 3))) {
2945 old_trans_id
= journal
->j_trans_id
;
2946 unlock_journal(p_s_sb
); /* allow others to finish this transaction */
2948 if (!join
&& (journal
->j_len_alloc
+ nblocks
+ 2) >=
2949 journal
->j_max_batch
&&
2950 ((journal
->j_len
+ nblocks
+ 2) * 100) <
2951 (journal
->j_len_alloc
* 75)) {
2952 if (atomic_read(&journal
->j_wcount
) > 10) {
2954 queue_log_writer(p_s_sb
);
2958 /* don't mess with joining the transaction if all we have to do is
2959 * wait for someone else to do a commit
2961 if (atomic_read(&journal
->j_jlock
)) {
2962 while (journal
->j_trans_id
== old_trans_id
&&
2963 atomic_read(&journal
->j_jlock
)) {
2964 queue_log_writer(p_s_sb
);
2968 retval
= journal_join(&myth
, p_s_sb
, 1);
2972 /* someone might have ended the transaction while we joined */
2973 if (old_trans_id
!= journal
->j_trans_id
) {
2974 retval
= do_journal_end(&myth
, p_s_sb
, 1, 0);
2976 retval
= do_journal_end(&myth
, p_s_sb
, 1, COMMIT_NOW
);
2982 PROC_INFO_INC(p_s_sb
, journal
.journal_relock_wcount
);
2985 /* we are the first writer, set trans_id */
2986 if (journal
->j_trans_start_time
== 0) {
2987 journal
->j_trans_start_time
= get_seconds();
2989 atomic_inc(&(journal
->j_wcount
));
2990 journal
->j_len_alloc
+= nblocks
;
2991 th
->t_blocks_logged
= 0;
2992 th
->t_blocks_allocated
= nblocks
;
2993 th
->t_trans_id
= journal
->j_trans_id
;
2994 unlock_journal(p_s_sb
);
2995 INIT_LIST_HEAD(&th
->t_list
);
3000 memset(th
, 0, sizeof(*th
));
3001 /* Re-set th->t_super, so we can properly keep track of how many
3002 * persistent transactions there are. We need to do this so if this
3003 * call is part of a failed restart_transaction, we can free it later */
3004 th
->t_super
= p_s_sb
;
3008 struct reiserfs_transaction_handle
*reiserfs_persistent_transaction(struct
3014 struct reiserfs_transaction_handle
*th
;
3016 /* if we're nesting into an existing transaction. It will be
3017 ** persistent on its own
3019 if (reiserfs_transaction_running(s
)) {
3020 th
= current
->journal_info
;
3022 if (th
->t_refcount
< 2) {
3027 th
= kmalloc(sizeof(struct reiserfs_transaction_handle
), GFP_NOFS
);
3030 ret
= journal_begin(th
, s
, nblocks
);
3036 SB_JOURNAL(s
)->j_persistent_trans
++;
3040 int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle
*th
)
3042 struct super_block
*s
= th
->t_super
;
3045 ret
= journal_end(th
, th
->t_super
, th
->t_blocks_allocated
);
3048 if (th
->t_refcount
== 0) {
3049 SB_JOURNAL(s
)->j_persistent_trans
--;
3055 static int journal_join(struct reiserfs_transaction_handle
*th
,
3056 struct super_block
*p_s_sb
, unsigned long nblocks
)
3058 struct reiserfs_transaction_handle
*cur_th
= current
->journal_info
;
3060 /* this keeps do_journal_end from NULLing out the current->journal_info
3063 th
->t_handle_save
= cur_th
;
3064 if (cur_th
&& cur_th
->t_refcount
> 1) {
3067 return do_journal_begin_r(th
, p_s_sb
, nblocks
, JBEGIN_JOIN
);
3070 int journal_join_abort(struct reiserfs_transaction_handle
*th
,
3071 struct super_block
*p_s_sb
, unsigned long nblocks
)
3073 struct reiserfs_transaction_handle
*cur_th
= current
->journal_info
;
3075 /* this keeps do_journal_end from NULLing out the current->journal_info
3078 th
->t_handle_save
= cur_th
;
3079 if (cur_th
&& cur_th
->t_refcount
> 1) {
3082 return do_journal_begin_r(th
, p_s_sb
, nblocks
, JBEGIN_ABORT
);
3085 int journal_begin(struct reiserfs_transaction_handle
*th
,
3086 struct super_block
*p_s_sb
, unsigned long nblocks
)
3088 struct reiserfs_transaction_handle
*cur_th
= current
->journal_info
;
3091 th
->t_handle_save
= NULL
;
3093 /* we are nesting into the current transaction */
3094 if (cur_th
->t_super
== p_s_sb
) {
3095 BUG_ON(!cur_th
->t_refcount
);
3096 cur_th
->t_refcount
++;
3097 memcpy(th
, cur_th
, sizeof(*th
));
3098 if (th
->t_refcount
<= 1)
3099 reiserfs_warning(p_s_sb
,
3100 "BAD: refcount <= 1, but journal_info != 0");
3103 /* we've ended up with a handle from a different filesystem.
3104 ** save it and restore on journal_end. This should never
3107 reiserfs_warning(p_s_sb
,
3108 "clm-2100: nesting info a different FS");
3109 th
->t_handle_save
= current
->journal_info
;
3110 current
->journal_info
= th
;
3113 current
->journal_info
= th
;
3115 ret
= do_journal_begin_r(th
, p_s_sb
, nblocks
, JBEGIN_REG
);
3116 if (current
->journal_info
!= th
)
3119 /* I guess this boils down to being the reciprocal of clm-2100 above.
3120 * If do_journal_begin_r fails, we need to put it back, since journal_end
3121 * won't be called to do it. */
3123 current
->journal_info
= th
->t_handle_save
;
3125 BUG_ON(!th
->t_refcount
);
3131 ** puts bh into the current transaction. If it was already there, reorders removes the
3132 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
3134 ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
3135 ** transaction is committed.
3137 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
3139 int journal_mark_dirty(struct reiserfs_transaction_handle
*th
,
3140 struct super_block
*p_s_sb
, struct buffer_head
*bh
)
3142 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3143 struct reiserfs_journal_cnode
*cn
= NULL
;
3144 int count_already_incd
= 0;
3146 BUG_ON(!th
->t_trans_id
);
3148 PROC_INFO_INC(p_s_sb
, journal
.mark_dirty
);
3149 if (th
->t_trans_id
!= journal
->j_trans_id
) {
3150 reiserfs_panic(th
->t_super
,
3151 "journal-1577: handle trans id %ld != current trans id %ld\n",
3152 th
->t_trans_id
, journal
->j_trans_id
);
3157 prepared
= test_clear_buffer_journal_prepared(bh
);
3158 clear_buffer_journal_restore_dirty(bh
);
3159 /* already in this transaction, we are done */
3160 if (buffer_journaled(bh
)) {
3161 PROC_INFO_INC(p_s_sb
, journal
.mark_dirty_already
);
3165 /* this must be turned into a panic instead of a warning. We can't allow
3166 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
3167 ** could get to disk too early. NOT GOOD.
3169 if (!prepared
|| buffer_dirty(bh
)) {
3170 reiserfs_warning(p_s_sb
, "journal-1777: buffer %llu bad state "
3171 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
3172 (unsigned long long)bh
->b_blocknr
,
3173 prepared
? ' ' : '!',
3174 buffer_locked(bh
) ? ' ' : '!',
3175 buffer_dirty(bh
) ? ' ' : '!',
3176 buffer_journal_dirty(bh
) ? ' ' : '!');
3179 if (atomic_read(&(journal
->j_wcount
)) <= 0) {
3180 reiserfs_warning(p_s_sb
,
3181 "journal-1409: journal_mark_dirty returning because j_wcount was %d",
3182 atomic_read(&(journal
->j_wcount
)));
3185 /* this error means I've screwed up, and we've overflowed the transaction.
3186 ** Nothing can be done here, except make the FS readonly or panic.
3188 if (journal
->j_len
>= journal
->j_trans_max
) {
3189 reiserfs_panic(th
->t_super
,
3190 "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n",
3194 if (buffer_journal_dirty(bh
)) {
3195 count_already_incd
= 1;
3196 PROC_INFO_INC(p_s_sb
, journal
.mark_dirty_notjournal
);
3197 clear_buffer_journal_dirty(bh
);
3200 if (journal
->j_len
> journal
->j_len_alloc
) {
3201 journal
->j_len_alloc
= journal
->j_len
+ JOURNAL_PER_BALANCE_CNT
;
3204 set_buffer_journaled(bh
);
3206 /* now put this guy on the end */
3208 cn
= get_cnode(p_s_sb
);
3210 reiserfs_panic(p_s_sb
, "get_cnode failed!\n");
3213 if (th
->t_blocks_logged
== th
->t_blocks_allocated
) {
3214 th
->t_blocks_allocated
+= JOURNAL_PER_BALANCE_CNT
;
3215 journal
->j_len_alloc
+= JOURNAL_PER_BALANCE_CNT
;
3217 th
->t_blocks_logged
++;
3221 cn
->blocknr
= bh
->b_blocknr
;
3224 insert_journal_hash(journal
->j_hash_table
, cn
);
3225 if (!count_already_incd
) {
3230 cn
->prev
= journal
->j_last
;
3232 if (journal
->j_last
) {
3233 journal
->j_last
->next
= cn
;
3234 journal
->j_last
= cn
;
3236 journal
->j_first
= cn
;
3237 journal
->j_last
= cn
;
3242 int journal_end(struct reiserfs_transaction_handle
*th
,
3243 struct super_block
*p_s_sb
, unsigned long nblocks
)
3245 if (!current
->journal_info
&& th
->t_refcount
> 1)
3246 reiserfs_warning(p_s_sb
, "REISER-NESTING: th NULL, refcount %d",
3249 if (!th
->t_trans_id
) {
3255 if (th
->t_refcount
> 0) {
3256 struct reiserfs_transaction_handle
*cur_th
=
3257 current
->journal_info
;
3259 /* we aren't allowed to close a nested transaction on a different
3260 ** filesystem from the one in the task struct
3262 if (cur_th
->t_super
!= th
->t_super
)
3266 memcpy(current
->journal_info
, th
, sizeof(*th
));
3271 return do_journal_end(th
, p_s_sb
, nblocks
, 0);
3275 /* removes from the current transaction, relsing and descrementing any counters.
3276 ** also files the removed buffer directly onto the clean list
3278 ** called by journal_mark_freed when a block has been deleted
3280 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
3282 static int remove_from_transaction(struct super_block
*p_s_sb
,
3283 b_blocknr_t blocknr
, int already_cleaned
)
3285 struct buffer_head
*bh
;
3286 struct reiserfs_journal_cnode
*cn
;
3287 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3290 cn
= get_journal_hash_dev(p_s_sb
, journal
->j_hash_table
, blocknr
);
3291 if (!cn
|| !cn
->bh
) {
3296 cn
->prev
->next
= cn
->next
;
3299 cn
->next
->prev
= cn
->prev
;
3301 if (cn
== journal
->j_first
) {
3302 journal
->j_first
= cn
->next
;
3304 if (cn
== journal
->j_last
) {
3305 journal
->j_last
= cn
->prev
;
3308 remove_journal_hash(p_s_sb
, journal
->j_hash_table
, NULL
,
3310 clear_buffer_journaled(bh
); /* don't log this one */
3312 if (!already_cleaned
) {
3313 clear_buffer_journal_dirty(bh
);
3314 clear_buffer_dirty(bh
);
3315 clear_buffer_journal_test(bh
);
3317 if (atomic_read(&(bh
->b_count
)) < 0) {
3318 reiserfs_warning(p_s_sb
,
3319 "journal-1752: remove from trans, b_count < 0");
3324 journal
->j_len_alloc
--;
3325 free_cnode(p_s_sb
, cn
);
3330 ** for any cnode in a journal list, it can only be dirtied of all the
3331 ** transactions that include it are commited to disk.
3332 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
3333 ** and 0 if you aren't
3335 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3336 ** blocks for a given transaction on disk
3339 static int can_dirty(struct reiserfs_journal_cnode
*cn
)
3341 struct super_block
*sb
= cn
->sb
;
3342 b_blocknr_t blocknr
= cn
->blocknr
;
3343 struct reiserfs_journal_cnode
*cur
= cn
->hprev
;
3346 /* first test hprev. These are all newer than cn, so any node here
3347 ** with the same block number and dev means this node can't be sent
3348 ** to disk right now.
3350 while (cur
&& can_dirty
) {
3351 if (cur
->jlist
&& cur
->bh
&& cur
->blocknr
&& cur
->sb
== sb
&&
3352 cur
->blocknr
== blocknr
) {
3357 /* then test hnext. These are all older than cn. As long as they
3358 ** are committed to the log, it is safe to write cn to disk
3361 while (cur
&& can_dirty
) {
3362 if (cur
->jlist
&& cur
->jlist
->j_len
> 0 &&
3363 atomic_read(&(cur
->jlist
->j_commit_left
)) > 0 && cur
->bh
&&
3364 cur
->blocknr
&& cur
->sb
== sb
&& cur
->blocknr
== blocknr
) {
3372 /* syncs the commit blocks, but does not force the real buffers to disk
3373 ** will wait until the current transaction is done/commited before returning
3375 int journal_end_sync(struct reiserfs_transaction_handle
*th
,
3376 struct super_block
*p_s_sb
, unsigned long nblocks
)
3378 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3380 BUG_ON(!th
->t_trans_id
);
3381 /* you can sync while nested, very, very bad */
3382 if (th
->t_refcount
> 1) {
3385 if (journal
->j_len
== 0) {
3386 reiserfs_prepare_for_journal(p_s_sb
, SB_BUFFER_WITH_SB(p_s_sb
),
3388 journal_mark_dirty(th
, p_s_sb
, SB_BUFFER_WITH_SB(p_s_sb
));
3390 return do_journal_end(th
, p_s_sb
, nblocks
, COMMIT_NOW
| WAIT
);
3394 ** writeback the pending async commits to disk
3396 static void flush_async_commits(void *p
)
3398 struct super_block
*p_s_sb
= p
;
3399 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3400 struct reiserfs_journal_list
*jl
;
3401 struct list_head
*entry
;
3404 if (!list_empty(&journal
->j_journal_list
)) {
3405 /* last entry is the youngest, commit it and you get everything */
3406 entry
= journal
->j_journal_list
.prev
;
3407 jl
= JOURNAL_LIST_ENTRY(entry
);
3408 flush_commit_list(p_s_sb
, jl
, 1);
3412 * this is a little racey, but there's no harm in missing
3413 * the filemap_fdata_write
3415 if (!atomic_read(&journal
->j_async_throttle
)
3416 && !reiserfs_is_journal_aborted(journal
)) {
3417 atomic_inc(&journal
->j_async_throttle
);
3418 filemap_fdatawrite(p_s_sb
->s_bdev
->bd_inode
->i_mapping
);
3419 atomic_dec(&journal
->j_async_throttle
);
3424 ** flushes any old transactions to disk
3425 ** ends the current transaction if it is too old
3427 int reiserfs_flush_old_commits(struct super_block
*p_s_sb
)
3430 struct reiserfs_transaction_handle th
;
3431 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3433 now
= get_seconds();
3434 /* safety check so we don't flush while we are replaying the log during
3437 if (list_empty(&journal
->j_journal_list
)) {
3441 /* check the current transaction. If there are no writers, and it is
3442 * too old, finish it, and force the commit blocks to disk
3444 if (atomic_read(&journal
->j_wcount
) <= 0 &&
3445 journal
->j_trans_start_time
> 0 &&
3446 journal
->j_len
> 0 &&
3447 (now
- journal
->j_trans_start_time
) > journal
->j_max_trans_age
) {
3448 if (!journal_join(&th
, p_s_sb
, 1)) {
3449 reiserfs_prepare_for_journal(p_s_sb
,
3450 SB_BUFFER_WITH_SB(p_s_sb
),
3452 journal_mark_dirty(&th
, p_s_sb
,
3453 SB_BUFFER_WITH_SB(p_s_sb
));
3455 /* we're only being called from kreiserfsd, it makes no sense to do
3456 ** an async commit so that kreiserfsd can do it later
3458 do_journal_end(&th
, p_s_sb
, 1, COMMIT_NOW
| WAIT
);
3461 return p_s_sb
->s_dirt
;
3465 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
3467 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
3468 ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3469 ** flushes the commit list and returns 0.
3471 ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
3473 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3475 static int check_journal_end(struct reiserfs_transaction_handle
*th
,
3476 struct super_block
*p_s_sb
, unsigned long nblocks
,
3481 int flush
= flags
& FLUSH_ALL
;
3482 int commit_now
= flags
& COMMIT_NOW
;
3483 int wait_on_commit
= flags
& WAIT
;
3484 struct reiserfs_journal_list
*jl
;
3485 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3487 BUG_ON(!th
->t_trans_id
);
3489 if (th
->t_trans_id
!= journal
->j_trans_id
) {
3490 reiserfs_panic(th
->t_super
,
3491 "journal-1577: handle trans id %ld != current trans id %ld\n",
3492 th
->t_trans_id
, journal
->j_trans_id
);
3495 journal
->j_len_alloc
-= (th
->t_blocks_allocated
- th
->t_blocks_logged
);
3496 if (atomic_read(&(journal
->j_wcount
)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3497 atomic_dec(&(journal
->j_wcount
));
3500 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
3501 ** will be dealt with by next transaction that actually writes something, but should be taken
3502 ** care of in this trans
3504 if (journal
->j_len
== 0) {
3507 /* if wcount > 0, and we are called to with flush or commit_now,
3508 ** we wait on j_join_wait. We will wake up when the last writer has
3509 ** finished the transaction, and started it on its way to the disk.
3510 ** Then, we flush the commit or journal list, and just return 0
3511 ** because the rest of journal end was already done for this transaction.
3513 if (atomic_read(&(journal
->j_wcount
)) > 0) {
3514 if (flush
|| commit_now
) {
3517 jl
= journal
->j_current_jl
;
3518 trans_id
= jl
->j_trans_id
;
3520 jl
->j_state
|= LIST_COMMIT_PENDING
;
3521 atomic_set(&(journal
->j_jlock
), 1);
3523 journal
->j_next_full_flush
= 1;
3525 unlock_journal(p_s_sb
);
3527 /* sleep while the current transaction is still j_jlocked */
3528 while (journal
->j_trans_id
== trans_id
) {
3529 if (atomic_read(&journal
->j_jlock
)) {
3530 queue_log_writer(p_s_sb
);
3532 lock_journal(p_s_sb
);
3533 if (journal
->j_trans_id
== trans_id
) {
3534 atomic_set(&(journal
->j_jlock
),
3537 unlock_journal(p_s_sb
);
3540 if (journal
->j_trans_id
== trans_id
) {
3544 && journal_list_still_alive(p_s_sb
, trans_id
)
3545 && wait_on_commit
) {
3546 flush_commit_list(p_s_sb
, jl
, 1);
3550 unlock_journal(p_s_sb
);
3554 /* deal with old transactions where we are the last writers */
3555 now
= get_seconds();
3556 if ((now
- journal
->j_trans_start_time
) > journal
->j_max_trans_age
) {
3558 journal
->j_next_async_flush
= 1;
3560 /* don't batch when someone is waiting on j_join_wait */
3561 /* don't batch when syncing the commit or flushing the whole trans */
3562 if (!(journal
->j_must_wait
> 0) && !(atomic_read(&(journal
->j_jlock
)))
3563 && !flush
&& !commit_now
&& (journal
->j_len
< journal
->j_max_batch
)
3564 && journal
->j_len_alloc
< journal
->j_max_batch
3565 && journal
->j_cnode_free
> (journal
->j_trans_max
* 3)) {
3566 journal
->j_bcount
++;
3567 unlock_journal(p_s_sb
);
3571 if (journal
->j_start
> SB_ONDISK_JOURNAL_SIZE(p_s_sb
)) {
3572 reiserfs_panic(p_s_sb
,
3573 "journal-003: journal_end: j_start (%ld) is too high\n",
3580 ** Does all the work that makes deleting blocks safe.
3581 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3584 ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3585 ** before this transaction has finished.
3587 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3588 ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3589 ** the block can't be reallocated yet.
3591 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3593 int journal_mark_freed(struct reiserfs_transaction_handle
*th
,
3594 struct super_block
*p_s_sb
, b_blocknr_t blocknr
)
3596 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3597 struct reiserfs_journal_cnode
*cn
= NULL
;
3598 struct buffer_head
*bh
= NULL
;
3599 struct reiserfs_list_bitmap
*jb
= NULL
;
3601 BUG_ON(!th
->t_trans_id
);
3603 cn
= get_journal_hash_dev(p_s_sb
, journal
->j_hash_table
, blocknr
);
3608 /* if it is journal new, we just remove it from this transaction */
3609 if (bh
&& buffer_journal_new(bh
)) {
3610 clear_buffer_journal_new(bh
);
3611 clear_prepared_bits(bh
);
3612 reiserfs_clean_and_file_buffer(bh
);
3613 cleaned
= remove_from_transaction(p_s_sb
, blocknr
, cleaned
);
3615 /* set the bit for this block in the journal bitmap for this transaction */
3616 jb
= journal
->j_current_jl
->j_list_bitmap
;
3618 reiserfs_panic(p_s_sb
,
3619 "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n");
3621 set_bit_in_list_bitmap(p_s_sb
, blocknr
, jb
);
3623 /* Note, the entire while loop is not allowed to schedule. */
3626 clear_prepared_bits(bh
);
3627 reiserfs_clean_and_file_buffer(bh
);
3629 cleaned
= remove_from_transaction(p_s_sb
, blocknr
, cleaned
);
3631 /* find all older transactions with this block, make sure they don't try to write it out */
3632 cn
= get_journal_hash_dev(p_s_sb
, journal
->j_list_hash_table
,
3635 if (p_s_sb
== cn
->sb
&& blocknr
== cn
->blocknr
) {
3636 set_bit(BLOCK_FREED
, &cn
->state
);
3639 /* remove_from_transaction will brelse the buffer if it was
3640 ** in the current trans
3642 clear_buffer_journal_dirty(cn
->
3644 clear_buffer_dirty(cn
->bh
);
3645 clear_buffer_journal_test(cn
->
3650 (&(cn
->bh
->b_count
)) < 0) {
3651 reiserfs_warning(p_s_sb
,
3652 "journal-2138: cn->bh->b_count < 0");
3655 if (cn
->jlist
) { /* since we are clearing the bh, we MUST dec nonzerolen */
3668 put_bh(bh
); /* get_hash grabs the buffer */
3669 if (atomic_read(&(bh
->b_count
)) < 0) {
3670 reiserfs_warning(p_s_sb
,
3671 "journal-2165: bh->b_count < 0");
3677 void reiserfs_update_inode_transaction(struct inode
*inode
)
3679 struct reiserfs_journal
*journal
= SB_JOURNAL(inode
->i_sb
);
3680 REISERFS_I(inode
)->i_jl
= journal
->j_current_jl
;
3681 REISERFS_I(inode
)->i_trans_id
= journal
->j_trans_id
;
3685 * returns -1 on error, 0 if no commits/barriers were done and 1
3686 * if a transaction was actually committed and the barrier was done
3688 static int __commit_trans_jl(struct inode
*inode
, unsigned long id
,
3689 struct reiserfs_journal_list
*jl
)
3691 struct reiserfs_transaction_handle th
;
3692 struct super_block
*sb
= inode
->i_sb
;
3693 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3696 /* is it from the current transaction, or from an unknown transaction? */
3697 if (id
== journal
->j_trans_id
) {
3698 jl
= journal
->j_current_jl
;
3699 /* try to let other writers come in and grow this transaction */
3700 let_transaction_grow(sb
, id
);
3701 if (journal
->j_trans_id
!= id
) {
3702 goto flush_commit_only
;
3705 ret
= journal_begin(&th
, sb
, 1);
3709 /* someone might have ended this transaction while we joined */
3710 if (journal
->j_trans_id
!= id
) {
3711 reiserfs_prepare_for_journal(sb
, SB_BUFFER_WITH_SB(sb
),
3713 journal_mark_dirty(&th
, sb
, SB_BUFFER_WITH_SB(sb
));
3714 ret
= journal_end(&th
, sb
, 1);
3715 goto flush_commit_only
;
3718 ret
= journal_end_sync(&th
, sb
, 1);
3723 /* this gets tricky, we have to make sure the journal list in
3724 * the inode still exists. We know the list is still around
3725 * if we've got a larger transaction id than the oldest list
3728 if (journal_list_still_alive(inode
->i_sb
, id
)) {
3730 * we only set ret to 1 when we know for sure
3731 * the barrier hasn't been started yet on the commit
3734 if (atomic_read(&jl
->j_commit_left
) > 1)
3736 flush_commit_list(sb
, jl
, 1);
3737 if (journal
->j_errno
)
3738 ret
= journal
->j_errno
;
3741 /* otherwise the list is gone, and long since committed */
3745 int reiserfs_commit_for_inode(struct inode
*inode
)
3747 unsigned long id
= REISERFS_I(inode
)->i_trans_id
;
3748 struct reiserfs_journal_list
*jl
= REISERFS_I(inode
)->i_jl
;
3750 /* for the whole inode, assume unset id means it was
3751 * changed in the current transaction. More conservative
3754 reiserfs_update_inode_transaction(inode
);
3755 id
= REISERFS_I(inode
)->i_trans_id
;
3756 /* jl will be updated in __commit_trans_jl */
3759 return __commit_trans_jl(inode
, id
, jl
);
3762 void reiserfs_restore_prepared_buffer(struct super_block
*p_s_sb
,
3763 struct buffer_head
*bh
)
3765 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3766 PROC_INFO_INC(p_s_sb
, journal
.restore_prepared
);
3770 if (test_clear_buffer_journal_restore_dirty(bh
) &&
3771 buffer_journal_dirty(bh
)) {
3772 struct reiserfs_journal_cnode
*cn
;
3773 cn
= get_journal_hash_dev(p_s_sb
,
3774 journal
->j_list_hash_table
,
3776 if (cn
&& can_dirty(cn
)) {
3777 set_buffer_journal_test(bh
);
3778 mark_buffer_dirty(bh
);
3781 clear_buffer_journal_prepared(bh
);
3784 extern struct tree_balance
*cur_tb
;
3786 ** before we can change a metadata block, we have to make sure it won't
3787 ** be written to disk while we are altering it. So, we must:
3792 int reiserfs_prepare_for_journal(struct super_block
*p_s_sb
,
3793 struct buffer_head
*bh
, int wait
)
3795 PROC_INFO_INC(p_s_sb
, journal
.prepare
);
3797 if (test_set_buffer_locked(bh
)) {
3802 set_buffer_journal_prepared(bh
);
3803 if (test_clear_buffer_dirty(bh
) && buffer_journal_dirty(bh
)) {
3804 clear_buffer_journal_test(bh
);
3805 set_buffer_journal_restore_dirty(bh
);
3811 static void flush_old_journal_lists(struct super_block
*s
)
3813 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
3814 struct reiserfs_journal_list
*jl
;
3815 struct list_head
*entry
;
3816 time_t now
= get_seconds();
3818 while (!list_empty(&journal
->j_journal_list
)) {
3819 entry
= journal
->j_journal_list
.next
;
3820 jl
= JOURNAL_LIST_ENTRY(entry
);
3821 /* this check should always be run, to send old lists to disk */
3822 if (jl
->j_timestamp
< (now
- (JOURNAL_MAX_TRANS_AGE
* 4))) {
3823 flush_used_journal_lists(s
, jl
);
3831 ** long and ugly. If flush, will not return until all commit
3832 ** blocks and all real buffers in the trans are on disk.
3833 ** If no_async, won't return until all commit blocks are on disk.
3835 ** keep reading, there are comments as you go along
3837 ** If the journal is aborted, we just clean up. Things like flushing
3838 ** journal lists, etc just won't happen.
3840 static int do_journal_end(struct reiserfs_transaction_handle
*th
,
3841 struct super_block
*p_s_sb
, unsigned long nblocks
,
3844 struct reiserfs_journal
*journal
= SB_JOURNAL(p_s_sb
);
3845 struct reiserfs_journal_cnode
*cn
, *next
, *jl_cn
;
3846 struct reiserfs_journal_cnode
*last_cn
= NULL
;
3847 struct reiserfs_journal_desc
*desc
;
3848 struct reiserfs_journal_commit
*commit
;
3849 struct buffer_head
*c_bh
; /* commit bh */
3850 struct buffer_head
*d_bh
; /* desc bh */
3851 int cur_write_start
= 0; /* start index of current log write */
3854 int flush
= flags
& FLUSH_ALL
;
3855 int wait_on_commit
= flags
& WAIT
;
3856 struct reiserfs_journal_list
*jl
, *temp_jl
;
3857 struct list_head
*entry
, *safe
;
3858 unsigned long jindex
;
3859 unsigned long commit_trans_id
;
3862 BUG_ON(th
->t_refcount
> 1);
3863 BUG_ON(!th
->t_trans_id
);
3866 current
->journal_info
= th
->t_handle_save
;
3867 reiserfs_check_lock_depth(p_s_sb
, "journal end");
3868 if (journal
->j_len
== 0) {
3869 reiserfs_prepare_for_journal(p_s_sb
, SB_BUFFER_WITH_SB(p_s_sb
),
3871 journal_mark_dirty(th
, p_s_sb
, SB_BUFFER_WITH_SB(p_s_sb
));
3874 lock_journal(p_s_sb
);
3875 if (journal
->j_next_full_flush
) {
3879 if (journal
->j_next_async_flush
) {
3880 flags
|= COMMIT_NOW
| WAIT
;
3884 /* check_journal_end locks the journal, and unlocks if it does not return 1
3885 ** it tells us if we should continue with the journal_end, or just return
3887 if (!check_journal_end(th
, p_s_sb
, nblocks
, flags
)) {
3889 wake_queued_writers(p_s_sb
);
3890 reiserfs_async_progress_wait(p_s_sb
);
3894 /* check_journal_end might set these, check again */
3895 if (journal
->j_next_full_flush
) {
3900 ** j must wait means we have to flush the log blocks, and the real blocks for
3903 if (journal
->j_must_wait
> 0) {
3906 #ifdef REISERFS_PREALLOCATE
3907 /* quota ops might need to nest, setup the journal_info pointer for them
3908 * and raise the refcount so that it is > 0. */
3909 current
->journal_info
= th
;
3911 reiserfs_discard_all_prealloc(th
); /* it should not involve new blocks into
3912 * the transaction */
3914 current
->journal_info
= th
->t_handle_save
;
3917 /* setup description block */
3919 journal_getblk(p_s_sb
,
3920 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
3922 set_buffer_uptodate(d_bh
);
3923 desc
= (struct reiserfs_journal_desc
*)(d_bh
)->b_data
;
3924 memset(d_bh
->b_data
, 0, d_bh
->b_size
);
3925 memcpy(get_journal_desc_magic(d_bh
), JOURNAL_DESC_MAGIC
, 8);
3926 set_desc_trans_id(desc
, journal
->j_trans_id
);
3928 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
3929 c_bh
= journal_getblk(p_s_sb
, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
3930 ((journal
->j_start
+ journal
->j_len
+
3931 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb
)));
3932 commit
= (struct reiserfs_journal_commit
*)c_bh
->b_data
;
3933 memset(c_bh
->b_data
, 0, c_bh
->b_size
);
3934 set_commit_trans_id(commit
, journal
->j_trans_id
);
3935 set_buffer_uptodate(c_bh
);
3937 /* init this journal list */
3938 jl
= journal
->j_current_jl
;
3940 /* we lock the commit before doing anything because
3941 * we want to make sure nobody tries to run flush_commit_list until
3942 * the new transaction is fully setup, and we've already flushed the
3945 down(&jl
->j_commit_lock
);
3947 /* save the transaction id in case we need to commit it later */
3948 commit_trans_id
= jl
->j_trans_id
;
3950 atomic_set(&jl
->j_older_commits_done
, 0);
3951 jl
->j_trans_id
= journal
->j_trans_id
;
3952 jl
->j_timestamp
= journal
->j_trans_start_time
;
3953 jl
->j_commit_bh
= c_bh
;
3954 jl
->j_start
= journal
->j_start
;
3955 jl
->j_len
= journal
->j_len
;
3956 atomic_set(&jl
->j_nonzerolen
, journal
->j_len
);
3957 atomic_set(&jl
->j_commit_left
, journal
->j_len
+ 2);
3958 jl
->j_realblock
= NULL
;
3960 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
3961 ** for each real block, add it to the journal list hash,
3962 ** copy into real block index array in the commit or desc block
3964 trans_half
= journal_trans_half(p_s_sb
->s_blocksize
);
3965 for (i
= 0, cn
= journal
->j_first
; cn
; cn
= cn
->next
, i
++) {
3966 if (buffer_journaled(cn
->bh
)) {
3967 jl_cn
= get_cnode(p_s_sb
);
3969 reiserfs_panic(p_s_sb
,
3970 "journal-1676, get_cnode returned NULL\n");
3973 jl
->j_realblock
= jl_cn
;
3975 jl_cn
->prev
= last_cn
;
3978 last_cn
->next
= jl_cn
;
3981 /* make sure the block we are trying to log is not a block
3982 of journal or reserved area */
3984 if (is_block_in_log_or_reserved_area
3985 (p_s_sb
, cn
->bh
->b_blocknr
)) {
3986 reiserfs_panic(p_s_sb
,
3987 "journal-2332: Trying to log block %lu, which is a log block\n",
3990 jl_cn
->blocknr
= cn
->bh
->b_blocknr
;
3995 insert_journal_hash(journal
->j_list_hash_table
, jl_cn
);
3996 if (i
< trans_half
) {
3997 desc
->j_realblock
[i
] =
3998 cpu_to_le32(cn
->bh
->b_blocknr
);
4000 commit
->j_realblock
[i
- trans_half
] =
4001 cpu_to_le32(cn
->bh
->b_blocknr
);
4007 set_desc_trans_len(desc
, journal
->j_len
);
4008 set_desc_mount_id(desc
, journal
->j_mount_id
);
4009 set_desc_trans_id(desc
, journal
->j_trans_id
);
4010 set_commit_trans_len(commit
, journal
->j_len
);
4012 /* special check in case all buffers in the journal were marked for not logging */
4013 if (journal
->j_len
== 0) {
4017 /* we're about to dirty all the log blocks, mark the description block
4018 * dirty now too. Don't mark the commit block dirty until all the
4019 * others are on disk
4021 mark_buffer_dirty(d_bh
);
4023 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
4024 cur_write_start
= journal
->j_start
;
4025 cn
= journal
->j_first
;
4026 jindex
= 1; /* start at one so we don't get the desc again */
4028 clear_buffer_journal_new(cn
->bh
);
4029 /* copy all the real blocks into log area. dirty log blocks */
4030 if (buffer_journaled(cn
->bh
)) {
4031 struct buffer_head
*tmp_bh
;
4035 journal_getblk(p_s_sb
,
4036 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb
) +
4039 SB_ONDISK_JOURNAL_SIZE(p_s_sb
)));
4040 set_buffer_uptodate(tmp_bh
);
4041 page
= cn
->bh
->b_page
;
4043 memcpy(tmp_bh
->b_data
,
4044 addr
+ offset_in_page(cn
->bh
->b_data
),
4047 mark_buffer_dirty(tmp_bh
);
4049 set_buffer_journal_dirty(cn
->bh
);
4050 clear_buffer_journaled(cn
->bh
);
4052 /* JDirty cleared sometime during transaction. don't log this one */
4053 reiserfs_warning(p_s_sb
,
4054 "journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!");
4058 free_cnode(p_s_sb
, cn
);
4063 /* we are done with both the c_bh and d_bh, but
4064 ** c_bh must be written after all other commit blocks,
4065 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
4068 journal
->j_current_jl
= alloc_journal_list(p_s_sb
);
4070 /* now it is safe to insert this transaction on the main list */
4071 list_add_tail(&jl
->j_list
, &journal
->j_journal_list
);
4072 list_add_tail(&jl
->j_working_list
, &journal
->j_working_list
);
4073 journal
->j_num_work_lists
++;
4075 /* reset journal values for the next transaction */
4076 old_start
= journal
->j_start
;
4078 (journal
->j_start
+ journal
->j_len
+
4079 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb
);
4080 atomic_set(&(journal
->j_wcount
), 0);
4081 journal
->j_bcount
= 0;
4082 journal
->j_last
= NULL
;
4083 journal
->j_first
= NULL
;
4085 journal
->j_trans_start_time
= 0;
4086 journal
->j_trans_id
++;
4087 journal
->j_current_jl
->j_trans_id
= journal
->j_trans_id
;
4088 journal
->j_must_wait
= 0;
4089 journal
->j_len_alloc
= 0;
4090 journal
->j_next_full_flush
= 0;
4091 journal
->j_next_async_flush
= 0;
4092 init_journal_hash(p_s_sb
);
4094 // make sure reiserfs_add_jh sees the new current_jl before we
4095 // write out the tails
4098 /* tail conversion targets have to hit the disk before we end the
4099 * transaction. Otherwise a later transaction might repack the tail
4100 * before this transaction commits, leaving the data block unflushed and
4101 * clean, if we crash before the later transaction commits, the data block
4104 if (!list_empty(&jl
->j_tail_bh_list
)) {
4106 write_ordered_buffers(&journal
->j_dirty_buffers_lock
,
4107 journal
, jl
, &jl
->j_tail_bh_list
);
4110 if (!list_empty(&jl
->j_tail_bh_list
))
4112 up(&jl
->j_commit_lock
);
4114 /* honor the flush wishes from the caller, simple commits can
4115 ** be done outside the journal lock, they are done below
4117 ** if we don't flush the commit list right now, we put it into
4118 ** the work queue so the people waiting on the async progress work
4119 ** queue don't wait for this proc to flush journal lists and such.
4122 flush_commit_list(p_s_sb
, jl
, 1);
4123 flush_journal_list(p_s_sb
, jl
, 1);
4124 } else if (!(jl
->j_state
& LIST_COMMIT_PENDING
))
4125 queue_delayed_work(commit_wq
, &journal
->j_work
, HZ
/ 10);
4127 /* if the next transaction has any chance of wrapping, flush
4128 ** transactions that might get overwritten. If any journal lists are very
4129 ** old flush them as well.
4132 list_for_each_safe(entry
, safe
, &journal
->j_journal_list
) {
4133 temp_jl
= JOURNAL_LIST_ENTRY(entry
);
4134 if (journal
->j_start
<= temp_jl
->j_start
) {
4135 if ((journal
->j_start
+ journal
->j_trans_max
+ 1) >=
4137 flush_used_journal_lists(p_s_sb
, temp_jl
);
4139 } else if ((journal
->j_start
+
4140 journal
->j_trans_max
+ 1) <
4141 SB_ONDISK_JOURNAL_SIZE(p_s_sb
)) {
4142 /* if we don't cross into the next transaction and we don't
4143 * wrap, there is no way we can overlap any later transactions
4148 } else if ((journal
->j_start
+
4149 journal
->j_trans_max
+ 1) >
4150 SB_ONDISK_JOURNAL_SIZE(p_s_sb
)) {
4151 if (((journal
->j_start
+ journal
->j_trans_max
+ 1) %
4152 SB_ONDISK_JOURNAL_SIZE(p_s_sb
)) >=
4154 flush_used_journal_lists(p_s_sb
, temp_jl
);
4157 /* we don't overlap anything from out start to the end of the
4158 * log, and our wrapped portion doesn't overlap anything at
4159 * the start of the log. We can break
4165 flush_old_journal_lists(p_s_sb
);
4167 journal
->j_current_jl
->j_list_bitmap
=
4168 get_list_bitmap(p_s_sb
, journal
->j_current_jl
);
4170 if (!(journal
->j_current_jl
->j_list_bitmap
)) {
4171 reiserfs_panic(p_s_sb
,
4172 "journal-1996: do_journal_end, could not get a list bitmap\n");
4175 atomic_set(&(journal
->j_jlock
), 0);
4176 unlock_journal(p_s_sb
);
4177 /* wake up any body waiting to join. */
4178 clear_bit(J_WRITERS_QUEUED
, &journal
->j_state
);
4179 wake_up(&(journal
->j_join_wait
));
4181 if (!flush
&& wait_on_commit
&&
4182 journal_list_still_alive(p_s_sb
, commit_trans_id
)) {
4183 flush_commit_list(p_s_sb
, jl
, 1);
4186 reiserfs_check_lock_depth(p_s_sb
, "journal end2");
4188 memset(th
, 0, sizeof(*th
));
4189 /* Re-set th->t_super, so we can properly keep track of how many
4190 * persistent transactions there are. We need to do this so if this
4191 * call is part of a failed restart_transaction, we can free it later */
4192 th
->t_super
= p_s_sb
;
4194 return journal
->j_errno
;
4197 static void __reiserfs_journal_abort_hard(struct super_block
*sb
)
4199 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
4200 if (test_bit(J_ABORTED
, &journal
->j_state
))
4203 printk(KERN_CRIT
"REISERFS: Aborting journal for filesystem on %s\n",
4204 reiserfs_bdevname(sb
));
4206 sb
->s_flags
|= MS_RDONLY
;
4207 set_bit(J_ABORTED
, &journal
->j_state
);
4209 #ifdef CONFIG_REISERFS_CHECK
4214 static void __reiserfs_journal_abort_soft(struct super_block
*sb
, int errno
)
4216 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
4217 if (test_bit(J_ABORTED
, &journal
->j_state
))
4220 if (!journal
->j_errno
)
4221 journal
->j_errno
= errno
;
4223 __reiserfs_journal_abort_hard(sb
);
4226 void reiserfs_journal_abort(struct super_block
*sb
, int errno
)
4228 return __reiserfs_journal_abort_soft(sb
, errno
);