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/time.h>
38 #include <linux/semaphore.h>
39 #include <linux/vmalloc.h>
40 #include <linux/reiserfs_fs.h>
41 #include <linux/kernel.h>
42 #include <linux/errno.h>
43 #include <linux/fcntl.h>
44 #include <linux/stat.h>
45 #include <linux/string.h>
46 #include <linux/smp_lock.h>
47 #include <linux/buffer_head.h>
48 #include <linux/workqueue.h>
49 #include <linux/writeback.h>
50 #include <linux/blkdev.h>
51 #include <linux/backing-dev.h>
52 #include <linux/uaccess.h>
53 #include <linux/slab.h>
55 #include <asm/system.h>
57 /* gets a struct reiserfs_journal_list * from a list head */
58 #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
60 #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
63 /* the number of mounted filesystems. This is used to decide when to
64 ** start and kill the commit workqueue
66 static int reiserfs_mounted_fs_count
;
68 static struct workqueue_struct
*commit_wq
;
70 #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
72 #define BUFNR 64 /*read ahead */
74 /* cnode stat bits. Move these into reiserfs_fs.h */
76 #define BLOCK_FREED 2 /* this block was freed, and can't be written. */
77 #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
79 #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
80 #define BLOCK_DIRTIED 5
82 /* journal list state bits */
83 #define LIST_TOUCHED 1
85 #define LIST_COMMIT_PENDING 4 /* someone will commit this list */
87 /* flags for do_journal_end */
88 #define FLUSH_ALL 1 /* flush commit and real blocks */
89 #define COMMIT_NOW 2 /* end and commit this transaction */
90 #define WAIT 4 /* wait for the log blocks to hit the disk */
92 static int do_journal_end(struct reiserfs_transaction_handle
*,
93 struct super_block
*, unsigned long nblocks
,
95 static int flush_journal_list(struct super_block
*s
,
96 struct reiserfs_journal_list
*jl
, int flushall
);
97 static int flush_commit_list(struct super_block
*s
,
98 struct reiserfs_journal_list
*jl
, int flushall
);
99 static int can_dirty(struct reiserfs_journal_cnode
*cn
);
100 static int journal_join(struct reiserfs_transaction_handle
*th
,
101 struct super_block
*sb
, unsigned long nblocks
);
102 static int release_journal_dev(struct super_block
*super
,
103 struct reiserfs_journal
*journal
);
104 static int dirty_one_transaction(struct super_block
*s
,
105 struct reiserfs_journal_list
*jl
);
106 static void flush_async_commits(struct work_struct
*work
);
107 static void queue_log_writer(struct super_block
*s
);
109 /* values for join in do_journal_begin_r */
111 JBEGIN_REG
= 0, /* regular journal begin */
112 JBEGIN_JOIN
= 1, /* join the running transaction if at all possible */
113 JBEGIN_ABORT
= 2, /* called from cleanup code, ignores aborted flag */
116 static int do_journal_begin_r(struct reiserfs_transaction_handle
*th
,
117 struct super_block
*sb
,
118 unsigned long nblocks
, int join
);
120 static void init_journal_hash(struct super_block
*sb
)
122 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
123 memset(journal
->j_hash_table
, 0,
124 JOURNAL_HASH_SIZE
* sizeof(struct reiserfs_journal_cnode
*));
128 ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
129 ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
132 static int reiserfs_clean_and_file_buffer(struct buffer_head
*bh
)
135 clear_buffer_dirty(bh
);
136 clear_buffer_journal_test(bh
);
141 static struct reiserfs_bitmap_node
*allocate_bitmap_node(struct super_block
144 struct reiserfs_bitmap_node
*bn
;
147 bn
= kmalloc(sizeof(struct reiserfs_bitmap_node
), GFP_NOFS
);
151 bn
->data
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
157 INIT_LIST_HEAD(&bn
->list
);
161 static struct reiserfs_bitmap_node
*get_bitmap_node(struct super_block
*sb
)
163 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
164 struct reiserfs_bitmap_node
*bn
= NULL
;
165 struct list_head
*entry
= journal
->j_bitmap_nodes
.next
;
167 journal
->j_used_bitmap_nodes
++;
170 if (entry
!= &journal
->j_bitmap_nodes
) {
171 bn
= list_entry(entry
, struct reiserfs_bitmap_node
, list
);
173 memset(bn
->data
, 0, sb
->s_blocksize
);
174 journal
->j_free_bitmap_nodes
--;
177 bn
= allocate_bitmap_node(sb
);
184 static inline void free_bitmap_node(struct super_block
*sb
,
185 struct reiserfs_bitmap_node
*bn
)
187 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
188 journal
->j_used_bitmap_nodes
--;
189 if (journal
->j_free_bitmap_nodes
> REISERFS_MAX_BITMAP_NODES
) {
193 list_add(&bn
->list
, &journal
->j_bitmap_nodes
);
194 journal
->j_free_bitmap_nodes
++;
198 static void allocate_bitmap_nodes(struct super_block
*sb
)
201 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
202 struct reiserfs_bitmap_node
*bn
= NULL
;
203 for (i
= 0; i
< REISERFS_MIN_BITMAP_NODES
; i
++) {
204 bn
= allocate_bitmap_node(sb
);
206 list_add(&bn
->list
, &journal
->j_bitmap_nodes
);
207 journal
->j_free_bitmap_nodes
++;
209 break; /* this is ok, we'll try again when more are needed */
214 static int set_bit_in_list_bitmap(struct super_block
*sb
,
216 struct reiserfs_list_bitmap
*jb
)
218 unsigned int bmap_nr
= block
/ (sb
->s_blocksize
<< 3);
219 unsigned int bit_nr
= block
% (sb
->s_blocksize
<< 3);
221 if (!jb
->bitmaps
[bmap_nr
]) {
222 jb
->bitmaps
[bmap_nr
] = get_bitmap_node(sb
);
224 set_bit(bit_nr
, (unsigned long *)jb
->bitmaps
[bmap_nr
]->data
);
228 static void cleanup_bitmap_list(struct super_block
*sb
,
229 struct reiserfs_list_bitmap
*jb
)
232 if (jb
->bitmaps
== NULL
)
235 for (i
= 0; i
< reiserfs_bmap_count(sb
); i
++) {
236 if (jb
->bitmaps
[i
]) {
237 free_bitmap_node(sb
, jb
->bitmaps
[i
]);
238 jb
->bitmaps
[i
] = NULL
;
244 ** only call this on FS unmount.
246 static int free_list_bitmaps(struct super_block
*sb
,
247 struct reiserfs_list_bitmap
*jb_array
)
250 struct reiserfs_list_bitmap
*jb
;
251 for (i
= 0; i
< JOURNAL_NUM_BITMAPS
; i
++) {
253 jb
->journal_list
= NULL
;
254 cleanup_bitmap_list(sb
, jb
);
261 static int free_bitmap_nodes(struct super_block
*sb
)
263 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
264 struct list_head
*next
= journal
->j_bitmap_nodes
.next
;
265 struct reiserfs_bitmap_node
*bn
;
267 while (next
!= &journal
->j_bitmap_nodes
) {
268 bn
= list_entry(next
, struct reiserfs_bitmap_node
, list
);
272 next
= journal
->j_bitmap_nodes
.next
;
273 journal
->j_free_bitmap_nodes
--;
280 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
281 ** jb_array is the array to be filled in.
283 int reiserfs_allocate_list_bitmaps(struct super_block
*sb
,
284 struct reiserfs_list_bitmap
*jb_array
,
285 unsigned int bmap_nr
)
289 struct reiserfs_list_bitmap
*jb
;
290 int mem
= bmap_nr
* sizeof(struct reiserfs_bitmap_node
*);
292 for (i
= 0; i
< JOURNAL_NUM_BITMAPS
; i
++) {
294 jb
->journal_list
= NULL
;
295 jb
->bitmaps
= vmalloc(mem
);
297 reiserfs_warning(sb
, "clm-2000", "unable to "
298 "allocate bitmaps for journal lists");
302 memset(jb
->bitmaps
, 0, mem
);
305 free_list_bitmaps(sb
, jb_array
);
312 ** find an available list bitmap. If you can't find one, flush a commit list
315 static struct reiserfs_list_bitmap
*get_list_bitmap(struct super_block
*sb
,
316 struct reiserfs_journal_list
320 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
321 struct reiserfs_list_bitmap
*jb
= NULL
;
323 for (j
= 0; j
< (JOURNAL_NUM_BITMAPS
* 3); j
++) {
324 i
= journal
->j_list_bitmap_index
;
325 journal
->j_list_bitmap_index
= (i
+ 1) % JOURNAL_NUM_BITMAPS
;
326 jb
= journal
->j_list_bitmap
+ i
;
327 if (journal
->j_list_bitmap
[i
].journal_list
) {
328 flush_commit_list(sb
,
329 journal
->j_list_bitmap
[i
].
331 if (!journal
->j_list_bitmap
[i
].journal_list
) {
338 if (jb
->journal_list
) { /* double check to make sure if flushed correctly */
341 jb
->journal_list
= jl
;
346 ** allocates a new chunk of X nodes, and links them all together as a list.
347 ** Uses the cnode->next and cnode->prev pointers
348 ** returns NULL on failure
350 static struct reiserfs_journal_cnode
*allocate_cnodes(int num_cnodes
)
352 struct reiserfs_journal_cnode
*head
;
354 if (num_cnodes
<= 0) {
357 head
= vmalloc(num_cnodes
* sizeof(struct reiserfs_journal_cnode
));
361 memset(head
, 0, num_cnodes
* sizeof(struct reiserfs_journal_cnode
));
363 head
[0].next
= head
+ 1;
364 for (i
= 1; i
< num_cnodes
; i
++) {
365 head
[i
].prev
= head
+ (i
- 1);
366 head
[i
].next
= head
+ (i
+ 1); /* if last one, overwrite it after the if */
368 head
[num_cnodes
- 1].next
= NULL
;
373 ** pulls a cnode off the free list, or returns NULL on failure
375 static struct reiserfs_journal_cnode
*get_cnode(struct super_block
*sb
)
377 struct reiserfs_journal_cnode
*cn
;
378 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
380 reiserfs_check_lock_depth(sb
, "get_cnode");
382 if (journal
->j_cnode_free
<= 0) {
385 journal
->j_cnode_used
++;
386 journal
->j_cnode_free
--;
387 cn
= journal
->j_cnode_free_list
;
392 cn
->next
->prev
= NULL
;
394 journal
->j_cnode_free_list
= cn
->next
;
395 memset(cn
, 0, sizeof(struct reiserfs_journal_cnode
));
400 ** returns a cnode to the free list
402 static void free_cnode(struct super_block
*sb
,
403 struct reiserfs_journal_cnode
*cn
)
405 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
407 reiserfs_check_lock_depth(sb
, "free_cnode");
409 journal
->j_cnode_used
--;
410 journal
->j_cnode_free
++;
411 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
412 cn
->next
= journal
->j_cnode_free_list
;
413 if (journal
->j_cnode_free_list
) {
414 journal
->j_cnode_free_list
->prev
= cn
;
416 cn
->prev
= NULL
; /* not needed with the memset, but I might kill the memset, and forget to do this */
417 journal
->j_cnode_free_list
= cn
;
420 static void clear_prepared_bits(struct buffer_head
*bh
)
422 clear_buffer_journal_prepared(bh
);
423 clear_buffer_journal_restore_dirty(bh
);
426 /* return a cnode with same dev, block number and size in table, or null if not found */
427 static inline struct reiserfs_journal_cnode
*get_journal_hash_dev(struct
431 reiserfs_journal_cnode
435 struct reiserfs_journal_cnode
*cn
;
436 cn
= journal_hash(table
, sb
, bl
);
438 if (cn
->blocknr
== bl
&& cn
->sb
== sb
)
442 return (struct reiserfs_journal_cnode
*)0;
446 ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
447 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
448 ** being overwritten by a replay after crashing.
450 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
451 ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
452 ** sure you never write the block without logging it.
454 ** next_zero_bit is a suggestion about the next block to try for find_forward.
455 ** when bl is rejected because it is set in a journal list bitmap, we search
456 ** for the next zero bit in the bitmap that rejected bl. Then, we return that
457 ** through next_zero_bit for find_forward to try.
459 ** Just because we return something in next_zero_bit does not mean we won't
460 ** reject it on the next call to reiserfs_in_journal
463 int reiserfs_in_journal(struct super_block
*sb
,
464 unsigned int bmap_nr
, int bit_nr
, int search_all
,
465 b_blocknr_t
* next_zero_bit
)
467 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
468 struct reiserfs_journal_cnode
*cn
;
469 struct reiserfs_list_bitmap
*jb
;
473 *next_zero_bit
= 0; /* always start this at zero. */
475 PROC_INFO_INC(sb
, journal
.in_journal
);
476 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
477 ** if we crash before the transaction that freed it commits, this transaction won't
478 ** have committed either, and the block will never be written
481 for (i
= 0; i
< JOURNAL_NUM_BITMAPS
; i
++) {
482 PROC_INFO_INC(sb
, journal
.in_journal_bitmap
);
483 jb
= journal
->j_list_bitmap
+ i
;
484 if (jb
->journal_list
&& jb
->bitmaps
[bmap_nr
] &&
486 (unsigned long *)jb
->bitmaps
[bmap_nr
]->
489 find_next_zero_bit((unsigned long *)
490 (jb
->bitmaps
[bmap_nr
]->
492 sb
->s_blocksize
<< 3,
499 bl
= bmap_nr
* (sb
->s_blocksize
<< 3) + bit_nr
;
500 /* is it in any old transactions? */
503 get_journal_hash_dev(sb
, journal
->j_list_hash_table
, bl
))) {
507 /* is it in the current transaction. This should never happen */
508 if ((cn
= get_journal_hash_dev(sb
, journal
->j_hash_table
, bl
))) {
513 PROC_INFO_INC(sb
, journal
.in_journal_reusable
);
518 /* insert cn into table
520 static inline void insert_journal_hash(struct reiserfs_journal_cnode
**table
,
521 struct reiserfs_journal_cnode
*cn
)
523 struct reiserfs_journal_cnode
*cn_orig
;
525 cn_orig
= journal_hash(table
, cn
->sb
, cn
->blocknr
);
531 journal_hash(table
, cn
->sb
, cn
->blocknr
) = cn
;
534 /* lock the current transaction */
535 static inline void lock_journal(struct super_block
*sb
)
537 PROC_INFO_INC(sb
, journal
.lock_journal
);
539 reiserfs_mutex_lock_safe(&SB_JOURNAL(sb
)->j_mutex
, sb
);
542 /* unlock the current transaction */
543 static inline void unlock_journal(struct super_block
*sb
)
545 mutex_unlock(&SB_JOURNAL(sb
)->j_mutex
);
548 static inline void get_journal_list(struct reiserfs_journal_list
*jl
)
553 static inline void put_journal_list(struct super_block
*s
,
554 struct reiserfs_journal_list
*jl
)
556 if (jl
->j_refcount
< 1) {
557 reiserfs_panic(s
, "journal-2", "trans id %u, refcount at %d",
558 jl
->j_trans_id
, jl
->j_refcount
);
560 if (--jl
->j_refcount
== 0)
565 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
566 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
569 static void cleanup_freed_for_journal_list(struct super_block
*sb
,
570 struct reiserfs_journal_list
*jl
)
573 struct reiserfs_list_bitmap
*jb
= jl
->j_list_bitmap
;
575 cleanup_bitmap_list(sb
, jb
);
577 jl
->j_list_bitmap
->journal_list
= NULL
;
578 jl
->j_list_bitmap
= NULL
;
581 static int journal_list_still_alive(struct super_block
*s
,
582 unsigned int trans_id
)
584 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
585 struct list_head
*entry
= &journal
->j_journal_list
;
586 struct reiserfs_journal_list
*jl
;
588 if (!list_empty(entry
)) {
589 jl
= JOURNAL_LIST_ENTRY(entry
->next
);
590 if (jl
->j_trans_id
<= trans_id
) {
598 * If page->mapping was null, we failed to truncate this page for
599 * some reason. Most likely because it was truncated after being
600 * logged via data=journal.
602 * This does a check to see if the buffer belongs to one of these
603 * lost pages before doing the final put_bh. If page->mapping was
604 * null, it tries to free buffers on the page, which should make the
605 * final page_cache_release drop the page from the lru.
607 static void release_buffer_page(struct buffer_head
*bh
)
609 struct page
*page
= bh
->b_page
;
610 if (!page
->mapping
&& trylock_page(page
)) {
611 page_cache_get(page
);
614 try_to_free_buffers(page
);
616 page_cache_release(page
);
622 static void reiserfs_end_buffer_io_sync(struct buffer_head
*bh
, int uptodate
)
624 char b
[BDEVNAME_SIZE
];
626 if (buffer_journaled(bh
)) {
627 reiserfs_warning(NULL
, "clm-2084",
628 "pinned buffer %lu:%s sent to disk",
629 bh
->b_blocknr
, bdevname(bh
->b_bdev
, b
));
632 set_buffer_uptodate(bh
);
634 clear_buffer_uptodate(bh
);
637 release_buffer_page(bh
);
640 static void reiserfs_end_ordered_io(struct buffer_head
*bh
, int uptodate
)
643 set_buffer_uptodate(bh
);
645 clear_buffer_uptodate(bh
);
650 static void submit_logged_buffer(struct buffer_head
*bh
)
653 bh
->b_end_io
= reiserfs_end_buffer_io_sync
;
654 clear_buffer_journal_new(bh
);
655 clear_buffer_dirty(bh
);
656 if (!test_clear_buffer_journal_test(bh
))
658 if (!buffer_uptodate(bh
))
660 submit_bh(WRITE
, bh
);
663 static void submit_ordered_buffer(struct buffer_head
*bh
)
666 bh
->b_end_io
= reiserfs_end_ordered_io
;
667 clear_buffer_dirty(bh
);
668 if (!buffer_uptodate(bh
))
670 submit_bh(WRITE
, bh
);
673 #define CHUNK_SIZE 32
674 struct buffer_chunk
{
675 struct buffer_head
*bh
[CHUNK_SIZE
];
679 static void write_chunk(struct buffer_chunk
*chunk
)
683 for (i
= 0; i
< chunk
->nr
; i
++) {
684 submit_logged_buffer(chunk
->bh
[i
]);
690 static void write_ordered_chunk(struct buffer_chunk
*chunk
)
694 for (i
= 0; i
< chunk
->nr
; i
++) {
695 submit_ordered_buffer(chunk
->bh
[i
]);
701 static int add_to_chunk(struct buffer_chunk
*chunk
, struct buffer_head
*bh
,
702 spinlock_t
* lock
, void (fn
) (struct buffer_chunk
*))
705 BUG_ON(chunk
->nr
>= CHUNK_SIZE
);
706 chunk
->bh
[chunk
->nr
++] = bh
;
707 if (chunk
->nr
>= CHUNK_SIZE
) {
718 static atomic_t nr_reiserfs_jh
= ATOMIC_INIT(0);
719 static struct reiserfs_jh
*alloc_jh(void)
721 struct reiserfs_jh
*jh
;
723 jh
= kmalloc(sizeof(*jh
), GFP_NOFS
);
725 atomic_inc(&nr_reiserfs_jh
);
733 * we want to free the jh when the buffer has been written
736 void reiserfs_free_jh(struct buffer_head
*bh
)
738 struct reiserfs_jh
*jh
;
742 bh
->b_private
= NULL
;
744 list_del_init(&jh
->list
);
746 if (atomic_read(&nr_reiserfs_jh
) <= 0)
748 atomic_dec(&nr_reiserfs_jh
);
753 static inline int __add_jh(struct reiserfs_journal
*j
, struct buffer_head
*bh
,
756 struct reiserfs_jh
*jh
;
759 spin_lock(&j
->j_dirty_buffers_lock
);
760 if (!bh
->b_private
) {
761 spin_unlock(&j
->j_dirty_buffers_lock
);
765 list_del_init(&jh
->list
);
770 spin_lock(&j
->j_dirty_buffers_lock
);
771 /* buffer must be locked for __add_jh, should be able to have
772 * two adds at the same time
774 BUG_ON(bh
->b_private
);
778 jh
->jl
= j
->j_current_jl
;
780 list_add_tail(&jh
->list
, &jh
->jl
->j_tail_bh_list
);
782 list_add_tail(&jh
->list
, &jh
->jl
->j_bh_list
);
784 spin_unlock(&j
->j_dirty_buffers_lock
);
788 int reiserfs_add_tail_list(struct inode
*inode
, struct buffer_head
*bh
)
790 return __add_jh(SB_JOURNAL(inode
->i_sb
), bh
, 1);
792 int reiserfs_add_ordered_list(struct inode
*inode
, struct buffer_head
*bh
)
794 return __add_jh(SB_JOURNAL(inode
->i_sb
), bh
, 0);
797 #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
798 static int write_ordered_buffers(spinlock_t
* lock
,
799 struct reiserfs_journal
*j
,
800 struct reiserfs_journal_list
*jl
,
801 struct list_head
*list
)
803 struct buffer_head
*bh
;
804 struct reiserfs_jh
*jh
;
805 int ret
= j
->j_errno
;
806 struct buffer_chunk chunk
;
807 struct list_head tmp
;
808 INIT_LIST_HEAD(&tmp
);
812 while (!list_empty(list
)) {
813 jh
= JH_ENTRY(list
->next
);
816 if (!trylock_buffer(bh
)) {
817 if (!buffer_dirty(bh
)) {
818 list_move(&jh
->list
, &tmp
);
823 write_ordered_chunk(&chunk
);
829 /* in theory, dirty non-uptodate buffers should never get here,
830 * but the upper layer io error paths still have a few quirks.
831 * Handle them here as gracefully as we can
833 if (!buffer_uptodate(bh
) && buffer_dirty(bh
)) {
834 clear_buffer_dirty(bh
);
837 if (buffer_dirty(bh
)) {
838 list_move(&jh
->list
, &tmp
);
839 add_to_chunk(&chunk
, bh
, lock
, write_ordered_chunk
);
841 reiserfs_free_jh(bh
);
846 cond_resched_lock(lock
);
850 write_ordered_chunk(&chunk
);
853 while (!list_empty(&tmp
)) {
854 jh
= JH_ENTRY(tmp
.prev
);
857 reiserfs_free_jh(bh
);
859 if (buffer_locked(bh
)) {
864 if (!buffer_uptodate(bh
)) {
867 /* ugly interaction with invalidatepage here.
868 * reiserfs_invalidate_page will pin any buffer that has a valid
869 * journal head from an older transaction. If someone else sets
870 * our buffer dirty after we write it in the first loop, and
871 * then someone truncates the page away, nobody will ever write
872 * the buffer. We're safe if we write the page one last time
873 * after freeing the journal header.
875 if (buffer_dirty(bh
) && unlikely(bh
->b_page
->mapping
== NULL
)) {
877 ll_rw_block(WRITE
, 1, &bh
);
881 cond_resched_lock(lock
);
887 static int flush_older_commits(struct super_block
*s
,
888 struct reiserfs_journal_list
*jl
)
890 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
891 struct reiserfs_journal_list
*other_jl
;
892 struct reiserfs_journal_list
*first_jl
;
893 struct list_head
*entry
;
894 unsigned int trans_id
= jl
->j_trans_id
;
895 unsigned int other_trans_id
;
896 unsigned int first_trans_id
;
900 * first we walk backwards to find the oldest uncommitted transation
903 entry
= jl
->j_list
.prev
;
905 other_jl
= JOURNAL_LIST_ENTRY(entry
);
906 if (entry
== &journal
->j_journal_list
||
907 atomic_read(&other_jl
->j_older_commits_done
))
911 entry
= other_jl
->j_list
.prev
;
914 /* if we didn't find any older uncommitted transactions, return now */
915 if (first_jl
== jl
) {
919 first_trans_id
= first_jl
->j_trans_id
;
921 entry
= &first_jl
->j_list
;
923 other_jl
= JOURNAL_LIST_ENTRY(entry
);
924 other_trans_id
= other_jl
->j_trans_id
;
926 if (other_trans_id
< trans_id
) {
927 if (atomic_read(&other_jl
->j_commit_left
) != 0) {
928 flush_commit_list(s
, other_jl
, 0);
930 /* list we were called with is gone, return */
931 if (!journal_list_still_alive(s
, trans_id
))
934 /* the one we just flushed is gone, this means all
935 * older lists are also gone, so first_jl is no longer
936 * valid either. Go back to the beginning.
938 if (!journal_list_still_alive
939 (s
, other_trans_id
)) {
944 if (entry
== &journal
->j_journal_list
)
953 static int reiserfs_async_progress_wait(struct super_block
*s
)
955 struct reiserfs_journal
*j
= SB_JOURNAL(s
);
957 if (atomic_read(&j
->j_async_throttle
)) {
958 reiserfs_write_unlock(s
);
959 congestion_wait(BLK_RW_ASYNC
, HZ
/ 10);
960 reiserfs_write_lock(s
);
967 ** if this journal list still has commit blocks unflushed, send them to disk.
969 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
970 ** Before the commit block can by written, every other log block must be safely on disk
973 static int flush_commit_list(struct super_block
*s
,
974 struct reiserfs_journal_list
*jl
, int flushall
)
978 struct buffer_head
*tbh
= NULL
;
979 unsigned int trans_id
= jl
->j_trans_id
;
980 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
984 reiserfs_check_lock_depth(s
, "flush_commit_list");
986 if (atomic_read(&jl
->j_older_commits_done
)) {
992 /* before we can put our commit blocks on disk, we have to make sure everyone older than
995 BUG_ON(jl
->j_len
<= 0);
996 BUG_ON(trans_id
== journal
->j_trans_id
);
998 get_journal_list(jl
);
1000 if (flush_older_commits(s
, jl
) == 1) {
1001 /* list disappeared during flush_older_commits. return */
1006 /* make sure nobody is trying to flush this one at the same time */
1007 reiserfs_mutex_lock_safe(&jl
->j_commit_mutex
, s
);
1009 if (!journal_list_still_alive(s
, trans_id
)) {
1010 mutex_unlock(&jl
->j_commit_mutex
);
1013 BUG_ON(jl
->j_trans_id
== 0);
1015 /* this commit is done, exit */
1016 if (atomic_read(&(jl
->j_commit_left
)) <= 0) {
1018 atomic_set(&(jl
->j_older_commits_done
), 1);
1020 mutex_unlock(&jl
->j_commit_mutex
);
1024 if (!list_empty(&jl
->j_bh_list
)) {
1028 * We might sleep in numerous places inside
1029 * write_ordered_buffers. Relax the write lock.
1031 reiserfs_write_unlock(s
);
1032 ret
= write_ordered_buffers(&journal
->j_dirty_buffers_lock
,
1033 journal
, jl
, &jl
->j_bh_list
);
1034 if (ret
< 0 && retval
== 0)
1036 reiserfs_write_lock(s
);
1038 BUG_ON(!list_empty(&jl
->j_bh_list
));
1040 * for the description block and all the log blocks, submit any buffers
1041 * that haven't already reached the disk. Try to write at least 256
1042 * log blocks. later on, we will only wait on blocks that correspond
1043 * to this transaction, but while we're unplugging we might as well
1044 * get a chunk of data on there.
1046 atomic_inc(&journal
->j_async_throttle
);
1047 write_len
= jl
->j_len
+ 1;
1048 if (write_len
< 256)
1050 for (i
= 0 ; i
< write_len
; i
++) {
1051 bn
= SB_ONDISK_JOURNAL_1st_BLOCK(s
) + (jl
->j_start
+ i
) %
1052 SB_ONDISK_JOURNAL_SIZE(s
);
1053 tbh
= journal_find_get_block(s
, bn
);
1055 if (buffer_dirty(tbh
)) {
1056 reiserfs_write_unlock(s
);
1057 ll_rw_block(WRITE
, 1, &tbh
);
1058 reiserfs_write_lock(s
);
1063 atomic_dec(&journal
->j_async_throttle
);
1065 for (i
= 0; i
< (jl
->j_len
+ 1); i
++) {
1066 bn
= SB_ONDISK_JOURNAL_1st_BLOCK(s
) +
1067 (jl
->j_start
+ i
) % SB_ONDISK_JOURNAL_SIZE(s
);
1068 tbh
= journal_find_get_block(s
, bn
);
1070 reiserfs_write_unlock(s
);
1071 wait_on_buffer(tbh
);
1072 reiserfs_write_lock(s
);
1073 // since we're using ll_rw_blk above, it might have skipped over
1074 // a locked buffer. Double check here
1076 /* redundant, sync_dirty_buffer() checks */
1077 if (buffer_dirty(tbh
)) {
1078 reiserfs_write_unlock(s
);
1079 sync_dirty_buffer(tbh
);
1080 reiserfs_write_lock(s
);
1082 if (unlikely(!buffer_uptodate(tbh
))) {
1083 #ifdef CONFIG_REISERFS_CHECK
1084 reiserfs_warning(s
, "journal-601",
1085 "buffer write failed");
1089 put_bh(tbh
); /* once for journal_find_get_block */
1090 put_bh(tbh
); /* once due to original getblk in do_journal_end */
1091 atomic_dec(&(jl
->j_commit_left
));
1094 BUG_ON(atomic_read(&(jl
->j_commit_left
)) != 1);
1096 /* If there was a write error in the journal - we can't commit
1097 * this transaction - it will be invalid and, if successful,
1098 * will just end up propagating the write error out to
1099 * the file system. */
1100 if (likely(!retval
&& !reiserfs_is_journal_aborted (journal
))) {
1101 if (buffer_dirty(jl
->j_commit_bh
))
1103 mark_buffer_dirty(jl
->j_commit_bh
) ;
1104 reiserfs_write_unlock(s
);
1105 if (reiserfs_barrier_flush(s
))
1106 __sync_dirty_buffer(jl
->j_commit_bh
, WRITE_FLUSH_FUA
);
1108 sync_dirty_buffer(jl
->j_commit_bh
);
1109 reiserfs_write_lock(s
);
1112 /* If there was a write error in the journal - we can't commit this
1113 * transaction - it will be invalid and, if successful, will just end
1114 * up propagating the write error out to the filesystem. */
1115 if (unlikely(!buffer_uptodate(jl
->j_commit_bh
))) {
1116 #ifdef CONFIG_REISERFS_CHECK
1117 reiserfs_warning(s
, "journal-615", "buffer write failed");
1121 bforget(jl
->j_commit_bh
);
1122 if (journal
->j_last_commit_id
!= 0 &&
1123 (jl
->j_trans_id
- journal
->j_last_commit_id
) != 1) {
1124 reiserfs_warning(s
, "clm-2200", "last commit %lu, current %lu",
1125 journal
->j_last_commit_id
, jl
->j_trans_id
);
1127 journal
->j_last_commit_id
= jl
->j_trans_id
;
1129 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1130 cleanup_freed_for_journal_list(s
, jl
);
1132 retval
= retval
? retval
: journal
->j_errno
;
1134 /* mark the metadata dirty */
1136 dirty_one_transaction(s
, jl
);
1137 atomic_dec(&(jl
->j_commit_left
));
1140 atomic_set(&(jl
->j_older_commits_done
), 1);
1142 mutex_unlock(&jl
->j_commit_mutex
);
1144 put_journal_list(s
, jl
);
1147 reiserfs_abort(s
, retval
, "Journal write error in %s",
1154 ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1155 ** returns NULL if it can't find anything
1157 static struct reiserfs_journal_list
*find_newer_jl_for_cn(struct
1158 reiserfs_journal_cnode
1161 struct super_block
*sb
= cn
->sb
;
1162 b_blocknr_t blocknr
= cn
->blocknr
;
1166 if (cn
->sb
== sb
&& cn
->blocknr
== blocknr
&& cn
->jlist
) {
1174 static int newer_jl_done(struct reiserfs_journal_cnode
*cn
)
1176 struct super_block
*sb
= cn
->sb
;
1177 b_blocknr_t blocknr
= cn
->blocknr
;
1181 if (cn
->sb
== sb
&& cn
->blocknr
== blocknr
&& cn
->jlist
&&
1182 atomic_read(&cn
->jlist
->j_commit_left
) != 0)
1189 static void remove_journal_hash(struct super_block
*,
1190 struct reiserfs_journal_cnode
**,
1191 struct reiserfs_journal_list
*, unsigned long,
1195 ** once all the real blocks have been flushed, it is safe to remove them from the
1196 ** journal list for this transaction. Aside from freeing the cnode, this also allows the
1197 ** block to be reallocated for data blocks if it had been deleted.
1199 static void remove_all_from_journal_list(struct super_block
*sb
,
1200 struct reiserfs_journal_list
*jl
,
1203 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
1204 struct reiserfs_journal_cnode
*cn
, *last
;
1205 cn
= jl
->j_realblock
;
1207 /* which is better, to lock once around the whole loop, or
1208 ** to lock for each call to remove_journal_hash?
1211 if (cn
->blocknr
!= 0) {
1213 reiserfs_warning(sb
, "reiserfs-2201",
1214 "block %u, bh is %d, state %ld",
1215 cn
->blocknr
, cn
->bh
? 1 : 0,
1219 remove_journal_hash(sb
, journal
->j_list_hash_table
,
1220 jl
, cn
->blocknr
, 1);
1224 free_cnode(sb
, last
);
1226 jl
->j_realblock
= NULL
;
1230 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1231 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1232 ** releasing blocks in this transaction for reuse as data blocks.
1233 ** called by flush_journal_list, before it calls remove_all_from_journal_list
1236 static int _update_journal_header_block(struct super_block
*sb
,
1237 unsigned long offset
,
1238 unsigned int trans_id
)
1240 struct reiserfs_journal_header
*jh
;
1241 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
1243 if (reiserfs_is_journal_aborted(journal
))
1246 if (trans_id
>= journal
->j_last_flush_trans_id
) {
1247 if (buffer_locked((journal
->j_header_bh
))) {
1248 reiserfs_write_unlock(sb
);
1249 wait_on_buffer((journal
->j_header_bh
));
1250 reiserfs_write_lock(sb
);
1251 if (unlikely(!buffer_uptodate(journal
->j_header_bh
))) {
1252 #ifdef CONFIG_REISERFS_CHECK
1253 reiserfs_warning(sb
, "journal-699",
1254 "buffer write failed");
1259 journal
->j_last_flush_trans_id
= trans_id
;
1260 journal
->j_first_unflushed_offset
= offset
;
1261 jh
= (struct reiserfs_journal_header
*)(journal
->j_header_bh
->
1263 jh
->j_last_flush_trans_id
= cpu_to_le32(trans_id
);
1264 jh
->j_first_unflushed_offset
= cpu_to_le32(offset
);
1265 jh
->j_mount_id
= cpu_to_le32(journal
->j_mount_id
);
1267 set_buffer_dirty(journal
->j_header_bh
);
1268 reiserfs_write_unlock(sb
);
1270 if (reiserfs_barrier_flush(sb
))
1271 __sync_dirty_buffer(journal
->j_header_bh
, WRITE_FLUSH_FUA
);
1273 sync_dirty_buffer(journal
->j_header_bh
);
1275 reiserfs_write_lock(sb
);
1276 if (!buffer_uptodate(journal
->j_header_bh
)) {
1277 reiserfs_warning(sb
, "journal-837",
1278 "IO error during journal replay");
1285 static int update_journal_header_block(struct super_block
*sb
,
1286 unsigned long offset
,
1287 unsigned int trans_id
)
1289 return _update_journal_header_block(sb
, offset
, trans_id
);
1293 ** flush any and all journal lists older than you are
1294 ** can only be called from flush_journal_list
1296 static int flush_older_journal_lists(struct super_block
*sb
,
1297 struct reiserfs_journal_list
*jl
)
1299 struct list_head
*entry
;
1300 struct reiserfs_journal_list
*other_jl
;
1301 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
1302 unsigned int trans_id
= jl
->j_trans_id
;
1304 /* we know we are the only ones flushing things, no extra race
1305 * protection is required.
1308 entry
= journal
->j_journal_list
.next
;
1310 if (entry
== &journal
->j_journal_list
)
1312 other_jl
= JOURNAL_LIST_ENTRY(entry
);
1313 if (other_jl
->j_trans_id
< trans_id
) {
1314 BUG_ON(other_jl
->j_refcount
<= 0);
1315 /* do not flush all */
1316 flush_journal_list(sb
, other_jl
, 0);
1318 /* other_jl is now deleted from the list */
1324 static void del_from_work_list(struct super_block
*s
,
1325 struct reiserfs_journal_list
*jl
)
1327 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1328 if (!list_empty(&jl
->j_working_list
)) {
1329 list_del_init(&jl
->j_working_list
);
1330 journal
->j_num_work_lists
--;
1334 /* flush a journal list, both commit and real blocks
1336 ** always set flushall to 1, unless you are calling from inside
1337 ** flush_journal_list
1339 ** IMPORTANT. This can only be called while there are no journal writers,
1340 ** and the journal is locked. That means it can only be called from
1341 ** do_journal_end, or by journal_release
1343 static int flush_journal_list(struct super_block
*s
,
1344 struct reiserfs_journal_list
*jl
, int flushall
)
1346 struct reiserfs_journal_list
*pjl
;
1347 struct reiserfs_journal_cnode
*cn
, *last
;
1351 struct buffer_head
*saved_bh
;
1352 unsigned long j_len_saved
= jl
->j_len
;
1353 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1356 BUG_ON(j_len_saved
<= 0);
1358 if (atomic_read(&journal
->j_wcount
) != 0) {
1359 reiserfs_warning(s
, "clm-2048", "called with wcount %d",
1360 atomic_read(&journal
->j_wcount
));
1362 BUG_ON(jl
->j_trans_id
== 0);
1364 /* if flushall == 0, the lock is already held */
1366 reiserfs_mutex_lock_safe(&journal
->j_flush_mutex
, s
);
1367 } else if (mutex_trylock(&journal
->j_flush_mutex
)) {
1372 if (j_len_saved
> journal
->j_trans_max
) {
1373 reiserfs_panic(s
, "journal-715", "length is %lu, trans id %lu",
1374 j_len_saved
, jl
->j_trans_id
);
1380 /* if all the work is already done, get out of here */
1381 if (atomic_read(&(jl
->j_nonzerolen
)) <= 0 &&
1382 atomic_read(&(jl
->j_commit_left
)) <= 0) {
1383 goto flush_older_and_return
;
1386 /* start by putting the commit list on disk. This will also flush
1387 ** the commit lists of any olders transactions
1389 flush_commit_list(s
, jl
, 1);
1391 if (!(jl
->j_state
& LIST_DIRTY
)
1392 && !reiserfs_is_journal_aborted(journal
))
1395 /* are we done now? */
1396 if (atomic_read(&(jl
->j_nonzerolen
)) <= 0 &&
1397 atomic_read(&(jl
->j_commit_left
)) <= 0) {
1398 goto flush_older_and_return
;
1401 /* loop through each cnode, see if we need to write it,
1402 ** or wait on a more recent transaction, or just ignore it
1404 if (atomic_read(&(journal
->j_wcount
)) != 0) {
1405 reiserfs_panic(s
, "journal-844", "journal list is flushing, "
1408 cn
= jl
->j_realblock
;
1413 /* blocknr of 0 is no longer in the hash, ignore it */
1414 if (cn
->blocknr
== 0) {
1418 /* This transaction failed commit. Don't write out to the disk */
1419 if (!(jl
->j_state
& LIST_DIRTY
))
1422 pjl
= find_newer_jl_for_cn(cn
);
1423 /* the order is important here. We check pjl to make sure we
1424 ** don't clear BH_JDirty_wait if we aren't the one writing this
1427 if (!pjl
&& cn
->bh
) {
1430 /* we do this to make sure nobody releases the buffer while
1431 ** we are working with it
1435 if (buffer_journal_dirty(saved_bh
)) {
1436 BUG_ON(!can_dirty(cn
));
1439 } else if (can_dirty(cn
)) {
1440 /* everything with !pjl && jwait should be writable */
1445 /* if someone has this block in a newer transaction, just make
1446 ** sure they are committed, and don't try writing it to disk
1449 if (atomic_read(&pjl
->j_commit_left
))
1450 flush_commit_list(s
, pjl
, 1);
1454 /* bh == NULL when the block got to disk on its own, OR,
1455 ** the block got freed in a future transaction
1457 if (saved_bh
== NULL
) {
1461 /* this should never happen. kupdate_one_transaction has this list
1462 ** locked while it works, so we should never see a buffer here that
1463 ** is not marked JDirty_wait
1465 if ((!was_jwait
) && !buffer_locked(saved_bh
)) {
1466 reiserfs_warning(s
, "journal-813",
1467 "BAD! buffer %llu %cdirty %cjwait, "
1468 "not in a newer tranasction",
1469 (unsigned long long)saved_bh
->
1470 b_blocknr
, was_dirty
? ' ' : '!',
1471 was_jwait
? ' ' : '!');
1474 /* we inc again because saved_bh gets decremented at free_cnode */
1476 set_bit(BLOCK_NEEDS_FLUSH
, &cn
->state
);
1477 lock_buffer(saved_bh
);
1478 BUG_ON(cn
->blocknr
!= saved_bh
->b_blocknr
);
1479 if (buffer_dirty(saved_bh
))
1480 submit_logged_buffer(saved_bh
);
1482 unlock_buffer(saved_bh
);
1485 reiserfs_warning(s
, "clm-2082",
1486 "Unable to flush buffer %llu in %s",
1487 (unsigned long long)saved_bh
->
1488 b_blocknr
, __func__
);
1494 /* we incremented this to keep others from taking the buffer head away */
1496 if (atomic_read(&(saved_bh
->b_count
)) < 0) {
1497 reiserfs_warning(s
, "journal-945",
1498 "saved_bh->b_count < 0");
1503 cn
= jl
->j_realblock
;
1505 if (test_bit(BLOCK_NEEDS_FLUSH
, &cn
->state
)) {
1507 reiserfs_panic(s
, "journal-1011",
1511 reiserfs_write_unlock(s
);
1512 wait_on_buffer(cn
->bh
);
1513 reiserfs_write_lock(s
);
1516 reiserfs_panic(s
, "journal-1012",
1519 if (unlikely(!buffer_uptodate(cn
->bh
))) {
1520 #ifdef CONFIG_REISERFS_CHECK
1521 reiserfs_warning(s
, "journal-949",
1522 "buffer write failed");
1526 /* note, we must clear the JDirty_wait bit after the up to date
1527 ** check, otherwise we race against our flushpage routine
1529 BUG_ON(!test_clear_buffer_journal_dirty
1532 /* drop one ref for us */
1534 /* drop one ref for journal_mark_dirty */
1535 release_buffer_page(cn
->bh
);
1542 reiserfs_abort(s
, -EIO
,
1543 "Write error while pushing transaction to disk in %s",
1545 flush_older_and_return
:
1547 /* before we can update the journal header block, we _must_ flush all
1548 ** real blocks from all older transactions to disk. This is because
1549 ** once the header block is updated, this transaction will not be
1550 ** replayed after a crash
1553 flush_older_journal_lists(s
, jl
);
1556 err
= journal
->j_errno
;
1557 /* before we can remove everything from the hash tables for this
1558 ** transaction, we must make sure it can never be replayed
1560 ** since we are only called from do_journal_end, we know for sure there
1561 ** are no allocations going on while we are flushing journal lists. So,
1562 ** we only need to update the journal header block for the last list
1565 if (!err
&& flushall
) {
1567 update_journal_header_block(s
,
1568 (jl
->j_start
+ jl
->j_len
+
1569 2) % SB_ONDISK_JOURNAL_SIZE(s
),
1572 reiserfs_abort(s
, -EIO
,
1573 "Write error while updating journal header in %s",
1576 remove_all_from_journal_list(s
, jl
, 0);
1577 list_del_init(&jl
->j_list
);
1578 journal
->j_num_lists
--;
1579 del_from_work_list(s
, jl
);
1581 if (journal
->j_last_flush_id
!= 0 &&
1582 (jl
->j_trans_id
- journal
->j_last_flush_id
) != 1) {
1583 reiserfs_warning(s
, "clm-2201", "last flush %lu, current %lu",
1584 journal
->j_last_flush_id
, jl
->j_trans_id
);
1586 journal
->j_last_flush_id
= jl
->j_trans_id
;
1588 /* not strictly required since we are freeing the list, but it should
1589 * help find code using dead lists later on
1592 atomic_set(&(jl
->j_nonzerolen
), 0);
1594 jl
->j_realblock
= NULL
;
1595 jl
->j_commit_bh
= NULL
;
1598 put_journal_list(s
, jl
);
1600 mutex_unlock(&journal
->j_flush_mutex
);
1605 static int test_transaction(struct super_block
*s
,
1606 struct reiserfs_journal_list
*jl
)
1608 struct reiserfs_journal_cnode
*cn
;
1610 if (jl
->j_len
== 0 || atomic_read(&jl
->j_nonzerolen
) == 0)
1613 cn
= jl
->j_realblock
;
1615 /* if the blocknr == 0, this has been cleared from the hash,
1618 if (cn
->blocknr
== 0) {
1621 if (cn
->bh
&& !newer_jl_done(cn
))
1630 static int write_one_transaction(struct super_block
*s
,
1631 struct reiserfs_journal_list
*jl
,
1632 struct buffer_chunk
*chunk
)
1634 struct reiserfs_journal_cnode
*cn
;
1637 jl
->j_state
|= LIST_TOUCHED
;
1638 del_from_work_list(s
, jl
);
1639 if (jl
->j_len
== 0 || atomic_read(&jl
->j_nonzerolen
) == 0) {
1643 cn
= jl
->j_realblock
;
1645 /* if the blocknr == 0, this has been cleared from the hash,
1648 if (cn
->blocknr
== 0) {
1651 if (cn
->bh
&& can_dirty(cn
) && buffer_dirty(cn
->bh
)) {
1652 struct buffer_head
*tmp_bh
;
1653 /* we can race against journal_mark_freed when we try
1654 * to lock_buffer(cn->bh), so we have to inc the buffer
1655 * count, and recheck things after locking
1659 lock_buffer(tmp_bh
);
1660 if (cn
->bh
&& can_dirty(cn
) && buffer_dirty(tmp_bh
)) {
1661 if (!buffer_journal_dirty(tmp_bh
) ||
1662 buffer_journal_prepared(tmp_bh
))
1664 add_to_chunk(chunk
, tmp_bh
, NULL
, write_chunk
);
1667 /* note, cn->bh might be null now */
1668 unlock_buffer(tmp_bh
);
1679 /* used by flush_commit_list */
1680 static int dirty_one_transaction(struct super_block
*s
,
1681 struct reiserfs_journal_list
*jl
)
1683 struct reiserfs_journal_cnode
*cn
;
1684 struct reiserfs_journal_list
*pjl
;
1687 jl
->j_state
|= LIST_DIRTY
;
1688 cn
= jl
->j_realblock
;
1690 /* look for a more recent transaction that logged this
1691 ** buffer. Only the most recent transaction with a buffer in
1692 ** it is allowed to send that buffer to disk
1694 pjl
= find_newer_jl_for_cn(cn
);
1695 if (!pjl
&& cn
->blocknr
&& cn
->bh
1696 && buffer_journal_dirty(cn
->bh
)) {
1697 BUG_ON(!can_dirty(cn
));
1698 /* if the buffer is prepared, it will either be logged
1699 * or restored. If restored, we need to make sure
1700 * it actually gets marked dirty
1702 clear_buffer_journal_new(cn
->bh
);
1703 if (buffer_journal_prepared(cn
->bh
)) {
1704 set_buffer_journal_restore_dirty(cn
->bh
);
1706 set_buffer_journal_test(cn
->bh
);
1707 mark_buffer_dirty(cn
->bh
);
1715 static int kupdate_transactions(struct super_block
*s
,
1716 struct reiserfs_journal_list
*jl
,
1717 struct reiserfs_journal_list
**next_jl
,
1718 unsigned int *next_trans_id
,
1719 int num_blocks
, int num_trans
)
1723 int transactions_flushed
= 0;
1724 unsigned int orig_trans_id
= jl
->j_trans_id
;
1725 struct buffer_chunk chunk
;
1726 struct list_head
*entry
;
1727 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1730 reiserfs_mutex_lock_safe(&journal
->j_flush_mutex
, s
);
1731 if (!journal_list_still_alive(s
, orig_trans_id
)) {
1735 /* we've got j_flush_mutex held, nobody is going to delete any
1736 * of these lists out from underneath us
1738 while ((num_trans
&& transactions_flushed
< num_trans
) ||
1739 (!num_trans
&& written
< num_blocks
)) {
1741 if (jl
->j_len
== 0 || (jl
->j_state
& LIST_TOUCHED
) ||
1742 atomic_read(&jl
->j_commit_left
)
1743 || !(jl
->j_state
& LIST_DIRTY
)) {
1744 del_from_work_list(s
, jl
);
1747 ret
= write_one_transaction(s
, jl
, &chunk
);
1751 transactions_flushed
++;
1753 entry
= jl
->j_list
.next
;
1756 if (entry
== &journal
->j_journal_list
) {
1759 jl
= JOURNAL_LIST_ENTRY(entry
);
1761 /* don't bother with older transactions */
1762 if (jl
->j_trans_id
<= orig_trans_id
)
1766 write_chunk(&chunk
);
1770 mutex_unlock(&journal
->j_flush_mutex
);
1774 /* for o_sync and fsync heavy applications, they tend to use
1775 ** all the journa list slots with tiny transactions. These
1776 ** trigger lots and lots of calls to update the header block, which
1777 ** adds seeks and slows things down.
1779 ** This function tries to clear out a large chunk of the journal lists
1780 ** at once, which makes everything faster since only the newest journal
1781 ** list updates the header block
1783 static int flush_used_journal_lists(struct super_block
*s
,
1784 struct reiserfs_journal_list
*jl
)
1786 unsigned long len
= 0;
1787 unsigned long cur_len
;
1791 struct reiserfs_journal_list
*tjl
;
1792 struct reiserfs_journal_list
*flush_jl
;
1793 unsigned int trans_id
;
1794 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
1796 flush_jl
= tjl
= jl
;
1798 /* in data logging mode, try harder to flush a lot of blocks */
1799 if (reiserfs_data_log(s
))
1801 /* flush for 256 transactions or limit blocks, whichever comes first */
1802 for (i
= 0; i
< 256 && len
< limit
; i
++) {
1803 if (atomic_read(&tjl
->j_commit_left
) ||
1804 tjl
->j_trans_id
< jl
->j_trans_id
) {
1807 cur_len
= atomic_read(&tjl
->j_nonzerolen
);
1809 tjl
->j_state
&= ~LIST_TOUCHED
;
1813 if (tjl
->j_list
.next
== &journal
->j_journal_list
)
1815 tjl
= JOURNAL_LIST_ENTRY(tjl
->j_list
.next
);
1817 /* try to find a group of blocks we can flush across all the
1818 ** transactions, but only bother if we've actually spanned
1819 ** across multiple lists
1821 if (flush_jl
!= jl
) {
1822 ret
= kupdate_transactions(s
, jl
, &tjl
, &trans_id
, len
, i
);
1824 flush_journal_list(s
, flush_jl
, 1);
1829 ** removes any nodes in table with name block and dev as bh.
1830 ** only touchs the hnext and hprev pointers.
1832 void remove_journal_hash(struct super_block
*sb
,
1833 struct reiserfs_journal_cnode
**table
,
1834 struct reiserfs_journal_list
*jl
,
1835 unsigned long block
, int remove_freed
)
1837 struct reiserfs_journal_cnode
*cur
;
1838 struct reiserfs_journal_cnode
**head
;
1840 head
= &(journal_hash(table
, sb
, block
));
1846 if (cur
->blocknr
== block
&& cur
->sb
== sb
1847 && (jl
== NULL
|| jl
== cur
->jlist
)
1848 && (!test_bit(BLOCK_FREED
, &cur
->state
) || remove_freed
)) {
1850 cur
->hnext
->hprev
= cur
->hprev
;
1853 cur
->hprev
->hnext
= cur
->hnext
;
1860 if (cur
->bh
&& cur
->jlist
) /* anybody who clears the cur->bh will also dec the nonzerolen */
1861 atomic_dec(&(cur
->jlist
->j_nonzerolen
));
1869 static void free_journal_ram(struct super_block
*sb
)
1871 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
1872 kfree(journal
->j_current_jl
);
1873 journal
->j_num_lists
--;
1875 vfree(journal
->j_cnode_free_orig
);
1876 free_list_bitmaps(sb
, journal
->j_list_bitmap
);
1877 free_bitmap_nodes(sb
); /* must be after free_list_bitmaps */
1878 if (journal
->j_header_bh
) {
1879 brelse(journal
->j_header_bh
);
1881 /* j_header_bh is on the journal dev, make sure not to release the journal
1882 * dev until we brelse j_header_bh
1884 release_journal_dev(sb
, journal
);
1889 ** call on unmount. Only set error to 1 if you haven't made your way out
1890 ** of read_super() yet. Any other caller must keep error at 0.
1892 static int do_journal_release(struct reiserfs_transaction_handle
*th
,
1893 struct super_block
*sb
, int error
)
1895 struct reiserfs_transaction_handle myth
;
1897 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
1899 /* we only want to flush out transactions if we were called with error == 0
1901 if (!error
&& !(sb
->s_flags
& MS_RDONLY
)) {
1902 /* end the current trans */
1903 BUG_ON(!th
->t_trans_id
);
1904 do_journal_end(th
, sb
, 10, FLUSH_ALL
);
1906 /* make sure something gets logged to force our way into the flush code */
1907 if (!journal_join(&myth
, sb
, 1)) {
1908 reiserfs_prepare_for_journal(sb
,
1909 SB_BUFFER_WITH_SB(sb
),
1911 journal_mark_dirty(&myth
, sb
,
1912 SB_BUFFER_WITH_SB(sb
));
1913 do_journal_end(&myth
, sb
, 1, FLUSH_ALL
);
1918 /* this also catches errors during the do_journal_end above */
1919 if (!error
&& reiserfs_is_journal_aborted(journal
)) {
1920 memset(&myth
, 0, sizeof(myth
));
1921 if (!journal_join_abort(&myth
, sb
, 1)) {
1922 reiserfs_prepare_for_journal(sb
,
1923 SB_BUFFER_WITH_SB(sb
),
1925 journal_mark_dirty(&myth
, sb
,
1926 SB_BUFFER_WITH_SB(sb
));
1927 do_journal_end(&myth
, sb
, 1, FLUSH_ALL
);
1931 reiserfs_mounted_fs_count
--;
1932 /* wait for all commits to finish */
1933 cancel_delayed_work(&SB_JOURNAL(sb
)->j_work
);
1936 * We must release the write lock here because
1937 * the workqueue job (flush_async_commit) needs this lock
1939 reiserfs_write_unlock(sb
);
1940 flush_workqueue(commit_wq
);
1942 if (!reiserfs_mounted_fs_count
) {
1943 destroy_workqueue(commit_wq
);
1947 free_journal_ram(sb
);
1949 reiserfs_write_lock(sb
);
1955 ** call on unmount. flush all journal trans, release all alloc'd ram
1957 int journal_release(struct reiserfs_transaction_handle
*th
,
1958 struct super_block
*sb
)
1960 return do_journal_release(th
, sb
, 0);
1964 ** only call from an error condition inside reiserfs_read_super!
1966 int journal_release_error(struct reiserfs_transaction_handle
*th
,
1967 struct super_block
*sb
)
1969 return do_journal_release(th
, sb
, 1);
1972 /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
1973 static int journal_compare_desc_commit(struct super_block
*sb
,
1974 struct reiserfs_journal_desc
*desc
,
1975 struct reiserfs_journal_commit
*commit
)
1977 if (get_commit_trans_id(commit
) != get_desc_trans_id(desc
) ||
1978 get_commit_trans_len(commit
) != get_desc_trans_len(desc
) ||
1979 get_commit_trans_len(commit
) > SB_JOURNAL(sb
)->j_trans_max
||
1980 get_commit_trans_len(commit
) <= 0) {
1986 /* returns 0 if it did not find a description block
1987 ** returns -1 if it found a corrupt commit block
1988 ** returns 1 if both desc and commit were valid
1990 static int journal_transaction_is_valid(struct super_block
*sb
,
1991 struct buffer_head
*d_bh
,
1992 unsigned int *oldest_invalid_trans_id
,
1993 unsigned long *newest_mount_id
)
1995 struct reiserfs_journal_desc
*desc
;
1996 struct reiserfs_journal_commit
*commit
;
1997 struct buffer_head
*c_bh
;
1998 unsigned long offset
;
2003 desc
= (struct reiserfs_journal_desc
*)d_bh
->b_data
;
2004 if (get_desc_trans_len(desc
) > 0
2005 && !memcmp(get_journal_desc_magic(d_bh
), JOURNAL_DESC_MAGIC
, 8)) {
2006 if (oldest_invalid_trans_id
&& *oldest_invalid_trans_id
2007 && get_desc_trans_id(desc
) > *oldest_invalid_trans_id
) {
2008 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2009 "journal-986: transaction "
2010 "is valid returning because trans_id %d is greater than "
2011 "oldest_invalid %lu",
2012 get_desc_trans_id(desc
),
2013 *oldest_invalid_trans_id
);
2017 && *newest_mount_id
> get_desc_mount_id(desc
)) {
2018 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2019 "journal-1087: transaction "
2020 "is valid returning because mount_id %d is less than "
2021 "newest_mount_id %lu",
2022 get_desc_mount_id(desc
),
2026 if (get_desc_trans_len(desc
) > SB_JOURNAL(sb
)->j_trans_max
) {
2027 reiserfs_warning(sb
, "journal-2018",
2028 "Bad transaction length %d "
2029 "encountered, ignoring transaction",
2030 get_desc_trans_len(desc
));
2033 offset
= d_bh
->b_blocknr
- SB_ONDISK_JOURNAL_1st_BLOCK(sb
);
2035 /* ok, we have a journal description block, lets see if the transaction was valid */
2038 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2039 ((offset
+ get_desc_trans_len(desc
) +
2040 1) % SB_ONDISK_JOURNAL_SIZE(sb
)));
2043 commit
= (struct reiserfs_journal_commit
*)c_bh
->b_data
;
2044 if (journal_compare_desc_commit(sb
, desc
, commit
)) {
2045 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2046 "journal_transaction_is_valid, commit offset %ld had bad "
2047 "time %d or length %d",
2049 SB_ONDISK_JOURNAL_1st_BLOCK(sb
),
2050 get_commit_trans_id(commit
),
2051 get_commit_trans_len(commit
));
2053 if (oldest_invalid_trans_id
) {
2054 *oldest_invalid_trans_id
=
2055 get_desc_trans_id(desc
);
2056 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2058 "transaction_is_valid setting oldest invalid trans_id "
2060 get_desc_trans_id(desc
));
2065 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2066 "journal-1006: found valid "
2067 "transaction start offset %llu, len %d id %d",
2069 SB_ONDISK_JOURNAL_1st_BLOCK(sb
),
2070 get_desc_trans_len(desc
),
2071 get_desc_trans_id(desc
));
2078 static void brelse_array(struct buffer_head
**heads
, int num
)
2081 for (i
= 0; i
< num
; i
++) {
2087 ** given the start, and values for the oldest acceptable transactions,
2088 ** this either reads in a replays a transaction, or returns because the transaction
2089 ** is invalid, or too old.
2091 static int journal_read_transaction(struct super_block
*sb
,
2092 unsigned long cur_dblock
,
2093 unsigned long oldest_start
,
2094 unsigned int oldest_trans_id
,
2095 unsigned long newest_mount_id
)
2097 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
2098 struct reiserfs_journal_desc
*desc
;
2099 struct reiserfs_journal_commit
*commit
;
2100 unsigned int trans_id
= 0;
2101 struct buffer_head
*c_bh
;
2102 struct buffer_head
*d_bh
;
2103 struct buffer_head
**log_blocks
= NULL
;
2104 struct buffer_head
**real_blocks
= NULL
;
2105 unsigned int trans_offset
;
2109 d_bh
= journal_bread(sb
, cur_dblock
);
2112 desc
= (struct reiserfs_journal_desc
*)d_bh
->b_data
;
2113 trans_offset
= d_bh
->b_blocknr
- SB_ONDISK_JOURNAL_1st_BLOCK(sb
);
2114 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
, "journal-1037: "
2115 "journal_read_transaction, offset %llu, len %d mount_id %d",
2116 d_bh
->b_blocknr
- SB_ONDISK_JOURNAL_1st_BLOCK(sb
),
2117 get_desc_trans_len(desc
), get_desc_mount_id(desc
));
2118 if (get_desc_trans_id(desc
) < oldest_trans_id
) {
2119 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
, "journal-1039: "
2120 "journal_read_trans skipping because %lu is too old",
2122 SB_ONDISK_JOURNAL_1st_BLOCK(sb
));
2126 if (get_desc_mount_id(desc
) != newest_mount_id
) {
2127 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
, "journal-1146: "
2128 "journal_read_trans skipping because %d is != "
2129 "newest_mount_id %lu", get_desc_mount_id(desc
),
2134 c_bh
= journal_bread(sb
, SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2135 ((trans_offset
+ get_desc_trans_len(desc
) + 1) %
2136 SB_ONDISK_JOURNAL_SIZE(sb
)));
2141 commit
= (struct reiserfs_journal_commit
*)c_bh
->b_data
;
2142 if (journal_compare_desc_commit(sb
, desc
, commit
)) {
2143 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2144 "journal_read_transaction, "
2145 "commit offset %llu had bad time %d or length %d",
2147 SB_ONDISK_JOURNAL_1st_BLOCK(sb
),
2148 get_commit_trans_id(commit
),
2149 get_commit_trans_len(commit
));
2155 if (bdev_read_only(sb
->s_bdev
)) {
2156 reiserfs_warning(sb
, "clm-2076",
2157 "device is readonly, unable to replay log");
2163 trans_id
= get_desc_trans_id(desc
);
2164 /* now we know we've got a good transaction, and it was inside the valid time ranges */
2165 log_blocks
= kmalloc(get_desc_trans_len(desc
) *
2166 sizeof(struct buffer_head
*), GFP_NOFS
);
2167 real_blocks
= kmalloc(get_desc_trans_len(desc
) *
2168 sizeof(struct buffer_head
*), GFP_NOFS
);
2169 if (!log_blocks
|| !real_blocks
) {
2174 reiserfs_warning(sb
, "journal-1169",
2175 "kmalloc failed, unable to mount FS");
2178 /* get all the buffer heads */
2179 trans_half
= journal_trans_half(sb
->s_blocksize
);
2180 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2183 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2185 i
) % SB_ONDISK_JOURNAL_SIZE(sb
));
2186 if (i
< trans_half
) {
2189 le32_to_cpu(desc
->j_realblock
[i
]));
2193 le32_to_cpu(commit
->
2194 j_realblock
[i
- trans_half
]));
2196 if (real_blocks
[i
]->b_blocknr
> SB_BLOCK_COUNT(sb
)) {
2197 reiserfs_warning(sb
, "journal-1207",
2198 "REPLAY FAILURE fsck required! "
2199 "Block to replay is outside of "
2203 /* make sure we don't try to replay onto log or reserved area */
2204 if (is_block_in_log_or_reserved_area
2205 (sb
, real_blocks
[i
]->b_blocknr
)) {
2206 reiserfs_warning(sb
, "journal-1204",
2207 "REPLAY FAILURE fsck required! "
2208 "Trying to replay onto a log block");
2210 brelse_array(log_blocks
, i
);
2211 brelse_array(real_blocks
, i
);
2219 /* read in the log blocks, memcpy to the corresponding real block */
2220 ll_rw_block(READ
, get_desc_trans_len(desc
), log_blocks
);
2221 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2223 reiserfs_write_unlock(sb
);
2224 wait_on_buffer(log_blocks
[i
]);
2225 reiserfs_write_lock(sb
);
2227 if (!buffer_uptodate(log_blocks
[i
])) {
2228 reiserfs_warning(sb
, "journal-1212",
2229 "REPLAY FAILURE fsck required! "
2230 "buffer write failed");
2231 brelse_array(log_blocks
+ i
,
2232 get_desc_trans_len(desc
) - i
);
2233 brelse_array(real_blocks
, get_desc_trans_len(desc
));
2240 memcpy(real_blocks
[i
]->b_data
, log_blocks
[i
]->b_data
,
2241 real_blocks
[i
]->b_size
);
2242 set_buffer_uptodate(real_blocks
[i
]);
2243 brelse(log_blocks
[i
]);
2245 /* flush out the real blocks */
2246 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2247 set_buffer_dirty(real_blocks
[i
]);
2248 write_dirty_buffer(real_blocks
[i
], WRITE
);
2250 for (i
= 0; i
< get_desc_trans_len(desc
); i
++) {
2251 wait_on_buffer(real_blocks
[i
]);
2252 if (!buffer_uptodate(real_blocks
[i
])) {
2253 reiserfs_warning(sb
, "journal-1226",
2254 "REPLAY FAILURE, fsck required! "
2255 "buffer write failed");
2256 brelse_array(real_blocks
+ i
,
2257 get_desc_trans_len(desc
) - i
);
2264 brelse(real_blocks
[i
]);
2267 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2268 ((trans_offset
+ get_desc_trans_len(desc
) +
2269 2) % SB_ONDISK_JOURNAL_SIZE(sb
));
2270 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2271 "journal-1095: setting journal " "start to offset %ld",
2272 cur_dblock
- SB_ONDISK_JOURNAL_1st_BLOCK(sb
));
2274 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
2275 journal
->j_start
= cur_dblock
- SB_ONDISK_JOURNAL_1st_BLOCK(sb
);
2276 journal
->j_last_flush_trans_id
= trans_id
;
2277 journal
->j_trans_id
= trans_id
+ 1;
2278 /* check for trans_id overflow */
2279 if (journal
->j_trans_id
== 0)
2280 journal
->j_trans_id
= 10;
2288 /* This function reads blocks starting from block and to max_block of bufsize
2289 size (but no more than BUFNR blocks at a time). This proved to improve
2290 mounting speed on self-rebuilding raid5 arrays at least.
2291 Right now it is only used from journal code. But later we might use it
2293 Note: Do not use journal_getblk/sb_getblk functions here! */
2294 static struct buffer_head
*reiserfs_breada(struct block_device
*dev
,
2295 b_blocknr_t block
, int bufsize
,
2296 b_blocknr_t max_block
)
2298 struct buffer_head
*bhlist
[BUFNR
];
2299 unsigned int blocks
= BUFNR
;
2300 struct buffer_head
*bh
;
2303 bh
= __getblk(dev
, block
, bufsize
);
2304 if (buffer_uptodate(bh
))
2307 if (block
+ BUFNR
> max_block
) {
2308 blocks
= max_block
- block
;
2312 for (i
= 1; i
< blocks
; i
++) {
2313 bh
= __getblk(dev
, block
+ i
, bufsize
);
2314 if (buffer_uptodate(bh
)) {
2320 ll_rw_block(READ
, j
, bhlist
);
2321 for (i
= 1; i
< j
; i
++)
2325 if (buffer_uptodate(bh
))
2332 ** read and replay the log
2333 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2334 ** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
2336 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2338 ** On exit, it sets things up so the first transaction will work correctly.
2340 static int journal_read(struct super_block
*sb
)
2342 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
2343 struct reiserfs_journal_desc
*desc
;
2344 unsigned int oldest_trans_id
= 0;
2345 unsigned int oldest_invalid_trans_id
= 0;
2347 unsigned long oldest_start
= 0;
2348 unsigned long cur_dblock
= 0;
2349 unsigned long newest_mount_id
= 9;
2350 struct buffer_head
*d_bh
;
2351 struct reiserfs_journal_header
*jh
;
2352 int valid_journal_header
= 0;
2353 int replay_count
= 0;
2354 int continue_replay
= 1;
2356 char b
[BDEVNAME_SIZE
];
2358 cur_dblock
= SB_ONDISK_JOURNAL_1st_BLOCK(sb
);
2359 reiserfs_info(sb
, "checking transaction log (%s)\n",
2360 bdevname(journal
->j_dev_bd
, b
));
2361 start
= get_seconds();
2363 /* step 1, read in the journal header block. Check the transaction it says
2364 ** is the first unflushed, and if that transaction is not valid,
2367 journal
->j_header_bh
= journal_bread(sb
,
2368 SB_ONDISK_JOURNAL_1st_BLOCK(sb
)
2369 + SB_ONDISK_JOURNAL_SIZE(sb
));
2370 if (!journal
->j_header_bh
) {
2373 jh
= (struct reiserfs_journal_header
*)(journal
->j_header_bh
->b_data
);
2374 if (le32_to_cpu(jh
->j_first_unflushed_offset
) <
2375 SB_ONDISK_JOURNAL_SIZE(sb
)
2376 && le32_to_cpu(jh
->j_last_flush_trans_id
) > 0) {
2378 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2379 le32_to_cpu(jh
->j_first_unflushed_offset
);
2380 oldest_trans_id
= le32_to_cpu(jh
->j_last_flush_trans_id
) + 1;
2381 newest_mount_id
= le32_to_cpu(jh
->j_mount_id
);
2382 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2383 "journal-1153: found in "
2384 "header: first_unflushed_offset %d, last_flushed_trans_id "
2385 "%lu", le32_to_cpu(jh
->j_first_unflushed_offset
),
2386 le32_to_cpu(jh
->j_last_flush_trans_id
));
2387 valid_journal_header
= 1;
2389 /* now, we try to read the first unflushed offset. If it is not valid,
2390 ** there is nothing more we can do, and it makes no sense to read
2391 ** through the whole log.
2395 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2396 le32_to_cpu(jh
->j_first_unflushed_offset
));
2397 ret
= journal_transaction_is_valid(sb
, d_bh
, NULL
, NULL
);
2399 continue_replay
= 0;
2402 goto start_log_replay
;
2405 /* ok, there are transactions that need to be replayed. start with the first log block, find
2406 ** all the valid transactions, and pick out the oldest.
2408 while (continue_replay
2410 (SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2411 SB_ONDISK_JOURNAL_SIZE(sb
))) {
2412 /* Note that it is required for blocksize of primary fs device and journal
2413 device to be the same */
2415 reiserfs_breada(journal
->j_dev_bd
, cur_dblock
,
2417 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2418 SB_ONDISK_JOURNAL_SIZE(sb
));
2420 journal_transaction_is_valid(sb
, d_bh
,
2421 &oldest_invalid_trans_id
,
2424 desc
= (struct reiserfs_journal_desc
*)d_bh
->b_data
;
2425 if (oldest_start
== 0) { /* init all oldest_ values */
2426 oldest_trans_id
= get_desc_trans_id(desc
);
2427 oldest_start
= d_bh
->b_blocknr
;
2428 newest_mount_id
= get_desc_mount_id(desc
);
2429 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2430 "journal-1179: Setting "
2431 "oldest_start to offset %llu, trans_id %lu",
2433 SB_ONDISK_JOURNAL_1st_BLOCK
2434 (sb
), oldest_trans_id
);
2435 } else if (oldest_trans_id
> get_desc_trans_id(desc
)) {
2436 /* one we just read was older */
2437 oldest_trans_id
= get_desc_trans_id(desc
);
2438 oldest_start
= d_bh
->b_blocknr
;
2439 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2440 "journal-1180: Resetting "
2441 "oldest_start to offset %lu, trans_id %lu",
2443 SB_ONDISK_JOURNAL_1st_BLOCK
2444 (sb
), oldest_trans_id
);
2446 if (newest_mount_id
< get_desc_mount_id(desc
)) {
2447 newest_mount_id
= get_desc_mount_id(desc
);
2448 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2449 "journal-1299: Setting "
2450 "newest_mount_id to %d",
2451 get_desc_mount_id(desc
));
2453 cur_dblock
+= get_desc_trans_len(desc
) + 2;
2461 cur_dblock
= oldest_start
;
2462 if (oldest_trans_id
) {
2463 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2464 "journal-1206: Starting replay "
2465 "from offset %llu, trans_id %lu",
2466 cur_dblock
- SB_ONDISK_JOURNAL_1st_BLOCK(sb
),
2471 while (continue_replay
&& oldest_trans_id
> 0) {
2473 journal_read_transaction(sb
, cur_dblock
, oldest_start
,
2474 oldest_trans_id
, newest_mount_id
);
2477 } else if (ret
!= 0) {
2481 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) + journal
->j_start
;
2483 if (cur_dblock
== oldest_start
)
2487 if (oldest_trans_id
== 0) {
2488 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
,
2489 "journal-1225: No valid " "transactions found");
2491 /* j_start does not get set correctly if we don't replay any transactions.
2492 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2493 ** copy the trans_id from the header
2495 if (valid_journal_header
&& replay_count
== 0) {
2496 journal
->j_start
= le32_to_cpu(jh
->j_first_unflushed_offset
);
2497 journal
->j_trans_id
=
2498 le32_to_cpu(jh
->j_last_flush_trans_id
) + 1;
2499 /* check for trans_id overflow */
2500 if (journal
->j_trans_id
== 0)
2501 journal
->j_trans_id
= 10;
2502 journal
->j_last_flush_trans_id
=
2503 le32_to_cpu(jh
->j_last_flush_trans_id
);
2504 journal
->j_mount_id
= le32_to_cpu(jh
->j_mount_id
) + 1;
2506 journal
->j_mount_id
= newest_mount_id
+ 1;
2508 reiserfs_debug(sb
, REISERFS_DEBUG_CODE
, "journal-1299: Setting "
2509 "newest_mount_id to %lu", journal
->j_mount_id
);
2510 journal
->j_first_unflushed_offset
= journal
->j_start
;
2511 if (replay_count
> 0) {
2513 "replayed %d transactions in %lu seconds\n",
2514 replay_count
, get_seconds() - start
);
2516 if (!bdev_read_only(sb
->s_bdev
) &&
2517 _update_journal_header_block(sb
, journal
->j_start
,
2518 journal
->j_last_flush_trans_id
)) {
2519 /* replay failed, caller must call free_journal_ram and abort
2527 static struct reiserfs_journal_list
*alloc_journal_list(struct super_block
*s
)
2529 struct reiserfs_journal_list
*jl
;
2530 jl
= kzalloc(sizeof(struct reiserfs_journal_list
),
2531 GFP_NOFS
| __GFP_NOFAIL
);
2532 INIT_LIST_HEAD(&jl
->j_list
);
2533 INIT_LIST_HEAD(&jl
->j_working_list
);
2534 INIT_LIST_HEAD(&jl
->j_tail_bh_list
);
2535 INIT_LIST_HEAD(&jl
->j_bh_list
);
2536 mutex_init(&jl
->j_commit_mutex
);
2537 SB_JOURNAL(s
)->j_num_lists
++;
2538 get_journal_list(jl
);
2542 static void journal_list_init(struct super_block
*sb
)
2544 SB_JOURNAL(sb
)->j_current_jl
= alloc_journal_list(sb
);
2547 static int release_journal_dev(struct super_block
*super
,
2548 struct reiserfs_journal
*journal
)
2554 if (journal
->j_dev_bd
!= NULL
) {
2555 result
= blkdev_put(journal
->j_dev_bd
, journal
->j_dev_mode
);
2556 journal
->j_dev_bd
= NULL
;
2560 reiserfs_warning(super
, "sh-457",
2561 "Cannot release journal device: %i", result
);
2566 static int journal_init_dev(struct super_block
*super
,
2567 struct reiserfs_journal
*journal
,
2568 const char *jdev_name
)
2572 fmode_t blkdev_mode
= FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
;
2573 char b
[BDEVNAME_SIZE
];
2577 journal
->j_dev_bd
= NULL
;
2578 jdev
= SB_ONDISK_JOURNAL_DEVICE(super
) ?
2579 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super
)) : super
->s_dev
;
2581 if (bdev_read_only(super
->s_bdev
))
2582 blkdev_mode
= FMODE_READ
;
2584 /* there is no "jdev" option and journal is on separate device */
2585 if ((!jdev_name
|| !jdev_name
[0])) {
2586 if (jdev
== super
->s_dev
)
2587 blkdev_mode
&= ~FMODE_EXCL
;
2588 journal
->j_dev_bd
= blkdev_get_by_dev(jdev
, blkdev_mode
,
2590 journal
->j_dev_mode
= blkdev_mode
;
2591 if (IS_ERR(journal
->j_dev_bd
)) {
2592 result
= PTR_ERR(journal
->j_dev_bd
);
2593 journal
->j_dev_bd
= NULL
;
2594 reiserfs_warning(super
, "sh-458",
2595 "cannot init journal device '%s': %i",
2596 __bdevname(jdev
, b
), result
);
2598 } else if (jdev
!= super
->s_dev
)
2599 set_blocksize(journal
->j_dev_bd
, super
->s_blocksize
);
2604 journal
->j_dev_mode
= blkdev_mode
;
2605 journal
->j_dev_bd
= blkdev_get_by_path(jdev_name
, blkdev_mode
, journal
);
2606 if (IS_ERR(journal
->j_dev_bd
)) {
2607 result
= PTR_ERR(journal
->j_dev_bd
);
2608 journal
->j_dev_bd
= NULL
;
2609 reiserfs_warning(super
,
2610 "journal_init_dev: Cannot open '%s': %i",
2615 set_blocksize(journal
->j_dev_bd
, super
->s_blocksize
);
2616 reiserfs_info(super
,
2617 "journal_init_dev: journal device: %s\n",
2618 bdevname(journal
->j_dev_bd
, b
));
2623 * When creating/tuning a file system user can assign some
2624 * journal params within boundaries which depend on the ratio
2625 * blocksize/standard_blocksize.
2627 * For blocks >= standard_blocksize transaction size should
2628 * be not less then JOURNAL_TRANS_MIN_DEFAULT, and not more
2629 * then JOURNAL_TRANS_MAX_DEFAULT.
2631 * For blocks < standard_blocksize these boundaries should be
2632 * decreased proportionally.
2634 #define REISERFS_STANDARD_BLKSIZE (4096)
2636 static int check_advise_trans_params(struct super_block
*sb
,
2637 struct reiserfs_journal
*journal
)
2639 if (journal
->j_trans_max
) {
2640 /* Non-default journal params.
2641 Do sanity check for them. */
2643 if (sb
->s_blocksize
< REISERFS_STANDARD_BLKSIZE
)
2644 ratio
= REISERFS_STANDARD_BLKSIZE
/ sb
->s_blocksize
;
2646 if (journal
->j_trans_max
> JOURNAL_TRANS_MAX_DEFAULT
/ ratio
||
2647 journal
->j_trans_max
< JOURNAL_TRANS_MIN_DEFAULT
/ ratio
||
2648 SB_ONDISK_JOURNAL_SIZE(sb
) / journal
->j_trans_max
<
2649 JOURNAL_MIN_RATIO
) {
2650 reiserfs_warning(sb
, "sh-462",
2651 "bad transaction max size (%u). "
2652 "FSCK?", journal
->j_trans_max
);
2655 if (journal
->j_max_batch
!= (journal
->j_trans_max
) *
2656 JOURNAL_MAX_BATCH_DEFAULT
/JOURNAL_TRANS_MAX_DEFAULT
) {
2657 reiserfs_warning(sb
, "sh-463",
2658 "bad transaction max batch (%u). "
2659 "FSCK?", journal
->j_max_batch
);
2663 /* Default journal params.
2664 The file system was created by old version
2665 of mkreiserfs, so some fields contain zeros,
2666 and we need to advise proper values for them */
2667 if (sb
->s_blocksize
!= REISERFS_STANDARD_BLKSIZE
) {
2668 reiserfs_warning(sb
, "sh-464", "bad blocksize (%u)",
2672 journal
->j_trans_max
= JOURNAL_TRANS_MAX_DEFAULT
;
2673 journal
->j_max_batch
= JOURNAL_MAX_BATCH_DEFAULT
;
2674 journal
->j_max_commit_age
= JOURNAL_MAX_COMMIT_AGE
;
2680 ** must be called once on fs mount. calls journal_read for you
2682 int journal_init(struct super_block
*sb
, const char *j_dev_name
,
2683 int old_format
, unsigned int commit_max_age
)
2685 int num_cnodes
= SB_ONDISK_JOURNAL_SIZE(sb
) * 2;
2686 struct buffer_head
*bhjh
;
2687 struct reiserfs_super_block
*rs
;
2688 struct reiserfs_journal_header
*jh
;
2689 struct reiserfs_journal
*journal
;
2690 struct reiserfs_journal_list
*jl
;
2691 char b
[BDEVNAME_SIZE
];
2695 * Unlock here to avoid various RECLAIM-FS-ON <-> IN-RECLAIM-FS
2696 * dependency inversion warnings.
2698 reiserfs_write_unlock(sb
);
2699 journal
= SB_JOURNAL(sb
) = vmalloc(sizeof(struct reiserfs_journal
));
2701 reiserfs_warning(sb
, "journal-1256",
2702 "unable to get memory for journal structure");
2703 reiserfs_write_lock(sb
);
2706 memset(journal
, 0, sizeof(struct reiserfs_journal
));
2707 INIT_LIST_HEAD(&journal
->j_bitmap_nodes
);
2708 INIT_LIST_HEAD(&journal
->j_prealloc_list
);
2709 INIT_LIST_HEAD(&journal
->j_working_list
);
2710 INIT_LIST_HEAD(&journal
->j_journal_list
);
2711 journal
->j_persistent_trans
= 0;
2712 ret
= reiserfs_allocate_list_bitmaps(sb
, journal
->j_list_bitmap
,
2713 reiserfs_bmap_count(sb
));
2714 reiserfs_write_lock(sb
);
2716 goto free_and_return
;
2718 allocate_bitmap_nodes(sb
);
2720 /* reserved for journal area support */
2721 SB_JOURNAL_1st_RESERVED_BLOCK(sb
) = (old_format
?
2722 REISERFS_OLD_DISK_OFFSET_IN_BYTES
2724 reiserfs_bmap_count(sb
) +
2726 REISERFS_DISK_OFFSET_IN_BYTES
/
2727 sb
->s_blocksize
+ 2);
2729 /* Sanity check to see is the standard journal fitting withing first bitmap
2730 (actual for small blocksizes) */
2731 if (!SB_ONDISK_JOURNAL_DEVICE(sb
) &&
2732 (SB_JOURNAL_1st_RESERVED_BLOCK(sb
) +
2733 SB_ONDISK_JOURNAL_SIZE(sb
) > sb
->s_blocksize
* 8)) {
2734 reiserfs_warning(sb
, "journal-1393",
2735 "journal does not fit for area addressed "
2736 "by first of bitmap blocks. It starts at "
2737 "%u and its size is %u. Block size %ld",
2738 SB_JOURNAL_1st_RESERVED_BLOCK(sb
),
2739 SB_ONDISK_JOURNAL_SIZE(sb
),
2741 goto free_and_return
;
2745 * We need to unlock here to avoid creating the following
2747 * reiserfs_lock -> sysfs_mutex
2748 * Because the reiserfs mmap path creates the following dependency:
2749 * mm->mmap -> reiserfs_lock, hence we have
2750 * mm->mmap -> reiserfs_lock ->sysfs_mutex
2751 * This would ends up in a circular dependency with sysfs readdir path
2752 * which does sysfs_mutex -> mm->mmap_sem
2753 * This is fine because the reiserfs lock is useless in mount path,
2754 * at least until we call journal_begin. We keep it for paranoid
2757 reiserfs_write_unlock(sb
);
2758 if (journal_init_dev(sb
, journal
, j_dev_name
) != 0) {
2759 reiserfs_write_lock(sb
);
2760 reiserfs_warning(sb
, "sh-462",
2761 "unable to initialize jornal device");
2762 goto free_and_return
;
2764 reiserfs_write_lock(sb
);
2766 rs
= SB_DISK_SUPER_BLOCK(sb
);
2768 /* read journal header */
2769 bhjh
= journal_bread(sb
,
2770 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
2771 SB_ONDISK_JOURNAL_SIZE(sb
));
2773 reiserfs_warning(sb
, "sh-459",
2774 "unable to read journal header");
2775 goto free_and_return
;
2777 jh
= (struct reiserfs_journal_header
*)(bhjh
->b_data
);
2779 /* make sure that journal matches to the super block */
2780 if (is_reiserfs_jr(rs
)
2781 && (le32_to_cpu(jh
->jh_journal
.jp_journal_magic
) !=
2782 sb_jp_journal_magic(rs
))) {
2783 reiserfs_warning(sb
, "sh-460",
2784 "journal header magic %x (device %s) does "
2785 "not match to magic found in super block %x",
2786 jh
->jh_journal
.jp_journal_magic
,
2787 bdevname(journal
->j_dev_bd
, b
),
2788 sb_jp_journal_magic(rs
));
2790 goto free_and_return
;
2793 journal
->j_trans_max
= le32_to_cpu(jh
->jh_journal
.jp_journal_trans_max
);
2794 journal
->j_max_batch
= le32_to_cpu(jh
->jh_journal
.jp_journal_max_batch
);
2795 journal
->j_max_commit_age
=
2796 le32_to_cpu(jh
->jh_journal
.jp_journal_max_commit_age
);
2797 journal
->j_max_trans_age
= JOURNAL_MAX_TRANS_AGE
;
2799 if (check_advise_trans_params(sb
, journal
) != 0)
2800 goto free_and_return
;
2801 journal
->j_default_max_commit_age
= journal
->j_max_commit_age
;
2803 if (commit_max_age
!= 0) {
2804 journal
->j_max_commit_age
= commit_max_age
;
2805 journal
->j_max_trans_age
= commit_max_age
;
2808 reiserfs_info(sb
, "journal params: device %s, size %u, "
2809 "journal first block %u, max trans len %u, max batch %u, "
2810 "max commit age %u, max trans age %u\n",
2811 bdevname(journal
->j_dev_bd
, b
),
2812 SB_ONDISK_JOURNAL_SIZE(sb
),
2813 SB_ONDISK_JOURNAL_1st_BLOCK(sb
),
2814 journal
->j_trans_max
,
2815 journal
->j_max_batch
,
2816 journal
->j_max_commit_age
, journal
->j_max_trans_age
);
2820 journal
->j_list_bitmap_index
= 0;
2821 journal_list_init(sb
);
2823 memset(journal
->j_list_hash_table
, 0,
2824 JOURNAL_HASH_SIZE
* sizeof(struct reiserfs_journal_cnode
*));
2826 INIT_LIST_HEAD(&journal
->j_dirty_buffers
);
2827 spin_lock_init(&journal
->j_dirty_buffers_lock
);
2829 journal
->j_start
= 0;
2831 journal
->j_len_alloc
= 0;
2832 atomic_set(&(journal
->j_wcount
), 0);
2833 atomic_set(&(journal
->j_async_throttle
), 0);
2834 journal
->j_bcount
= 0;
2835 journal
->j_trans_start_time
= 0;
2836 journal
->j_last
= NULL
;
2837 journal
->j_first
= NULL
;
2838 init_waitqueue_head(&(journal
->j_join_wait
));
2839 mutex_init(&journal
->j_mutex
);
2840 mutex_init(&journal
->j_flush_mutex
);
2842 journal
->j_trans_id
= 10;
2843 journal
->j_mount_id
= 10;
2844 journal
->j_state
= 0;
2845 atomic_set(&(journal
->j_jlock
), 0);
2846 reiserfs_write_unlock(sb
);
2847 journal
->j_cnode_free_list
= allocate_cnodes(num_cnodes
);
2848 reiserfs_write_lock(sb
);
2849 journal
->j_cnode_free_orig
= journal
->j_cnode_free_list
;
2850 journal
->j_cnode_free
= journal
->j_cnode_free_list
? num_cnodes
: 0;
2851 journal
->j_cnode_used
= 0;
2852 journal
->j_must_wait
= 0;
2854 if (journal
->j_cnode_free
== 0) {
2855 reiserfs_warning(sb
, "journal-2004", "Journal cnode memory "
2856 "allocation failed (%ld bytes). Journal is "
2857 "too large for available memory. Usually "
2858 "this is due to a journal that is too large.",
2859 sizeof (struct reiserfs_journal_cnode
) * num_cnodes
);
2860 goto free_and_return
;
2863 init_journal_hash(sb
);
2864 jl
= journal
->j_current_jl
;
2865 jl
->j_list_bitmap
= get_list_bitmap(sb
, jl
);
2866 if (!jl
->j_list_bitmap
) {
2867 reiserfs_warning(sb
, "journal-2005",
2868 "get_list_bitmap failed for journal list 0");
2869 goto free_and_return
;
2871 if (journal_read(sb
) < 0) {
2872 reiserfs_warning(sb
, "reiserfs-2006",
2873 "Replay Failure, unable to mount");
2874 goto free_and_return
;
2877 reiserfs_mounted_fs_count
++;
2878 if (reiserfs_mounted_fs_count
<= 1) {
2879 reiserfs_write_unlock(sb
);
2880 commit_wq
= create_workqueue("reiserfs");
2881 reiserfs_write_lock(sb
);
2884 INIT_DELAYED_WORK(&journal
->j_work
, flush_async_commits
);
2885 journal
->j_work_sb
= sb
;
2888 free_journal_ram(sb
);
2893 ** test for a polite end of the current transaction. Used by file_write, and should
2894 ** be used by delete to make sure they don't write more than can fit inside a single
2897 int journal_transaction_should_end(struct reiserfs_transaction_handle
*th
,
2900 struct reiserfs_journal
*journal
= SB_JOURNAL(th
->t_super
);
2901 time_t now
= get_seconds();
2902 /* cannot restart while nested */
2903 BUG_ON(!th
->t_trans_id
);
2904 if (th
->t_refcount
> 1)
2906 if (journal
->j_must_wait
> 0 ||
2907 (journal
->j_len_alloc
+ new_alloc
) >= journal
->j_max_batch
||
2908 atomic_read(&(journal
->j_jlock
)) ||
2909 (now
- journal
->j_trans_start_time
) > journal
->j_max_trans_age
||
2910 journal
->j_cnode_free
< (journal
->j_trans_max
* 3)) {
2913 /* protected by the BKL here */
2914 journal
->j_len_alloc
+= new_alloc
;
2915 th
->t_blocks_allocated
+= new_alloc
;
2919 /* this must be called inside a transaction, and requires the
2920 ** kernel_lock to be held
2922 void reiserfs_block_writes(struct reiserfs_transaction_handle
*th
)
2924 struct reiserfs_journal
*journal
= SB_JOURNAL(th
->t_super
);
2925 BUG_ON(!th
->t_trans_id
);
2926 journal
->j_must_wait
= 1;
2927 set_bit(J_WRITERS_BLOCKED
, &journal
->j_state
);
2931 /* this must be called without a transaction started, and does not
2934 void reiserfs_allow_writes(struct super_block
*s
)
2936 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2937 clear_bit(J_WRITERS_BLOCKED
, &journal
->j_state
);
2938 wake_up(&journal
->j_join_wait
);
2941 /* this must be called without a transaction started, and does not
2944 void reiserfs_wait_on_write_block(struct super_block
*s
)
2946 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2947 wait_event(journal
->j_join_wait
,
2948 !test_bit(J_WRITERS_BLOCKED
, &journal
->j_state
));
2951 static void queue_log_writer(struct super_block
*s
)
2954 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2955 set_bit(J_WRITERS_QUEUED
, &journal
->j_state
);
2958 * we don't want to use wait_event here because
2959 * we only want to wait once.
2961 init_waitqueue_entry(&wait
, current
);
2962 add_wait_queue(&journal
->j_join_wait
, &wait
);
2963 set_current_state(TASK_UNINTERRUPTIBLE
);
2964 if (test_bit(J_WRITERS_QUEUED
, &journal
->j_state
)) {
2965 reiserfs_write_unlock(s
);
2967 reiserfs_write_lock(s
);
2969 __set_current_state(TASK_RUNNING
);
2970 remove_wait_queue(&journal
->j_join_wait
, &wait
);
2973 static void wake_queued_writers(struct super_block
*s
)
2975 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
2976 if (test_and_clear_bit(J_WRITERS_QUEUED
, &journal
->j_state
))
2977 wake_up(&journal
->j_join_wait
);
2980 static void let_transaction_grow(struct super_block
*sb
, unsigned int trans_id
)
2982 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
2983 unsigned long bcount
= journal
->j_bcount
;
2985 reiserfs_write_unlock(sb
);
2986 schedule_timeout_uninterruptible(1);
2987 reiserfs_write_lock(sb
);
2988 journal
->j_current_jl
->j_state
|= LIST_COMMIT_PENDING
;
2989 while ((atomic_read(&journal
->j_wcount
) > 0 ||
2990 atomic_read(&journal
->j_jlock
)) &&
2991 journal
->j_trans_id
== trans_id
) {
2992 queue_log_writer(sb
);
2994 if (journal
->j_trans_id
!= trans_id
)
2996 if (bcount
== journal
->j_bcount
)
2998 bcount
= journal
->j_bcount
;
3002 /* join == true if you must join an existing transaction.
3003 ** join == false if you can deal with waiting for others to finish
3005 ** this will block until the transaction is joinable. send the number of blocks you
3006 ** expect to use in nblocks.
3008 static int do_journal_begin_r(struct reiserfs_transaction_handle
*th
,
3009 struct super_block
*sb
, unsigned long nblocks
,
3012 time_t now
= get_seconds();
3013 unsigned int old_trans_id
;
3014 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3015 struct reiserfs_transaction_handle myth
;
3016 int sched_count
= 0;
3019 reiserfs_check_lock_depth(sb
, "journal_begin");
3020 BUG_ON(nblocks
> journal
->j_trans_max
);
3022 PROC_INFO_INC(sb
, journal
.journal_being
);
3023 /* set here for journal_join */
3029 if (join
!= JBEGIN_ABORT
&& reiserfs_is_journal_aborted(journal
)) {
3031 retval
= journal
->j_errno
;
3034 journal
->j_bcount
++;
3036 if (test_bit(J_WRITERS_BLOCKED
, &journal
->j_state
)) {
3038 reiserfs_write_unlock(sb
);
3039 reiserfs_wait_on_write_block(sb
);
3040 reiserfs_write_lock(sb
);
3041 PROC_INFO_INC(sb
, journal
.journal_relock_writers
);
3044 now
= get_seconds();
3046 /* if there is no room in the journal OR
3047 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
3048 ** we don't sleep if there aren't other writers
3051 if ((!join
&& journal
->j_must_wait
> 0) ||
3053 && (journal
->j_len_alloc
+ nblocks
+ 2) >= journal
->j_max_batch
)
3054 || (!join
&& atomic_read(&journal
->j_wcount
) > 0
3055 && journal
->j_trans_start_time
> 0
3056 && (now
- journal
->j_trans_start_time
) >
3057 journal
->j_max_trans_age
) || (!join
3058 && atomic_read(&journal
->j_jlock
))
3059 || (!join
&& journal
->j_cnode_free
< (journal
->j_trans_max
* 3))) {
3061 old_trans_id
= journal
->j_trans_id
;
3062 unlock_journal(sb
); /* allow others to finish this transaction */
3064 if (!join
&& (journal
->j_len_alloc
+ nblocks
+ 2) >=
3065 journal
->j_max_batch
&&
3066 ((journal
->j_len
+ nblocks
+ 2) * 100) <
3067 (journal
->j_len_alloc
* 75)) {
3068 if (atomic_read(&journal
->j_wcount
) > 10) {
3070 queue_log_writer(sb
);
3074 /* don't mess with joining the transaction if all we have to do is
3075 * wait for someone else to do a commit
3077 if (atomic_read(&journal
->j_jlock
)) {
3078 while (journal
->j_trans_id
== old_trans_id
&&
3079 atomic_read(&journal
->j_jlock
)) {
3080 queue_log_writer(sb
);
3084 retval
= journal_join(&myth
, sb
, 1);
3088 /* someone might have ended the transaction while we joined */
3089 if (old_trans_id
!= journal
->j_trans_id
) {
3090 retval
= do_journal_end(&myth
, sb
, 1, 0);
3092 retval
= do_journal_end(&myth
, sb
, 1, COMMIT_NOW
);
3098 PROC_INFO_INC(sb
, journal
.journal_relock_wcount
);
3101 /* we are the first writer, set trans_id */
3102 if (journal
->j_trans_start_time
== 0) {
3103 journal
->j_trans_start_time
= get_seconds();
3105 atomic_inc(&(journal
->j_wcount
));
3106 journal
->j_len_alloc
+= nblocks
;
3107 th
->t_blocks_logged
= 0;
3108 th
->t_blocks_allocated
= nblocks
;
3109 th
->t_trans_id
= journal
->j_trans_id
;
3111 INIT_LIST_HEAD(&th
->t_list
);
3116 memset(th
, 0, sizeof(*th
));
3117 /* Re-set th->t_super, so we can properly keep track of how many
3118 * persistent transactions there are. We need to do this so if this
3119 * call is part of a failed restart_transaction, we can free it later */
3124 struct reiserfs_transaction_handle
*reiserfs_persistent_transaction(struct
3130 struct reiserfs_transaction_handle
*th
;
3132 /* if we're nesting into an existing transaction. It will be
3133 ** persistent on its own
3135 if (reiserfs_transaction_running(s
)) {
3136 th
= current
->journal_info
;
3138 BUG_ON(th
->t_refcount
< 2);
3142 th
= kmalloc(sizeof(struct reiserfs_transaction_handle
), GFP_NOFS
);
3145 ret
= journal_begin(th
, s
, nblocks
);
3151 SB_JOURNAL(s
)->j_persistent_trans
++;
3155 int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle
*th
)
3157 struct super_block
*s
= th
->t_super
;
3160 ret
= journal_end(th
, th
->t_super
, th
->t_blocks_allocated
);
3163 if (th
->t_refcount
== 0) {
3164 SB_JOURNAL(s
)->j_persistent_trans
--;
3170 static int journal_join(struct reiserfs_transaction_handle
*th
,
3171 struct super_block
*sb
, unsigned long nblocks
)
3173 struct reiserfs_transaction_handle
*cur_th
= current
->journal_info
;
3175 /* this keeps do_journal_end from NULLing out the current->journal_info
3178 th
->t_handle_save
= cur_th
;
3179 BUG_ON(cur_th
&& cur_th
->t_refcount
> 1);
3180 return do_journal_begin_r(th
, sb
, nblocks
, JBEGIN_JOIN
);
3183 int journal_join_abort(struct reiserfs_transaction_handle
*th
,
3184 struct super_block
*sb
, unsigned long nblocks
)
3186 struct reiserfs_transaction_handle
*cur_th
= current
->journal_info
;
3188 /* this keeps do_journal_end from NULLing out the current->journal_info
3191 th
->t_handle_save
= cur_th
;
3192 BUG_ON(cur_th
&& cur_th
->t_refcount
> 1);
3193 return do_journal_begin_r(th
, sb
, nblocks
, JBEGIN_ABORT
);
3196 int journal_begin(struct reiserfs_transaction_handle
*th
,
3197 struct super_block
*sb
, unsigned long nblocks
)
3199 struct reiserfs_transaction_handle
*cur_th
= current
->journal_info
;
3202 th
->t_handle_save
= NULL
;
3204 /* we are nesting into the current transaction */
3205 if (cur_th
->t_super
== sb
) {
3206 BUG_ON(!cur_th
->t_refcount
);
3207 cur_th
->t_refcount
++;
3208 memcpy(th
, cur_th
, sizeof(*th
));
3209 if (th
->t_refcount
<= 1)
3210 reiserfs_warning(sb
, "reiserfs-2005",
3211 "BAD: refcount <= 1, but "
3212 "journal_info != 0");
3215 /* we've ended up with a handle from a different filesystem.
3216 ** save it and restore on journal_end. This should never
3219 reiserfs_warning(sb
, "clm-2100",
3220 "nesting info a different FS");
3221 th
->t_handle_save
= current
->journal_info
;
3222 current
->journal_info
= th
;
3225 current
->journal_info
= th
;
3227 ret
= do_journal_begin_r(th
, sb
, nblocks
, JBEGIN_REG
);
3228 BUG_ON(current
->journal_info
!= th
);
3230 /* I guess this boils down to being the reciprocal of clm-2100 above.
3231 * If do_journal_begin_r fails, we need to put it back, since journal_end
3232 * won't be called to do it. */
3234 current
->journal_info
= th
->t_handle_save
;
3236 BUG_ON(!th
->t_refcount
);
3242 ** puts bh into the current transaction. If it was already there, reorders removes the
3243 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
3245 ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
3246 ** transaction is committed.
3248 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
3250 int journal_mark_dirty(struct reiserfs_transaction_handle
*th
,
3251 struct super_block
*sb
, struct buffer_head
*bh
)
3253 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3254 struct reiserfs_journal_cnode
*cn
= NULL
;
3255 int count_already_incd
= 0;
3257 BUG_ON(!th
->t_trans_id
);
3259 PROC_INFO_INC(sb
, journal
.mark_dirty
);
3260 if (th
->t_trans_id
!= journal
->j_trans_id
) {
3261 reiserfs_panic(th
->t_super
, "journal-1577",
3262 "handle trans id %ld != current trans id %ld",
3263 th
->t_trans_id
, journal
->j_trans_id
);
3268 prepared
= test_clear_buffer_journal_prepared(bh
);
3269 clear_buffer_journal_restore_dirty(bh
);
3270 /* already in this transaction, we are done */
3271 if (buffer_journaled(bh
)) {
3272 PROC_INFO_INC(sb
, journal
.mark_dirty_already
);
3276 /* this must be turned into a panic instead of a warning. We can't allow
3277 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
3278 ** could get to disk too early. NOT GOOD.
3280 if (!prepared
|| buffer_dirty(bh
)) {
3281 reiserfs_warning(sb
, "journal-1777",
3282 "buffer %llu bad state "
3283 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
3284 (unsigned long long)bh
->b_blocknr
,
3285 prepared
? ' ' : '!',
3286 buffer_locked(bh
) ? ' ' : '!',
3287 buffer_dirty(bh
) ? ' ' : '!',
3288 buffer_journal_dirty(bh
) ? ' ' : '!');
3291 if (atomic_read(&(journal
->j_wcount
)) <= 0) {
3292 reiserfs_warning(sb
, "journal-1409",
3293 "returning because j_wcount was %d",
3294 atomic_read(&(journal
->j_wcount
)));
3297 /* this error means I've screwed up, and we've overflowed the transaction.
3298 ** Nothing can be done here, except make the FS readonly or panic.
3300 if (journal
->j_len
>= journal
->j_trans_max
) {
3301 reiserfs_panic(th
->t_super
, "journal-1413",
3302 "j_len (%lu) is too big",
3306 if (buffer_journal_dirty(bh
)) {
3307 count_already_incd
= 1;
3308 PROC_INFO_INC(sb
, journal
.mark_dirty_notjournal
);
3309 clear_buffer_journal_dirty(bh
);
3312 if (journal
->j_len
> journal
->j_len_alloc
) {
3313 journal
->j_len_alloc
= journal
->j_len
+ JOURNAL_PER_BALANCE_CNT
;
3316 set_buffer_journaled(bh
);
3318 /* now put this guy on the end */
3322 reiserfs_panic(sb
, "journal-4", "get_cnode failed!");
3325 if (th
->t_blocks_logged
== th
->t_blocks_allocated
) {
3326 th
->t_blocks_allocated
+= JOURNAL_PER_BALANCE_CNT
;
3327 journal
->j_len_alloc
+= JOURNAL_PER_BALANCE_CNT
;
3329 th
->t_blocks_logged
++;
3333 cn
->blocknr
= bh
->b_blocknr
;
3336 insert_journal_hash(journal
->j_hash_table
, cn
);
3337 if (!count_already_incd
) {
3342 cn
->prev
= journal
->j_last
;
3344 if (journal
->j_last
) {
3345 journal
->j_last
->next
= cn
;
3346 journal
->j_last
= cn
;
3348 journal
->j_first
= cn
;
3349 journal
->j_last
= cn
;
3354 int journal_end(struct reiserfs_transaction_handle
*th
,
3355 struct super_block
*sb
, unsigned long nblocks
)
3357 if (!current
->journal_info
&& th
->t_refcount
> 1)
3358 reiserfs_warning(sb
, "REISER-NESTING",
3359 "th NULL, refcount %d", th
->t_refcount
);
3361 if (!th
->t_trans_id
) {
3367 if (th
->t_refcount
> 0) {
3368 struct reiserfs_transaction_handle
*cur_th
=
3369 current
->journal_info
;
3371 /* we aren't allowed to close a nested transaction on a different
3372 ** filesystem from the one in the task struct
3374 BUG_ON(cur_th
->t_super
!= th
->t_super
);
3377 memcpy(current
->journal_info
, th
, sizeof(*th
));
3382 return do_journal_end(th
, sb
, nblocks
, 0);
3386 /* removes from the current transaction, relsing and descrementing any counters.
3387 ** also files the removed buffer directly onto the clean list
3389 ** called by journal_mark_freed when a block has been deleted
3391 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
3393 static int remove_from_transaction(struct super_block
*sb
,
3394 b_blocknr_t blocknr
, int already_cleaned
)
3396 struct buffer_head
*bh
;
3397 struct reiserfs_journal_cnode
*cn
;
3398 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3401 cn
= get_journal_hash_dev(sb
, journal
->j_hash_table
, blocknr
);
3402 if (!cn
|| !cn
->bh
) {
3407 cn
->prev
->next
= cn
->next
;
3410 cn
->next
->prev
= cn
->prev
;
3412 if (cn
== journal
->j_first
) {
3413 journal
->j_first
= cn
->next
;
3415 if (cn
== journal
->j_last
) {
3416 journal
->j_last
= cn
->prev
;
3419 remove_journal_hash(sb
, journal
->j_hash_table
, NULL
,
3421 clear_buffer_journaled(bh
); /* don't log this one */
3423 if (!already_cleaned
) {
3424 clear_buffer_journal_dirty(bh
);
3425 clear_buffer_dirty(bh
);
3426 clear_buffer_journal_test(bh
);
3428 if (atomic_read(&(bh
->b_count
)) < 0) {
3429 reiserfs_warning(sb
, "journal-1752",
3435 journal
->j_len_alloc
--;
3441 ** for any cnode in a journal list, it can only be dirtied of all the
3442 ** transactions that include it are committed to disk.
3443 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
3444 ** and 0 if you aren't
3446 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3447 ** blocks for a given transaction on disk
3450 static int can_dirty(struct reiserfs_journal_cnode
*cn
)
3452 struct super_block
*sb
= cn
->sb
;
3453 b_blocknr_t blocknr
= cn
->blocknr
;
3454 struct reiserfs_journal_cnode
*cur
= cn
->hprev
;
3457 /* first test hprev. These are all newer than cn, so any node here
3458 ** with the same block number and dev means this node can't be sent
3459 ** to disk right now.
3461 while (cur
&& can_dirty
) {
3462 if (cur
->jlist
&& cur
->bh
&& cur
->blocknr
&& cur
->sb
== sb
&&
3463 cur
->blocknr
== blocknr
) {
3468 /* then test hnext. These are all older than cn. As long as they
3469 ** are committed to the log, it is safe to write cn to disk
3472 while (cur
&& can_dirty
) {
3473 if (cur
->jlist
&& cur
->jlist
->j_len
> 0 &&
3474 atomic_read(&(cur
->jlist
->j_commit_left
)) > 0 && cur
->bh
&&
3475 cur
->blocknr
&& cur
->sb
== sb
&& cur
->blocknr
== blocknr
) {
3483 /* syncs the commit blocks, but does not force the real buffers to disk
3484 ** will wait until the current transaction is done/committed before returning
3486 int journal_end_sync(struct reiserfs_transaction_handle
*th
,
3487 struct super_block
*sb
, unsigned long nblocks
)
3489 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3491 BUG_ON(!th
->t_trans_id
);
3492 /* you can sync while nested, very, very bad */
3493 BUG_ON(th
->t_refcount
> 1);
3494 if (journal
->j_len
== 0) {
3495 reiserfs_prepare_for_journal(sb
, SB_BUFFER_WITH_SB(sb
),
3497 journal_mark_dirty(th
, sb
, SB_BUFFER_WITH_SB(sb
));
3499 return do_journal_end(th
, sb
, nblocks
, COMMIT_NOW
| WAIT
);
3503 ** writeback the pending async commits to disk
3505 static void flush_async_commits(struct work_struct
*work
)
3507 struct reiserfs_journal
*journal
=
3508 container_of(work
, struct reiserfs_journal
, j_work
.work
);
3509 struct super_block
*sb
= journal
->j_work_sb
;
3510 struct reiserfs_journal_list
*jl
;
3511 struct list_head
*entry
;
3513 reiserfs_write_lock(sb
);
3514 if (!list_empty(&journal
->j_journal_list
)) {
3515 /* last entry is the youngest, commit it and you get everything */
3516 entry
= journal
->j_journal_list
.prev
;
3517 jl
= JOURNAL_LIST_ENTRY(entry
);
3518 flush_commit_list(sb
, jl
, 1);
3520 reiserfs_write_unlock(sb
);
3524 ** flushes any old transactions to disk
3525 ** ends the current transaction if it is too old
3527 int reiserfs_flush_old_commits(struct super_block
*sb
)
3530 struct reiserfs_transaction_handle th
;
3531 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3533 now
= get_seconds();
3534 /* safety check so we don't flush while we are replaying the log during
3537 if (list_empty(&journal
->j_journal_list
)) {
3541 /* check the current transaction. If there are no writers, and it is
3542 * too old, finish it, and force the commit blocks to disk
3544 if (atomic_read(&journal
->j_wcount
) <= 0 &&
3545 journal
->j_trans_start_time
> 0 &&
3546 journal
->j_len
> 0 &&
3547 (now
- journal
->j_trans_start_time
) > journal
->j_max_trans_age
) {
3548 if (!journal_join(&th
, sb
, 1)) {
3549 reiserfs_prepare_for_journal(sb
,
3550 SB_BUFFER_WITH_SB(sb
),
3552 journal_mark_dirty(&th
, sb
,
3553 SB_BUFFER_WITH_SB(sb
));
3555 /* we're only being called from kreiserfsd, it makes no sense to do
3556 ** an async commit so that kreiserfsd can do it later
3558 do_journal_end(&th
, sb
, 1, COMMIT_NOW
| WAIT
);
3565 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
3567 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
3568 ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3569 ** flushes the commit list and returns 0.
3571 ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
3573 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3575 static int check_journal_end(struct reiserfs_transaction_handle
*th
,
3576 struct super_block
*sb
, unsigned long nblocks
,
3581 int flush
= flags
& FLUSH_ALL
;
3582 int commit_now
= flags
& COMMIT_NOW
;
3583 int wait_on_commit
= flags
& WAIT
;
3584 struct reiserfs_journal_list
*jl
;
3585 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3587 BUG_ON(!th
->t_trans_id
);
3589 if (th
->t_trans_id
!= journal
->j_trans_id
) {
3590 reiserfs_panic(th
->t_super
, "journal-1577",
3591 "handle trans id %ld != current trans id %ld",
3592 th
->t_trans_id
, journal
->j_trans_id
);
3595 journal
->j_len_alloc
-= (th
->t_blocks_allocated
- th
->t_blocks_logged
);
3596 if (atomic_read(&(journal
->j_wcount
)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3597 atomic_dec(&(journal
->j_wcount
));
3600 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
3601 ** will be dealt with by next transaction that actually writes something, but should be taken
3602 ** care of in this trans
3604 BUG_ON(journal
->j_len
== 0);
3606 /* if wcount > 0, and we are called to with flush or commit_now,
3607 ** we wait on j_join_wait. We will wake up when the last writer has
3608 ** finished the transaction, and started it on its way to the disk.
3609 ** Then, we flush the commit or journal list, and just return 0
3610 ** because the rest of journal end was already done for this transaction.
3612 if (atomic_read(&(journal
->j_wcount
)) > 0) {
3613 if (flush
|| commit_now
) {
3616 jl
= journal
->j_current_jl
;
3617 trans_id
= jl
->j_trans_id
;
3619 jl
->j_state
|= LIST_COMMIT_PENDING
;
3620 atomic_set(&(journal
->j_jlock
), 1);
3622 journal
->j_next_full_flush
= 1;
3626 /* sleep while the current transaction is still j_jlocked */
3627 while (journal
->j_trans_id
== trans_id
) {
3628 if (atomic_read(&journal
->j_jlock
)) {
3629 queue_log_writer(sb
);
3632 if (journal
->j_trans_id
== trans_id
) {
3633 atomic_set(&(journal
->j_jlock
),
3639 BUG_ON(journal
->j_trans_id
== trans_id
);
3642 && journal_list_still_alive(sb
, trans_id
)
3643 && wait_on_commit
) {
3644 flush_commit_list(sb
, jl
, 1);
3652 /* deal with old transactions where we are the last writers */
3653 now
= get_seconds();
3654 if ((now
- journal
->j_trans_start_time
) > journal
->j_max_trans_age
) {
3656 journal
->j_next_async_flush
= 1;
3658 /* don't batch when someone is waiting on j_join_wait */
3659 /* don't batch when syncing the commit or flushing the whole trans */
3660 if (!(journal
->j_must_wait
> 0) && !(atomic_read(&(journal
->j_jlock
)))
3661 && !flush
&& !commit_now
&& (journal
->j_len
< journal
->j_max_batch
)
3662 && journal
->j_len_alloc
< journal
->j_max_batch
3663 && journal
->j_cnode_free
> (journal
->j_trans_max
* 3)) {
3664 journal
->j_bcount
++;
3669 if (journal
->j_start
> SB_ONDISK_JOURNAL_SIZE(sb
)) {
3670 reiserfs_panic(sb
, "journal-003",
3671 "j_start (%ld) is too high",
3678 ** Does all the work that makes deleting blocks safe.
3679 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3682 ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3683 ** before this transaction has finished.
3685 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3686 ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3687 ** the block can't be reallocated yet.
3689 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3691 int journal_mark_freed(struct reiserfs_transaction_handle
*th
,
3692 struct super_block
*sb
, b_blocknr_t blocknr
)
3694 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3695 struct reiserfs_journal_cnode
*cn
= NULL
;
3696 struct buffer_head
*bh
= NULL
;
3697 struct reiserfs_list_bitmap
*jb
= NULL
;
3699 BUG_ON(!th
->t_trans_id
);
3701 cn
= get_journal_hash_dev(sb
, journal
->j_hash_table
, blocknr
);
3706 /* if it is journal new, we just remove it from this transaction */
3707 if (bh
&& buffer_journal_new(bh
)) {
3708 clear_buffer_journal_new(bh
);
3709 clear_prepared_bits(bh
);
3710 reiserfs_clean_and_file_buffer(bh
);
3711 cleaned
= remove_from_transaction(sb
, blocknr
, cleaned
);
3713 /* set the bit for this block in the journal bitmap for this transaction */
3714 jb
= journal
->j_current_jl
->j_list_bitmap
;
3716 reiserfs_panic(sb
, "journal-1702",
3717 "journal_list_bitmap is NULL");
3719 set_bit_in_list_bitmap(sb
, blocknr
, jb
);
3721 /* Note, the entire while loop is not allowed to schedule. */
3724 clear_prepared_bits(bh
);
3725 reiserfs_clean_and_file_buffer(bh
);
3727 cleaned
= remove_from_transaction(sb
, blocknr
, cleaned
);
3729 /* find all older transactions with this block, make sure they don't try to write it out */
3730 cn
= get_journal_hash_dev(sb
, journal
->j_list_hash_table
,
3733 if (sb
== cn
->sb
&& blocknr
== cn
->blocknr
) {
3734 set_bit(BLOCK_FREED
, &cn
->state
);
3737 /* remove_from_transaction will brelse the buffer if it was
3738 ** in the current trans
3740 clear_buffer_journal_dirty(cn
->
3742 clear_buffer_dirty(cn
->bh
);
3743 clear_buffer_journal_test(cn
->
3748 (&(cn
->bh
->b_count
)) < 0) {
3749 reiserfs_warning(sb
,
3751 "cn->bh->b_count < 0");
3754 if (cn
->jlist
) { /* since we are clearing the bh, we MUST dec nonzerolen */
3767 release_buffer_page(bh
); /* get_hash grabs the buffer */
3771 void reiserfs_update_inode_transaction(struct inode
*inode
)
3773 struct reiserfs_journal
*journal
= SB_JOURNAL(inode
->i_sb
);
3774 REISERFS_I(inode
)->i_jl
= journal
->j_current_jl
;
3775 REISERFS_I(inode
)->i_trans_id
= journal
->j_trans_id
;
3779 * returns -1 on error, 0 if no commits/barriers were done and 1
3780 * if a transaction was actually committed and the barrier was done
3782 static int __commit_trans_jl(struct inode
*inode
, unsigned long id
,
3783 struct reiserfs_journal_list
*jl
)
3785 struct reiserfs_transaction_handle th
;
3786 struct super_block
*sb
= inode
->i_sb
;
3787 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3790 /* is it from the current transaction, or from an unknown transaction? */
3791 if (id
== journal
->j_trans_id
) {
3792 jl
= journal
->j_current_jl
;
3793 /* try to let other writers come in and grow this transaction */
3794 let_transaction_grow(sb
, id
);
3795 if (journal
->j_trans_id
!= id
) {
3796 goto flush_commit_only
;
3799 ret
= journal_begin(&th
, sb
, 1);
3803 /* someone might have ended this transaction while we joined */
3804 if (journal
->j_trans_id
!= id
) {
3805 reiserfs_prepare_for_journal(sb
, SB_BUFFER_WITH_SB(sb
),
3807 journal_mark_dirty(&th
, sb
, SB_BUFFER_WITH_SB(sb
));
3808 ret
= journal_end(&th
, sb
, 1);
3809 goto flush_commit_only
;
3812 ret
= journal_end_sync(&th
, sb
, 1);
3817 /* this gets tricky, we have to make sure the journal list in
3818 * the inode still exists. We know the list is still around
3819 * if we've got a larger transaction id than the oldest list
3822 if (journal_list_still_alive(inode
->i_sb
, id
)) {
3824 * we only set ret to 1 when we know for sure
3825 * the barrier hasn't been started yet on the commit
3828 if (atomic_read(&jl
->j_commit_left
) > 1)
3830 flush_commit_list(sb
, jl
, 1);
3831 if (journal
->j_errno
)
3832 ret
= journal
->j_errno
;
3835 /* otherwise the list is gone, and long since committed */
3839 int reiserfs_commit_for_inode(struct inode
*inode
)
3841 unsigned int id
= REISERFS_I(inode
)->i_trans_id
;
3842 struct reiserfs_journal_list
*jl
= REISERFS_I(inode
)->i_jl
;
3844 /* for the whole inode, assume unset id means it was
3845 * changed in the current transaction. More conservative
3848 reiserfs_update_inode_transaction(inode
);
3849 id
= REISERFS_I(inode
)->i_trans_id
;
3850 /* jl will be updated in __commit_trans_jl */
3853 return __commit_trans_jl(inode
, id
, jl
);
3856 void reiserfs_restore_prepared_buffer(struct super_block
*sb
,
3857 struct buffer_head
*bh
)
3859 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3860 PROC_INFO_INC(sb
, journal
.restore_prepared
);
3864 if (test_clear_buffer_journal_restore_dirty(bh
) &&
3865 buffer_journal_dirty(bh
)) {
3866 struct reiserfs_journal_cnode
*cn
;
3867 cn
= get_journal_hash_dev(sb
,
3868 journal
->j_list_hash_table
,
3870 if (cn
&& can_dirty(cn
)) {
3871 set_buffer_journal_test(bh
);
3872 mark_buffer_dirty(bh
);
3875 clear_buffer_journal_prepared(bh
);
3878 extern struct tree_balance
*cur_tb
;
3880 ** before we can change a metadata block, we have to make sure it won't
3881 ** be written to disk while we are altering it. So, we must:
3886 int reiserfs_prepare_for_journal(struct super_block
*sb
,
3887 struct buffer_head
*bh
, int wait
)
3889 PROC_INFO_INC(sb
, journal
.prepare
);
3891 if (!trylock_buffer(bh
)) {
3896 set_buffer_journal_prepared(bh
);
3897 if (test_clear_buffer_dirty(bh
) && buffer_journal_dirty(bh
)) {
3898 clear_buffer_journal_test(bh
);
3899 set_buffer_journal_restore_dirty(bh
);
3905 static void flush_old_journal_lists(struct super_block
*s
)
3907 struct reiserfs_journal
*journal
= SB_JOURNAL(s
);
3908 struct reiserfs_journal_list
*jl
;
3909 struct list_head
*entry
;
3910 time_t now
= get_seconds();
3912 while (!list_empty(&journal
->j_journal_list
)) {
3913 entry
= journal
->j_journal_list
.next
;
3914 jl
= JOURNAL_LIST_ENTRY(entry
);
3915 /* this check should always be run, to send old lists to disk */
3916 if (jl
->j_timestamp
< (now
- (JOURNAL_MAX_TRANS_AGE
* 4)) &&
3917 atomic_read(&jl
->j_commit_left
) == 0 &&
3918 test_transaction(s
, jl
)) {
3919 flush_used_journal_lists(s
, jl
);
3927 ** long and ugly. If flush, will not return until all commit
3928 ** blocks and all real buffers in the trans are on disk.
3929 ** If no_async, won't return until all commit blocks are on disk.
3931 ** keep reading, there are comments as you go along
3933 ** If the journal is aborted, we just clean up. Things like flushing
3934 ** journal lists, etc just won't happen.
3936 static int do_journal_end(struct reiserfs_transaction_handle
*th
,
3937 struct super_block
*sb
, unsigned long nblocks
,
3940 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
3941 struct reiserfs_journal_cnode
*cn
, *next
, *jl_cn
;
3942 struct reiserfs_journal_cnode
*last_cn
= NULL
;
3943 struct reiserfs_journal_desc
*desc
;
3944 struct reiserfs_journal_commit
*commit
;
3945 struct buffer_head
*c_bh
; /* commit bh */
3946 struct buffer_head
*d_bh
; /* desc bh */
3947 int cur_write_start
= 0; /* start index of current log write */
3952 struct reiserfs_journal_list
*jl
, *temp_jl
;
3953 struct list_head
*entry
, *safe
;
3954 unsigned long jindex
;
3955 unsigned int commit_trans_id
;
3958 BUG_ON(th
->t_refcount
> 1);
3959 BUG_ON(!th
->t_trans_id
);
3961 /* protect flush_older_commits from doing mistakes if the
3962 transaction ID counter gets overflowed. */
3963 if (th
->t_trans_id
== ~0U)
3964 flags
|= FLUSH_ALL
| COMMIT_NOW
| WAIT
;
3965 flush
= flags
& FLUSH_ALL
;
3966 wait_on_commit
= flags
& WAIT
;
3969 current
->journal_info
= th
->t_handle_save
;
3970 reiserfs_check_lock_depth(sb
, "journal end");
3971 if (journal
->j_len
== 0) {
3972 reiserfs_prepare_for_journal(sb
, SB_BUFFER_WITH_SB(sb
),
3974 journal_mark_dirty(th
, sb
, SB_BUFFER_WITH_SB(sb
));
3978 if (journal
->j_next_full_flush
) {
3982 if (journal
->j_next_async_flush
) {
3983 flags
|= COMMIT_NOW
| WAIT
;
3987 /* check_journal_end locks the journal, and unlocks if it does not return 1
3988 ** it tells us if we should continue with the journal_end, or just return
3990 if (!check_journal_end(th
, sb
, nblocks
, flags
)) {
3992 wake_queued_writers(sb
);
3993 reiserfs_async_progress_wait(sb
);
3997 /* check_journal_end might set these, check again */
3998 if (journal
->j_next_full_flush
) {
4003 ** j must wait means we have to flush the log blocks, and the real blocks for
4006 if (journal
->j_must_wait
> 0) {
4009 #ifdef REISERFS_PREALLOCATE
4010 /* quota ops might need to nest, setup the journal_info pointer for them
4011 * and raise the refcount so that it is > 0. */
4012 current
->journal_info
= th
;
4014 reiserfs_discard_all_prealloc(th
); /* it should not involve new blocks into
4015 * the transaction */
4017 current
->journal_info
= th
->t_handle_save
;
4020 /* setup description block */
4023 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
4025 set_buffer_uptodate(d_bh
);
4026 desc
= (struct reiserfs_journal_desc
*)(d_bh
)->b_data
;
4027 memset(d_bh
->b_data
, 0, d_bh
->b_size
);
4028 memcpy(get_journal_desc_magic(d_bh
), JOURNAL_DESC_MAGIC
, 8);
4029 set_desc_trans_id(desc
, journal
->j_trans_id
);
4031 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
4032 c_bh
= journal_getblk(sb
, SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
4033 ((journal
->j_start
+ journal
->j_len
+
4034 1) % SB_ONDISK_JOURNAL_SIZE(sb
)));
4035 commit
= (struct reiserfs_journal_commit
*)c_bh
->b_data
;
4036 memset(c_bh
->b_data
, 0, c_bh
->b_size
);
4037 set_commit_trans_id(commit
, journal
->j_trans_id
);
4038 set_buffer_uptodate(c_bh
);
4040 /* init this journal list */
4041 jl
= journal
->j_current_jl
;
4043 /* we lock the commit before doing anything because
4044 * we want to make sure nobody tries to run flush_commit_list until
4045 * the new transaction is fully setup, and we've already flushed the
4048 reiserfs_mutex_lock_safe(&jl
->j_commit_mutex
, sb
);
4050 /* save the transaction id in case we need to commit it later */
4051 commit_trans_id
= jl
->j_trans_id
;
4053 atomic_set(&jl
->j_older_commits_done
, 0);
4054 jl
->j_trans_id
= journal
->j_trans_id
;
4055 jl
->j_timestamp
= journal
->j_trans_start_time
;
4056 jl
->j_commit_bh
= c_bh
;
4057 jl
->j_start
= journal
->j_start
;
4058 jl
->j_len
= journal
->j_len
;
4059 atomic_set(&jl
->j_nonzerolen
, journal
->j_len
);
4060 atomic_set(&jl
->j_commit_left
, journal
->j_len
+ 2);
4061 jl
->j_realblock
= NULL
;
4063 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
4064 ** for each real block, add it to the journal list hash,
4065 ** copy into real block index array in the commit or desc block
4067 trans_half
= journal_trans_half(sb
->s_blocksize
);
4068 for (i
= 0, cn
= journal
->j_first
; cn
; cn
= cn
->next
, i
++) {
4069 if (buffer_journaled(cn
->bh
)) {
4070 jl_cn
= get_cnode(sb
);
4072 reiserfs_panic(sb
, "journal-1676",
4073 "get_cnode returned NULL");
4076 jl
->j_realblock
= jl_cn
;
4078 jl_cn
->prev
= last_cn
;
4081 last_cn
->next
= jl_cn
;
4084 /* make sure the block we are trying to log is not a block
4085 of journal or reserved area */
4087 if (is_block_in_log_or_reserved_area
4088 (sb
, cn
->bh
->b_blocknr
)) {
4089 reiserfs_panic(sb
, "journal-2332",
4090 "Trying to log block %lu, "
4091 "which is a log block",
4094 jl_cn
->blocknr
= cn
->bh
->b_blocknr
;
4099 insert_journal_hash(journal
->j_list_hash_table
, jl_cn
);
4100 if (i
< trans_half
) {
4101 desc
->j_realblock
[i
] =
4102 cpu_to_le32(cn
->bh
->b_blocknr
);
4104 commit
->j_realblock
[i
- trans_half
] =
4105 cpu_to_le32(cn
->bh
->b_blocknr
);
4111 set_desc_trans_len(desc
, journal
->j_len
);
4112 set_desc_mount_id(desc
, journal
->j_mount_id
);
4113 set_desc_trans_id(desc
, journal
->j_trans_id
);
4114 set_commit_trans_len(commit
, journal
->j_len
);
4116 /* special check in case all buffers in the journal were marked for not logging */
4117 BUG_ON(journal
->j_len
== 0);
4119 /* we're about to dirty all the log blocks, mark the description block
4120 * dirty now too. Don't mark the commit block dirty until all the
4121 * others are on disk
4123 mark_buffer_dirty(d_bh
);
4125 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
4126 cur_write_start
= journal
->j_start
;
4127 cn
= journal
->j_first
;
4128 jindex
= 1; /* start at one so we don't get the desc again */
4130 clear_buffer_journal_new(cn
->bh
);
4131 /* copy all the real blocks into log area. dirty log blocks */
4132 if (buffer_journaled(cn
->bh
)) {
4133 struct buffer_head
*tmp_bh
;
4138 SB_ONDISK_JOURNAL_1st_BLOCK(sb
) +
4141 SB_ONDISK_JOURNAL_SIZE(sb
)));
4142 set_buffer_uptodate(tmp_bh
);
4143 page
= cn
->bh
->b_page
;
4145 memcpy(tmp_bh
->b_data
,
4146 addr
+ offset_in_page(cn
->bh
->b_data
),
4149 mark_buffer_dirty(tmp_bh
);
4151 set_buffer_journal_dirty(cn
->bh
);
4152 clear_buffer_journaled(cn
->bh
);
4154 /* JDirty cleared sometime during transaction. don't log this one */
4155 reiserfs_warning(sb
, "journal-2048",
4156 "BAD, buffer in journal hash, "
4163 reiserfs_write_unlock(sb
);
4165 reiserfs_write_lock(sb
);
4168 /* we are done with both the c_bh and d_bh, but
4169 ** c_bh must be written after all other commit blocks,
4170 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
4173 journal
->j_current_jl
= alloc_journal_list(sb
);
4175 /* now it is safe to insert this transaction on the main list */
4176 list_add_tail(&jl
->j_list
, &journal
->j_journal_list
);
4177 list_add_tail(&jl
->j_working_list
, &journal
->j_working_list
);
4178 journal
->j_num_work_lists
++;
4180 /* reset journal values for the next transaction */
4181 old_start
= journal
->j_start
;
4183 (journal
->j_start
+ journal
->j_len
+
4184 2) % SB_ONDISK_JOURNAL_SIZE(sb
);
4185 atomic_set(&(journal
->j_wcount
), 0);
4186 journal
->j_bcount
= 0;
4187 journal
->j_last
= NULL
;
4188 journal
->j_first
= NULL
;
4190 journal
->j_trans_start_time
= 0;
4191 /* check for trans_id overflow */
4192 if (++journal
->j_trans_id
== 0)
4193 journal
->j_trans_id
= 10;
4194 journal
->j_current_jl
->j_trans_id
= journal
->j_trans_id
;
4195 journal
->j_must_wait
= 0;
4196 journal
->j_len_alloc
= 0;
4197 journal
->j_next_full_flush
= 0;
4198 journal
->j_next_async_flush
= 0;
4199 init_journal_hash(sb
);
4201 // make sure reiserfs_add_jh sees the new current_jl before we
4202 // write out the tails
4205 /* tail conversion targets have to hit the disk before we end the
4206 * transaction. Otherwise a later transaction might repack the tail
4207 * before this transaction commits, leaving the data block unflushed and
4208 * clean, if we crash before the later transaction commits, the data block
4211 if (!list_empty(&jl
->j_tail_bh_list
)) {
4212 reiserfs_write_unlock(sb
);
4213 write_ordered_buffers(&journal
->j_dirty_buffers_lock
,
4214 journal
, jl
, &jl
->j_tail_bh_list
);
4215 reiserfs_write_lock(sb
);
4217 BUG_ON(!list_empty(&jl
->j_tail_bh_list
));
4218 mutex_unlock(&jl
->j_commit_mutex
);
4220 /* honor the flush wishes from the caller, simple commits can
4221 ** be done outside the journal lock, they are done below
4223 ** if we don't flush the commit list right now, we put it into
4224 ** the work queue so the people waiting on the async progress work
4225 ** queue don't wait for this proc to flush journal lists and such.
4228 flush_commit_list(sb
, jl
, 1);
4229 flush_journal_list(sb
, jl
, 1);
4230 } else if (!(jl
->j_state
& LIST_COMMIT_PENDING
))
4231 queue_delayed_work(commit_wq
, &journal
->j_work
, HZ
/ 10);
4233 /* if the next transaction has any chance of wrapping, flush
4234 ** transactions that might get overwritten. If any journal lists are very
4235 ** old flush them as well.
4238 list_for_each_safe(entry
, safe
, &journal
->j_journal_list
) {
4239 temp_jl
= JOURNAL_LIST_ENTRY(entry
);
4240 if (journal
->j_start
<= temp_jl
->j_start
) {
4241 if ((journal
->j_start
+ journal
->j_trans_max
+ 1) >=
4243 flush_used_journal_lists(sb
, temp_jl
);
4245 } else if ((journal
->j_start
+
4246 journal
->j_trans_max
+ 1) <
4247 SB_ONDISK_JOURNAL_SIZE(sb
)) {
4248 /* if we don't cross into the next transaction and we don't
4249 * wrap, there is no way we can overlap any later transactions
4254 } else if ((journal
->j_start
+
4255 journal
->j_trans_max
+ 1) >
4256 SB_ONDISK_JOURNAL_SIZE(sb
)) {
4257 if (((journal
->j_start
+ journal
->j_trans_max
+ 1) %
4258 SB_ONDISK_JOURNAL_SIZE(sb
)) >=
4260 flush_used_journal_lists(sb
, temp_jl
);
4263 /* we don't overlap anything from out start to the end of the
4264 * log, and our wrapped portion doesn't overlap anything at
4265 * the start of the log. We can break
4271 flush_old_journal_lists(sb
);
4273 journal
->j_current_jl
->j_list_bitmap
=
4274 get_list_bitmap(sb
, journal
->j_current_jl
);
4276 if (!(journal
->j_current_jl
->j_list_bitmap
)) {
4277 reiserfs_panic(sb
, "journal-1996",
4278 "could not get a list bitmap");
4281 atomic_set(&(journal
->j_jlock
), 0);
4283 /* wake up any body waiting to join. */
4284 clear_bit(J_WRITERS_QUEUED
, &journal
->j_state
);
4285 wake_up(&(journal
->j_join_wait
));
4287 if (!flush
&& wait_on_commit
&&
4288 journal_list_still_alive(sb
, commit_trans_id
)) {
4289 flush_commit_list(sb
, jl
, 1);
4292 reiserfs_check_lock_depth(sb
, "journal end2");
4294 memset(th
, 0, sizeof(*th
));
4295 /* Re-set th->t_super, so we can properly keep track of how many
4296 * persistent transactions there are. We need to do this so if this
4297 * call is part of a failed restart_transaction, we can free it later */
4300 return journal
->j_errno
;
4303 /* Send the file system read only and refuse new transactions */
4304 void reiserfs_abort_journal(struct super_block
*sb
, int errno
)
4306 struct reiserfs_journal
*journal
= SB_JOURNAL(sb
);
4307 if (test_bit(J_ABORTED
, &journal
->j_state
))
4310 if (!journal
->j_errno
)
4311 journal
->j_errno
= errno
;
4313 sb
->s_flags
|= MS_RDONLY
;
4314 set_bit(J_ABORTED
, &journal
->j_state
);
4316 #ifdef CONFIG_REISERFS_CHECK