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