2 ** Write ahead logging implementation copyright Chris Mason 2000
4 ** The background commits make this code very interelated, and
5 ** overly complex. I need to rethink things a bit....The major players:
7 ** journal_begin -- call with the number of blocks you expect to log.
8 ** If the current transaction is too
9 ** old, it will block until the current transaction is
10 ** finished, and then start a new one.
11 ** Usually, your transaction will get joined in with
12 ** previous ones for speed.
14 ** journal_join -- same as journal_begin, but won't block on the current
15 ** transaction regardless of age. Don't ever call
16 ** this. Ever. There are only two places it should be
17 ** called from, and they are both inside this file.
19 ** journal_mark_dirty -- adds blocks into this transaction. clears any flags
20 ** that might make them get sent to disk
21 ** and then marks them BH_JDirty. Puts the buffer head
22 ** into the current transaction hash.
24 ** journal_end -- if the current transaction is batchable, it does nothing
25 ** otherwise, it could do an async/synchronous commit, or
26 ** a full flush of all log and real blocks in the
29 ** flush_old_commits -- if the current transaction is too old, it is ended and
30 ** commit blocks are sent to disk. Forces commit blocks
31 ** to disk for all backgrounded commits that have been
33 ** -- Note, if you call this as an immediate flush from
34 ** from within kupdate, it will ignore the immediate flag
37 #include <linux/config.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 #include <linux/time.h>
42 #include <asm/semaphore.h>
44 #include <linux/vmalloc.h>
45 #include <linux/reiserfs_fs.h>
47 #include <linux/kernel.h>
48 #include <linux/errno.h>
49 #include <linux/fcntl.h>
50 #include <linux/stat.h>
51 #include <linux/string.h>
52 #include <linux/smp_lock.h>
53 #include <linux/buffer_head.h>
54 #include <linux/workqueue.h>
55 #include <linux/writeback.h>
56 #include <linux/blkdev.h>
58 /* gets a struct reiserfs_journal_list * from a list head */
59 #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
61 #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
64 /* the number of mounted filesystems. This is used to decide when to
65 ** start and kill the commit workqueue
67 static int reiserfs_mounted_fs_count;
69 static struct workqueue_struct *commit_wq;
71 #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
73 #define BUFNR 64 /*read ahead */
75 /* cnode stat bits. Move these into reiserfs_fs.h */
77 #define BLOCK_FREED 2 /* this block was freed, and can't be written. */
78 #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
80 #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
81 #define BLOCK_DIRTIED 5
83 /* journal list state bits */
84 #define LIST_TOUCHED 1
86 #define LIST_COMMIT_PENDING 4 /* someone will commit this list */
88 /* flags for do_journal_end */
89 #define FLUSH_ALL 1 /* flush commit and real blocks */
90 #define COMMIT_NOW 2 /* end and commit this transaction */
91 #define WAIT 4 /* wait for the log blocks to hit the disk */
93 static int do_journal_end(struct reiserfs_transaction_handle *,
94 struct super_block *, unsigned long nblocks,
96 static int flush_journal_list(struct super_block *s,
97 struct reiserfs_journal_list *jl, int flushall);
98 static int flush_commit_list(struct super_block *s,
99 struct reiserfs_journal_list *jl, int flushall);
100 static int can_dirty(struct reiserfs_journal_cnode *cn);
101 static int journal_join(struct reiserfs_transaction_handle *th,
102 struct super_block *p_s_sb, unsigned long nblocks);
103 static int release_journal_dev(struct super_block *super,
104 struct reiserfs_journal *journal);
105 static int dirty_one_transaction(struct super_block *s,
106 struct reiserfs_journal_list *jl);
107 static void flush_async_commits(void *p);
108 static void queue_log_writer(struct super_block *s);
110 /* values for join in do_journal_begin_r */
112 JBEGIN_REG = 0, /* regular journal begin */
113 JBEGIN_JOIN = 1, /* join the running transaction if at all possible */
114 JBEGIN_ABORT = 2, /* called from cleanup code, ignores aborted flag */
117 static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
118 struct super_block *p_s_sb,
119 unsigned long nblocks, int join);
121 static void init_journal_hash(struct super_block *p_s_sb)
123 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
124 memset(journal->j_hash_table, 0,
125 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
129 ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
130 ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
133 static int reiserfs_clean_and_file_buffer(struct buffer_head *bh)
136 clear_buffer_dirty(bh);
137 clear_buffer_journal_test(bh);
142 static void disable_barrier(struct super_block *s)
144 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_BARRIER_FLUSH);
145 printk("reiserfs: disabling flush barriers on %s\n",
146 reiserfs_bdevname(s));
149 static struct reiserfs_bitmap_node *allocate_bitmap_node(struct super_block
152 struct reiserfs_bitmap_node *bn;
155 bn = reiserfs_kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS,
160 bn->data = reiserfs_kmalloc(p_s_sb->s_blocksize, GFP_NOFS, p_s_sb);
162 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb);
166 memset(bn->data, 0, p_s_sb->s_blocksize);
167 INIT_LIST_HEAD(&bn->list);
171 static struct reiserfs_bitmap_node *get_bitmap_node(struct super_block *p_s_sb)
173 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
174 struct reiserfs_bitmap_node *bn = NULL;
175 struct list_head *entry = journal->j_bitmap_nodes.next;
177 journal->j_used_bitmap_nodes++;
180 if (entry != &journal->j_bitmap_nodes) {
181 bn = list_entry(entry, struct reiserfs_bitmap_node, list);
183 memset(bn->data, 0, p_s_sb->s_blocksize);
184 journal->j_free_bitmap_nodes--;
187 bn = allocate_bitmap_node(p_s_sb);
194 static inline void free_bitmap_node(struct super_block *p_s_sb,
195 struct reiserfs_bitmap_node *bn)
197 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
198 journal->j_used_bitmap_nodes--;
199 if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
200 reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb);
201 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb);
203 list_add(&bn->list, &journal->j_bitmap_nodes);
204 journal->j_free_bitmap_nodes++;
208 static void allocate_bitmap_nodes(struct super_block *p_s_sb)
211 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
212 struct reiserfs_bitmap_node *bn = NULL;
213 for (i = 0; i < REISERFS_MIN_BITMAP_NODES; i++) {
214 bn = allocate_bitmap_node(p_s_sb);
216 list_add(&bn->list, &journal->j_bitmap_nodes);
217 journal->j_free_bitmap_nodes++;
219 break; // this is ok, we'll try again when more are needed
224 static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
225 struct reiserfs_list_bitmap *jb)
227 int bmap_nr = block / (p_s_sb->s_blocksize << 3);
228 int bit_nr = block % (p_s_sb->s_blocksize << 3);
230 if (!jb->bitmaps[bmap_nr]) {
231 jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb);
233 set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data);
237 static void cleanup_bitmap_list(struct super_block *p_s_sb,
238 struct reiserfs_list_bitmap *jb)
241 if (jb->bitmaps == NULL)
244 for (i = 0; i < SB_BMAP_NR(p_s_sb); i++) {
245 if (jb->bitmaps[i]) {
246 free_bitmap_node(p_s_sb, jb->bitmaps[i]);
247 jb->bitmaps[i] = NULL;
253 ** only call this on FS unmount.
255 static int free_list_bitmaps(struct super_block *p_s_sb,
256 struct reiserfs_list_bitmap *jb_array)
259 struct reiserfs_list_bitmap *jb;
260 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
262 jb->journal_list = NULL;
263 cleanup_bitmap_list(p_s_sb, jb);
270 static int free_bitmap_nodes(struct super_block *p_s_sb)
272 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
273 struct list_head *next = journal->j_bitmap_nodes.next;
274 struct reiserfs_bitmap_node *bn;
276 while (next != &journal->j_bitmap_nodes) {
277 bn = list_entry(next, struct reiserfs_bitmap_node, list);
279 reiserfs_kfree(bn->data, p_s_sb->s_blocksize, p_s_sb);
280 reiserfs_kfree(bn, sizeof(struct reiserfs_bitmap_node), p_s_sb);
281 next = journal->j_bitmap_nodes.next;
282 journal->j_free_bitmap_nodes--;
289 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
290 ** jb_array is the array to be filled in.
292 int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
293 struct reiserfs_list_bitmap *jb_array,
298 struct reiserfs_list_bitmap *jb;
299 int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *);
301 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
303 jb->journal_list = NULL;
304 jb->bitmaps = vmalloc(mem);
306 reiserfs_warning(p_s_sb,
307 "clm-2000, unable to allocate bitmaps for journal lists");
311 memset(jb->bitmaps, 0, mem);
314 free_list_bitmaps(p_s_sb, jb_array);
321 ** find an available list bitmap. If you can't find one, flush a commit list
324 static struct reiserfs_list_bitmap *get_list_bitmap(struct super_block *p_s_sb,
325 struct reiserfs_journal_list
329 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
330 struct reiserfs_list_bitmap *jb = NULL;
332 for (j = 0; j < (JOURNAL_NUM_BITMAPS * 3); j++) {
333 i = journal->j_list_bitmap_index;
334 journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS;
335 jb = journal->j_list_bitmap + i;
336 if (journal->j_list_bitmap[i].journal_list) {
337 flush_commit_list(p_s_sb,
338 journal->j_list_bitmap[i].
340 if (!journal->j_list_bitmap[i].journal_list) {
347 if (jb->journal_list) { /* double check to make sure if flushed correctly */
350 jb->journal_list = jl;
355 ** allocates a new chunk of X nodes, and links them all together as a list.
356 ** Uses the cnode->next and cnode->prev pointers
357 ** returns NULL on failure
359 static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes)
361 struct reiserfs_journal_cnode *head;
363 if (num_cnodes <= 0) {
366 head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode));
370 memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode));
372 head[0].next = head + 1;
373 for (i = 1; i < num_cnodes; i++) {
374 head[i].prev = head + (i - 1);
375 head[i].next = head + (i + 1); /* if last one, overwrite it after the if */
377 head[num_cnodes - 1].next = NULL;
382 ** pulls a cnode off the free list, or returns NULL on failure
384 static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb)
386 struct reiserfs_journal_cnode *cn;
387 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
389 reiserfs_check_lock_depth(p_s_sb, "get_cnode");
391 if (journal->j_cnode_free <= 0) {
394 journal->j_cnode_used++;
395 journal->j_cnode_free--;
396 cn = journal->j_cnode_free_list;
401 cn->next->prev = NULL;
403 journal->j_cnode_free_list = cn->next;
404 memset(cn, 0, sizeof(struct reiserfs_journal_cnode));
409 ** returns a cnode to the free list
411 static void free_cnode(struct super_block *p_s_sb,
412 struct reiserfs_journal_cnode *cn)
414 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
416 reiserfs_check_lock_depth(p_s_sb, "free_cnode");
418 journal->j_cnode_used--;
419 journal->j_cnode_free++;
420 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
421 cn->next = journal->j_cnode_free_list;
422 if (journal->j_cnode_free_list) {
423 journal->j_cnode_free_list->prev = cn;
425 cn->prev = NULL; /* not needed with the memset, but I might kill the memset, and forget to do this */
426 journal->j_cnode_free_list = cn;
429 static void clear_prepared_bits(struct buffer_head *bh)
431 clear_buffer_journal_prepared(bh);
432 clear_buffer_journal_restore_dirty(bh);
435 /* utility function to force a BUG if it is called without the big
436 ** kernel lock held. caller is the string printed just before calling BUG()
438 void reiserfs_check_lock_depth(struct super_block *sb, char *caller)
441 if (current->lock_depth < 0) {
442 reiserfs_panic(sb, "%s called without kernel lock held",
450 /* return a cnode with same dev, block number and size in table, or null if not found */
451 static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct
455 reiserfs_journal_cnode
459 struct reiserfs_journal_cnode *cn;
460 cn = journal_hash(table, sb, bl);
462 if (cn->blocknr == bl && cn->sb == sb)
466 return (struct reiserfs_journal_cnode *)0;
470 ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
471 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
472 ** being overwritten by a replay after crashing.
474 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
475 ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
476 ** sure you never write the block without logging it.
478 ** next_zero_bit is a suggestion about the next block to try for find_forward.
479 ** when bl is rejected because it is set in a journal list bitmap, we search
480 ** for the next zero bit in the bitmap that rejected bl. Then, we return that
481 ** through next_zero_bit for find_forward to try.
483 ** Just because we return something in next_zero_bit does not mean we won't
484 ** reject it on the next call to reiserfs_in_journal
487 int reiserfs_in_journal(struct super_block *p_s_sb,
488 int bmap_nr, int bit_nr, int search_all,
489 b_blocknr_t * next_zero_bit)
491 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
492 struct reiserfs_journal_cnode *cn;
493 struct reiserfs_list_bitmap *jb;
497 *next_zero_bit = 0; /* always start this at zero. */
499 PROC_INFO_INC(p_s_sb, journal.in_journal);
500 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
501 ** if we crash before the transaction that freed it commits, this transaction won't
502 ** have committed either, and the block will never be written
505 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
506 PROC_INFO_INC(p_s_sb, journal.in_journal_bitmap);
507 jb = journal->j_list_bitmap + i;
508 if (jb->journal_list && jb->bitmaps[bmap_nr] &&
510 (unsigned long *)jb->bitmaps[bmap_nr]->
513 find_next_zero_bit((unsigned long *)
514 (jb->bitmaps[bmap_nr]->
516 p_s_sb->s_blocksize << 3,
523 bl = bmap_nr * (p_s_sb->s_blocksize << 3) + bit_nr;
524 /* is it in any old transactions? */
527 get_journal_hash_dev(p_s_sb, journal->j_list_hash_table, bl))) {
531 /* is it in the current transaction. This should never happen */
532 if ((cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, bl))) {
537 PROC_INFO_INC(p_s_sb, journal.in_journal_reusable);
542 /* insert cn into table
544 static inline void insert_journal_hash(struct reiserfs_journal_cnode **table,
545 struct reiserfs_journal_cnode *cn)
547 struct reiserfs_journal_cnode *cn_orig;
549 cn_orig = journal_hash(table, cn->sb, cn->blocknr);
555 journal_hash(table, cn->sb, cn->blocknr) = cn;
558 /* lock the current transaction */
559 static inline void lock_journal(struct super_block *p_s_sb)
561 PROC_INFO_INC(p_s_sb, journal.lock_journal);
562 down(&SB_JOURNAL(p_s_sb)->j_lock);
565 /* unlock the current transaction */
566 static inline void unlock_journal(struct super_block *p_s_sb)
568 up(&SB_JOURNAL(p_s_sb)->j_lock);
571 static inline void get_journal_list(struct reiserfs_journal_list *jl)
576 static inline void put_journal_list(struct super_block *s,
577 struct reiserfs_journal_list *jl)
579 if (jl->j_refcount < 1) {
580 reiserfs_panic(s, "trans id %lu, refcount at %d",
581 jl->j_trans_id, jl->j_refcount);
583 if (--jl->j_refcount == 0)
584 reiserfs_kfree(jl, sizeof(struct reiserfs_journal_list), s);
588 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
589 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
592 static void cleanup_freed_for_journal_list(struct super_block *p_s_sb,
593 struct reiserfs_journal_list *jl)
596 struct reiserfs_list_bitmap *jb = jl->j_list_bitmap;
598 cleanup_bitmap_list(p_s_sb, jb);
600 jl->j_list_bitmap->journal_list = NULL;
601 jl->j_list_bitmap = NULL;
604 static int journal_list_still_alive(struct super_block *s,
605 unsigned long trans_id)
607 struct reiserfs_journal *journal = SB_JOURNAL(s);
608 struct list_head *entry = &journal->j_journal_list;
609 struct reiserfs_journal_list *jl;
611 if (!list_empty(entry)) {
612 jl = JOURNAL_LIST_ENTRY(entry->next);
613 if (jl->j_trans_id <= trans_id) {
620 static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
622 char b[BDEVNAME_SIZE];
624 if (buffer_journaled(bh)) {
625 reiserfs_warning(NULL,
626 "clm-2084: pinned buffer %lu:%s sent to disk",
627 bh->b_blocknr, bdevname(bh->b_bdev, b));
630 set_buffer_uptodate(bh);
632 clear_buffer_uptodate(bh);
637 static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate)
640 set_buffer_uptodate(bh);
642 clear_buffer_uptodate(bh);
647 static void submit_logged_buffer(struct buffer_head *bh)
650 bh->b_end_io = reiserfs_end_buffer_io_sync;
651 clear_buffer_journal_new(bh);
652 clear_buffer_dirty(bh);
653 if (!test_clear_buffer_journal_test(bh))
655 if (!buffer_uptodate(bh))
657 submit_bh(WRITE, bh);
660 static void submit_ordered_buffer(struct buffer_head *bh)
663 bh->b_end_io = reiserfs_end_ordered_io;
664 clear_buffer_dirty(bh);
665 if (!buffer_uptodate(bh))
667 submit_bh(WRITE, bh);
670 static int submit_barrier_buffer(struct buffer_head *bh)
673 bh->b_end_io = reiserfs_end_ordered_io;
674 clear_buffer_dirty(bh);
675 if (!buffer_uptodate(bh))
677 return submit_bh(WRITE_BARRIER, bh);
680 static void check_barrier_completion(struct super_block *s,
681 struct buffer_head *bh)
683 if (buffer_eopnotsupp(bh)) {
684 clear_buffer_eopnotsupp(bh);
686 set_buffer_uptodate(bh);
687 set_buffer_dirty(bh);
688 sync_dirty_buffer(bh);
692 #define CHUNK_SIZE 32
693 struct buffer_chunk {
694 struct buffer_head *bh[CHUNK_SIZE];
698 static void write_chunk(struct buffer_chunk *chunk)
702 for (i = 0; i < chunk->nr; i++) {
703 submit_logged_buffer(chunk->bh[i]);
709 static void write_ordered_chunk(struct buffer_chunk *chunk)
713 for (i = 0; i < chunk->nr; i++) {
714 submit_ordered_buffer(chunk->bh[i]);
720 static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
721 spinlock_t * lock, void (fn) (struct buffer_chunk *))
724 if (chunk->nr >= CHUNK_SIZE)
726 chunk->bh[chunk->nr++] = bh;
727 if (chunk->nr >= CHUNK_SIZE) {
738 static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
739 static struct reiserfs_jh *alloc_jh(void)
741 struct reiserfs_jh *jh;
743 jh = kmalloc(sizeof(*jh), GFP_NOFS);
745 atomic_inc(&nr_reiserfs_jh);
753 * we want to free the jh when the buffer has been written
756 void reiserfs_free_jh(struct buffer_head *bh)
758 struct reiserfs_jh *jh;
762 bh->b_private = NULL;
764 list_del_init(&jh->list);
766 if (atomic_read(&nr_reiserfs_jh) <= 0)
768 atomic_dec(&nr_reiserfs_jh);
773 static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
776 struct reiserfs_jh *jh;
779 spin_lock(&j->j_dirty_buffers_lock);
780 if (!bh->b_private) {
781 spin_unlock(&j->j_dirty_buffers_lock);
785 list_del_init(&jh->list);
790 spin_lock(&j->j_dirty_buffers_lock);
791 /* buffer must be locked for __add_jh, should be able to have
792 * two adds at the same time
799 jh->jl = j->j_current_jl;
801 list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
803 list_add_tail(&jh->list, &jh->jl->j_bh_list);
805 spin_unlock(&j->j_dirty_buffers_lock);
809 int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh)
811 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
813 int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh)
815 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
818 #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
819 static int write_ordered_buffers(spinlock_t * lock,
820 struct reiserfs_journal *j,
821 struct reiserfs_journal_list *jl,
822 struct list_head *list)
824 struct buffer_head *bh;
825 struct reiserfs_jh *jh;
826 int ret = j->j_errno;
827 struct buffer_chunk chunk;
828 struct list_head tmp;
829 INIT_LIST_HEAD(&tmp);
833 while (!list_empty(list)) {
834 jh = JH_ENTRY(list->next);
837 if (test_set_buffer_locked(bh)) {
838 if (!buffer_dirty(bh)) {
839 list_del_init(&jh->list);
840 list_add(&jh->list, &tmp);
845 write_ordered_chunk(&chunk);
851 if (buffer_dirty(bh)) {
852 list_del_init(&jh->list);
853 list_add(&jh->list, &tmp);
854 add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
856 reiserfs_free_jh(bh);
861 cond_resched_lock(lock);
865 write_ordered_chunk(&chunk);
868 while (!list_empty(&tmp)) {
869 jh = JH_ENTRY(tmp.prev);
872 reiserfs_free_jh(bh);
874 if (buffer_locked(bh)) {
879 if (!buffer_uptodate(bh)) {
883 cond_resched_lock(lock);
889 static int flush_older_commits(struct super_block *s,
890 struct reiserfs_journal_list *jl)
892 struct reiserfs_journal *journal = SB_JOURNAL(s);
893 struct reiserfs_journal_list *other_jl;
894 struct reiserfs_journal_list *first_jl;
895 struct list_head *entry;
896 unsigned long trans_id = jl->j_trans_id;
897 unsigned long other_trans_id;
898 unsigned long first_trans_id;
902 * first we walk backwards to find the oldest uncommitted transation
905 entry = jl->j_list.prev;
907 other_jl = JOURNAL_LIST_ENTRY(entry);
908 if (entry == &journal->j_journal_list ||
909 atomic_read(&other_jl->j_older_commits_done))
913 entry = other_jl->j_list.prev;
916 /* if we didn't find any older uncommitted transactions, return now */
917 if (first_jl == jl) {
921 first_trans_id = first_jl->j_trans_id;
923 entry = &first_jl->j_list;
925 other_jl = JOURNAL_LIST_ENTRY(entry);
926 other_trans_id = other_jl->j_trans_id;
928 if (other_trans_id < trans_id) {
929 if (atomic_read(&other_jl->j_commit_left) != 0) {
930 flush_commit_list(s, other_jl, 0);
932 /* list we were called with is gone, return */
933 if (!journal_list_still_alive(s, trans_id))
936 /* the one we just flushed is gone, this means all
937 * older lists are also gone, so first_jl is no longer
938 * valid either. Go back to the beginning.
940 if (!journal_list_still_alive
941 (s, other_trans_id)) {
946 if (entry == &journal->j_journal_list)
954 int reiserfs_async_progress_wait(struct super_block *s)
957 struct reiserfs_journal *j = SB_JOURNAL(s);
958 if (atomic_read(&j->j_async_throttle))
959 blk_congestion_wait(WRITE, HZ / 10);
964 ** if this journal list still has commit blocks unflushed, send them to disk.
966 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
967 ** Before the commit block can by written, every other log block must be safely on disk
970 static int flush_commit_list(struct super_block *s,
971 struct reiserfs_journal_list *jl, int flushall)
975 struct buffer_head *tbh = NULL;
976 unsigned long trans_id = jl->j_trans_id;
977 struct reiserfs_journal *journal = SB_JOURNAL(s);
981 reiserfs_check_lock_depth(s, "flush_commit_list");
983 if (atomic_read(&jl->j_older_commits_done)) {
989 /* before we can put our commit blocks on disk, we have to make sure everyone older than
992 BUG_ON(jl->j_len <= 0);
993 BUG_ON(trans_id == journal->j_trans_id);
995 get_journal_list(jl);
997 if (flush_older_commits(s, jl) == 1) {
998 /* list disappeared during flush_older_commits. return */
1003 /* make sure nobody is trying to flush this one at the same time */
1004 down(&jl->j_commit_lock);
1005 if (!journal_list_still_alive(s, trans_id)) {
1006 up(&jl->j_commit_lock);
1009 BUG_ON(jl->j_trans_id == 0);
1011 /* this commit is done, exit */
1012 if (atomic_read(&(jl->j_commit_left)) <= 0) {
1014 atomic_set(&(jl->j_older_commits_done), 1);
1016 up(&jl->j_commit_lock);
1020 if (!list_empty(&jl->j_bh_list)) {
1022 write_ordered_buffers(&journal->j_dirty_buffers_lock,
1023 journal, jl, &jl->j_bh_list);
1026 BUG_ON(!list_empty(&jl->j_bh_list));
1028 * for the description block and all the log blocks, submit any buffers
1029 * that haven't already reached the disk
1031 atomic_inc(&journal->j_async_throttle);
1032 for (i = 0; i < (jl->j_len + 1); i++) {
1033 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start + i) %
1034 SB_ONDISK_JOURNAL_SIZE(s);
1035 tbh = journal_find_get_block(s, bn);
1036 if (buffer_dirty(tbh)) /* redundant, ll_rw_block() checks */
1037 ll_rw_block(SWRITE, 1, &tbh);
1040 atomic_dec(&journal->j_async_throttle);
1042 /* We're skipping the commit if there's an error */
1043 if (retval || reiserfs_is_journal_aborted(journal))
1046 /* wait on everything written so far before writing the commit
1047 * if we are in barrier mode, send the commit down now
1049 barrier = reiserfs_barrier_flush(s);
1052 lock_buffer(jl->j_commit_bh);
1053 ret = submit_barrier_buffer(jl->j_commit_bh);
1054 if (ret == -EOPNOTSUPP) {
1055 set_buffer_uptodate(jl->j_commit_bh);
1060 for (i = 0; i < (jl->j_len + 1); i++) {
1061 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
1062 (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s);
1063 tbh = journal_find_get_block(s, bn);
1064 wait_on_buffer(tbh);
1065 // since we're using ll_rw_blk above, it might have skipped over
1066 // a locked buffer. Double check here
1068 if (buffer_dirty(tbh)) /* redundant, sync_dirty_buffer() checks */
1069 sync_dirty_buffer(tbh);
1070 if (unlikely(!buffer_uptodate(tbh))) {
1071 #ifdef CONFIG_REISERFS_CHECK
1072 reiserfs_warning(s, "journal-601, buffer write failed");
1076 put_bh(tbh); /* once for journal_find_get_block */
1077 put_bh(tbh); /* once due to original getblk in do_journal_end */
1078 atomic_dec(&(jl->j_commit_left));
1081 BUG_ON(atomic_read(&(jl->j_commit_left)) != 1);
1084 /* If there was a write error in the journal - we can't commit
1085 * this transaction - it will be invalid and, if successful,
1086 * will just end up propogating the write error out to
1087 * the file system. */
1088 if (likely(!retval && !reiserfs_is_journal_aborted (journal))) {
1089 if (buffer_dirty(jl->j_commit_bh))
1091 mark_buffer_dirty(jl->j_commit_bh) ;
1092 sync_dirty_buffer(jl->j_commit_bh) ;
1095 wait_on_buffer(jl->j_commit_bh);
1097 check_barrier_completion(s, jl->j_commit_bh);
1099 /* If there was a write error in the journal - we can't commit this
1100 * transaction - it will be invalid and, if successful, will just end
1101 * up propogating the write error out to the filesystem. */
1102 if (unlikely(!buffer_uptodate(jl->j_commit_bh))) {
1103 #ifdef CONFIG_REISERFS_CHECK
1104 reiserfs_warning(s, "journal-615: buffer write failed");
1108 bforget(jl->j_commit_bh);
1109 if (journal->j_last_commit_id != 0 &&
1110 (jl->j_trans_id - journal->j_last_commit_id) != 1) {
1111 reiserfs_warning(s, "clm-2200: last commit %lu, current %lu",
1112 journal->j_last_commit_id, jl->j_trans_id);
1114 journal->j_last_commit_id = jl->j_trans_id;
1116 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1117 cleanup_freed_for_journal_list(s, jl);
1119 retval = retval ? retval : journal->j_errno;
1121 /* mark the metadata dirty */
1123 dirty_one_transaction(s, jl);
1124 atomic_dec(&(jl->j_commit_left));
1127 atomic_set(&(jl->j_older_commits_done), 1);
1129 up(&jl->j_commit_lock);
1131 put_journal_list(s, jl);
1134 reiserfs_abort(s, retval, "Journal write error in %s",
1141 ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1142 ** returns NULL if it can't find anything
1144 static struct reiserfs_journal_list *find_newer_jl_for_cn(struct
1145 reiserfs_journal_cnode
1148 struct super_block *sb = cn->sb;
1149 b_blocknr_t blocknr = cn->blocknr;
1153 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
1161 static void remove_journal_hash(struct super_block *,
1162 struct reiserfs_journal_cnode **,
1163 struct reiserfs_journal_list *, unsigned long,
1167 ** once all the real blocks have been flushed, it is safe to remove them from the
1168 ** journal list for this transaction. Aside from freeing the cnode, this also allows the
1169 ** block to be reallocated for data blocks if it had been deleted.
1171 static void remove_all_from_journal_list(struct super_block *p_s_sb,
1172 struct reiserfs_journal_list *jl,
1175 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1176 struct reiserfs_journal_cnode *cn, *last;
1177 cn = jl->j_realblock;
1179 /* which is better, to lock once around the whole loop, or
1180 ** to lock for each call to remove_journal_hash?
1183 if (cn->blocknr != 0) {
1185 reiserfs_warning(p_s_sb,
1186 "block %u, bh is %d, state %ld",
1187 cn->blocknr, cn->bh ? 1 : 0,
1191 remove_journal_hash(p_s_sb, journal->j_list_hash_table,
1192 jl, cn->blocknr, 1);
1196 free_cnode(p_s_sb, last);
1198 jl->j_realblock = NULL;
1202 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1203 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1204 ** releasing blocks in this transaction for reuse as data blocks.
1205 ** called by flush_journal_list, before it calls remove_all_from_journal_list
1208 static int _update_journal_header_block(struct super_block *p_s_sb,
1209 unsigned long offset,
1210 unsigned long trans_id)
1212 struct reiserfs_journal_header *jh;
1213 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1215 if (reiserfs_is_journal_aborted(journal))
1218 if (trans_id >= journal->j_last_flush_trans_id) {
1219 if (buffer_locked((journal->j_header_bh))) {
1220 wait_on_buffer((journal->j_header_bh));
1221 if (unlikely(!buffer_uptodate(journal->j_header_bh))) {
1222 #ifdef CONFIG_REISERFS_CHECK
1223 reiserfs_warning(p_s_sb,
1224 "journal-699: buffer write failed");
1229 journal->j_last_flush_trans_id = trans_id;
1230 journal->j_first_unflushed_offset = offset;
1231 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->
1233 jh->j_last_flush_trans_id = cpu_to_le32(trans_id);
1234 jh->j_first_unflushed_offset = cpu_to_le32(offset);
1235 jh->j_mount_id = cpu_to_le32(journal->j_mount_id);
1237 if (reiserfs_barrier_flush(p_s_sb)) {
1239 lock_buffer(journal->j_header_bh);
1240 ret = submit_barrier_buffer(journal->j_header_bh);
1241 if (ret == -EOPNOTSUPP) {
1242 set_buffer_uptodate(journal->j_header_bh);
1243 disable_barrier(p_s_sb);
1246 wait_on_buffer(journal->j_header_bh);
1247 check_barrier_completion(p_s_sb, journal->j_header_bh);
1250 set_buffer_dirty(journal->j_header_bh);
1251 sync_dirty_buffer(journal->j_header_bh);
1253 if (!buffer_uptodate(journal->j_header_bh)) {
1254 reiserfs_warning(p_s_sb,
1255 "journal-837: IO error during journal replay");
1262 static int update_journal_header_block(struct super_block *p_s_sb,
1263 unsigned long offset,
1264 unsigned long trans_id)
1266 return _update_journal_header_block(p_s_sb, offset, trans_id);
1270 ** flush any and all journal lists older than you are
1271 ** can only be called from flush_journal_list
1273 static int flush_older_journal_lists(struct super_block *p_s_sb,
1274 struct reiserfs_journal_list *jl)
1276 struct list_head *entry;
1277 struct reiserfs_journal_list *other_jl;
1278 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1279 unsigned long trans_id = jl->j_trans_id;
1281 /* we know we are the only ones flushing things, no extra race
1282 * protection is required.
1285 entry = journal->j_journal_list.next;
1287 if (entry == &journal->j_journal_list)
1289 other_jl = JOURNAL_LIST_ENTRY(entry);
1290 if (other_jl->j_trans_id < trans_id) {
1291 BUG_ON(other_jl->j_refcount <= 0);
1292 /* do not flush all */
1293 flush_journal_list(p_s_sb, other_jl, 0);
1295 /* other_jl is now deleted from the list */
1301 static void del_from_work_list(struct super_block *s,
1302 struct reiserfs_journal_list *jl)
1304 struct reiserfs_journal *journal = SB_JOURNAL(s);
1305 if (!list_empty(&jl->j_working_list)) {
1306 list_del_init(&jl->j_working_list);
1307 journal->j_num_work_lists--;
1311 /* flush a journal list, both commit and real blocks
1313 ** always set flushall to 1, unless you are calling from inside
1314 ** flush_journal_list
1316 ** IMPORTANT. This can only be called while there are no journal writers,
1317 ** and the journal is locked. That means it can only be called from
1318 ** do_journal_end, or by journal_release
1320 static int flush_journal_list(struct super_block *s,
1321 struct reiserfs_journal_list *jl, int flushall)
1323 struct reiserfs_journal_list *pjl;
1324 struct reiserfs_journal_cnode *cn, *last;
1328 struct buffer_head *saved_bh;
1329 unsigned long j_len_saved = jl->j_len;
1330 struct reiserfs_journal *journal = SB_JOURNAL(s);
1333 BUG_ON(j_len_saved <= 0);
1335 if (atomic_read(&journal->j_wcount) != 0) {
1337 "clm-2048: flush_journal_list called with wcount %d",
1338 atomic_read(&journal->j_wcount));
1340 BUG_ON(jl->j_trans_id == 0);
1342 /* if flushall == 0, the lock is already held */
1344 down(&journal->j_flush_sem);
1345 } else if (!down_trylock(&journal->j_flush_sem)) {
1350 if (j_len_saved > journal->j_trans_max) {
1352 "journal-715: flush_journal_list, length is %lu, trans id %lu\n",
1353 j_len_saved, jl->j_trans_id);
1359 /* if all the work is already done, get out of here */
1360 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1361 atomic_read(&(jl->j_commit_left)) <= 0) {
1362 goto flush_older_and_return;
1365 /* start by putting the commit list on disk. This will also flush
1366 ** the commit lists of any olders transactions
1368 flush_commit_list(s, jl, 1);
1370 if (!(jl->j_state & LIST_DIRTY)
1371 && !reiserfs_is_journal_aborted(journal))
1374 /* are we done now? */
1375 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1376 atomic_read(&(jl->j_commit_left)) <= 0) {
1377 goto flush_older_and_return;
1380 /* loop through each cnode, see if we need to write it,
1381 ** or wait on a more recent transaction, or just ignore it
1383 if (atomic_read(&(journal->j_wcount)) != 0) {
1385 "journal-844: panic journal list is flushing, wcount is not 0\n");
1387 cn = jl->j_realblock;
1392 /* blocknr of 0 is no longer in the hash, ignore it */
1393 if (cn->blocknr == 0) {
1397 /* This transaction failed commit. Don't write out to the disk */
1398 if (!(jl->j_state & LIST_DIRTY))
1401 pjl = find_newer_jl_for_cn(cn);
1402 /* the order is important here. We check pjl to make sure we
1403 ** don't clear BH_JDirty_wait if we aren't the one writing this
1406 if (!pjl && cn->bh) {
1409 /* we do this to make sure nobody releases the buffer while
1410 ** we are working with it
1414 if (buffer_journal_dirty(saved_bh)) {
1415 BUG_ON(!can_dirty(cn));
1418 } else if (can_dirty(cn)) {
1419 /* everything with !pjl && jwait should be writable */
1424 /* if someone has this block in a newer transaction, just make
1425 ** sure they are commited, and don't try writing it to disk
1428 if (atomic_read(&pjl->j_commit_left))
1429 flush_commit_list(s, pjl, 1);
1433 /* bh == NULL when the block got to disk on its own, OR,
1434 ** the block got freed in a future transaction
1436 if (saved_bh == NULL) {
1440 /* this should never happen. kupdate_one_transaction has this list
1441 ** locked while it works, so we should never see a buffer here that
1442 ** is not marked JDirty_wait
1444 if ((!was_jwait) && !buffer_locked(saved_bh)) {
1446 "journal-813: BAD! buffer %llu %cdirty %cjwait, "
1447 "not in a newer tranasction",
1448 (unsigned long long)saved_bh->
1449 b_blocknr, was_dirty ? ' ' : '!',
1450 was_jwait ? ' ' : '!');
1453 /* we inc again because saved_bh gets decremented at free_cnode */
1455 set_bit(BLOCK_NEEDS_FLUSH, &cn->state);
1456 lock_buffer(saved_bh);
1457 BUG_ON(cn->blocknr != saved_bh->b_blocknr);
1458 if (buffer_dirty(saved_bh))
1459 submit_logged_buffer(saved_bh);
1461 unlock_buffer(saved_bh);
1465 "clm-2082: Unable to flush buffer %llu in %s",
1466 (unsigned long long)saved_bh->
1467 b_blocknr, __FUNCTION__);
1473 /* we incremented this to keep others from taking the buffer head away */
1475 if (atomic_read(&(saved_bh->b_count)) < 0) {
1477 "journal-945: saved_bh->b_count < 0");
1482 cn = jl->j_realblock;
1484 if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1487 "journal-1011: cn->bh is NULL\n");
1489 wait_on_buffer(cn->bh);
1492 "journal-1012: cn->bh is NULL\n");
1494 if (unlikely(!buffer_uptodate(cn->bh))) {
1495 #ifdef CONFIG_REISERFS_CHECK
1497 "journal-949: buffer write failed\n");
1501 /* note, we must clear the JDirty_wait bit after the up to date
1502 ** check, otherwise we race against our flushpage routine
1504 BUG_ON(!test_clear_buffer_journal_dirty
1507 /* undo the inc from journal_mark_dirty */
1516 reiserfs_abort(s, -EIO,
1517 "Write error while pushing transaction to disk in %s",
1519 flush_older_and_return:
1521 /* before we can update the journal header block, we _must_ flush all
1522 ** real blocks from all older transactions to disk. This is because
1523 ** once the header block is updated, this transaction will not be
1524 ** replayed after a crash
1527 flush_older_journal_lists(s, jl);
1530 err = journal->j_errno;
1531 /* before we can remove everything from the hash tables for this
1532 ** transaction, we must make sure it can never be replayed
1534 ** since we are only called from do_journal_end, we know for sure there
1535 ** are no allocations going on while we are flushing journal lists. So,
1536 ** we only need to update the journal header block for the last list
1539 if (!err && flushall) {
1541 update_journal_header_block(s,
1542 (jl->j_start + jl->j_len +
1543 2) % SB_ONDISK_JOURNAL_SIZE(s),
1546 reiserfs_abort(s, -EIO,
1547 "Write error while updating journal header in %s",
1550 remove_all_from_journal_list(s, jl, 0);
1551 list_del_init(&jl->j_list);
1552 journal->j_num_lists--;
1553 del_from_work_list(s, jl);
1555 if (journal->j_last_flush_id != 0 &&
1556 (jl->j_trans_id - journal->j_last_flush_id) != 1) {
1557 reiserfs_warning(s, "clm-2201: last flush %lu, current %lu",
1558 journal->j_last_flush_id, jl->j_trans_id);
1560 journal->j_last_flush_id = jl->j_trans_id;
1562 /* not strictly required since we are freeing the list, but it should
1563 * help find code using dead lists later on
1566 atomic_set(&(jl->j_nonzerolen), 0);
1568 jl->j_realblock = NULL;
1569 jl->j_commit_bh = NULL;
1572 put_journal_list(s, jl);
1574 up(&journal->j_flush_sem);
1579 static int write_one_transaction(struct super_block *s,
1580 struct reiserfs_journal_list *jl,
1581 struct buffer_chunk *chunk)
1583 struct reiserfs_journal_cnode *cn;
1586 jl->j_state |= LIST_TOUCHED;
1587 del_from_work_list(s, jl);
1588 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
1592 cn = jl->j_realblock;
1594 /* if the blocknr == 0, this has been cleared from the hash,
1597 if (cn->blocknr == 0) {
1600 if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
1601 struct buffer_head *tmp_bh;
1602 /* we can race against journal_mark_freed when we try
1603 * to lock_buffer(cn->bh), so we have to inc the buffer
1604 * count, and recheck things after locking
1608 lock_buffer(tmp_bh);
1609 if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
1610 if (!buffer_journal_dirty(tmp_bh) ||
1611 buffer_journal_prepared(tmp_bh))
1613 add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
1616 /* note, cn->bh might be null now */
1617 unlock_buffer(tmp_bh);
1628 /* used by flush_commit_list */
1629 static int dirty_one_transaction(struct super_block *s,
1630 struct reiserfs_journal_list *jl)
1632 struct reiserfs_journal_cnode *cn;
1633 struct reiserfs_journal_list *pjl;
1636 jl->j_state |= LIST_DIRTY;
1637 cn = jl->j_realblock;
1639 /* look for a more recent transaction that logged this
1640 ** buffer. Only the most recent transaction with a buffer in
1641 ** it is allowed to send that buffer to disk
1643 pjl = find_newer_jl_for_cn(cn);
1644 if (!pjl && cn->blocknr && cn->bh
1645 && buffer_journal_dirty(cn->bh)) {
1646 BUG_ON(!can_dirty(cn));
1647 /* if the buffer is prepared, it will either be logged
1648 * or restored. If restored, we need to make sure
1649 * it actually gets marked dirty
1651 clear_buffer_journal_new(cn->bh);
1652 if (buffer_journal_prepared(cn->bh)) {
1653 set_buffer_journal_restore_dirty(cn->bh);
1655 set_buffer_journal_test(cn->bh);
1656 mark_buffer_dirty(cn->bh);
1664 static int kupdate_transactions(struct super_block *s,
1665 struct reiserfs_journal_list *jl,
1666 struct reiserfs_journal_list **next_jl,
1667 unsigned long *next_trans_id,
1668 int num_blocks, int num_trans)
1672 int transactions_flushed = 0;
1673 unsigned long orig_trans_id = jl->j_trans_id;
1674 struct buffer_chunk chunk;
1675 struct list_head *entry;
1676 struct reiserfs_journal *journal = SB_JOURNAL(s);
1679 down(&journal->j_flush_sem);
1680 if (!journal_list_still_alive(s, orig_trans_id)) {
1684 /* we've got j_flush_sem held, nobody is going to delete any
1685 * of these lists out from underneath us
1687 while ((num_trans && transactions_flushed < num_trans) ||
1688 (!num_trans && written < num_blocks)) {
1690 if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
1691 atomic_read(&jl->j_commit_left)
1692 || !(jl->j_state & LIST_DIRTY)) {
1693 del_from_work_list(s, jl);
1696 ret = write_one_transaction(s, jl, &chunk);
1700 transactions_flushed++;
1702 entry = jl->j_list.next;
1705 if (entry == &journal->j_journal_list) {
1708 jl = JOURNAL_LIST_ENTRY(entry);
1710 /* don't bother with older transactions */
1711 if (jl->j_trans_id <= orig_trans_id)
1715 write_chunk(&chunk);
1719 up(&journal->j_flush_sem);
1723 /* for o_sync and fsync heavy applications, they tend to use
1724 ** all the journa list slots with tiny transactions. These
1725 ** trigger lots and lots of calls to update the header block, which
1726 ** adds seeks and slows things down.
1728 ** This function tries to clear out a large chunk of the journal lists
1729 ** at once, which makes everything faster since only the newest journal
1730 ** list updates the header block
1732 static int flush_used_journal_lists(struct super_block *s,
1733 struct reiserfs_journal_list *jl)
1735 unsigned long len = 0;
1736 unsigned long cur_len;
1740 struct reiserfs_journal_list *tjl;
1741 struct reiserfs_journal_list *flush_jl;
1742 unsigned long trans_id;
1743 struct reiserfs_journal *journal = SB_JOURNAL(s);
1745 flush_jl = tjl = jl;
1747 /* in data logging mode, try harder to flush a lot of blocks */
1748 if (reiserfs_data_log(s))
1750 /* flush for 256 transactions or limit blocks, whichever comes first */
1751 for (i = 0; i < 256 && len < limit; i++) {
1752 if (atomic_read(&tjl->j_commit_left) ||
1753 tjl->j_trans_id < jl->j_trans_id) {
1756 cur_len = atomic_read(&tjl->j_nonzerolen);
1758 tjl->j_state &= ~LIST_TOUCHED;
1762 if (tjl->j_list.next == &journal->j_journal_list)
1764 tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
1766 /* try to find a group of blocks we can flush across all the
1767 ** transactions, but only bother if we've actually spanned
1768 ** across multiple lists
1770 if (flush_jl != jl) {
1771 ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
1773 flush_journal_list(s, flush_jl, 1);
1778 ** removes any nodes in table with name block and dev as bh.
1779 ** only touchs the hnext and hprev pointers.
1781 void remove_journal_hash(struct super_block *sb,
1782 struct reiserfs_journal_cnode **table,
1783 struct reiserfs_journal_list *jl,
1784 unsigned long block, int remove_freed)
1786 struct reiserfs_journal_cnode *cur;
1787 struct reiserfs_journal_cnode **head;
1789 head = &(journal_hash(table, sb, block));
1795 if (cur->blocknr == block && cur->sb == sb
1796 && (jl == NULL || jl == cur->jlist)
1797 && (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1799 cur->hnext->hprev = cur->hprev;
1802 cur->hprev->hnext = cur->hnext;
1809 if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1810 atomic_dec(&(cur->jlist->j_nonzerolen));
1818 static void free_journal_ram(struct super_block *p_s_sb)
1820 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1821 reiserfs_kfree(journal->j_current_jl,
1822 sizeof(struct reiserfs_journal_list), p_s_sb);
1823 journal->j_num_lists--;
1825 vfree(journal->j_cnode_free_orig);
1826 free_list_bitmaps(p_s_sb, journal->j_list_bitmap);
1827 free_bitmap_nodes(p_s_sb); /* must be after free_list_bitmaps */
1828 if (journal->j_header_bh) {
1829 brelse(journal->j_header_bh);
1831 /* j_header_bh is on the journal dev, make sure not to release the journal
1832 * dev until we brelse j_header_bh
1834 release_journal_dev(p_s_sb, journal);
1839 ** call on unmount. Only set error to 1 if you haven't made your way out
1840 ** of read_super() yet. Any other caller must keep error at 0.
1842 static int do_journal_release(struct reiserfs_transaction_handle *th,
1843 struct super_block *p_s_sb, int error)
1845 struct reiserfs_transaction_handle myth;
1847 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1849 /* we only want to flush out transactions if we were called with error == 0
1851 if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
1852 /* end the current trans */
1853 BUG_ON(!th->t_trans_id);
1854 do_journal_end(th, p_s_sb, 10, FLUSH_ALL);
1856 /* make sure something gets logged to force our way into the flush code */
1857 if (!journal_join(&myth, p_s_sb, 1)) {
1858 reiserfs_prepare_for_journal(p_s_sb,
1859 SB_BUFFER_WITH_SB(p_s_sb),
1861 journal_mark_dirty(&myth, p_s_sb,
1862 SB_BUFFER_WITH_SB(p_s_sb));
1863 do_journal_end(&myth, p_s_sb, 1, FLUSH_ALL);
1868 /* this also catches errors during the do_journal_end above */
1869 if (!error && reiserfs_is_journal_aborted(journal)) {
1870 memset(&myth, 0, sizeof(myth));
1871 if (!journal_join_abort(&myth, p_s_sb, 1)) {
1872 reiserfs_prepare_for_journal(p_s_sb,
1873 SB_BUFFER_WITH_SB(p_s_sb),
1875 journal_mark_dirty(&myth, p_s_sb,
1876 SB_BUFFER_WITH_SB(p_s_sb));
1877 do_journal_end(&myth, p_s_sb, 1, FLUSH_ALL);
1881 reiserfs_mounted_fs_count--;
1882 /* wait for all commits to finish */
1883 cancel_delayed_work(&SB_JOURNAL(p_s_sb)->j_work);
1884 flush_workqueue(commit_wq);
1885 if (!reiserfs_mounted_fs_count) {
1886 destroy_workqueue(commit_wq);
1890 free_journal_ram(p_s_sb);
1896 ** call on unmount. flush all journal trans, release all alloc'd ram
1898 int journal_release(struct reiserfs_transaction_handle *th,
1899 struct super_block *p_s_sb)
1901 return do_journal_release(th, p_s_sb, 0);
1905 ** only call from an error condition inside reiserfs_read_super!
1907 int journal_release_error(struct reiserfs_transaction_handle *th,
1908 struct super_block *p_s_sb)
1910 return do_journal_release(th, p_s_sb, 1);
1913 /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
1914 static int journal_compare_desc_commit(struct super_block *p_s_sb,
1915 struct reiserfs_journal_desc *desc,
1916 struct reiserfs_journal_commit *commit)
1918 if (get_commit_trans_id(commit) != get_desc_trans_id(desc) ||
1919 get_commit_trans_len(commit) != get_desc_trans_len(desc) ||
1920 get_commit_trans_len(commit) > SB_JOURNAL(p_s_sb)->j_trans_max ||
1921 get_commit_trans_len(commit) <= 0) {
1927 /* returns 0 if it did not find a description block
1928 ** returns -1 if it found a corrupt commit block
1929 ** returns 1 if both desc and commit were valid
1931 static int journal_transaction_is_valid(struct super_block *p_s_sb,
1932 struct buffer_head *d_bh,
1933 unsigned long *oldest_invalid_trans_id,
1934 unsigned long *newest_mount_id)
1936 struct reiserfs_journal_desc *desc;
1937 struct reiserfs_journal_commit *commit;
1938 struct buffer_head *c_bh;
1939 unsigned long offset;
1944 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
1945 if (get_desc_trans_len(desc) > 0
1946 && !memcmp(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8)) {
1947 if (oldest_invalid_trans_id && *oldest_invalid_trans_id
1948 && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
1949 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1950 "journal-986: transaction "
1951 "is valid returning because trans_id %d is greater than "
1952 "oldest_invalid %lu",
1953 get_desc_trans_id(desc),
1954 *oldest_invalid_trans_id);
1958 && *newest_mount_id > get_desc_mount_id(desc)) {
1959 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1960 "journal-1087: transaction "
1961 "is valid returning because mount_id %d is less than "
1962 "newest_mount_id %lu",
1963 get_desc_mount_id(desc),
1967 if (get_desc_trans_len(desc) > SB_JOURNAL(p_s_sb)->j_trans_max) {
1968 reiserfs_warning(p_s_sb,
1969 "journal-2018: Bad transaction length %d encountered, ignoring transaction",
1970 get_desc_trans_len(desc));
1973 offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
1975 /* ok, we have a journal description block, lets see if the transaction was valid */
1977 journal_bread(p_s_sb,
1978 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
1979 ((offset + get_desc_trans_len(desc) +
1980 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
1983 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
1984 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
1985 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1986 "journal_transaction_is_valid, commit offset %ld had bad "
1987 "time %d or length %d",
1989 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
1990 get_commit_trans_id(commit),
1991 get_commit_trans_len(commit));
1993 if (oldest_invalid_trans_id) {
1994 *oldest_invalid_trans_id =
1995 get_desc_trans_id(desc);
1996 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1998 "transaction_is_valid setting oldest invalid trans_id "
2000 get_desc_trans_id(desc));
2005 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2006 "journal-1006: found valid "
2007 "transaction start offset %llu, len %d id %d",
2009 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2010 get_desc_trans_len(desc),
2011 get_desc_trans_id(desc));
2018 static void brelse_array(struct buffer_head **heads, int num)
2021 for (i = 0; i < num; i++) {
2027 ** given the start, and values for the oldest acceptable transactions,
2028 ** this either reads in a replays a transaction, or returns because the transaction
2029 ** is invalid, or too old.
2031 static int journal_read_transaction(struct super_block *p_s_sb,
2032 unsigned long cur_dblock,
2033 unsigned long oldest_start,
2034 unsigned long oldest_trans_id,
2035 unsigned long newest_mount_id)
2037 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2038 struct reiserfs_journal_desc *desc;
2039 struct reiserfs_journal_commit *commit;
2040 unsigned long trans_id = 0;
2041 struct buffer_head *c_bh;
2042 struct buffer_head *d_bh;
2043 struct buffer_head **log_blocks = NULL;
2044 struct buffer_head **real_blocks = NULL;
2045 unsigned long trans_offset;
2049 d_bh = journal_bread(p_s_sb, cur_dblock);
2052 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2053 trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2054 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
2055 "journal_read_transaction, offset %llu, len %d mount_id %d",
2056 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2057 get_desc_trans_len(desc), get_desc_mount_id(desc));
2058 if (get_desc_trans_id(desc) < oldest_trans_id) {
2059 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
2060 "journal_read_trans skipping because %lu is too old",
2062 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb));
2066 if (get_desc_mount_id(desc) != newest_mount_id) {
2067 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
2068 "journal_read_trans skipping because %d is != "
2069 "newest_mount_id %lu", get_desc_mount_id(desc),
2074 c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2075 ((trans_offset + get_desc_trans_len(desc) + 1) %
2076 SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
2081 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
2082 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
2083 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2084 "journal_read_transaction, "
2085 "commit offset %llu had bad time %d or length %d",
2087 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2088 get_commit_trans_id(commit),
2089 get_commit_trans_len(commit));
2094 trans_id = get_desc_trans_id(desc);
2095 /* now we know we've got a good transaction, and it was inside the valid time ranges */
2097 reiserfs_kmalloc(get_desc_trans_len(desc) *
2098 sizeof(struct buffer_head *), GFP_NOFS, p_s_sb);
2100 reiserfs_kmalloc(get_desc_trans_len(desc) *
2101 sizeof(struct buffer_head *), GFP_NOFS, p_s_sb);
2102 if (!log_blocks || !real_blocks) {
2105 reiserfs_kfree(log_blocks,
2106 get_desc_trans_len(desc) *
2107 sizeof(struct buffer_head *), p_s_sb);
2108 reiserfs_kfree(real_blocks,
2109 get_desc_trans_len(desc) *
2110 sizeof(struct buffer_head *), p_s_sb);
2111 reiserfs_warning(p_s_sb,
2112 "journal-1169: kmalloc failed, unable to mount FS");
2115 /* get all the buffer heads */
2116 trans_half = journal_trans_half(p_s_sb->s_blocksize);
2117 for (i = 0; i < get_desc_trans_len(desc); i++) {
2119 journal_getblk(p_s_sb,
2120 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2122 i) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2123 if (i < trans_half) {
2126 le32_to_cpu(desc->j_realblock[i]));
2130 le32_to_cpu(commit->
2131 j_realblock[i - trans_half]));
2133 if (real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(p_s_sb)) {
2134 reiserfs_warning(p_s_sb,
2135 "journal-1207: REPLAY FAILURE fsck required! Block to replay is outside of filesystem");
2138 /* make sure we don't try to replay onto log or reserved area */
2139 if (is_block_in_log_or_reserved_area
2140 (p_s_sb, real_blocks[i]->b_blocknr)) {
2141 reiserfs_warning(p_s_sb,
2142 "journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block");
2144 brelse_array(log_blocks, i);
2145 brelse_array(real_blocks, i);
2148 reiserfs_kfree(log_blocks,
2149 get_desc_trans_len(desc) *
2150 sizeof(struct buffer_head *), p_s_sb);
2151 reiserfs_kfree(real_blocks,
2152 get_desc_trans_len(desc) *
2153 sizeof(struct buffer_head *), p_s_sb);
2157 /* read in the log blocks, memcpy to the corresponding real block */
2158 ll_rw_block(READ, get_desc_trans_len(desc), log_blocks);
2159 for (i = 0; i < get_desc_trans_len(desc); i++) {
2160 wait_on_buffer(log_blocks[i]);
2161 if (!buffer_uptodate(log_blocks[i])) {
2162 reiserfs_warning(p_s_sb,
2163 "journal-1212: REPLAY FAILURE fsck required! buffer write failed");
2164 brelse_array(log_blocks + i,
2165 get_desc_trans_len(desc) - i);
2166 brelse_array(real_blocks, get_desc_trans_len(desc));
2169 reiserfs_kfree(log_blocks,
2170 get_desc_trans_len(desc) *
2171 sizeof(struct buffer_head *), p_s_sb);
2172 reiserfs_kfree(real_blocks,
2173 get_desc_trans_len(desc) *
2174 sizeof(struct buffer_head *), p_s_sb);
2177 memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data,
2178 real_blocks[i]->b_size);
2179 set_buffer_uptodate(real_blocks[i]);
2180 brelse(log_blocks[i]);
2182 /* flush out the real blocks */
2183 for (i = 0; i < get_desc_trans_len(desc); i++) {
2184 set_buffer_dirty(real_blocks[i]);
2185 ll_rw_block(SWRITE, 1, real_blocks + i);
2187 for (i = 0; i < get_desc_trans_len(desc); i++) {
2188 wait_on_buffer(real_blocks[i]);
2189 if (!buffer_uptodate(real_blocks[i])) {
2190 reiserfs_warning(p_s_sb,
2191 "journal-1226: REPLAY FAILURE, fsck required! buffer write failed");
2192 brelse_array(real_blocks + i,
2193 get_desc_trans_len(desc) - i);
2196 reiserfs_kfree(log_blocks,
2197 get_desc_trans_len(desc) *
2198 sizeof(struct buffer_head *), p_s_sb);
2199 reiserfs_kfree(real_blocks,
2200 get_desc_trans_len(desc) *
2201 sizeof(struct buffer_head *), p_s_sb);
2204 brelse(real_blocks[i]);
2207 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2208 ((trans_offset + get_desc_trans_len(desc) +
2209 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2210 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2211 "journal-1095: setting journal " "start to offset %ld",
2212 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb));
2214 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
2215 journal->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2216 journal->j_last_flush_trans_id = trans_id;
2217 journal->j_trans_id = trans_id + 1;
2220 reiserfs_kfree(log_blocks,
2221 le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *),
2223 reiserfs_kfree(real_blocks,
2224 le32_to_cpu(desc->j_len) * sizeof(struct buffer_head *),
2229 /* This function reads blocks starting from block and to max_block of bufsize
2230 size (but no more than BUFNR blocks at a time). This proved to improve
2231 mounting speed on self-rebuilding raid5 arrays at least.
2232 Right now it is only used from journal code. But later we might use it
2234 Note: Do not use journal_getblk/sb_getblk functions here! */
2235 static struct buffer_head *reiserfs_breada(struct block_device *dev, int block,
2236 int bufsize, unsigned int max_block)
2238 struct buffer_head *bhlist[BUFNR];
2239 unsigned int blocks = BUFNR;
2240 struct buffer_head *bh;
2243 bh = __getblk(dev, block, bufsize);
2244 if (buffer_uptodate(bh))
2247 if (block + BUFNR > max_block) {
2248 blocks = max_block - block;
2252 for (i = 1; i < blocks; i++) {
2253 bh = __getblk(dev, block + i, bufsize);
2254 if (buffer_uptodate(bh)) {
2260 ll_rw_block(READ, j, bhlist);
2261 for (i = 1; i < j; i++)
2265 if (buffer_uptodate(bh))
2272 ** read and replay the log
2273 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2274 ** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
2276 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2278 ** On exit, it sets things up so the first transaction will work correctly.
2280 static int journal_read(struct super_block *p_s_sb)
2282 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2283 struct reiserfs_journal_desc *desc;
2284 unsigned long oldest_trans_id = 0;
2285 unsigned long oldest_invalid_trans_id = 0;
2287 unsigned long oldest_start = 0;
2288 unsigned long cur_dblock = 0;
2289 unsigned long newest_mount_id = 9;
2290 struct buffer_head *d_bh;
2291 struct reiserfs_journal_header *jh;
2292 int valid_journal_header = 0;
2293 int replay_count = 0;
2294 int continue_replay = 1;
2296 char b[BDEVNAME_SIZE];
2298 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2299 reiserfs_info(p_s_sb, "checking transaction log (%s)\n",
2300 bdevname(journal->j_dev_bd, b));
2301 start = get_seconds();
2303 /* step 1, read in the journal header block. Check the transaction it says
2304 ** is the first unflushed, and if that transaction is not valid,
2307 journal->j_header_bh = journal_bread(p_s_sb,
2308 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)
2309 + SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2310 if (!journal->j_header_bh) {
2313 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data);
2314 if (le32_to_cpu(jh->j_first_unflushed_offset) >= 0 &&
2315 le32_to_cpu(jh->j_first_unflushed_offset) <
2316 SB_ONDISK_JOURNAL_SIZE(p_s_sb)
2317 && le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
2319 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2320 le32_to_cpu(jh->j_first_unflushed_offset);
2321 oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2322 newest_mount_id = le32_to_cpu(jh->j_mount_id);
2323 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2324 "journal-1153: found in "
2325 "header: first_unflushed_offset %d, last_flushed_trans_id "
2326 "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
2327 le32_to_cpu(jh->j_last_flush_trans_id));
2328 valid_journal_header = 1;
2330 /* now, we try to read the first unflushed offset. If it is not valid,
2331 ** there is nothing more we can do, and it makes no sense to read
2332 ** through the whole log.
2335 journal_bread(p_s_sb,
2336 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2337 le32_to_cpu(jh->j_first_unflushed_offset));
2338 ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL);
2340 continue_replay = 0;
2343 goto start_log_replay;
2346 if (continue_replay && bdev_read_only(p_s_sb->s_bdev)) {
2347 reiserfs_warning(p_s_sb,
2348 "clm-2076: device is readonly, unable to replay log");
2352 /* ok, there are transactions that need to be replayed. start with the first log block, find
2353 ** all the valid transactions, and pick out the oldest.
2355 while (continue_replay
2357 (SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2358 SB_ONDISK_JOURNAL_SIZE(p_s_sb))) {
2359 /* Note that it is required for blocksize of primary fs device and journal
2360 device to be the same */
2362 reiserfs_breada(journal->j_dev_bd, cur_dblock,
2363 p_s_sb->s_blocksize,
2364 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2365 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2367 journal_transaction_is_valid(p_s_sb, d_bh,
2368 &oldest_invalid_trans_id,
2371 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2372 if (oldest_start == 0) { /* init all oldest_ values */
2373 oldest_trans_id = get_desc_trans_id(desc);
2374 oldest_start = d_bh->b_blocknr;
2375 newest_mount_id = get_desc_mount_id(desc);
2376 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2377 "journal-1179: Setting "
2378 "oldest_start to offset %llu, trans_id %lu",
2380 SB_ONDISK_JOURNAL_1st_BLOCK
2381 (p_s_sb), oldest_trans_id);
2382 } else if (oldest_trans_id > get_desc_trans_id(desc)) {
2383 /* one we just read was older */
2384 oldest_trans_id = get_desc_trans_id(desc);
2385 oldest_start = d_bh->b_blocknr;
2386 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2387 "journal-1180: Resetting "
2388 "oldest_start to offset %lu, trans_id %lu",
2390 SB_ONDISK_JOURNAL_1st_BLOCK
2391 (p_s_sb), oldest_trans_id);
2393 if (newest_mount_id < get_desc_mount_id(desc)) {
2394 newest_mount_id = get_desc_mount_id(desc);
2395 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2396 "journal-1299: Setting "
2397 "newest_mount_id to %d",
2398 get_desc_mount_id(desc));
2400 cur_dblock += get_desc_trans_len(desc) + 2;
2408 cur_dblock = oldest_start;
2409 if (oldest_trans_id) {
2410 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2411 "journal-1206: Starting replay "
2412 "from offset %llu, trans_id %lu",
2413 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2418 while (continue_replay && oldest_trans_id > 0) {
2420 journal_read_transaction(p_s_sb, cur_dblock, oldest_start,
2421 oldest_trans_id, newest_mount_id);
2424 } else if (ret != 0) {
2428 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + journal->j_start;
2430 if (cur_dblock == oldest_start)
2434 if (oldest_trans_id == 0) {
2435 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2436 "journal-1225: No valid " "transactions found");
2438 /* j_start does not get set correctly if we don't replay any transactions.
2439 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2440 ** copy the trans_id from the header
2442 if (valid_journal_header && replay_count == 0) {
2443 journal->j_start = le32_to_cpu(jh->j_first_unflushed_offset);
2444 journal->j_trans_id =
2445 le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2446 journal->j_last_flush_trans_id =
2447 le32_to_cpu(jh->j_last_flush_trans_id);
2448 journal->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
2450 journal->j_mount_id = newest_mount_id + 1;
2452 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2453 "newest_mount_id to %lu", journal->j_mount_id);
2454 journal->j_first_unflushed_offset = journal->j_start;
2455 if (replay_count > 0) {
2456 reiserfs_info(p_s_sb,
2457 "replayed %d transactions in %lu seconds\n",
2458 replay_count, get_seconds() - start);
2460 if (!bdev_read_only(p_s_sb->s_bdev) &&
2461 _update_journal_header_block(p_s_sb, journal->j_start,
2462 journal->j_last_flush_trans_id)) {
2463 /* replay failed, caller must call free_journal_ram and abort
2471 static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
2473 struct reiserfs_journal_list *jl;
2475 jl = reiserfs_kmalloc(sizeof(struct reiserfs_journal_list), GFP_NOFS,
2481 memset(jl, 0, sizeof(*jl));
2482 INIT_LIST_HEAD(&jl->j_list);
2483 INIT_LIST_HEAD(&jl->j_working_list);
2484 INIT_LIST_HEAD(&jl->j_tail_bh_list);
2485 INIT_LIST_HEAD(&jl->j_bh_list);
2486 sema_init(&jl->j_commit_lock, 1);
2487 SB_JOURNAL(s)->j_num_lists++;
2488 get_journal_list(jl);
2492 static void journal_list_init(struct super_block *p_s_sb)
2494 SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
2497 static int release_journal_dev(struct super_block *super,
2498 struct reiserfs_journal *journal)
2504 if (journal->j_dev_file != NULL) {
2505 result = filp_close(journal->j_dev_file, NULL);
2506 journal->j_dev_file = NULL;
2507 journal->j_dev_bd = NULL;
2508 } else if (journal->j_dev_bd != NULL) {
2509 result = blkdev_put(journal->j_dev_bd);
2510 journal->j_dev_bd = NULL;
2514 reiserfs_warning(super,
2515 "sh-457: release_journal_dev: Cannot release journal device: %i",
2521 static int journal_init_dev(struct super_block *super,
2522 struct reiserfs_journal *journal,
2523 const char *jdev_name)
2527 int blkdev_mode = FMODE_READ | FMODE_WRITE;
2528 char b[BDEVNAME_SIZE];
2532 journal->j_dev_bd = NULL;
2533 journal->j_dev_file = NULL;
2534 jdev = SB_ONDISK_JOURNAL_DEVICE(super) ?
2535 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
2537 if (bdev_read_only(super->s_bdev))
2538 blkdev_mode = FMODE_READ;
2540 /* there is no "jdev" option and journal is on separate device */
2541 if ((!jdev_name || !jdev_name[0])) {
2542 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode);
2543 if (IS_ERR(journal->j_dev_bd)) {
2544 result = PTR_ERR(journal->j_dev_bd);
2545 journal->j_dev_bd = NULL;
2546 reiserfs_warning(super, "sh-458: journal_init_dev: "
2547 "cannot init journal device '%s': %i",
2548 __bdevname(jdev, b), result);
2550 } else if (jdev != super->s_dev)
2551 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2555 journal->j_dev_file = filp_open(jdev_name, 0, 0);
2556 if (!IS_ERR(journal->j_dev_file)) {
2557 struct inode *jdev_inode = journal->j_dev_file->f_mapping->host;
2558 if (!S_ISBLK(jdev_inode->i_mode)) {
2559 reiserfs_warning(super, "journal_init_dev: '%s' is "
2560 "not a block device", jdev_name);
2562 release_journal_dev(super, journal);
2565 journal->j_dev_bd = I_BDEV(jdev_inode);
2566 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2567 reiserfs_info(super,
2568 "journal_init_dev: journal device: %s\n",
2569 bdevname(journal->j_dev_bd, b));
2572 result = PTR_ERR(journal->j_dev_file);
2573 journal->j_dev_file = NULL;
2574 reiserfs_warning(super,
2575 "journal_init_dev: Cannot open '%s': %i",
2582 ** must be called once on fs mount. calls journal_read for you
2584 int journal_init(struct super_block *p_s_sb, const char *j_dev_name,
2585 int old_format, unsigned int commit_max_age)
2587 int num_cnodes = SB_ONDISK_JOURNAL_SIZE(p_s_sb) * 2;
2588 struct buffer_head *bhjh;
2589 struct reiserfs_super_block *rs;
2590 struct reiserfs_journal_header *jh;
2591 struct reiserfs_journal *journal;
2592 struct reiserfs_journal_list *jl;
2593 char b[BDEVNAME_SIZE];
2595 journal = SB_JOURNAL(p_s_sb) = vmalloc(sizeof(struct reiserfs_journal));
2597 reiserfs_warning(p_s_sb,
2598 "journal-1256: unable to get memory for journal structure");
2601 memset(journal, 0, sizeof(struct reiserfs_journal));
2602 INIT_LIST_HEAD(&journal->j_bitmap_nodes);
2603 INIT_LIST_HEAD(&journal->j_prealloc_list);
2604 INIT_LIST_HEAD(&journal->j_working_list);
2605 INIT_LIST_HEAD(&journal->j_journal_list);
2606 journal->j_persistent_trans = 0;
2607 if (reiserfs_allocate_list_bitmaps(p_s_sb,
2608 journal->j_list_bitmap,
2609 SB_BMAP_NR(p_s_sb)))
2610 goto free_and_return;
2611 allocate_bitmap_nodes(p_s_sb);
2613 /* reserved for journal area support */
2614 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) = (old_format ?
2615 REISERFS_OLD_DISK_OFFSET_IN_BYTES
2616 / p_s_sb->s_blocksize +
2617 SB_BMAP_NR(p_s_sb) +
2619 REISERFS_DISK_OFFSET_IN_BYTES /
2620 p_s_sb->s_blocksize + 2);
2622 /* Sanity check to see is the standard journal fitting withing first bitmap
2623 (actual for small blocksizes) */
2624 if (!SB_ONDISK_JOURNAL_DEVICE(p_s_sb) &&
2625 (SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) +
2626 SB_ONDISK_JOURNAL_SIZE(p_s_sb) > p_s_sb->s_blocksize * 8)) {
2627 reiserfs_warning(p_s_sb,
2628 "journal-1393: journal does not fit for area "
2629 "addressed by first of bitmap blocks. It starts at "
2630 "%u and its size is %u. Block size %ld",
2631 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb),
2632 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2633 p_s_sb->s_blocksize);
2634 goto free_and_return;
2637 if (journal_init_dev(p_s_sb, journal, j_dev_name) != 0) {
2638 reiserfs_warning(p_s_sb,
2639 "sh-462: unable to initialize jornal device");
2640 goto free_and_return;
2643 rs = SB_DISK_SUPER_BLOCK(p_s_sb);
2645 /* read journal header */
2646 bhjh = journal_bread(p_s_sb,
2647 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2648 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2650 reiserfs_warning(p_s_sb,
2651 "sh-459: unable to read journal header");
2652 goto free_and_return;
2654 jh = (struct reiserfs_journal_header *)(bhjh->b_data);
2656 /* make sure that journal matches to the super block */
2657 if (is_reiserfs_jr(rs)
2658 && (le32_to_cpu(jh->jh_journal.jp_journal_magic) !=
2659 sb_jp_journal_magic(rs))) {
2660 reiserfs_warning(p_s_sb,
2661 "sh-460: journal header magic %x "
2662 "(device %s) does not match to magic found in super "
2663 "block %x", jh->jh_journal.jp_journal_magic,
2664 bdevname(journal->j_dev_bd, b),
2665 sb_jp_journal_magic(rs));
2667 goto free_and_return;
2670 journal->j_trans_max = le32_to_cpu(jh->jh_journal.jp_journal_trans_max);
2671 journal->j_max_batch = le32_to_cpu(jh->jh_journal.jp_journal_max_batch);
2672 journal->j_max_commit_age =
2673 le32_to_cpu(jh->jh_journal.jp_journal_max_commit_age);
2674 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
2676 if (journal->j_trans_max) {
2677 /* make sure these parameters are available, assign it if they are not */
2678 __u32 initial = journal->j_trans_max;
2681 if (p_s_sb->s_blocksize < 4096)
2682 ratio = 4096 / p_s_sb->s_blocksize;
2684 if (SB_ONDISK_JOURNAL_SIZE(p_s_sb) / journal->j_trans_max <
2686 journal->j_trans_max =
2687 SB_ONDISK_JOURNAL_SIZE(p_s_sb) / JOURNAL_MIN_RATIO;
2688 if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio)
2689 journal->j_trans_max =
2690 JOURNAL_TRANS_MAX_DEFAULT / ratio;
2691 if (journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio)
2692 journal->j_trans_max =
2693 JOURNAL_TRANS_MIN_DEFAULT / ratio;
2695 if (journal->j_trans_max != initial)
2696 reiserfs_warning(p_s_sb,
2697 "sh-461: journal_init: wrong transaction max size (%u). Changed to %u",
2698 initial, journal->j_trans_max);
2700 journal->j_max_batch = journal->j_trans_max *
2701 JOURNAL_MAX_BATCH_DEFAULT / JOURNAL_TRANS_MAX_DEFAULT;
2704 if (!journal->j_trans_max) {
2705 /*we have the file system was created by old version of mkreiserfs
2706 so this field contains zero value */
2707 journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT;
2708 journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT;
2709 journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE;
2711 /* for blocksize >= 4096 - max transaction size is 1024. For block size < 4096
2712 trans max size is decreased proportionally */
2713 if (p_s_sb->s_blocksize < 4096) {
2714 journal->j_trans_max /= (4096 / p_s_sb->s_blocksize);
2715 journal->j_max_batch = (journal->j_trans_max) * 9 / 10;
2719 journal->j_default_max_commit_age = journal->j_max_commit_age;
2721 if (commit_max_age != 0) {
2722 journal->j_max_commit_age = commit_max_age;
2723 journal->j_max_trans_age = commit_max_age;
2726 reiserfs_info(p_s_sb, "journal params: device %s, size %u, "
2727 "journal first block %u, max trans len %u, max batch %u, "
2728 "max commit age %u, max trans age %u\n",
2729 bdevname(journal->j_dev_bd, b),
2730 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2731 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2732 journal->j_trans_max,
2733 journal->j_max_batch,
2734 journal->j_max_commit_age, journal->j_max_trans_age);
2738 journal->j_list_bitmap_index = 0;
2739 journal_list_init(p_s_sb);
2741 memset(journal->j_list_hash_table, 0,
2742 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
2744 INIT_LIST_HEAD(&journal->j_dirty_buffers);
2745 spin_lock_init(&journal->j_dirty_buffers_lock);
2747 journal->j_start = 0;
2749 journal->j_len_alloc = 0;
2750 atomic_set(&(journal->j_wcount), 0);
2751 atomic_set(&(journal->j_async_throttle), 0);
2752 journal->j_bcount = 0;
2753 journal->j_trans_start_time = 0;
2754 journal->j_last = NULL;
2755 journal->j_first = NULL;
2756 init_waitqueue_head(&(journal->j_join_wait));
2757 sema_init(&journal->j_lock, 1);
2758 sema_init(&journal->j_flush_sem, 1);
2760 journal->j_trans_id = 10;
2761 journal->j_mount_id = 10;
2762 journal->j_state = 0;
2763 atomic_set(&(journal->j_jlock), 0);
2764 journal->j_cnode_free_list = allocate_cnodes(num_cnodes);
2765 journal->j_cnode_free_orig = journal->j_cnode_free_list;
2766 journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0;
2767 journal->j_cnode_used = 0;
2768 journal->j_must_wait = 0;
2770 if (journal->j_cnode_free == 0) {
2771 reiserfs_warning(p_s_sb, "journal-2004: Journal cnode memory "
2772 "allocation failed (%ld bytes). Journal is "
2773 "too large for available memory. Usually "
2774 "this is due to a journal that is too large.",
2775 sizeof (struct reiserfs_journal_cnode) * num_cnodes);
2776 goto free_and_return;
2779 init_journal_hash(p_s_sb);
2780 jl = journal->j_current_jl;
2781 jl->j_list_bitmap = get_list_bitmap(p_s_sb, jl);
2782 if (!jl->j_list_bitmap) {
2783 reiserfs_warning(p_s_sb,
2784 "journal-2005, get_list_bitmap failed for journal list 0");
2785 goto free_and_return;
2787 if (journal_read(p_s_sb) < 0) {
2788 reiserfs_warning(p_s_sb, "Replay Failure, unable to mount");
2789 goto free_and_return;
2792 reiserfs_mounted_fs_count++;
2793 if (reiserfs_mounted_fs_count <= 1)
2794 commit_wq = create_workqueue("reiserfs");
2796 INIT_WORK(&journal->j_work, flush_async_commits, p_s_sb);
2799 free_journal_ram(p_s_sb);
2804 ** test for a polite end of the current transaction. Used by file_write, and should
2805 ** be used by delete to make sure they don't write more than can fit inside a single
2808 int journal_transaction_should_end(struct reiserfs_transaction_handle *th,
2811 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2812 time_t now = get_seconds();
2813 /* cannot restart while nested */
2814 BUG_ON(!th->t_trans_id);
2815 if (th->t_refcount > 1)
2817 if (journal->j_must_wait > 0 ||
2818 (journal->j_len_alloc + new_alloc) >= journal->j_max_batch ||
2819 atomic_read(&(journal->j_jlock)) ||
2820 (now - journal->j_trans_start_time) > journal->j_max_trans_age ||
2821 journal->j_cnode_free < (journal->j_trans_max * 3)) {
2827 /* this must be called inside a transaction, and requires the
2828 ** kernel_lock to be held
2830 void reiserfs_block_writes(struct reiserfs_transaction_handle *th)
2832 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2833 BUG_ON(!th->t_trans_id);
2834 journal->j_must_wait = 1;
2835 set_bit(J_WRITERS_BLOCKED, &journal->j_state);
2839 /* this must be called without a transaction started, and does not
2842 void reiserfs_allow_writes(struct super_block *s)
2844 struct reiserfs_journal *journal = SB_JOURNAL(s);
2845 clear_bit(J_WRITERS_BLOCKED, &journal->j_state);
2846 wake_up(&journal->j_join_wait);
2849 /* this must be called without a transaction started, and does not
2852 void reiserfs_wait_on_write_block(struct super_block *s)
2854 struct reiserfs_journal *journal = SB_JOURNAL(s);
2855 wait_event(journal->j_join_wait,
2856 !test_bit(J_WRITERS_BLOCKED, &journal->j_state));
2859 static void queue_log_writer(struct super_block *s)
2862 struct reiserfs_journal *journal = SB_JOURNAL(s);
2863 set_bit(J_WRITERS_QUEUED, &journal->j_state);
2866 * we don't want to use wait_event here because
2867 * we only want to wait once.
2869 init_waitqueue_entry(&wait, current);
2870 add_wait_queue(&journal->j_join_wait, &wait);
2871 set_current_state(TASK_UNINTERRUPTIBLE);
2872 if (test_bit(J_WRITERS_QUEUED, &journal->j_state))
2874 current->state = TASK_RUNNING;
2875 remove_wait_queue(&journal->j_join_wait, &wait);
2878 static void wake_queued_writers(struct super_block *s)
2880 struct reiserfs_journal *journal = SB_JOURNAL(s);
2881 if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state))
2882 wake_up(&journal->j_join_wait);
2885 static void let_transaction_grow(struct super_block *sb, unsigned long trans_id)
2887 struct reiserfs_journal *journal = SB_JOURNAL(sb);
2888 unsigned long bcount = journal->j_bcount;
2890 schedule_timeout_uninterruptible(1);
2891 journal->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2892 while ((atomic_read(&journal->j_wcount) > 0 ||
2893 atomic_read(&journal->j_jlock)) &&
2894 journal->j_trans_id == trans_id) {
2895 queue_log_writer(sb);
2897 if (journal->j_trans_id != trans_id)
2899 if (bcount == journal->j_bcount)
2901 bcount = journal->j_bcount;
2905 /* join == true if you must join an existing transaction.
2906 ** join == false if you can deal with waiting for others to finish
2908 ** this will block until the transaction is joinable. send the number of blocks you
2909 ** expect to use in nblocks.
2911 static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
2912 struct super_block *p_s_sb, unsigned long nblocks,
2915 time_t now = get_seconds();
2917 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2918 struct reiserfs_transaction_handle myth;
2919 int sched_count = 0;
2922 reiserfs_check_lock_depth(p_s_sb, "journal_begin");
2923 if (nblocks > journal->j_trans_max)
2926 PROC_INFO_INC(p_s_sb, journal.journal_being);
2927 /* set here for journal_join */
2929 th->t_super = p_s_sb;
2932 lock_journal(p_s_sb);
2933 if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted(journal)) {
2934 unlock_journal(p_s_sb);
2935 retval = journal->j_errno;
2938 journal->j_bcount++;
2940 if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) {
2941 unlock_journal(p_s_sb);
2942 reiserfs_wait_on_write_block(p_s_sb);
2943 PROC_INFO_INC(p_s_sb, journal.journal_relock_writers);
2946 now = get_seconds();
2948 /* if there is no room in the journal OR
2949 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
2950 ** we don't sleep if there aren't other writers
2953 if ((!join && journal->j_must_wait > 0) ||
2955 && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch)
2956 || (!join && atomic_read(&journal->j_wcount) > 0
2957 && journal->j_trans_start_time > 0
2958 && (now - journal->j_trans_start_time) >
2959 journal->j_max_trans_age) || (!join
2960 && atomic_read(&journal->j_jlock))
2961 || (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) {
2963 old_trans_id = journal->j_trans_id;
2964 unlock_journal(p_s_sb); /* allow others to finish this transaction */
2966 if (!join && (journal->j_len_alloc + nblocks + 2) >=
2967 journal->j_max_batch &&
2968 ((journal->j_len + nblocks + 2) * 100) <
2969 (journal->j_len_alloc * 75)) {
2970 if (atomic_read(&journal->j_wcount) > 10) {
2972 queue_log_writer(p_s_sb);
2976 /* don't mess with joining the transaction if all we have to do is
2977 * wait for someone else to do a commit
2979 if (atomic_read(&journal->j_jlock)) {
2980 while (journal->j_trans_id == old_trans_id &&
2981 atomic_read(&journal->j_jlock)) {
2982 queue_log_writer(p_s_sb);
2986 retval = journal_join(&myth, p_s_sb, 1);
2990 /* someone might have ended the transaction while we joined */
2991 if (old_trans_id != journal->j_trans_id) {
2992 retval = do_journal_end(&myth, p_s_sb, 1, 0);
2994 retval = do_journal_end(&myth, p_s_sb, 1, COMMIT_NOW);
3000 PROC_INFO_INC(p_s_sb, journal.journal_relock_wcount);
3003 /* we are the first writer, set trans_id */
3004 if (journal->j_trans_start_time == 0) {
3005 journal->j_trans_start_time = get_seconds();
3007 atomic_inc(&(journal->j_wcount));
3008 journal->j_len_alloc += nblocks;
3009 th->t_blocks_logged = 0;
3010 th->t_blocks_allocated = nblocks;
3011 th->t_trans_id = journal->j_trans_id;
3012 unlock_journal(p_s_sb);
3013 INIT_LIST_HEAD(&th->t_list);
3018 memset(th, 0, sizeof(*th));
3019 /* Re-set th->t_super, so we can properly keep track of how many
3020 * persistent transactions there are. We need to do this so if this
3021 * call is part of a failed restart_transaction, we can free it later */
3022 th->t_super = p_s_sb;
3026 struct reiserfs_transaction_handle *reiserfs_persistent_transaction(struct
3032 struct reiserfs_transaction_handle *th;
3034 /* if we're nesting into an existing transaction. It will be
3035 ** persistent on its own
3037 if (reiserfs_transaction_running(s)) {
3038 th = current->journal_info;
3040 if (th->t_refcount < 2) {
3045 th = reiserfs_kmalloc(sizeof(struct reiserfs_transaction_handle),
3049 ret = journal_begin(th, s, nblocks);
3051 reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle),
3056 SB_JOURNAL(s)->j_persistent_trans++;
3060 int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th)
3062 struct super_block *s = th->t_super;
3065 ret = journal_end(th, th->t_super, th->t_blocks_allocated);
3068 if (th->t_refcount == 0) {
3069 SB_JOURNAL(s)->j_persistent_trans--;
3070 reiserfs_kfree(th, sizeof(struct reiserfs_transaction_handle),
3076 static int journal_join(struct reiserfs_transaction_handle *th,
3077 struct super_block *p_s_sb, unsigned long nblocks)
3079 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3081 /* this keeps do_journal_end from NULLing out the current->journal_info
3084 th->t_handle_save = cur_th;
3085 if (cur_th && cur_th->t_refcount > 1) {
3088 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_JOIN);
3091 int journal_join_abort(struct reiserfs_transaction_handle *th,
3092 struct super_block *p_s_sb, unsigned long nblocks)
3094 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3096 /* this keeps do_journal_end from NULLing out the current->journal_info
3099 th->t_handle_save = cur_th;
3100 if (cur_th && cur_th->t_refcount > 1) {
3103 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_ABORT);
3106 int journal_begin(struct reiserfs_transaction_handle *th,
3107 struct super_block *p_s_sb, unsigned long nblocks)
3109 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3112 th->t_handle_save = NULL;
3114 /* we are nesting into the current transaction */
3115 if (cur_th->t_super == p_s_sb) {
3116 BUG_ON(!cur_th->t_refcount);
3117 cur_th->t_refcount++;
3118 memcpy(th, cur_th, sizeof(*th));
3119 if (th->t_refcount <= 1)
3120 reiserfs_warning(p_s_sb,
3121 "BAD: refcount <= 1, but journal_info != 0");
3124 /* we've ended up with a handle from a different filesystem.
3125 ** save it and restore on journal_end. This should never
3128 reiserfs_warning(p_s_sb,
3129 "clm-2100: nesting info a different FS");
3130 th->t_handle_save = current->journal_info;
3131 current->journal_info = th;
3134 current->journal_info = th;
3136 ret = do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_REG);
3137 if (current->journal_info != th)
3140 /* I guess this boils down to being the reciprocal of clm-2100 above.
3141 * If do_journal_begin_r fails, we need to put it back, since journal_end
3142 * won't be called to do it. */
3144 current->journal_info = th->t_handle_save;
3146 BUG_ON(!th->t_refcount);
3152 ** puts bh into the current transaction. If it was already there, reorders removes the
3153 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
3155 ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
3156 ** transaction is committed.
3158 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
3160 int journal_mark_dirty(struct reiserfs_transaction_handle *th,
3161 struct super_block *p_s_sb, struct buffer_head *bh)
3163 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3164 struct reiserfs_journal_cnode *cn = NULL;
3165 int count_already_incd = 0;
3167 BUG_ON(!th->t_trans_id);
3169 PROC_INFO_INC(p_s_sb, journal.mark_dirty);
3170 if (th->t_trans_id != journal->j_trans_id) {
3171 reiserfs_panic(th->t_super,
3172 "journal-1577: handle trans id %ld != current trans id %ld\n",
3173 th->t_trans_id, journal->j_trans_id);
3178 prepared = test_clear_buffer_journal_prepared(bh);
3179 clear_buffer_journal_restore_dirty(bh);
3180 /* already in this transaction, we are done */
3181 if (buffer_journaled(bh)) {
3182 PROC_INFO_INC(p_s_sb, journal.mark_dirty_already);
3186 /* this must be turned into a panic instead of a warning. We can't allow
3187 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
3188 ** could get to disk too early. NOT GOOD.
3190 if (!prepared || buffer_dirty(bh)) {
3191 reiserfs_warning(p_s_sb, "journal-1777: buffer %llu bad state "
3192 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
3193 (unsigned long long)bh->b_blocknr,
3194 prepared ? ' ' : '!',
3195 buffer_locked(bh) ? ' ' : '!',
3196 buffer_dirty(bh) ? ' ' : '!',
3197 buffer_journal_dirty(bh) ? ' ' : '!');
3200 if (atomic_read(&(journal->j_wcount)) <= 0) {
3201 reiserfs_warning(p_s_sb,
3202 "journal-1409: journal_mark_dirty returning because j_wcount was %d",
3203 atomic_read(&(journal->j_wcount)));
3206 /* this error means I've screwed up, and we've overflowed the transaction.
3207 ** Nothing can be done here, except make the FS readonly or panic.
3209 if (journal->j_len >= journal->j_trans_max) {
3210 reiserfs_panic(th->t_super,
3211 "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n",
3215 if (buffer_journal_dirty(bh)) {
3216 count_already_incd = 1;
3217 PROC_INFO_INC(p_s_sb, journal.mark_dirty_notjournal);
3218 clear_buffer_journal_dirty(bh);
3221 if (journal->j_len > journal->j_len_alloc) {
3222 journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT;
3225 set_buffer_journaled(bh);
3227 /* now put this guy on the end */
3229 cn = get_cnode(p_s_sb);
3231 reiserfs_panic(p_s_sb, "get_cnode failed!\n");
3234 if (th->t_blocks_logged == th->t_blocks_allocated) {
3235 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT;
3236 journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT;
3238 th->t_blocks_logged++;
3242 cn->blocknr = bh->b_blocknr;
3245 insert_journal_hash(journal->j_hash_table, cn);
3246 if (!count_already_incd) {
3251 cn->prev = journal->j_last;
3253 if (journal->j_last) {
3254 journal->j_last->next = cn;
3255 journal->j_last = cn;
3257 journal->j_first = cn;
3258 journal->j_last = cn;
3263 int journal_end(struct reiserfs_transaction_handle *th,
3264 struct super_block *p_s_sb, unsigned long nblocks)
3266 if (!current->journal_info && th->t_refcount > 1)
3267 reiserfs_warning(p_s_sb, "REISER-NESTING: th NULL, refcount %d",
3270 if (!th->t_trans_id) {
3276 if (th->t_refcount > 0) {
3277 struct reiserfs_transaction_handle *cur_th =
3278 current->journal_info;
3280 /* we aren't allowed to close a nested transaction on a different
3281 ** filesystem from the one in the task struct
3283 if (cur_th->t_super != th->t_super)
3287 memcpy(current->journal_info, th, sizeof(*th));
3292 return do_journal_end(th, p_s_sb, nblocks, 0);
3296 /* removes from the current transaction, relsing and descrementing any counters.
3297 ** also files the removed buffer directly onto the clean list
3299 ** called by journal_mark_freed when a block has been deleted
3301 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
3303 static int remove_from_transaction(struct super_block *p_s_sb,
3304 b_blocknr_t blocknr, int already_cleaned)
3306 struct buffer_head *bh;
3307 struct reiserfs_journal_cnode *cn;
3308 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3311 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr);
3312 if (!cn || !cn->bh) {
3317 cn->prev->next = cn->next;
3320 cn->next->prev = cn->prev;
3322 if (cn == journal->j_first) {
3323 journal->j_first = cn->next;
3325 if (cn == journal->j_last) {
3326 journal->j_last = cn->prev;
3329 remove_journal_hash(p_s_sb, journal->j_hash_table, NULL,
3331 clear_buffer_journaled(bh); /* don't log this one */
3333 if (!already_cleaned) {
3334 clear_buffer_journal_dirty(bh);
3335 clear_buffer_dirty(bh);
3336 clear_buffer_journal_test(bh);
3338 if (atomic_read(&(bh->b_count)) < 0) {
3339 reiserfs_warning(p_s_sb,
3340 "journal-1752: remove from trans, b_count < 0");
3345 journal->j_len_alloc--;
3346 free_cnode(p_s_sb, cn);
3351 ** for any cnode in a journal list, it can only be dirtied of all the
3352 ** transactions that include it are commited to disk.
3353 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
3354 ** and 0 if you aren't
3356 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3357 ** blocks for a given transaction on disk
3360 static int can_dirty(struct reiserfs_journal_cnode *cn)
3362 struct super_block *sb = cn->sb;
3363 b_blocknr_t blocknr = cn->blocknr;
3364 struct reiserfs_journal_cnode *cur = cn->hprev;
3367 /* first test hprev. These are all newer than cn, so any node here
3368 ** with the same block number and dev means this node can't be sent
3369 ** to disk right now.
3371 while (cur && can_dirty) {
3372 if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
3373 cur->blocknr == blocknr) {
3378 /* then test hnext. These are all older than cn. As long as they
3379 ** are committed to the log, it is safe to write cn to disk
3382 while (cur && can_dirty) {
3383 if (cur->jlist && cur->jlist->j_len > 0 &&
3384 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
3385 cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
3393 /* syncs the commit blocks, but does not force the real buffers to disk
3394 ** will wait until the current transaction is done/commited before returning
3396 int journal_end_sync(struct reiserfs_transaction_handle *th,
3397 struct super_block *p_s_sb, unsigned long nblocks)
3399 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3401 BUG_ON(!th->t_trans_id);
3402 /* you can sync while nested, very, very bad */
3403 if (th->t_refcount > 1) {
3406 if (journal->j_len == 0) {
3407 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb),
3409 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb));
3411 return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT);
3415 ** writeback the pending async commits to disk
3417 static void flush_async_commits(void *p)
3419 struct super_block *p_s_sb = p;
3420 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3421 struct reiserfs_journal_list *jl;
3422 struct list_head *entry;
3425 if (!list_empty(&journal->j_journal_list)) {
3426 /* last entry is the youngest, commit it and you get everything */
3427 entry = journal->j_journal_list.prev;
3428 jl = JOURNAL_LIST_ENTRY(entry);
3429 flush_commit_list(p_s_sb, jl, 1);
3433 * this is a little racey, but there's no harm in missing
3434 * the filemap_fdata_write
3436 if (!atomic_read(&journal->j_async_throttle)
3437 && !reiserfs_is_journal_aborted(journal)) {
3438 atomic_inc(&journal->j_async_throttle);
3439 filemap_fdatawrite(p_s_sb->s_bdev->bd_inode->i_mapping);
3440 atomic_dec(&journal->j_async_throttle);
3445 ** flushes any old transactions to disk
3446 ** ends the current transaction if it is too old
3448 int reiserfs_flush_old_commits(struct super_block *p_s_sb)
3451 struct reiserfs_transaction_handle th;
3452 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3454 now = get_seconds();
3455 /* safety check so we don't flush while we are replaying the log during
3458 if (list_empty(&journal->j_journal_list)) {
3462 /* check the current transaction. If there are no writers, and it is
3463 * too old, finish it, and force the commit blocks to disk
3465 if (atomic_read(&journal->j_wcount) <= 0 &&
3466 journal->j_trans_start_time > 0 &&
3467 journal->j_len > 0 &&
3468 (now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3469 if (!journal_join(&th, p_s_sb, 1)) {
3470 reiserfs_prepare_for_journal(p_s_sb,
3471 SB_BUFFER_WITH_SB(p_s_sb),
3473 journal_mark_dirty(&th, p_s_sb,
3474 SB_BUFFER_WITH_SB(p_s_sb));
3476 /* we're only being called from kreiserfsd, it makes no sense to do
3477 ** an async commit so that kreiserfsd can do it later
3479 do_journal_end(&th, p_s_sb, 1, COMMIT_NOW | WAIT);
3482 return p_s_sb->s_dirt;
3486 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
3488 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
3489 ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3490 ** flushes the commit list and returns 0.
3492 ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
3494 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3496 static int check_journal_end(struct reiserfs_transaction_handle *th,
3497 struct super_block *p_s_sb, unsigned long nblocks,
3502 int flush = flags & FLUSH_ALL;
3503 int commit_now = flags & COMMIT_NOW;
3504 int wait_on_commit = flags & WAIT;
3505 struct reiserfs_journal_list *jl;
3506 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3508 BUG_ON(!th->t_trans_id);
3510 if (th->t_trans_id != journal->j_trans_id) {
3511 reiserfs_panic(th->t_super,
3512 "journal-1577: handle trans id %ld != current trans id %ld\n",
3513 th->t_trans_id, journal->j_trans_id);
3516 journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged);
3517 if (atomic_read(&(journal->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3518 atomic_dec(&(journal->j_wcount));
3521 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
3522 ** will be dealt with by next transaction that actually writes something, but should be taken
3523 ** care of in this trans
3525 if (journal->j_len == 0) {
3528 /* if wcount > 0, and we are called to with flush or commit_now,
3529 ** we wait on j_join_wait. We will wake up when the last writer has
3530 ** finished the transaction, and started it on its way to the disk.
3531 ** Then, we flush the commit or journal list, and just return 0
3532 ** because the rest of journal end was already done for this transaction.
3534 if (atomic_read(&(journal->j_wcount)) > 0) {
3535 if (flush || commit_now) {
3538 jl = journal->j_current_jl;
3539 trans_id = jl->j_trans_id;
3541 jl->j_state |= LIST_COMMIT_PENDING;
3542 atomic_set(&(journal->j_jlock), 1);
3544 journal->j_next_full_flush = 1;
3546 unlock_journal(p_s_sb);
3548 /* sleep while the current transaction is still j_jlocked */
3549 while (journal->j_trans_id == trans_id) {
3550 if (atomic_read(&journal->j_jlock)) {
3551 queue_log_writer(p_s_sb);
3553 lock_journal(p_s_sb);
3554 if (journal->j_trans_id == trans_id) {
3555 atomic_set(&(journal->j_jlock),
3558 unlock_journal(p_s_sb);
3561 if (journal->j_trans_id == trans_id) {
3565 && journal_list_still_alive(p_s_sb, trans_id)
3566 && wait_on_commit) {
3567 flush_commit_list(p_s_sb, jl, 1);
3571 unlock_journal(p_s_sb);
3575 /* deal with old transactions where we are the last writers */
3576 now = get_seconds();
3577 if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3579 journal->j_next_async_flush = 1;
3581 /* don't batch when someone is waiting on j_join_wait */
3582 /* don't batch when syncing the commit or flushing the whole trans */
3583 if (!(journal->j_must_wait > 0) && !(atomic_read(&(journal->j_jlock)))
3584 && !flush && !commit_now && (journal->j_len < journal->j_max_batch)
3585 && journal->j_len_alloc < journal->j_max_batch
3586 && journal->j_cnode_free > (journal->j_trans_max * 3)) {
3587 journal->j_bcount++;
3588 unlock_journal(p_s_sb);
3592 if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
3593 reiserfs_panic(p_s_sb,
3594 "journal-003: journal_end: j_start (%ld) is too high\n",
3601 ** Does all the work that makes deleting blocks safe.
3602 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3605 ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3606 ** before this transaction has finished.
3608 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3609 ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3610 ** the block can't be reallocated yet.
3612 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3614 int journal_mark_freed(struct reiserfs_transaction_handle *th,
3615 struct super_block *p_s_sb, b_blocknr_t blocknr)
3617 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3618 struct reiserfs_journal_cnode *cn = NULL;
3619 struct buffer_head *bh = NULL;
3620 struct reiserfs_list_bitmap *jb = NULL;
3622 BUG_ON(!th->t_trans_id);
3624 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr);
3629 /* if it is journal new, we just remove it from this transaction */
3630 if (bh && buffer_journal_new(bh)) {
3631 clear_buffer_journal_new(bh);
3632 clear_prepared_bits(bh);
3633 reiserfs_clean_and_file_buffer(bh);
3634 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned);
3636 /* set the bit for this block in the journal bitmap for this transaction */
3637 jb = journal->j_current_jl->j_list_bitmap;
3639 reiserfs_panic(p_s_sb,
3640 "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n");
3642 set_bit_in_list_bitmap(p_s_sb, blocknr, jb);
3644 /* Note, the entire while loop is not allowed to schedule. */
3647 clear_prepared_bits(bh);
3648 reiserfs_clean_and_file_buffer(bh);
3650 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned);
3652 /* find all older transactions with this block, make sure they don't try to write it out */
3653 cn = get_journal_hash_dev(p_s_sb, journal->j_list_hash_table,
3656 if (p_s_sb == cn->sb && blocknr == cn->blocknr) {
3657 set_bit(BLOCK_FREED, &cn->state);
3660 /* remove_from_transaction will brelse the buffer if it was
3661 ** in the current trans
3663 clear_buffer_journal_dirty(cn->
3665 clear_buffer_dirty(cn->bh);
3666 clear_buffer_journal_test(cn->
3671 (&(cn->bh->b_count)) < 0) {
3672 reiserfs_warning(p_s_sb,
3673 "journal-2138: cn->bh->b_count < 0");
3676 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3689 put_bh(bh); /* get_hash grabs the buffer */
3690 if (atomic_read(&(bh->b_count)) < 0) {
3691 reiserfs_warning(p_s_sb,
3692 "journal-2165: bh->b_count < 0");
3698 void reiserfs_update_inode_transaction(struct inode *inode)
3700 struct reiserfs_journal *journal = SB_JOURNAL(inode->i_sb);
3701 REISERFS_I(inode)->i_jl = journal->j_current_jl;
3702 REISERFS_I(inode)->i_trans_id = journal->j_trans_id;
3706 * returns -1 on error, 0 if no commits/barriers were done and 1
3707 * if a transaction was actually committed and the barrier was done
3709 static int __commit_trans_jl(struct inode *inode, unsigned long id,
3710 struct reiserfs_journal_list *jl)
3712 struct reiserfs_transaction_handle th;
3713 struct super_block *sb = inode->i_sb;
3714 struct reiserfs_journal *journal = SB_JOURNAL(sb);
3717 /* is it from the current transaction, or from an unknown transaction? */
3718 if (id == journal->j_trans_id) {
3719 jl = journal->j_current_jl;
3720 /* try to let other writers come in and grow this transaction */
3721 let_transaction_grow(sb, id);
3722 if (journal->j_trans_id != id) {
3723 goto flush_commit_only;
3726 ret = journal_begin(&th, sb, 1);
3730 /* someone might have ended this transaction while we joined */
3731 if (journal->j_trans_id != id) {
3732 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
3734 journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb));
3735 ret = journal_end(&th, sb, 1);
3736 goto flush_commit_only;
3739 ret = journal_end_sync(&th, sb, 1);
3744 /* this gets tricky, we have to make sure the journal list in
3745 * the inode still exists. We know the list is still around
3746 * if we've got a larger transaction id than the oldest list
3749 if (journal_list_still_alive(inode->i_sb, id)) {
3751 * we only set ret to 1 when we know for sure
3752 * the barrier hasn't been started yet on the commit
3755 if (atomic_read(&jl->j_commit_left) > 1)
3757 flush_commit_list(sb, jl, 1);
3758 if (journal->j_errno)
3759 ret = journal->j_errno;
3762 /* otherwise the list is gone, and long since committed */
3766 int reiserfs_commit_for_inode(struct inode *inode)
3768 unsigned long id = REISERFS_I(inode)->i_trans_id;
3769 struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
3771 /* for the whole inode, assume unset id means it was
3772 * changed in the current transaction. More conservative
3775 reiserfs_update_inode_transaction(inode);
3776 id = REISERFS_I(inode)->i_trans_id;
3777 /* jl will be updated in __commit_trans_jl */
3780 return __commit_trans_jl(inode, id, jl);
3783 void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb,
3784 struct buffer_head *bh)
3786 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3787 PROC_INFO_INC(p_s_sb, journal.restore_prepared);
3791 if (test_clear_buffer_journal_restore_dirty(bh) &&
3792 buffer_journal_dirty(bh)) {
3793 struct reiserfs_journal_cnode *cn;
3794 cn = get_journal_hash_dev(p_s_sb,
3795 journal->j_list_hash_table,
3797 if (cn && can_dirty(cn)) {
3798 set_buffer_journal_test(bh);
3799 mark_buffer_dirty(bh);
3802 clear_buffer_journal_prepared(bh);
3805 extern struct tree_balance *cur_tb;
3807 ** before we can change a metadata block, we have to make sure it won't
3808 ** be written to disk while we are altering it. So, we must:
3813 int reiserfs_prepare_for_journal(struct super_block *p_s_sb,
3814 struct buffer_head *bh, int wait)
3816 PROC_INFO_INC(p_s_sb, journal.prepare);
3818 if (test_set_buffer_locked(bh)) {
3823 set_buffer_journal_prepared(bh);
3824 if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
3825 clear_buffer_journal_test(bh);
3826 set_buffer_journal_restore_dirty(bh);
3832 static void flush_old_journal_lists(struct super_block *s)
3834 struct reiserfs_journal *journal = SB_JOURNAL(s);
3835 struct reiserfs_journal_list *jl;
3836 struct list_head *entry;
3837 time_t now = get_seconds();
3839 while (!list_empty(&journal->j_journal_list)) {
3840 entry = journal->j_journal_list.next;
3841 jl = JOURNAL_LIST_ENTRY(entry);
3842 /* this check should always be run, to send old lists to disk */
3843 if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4))) {
3844 flush_used_journal_lists(s, jl);
3852 ** long and ugly. If flush, will not return until all commit
3853 ** blocks and all real buffers in the trans are on disk.
3854 ** If no_async, won't return until all commit blocks are on disk.
3856 ** keep reading, there are comments as you go along
3858 ** If the journal is aborted, we just clean up. Things like flushing
3859 ** journal lists, etc just won't happen.
3861 static int do_journal_end(struct reiserfs_transaction_handle *th,
3862 struct super_block *p_s_sb, unsigned long nblocks,
3865 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3866 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
3867 struct reiserfs_journal_cnode *last_cn = NULL;
3868 struct reiserfs_journal_desc *desc;
3869 struct reiserfs_journal_commit *commit;
3870 struct buffer_head *c_bh; /* commit bh */
3871 struct buffer_head *d_bh; /* desc bh */
3872 int cur_write_start = 0; /* start index of current log write */
3875 int flush = flags & FLUSH_ALL;
3876 int wait_on_commit = flags & WAIT;
3877 struct reiserfs_journal_list *jl, *temp_jl;
3878 struct list_head *entry, *safe;
3879 unsigned long jindex;
3880 unsigned long commit_trans_id;
3883 BUG_ON(th->t_refcount > 1);
3884 BUG_ON(!th->t_trans_id);
3887 current->journal_info = th->t_handle_save;
3888 reiserfs_check_lock_depth(p_s_sb, "journal end");
3889 if (journal->j_len == 0) {
3890 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb),
3892 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb));
3895 lock_journal(p_s_sb);
3896 if (journal->j_next_full_flush) {
3900 if (journal->j_next_async_flush) {
3901 flags |= COMMIT_NOW | WAIT;
3905 /* check_journal_end locks the journal, and unlocks if it does not return 1
3906 ** it tells us if we should continue with the journal_end, or just return
3908 if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
3910 wake_queued_writers(p_s_sb);
3911 reiserfs_async_progress_wait(p_s_sb);
3915 /* check_journal_end might set these, check again */
3916 if (journal->j_next_full_flush) {
3921 ** j must wait means we have to flush the log blocks, and the real blocks for
3924 if (journal->j_must_wait > 0) {
3927 #ifdef REISERFS_PREALLOCATE
3928 /* quota ops might need to nest, setup the journal_info pointer for them
3929 * and raise the refcount so that it is > 0. */
3930 current->journal_info = th;
3932 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
3933 * the transaction */
3935 current->journal_info = th->t_handle_save;
3938 /* setup description block */
3940 journal_getblk(p_s_sb,
3941 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3943 set_buffer_uptodate(d_bh);
3944 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data;
3945 memset(d_bh->b_data, 0, d_bh->b_size);
3946 memcpy(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8);
3947 set_desc_trans_id(desc, journal->j_trans_id);
3949 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
3950 c_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3951 ((journal->j_start + journal->j_len +
3952 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
3953 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
3954 memset(c_bh->b_data, 0, c_bh->b_size);
3955 set_commit_trans_id(commit, journal->j_trans_id);
3956 set_buffer_uptodate(c_bh);
3958 /* init this journal list */
3959 jl = journal->j_current_jl;
3961 /* we lock the commit before doing anything because
3962 * we want to make sure nobody tries to run flush_commit_list until
3963 * the new transaction is fully setup, and we've already flushed the
3966 down(&jl->j_commit_lock);
3968 /* save the transaction id in case we need to commit it later */
3969 commit_trans_id = jl->j_trans_id;
3971 atomic_set(&jl->j_older_commits_done, 0);
3972 jl->j_trans_id = journal->j_trans_id;
3973 jl->j_timestamp = journal->j_trans_start_time;
3974 jl->j_commit_bh = c_bh;
3975 jl->j_start = journal->j_start;
3976 jl->j_len = journal->j_len;
3977 atomic_set(&jl->j_nonzerolen, journal->j_len);
3978 atomic_set(&jl->j_commit_left, journal->j_len + 2);
3979 jl->j_realblock = NULL;
3981 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
3982 ** for each real block, add it to the journal list hash,
3983 ** copy into real block index array in the commit or desc block
3985 trans_half = journal_trans_half(p_s_sb->s_blocksize);
3986 for (i = 0, cn = journal->j_first; cn; cn = cn->next, i++) {
3987 if (buffer_journaled(cn->bh)) {
3988 jl_cn = get_cnode(p_s_sb);
3990 reiserfs_panic(p_s_sb,
3991 "journal-1676, get_cnode returned NULL\n");
3994 jl->j_realblock = jl_cn;
3996 jl_cn->prev = last_cn;
3999 last_cn->next = jl_cn;
4002 /* make sure the block we are trying to log is not a block
4003 of journal or reserved area */
4005 if (is_block_in_log_or_reserved_area
4006 (p_s_sb, cn->bh->b_blocknr)) {
4007 reiserfs_panic(p_s_sb,
4008 "journal-2332: Trying to log block %lu, which is a log block\n",
4011 jl_cn->blocknr = cn->bh->b_blocknr;
4016 insert_journal_hash(journal->j_list_hash_table, jl_cn);
4017 if (i < trans_half) {
4018 desc->j_realblock[i] =
4019 cpu_to_le32(cn->bh->b_blocknr);
4021 commit->j_realblock[i - trans_half] =
4022 cpu_to_le32(cn->bh->b_blocknr);
4028 set_desc_trans_len(desc, journal->j_len);
4029 set_desc_mount_id(desc, journal->j_mount_id);
4030 set_desc_trans_id(desc, journal->j_trans_id);
4031 set_commit_trans_len(commit, journal->j_len);
4033 /* special check in case all buffers in the journal were marked for not logging */
4034 if (journal->j_len == 0) {
4038 /* we're about to dirty all the log blocks, mark the description block
4039 * dirty now too. Don't mark the commit block dirty until all the
4040 * others are on disk
4042 mark_buffer_dirty(d_bh);
4044 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
4045 cur_write_start = journal->j_start;
4046 cn = journal->j_first;
4047 jindex = 1; /* start at one so we don't get the desc again */
4049 clear_buffer_journal_new(cn->bh);
4050 /* copy all the real blocks into log area. dirty log blocks */
4051 if (buffer_journaled(cn->bh)) {
4052 struct buffer_head *tmp_bh;
4056 journal_getblk(p_s_sb,
4057 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
4060 SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
4061 set_buffer_uptodate(tmp_bh);
4062 page = cn->bh->b_page;
4064 memcpy(tmp_bh->b_data,
4065 addr + offset_in_page(cn->bh->b_data),
4068 mark_buffer_dirty(tmp_bh);
4070 set_buffer_journal_dirty(cn->bh);
4071 clear_buffer_journaled(cn->bh);
4073 /* JDirty cleared sometime during transaction. don't log this one */
4074 reiserfs_warning(p_s_sb,
4075 "journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!");
4079 free_cnode(p_s_sb, cn);
4084 /* we are done with both the c_bh and d_bh, but
4085 ** c_bh must be written after all other commit blocks,
4086 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
4089 journal->j_current_jl = alloc_journal_list(p_s_sb);
4091 /* now it is safe to insert this transaction on the main list */
4092 list_add_tail(&jl->j_list, &journal->j_journal_list);
4093 list_add_tail(&jl->j_working_list, &journal->j_working_list);
4094 journal->j_num_work_lists++;
4096 /* reset journal values for the next transaction */
4097 old_start = journal->j_start;
4099 (journal->j_start + journal->j_len +
4100 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb);
4101 atomic_set(&(journal->j_wcount), 0);
4102 journal->j_bcount = 0;
4103 journal->j_last = NULL;
4104 journal->j_first = NULL;
4106 journal->j_trans_start_time = 0;
4107 journal->j_trans_id++;
4108 journal->j_current_jl->j_trans_id = journal->j_trans_id;
4109 journal->j_must_wait = 0;
4110 journal->j_len_alloc = 0;
4111 journal->j_next_full_flush = 0;
4112 journal->j_next_async_flush = 0;
4113 init_journal_hash(p_s_sb);
4115 // make sure reiserfs_add_jh sees the new current_jl before we
4116 // write out the tails
4119 /* tail conversion targets have to hit the disk before we end the
4120 * transaction. Otherwise a later transaction might repack the tail
4121 * before this transaction commits, leaving the data block unflushed and
4122 * clean, if we crash before the later transaction commits, the data block
4125 if (!list_empty(&jl->j_tail_bh_list)) {
4127 write_ordered_buffers(&journal->j_dirty_buffers_lock,
4128 journal, jl, &jl->j_tail_bh_list);
4131 if (!list_empty(&jl->j_tail_bh_list))
4133 up(&jl->j_commit_lock);
4135 /* honor the flush wishes from the caller, simple commits can
4136 ** be done outside the journal lock, they are done below
4138 ** if we don't flush the commit list right now, we put it into
4139 ** the work queue so the people waiting on the async progress work
4140 ** queue don't wait for this proc to flush journal lists and such.
4143 flush_commit_list(p_s_sb, jl, 1);
4144 flush_journal_list(p_s_sb, jl, 1);
4145 } else if (!(jl->j_state & LIST_COMMIT_PENDING))
4146 queue_delayed_work(commit_wq, &journal->j_work, HZ / 10);
4148 /* if the next transaction has any chance of wrapping, flush
4149 ** transactions that might get overwritten. If any journal lists are very
4150 ** old flush them as well.
4153 list_for_each_safe(entry, safe, &journal->j_journal_list) {
4154 temp_jl = JOURNAL_LIST_ENTRY(entry);
4155 if (journal->j_start <= temp_jl->j_start) {
4156 if ((journal->j_start + journal->j_trans_max + 1) >=
4158 flush_used_journal_lists(p_s_sb, temp_jl);
4160 } else if ((journal->j_start +
4161 journal->j_trans_max + 1) <
4162 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
4163 /* if we don't cross into the next transaction and we don't
4164 * wrap, there is no way we can overlap any later transactions
4169 } else if ((journal->j_start +
4170 journal->j_trans_max + 1) >
4171 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
4172 if (((journal->j_start + journal->j_trans_max + 1) %
4173 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) >=
4175 flush_used_journal_lists(p_s_sb, temp_jl);
4178 /* we don't overlap anything from out start to the end of the
4179 * log, and our wrapped portion doesn't overlap anything at
4180 * the start of the log. We can break
4186 flush_old_journal_lists(p_s_sb);
4188 journal->j_current_jl->j_list_bitmap =
4189 get_list_bitmap(p_s_sb, journal->j_current_jl);
4191 if (!(journal->j_current_jl->j_list_bitmap)) {
4192 reiserfs_panic(p_s_sb,
4193 "journal-1996: do_journal_end, could not get a list bitmap\n");
4196 atomic_set(&(journal->j_jlock), 0);
4197 unlock_journal(p_s_sb);
4198 /* wake up any body waiting to join. */
4199 clear_bit(J_WRITERS_QUEUED, &journal->j_state);
4200 wake_up(&(journal->j_join_wait));
4202 if (!flush && wait_on_commit &&
4203 journal_list_still_alive(p_s_sb, commit_trans_id)) {
4204 flush_commit_list(p_s_sb, jl, 1);
4207 reiserfs_check_lock_depth(p_s_sb, "journal end2");
4209 memset(th, 0, sizeof(*th));
4210 /* Re-set th->t_super, so we can properly keep track of how many
4211 * persistent transactions there are. We need to do this so if this
4212 * call is part of a failed restart_transaction, we can free it later */
4213 th->t_super = p_s_sb;
4215 return journal->j_errno;
4218 static void __reiserfs_journal_abort_hard(struct super_block *sb)
4220 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4221 if (test_bit(J_ABORTED, &journal->j_state))
4224 printk(KERN_CRIT "REISERFS: Aborting journal for filesystem on %s\n",
4225 reiserfs_bdevname(sb));
4227 sb->s_flags |= MS_RDONLY;
4228 set_bit(J_ABORTED, &journal->j_state);
4230 #ifdef CONFIG_REISERFS_CHECK
4235 static void __reiserfs_journal_abort_soft(struct super_block *sb, int errno)
4237 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4238 if (test_bit(J_ABORTED, &journal->j_state))
4241 if (!journal->j_errno)
4242 journal->j_errno = errno;
4244 __reiserfs_journal_abort_hard(sb);
4247 void reiserfs_journal_abort(struct super_block *sb, int errno)
4249 return __reiserfs_journal_abort_soft(sb, errno);