[SK_BUFF]: Fix missing offset adjustment in pskb_expand_head
[linux-2.6.22.y-op.git] / fs / reiserfs / journal.c
blob7280a23ef3444aa4b8370dcb99b8ee46ee246dec
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 <asm/uaccess.h>
38 #include <asm/system.h>
40 #include <linux/time.h>
41 #include <asm/semaphore.h>
43 #include <linux/vmalloc.h>
44 #include <linux/reiserfs_fs.h>
46 #include <linux/kernel.h>
47 #include <linux/errno.h>
48 #include <linux/fcntl.h>
49 #include <linux/stat.h>
50 #include <linux/string.h>
51 #include <linux/smp_lock.h>
52 #include <linux/buffer_head.h>
53 #include <linux/workqueue.h>
54 #include <linux/writeback.h>
55 #include <linux/blkdev.h>
56 #include <linux/backing-dev.h>
58 /* gets a struct reiserfs_journal_list * from a list head */
59 #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
60 j_list))
61 #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
62 j_working_list))
64 /* the number of mounted filesystems. This is used to decide when to
65 ** start and kill the commit workqueue
67 static int reiserfs_mounted_fs_count;
69 static struct workqueue_struct *commit_wq;
71 #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
72 structs at 4k */
73 #define BUFNR 64 /*read ahead */
75 /* cnode stat bits. Move these into reiserfs_fs.h */
77 #define BLOCK_FREED 2 /* this block was freed, and can't be written. */
78 #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
80 #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
81 #define BLOCK_DIRTIED 5
83 /* journal list state bits */
84 #define LIST_TOUCHED 1
85 #define LIST_DIRTY 2
86 #define LIST_COMMIT_PENDING 4 /* someone will commit this list */
88 /* flags for do_journal_end */
89 #define FLUSH_ALL 1 /* flush commit and real blocks */
90 #define COMMIT_NOW 2 /* end and commit this transaction */
91 #define WAIT 4 /* wait for the log blocks to hit the disk */
93 static int do_journal_end(struct reiserfs_transaction_handle *,
94 struct super_block *, unsigned long nblocks,
95 int flags);
96 static int flush_journal_list(struct super_block *s,
97 struct reiserfs_journal_list *jl, int flushall);
98 static int flush_commit_list(struct super_block *s,
99 struct reiserfs_journal_list *jl, int flushall);
100 static int can_dirty(struct reiserfs_journal_cnode *cn);
101 static int journal_join(struct reiserfs_transaction_handle *th,
102 struct super_block *p_s_sb, unsigned long nblocks);
103 static int release_journal_dev(struct super_block *super,
104 struct reiserfs_journal *journal);
105 static int dirty_one_transaction(struct super_block *s,
106 struct reiserfs_journal_list *jl);
107 static void flush_async_commits(struct work_struct *work);
108 static void queue_log_writer(struct super_block *s);
110 /* values for join in do_journal_begin_r */
111 enum {
112 JBEGIN_REG = 0, /* regular journal begin */
113 JBEGIN_JOIN = 1, /* join the running transaction if at all possible */
114 JBEGIN_ABORT = 2, /* called from cleanup code, ignores aborted flag */
117 static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
118 struct super_block *p_s_sb,
119 unsigned long nblocks, int join);
121 static void init_journal_hash(struct super_block *p_s_sb)
123 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
124 memset(journal->j_hash_table, 0,
125 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
129 ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
130 ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
131 ** more details.
133 static int reiserfs_clean_and_file_buffer(struct buffer_head *bh)
135 if (bh) {
136 clear_buffer_dirty(bh);
137 clear_buffer_journal_test(bh);
139 return 0;
142 static void disable_barrier(struct super_block *s)
144 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_BARRIER_FLUSH);
145 printk("reiserfs: disabling flush barriers on %s\n",
146 reiserfs_bdevname(s));
149 static struct reiserfs_bitmap_node *allocate_bitmap_node(struct super_block
150 *p_s_sb)
152 struct reiserfs_bitmap_node *bn;
153 static int id;
155 bn = kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS);
156 if (!bn) {
157 return NULL;
159 bn->data = kzalloc(p_s_sb->s_blocksize, GFP_NOFS);
160 if (!bn->data) {
161 kfree(bn);
162 return NULL;
164 bn->id = id++;
165 INIT_LIST_HEAD(&bn->list);
166 return bn;
169 static struct reiserfs_bitmap_node *get_bitmap_node(struct super_block *p_s_sb)
171 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
172 struct reiserfs_bitmap_node *bn = NULL;
173 struct list_head *entry = journal->j_bitmap_nodes.next;
175 journal->j_used_bitmap_nodes++;
176 repeat:
178 if (entry != &journal->j_bitmap_nodes) {
179 bn = list_entry(entry, struct reiserfs_bitmap_node, list);
180 list_del(entry);
181 memset(bn->data, 0, p_s_sb->s_blocksize);
182 journal->j_free_bitmap_nodes--;
183 return bn;
185 bn = allocate_bitmap_node(p_s_sb);
186 if (!bn) {
187 yield();
188 goto repeat;
190 return bn;
192 static inline void free_bitmap_node(struct super_block *p_s_sb,
193 struct reiserfs_bitmap_node *bn)
195 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
196 journal->j_used_bitmap_nodes--;
197 if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
198 kfree(bn->data);
199 kfree(bn);
200 } else {
201 list_add(&bn->list, &journal->j_bitmap_nodes);
202 journal->j_free_bitmap_nodes++;
206 static void allocate_bitmap_nodes(struct super_block *p_s_sb)
208 int i;
209 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
210 struct reiserfs_bitmap_node *bn = NULL;
211 for (i = 0; i < REISERFS_MIN_BITMAP_NODES; i++) {
212 bn = allocate_bitmap_node(p_s_sb);
213 if (bn) {
214 list_add(&bn->list, &journal->j_bitmap_nodes);
215 journal->j_free_bitmap_nodes++;
216 } else {
217 break; // this is ok, we'll try again when more are needed
222 static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
223 struct reiserfs_list_bitmap *jb)
225 int bmap_nr = block / (p_s_sb->s_blocksize << 3);
226 int bit_nr = block % (p_s_sb->s_blocksize << 3);
228 if (!jb->bitmaps[bmap_nr]) {
229 jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb);
231 set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data);
232 return 0;
235 static void cleanup_bitmap_list(struct super_block *p_s_sb,
236 struct reiserfs_list_bitmap *jb)
238 int i;
239 if (jb->bitmaps == NULL)
240 return;
242 for (i = 0; i < SB_BMAP_NR(p_s_sb); i++) {
243 if (jb->bitmaps[i]) {
244 free_bitmap_node(p_s_sb, jb->bitmaps[i]);
245 jb->bitmaps[i] = NULL;
251 ** only call this on FS unmount.
253 static int free_list_bitmaps(struct super_block *p_s_sb,
254 struct reiserfs_list_bitmap *jb_array)
256 int i;
257 struct reiserfs_list_bitmap *jb;
258 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
259 jb = jb_array + i;
260 jb->journal_list = NULL;
261 cleanup_bitmap_list(p_s_sb, jb);
262 vfree(jb->bitmaps);
263 jb->bitmaps = NULL;
265 return 0;
268 static int free_bitmap_nodes(struct super_block *p_s_sb)
270 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
271 struct list_head *next = journal->j_bitmap_nodes.next;
272 struct reiserfs_bitmap_node *bn;
274 while (next != &journal->j_bitmap_nodes) {
275 bn = list_entry(next, struct reiserfs_bitmap_node, list);
276 list_del(next);
277 kfree(bn->data);
278 kfree(bn);
279 next = journal->j_bitmap_nodes.next;
280 journal->j_free_bitmap_nodes--;
283 return 0;
287 ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
288 ** jb_array is the array to be filled in.
290 int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
291 struct reiserfs_list_bitmap *jb_array,
292 int bmap_nr)
294 int i;
295 int failed = 0;
296 struct reiserfs_list_bitmap *jb;
297 int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *);
299 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
300 jb = jb_array + i;
301 jb->journal_list = NULL;
302 jb->bitmaps = vmalloc(mem);
303 if (!jb->bitmaps) {
304 reiserfs_warning(p_s_sb,
305 "clm-2000, unable to allocate bitmaps for journal lists");
306 failed = 1;
307 break;
309 memset(jb->bitmaps, 0, mem);
311 if (failed) {
312 free_list_bitmaps(p_s_sb, jb_array);
313 return -1;
315 return 0;
319 ** find an available list bitmap. If you can't find one, flush a commit list
320 ** and try again
322 static struct reiserfs_list_bitmap *get_list_bitmap(struct super_block *p_s_sb,
323 struct reiserfs_journal_list
324 *jl)
326 int i, j;
327 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
328 struct reiserfs_list_bitmap *jb = NULL;
330 for (j = 0; j < (JOURNAL_NUM_BITMAPS * 3); j++) {
331 i = journal->j_list_bitmap_index;
332 journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS;
333 jb = journal->j_list_bitmap + i;
334 if (journal->j_list_bitmap[i].journal_list) {
335 flush_commit_list(p_s_sb,
336 journal->j_list_bitmap[i].
337 journal_list, 1);
338 if (!journal->j_list_bitmap[i].journal_list) {
339 break;
341 } else {
342 break;
345 if (jb->journal_list) { /* double check to make sure if flushed correctly */
346 return NULL;
348 jb->journal_list = jl;
349 return jb;
353 ** allocates a new chunk of X nodes, and links them all together as a list.
354 ** Uses the cnode->next and cnode->prev pointers
355 ** returns NULL on failure
357 static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes)
359 struct reiserfs_journal_cnode *head;
360 int i;
361 if (num_cnodes <= 0) {
362 return NULL;
364 head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode));
365 if (!head) {
366 return NULL;
368 memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode));
369 head[0].prev = NULL;
370 head[0].next = head + 1;
371 for (i = 1; i < num_cnodes; i++) {
372 head[i].prev = head + (i - 1);
373 head[i].next = head + (i + 1); /* if last one, overwrite it after the if */
375 head[num_cnodes - 1].next = NULL;
376 return head;
380 ** pulls a cnode off the free list, or returns NULL on failure
382 static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb)
384 struct reiserfs_journal_cnode *cn;
385 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
387 reiserfs_check_lock_depth(p_s_sb, "get_cnode");
389 if (journal->j_cnode_free <= 0) {
390 return NULL;
392 journal->j_cnode_used++;
393 journal->j_cnode_free--;
394 cn = journal->j_cnode_free_list;
395 if (!cn) {
396 return cn;
398 if (cn->next) {
399 cn->next->prev = NULL;
401 journal->j_cnode_free_list = cn->next;
402 memset(cn, 0, sizeof(struct reiserfs_journal_cnode));
403 return cn;
407 ** returns a cnode to the free list
409 static void free_cnode(struct super_block *p_s_sb,
410 struct reiserfs_journal_cnode *cn)
412 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
414 reiserfs_check_lock_depth(p_s_sb, "free_cnode");
416 journal->j_cnode_used--;
417 journal->j_cnode_free++;
418 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
419 cn->next = journal->j_cnode_free_list;
420 if (journal->j_cnode_free_list) {
421 journal->j_cnode_free_list->prev = cn;
423 cn->prev = NULL; /* not needed with the memset, but I might kill the memset, and forget to do this */
424 journal->j_cnode_free_list = cn;
427 static void clear_prepared_bits(struct buffer_head *bh)
429 clear_buffer_journal_prepared(bh);
430 clear_buffer_journal_restore_dirty(bh);
433 /* utility function to force a BUG if it is called without the big
434 ** kernel lock held. caller is the string printed just before calling BUG()
436 void reiserfs_check_lock_depth(struct super_block *sb, char *caller)
438 #ifdef CONFIG_SMP
439 if (current->lock_depth < 0) {
440 reiserfs_panic(sb, "%s called without kernel lock held",
441 caller);
443 #else
445 #endif
448 /* return a cnode with same dev, block number and size in table, or null if not found */
449 static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct
450 super_block
451 *sb,
452 struct
453 reiserfs_journal_cnode
454 **table,
455 long bl)
457 struct reiserfs_journal_cnode *cn;
458 cn = journal_hash(table, sb, bl);
459 while (cn) {
460 if (cn->blocknr == bl && cn->sb == sb)
461 return cn;
462 cn = cn->hnext;
464 return (struct reiserfs_journal_cnode *)0;
468 ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
469 ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
470 ** being overwritten by a replay after crashing.
472 ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
473 ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
474 ** sure you never write the block without logging it.
476 ** next_zero_bit is a suggestion about the next block to try for find_forward.
477 ** when bl is rejected because it is set in a journal list bitmap, we search
478 ** for the next zero bit in the bitmap that rejected bl. Then, we return that
479 ** through next_zero_bit for find_forward to try.
481 ** Just because we return something in next_zero_bit does not mean we won't
482 ** reject it on the next call to reiserfs_in_journal
485 int reiserfs_in_journal(struct super_block *p_s_sb,
486 int bmap_nr, int bit_nr, int search_all,
487 b_blocknr_t * next_zero_bit)
489 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
490 struct reiserfs_journal_cnode *cn;
491 struct reiserfs_list_bitmap *jb;
492 int i;
493 unsigned long bl;
495 *next_zero_bit = 0; /* always start this at zero. */
497 PROC_INFO_INC(p_s_sb, journal.in_journal);
498 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
499 ** if we crash before the transaction that freed it commits, this transaction won't
500 ** have committed either, and the block will never be written
502 if (search_all) {
503 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
504 PROC_INFO_INC(p_s_sb, journal.in_journal_bitmap);
505 jb = journal->j_list_bitmap + i;
506 if (jb->journal_list && jb->bitmaps[bmap_nr] &&
507 test_bit(bit_nr,
508 (unsigned long *)jb->bitmaps[bmap_nr]->
509 data)) {
510 *next_zero_bit =
511 find_next_zero_bit((unsigned long *)
512 (jb->bitmaps[bmap_nr]->
513 data),
514 p_s_sb->s_blocksize << 3,
515 bit_nr + 1);
516 return 1;
521 bl = bmap_nr * (p_s_sb->s_blocksize << 3) + bit_nr;
522 /* is it in any old transactions? */
523 if (search_all
524 && (cn =
525 get_journal_hash_dev(p_s_sb, journal->j_list_hash_table, bl))) {
526 return 1;
529 /* is it in the current transaction. This should never happen */
530 if ((cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, bl))) {
531 BUG();
532 return 1;
535 PROC_INFO_INC(p_s_sb, journal.in_journal_reusable);
536 /* safe for reuse */
537 return 0;
540 /* insert cn into table
542 static inline void insert_journal_hash(struct reiserfs_journal_cnode **table,
543 struct reiserfs_journal_cnode *cn)
545 struct reiserfs_journal_cnode *cn_orig;
547 cn_orig = journal_hash(table, cn->sb, cn->blocknr);
548 cn->hnext = cn_orig;
549 cn->hprev = NULL;
550 if (cn_orig) {
551 cn_orig->hprev = cn;
553 journal_hash(table, cn->sb, cn->blocknr) = cn;
556 /* lock the current transaction */
557 static inline void lock_journal(struct super_block *p_s_sb)
559 PROC_INFO_INC(p_s_sb, journal.lock_journal);
560 down(&SB_JOURNAL(p_s_sb)->j_lock);
563 /* unlock the current transaction */
564 static inline void unlock_journal(struct super_block *p_s_sb)
566 up(&SB_JOURNAL(p_s_sb)->j_lock);
569 static inline void get_journal_list(struct reiserfs_journal_list *jl)
571 jl->j_refcount++;
574 static inline void put_journal_list(struct super_block *s,
575 struct reiserfs_journal_list *jl)
577 if (jl->j_refcount < 1) {
578 reiserfs_panic(s, "trans id %lu, refcount at %d",
579 jl->j_trans_id, jl->j_refcount);
581 if (--jl->j_refcount == 0)
582 kfree(jl);
586 ** this used to be much more involved, and I'm keeping it just in case things get ugly again.
587 ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
588 ** transaction.
590 static void cleanup_freed_for_journal_list(struct super_block *p_s_sb,
591 struct reiserfs_journal_list *jl)
594 struct reiserfs_list_bitmap *jb = jl->j_list_bitmap;
595 if (jb) {
596 cleanup_bitmap_list(p_s_sb, jb);
598 jl->j_list_bitmap->journal_list = NULL;
599 jl->j_list_bitmap = NULL;
602 static int journal_list_still_alive(struct super_block *s,
603 unsigned long trans_id)
605 struct reiserfs_journal *journal = SB_JOURNAL(s);
606 struct list_head *entry = &journal->j_journal_list;
607 struct reiserfs_journal_list *jl;
609 if (!list_empty(entry)) {
610 jl = JOURNAL_LIST_ENTRY(entry->next);
611 if (jl->j_trans_id <= trans_id) {
612 return 1;
615 return 0;
618 static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
620 char b[BDEVNAME_SIZE];
622 if (buffer_journaled(bh)) {
623 reiserfs_warning(NULL,
624 "clm-2084: pinned buffer %lu:%s sent to disk",
625 bh->b_blocknr, bdevname(bh->b_bdev, b));
627 if (uptodate)
628 set_buffer_uptodate(bh);
629 else
630 clear_buffer_uptodate(bh);
631 unlock_buffer(bh);
632 put_bh(bh);
635 static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate)
637 if (uptodate)
638 set_buffer_uptodate(bh);
639 else
640 clear_buffer_uptodate(bh);
641 unlock_buffer(bh);
642 put_bh(bh);
645 static void submit_logged_buffer(struct buffer_head *bh)
647 get_bh(bh);
648 bh->b_end_io = reiserfs_end_buffer_io_sync;
649 clear_buffer_journal_new(bh);
650 clear_buffer_dirty(bh);
651 if (!test_clear_buffer_journal_test(bh))
652 BUG();
653 if (!buffer_uptodate(bh))
654 BUG();
655 submit_bh(WRITE, bh);
658 static void submit_ordered_buffer(struct buffer_head *bh)
660 get_bh(bh);
661 bh->b_end_io = reiserfs_end_ordered_io;
662 clear_buffer_dirty(bh);
663 if (!buffer_uptodate(bh))
664 BUG();
665 submit_bh(WRITE, bh);
668 static int submit_barrier_buffer(struct buffer_head *bh)
670 get_bh(bh);
671 bh->b_end_io = reiserfs_end_ordered_io;
672 clear_buffer_dirty(bh);
673 if (!buffer_uptodate(bh))
674 BUG();
675 return submit_bh(WRITE_BARRIER, bh);
678 static void check_barrier_completion(struct super_block *s,
679 struct buffer_head *bh)
681 if (buffer_eopnotsupp(bh)) {
682 clear_buffer_eopnotsupp(bh);
683 disable_barrier(s);
684 set_buffer_uptodate(bh);
685 set_buffer_dirty(bh);
686 sync_dirty_buffer(bh);
690 #define CHUNK_SIZE 32
691 struct buffer_chunk {
692 struct buffer_head *bh[CHUNK_SIZE];
693 int nr;
696 static void write_chunk(struct buffer_chunk *chunk)
698 int i;
699 get_fs_excl();
700 for (i = 0; i < chunk->nr; i++) {
701 submit_logged_buffer(chunk->bh[i]);
703 chunk->nr = 0;
704 put_fs_excl();
707 static void write_ordered_chunk(struct buffer_chunk *chunk)
709 int i;
710 get_fs_excl();
711 for (i = 0; i < chunk->nr; i++) {
712 submit_ordered_buffer(chunk->bh[i]);
714 chunk->nr = 0;
715 put_fs_excl();
718 static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
719 spinlock_t * lock, void (fn) (struct buffer_chunk *))
721 int ret = 0;
722 BUG_ON(chunk->nr >= CHUNK_SIZE);
723 chunk->bh[chunk->nr++] = bh;
724 if (chunk->nr >= CHUNK_SIZE) {
725 ret = 1;
726 if (lock)
727 spin_unlock(lock);
728 fn(chunk);
729 if (lock)
730 spin_lock(lock);
732 return ret;
735 static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
736 static struct reiserfs_jh *alloc_jh(void)
738 struct reiserfs_jh *jh;
739 while (1) {
740 jh = kmalloc(sizeof(*jh), GFP_NOFS);
741 if (jh) {
742 atomic_inc(&nr_reiserfs_jh);
743 return jh;
745 yield();
750 * we want to free the jh when the buffer has been written
751 * and waited on
753 void reiserfs_free_jh(struct buffer_head *bh)
755 struct reiserfs_jh *jh;
757 jh = bh->b_private;
758 if (jh) {
759 bh->b_private = NULL;
760 jh->bh = NULL;
761 list_del_init(&jh->list);
762 kfree(jh);
763 if (atomic_read(&nr_reiserfs_jh) <= 0)
764 BUG();
765 atomic_dec(&nr_reiserfs_jh);
766 put_bh(bh);
770 static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
771 int tail)
773 struct reiserfs_jh *jh;
775 if (bh->b_private) {
776 spin_lock(&j->j_dirty_buffers_lock);
777 if (!bh->b_private) {
778 spin_unlock(&j->j_dirty_buffers_lock);
779 goto no_jh;
781 jh = bh->b_private;
782 list_del_init(&jh->list);
783 } else {
784 no_jh:
785 get_bh(bh);
786 jh = alloc_jh();
787 spin_lock(&j->j_dirty_buffers_lock);
788 /* buffer must be locked for __add_jh, should be able to have
789 * two adds at the same time
791 BUG_ON(bh->b_private);
792 jh->bh = bh;
793 bh->b_private = jh;
795 jh->jl = j->j_current_jl;
796 if (tail)
797 list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
798 else {
799 list_add_tail(&jh->list, &jh->jl->j_bh_list);
801 spin_unlock(&j->j_dirty_buffers_lock);
802 return 0;
805 int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh)
807 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
809 int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh)
811 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
814 #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
815 static int write_ordered_buffers(spinlock_t * lock,
816 struct reiserfs_journal *j,
817 struct reiserfs_journal_list *jl,
818 struct list_head *list)
820 struct buffer_head *bh;
821 struct reiserfs_jh *jh;
822 int ret = j->j_errno;
823 struct buffer_chunk chunk;
824 struct list_head tmp;
825 INIT_LIST_HEAD(&tmp);
827 chunk.nr = 0;
828 spin_lock(lock);
829 while (!list_empty(list)) {
830 jh = JH_ENTRY(list->next);
831 bh = jh->bh;
832 get_bh(bh);
833 if (test_set_buffer_locked(bh)) {
834 if (!buffer_dirty(bh)) {
835 list_move(&jh->list, &tmp);
836 goto loop_next;
838 spin_unlock(lock);
839 if (chunk.nr)
840 write_ordered_chunk(&chunk);
841 wait_on_buffer(bh);
842 cond_resched();
843 spin_lock(lock);
844 goto loop_next;
846 /* in theory, dirty non-uptodate buffers should never get here,
847 * but the upper layer io error paths still have a few quirks.
848 * Handle them here as gracefully as we can
850 if (!buffer_uptodate(bh) && buffer_dirty(bh)) {
851 clear_buffer_dirty(bh);
852 ret = -EIO;
854 if (buffer_dirty(bh)) {
855 list_move(&jh->list, &tmp);
856 add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
857 } else {
858 reiserfs_free_jh(bh);
859 unlock_buffer(bh);
861 loop_next:
862 put_bh(bh);
863 cond_resched_lock(lock);
865 if (chunk.nr) {
866 spin_unlock(lock);
867 write_ordered_chunk(&chunk);
868 spin_lock(lock);
870 while (!list_empty(&tmp)) {
871 jh = JH_ENTRY(tmp.prev);
872 bh = jh->bh;
873 get_bh(bh);
874 reiserfs_free_jh(bh);
876 if (buffer_locked(bh)) {
877 spin_unlock(lock);
878 wait_on_buffer(bh);
879 spin_lock(lock);
881 if (!buffer_uptodate(bh)) {
882 ret = -EIO;
884 /* ugly interaction with invalidatepage here.
885 * reiserfs_invalidate_page will pin any buffer that has a valid
886 * journal head from an older transaction. If someone else sets
887 * our buffer dirty after we write it in the first loop, and
888 * then someone truncates the page away, nobody will ever write
889 * the buffer. We're safe if we write the page one last time
890 * after freeing the journal header.
892 if (buffer_dirty(bh) && unlikely(bh->b_page->mapping == NULL)) {
893 spin_unlock(lock);
894 ll_rw_block(WRITE, 1, &bh);
895 spin_lock(lock);
897 put_bh(bh);
898 cond_resched_lock(lock);
900 spin_unlock(lock);
901 return ret;
904 static int flush_older_commits(struct super_block *s,
905 struct reiserfs_journal_list *jl)
907 struct reiserfs_journal *journal = SB_JOURNAL(s);
908 struct reiserfs_journal_list *other_jl;
909 struct reiserfs_journal_list *first_jl;
910 struct list_head *entry;
911 unsigned long trans_id = jl->j_trans_id;
912 unsigned long other_trans_id;
913 unsigned long first_trans_id;
915 find_first:
917 * first we walk backwards to find the oldest uncommitted transation
919 first_jl = jl;
920 entry = jl->j_list.prev;
921 while (1) {
922 other_jl = JOURNAL_LIST_ENTRY(entry);
923 if (entry == &journal->j_journal_list ||
924 atomic_read(&other_jl->j_older_commits_done))
925 break;
927 first_jl = other_jl;
928 entry = other_jl->j_list.prev;
931 /* if we didn't find any older uncommitted transactions, return now */
932 if (first_jl == jl) {
933 return 0;
936 first_trans_id = first_jl->j_trans_id;
938 entry = &first_jl->j_list;
939 while (1) {
940 other_jl = JOURNAL_LIST_ENTRY(entry);
941 other_trans_id = other_jl->j_trans_id;
943 if (other_trans_id < trans_id) {
944 if (atomic_read(&other_jl->j_commit_left) != 0) {
945 flush_commit_list(s, other_jl, 0);
947 /* list we were called with is gone, return */
948 if (!journal_list_still_alive(s, trans_id))
949 return 1;
951 /* the one we just flushed is gone, this means all
952 * older lists are also gone, so first_jl is no longer
953 * valid either. Go back to the beginning.
955 if (!journal_list_still_alive
956 (s, other_trans_id)) {
957 goto find_first;
960 entry = entry->next;
961 if (entry == &journal->j_journal_list)
962 return 0;
963 } else {
964 return 0;
967 return 0;
969 int reiserfs_async_progress_wait(struct super_block *s)
971 DEFINE_WAIT(wait);
972 struct reiserfs_journal *j = SB_JOURNAL(s);
973 if (atomic_read(&j->j_async_throttle))
974 congestion_wait(WRITE, HZ / 10);
975 return 0;
979 ** if this journal list still has commit blocks unflushed, send them to disk.
981 ** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
982 ** Before the commit block can by written, every other log block must be safely on disk
985 static int flush_commit_list(struct super_block *s,
986 struct reiserfs_journal_list *jl, int flushall)
988 int i;
989 int bn;
990 struct buffer_head *tbh = NULL;
991 unsigned long trans_id = jl->j_trans_id;
992 struct reiserfs_journal *journal = SB_JOURNAL(s);
993 int barrier = 0;
994 int retval = 0;
995 int write_len;
997 reiserfs_check_lock_depth(s, "flush_commit_list");
999 if (atomic_read(&jl->j_older_commits_done)) {
1000 return 0;
1003 get_fs_excl();
1005 /* before we can put our commit blocks on disk, we have to make sure everyone older than
1006 ** us is on disk too
1008 BUG_ON(jl->j_len <= 0);
1009 BUG_ON(trans_id == journal->j_trans_id);
1011 get_journal_list(jl);
1012 if (flushall) {
1013 if (flush_older_commits(s, jl) == 1) {
1014 /* list disappeared during flush_older_commits. return */
1015 goto put_jl;
1019 /* make sure nobody is trying to flush this one at the same time */
1020 down(&jl->j_commit_lock);
1021 if (!journal_list_still_alive(s, trans_id)) {
1022 up(&jl->j_commit_lock);
1023 goto put_jl;
1025 BUG_ON(jl->j_trans_id == 0);
1027 /* this commit is done, exit */
1028 if (atomic_read(&(jl->j_commit_left)) <= 0) {
1029 if (flushall) {
1030 atomic_set(&(jl->j_older_commits_done), 1);
1032 up(&jl->j_commit_lock);
1033 goto put_jl;
1036 if (!list_empty(&jl->j_bh_list)) {
1037 int ret;
1038 unlock_kernel();
1039 ret = write_ordered_buffers(&journal->j_dirty_buffers_lock,
1040 journal, jl, &jl->j_bh_list);
1041 if (ret < 0 && retval == 0)
1042 retval = ret;
1043 lock_kernel();
1045 BUG_ON(!list_empty(&jl->j_bh_list));
1047 * for the description block and all the log blocks, submit any buffers
1048 * that haven't already reached the disk. Try to write at least 256
1049 * log blocks. later on, we will only wait on blocks that correspond
1050 * to this transaction, but while we're unplugging we might as well
1051 * get a chunk of data on there.
1053 atomic_inc(&journal->j_async_throttle);
1054 write_len = jl->j_len + 1;
1055 if (write_len < 256)
1056 write_len = 256;
1057 for (i = 0 ; i < write_len ; i++) {
1058 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start + i) %
1059 SB_ONDISK_JOURNAL_SIZE(s);
1060 tbh = journal_find_get_block(s, bn);
1061 if (tbh) {
1062 if (buffer_dirty(tbh))
1063 ll_rw_block(WRITE, 1, &tbh) ;
1064 put_bh(tbh) ;
1067 atomic_dec(&journal->j_async_throttle);
1069 /* We're skipping the commit if there's an error */
1070 if (retval || reiserfs_is_journal_aborted(journal))
1071 barrier = 0;
1073 /* wait on everything written so far before writing the commit
1074 * if we are in barrier mode, send the commit down now
1076 barrier = reiserfs_barrier_flush(s);
1077 if (barrier) {
1078 int ret;
1079 lock_buffer(jl->j_commit_bh);
1080 ret = submit_barrier_buffer(jl->j_commit_bh);
1081 if (ret == -EOPNOTSUPP) {
1082 set_buffer_uptodate(jl->j_commit_bh);
1083 disable_barrier(s);
1084 barrier = 0;
1087 for (i = 0; i < (jl->j_len + 1); i++) {
1088 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
1089 (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s);
1090 tbh = journal_find_get_block(s, bn);
1091 wait_on_buffer(tbh);
1092 // since we're using ll_rw_blk above, it might have skipped over
1093 // a locked buffer. Double check here
1095 if (buffer_dirty(tbh)) /* redundant, sync_dirty_buffer() checks */
1096 sync_dirty_buffer(tbh);
1097 if (unlikely(!buffer_uptodate(tbh))) {
1098 #ifdef CONFIG_REISERFS_CHECK
1099 reiserfs_warning(s, "journal-601, buffer write failed");
1100 #endif
1101 retval = -EIO;
1103 put_bh(tbh); /* once for journal_find_get_block */
1104 put_bh(tbh); /* once due to original getblk in do_journal_end */
1105 atomic_dec(&(jl->j_commit_left));
1108 BUG_ON(atomic_read(&(jl->j_commit_left)) != 1);
1110 if (!barrier) {
1111 /* If there was a write error in the journal - we can't commit
1112 * this transaction - it will be invalid and, if successful,
1113 * will just end up propogating the write error out to
1114 * the file system. */
1115 if (likely(!retval && !reiserfs_is_journal_aborted (journal))) {
1116 if (buffer_dirty(jl->j_commit_bh))
1117 BUG();
1118 mark_buffer_dirty(jl->j_commit_bh) ;
1119 sync_dirty_buffer(jl->j_commit_bh) ;
1121 } else
1122 wait_on_buffer(jl->j_commit_bh);
1124 check_barrier_completion(s, jl->j_commit_bh);
1126 /* If there was a write error in the journal - we can't commit this
1127 * transaction - it will be invalid and, if successful, will just end
1128 * up propogating the write error out to the filesystem. */
1129 if (unlikely(!buffer_uptodate(jl->j_commit_bh))) {
1130 #ifdef CONFIG_REISERFS_CHECK
1131 reiserfs_warning(s, "journal-615: buffer write failed");
1132 #endif
1133 retval = -EIO;
1135 bforget(jl->j_commit_bh);
1136 if (journal->j_last_commit_id != 0 &&
1137 (jl->j_trans_id - journal->j_last_commit_id) != 1) {
1138 reiserfs_warning(s, "clm-2200: last commit %lu, current %lu",
1139 journal->j_last_commit_id, jl->j_trans_id);
1141 journal->j_last_commit_id = jl->j_trans_id;
1143 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1144 cleanup_freed_for_journal_list(s, jl);
1146 retval = retval ? retval : journal->j_errno;
1148 /* mark the metadata dirty */
1149 if (!retval)
1150 dirty_one_transaction(s, jl);
1151 atomic_dec(&(jl->j_commit_left));
1153 if (flushall) {
1154 atomic_set(&(jl->j_older_commits_done), 1);
1156 up(&jl->j_commit_lock);
1157 put_jl:
1158 put_journal_list(s, jl);
1160 if (retval)
1161 reiserfs_abort(s, retval, "Journal write error in %s",
1162 __FUNCTION__);
1163 put_fs_excl();
1164 return retval;
1168 ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1169 ** returns NULL if it can't find anything
1171 static struct reiserfs_journal_list *find_newer_jl_for_cn(struct
1172 reiserfs_journal_cnode
1173 *cn)
1175 struct super_block *sb = cn->sb;
1176 b_blocknr_t blocknr = cn->blocknr;
1178 cn = cn->hprev;
1179 while (cn) {
1180 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
1181 return cn->jlist;
1183 cn = cn->hprev;
1185 return NULL;
1188 static int newer_jl_done(struct reiserfs_journal_cnode *cn)
1190 struct super_block *sb = cn->sb;
1191 b_blocknr_t blocknr = cn->blocknr;
1193 cn = cn->hprev;
1194 while (cn) {
1195 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist &&
1196 atomic_read(&cn->jlist->j_commit_left) != 0)
1197 return 0;
1198 cn = cn->hprev;
1200 return 1;
1203 static void remove_journal_hash(struct super_block *,
1204 struct reiserfs_journal_cnode **,
1205 struct reiserfs_journal_list *, unsigned long,
1206 int);
1209 ** once all the real blocks have been flushed, it is safe to remove them from the
1210 ** journal list for this transaction. Aside from freeing the cnode, this also allows the
1211 ** block to be reallocated for data blocks if it had been deleted.
1213 static void remove_all_from_journal_list(struct super_block *p_s_sb,
1214 struct reiserfs_journal_list *jl,
1215 int debug)
1217 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1218 struct reiserfs_journal_cnode *cn, *last;
1219 cn = jl->j_realblock;
1221 /* which is better, to lock once around the whole loop, or
1222 ** to lock for each call to remove_journal_hash?
1224 while (cn) {
1225 if (cn->blocknr != 0) {
1226 if (debug) {
1227 reiserfs_warning(p_s_sb,
1228 "block %u, bh is %d, state %ld",
1229 cn->blocknr, cn->bh ? 1 : 0,
1230 cn->state);
1232 cn->state = 0;
1233 remove_journal_hash(p_s_sb, journal->j_list_hash_table,
1234 jl, cn->blocknr, 1);
1236 last = cn;
1237 cn = cn->next;
1238 free_cnode(p_s_sb, last);
1240 jl->j_realblock = NULL;
1244 ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1245 ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1246 ** releasing blocks in this transaction for reuse as data blocks.
1247 ** called by flush_journal_list, before it calls remove_all_from_journal_list
1250 static int _update_journal_header_block(struct super_block *p_s_sb,
1251 unsigned long offset,
1252 unsigned long trans_id)
1254 struct reiserfs_journal_header *jh;
1255 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1257 if (reiserfs_is_journal_aborted(journal))
1258 return -EIO;
1260 if (trans_id >= journal->j_last_flush_trans_id) {
1261 if (buffer_locked((journal->j_header_bh))) {
1262 wait_on_buffer((journal->j_header_bh));
1263 if (unlikely(!buffer_uptodate(journal->j_header_bh))) {
1264 #ifdef CONFIG_REISERFS_CHECK
1265 reiserfs_warning(p_s_sb,
1266 "journal-699: buffer write failed");
1267 #endif
1268 return -EIO;
1271 journal->j_last_flush_trans_id = trans_id;
1272 journal->j_first_unflushed_offset = offset;
1273 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->
1274 b_data);
1275 jh->j_last_flush_trans_id = cpu_to_le32(trans_id);
1276 jh->j_first_unflushed_offset = cpu_to_le32(offset);
1277 jh->j_mount_id = cpu_to_le32(journal->j_mount_id);
1279 if (reiserfs_barrier_flush(p_s_sb)) {
1280 int ret;
1281 lock_buffer(journal->j_header_bh);
1282 ret = submit_barrier_buffer(journal->j_header_bh);
1283 if (ret == -EOPNOTSUPP) {
1284 set_buffer_uptodate(journal->j_header_bh);
1285 disable_barrier(p_s_sb);
1286 goto sync;
1288 wait_on_buffer(journal->j_header_bh);
1289 check_barrier_completion(p_s_sb, journal->j_header_bh);
1290 } else {
1291 sync:
1292 set_buffer_dirty(journal->j_header_bh);
1293 sync_dirty_buffer(journal->j_header_bh);
1295 if (!buffer_uptodate(journal->j_header_bh)) {
1296 reiserfs_warning(p_s_sb,
1297 "journal-837: IO error during journal replay");
1298 return -EIO;
1301 return 0;
1304 static int update_journal_header_block(struct super_block *p_s_sb,
1305 unsigned long offset,
1306 unsigned long trans_id)
1308 return _update_journal_header_block(p_s_sb, offset, trans_id);
1312 ** flush any and all journal lists older than you are
1313 ** can only be called from flush_journal_list
1315 static int flush_older_journal_lists(struct super_block *p_s_sb,
1316 struct reiserfs_journal_list *jl)
1318 struct list_head *entry;
1319 struct reiserfs_journal_list *other_jl;
1320 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1321 unsigned long trans_id = jl->j_trans_id;
1323 /* we know we are the only ones flushing things, no extra race
1324 * protection is required.
1326 restart:
1327 entry = journal->j_journal_list.next;
1328 /* Did we wrap? */
1329 if (entry == &journal->j_journal_list)
1330 return 0;
1331 other_jl = JOURNAL_LIST_ENTRY(entry);
1332 if (other_jl->j_trans_id < trans_id) {
1333 BUG_ON(other_jl->j_refcount <= 0);
1334 /* do not flush all */
1335 flush_journal_list(p_s_sb, other_jl, 0);
1337 /* other_jl is now deleted from the list */
1338 goto restart;
1340 return 0;
1343 static void del_from_work_list(struct super_block *s,
1344 struct reiserfs_journal_list *jl)
1346 struct reiserfs_journal *journal = SB_JOURNAL(s);
1347 if (!list_empty(&jl->j_working_list)) {
1348 list_del_init(&jl->j_working_list);
1349 journal->j_num_work_lists--;
1353 /* flush a journal list, both commit and real blocks
1355 ** always set flushall to 1, unless you are calling from inside
1356 ** flush_journal_list
1358 ** IMPORTANT. This can only be called while there are no journal writers,
1359 ** and the journal is locked. That means it can only be called from
1360 ** do_journal_end, or by journal_release
1362 static int flush_journal_list(struct super_block *s,
1363 struct reiserfs_journal_list *jl, int flushall)
1365 struct reiserfs_journal_list *pjl;
1366 struct reiserfs_journal_cnode *cn, *last;
1367 int count;
1368 int was_jwait = 0;
1369 int was_dirty = 0;
1370 struct buffer_head *saved_bh;
1371 unsigned long j_len_saved = jl->j_len;
1372 struct reiserfs_journal *journal = SB_JOURNAL(s);
1373 int err = 0;
1375 BUG_ON(j_len_saved <= 0);
1377 if (atomic_read(&journal->j_wcount) != 0) {
1378 reiserfs_warning(s,
1379 "clm-2048: flush_journal_list called with wcount %d",
1380 atomic_read(&journal->j_wcount));
1382 BUG_ON(jl->j_trans_id == 0);
1384 /* if flushall == 0, the lock is already held */
1385 if (flushall) {
1386 down(&journal->j_flush_sem);
1387 } else if (!down_trylock(&journal->j_flush_sem)) {
1388 BUG();
1391 count = 0;
1392 if (j_len_saved > journal->j_trans_max) {
1393 reiserfs_panic(s,
1394 "journal-715: flush_journal_list, length is %lu, trans id %lu\n",
1395 j_len_saved, jl->j_trans_id);
1396 return 0;
1399 get_fs_excl();
1401 /* if all the work is already done, get out of here */
1402 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1403 atomic_read(&(jl->j_commit_left)) <= 0) {
1404 goto flush_older_and_return;
1407 /* start by putting the commit list on disk. This will also flush
1408 ** the commit lists of any olders transactions
1410 flush_commit_list(s, jl, 1);
1412 if (!(jl->j_state & LIST_DIRTY)
1413 && !reiserfs_is_journal_aborted(journal))
1414 BUG();
1416 /* are we done now? */
1417 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1418 atomic_read(&(jl->j_commit_left)) <= 0) {
1419 goto flush_older_and_return;
1422 /* loop through each cnode, see if we need to write it,
1423 ** or wait on a more recent transaction, or just ignore it
1425 if (atomic_read(&(journal->j_wcount)) != 0) {
1426 reiserfs_panic(s,
1427 "journal-844: panic journal list is flushing, wcount is not 0\n");
1429 cn = jl->j_realblock;
1430 while (cn) {
1431 was_jwait = 0;
1432 was_dirty = 0;
1433 saved_bh = NULL;
1434 /* blocknr of 0 is no longer in the hash, ignore it */
1435 if (cn->blocknr == 0) {
1436 goto free_cnode;
1439 /* This transaction failed commit. Don't write out to the disk */
1440 if (!(jl->j_state & LIST_DIRTY))
1441 goto free_cnode;
1443 pjl = find_newer_jl_for_cn(cn);
1444 /* the order is important here. We check pjl to make sure we
1445 ** don't clear BH_JDirty_wait if we aren't the one writing this
1446 ** block to disk
1448 if (!pjl && cn->bh) {
1449 saved_bh = cn->bh;
1451 /* we do this to make sure nobody releases the buffer while
1452 ** we are working with it
1454 get_bh(saved_bh);
1456 if (buffer_journal_dirty(saved_bh)) {
1457 BUG_ON(!can_dirty(cn));
1458 was_jwait = 1;
1459 was_dirty = 1;
1460 } else if (can_dirty(cn)) {
1461 /* everything with !pjl && jwait should be writable */
1462 BUG();
1466 /* if someone has this block in a newer transaction, just make
1467 ** sure they are committed, and don't try writing it to disk
1469 if (pjl) {
1470 if (atomic_read(&pjl->j_commit_left))
1471 flush_commit_list(s, pjl, 1);
1472 goto free_cnode;
1475 /* bh == NULL when the block got to disk on its own, OR,
1476 ** the block got freed in a future transaction
1478 if (saved_bh == NULL) {
1479 goto free_cnode;
1482 /* this should never happen. kupdate_one_transaction has this list
1483 ** locked while it works, so we should never see a buffer here that
1484 ** is not marked JDirty_wait
1486 if ((!was_jwait) && !buffer_locked(saved_bh)) {
1487 reiserfs_warning(s,
1488 "journal-813: BAD! buffer %llu %cdirty %cjwait, "
1489 "not in a newer tranasction",
1490 (unsigned long long)saved_bh->
1491 b_blocknr, was_dirty ? ' ' : '!',
1492 was_jwait ? ' ' : '!');
1494 if (was_dirty) {
1495 /* we inc again because saved_bh gets decremented at free_cnode */
1496 get_bh(saved_bh);
1497 set_bit(BLOCK_NEEDS_FLUSH, &cn->state);
1498 lock_buffer(saved_bh);
1499 BUG_ON(cn->blocknr != saved_bh->b_blocknr);
1500 if (buffer_dirty(saved_bh))
1501 submit_logged_buffer(saved_bh);
1502 else
1503 unlock_buffer(saved_bh);
1504 count++;
1505 } else {
1506 reiserfs_warning(s,
1507 "clm-2082: Unable to flush buffer %llu in %s",
1508 (unsigned long long)saved_bh->
1509 b_blocknr, __FUNCTION__);
1511 free_cnode:
1512 last = cn;
1513 cn = cn->next;
1514 if (saved_bh) {
1515 /* we incremented this to keep others from taking the buffer head away */
1516 put_bh(saved_bh);
1517 if (atomic_read(&(saved_bh->b_count)) < 0) {
1518 reiserfs_warning(s,
1519 "journal-945: saved_bh->b_count < 0");
1523 if (count > 0) {
1524 cn = jl->j_realblock;
1525 while (cn) {
1526 if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1527 if (!cn->bh) {
1528 reiserfs_panic(s,
1529 "journal-1011: cn->bh is NULL\n");
1531 wait_on_buffer(cn->bh);
1532 if (!cn->bh) {
1533 reiserfs_panic(s,
1534 "journal-1012: cn->bh is NULL\n");
1536 if (unlikely(!buffer_uptodate(cn->bh))) {
1537 #ifdef CONFIG_REISERFS_CHECK
1538 reiserfs_warning(s,
1539 "journal-949: buffer write failed\n");
1540 #endif
1541 err = -EIO;
1543 /* note, we must clear the JDirty_wait bit after the up to date
1544 ** check, otherwise we race against our flushpage routine
1546 BUG_ON(!test_clear_buffer_journal_dirty
1547 (cn->bh));
1549 /* undo the inc from journal_mark_dirty */
1550 put_bh(cn->bh);
1551 brelse(cn->bh);
1553 cn = cn->next;
1557 if (err)
1558 reiserfs_abort(s, -EIO,
1559 "Write error while pushing transaction to disk in %s",
1560 __FUNCTION__);
1561 flush_older_and_return:
1563 /* before we can update the journal header block, we _must_ flush all
1564 ** real blocks from all older transactions to disk. This is because
1565 ** once the header block is updated, this transaction will not be
1566 ** replayed after a crash
1568 if (flushall) {
1569 flush_older_journal_lists(s, jl);
1572 err = journal->j_errno;
1573 /* before we can remove everything from the hash tables for this
1574 ** transaction, we must make sure it can never be replayed
1576 ** since we are only called from do_journal_end, we know for sure there
1577 ** are no allocations going on while we are flushing journal lists. So,
1578 ** we only need to update the journal header block for the last list
1579 ** being flushed
1581 if (!err && flushall) {
1582 err =
1583 update_journal_header_block(s,
1584 (jl->j_start + jl->j_len +
1585 2) % SB_ONDISK_JOURNAL_SIZE(s),
1586 jl->j_trans_id);
1587 if (err)
1588 reiserfs_abort(s, -EIO,
1589 "Write error while updating journal header in %s",
1590 __FUNCTION__);
1592 remove_all_from_journal_list(s, jl, 0);
1593 list_del_init(&jl->j_list);
1594 journal->j_num_lists--;
1595 del_from_work_list(s, jl);
1597 if (journal->j_last_flush_id != 0 &&
1598 (jl->j_trans_id - journal->j_last_flush_id) != 1) {
1599 reiserfs_warning(s, "clm-2201: last flush %lu, current %lu",
1600 journal->j_last_flush_id, jl->j_trans_id);
1602 journal->j_last_flush_id = jl->j_trans_id;
1604 /* not strictly required since we are freeing the list, but it should
1605 * help find code using dead lists later on
1607 jl->j_len = 0;
1608 atomic_set(&(jl->j_nonzerolen), 0);
1609 jl->j_start = 0;
1610 jl->j_realblock = NULL;
1611 jl->j_commit_bh = NULL;
1612 jl->j_trans_id = 0;
1613 jl->j_state = 0;
1614 put_journal_list(s, jl);
1615 if (flushall)
1616 up(&journal->j_flush_sem);
1617 put_fs_excl();
1618 return err;
1621 static int test_transaction(struct super_block *s,
1622 struct reiserfs_journal_list *jl)
1624 struct reiserfs_journal_cnode *cn;
1626 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0)
1627 return 1;
1629 cn = jl->j_realblock;
1630 while (cn) {
1631 /* if the blocknr == 0, this has been cleared from the hash,
1632 ** skip it
1634 if (cn->blocknr == 0) {
1635 goto next;
1637 if (cn->bh && !newer_jl_done(cn))
1638 return 0;
1639 next:
1640 cn = cn->next;
1641 cond_resched();
1643 return 0;
1646 static int write_one_transaction(struct super_block *s,
1647 struct reiserfs_journal_list *jl,
1648 struct buffer_chunk *chunk)
1650 struct reiserfs_journal_cnode *cn;
1651 int ret = 0;
1653 jl->j_state |= LIST_TOUCHED;
1654 del_from_work_list(s, jl);
1655 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
1656 return 0;
1659 cn = jl->j_realblock;
1660 while (cn) {
1661 /* if the blocknr == 0, this has been cleared from the hash,
1662 ** skip it
1664 if (cn->blocknr == 0) {
1665 goto next;
1667 if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
1668 struct buffer_head *tmp_bh;
1669 /* we can race against journal_mark_freed when we try
1670 * to lock_buffer(cn->bh), so we have to inc the buffer
1671 * count, and recheck things after locking
1673 tmp_bh = cn->bh;
1674 get_bh(tmp_bh);
1675 lock_buffer(tmp_bh);
1676 if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
1677 if (!buffer_journal_dirty(tmp_bh) ||
1678 buffer_journal_prepared(tmp_bh))
1679 BUG();
1680 add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
1681 ret++;
1682 } else {
1683 /* note, cn->bh might be null now */
1684 unlock_buffer(tmp_bh);
1686 put_bh(tmp_bh);
1688 next:
1689 cn = cn->next;
1690 cond_resched();
1692 return ret;
1695 /* used by flush_commit_list */
1696 static int dirty_one_transaction(struct super_block *s,
1697 struct reiserfs_journal_list *jl)
1699 struct reiserfs_journal_cnode *cn;
1700 struct reiserfs_journal_list *pjl;
1701 int ret = 0;
1703 jl->j_state |= LIST_DIRTY;
1704 cn = jl->j_realblock;
1705 while (cn) {
1706 /* look for a more recent transaction that logged this
1707 ** buffer. Only the most recent transaction with a buffer in
1708 ** it is allowed to send that buffer to disk
1710 pjl = find_newer_jl_for_cn(cn);
1711 if (!pjl && cn->blocknr && cn->bh
1712 && buffer_journal_dirty(cn->bh)) {
1713 BUG_ON(!can_dirty(cn));
1714 /* if the buffer is prepared, it will either be logged
1715 * or restored. If restored, we need to make sure
1716 * it actually gets marked dirty
1718 clear_buffer_journal_new(cn->bh);
1719 if (buffer_journal_prepared(cn->bh)) {
1720 set_buffer_journal_restore_dirty(cn->bh);
1721 } else {
1722 set_buffer_journal_test(cn->bh);
1723 mark_buffer_dirty(cn->bh);
1726 cn = cn->next;
1728 return ret;
1731 static int kupdate_transactions(struct super_block *s,
1732 struct reiserfs_journal_list *jl,
1733 struct reiserfs_journal_list **next_jl,
1734 unsigned long *next_trans_id,
1735 int num_blocks, int num_trans)
1737 int ret = 0;
1738 int written = 0;
1739 int transactions_flushed = 0;
1740 unsigned long orig_trans_id = jl->j_trans_id;
1741 struct buffer_chunk chunk;
1742 struct list_head *entry;
1743 struct reiserfs_journal *journal = SB_JOURNAL(s);
1744 chunk.nr = 0;
1746 down(&journal->j_flush_sem);
1747 if (!journal_list_still_alive(s, orig_trans_id)) {
1748 goto done;
1751 /* we've got j_flush_sem held, nobody is going to delete any
1752 * of these lists out from underneath us
1754 while ((num_trans && transactions_flushed < num_trans) ||
1755 (!num_trans && written < num_blocks)) {
1757 if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
1758 atomic_read(&jl->j_commit_left)
1759 || !(jl->j_state & LIST_DIRTY)) {
1760 del_from_work_list(s, jl);
1761 break;
1763 ret = write_one_transaction(s, jl, &chunk);
1765 if (ret < 0)
1766 goto done;
1767 transactions_flushed++;
1768 written += ret;
1769 entry = jl->j_list.next;
1771 /* did we wrap? */
1772 if (entry == &journal->j_journal_list) {
1773 break;
1775 jl = JOURNAL_LIST_ENTRY(entry);
1777 /* don't bother with older transactions */
1778 if (jl->j_trans_id <= orig_trans_id)
1779 break;
1781 if (chunk.nr) {
1782 write_chunk(&chunk);
1785 done:
1786 up(&journal->j_flush_sem);
1787 return ret;
1790 /* for o_sync and fsync heavy applications, they tend to use
1791 ** all the journa list slots with tiny transactions. These
1792 ** trigger lots and lots of calls to update the header block, which
1793 ** adds seeks and slows things down.
1795 ** This function tries to clear out a large chunk of the journal lists
1796 ** at once, which makes everything faster since only the newest journal
1797 ** list updates the header block
1799 static int flush_used_journal_lists(struct super_block *s,
1800 struct reiserfs_journal_list *jl)
1802 unsigned long len = 0;
1803 unsigned long cur_len;
1804 int ret;
1805 int i;
1806 int limit = 256;
1807 struct reiserfs_journal_list *tjl;
1808 struct reiserfs_journal_list *flush_jl;
1809 unsigned long trans_id;
1810 struct reiserfs_journal *journal = SB_JOURNAL(s);
1812 flush_jl = tjl = jl;
1814 /* in data logging mode, try harder to flush a lot of blocks */
1815 if (reiserfs_data_log(s))
1816 limit = 1024;
1817 /* flush for 256 transactions or limit blocks, whichever comes first */
1818 for (i = 0; i < 256 && len < limit; i++) {
1819 if (atomic_read(&tjl->j_commit_left) ||
1820 tjl->j_trans_id < jl->j_trans_id) {
1821 break;
1823 cur_len = atomic_read(&tjl->j_nonzerolen);
1824 if (cur_len > 0) {
1825 tjl->j_state &= ~LIST_TOUCHED;
1827 len += cur_len;
1828 flush_jl = tjl;
1829 if (tjl->j_list.next == &journal->j_journal_list)
1830 break;
1831 tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
1833 /* try to find a group of blocks we can flush across all the
1834 ** transactions, but only bother if we've actually spanned
1835 ** across multiple lists
1837 if (flush_jl != jl) {
1838 ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
1840 flush_journal_list(s, flush_jl, 1);
1841 return 0;
1845 ** removes any nodes in table with name block and dev as bh.
1846 ** only touchs the hnext and hprev pointers.
1848 void remove_journal_hash(struct super_block *sb,
1849 struct reiserfs_journal_cnode **table,
1850 struct reiserfs_journal_list *jl,
1851 unsigned long block, int remove_freed)
1853 struct reiserfs_journal_cnode *cur;
1854 struct reiserfs_journal_cnode **head;
1856 head = &(journal_hash(table, sb, block));
1857 if (!head) {
1858 return;
1860 cur = *head;
1861 while (cur) {
1862 if (cur->blocknr == block && cur->sb == sb
1863 && (jl == NULL || jl == cur->jlist)
1864 && (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1865 if (cur->hnext) {
1866 cur->hnext->hprev = cur->hprev;
1868 if (cur->hprev) {
1869 cur->hprev->hnext = cur->hnext;
1870 } else {
1871 *head = cur->hnext;
1873 cur->blocknr = 0;
1874 cur->sb = NULL;
1875 cur->state = 0;
1876 if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1877 atomic_dec(&(cur->jlist->j_nonzerolen));
1878 cur->bh = NULL;
1879 cur->jlist = NULL;
1881 cur = cur->hnext;
1885 static void free_journal_ram(struct super_block *p_s_sb)
1887 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1888 kfree(journal->j_current_jl);
1889 journal->j_num_lists--;
1891 vfree(journal->j_cnode_free_orig);
1892 free_list_bitmaps(p_s_sb, journal->j_list_bitmap);
1893 free_bitmap_nodes(p_s_sb); /* must be after free_list_bitmaps */
1894 if (journal->j_header_bh) {
1895 brelse(journal->j_header_bh);
1897 /* j_header_bh is on the journal dev, make sure not to release the journal
1898 * dev until we brelse j_header_bh
1900 release_journal_dev(p_s_sb, journal);
1901 vfree(journal);
1905 ** call on unmount. Only set error to 1 if you haven't made your way out
1906 ** of read_super() yet. Any other caller must keep error at 0.
1908 static int do_journal_release(struct reiserfs_transaction_handle *th,
1909 struct super_block *p_s_sb, int error)
1911 struct reiserfs_transaction_handle myth;
1912 int flushed = 0;
1913 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1915 /* we only want to flush out transactions if we were called with error == 0
1917 if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
1918 /* end the current trans */
1919 BUG_ON(!th->t_trans_id);
1920 do_journal_end(th, p_s_sb, 10, FLUSH_ALL);
1922 /* make sure something gets logged to force our way into the flush code */
1923 if (!journal_join(&myth, p_s_sb, 1)) {
1924 reiserfs_prepare_for_journal(p_s_sb,
1925 SB_BUFFER_WITH_SB(p_s_sb),
1927 journal_mark_dirty(&myth, p_s_sb,
1928 SB_BUFFER_WITH_SB(p_s_sb));
1929 do_journal_end(&myth, p_s_sb, 1, FLUSH_ALL);
1930 flushed = 1;
1934 /* this also catches errors during the do_journal_end above */
1935 if (!error && reiserfs_is_journal_aborted(journal)) {
1936 memset(&myth, 0, sizeof(myth));
1937 if (!journal_join_abort(&myth, p_s_sb, 1)) {
1938 reiserfs_prepare_for_journal(p_s_sb,
1939 SB_BUFFER_WITH_SB(p_s_sb),
1941 journal_mark_dirty(&myth, p_s_sb,
1942 SB_BUFFER_WITH_SB(p_s_sb));
1943 do_journal_end(&myth, p_s_sb, 1, FLUSH_ALL);
1947 reiserfs_mounted_fs_count--;
1948 /* wait for all commits to finish */
1949 cancel_delayed_work(&SB_JOURNAL(p_s_sb)->j_work);
1950 flush_workqueue(commit_wq);
1951 if (!reiserfs_mounted_fs_count) {
1952 destroy_workqueue(commit_wq);
1953 commit_wq = NULL;
1956 free_journal_ram(p_s_sb);
1958 return 0;
1962 ** call on unmount. flush all journal trans, release all alloc'd ram
1964 int journal_release(struct reiserfs_transaction_handle *th,
1965 struct super_block *p_s_sb)
1967 return do_journal_release(th, p_s_sb, 0);
1971 ** only call from an error condition inside reiserfs_read_super!
1973 int journal_release_error(struct reiserfs_transaction_handle *th,
1974 struct super_block *p_s_sb)
1976 return do_journal_release(th, p_s_sb, 1);
1979 /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
1980 static int journal_compare_desc_commit(struct super_block *p_s_sb,
1981 struct reiserfs_journal_desc *desc,
1982 struct reiserfs_journal_commit *commit)
1984 if (get_commit_trans_id(commit) != get_desc_trans_id(desc) ||
1985 get_commit_trans_len(commit) != get_desc_trans_len(desc) ||
1986 get_commit_trans_len(commit) > SB_JOURNAL(p_s_sb)->j_trans_max ||
1987 get_commit_trans_len(commit) <= 0) {
1988 return 1;
1990 return 0;
1993 /* returns 0 if it did not find a description block
1994 ** returns -1 if it found a corrupt commit block
1995 ** returns 1 if both desc and commit were valid
1997 static int journal_transaction_is_valid(struct super_block *p_s_sb,
1998 struct buffer_head *d_bh,
1999 unsigned long *oldest_invalid_trans_id,
2000 unsigned long *newest_mount_id)
2002 struct reiserfs_journal_desc *desc;
2003 struct reiserfs_journal_commit *commit;
2004 struct buffer_head *c_bh;
2005 unsigned long offset;
2007 if (!d_bh)
2008 return 0;
2010 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2011 if (get_desc_trans_len(desc) > 0
2012 && !memcmp(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8)) {
2013 if (oldest_invalid_trans_id && *oldest_invalid_trans_id
2014 && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
2015 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2016 "journal-986: transaction "
2017 "is valid returning because trans_id %d is greater than "
2018 "oldest_invalid %lu",
2019 get_desc_trans_id(desc),
2020 *oldest_invalid_trans_id);
2021 return 0;
2023 if (newest_mount_id
2024 && *newest_mount_id > get_desc_mount_id(desc)) {
2025 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2026 "journal-1087: transaction "
2027 "is valid returning because mount_id %d is less than "
2028 "newest_mount_id %lu",
2029 get_desc_mount_id(desc),
2030 *newest_mount_id);
2031 return -1;
2033 if (get_desc_trans_len(desc) > SB_JOURNAL(p_s_sb)->j_trans_max) {
2034 reiserfs_warning(p_s_sb,
2035 "journal-2018: Bad transaction length %d encountered, ignoring transaction",
2036 get_desc_trans_len(desc));
2037 return -1;
2039 offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2041 /* ok, we have a journal description block, lets see if the transaction was valid */
2042 c_bh =
2043 journal_bread(p_s_sb,
2044 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2045 ((offset + get_desc_trans_len(desc) +
2046 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
2047 if (!c_bh)
2048 return 0;
2049 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
2050 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
2051 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2052 "journal_transaction_is_valid, commit offset %ld had bad "
2053 "time %d or length %d",
2054 c_bh->b_blocknr -
2055 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2056 get_commit_trans_id(commit),
2057 get_commit_trans_len(commit));
2058 brelse(c_bh);
2059 if (oldest_invalid_trans_id) {
2060 *oldest_invalid_trans_id =
2061 get_desc_trans_id(desc);
2062 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2063 "journal-1004: "
2064 "transaction_is_valid setting oldest invalid trans_id "
2065 "to %d",
2066 get_desc_trans_id(desc));
2068 return -1;
2070 brelse(c_bh);
2071 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2072 "journal-1006: found valid "
2073 "transaction start offset %llu, len %d id %d",
2074 d_bh->b_blocknr -
2075 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2076 get_desc_trans_len(desc),
2077 get_desc_trans_id(desc));
2078 return 1;
2079 } else {
2080 return 0;
2084 static void brelse_array(struct buffer_head **heads, int num)
2086 int i;
2087 for (i = 0; i < num; i++) {
2088 brelse(heads[i]);
2093 ** given the start, and values for the oldest acceptable transactions,
2094 ** this either reads in a replays a transaction, or returns because the transaction
2095 ** is invalid, or too old.
2097 static int journal_read_transaction(struct super_block *p_s_sb,
2098 unsigned long cur_dblock,
2099 unsigned long oldest_start,
2100 unsigned long oldest_trans_id,
2101 unsigned long newest_mount_id)
2103 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2104 struct reiserfs_journal_desc *desc;
2105 struct reiserfs_journal_commit *commit;
2106 unsigned long trans_id = 0;
2107 struct buffer_head *c_bh;
2108 struct buffer_head *d_bh;
2109 struct buffer_head **log_blocks = NULL;
2110 struct buffer_head **real_blocks = NULL;
2111 unsigned long trans_offset;
2112 int i;
2113 int trans_half;
2115 d_bh = journal_bread(p_s_sb, cur_dblock);
2116 if (!d_bh)
2117 return 1;
2118 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2119 trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2120 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
2121 "journal_read_transaction, offset %llu, len %d mount_id %d",
2122 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2123 get_desc_trans_len(desc), get_desc_mount_id(desc));
2124 if (get_desc_trans_id(desc) < oldest_trans_id) {
2125 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
2126 "journal_read_trans skipping because %lu is too old",
2127 cur_dblock -
2128 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb));
2129 brelse(d_bh);
2130 return 1;
2132 if (get_desc_mount_id(desc) != newest_mount_id) {
2133 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
2134 "journal_read_trans skipping because %d is != "
2135 "newest_mount_id %lu", get_desc_mount_id(desc),
2136 newest_mount_id);
2137 brelse(d_bh);
2138 return 1;
2140 c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2141 ((trans_offset + get_desc_trans_len(desc) + 1) %
2142 SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
2143 if (!c_bh) {
2144 brelse(d_bh);
2145 return 1;
2147 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
2148 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
2149 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2150 "journal_read_transaction, "
2151 "commit offset %llu had bad time %d or length %d",
2152 c_bh->b_blocknr -
2153 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2154 get_commit_trans_id(commit),
2155 get_commit_trans_len(commit));
2156 brelse(c_bh);
2157 brelse(d_bh);
2158 return 1;
2160 trans_id = get_desc_trans_id(desc);
2161 /* now we know we've got a good transaction, and it was inside the valid time ranges */
2162 log_blocks = kmalloc(get_desc_trans_len(desc) *
2163 sizeof(struct buffer_head *), GFP_NOFS);
2164 real_blocks = kmalloc(get_desc_trans_len(desc) *
2165 sizeof(struct buffer_head *), GFP_NOFS);
2166 if (!log_blocks || !real_blocks) {
2167 brelse(c_bh);
2168 brelse(d_bh);
2169 kfree(log_blocks);
2170 kfree(real_blocks);
2171 reiserfs_warning(p_s_sb,
2172 "journal-1169: kmalloc failed, unable to mount FS");
2173 return -1;
2175 /* get all the buffer heads */
2176 trans_half = journal_trans_half(p_s_sb->s_blocksize);
2177 for (i = 0; i < get_desc_trans_len(desc); i++) {
2178 log_blocks[i] =
2179 journal_getblk(p_s_sb,
2180 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2181 (trans_offset + 1 +
2182 i) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2183 if (i < trans_half) {
2184 real_blocks[i] =
2185 sb_getblk(p_s_sb,
2186 le32_to_cpu(desc->j_realblock[i]));
2187 } else {
2188 real_blocks[i] =
2189 sb_getblk(p_s_sb,
2190 le32_to_cpu(commit->
2191 j_realblock[i - trans_half]));
2193 if (real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(p_s_sb)) {
2194 reiserfs_warning(p_s_sb,
2195 "journal-1207: REPLAY FAILURE fsck required! Block to replay is outside of filesystem");
2196 goto abort_replay;
2198 /* make sure we don't try to replay onto log or reserved area */
2199 if (is_block_in_log_or_reserved_area
2200 (p_s_sb, real_blocks[i]->b_blocknr)) {
2201 reiserfs_warning(p_s_sb,
2202 "journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block");
2203 abort_replay:
2204 brelse_array(log_blocks, i);
2205 brelse_array(real_blocks, i);
2206 brelse(c_bh);
2207 brelse(d_bh);
2208 kfree(log_blocks);
2209 kfree(real_blocks);
2210 return -1;
2213 /* read in the log blocks, memcpy to the corresponding real block */
2214 ll_rw_block(READ, get_desc_trans_len(desc), log_blocks);
2215 for (i = 0; i < get_desc_trans_len(desc); i++) {
2216 wait_on_buffer(log_blocks[i]);
2217 if (!buffer_uptodate(log_blocks[i])) {
2218 reiserfs_warning(p_s_sb,
2219 "journal-1212: REPLAY FAILURE fsck required! buffer write failed");
2220 brelse_array(log_blocks + i,
2221 get_desc_trans_len(desc) - i);
2222 brelse_array(real_blocks, get_desc_trans_len(desc));
2223 brelse(c_bh);
2224 brelse(d_bh);
2225 kfree(log_blocks);
2226 kfree(real_blocks);
2227 return -1;
2229 memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data,
2230 real_blocks[i]->b_size);
2231 set_buffer_uptodate(real_blocks[i]);
2232 brelse(log_blocks[i]);
2234 /* flush out the real blocks */
2235 for (i = 0; i < get_desc_trans_len(desc); i++) {
2236 set_buffer_dirty(real_blocks[i]);
2237 ll_rw_block(SWRITE, 1, real_blocks + i);
2239 for (i = 0; i < get_desc_trans_len(desc); i++) {
2240 wait_on_buffer(real_blocks[i]);
2241 if (!buffer_uptodate(real_blocks[i])) {
2242 reiserfs_warning(p_s_sb,
2243 "journal-1226: REPLAY FAILURE, fsck required! buffer write failed");
2244 brelse_array(real_blocks + i,
2245 get_desc_trans_len(desc) - i);
2246 brelse(c_bh);
2247 brelse(d_bh);
2248 kfree(log_blocks);
2249 kfree(real_blocks);
2250 return -1;
2252 brelse(real_blocks[i]);
2254 cur_dblock =
2255 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2256 ((trans_offset + get_desc_trans_len(desc) +
2257 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2258 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2259 "journal-1095: setting journal " "start to offset %ld",
2260 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb));
2262 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
2263 journal->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2264 journal->j_last_flush_trans_id = trans_id;
2265 journal->j_trans_id = trans_id + 1;
2266 /* check for trans_id overflow */
2267 if (journal->j_trans_id == 0)
2268 journal->j_trans_id = 10;
2269 brelse(c_bh);
2270 brelse(d_bh);
2271 kfree(log_blocks);
2272 kfree(real_blocks);
2273 return 0;
2276 /* This function reads blocks starting from block and to max_block of bufsize
2277 size (but no more than BUFNR blocks at a time). This proved to improve
2278 mounting speed on self-rebuilding raid5 arrays at least.
2279 Right now it is only used from journal code. But later we might use it
2280 from other places.
2281 Note: Do not use journal_getblk/sb_getblk functions here! */
2282 static struct buffer_head *reiserfs_breada(struct block_device *dev, int block,
2283 int bufsize, unsigned int max_block)
2285 struct buffer_head *bhlist[BUFNR];
2286 unsigned int blocks = BUFNR;
2287 struct buffer_head *bh;
2288 int i, j;
2290 bh = __getblk(dev, block, bufsize);
2291 if (buffer_uptodate(bh))
2292 return (bh);
2294 if (block + BUFNR > max_block) {
2295 blocks = max_block - block;
2297 bhlist[0] = bh;
2298 j = 1;
2299 for (i = 1; i < blocks; i++) {
2300 bh = __getblk(dev, block + i, bufsize);
2301 if (buffer_uptodate(bh)) {
2302 brelse(bh);
2303 break;
2304 } else
2305 bhlist[j++] = bh;
2307 ll_rw_block(READ, j, bhlist);
2308 for (i = 1; i < j; i++)
2309 brelse(bhlist[i]);
2310 bh = bhlist[0];
2311 wait_on_buffer(bh);
2312 if (buffer_uptodate(bh))
2313 return bh;
2314 brelse(bh);
2315 return NULL;
2319 ** read and replay the log
2320 ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2321 ** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
2323 ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2325 ** On exit, it sets things up so the first transaction will work correctly.
2327 static int journal_read(struct super_block *p_s_sb)
2329 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2330 struct reiserfs_journal_desc *desc;
2331 unsigned long oldest_trans_id = 0;
2332 unsigned long oldest_invalid_trans_id = 0;
2333 time_t start;
2334 unsigned long oldest_start = 0;
2335 unsigned long cur_dblock = 0;
2336 unsigned long newest_mount_id = 9;
2337 struct buffer_head *d_bh;
2338 struct reiserfs_journal_header *jh;
2339 int valid_journal_header = 0;
2340 int replay_count = 0;
2341 int continue_replay = 1;
2342 int ret;
2343 char b[BDEVNAME_SIZE];
2345 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2346 reiserfs_info(p_s_sb, "checking transaction log (%s)\n",
2347 bdevname(journal->j_dev_bd, b));
2348 start = get_seconds();
2350 /* step 1, read in the journal header block. Check the transaction it says
2351 ** is the first unflushed, and if that transaction is not valid,
2352 ** replay is done
2354 journal->j_header_bh = journal_bread(p_s_sb,
2355 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)
2356 + SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2357 if (!journal->j_header_bh) {
2358 return 1;
2360 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data);
2361 if (le32_to_cpu(jh->j_first_unflushed_offset) <
2362 SB_ONDISK_JOURNAL_SIZE(p_s_sb)
2363 && le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
2364 oldest_start =
2365 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2366 le32_to_cpu(jh->j_first_unflushed_offset);
2367 oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2368 newest_mount_id = le32_to_cpu(jh->j_mount_id);
2369 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2370 "journal-1153: found in "
2371 "header: first_unflushed_offset %d, last_flushed_trans_id "
2372 "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
2373 le32_to_cpu(jh->j_last_flush_trans_id));
2374 valid_journal_header = 1;
2376 /* now, we try to read the first unflushed offset. If it is not valid,
2377 ** there is nothing more we can do, and it makes no sense to read
2378 ** through the whole log.
2380 d_bh =
2381 journal_bread(p_s_sb,
2382 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2383 le32_to_cpu(jh->j_first_unflushed_offset));
2384 ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL);
2385 if (!ret) {
2386 continue_replay = 0;
2388 brelse(d_bh);
2389 goto start_log_replay;
2392 if (continue_replay && bdev_read_only(p_s_sb->s_bdev)) {
2393 reiserfs_warning(p_s_sb,
2394 "clm-2076: device is readonly, unable to replay log");
2395 return -1;
2398 /* ok, there are transactions that need to be replayed. start with the first log block, find
2399 ** all the valid transactions, and pick out the oldest.
2401 while (continue_replay
2402 && cur_dblock <
2403 (SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2404 SB_ONDISK_JOURNAL_SIZE(p_s_sb))) {
2405 /* Note that it is required for blocksize of primary fs device and journal
2406 device to be the same */
2407 d_bh =
2408 reiserfs_breada(journal->j_dev_bd, cur_dblock,
2409 p_s_sb->s_blocksize,
2410 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2411 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2412 ret =
2413 journal_transaction_is_valid(p_s_sb, d_bh,
2414 &oldest_invalid_trans_id,
2415 &newest_mount_id);
2416 if (ret == 1) {
2417 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2418 if (oldest_start == 0) { /* init all oldest_ values */
2419 oldest_trans_id = get_desc_trans_id(desc);
2420 oldest_start = d_bh->b_blocknr;
2421 newest_mount_id = get_desc_mount_id(desc);
2422 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2423 "journal-1179: Setting "
2424 "oldest_start to offset %llu, trans_id %lu",
2425 oldest_start -
2426 SB_ONDISK_JOURNAL_1st_BLOCK
2427 (p_s_sb), oldest_trans_id);
2428 } else if (oldest_trans_id > get_desc_trans_id(desc)) {
2429 /* one we just read was older */
2430 oldest_trans_id = get_desc_trans_id(desc);
2431 oldest_start = d_bh->b_blocknr;
2432 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2433 "journal-1180: Resetting "
2434 "oldest_start to offset %lu, trans_id %lu",
2435 oldest_start -
2436 SB_ONDISK_JOURNAL_1st_BLOCK
2437 (p_s_sb), oldest_trans_id);
2439 if (newest_mount_id < get_desc_mount_id(desc)) {
2440 newest_mount_id = get_desc_mount_id(desc);
2441 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2442 "journal-1299: Setting "
2443 "newest_mount_id to %d",
2444 get_desc_mount_id(desc));
2446 cur_dblock += get_desc_trans_len(desc) + 2;
2447 } else {
2448 cur_dblock++;
2450 brelse(d_bh);
2453 start_log_replay:
2454 cur_dblock = oldest_start;
2455 if (oldest_trans_id) {
2456 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2457 "journal-1206: Starting replay "
2458 "from offset %llu, trans_id %lu",
2459 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2460 oldest_trans_id);
2463 replay_count = 0;
2464 while (continue_replay && oldest_trans_id > 0) {
2465 ret =
2466 journal_read_transaction(p_s_sb, cur_dblock, oldest_start,
2467 oldest_trans_id, newest_mount_id);
2468 if (ret < 0) {
2469 return ret;
2470 } else if (ret != 0) {
2471 break;
2473 cur_dblock =
2474 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + journal->j_start;
2475 replay_count++;
2476 if (cur_dblock == oldest_start)
2477 break;
2480 if (oldest_trans_id == 0) {
2481 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2482 "journal-1225: No valid " "transactions found");
2484 /* j_start does not get set correctly if we don't replay any transactions.
2485 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2486 ** copy the trans_id from the header
2488 if (valid_journal_header && replay_count == 0) {
2489 journal->j_start = le32_to_cpu(jh->j_first_unflushed_offset);
2490 journal->j_trans_id =
2491 le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2492 /* check for trans_id overflow */
2493 if (journal->j_trans_id == 0)
2494 journal->j_trans_id = 10;
2495 journal->j_last_flush_trans_id =
2496 le32_to_cpu(jh->j_last_flush_trans_id);
2497 journal->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
2498 } else {
2499 journal->j_mount_id = newest_mount_id + 1;
2501 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
2502 "newest_mount_id to %lu", journal->j_mount_id);
2503 journal->j_first_unflushed_offset = journal->j_start;
2504 if (replay_count > 0) {
2505 reiserfs_info(p_s_sb,
2506 "replayed %d transactions in %lu seconds\n",
2507 replay_count, get_seconds() - start);
2509 if (!bdev_read_only(p_s_sb->s_bdev) &&
2510 _update_journal_header_block(p_s_sb, journal->j_start,
2511 journal->j_last_flush_trans_id)) {
2512 /* replay failed, caller must call free_journal_ram and abort
2513 ** the mount
2515 return -1;
2517 return 0;
2520 static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
2522 struct reiserfs_journal_list *jl;
2523 jl = kzalloc(sizeof(struct reiserfs_journal_list),
2524 GFP_NOFS | __GFP_NOFAIL);
2525 INIT_LIST_HEAD(&jl->j_list);
2526 INIT_LIST_HEAD(&jl->j_working_list);
2527 INIT_LIST_HEAD(&jl->j_tail_bh_list);
2528 INIT_LIST_HEAD(&jl->j_bh_list);
2529 sema_init(&jl->j_commit_lock, 1);
2530 SB_JOURNAL(s)->j_num_lists++;
2531 get_journal_list(jl);
2532 return jl;
2535 static void journal_list_init(struct super_block *p_s_sb)
2537 SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
2540 static int release_journal_dev(struct super_block *super,
2541 struct reiserfs_journal *journal)
2543 int result;
2545 result = 0;
2547 if (journal->j_dev_file != NULL) {
2548 result = filp_close(journal->j_dev_file, NULL);
2549 journal->j_dev_file = NULL;
2550 journal->j_dev_bd = NULL;
2551 } else if (journal->j_dev_bd != NULL) {
2552 result = blkdev_put(journal->j_dev_bd);
2553 journal->j_dev_bd = NULL;
2556 if (result != 0) {
2557 reiserfs_warning(super,
2558 "sh-457: release_journal_dev: Cannot release journal device: %i",
2559 result);
2561 return result;
2564 static int journal_init_dev(struct super_block *super,
2565 struct reiserfs_journal *journal,
2566 const char *jdev_name)
2568 int result;
2569 dev_t jdev;
2570 int blkdev_mode = FMODE_READ | FMODE_WRITE;
2571 char b[BDEVNAME_SIZE];
2573 result = 0;
2575 journal->j_dev_bd = NULL;
2576 journal->j_dev_file = NULL;
2577 jdev = SB_ONDISK_JOURNAL_DEVICE(super) ?
2578 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
2580 if (bdev_read_only(super->s_bdev))
2581 blkdev_mode = FMODE_READ;
2583 /* there is no "jdev" option and journal is on separate device */
2584 if ((!jdev_name || !jdev_name[0])) {
2585 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode);
2586 if (IS_ERR(journal->j_dev_bd)) {
2587 result = PTR_ERR(journal->j_dev_bd);
2588 journal->j_dev_bd = NULL;
2589 reiserfs_warning(super, "sh-458: journal_init_dev: "
2590 "cannot init journal device '%s': %i",
2591 __bdevname(jdev, b), result);
2592 return result;
2593 } else if (jdev != super->s_dev)
2594 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2595 return 0;
2598 journal->j_dev_file = filp_open(jdev_name, 0, 0);
2599 if (!IS_ERR(journal->j_dev_file)) {
2600 struct inode *jdev_inode = journal->j_dev_file->f_mapping->host;
2601 if (!S_ISBLK(jdev_inode->i_mode)) {
2602 reiserfs_warning(super, "journal_init_dev: '%s' is "
2603 "not a block device", jdev_name);
2604 result = -ENOTBLK;
2605 release_journal_dev(super, journal);
2606 } else {
2607 /* ok */
2608 journal->j_dev_bd = I_BDEV(jdev_inode);
2609 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2610 reiserfs_info(super,
2611 "journal_init_dev: journal device: %s\n",
2612 bdevname(journal->j_dev_bd, b));
2614 } else {
2615 result = PTR_ERR(journal->j_dev_file);
2616 journal->j_dev_file = NULL;
2617 reiserfs_warning(super,
2618 "journal_init_dev: Cannot open '%s': %i",
2619 jdev_name, result);
2621 return result;
2625 ** must be called once on fs mount. calls journal_read for you
2627 int journal_init(struct super_block *p_s_sb, const char *j_dev_name,
2628 int old_format, unsigned int commit_max_age)
2630 int num_cnodes = SB_ONDISK_JOURNAL_SIZE(p_s_sb) * 2;
2631 struct buffer_head *bhjh;
2632 struct reiserfs_super_block *rs;
2633 struct reiserfs_journal_header *jh;
2634 struct reiserfs_journal *journal;
2635 struct reiserfs_journal_list *jl;
2636 char b[BDEVNAME_SIZE];
2638 journal = SB_JOURNAL(p_s_sb) = vmalloc(sizeof(struct reiserfs_journal));
2639 if (!journal) {
2640 reiserfs_warning(p_s_sb,
2641 "journal-1256: unable to get memory for journal structure");
2642 return 1;
2644 memset(journal, 0, sizeof(struct reiserfs_journal));
2645 INIT_LIST_HEAD(&journal->j_bitmap_nodes);
2646 INIT_LIST_HEAD(&journal->j_prealloc_list);
2647 INIT_LIST_HEAD(&journal->j_working_list);
2648 INIT_LIST_HEAD(&journal->j_journal_list);
2649 journal->j_persistent_trans = 0;
2650 if (reiserfs_allocate_list_bitmaps(p_s_sb,
2651 journal->j_list_bitmap,
2652 SB_BMAP_NR(p_s_sb)))
2653 goto free_and_return;
2654 allocate_bitmap_nodes(p_s_sb);
2656 /* reserved for journal area support */
2657 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) = (old_format ?
2658 REISERFS_OLD_DISK_OFFSET_IN_BYTES
2659 / p_s_sb->s_blocksize +
2660 SB_BMAP_NR(p_s_sb) +
2662 REISERFS_DISK_OFFSET_IN_BYTES /
2663 p_s_sb->s_blocksize + 2);
2665 /* Sanity check to see is the standard journal fitting withing first bitmap
2666 (actual for small blocksizes) */
2667 if (!SB_ONDISK_JOURNAL_DEVICE(p_s_sb) &&
2668 (SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) +
2669 SB_ONDISK_JOURNAL_SIZE(p_s_sb) > p_s_sb->s_blocksize * 8)) {
2670 reiserfs_warning(p_s_sb,
2671 "journal-1393: journal does not fit for area "
2672 "addressed by first of bitmap blocks. It starts at "
2673 "%u and its size is %u. Block size %ld",
2674 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb),
2675 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2676 p_s_sb->s_blocksize);
2677 goto free_and_return;
2680 if (journal_init_dev(p_s_sb, journal, j_dev_name) != 0) {
2681 reiserfs_warning(p_s_sb,
2682 "sh-462: unable to initialize jornal device");
2683 goto free_and_return;
2686 rs = SB_DISK_SUPER_BLOCK(p_s_sb);
2688 /* read journal header */
2689 bhjh = journal_bread(p_s_sb,
2690 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2691 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2692 if (!bhjh) {
2693 reiserfs_warning(p_s_sb,
2694 "sh-459: unable to read journal header");
2695 goto free_and_return;
2697 jh = (struct reiserfs_journal_header *)(bhjh->b_data);
2699 /* make sure that journal matches to the super block */
2700 if (is_reiserfs_jr(rs)
2701 && (le32_to_cpu(jh->jh_journal.jp_journal_magic) !=
2702 sb_jp_journal_magic(rs))) {
2703 reiserfs_warning(p_s_sb,
2704 "sh-460: journal header magic %x "
2705 "(device %s) does not match to magic found in super "
2706 "block %x", jh->jh_journal.jp_journal_magic,
2707 bdevname(journal->j_dev_bd, b),
2708 sb_jp_journal_magic(rs));
2709 brelse(bhjh);
2710 goto free_and_return;
2713 journal->j_trans_max = le32_to_cpu(jh->jh_journal.jp_journal_trans_max);
2714 journal->j_max_batch = le32_to_cpu(jh->jh_journal.jp_journal_max_batch);
2715 journal->j_max_commit_age =
2716 le32_to_cpu(jh->jh_journal.jp_journal_max_commit_age);
2717 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
2719 if (journal->j_trans_max) {
2720 /* make sure these parameters are available, assign it if they are not */
2721 __u32 initial = journal->j_trans_max;
2722 __u32 ratio = 1;
2724 if (p_s_sb->s_blocksize < 4096)
2725 ratio = 4096 / p_s_sb->s_blocksize;
2727 if (SB_ONDISK_JOURNAL_SIZE(p_s_sb) / journal->j_trans_max <
2728 JOURNAL_MIN_RATIO)
2729 journal->j_trans_max =
2730 SB_ONDISK_JOURNAL_SIZE(p_s_sb) / JOURNAL_MIN_RATIO;
2731 if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio)
2732 journal->j_trans_max =
2733 JOURNAL_TRANS_MAX_DEFAULT / ratio;
2734 if (journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio)
2735 journal->j_trans_max =
2736 JOURNAL_TRANS_MIN_DEFAULT / ratio;
2738 if (journal->j_trans_max != initial)
2739 reiserfs_warning(p_s_sb,
2740 "sh-461: journal_init: wrong transaction max size (%u). Changed to %u",
2741 initial, journal->j_trans_max);
2743 journal->j_max_batch = journal->j_trans_max *
2744 JOURNAL_MAX_BATCH_DEFAULT / JOURNAL_TRANS_MAX_DEFAULT;
2747 if (!journal->j_trans_max) {
2748 /*we have the file system was created by old version of mkreiserfs
2749 so this field contains zero value */
2750 journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT;
2751 journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT;
2752 journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE;
2754 /* for blocksize >= 4096 - max transaction size is 1024. For block size < 4096
2755 trans max size is decreased proportionally */
2756 if (p_s_sb->s_blocksize < 4096) {
2757 journal->j_trans_max /= (4096 / p_s_sb->s_blocksize);
2758 journal->j_max_batch = (journal->j_trans_max) * 9 / 10;
2762 journal->j_default_max_commit_age = journal->j_max_commit_age;
2764 if (commit_max_age != 0) {
2765 journal->j_max_commit_age = commit_max_age;
2766 journal->j_max_trans_age = commit_max_age;
2769 reiserfs_info(p_s_sb, "journal params: device %s, size %u, "
2770 "journal first block %u, max trans len %u, max batch %u, "
2771 "max commit age %u, max trans age %u\n",
2772 bdevname(journal->j_dev_bd, b),
2773 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2774 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2775 journal->j_trans_max,
2776 journal->j_max_batch,
2777 journal->j_max_commit_age, journal->j_max_trans_age);
2779 brelse(bhjh);
2781 journal->j_list_bitmap_index = 0;
2782 journal_list_init(p_s_sb);
2784 memset(journal->j_list_hash_table, 0,
2785 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
2787 INIT_LIST_HEAD(&journal->j_dirty_buffers);
2788 spin_lock_init(&journal->j_dirty_buffers_lock);
2790 journal->j_start = 0;
2791 journal->j_len = 0;
2792 journal->j_len_alloc = 0;
2793 atomic_set(&(journal->j_wcount), 0);
2794 atomic_set(&(journal->j_async_throttle), 0);
2795 journal->j_bcount = 0;
2796 journal->j_trans_start_time = 0;
2797 journal->j_last = NULL;
2798 journal->j_first = NULL;
2799 init_waitqueue_head(&(journal->j_join_wait));
2800 sema_init(&journal->j_lock, 1);
2801 sema_init(&journal->j_flush_sem, 1);
2803 journal->j_trans_id = 10;
2804 journal->j_mount_id = 10;
2805 journal->j_state = 0;
2806 atomic_set(&(journal->j_jlock), 0);
2807 journal->j_cnode_free_list = allocate_cnodes(num_cnodes);
2808 journal->j_cnode_free_orig = journal->j_cnode_free_list;
2809 journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0;
2810 journal->j_cnode_used = 0;
2811 journal->j_must_wait = 0;
2813 if (journal->j_cnode_free == 0) {
2814 reiserfs_warning(p_s_sb, "journal-2004: Journal cnode memory "
2815 "allocation failed (%ld bytes). Journal is "
2816 "too large for available memory. Usually "
2817 "this is due to a journal that is too large.",
2818 sizeof (struct reiserfs_journal_cnode) * num_cnodes);
2819 goto free_and_return;
2822 init_journal_hash(p_s_sb);
2823 jl = journal->j_current_jl;
2824 jl->j_list_bitmap = get_list_bitmap(p_s_sb, jl);
2825 if (!jl->j_list_bitmap) {
2826 reiserfs_warning(p_s_sb,
2827 "journal-2005, get_list_bitmap failed for journal list 0");
2828 goto free_and_return;
2830 if (journal_read(p_s_sb) < 0) {
2831 reiserfs_warning(p_s_sb, "Replay Failure, unable to mount");
2832 goto free_and_return;
2835 reiserfs_mounted_fs_count++;
2836 if (reiserfs_mounted_fs_count <= 1)
2837 commit_wq = create_workqueue("reiserfs");
2839 INIT_DELAYED_WORK(&journal->j_work, flush_async_commits);
2840 journal->j_work_sb = p_s_sb;
2841 return 0;
2842 free_and_return:
2843 free_journal_ram(p_s_sb);
2844 return 1;
2848 ** test for a polite end of the current transaction. Used by file_write, and should
2849 ** be used by delete to make sure they don't write more than can fit inside a single
2850 ** transaction
2852 int journal_transaction_should_end(struct reiserfs_transaction_handle *th,
2853 int new_alloc)
2855 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2856 time_t now = get_seconds();
2857 /* cannot restart while nested */
2858 BUG_ON(!th->t_trans_id);
2859 if (th->t_refcount > 1)
2860 return 0;
2861 if (journal->j_must_wait > 0 ||
2862 (journal->j_len_alloc + new_alloc) >= journal->j_max_batch ||
2863 atomic_read(&(journal->j_jlock)) ||
2864 (now - journal->j_trans_start_time) > journal->j_max_trans_age ||
2865 journal->j_cnode_free < (journal->j_trans_max * 3)) {
2866 return 1;
2868 /* protected by the BKL here */
2869 journal->j_len_alloc += new_alloc;
2870 th->t_blocks_allocated += new_alloc ;
2871 return 0;
2874 /* this must be called inside a transaction, and requires the
2875 ** kernel_lock to be held
2877 void reiserfs_block_writes(struct reiserfs_transaction_handle *th)
2879 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2880 BUG_ON(!th->t_trans_id);
2881 journal->j_must_wait = 1;
2882 set_bit(J_WRITERS_BLOCKED, &journal->j_state);
2883 return;
2886 /* this must be called without a transaction started, and does not
2887 ** require BKL
2889 void reiserfs_allow_writes(struct super_block *s)
2891 struct reiserfs_journal *journal = SB_JOURNAL(s);
2892 clear_bit(J_WRITERS_BLOCKED, &journal->j_state);
2893 wake_up(&journal->j_join_wait);
2896 /* this must be called without a transaction started, and does not
2897 ** require BKL
2899 void reiserfs_wait_on_write_block(struct super_block *s)
2901 struct reiserfs_journal *journal = SB_JOURNAL(s);
2902 wait_event(journal->j_join_wait,
2903 !test_bit(J_WRITERS_BLOCKED, &journal->j_state));
2906 static void queue_log_writer(struct super_block *s)
2908 wait_queue_t wait;
2909 struct reiserfs_journal *journal = SB_JOURNAL(s);
2910 set_bit(J_WRITERS_QUEUED, &journal->j_state);
2913 * we don't want to use wait_event here because
2914 * we only want to wait once.
2916 init_waitqueue_entry(&wait, current);
2917 add_wait_queue(&journal->j_join_wait, &wait);
2918 set_current_state(TASK_UNINTERRUPTIBLE);
2919 if (test_bit(J_WRITERS_QUEUED, &journal->j_state))
2920 schedule();
2921 current->state = TASK_RUNNING;
2922 remove_wait_queue(&journal->j_join_wait, &wait);
2925 static void wake_queued_writers(struct super_block *s)
2927 struct reiserfs_journal *journal = SB_JOURNAL(s);
2928 if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state))
2929 wake_up(&journal->j_join_wait);
2932 static void let_transaction_grow(struct super_block *sb, unsigned long trans_id)
2934 struct reiserfs_journal *journal = SB_JOURNAL(sb);
2935 unsigned long bcount = journal->j_bcount;
2936 while (1) {
2937 schedule_timeout_uninterruptible(1);
2938 journal->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2939 while ((atomic_read(&journal->j_wcount) > 0 ||
2940 atomic_read(&journal->j_jlock)) &&
2941 journal->j_trans_id == trans_id) {
2942 queue_log_writer(sb);
2944 if (journal->j_trans_id != trans_id)
2945 break;
2946 if (bcount == journal->j_bcount)
2947 break;
2948 bcount = journal->j_bcount;
2952 /* join == true if you must join an existing transaction.
2953 ** join == false if you can deal with waiting for others to finish
2955 ** this will block until the transaction is joinable. send the number of blocks you
2956 ** expect to use in nblocks.
2958 static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
2959 struct super_block *p_s_sb, unsigned long nblocks,
2960 int join)
2962 time_t now = get_seconds();
2963 int old_trans_id;
2964 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2965 struct reiserfs_transaction_handle myth;
2966 int sched_count = 0;
2967 int retval;
2969 reiserfs_check_lock_depth(p_s_sb, "journal_begin");
2970 BUG_ON(nblocks > journal->j_trans_max);
2972 PROC_INFO_INC(p_s_sb, journal.journal_being);
2973 /* set here for journal_join */
2974 th->t_refcount = 1;
2975 th->t_super = p_s_sb;
2977 relock:
2978 lock_journal(p_s_sb);
2979 if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted(journal)) {
2980 unlock_journal(p_s_sb);
2981 retval = journal->j_errno;
2982 goto out_fail;
2984 journal->j_bcount++;
2986 if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) {
2987 unlock_journal(p_s_sb);
2988 reiserfs_wait_on_write_block(p_s_sb);
2989 PROC_INFO_INC(p_s_sb, journal.journal_relock_writers);
2990 goto relock;
2992 now = get_seconds();
2994 /* if there is no room in the journal OR
2995 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
2996 ** we don't sleep if there aren't other writers
2999 if ((!join && journal->j_must_wait > 0) ||
3000 (!join
3001 && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch)
3002 || (!join && atomic_read(&journal->j_wcount) > 0
3003 && journal->j_trans_start_time > 0
3004 && (now - journal->j_trans_start_time) >
3005 journal->j_max_trans_age) || (!join
3006 && atomic_read(&journal->j_jlock))
3007 || (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) {
3009 old_trans_id = journal->j_trans_id;
3010 unlock_journal(p_s_sb); /* allow others to finish this transaction */
3012 if (!join && (journal->j_len_alloc + nblocks + 2) >=
3013 journal->j_max_batch &&
3014 ((journal->j_len + nblocks + 2) * 100) <
3015 (journal->j_len_alloc * 75)) {
3016 if (atomic_read(&journal->j_wcount) > 10) {
3017 sched_count++;
3018 queue_log_writer(p_s_sb);
3019 goto relock;
3022 /* don't mess with joining the transaction if all we have to do is
3023 * wait for someone else to do a commit
3025 if (atomic_read(&journal->j_jlock)) {
3026 while (journal->j_trans_id == old_trans_id &&
3027 atomic_read(&journal->j_jlock)) {
3028 queue_log_writer(p_s_sb);
3030 goto relock;
3032 retval = journal_join(&myth, p_s_sb, 1);
3033 if (retval)
3034 goto out_fail;
3036 /* someone might have ended the transaction while we joined */
3037 if (old_trans_id != journal->j_trans_id) {
3038 retval = do_journal_end(&myth, p_s_sb, 1, 0);
3039 } else {
3040 retval = do_journal_end(&myth, p_s_sb, 1, COMMIT_NOW);
3043 if (retval)
3044 goto out_fail;
3046 PROC_INFO_INC(p_s_sb, journal.journal_relock_wcount);
3047 goto relock;
3049 /* we are the first writer, set trans_id */
3050 if (journal->j_trans_start_time == 0) {
3051 journal->j_trans_start_time = get_seconds();
3053 atomic_inc(&(journal->j_wcount));
3054 journal->j_len_alloc += nblocks;
3055 th->t_blocks_logged = 0;
3056 th->t_blocks_allocated = nblocks;
3057 th->t_trans_id = journal->j_trans_id;
3058 unlock_journal(p_s_sb);
3059 INIT_LIST_HEAD(&th->t_list);
3060 get_fs_excl();
3061 return 0;
3063 out_fail:
3064 memset(th, 0, sizeof(*th));
3065 /* Re-set th->t_super, so we can properly keep track of how many
3066 * persistent transactions there are. We need to do this so if this
3067 * call is part of a failed restart_transaction, we can free it later */
3068 th->t_super = p_s_sb;
3069 return retval;
3072 struct reiserfs_transaction_handle *reiserfs_persistent_transaction(struct
3073 super_block
3075 int nblocks)
3077 int ret;
3078 struct reiserfs_transaction_handle *th;
3080 /* if we're nesting into an existing transaction. It will be
3081 ** persistent on its own
3083 if (reiserfs_transaction_running(s)) {
3084 th = current->journal_info;
3085 th->t_refcount++;
3086 BUG_ON(th->t_refcount < 2);
3088 return th;
3090 th = kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS);
3091 if (!th)
3092 return NULL;
3093 ret = journal_begin(th, s, nblocks);
3094 if (ret) {
3095 kfree(th);
3096 return NULL;
3099 SB_JOURNAL(s)->j_persistent_trans++;
3100 return th;
3103 int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th)
3105 struct super_block *s = th->t_super;
3106 int ret = 0;
3107 if (th->t_trans_id)
3108 ret = journal_end(th, th->t_super, th->t_blocks_allocated);
3109 else
3110 ret = -EIO;
3111 if (th->t_refcount == 0) {
3112 SB_JOURNAL(s)->j_persistent_trans--;
3113 kfree(th);
3115 return ret;
3118 static int journal_join(struct reiserfs_transaction_handle *th,
3119 struct super_block *p_s_sb, unsigned long nblocks)
3121 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3123 /* this keeps do_journal_end from NULLing out the current->journal_info
3124 ** pointer
3126 th->t_handle_save = cur_th;
3127 BUG_ON(cur_th && cur_th->t_refcount > 1);
3128 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_JOIN);
3131 int journal_join_abort(struct reiserfs_transaction_handle *th,
3132 struct super_block *p_s_sb, unsigned long nblocks)
3134 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3136 /* this keeps do_journal_end from NULLing out the current->journal_info
3137 ** pointer
3139 th->t_handle_save = cur_th;
3140 BUG_ON(cur_th && cur_th->t_refcount > 1);
3141 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_ABORT);
3144 int journal_begin(struct reiserfs_transaction_handle *th,
3145 struct super_block *p_s_sb, unsigned long nblocks)
3147 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3148 int ret;
3150 th->t_handle_save = NULL;
3151 if (cur_th) {
3152 /* we are nesting into the current transaction */
3153 if (cur_th->t_super == p_s_sb) {
3154 BUG_ON(!cur_th->t_refcount);
3155 cur_th->t_refcount++;
3156 memcpy(th, cur_th, sizeof(*th));
3157 if (th->t_refcount <= 1)
3158 reiserfs_warning(p_s_sb,
3159 "BAD: refcount <= 1, but journal_info != 0");
3160 return 0;
3161 } else {
3162 /* we've ended up with a handle from a different filesystem.
3163 ** save it and restore on journal_end. This should never
3164 ** really happen...
3166 reiserfs_warning(p_s_sb,
3167 "clm-2100: nesting info a different FS");
3168 th->t_handle_save = current->journal_info;
3169 current->journal_info = th;
3171 } else {
3172 current->journal_info = th;
3174 ret = do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_REG);
3175 BUG_ON(current->journal_info != th);
3177 /* I guess this boils down to being the reciprocal of clm-2100 above.
3178 * If do_journal_begin_r fails, we need to put it back, since journal_end
3179 * won't be called to do it. */
3180 if (ret)
3181 current->journal_info = th->t_handle_save;
3182 else
3183 BUG_ON(!th->t_refcount);
3185 return ret;
3189 ** puts bh into the current transaction. If it was already there, reorders removes the
3190 ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
3192 ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
3193 ** transaction is committed.
3195 ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
3197 int journal_mark_dirty(struct reiserfs_transaction_handle *th,
3198 struct super_block *p_s_sb, struct buffer_head *bh)
3200 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3201 struct reiserfs_journal_cnode *cn = NULL;
3202 int count_already_incd = 0;
3203 int prepared = 0;
3204 BUG_ON(!th->t_trans_id);
3206 PROC_INFO_INC(p_s_sb, journal.mark_dirty);
3207 if (th->t_trans_id != journal->j_trans_id) {
3208 reiserfs_panic(th->t_super,
3209 "journal-1577: handle trans id %ld != current trans id %ld\n",
3210 th->t_trans_id, journal->j_trans_id);
3213 p_s_sb->s_dirt = 1;
3215 prepared = test_clear_buffer_journal_prepared(bh);
3216 clear_buffer_journal_restore_dirty(bh);
3217 /* already in this transaction, we are done */
3218 if (buffer_journaled(bh)) {
3219 PROC_INFO_INC(p_s_sb, journal.mark_dirty_already);
3220 return 0;
3223 /* this must be turned into a panic instead of a warning. We can't allow
3224 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
3225 ** could get to disk too early. NOT GOOD.
3227 if (!prepared || buffer_dirty(bh)) {
3228 reiserfs_warning(p_s_sb, "journal-1777: buffer %llu bad state "
3229 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
3230 (unsigned long long)bh->b_blocknr,
3231 prepared ? ' ' : '!',
3232 buffer_locked(bh) ? ' ' : '!',
3233 buffer_dirty(bh) ? ' ' : '!',
3234 buffer_journal_dirty(bh) ? ' ' : '!');
3237 if (atomic_read(&(journal->j_wcount)) <= 0) {
3238 reiserfs_warning(p_s_sb,
3239 "journal-1409: journal_mark_dirty returning because j_wcount was %d",
3240 atomic_read(&(journal->j_wcount)));
3241 return 1;
3243 /* this error means I've screwed up, and we've overflowed the transaction.
3244 ** Nothing can be done here, except make the FS readonly or panic.
3246 if (journal->j_len >= journal->j_trans_max) {
3247 reiserfs_panic(th->t_super,
3248 "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n",
3249 journal->j_len);
3252 if (buffer_journal_dirty(bh)) {
3253 count_already_incd = 1;
3254 PROC_INFO_INC(p_s_sb, journal.mark_dirty_notjournal);
3255 clear_buffer_journal_dirty(bh);
3258 if (journal->j_len > journal->j_len_alloc) {
3259 journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT;
3262 set_buffer_journaled(bh);
3264 /* now put this guy on the end */
3265 if (!cn) {
3266 cn = get_cnode(p_s_sb);
3267 if (!cn) {
3268 reiserfs_panic(p_s_sb, "get_cnode failed!\n");
3271 if (th->t_blocks_logged == th->t_blocks_allocated) {
3272 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT;
3273 journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT;
3275 th->t_blocks_logged++;
3276 journal->j_len++;
3278 cn->bh = bh;
3279 cn->blocknr = bh->b_blocknr;
3280 cn->sb = p_s_sb;
3281 cn->jlist = NULL;
3282 insert_journal_hash(journal->j_hash_table, cn);
3283 if (!count_already_incd) {
3284 get_bh(bh);
3287 cn->next = NULL;
3288 cn->prev = journal->j_last;
3289 cn->bh = bh;
3290 if (journal->j_last) {
3291 journal->j_last->next = cn;
3292 journal->j_last = cn;
3293 } else {
3294 journal->j_first = cn;
3295 journal->j_last = cn;
3297 return 0;
3300 int journal_end(struct reiserfs_transaction_handle *th,
3301 struct super_block *p_s_sb, unsigned long nblocks)
3303 if (!current->journal_info && th->t_refcount > 1)
3304 reiserfs_warning(p_s_sb, "REISER-NESTING: th NULL, refcount %d",
3305 th->t_refcount);
3307 if (!th->t_trans_id) {
3308 WARN_ON(1);
3309 return -EIO;
3312 th->t_refcount--;
3313 if (th->t_refcount > 0) {
3314 struct reiserfs_transaction_handle *cur_th =
3315 current->journal_info;
3317 /* we aren't allowed to close a nested transaction on a different
3318 ** filesystem from the one in the task struct
3320 BUG_ON(cur_th->t_super != th->t_super);
3322 if (th != cur_th) {
3323 memcpy(current->journal_info, th, sizeof(*th));
3324 th->t_trans_id = 0;
3326 return 0;
3327 } else {
3328 return do_journal_end(th, p_s_sb, nblocks, 0);
3332 /* removes from the current transaction, relsing and descrementing any counters.
3333 ** also files the removed buffer directly onto the clean list
3335 ** called by journal_mark_freed when a block has been deleted
3337 ** returns 1 if it cleaned and relsed the buffer. 0 otherwise
3339 static int remove_from_transaction(struct super_block *p_s_sb,
3340 b_blocknr_t blocknr, int already_cleaned)
3342 struct buffer_head *bh;
3343 struct reiserfs_journal_cnode *cn;
3344 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3345 int ret = 0;
3347 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr);
3348 if (!cn || !cn->bh) {
3349 return ret;
3351 bh = cn->bh;
3352 if (cn->prev) {
3353 cn->prev->next = cn->next;
3355 if (cn->next) {
3356 cn->next->prev = cn->prev;
3358 if (cn == journal->j_first) {
3359 journal->j_first = cn->next;
3361 if (cn == journal->j_last) {
3362 journal->j_last = cn->prev;
3364 if (bh)
3365 remove_journal_hash(p_s_sb, journal->j_hash_table, NULL,
3366 bh->b_blocknr, 0);
3367 clear_buffer_journaled(bh); /* don't log this one */
3369 if (!already_cleaned) {
3370 clear_buffer_journal_dirty(bh);
3371 clear_buffer_dirty(bh);
3372 clear_buffer_journal_test(bh);
3373 put_bh(bh);
3374 if (atomic_read(&(bh->b_count)) < 0) {
3375 reiserfs_warning(p_s_sb,
3376 "journal-1752: remove from trans, b_count < 0");
3378 ret = 1;
3380 journal->j_len--;
3381 journal->j_len_alloc--;
3382 free_cnode(p_s_sb, cn);
3383 return ret;
3387 ** for any cnode in a journal list, it can only be dirtied of all the
3388 ** transactions that include it are committed to disk.
3389 ** this checks through each transaction, and returns 1 if you are allowed to dirty,
3390 ** and 0 if you aren't
3392 ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3393 ** blocks for a given transaction on disk
3396 static int can_dirty(struct reiserfs_journal_cnode *cn)
3398 struct super_block *sb = cn->sb;
3399 b_blocknr_t blocknr = cn->blocknr;
3400 struct reiserfs_journal_cnode *cur = cn->hprev;
3401 int can_dirty = 1;
3403 /* first test hprev. These are all newer than cn, so any node here
3404 ** with the same block number and dev means this node can't be sent
3405 ** to disk right now.
3407 while (cur && can_dirty) {
3408 if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
3409 cur->blocknr == blocknr) {
3410 can_dirty = 0;
3412 cur = cur->hprev;
3414 /* then test hnext. These are all older than cn. As long as they
3415 ** are committed to the log, it is safe to write cn to disk
3417 cur = cn->hnext;
3418 while (cur && can_dirty) {
3419 if (cur->jlist && cur->jlist->j_len > 0 &&
3420 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
3421 cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
3422 can_dirty = 0;
3424 cur = cur->hnext;
3426 return can_dirty;
3429 /* syncs the commit blocks, but does not force the real buffers to disk
3430 ** will wait until the current transaction is done/committed before returning
3432 int journal_end_sync(struct reiserfs_transaction_handle *th,
3433 struct super_block *p_s_sb, unsigned long nblocks)
3435 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3437 BUG_ON(!th->t_trans_id);
3438 /* you can sync while nested, very, very bad */
3439 BUG_ON(th->t_refcount > 1);
3440 if (journal->j_len == 0) {
3441 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb),
3443 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb));
3445 return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT);
3449 ** writeback the pending async commits to disk
3451 static void flush_async_commits(struct work_struct *work)
3453 struct reiserfs_journal *journal =
3454 container_of(work, struct reiserfs_journal, j_work.work);
3455 struct super_block *p_s_sb = journal->j_work_sb;
3456 struct reiserfs_journal_list *jl;
3457 struct list_head *entry;
3459 lock_kernel();
3460 if (!list_empty(&journal->j_journal_list)) {
3461 /* last entry is the youngest, commit it and you get everything */
3462 entry = journal->j_journal_list.prev;
3463 jl = JOURNAL_LIST_ENTRY(entry);
3464 flush_commit_list(p_s_sb, jl, 1);
3466 unlock_kernel();
3470 ** flushes any old transactions to disk
3471 ** ends the current transaction if it is too old
3473 int reiserfs_flush_old_commits(struct super_block *p_s_sb)
3475 time_t now;
3476 struct reiserfs_transaction_handle th;
3477 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3479 now = get_seconds();
3480 /* safety check so we don't flush while we are replaying the log during
3481 * mount
3483 if (list_empty(&journal->j_journal_list)) {
3484 return 0;
3487 /* check the current transaction. If there are no writers, and it is
3488 * too old, finish it, and force the commit blocks to disk
3490 if (atomic_read(&journal->j_wcount) <= 0 &&
3491 journal->j_trans_start_time > 0 &&
3492 journal->j_len > 0 &&
3493 (now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3494 if (!journal_join(&th, p_s_sb, 1)) {
3495 reiserfs_prepare_for_journal(p_s_sb,
3496 SB_BUFFER_WITH_SB(p_s_sb),
3498 journal_mark_dirty(&th, p_s_sb,
3499 SB_BUFFER_WITH_SB(p_s_sb));
3501 /* we're only being called from kreiserfsd, it makes no sense to do
3502 ** an async commit so that kreiserfsd can do it later
3504 do_journal_end(&th, p_s_sb, 1, COMMIT_NOW | WAIT);
3507 return p_s_sb->s_dirt;
3511 ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
3513 ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
3514 ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3515 ** flushes the commit list and returns 0.
3517 ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
3519 ** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3521 static int check_journal_end(struct reiserfs_transaction_handle *th,
3522 struct super_block *p_s_sb, unsigned long nblocks,
3523 int flags)
3526 time_t now;
3527 int flush = flags & FLUSH_ALL;
3528 int commit_now = flags & COMMIT_NOW;
3529 int wait_on_commit = flags & WAIT;
3530 struct reiserfs_journal_list *jl;
3531 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3533 BUG_ON(!th->t_trans_id);
3535 if (th->t_trans_id != journal->j_trans_id) {
3536 reiserfs_panic(th->t_super,
3537 "journal-1577: handle trans id %ld != current trans id %ld\n",
3538 th->t_trans_id, journal->j_trans_id);
3541 journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged);
3542 if (atomic_read(&(journal->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3543 atomic_dec(&(journal->j_wcount));
3546 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
3547 ** will be dealt with by next transaction that actually writes something, but should be taken
3548 ** care of in this trans
3550 BUG_ON(journal->j_len == 0);
3552 /* if wcount > 0, and we are called to with flush or commit_now,
3553 ** we wait on j_join_wait. We will wake up when the last writer has
3554 ** finished the transaction, and started it on its way to the disk.
3555 ** Then, we flush the commit or journal list, and just return 0
3556 ** because the rest of journal end was already done for this transaction.
3558 if (atomic_read(&(journal->j_wcount)) > 0) {
3559 if (flush || commit_now) {
3560 unsigned trans_id;
3562 jl = journal->j_current_jl;
3563 trans_id = jl->j_trans_id;
3564 if (wait_on_commit)
3565 jl->j_state |= LIST_COMMIT_PENDING;
3566 atomic_set(&(journal->j_jlock), 1);
3567 if (flush) {
3568 journal->j_next_full_flush = 1;
3570 unlock_journal(p_s_sb);
3572 /* sleep while the current transaction is still j_jlocked */
3573 while (journal->j_trans_id == trans_id) {
3574 if (atomic_read(&journal->j_jlock)) {
3575 queue_log_writer(p_s_sb);
3576 } else {
3577 lock_journal(p_s_sb);
3578 if (journal->j_trans_id == trans_id) {
3579 atomic_set(&(journal->j_jlock),
3582 unlock_journal(p_s_sb);
3585 BUG_ON(journal->j_trans_id == trans_id);
3587 if (commit_now
3588 && journal_list_still_alive(p_s_sb, trans_id)
3589 && wait_on_commit) {
3590 flush_commit_list(p_s_sb, jl, 1);
3592 return 0;
3594 unlock_journal(p_s_sb);
3595 return 0;
3598 /* deal with old transactions where we are the last writers */
3599 now = get_seconds();
3600 if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3601 commit_now = 1;
3602 journal->j_next_async_flush = 1;
3604 /* don't batch when someone is waiting on j_join_wait */
3605 /* don't batch when syncing the commit or flushing the whole trans */
3606 if (!(journal->j_must_wait > 0) && !(atomic_read(&(journal->j_jlock)))
3607 && !flush && !commit_now && (journal->j_len < journal->j_max_batch)
3608 && journal->j_len_alloc < journal->j_max_batch
3609 && journal->j_cnode_free > (journal->j_trans_max * 3)) {
3610 journal->j_bcount++;
3611 unlock_journal(p_s_sb);
3612 return 0;
3615 if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
3616 reiserfs_panic(p_s_sb,
3617 "journal-003: journal_end: j_start (%ld) is too high\n",
3618 journal->j_start);
3620 return 1;
3624 ** Does all the work that makes deleting blocks safe.
3625 ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3627 ** otherwise:
3628 ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3629 ** before this transaction has finished.
3631 ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3632 ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3633 ** the block can't be reallocated yet.
3635 ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3637 int journal_mark_freed(struct reiserfs_transaction_handle *th,
3638 struct super_block *p_s_sb, b_blocknr_t blocknr)
3640 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3641 struct reiserfs_journal_cnode *cn = NULL;
3642 struct buffer_head *bh = NULL;
3643 struct reiserfs_list_bitmap *jb = NULL;
3644 int cleaned = 0;
3645 BUG_ON(!th->t_trans_id);
3647 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr);
3648 if (cn && cn->bh) {
3649 bh = cn->bh;
3650 get_bh(bh);
3652 /* if it is journal new, we just remove it from this transaction */
3653 if (bh && buffer_journal_new(bh)) {
3654 clear_buffer_journal_new(bh);
3655 clear_prepared_bits(bh);
3656 reiserfs_clean_and_file_buffer(bh);
3657 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned);
3658 } else {
3659 /* set the bit for this block in the journal bitmap for this transaction */
3660 jb = journal->j_current_jl->j_list_bitmap;
3661 if (!jb) {
3662 reiserfs_panic(p_s_sb,
3663 "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n");
3665 set_bit_in_list_bitmap(p_s_sb, blocknr, jb);
3667 /* Note, the entire while loop is not allowed to schedule. */
3669 if (bh) {
3670 clear_prepared_bits(bh);
3671 reiserfs_clean_and_file_buffer(bh);
3673 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned);
3675 /* find all older transactions with this block, make sure they don't try to write it out */
3676 cn = get_journal_hash_dev(p_s_sb, journal->j_list_hash_table,
3677 blocknr);
3678 while (cn) {
3679 if (p_s_sb == cn->sb && blocknr == cn->blocknr) {
3680 set_bit(BLOCK_FREED, &cn->state);
3681 if (cn->bh) {
3682 if (!cleaned) {
3683 /* remove_from_transaction will brelse the buffer if it was
3684 ** in the current trans
3686 clear_buffer_journal_dirty(cn->
3687 bh);
3688 clear_buffer_dirty(cn->bh);
3689 clear_buffer_journal_test(cn->
3690 bh);
3691 cleaned = 1;
3692 put_bh(cn->bh);
3693 if (atomic_read
3694 (&(cn->bh->b_count)) < 0) {
3695 reiserfs_warning(p_s_sb,
3696 "journal-2138: cn->bh->b_count < 0");
3699 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3700 atomic_dec(&
3701 (cn->jlist->
3702 j_nonzerolen));
3704 cn->bh = NULL;
3707 cn = cn->hnext;
3711 if (bh) {
3712 put_bh(bh); /* get_hash grabs the buffer */
3713 if (atomic_read(&(bh->b_count)) < 0) {
3714 reiserfs_warning(p_s_sb,
3715 "journal-2165: bh->b_count < 0");
3718 return 0;
3721 void reiserfs_update_inode_transaction(struct inode *inode)
3723 struct reiserfs_journal *journal = SB_JOURNAL(inode->i_sb);
3724 REISERFS_I(inode)->i_jl = journal->j_current_jl;
3725 REISERFS_I(inode)->i_trans_id = journal->j_trans_id;
3729 * returns -1 on error, 0 if no commits/barriers were done and 1
3730 * if a transaction was actually committed and the barrier was done
3732 static int __commit_trans_jl(struct inode *inode, unsigned long id,
3733 struct reiserfs_journal_list *jl)
3735 struct reiserfs_transaction_handle th;
3736 struct super_block *sb = inode->i_sb;
3737 struct reiserfs_journal *journal = SB_JOURNAL(sb);
3738 int ret = 0;
3740 /* is it from the current transaction, or from an unknown transaction? */
3741 if (id == journal->j_trans_id) {
3742 jl = journal->j_current_jl;
3743 /* try to let other writers come in and grow this transaction */
3744 let_transaction_grow(sb, id);
3745 if (journal->j_trans_id != id) {
3746 goto flush_commit_only;
3749 ret = journal_begin(&th, sb, 1);
3750 if (ret)
3751 return ret;
3753 /* someone might have ended this transaction while we joined */
3754 if (journal->j_trans_id != id) {
3755 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
3757 journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb));
3758 ret = journal_end(&th, sb, 1);
3759 goto flush_commit_only;
3762 ret = journal_end_sync(&th, sb, 1);
3763 if (!ret)
3764 ret = 1;
3766 } else {
3767 /* this gets tricky, we have to make sure the journal list in
3768 * the inode still exists. We know the list is still around
3769 * if we've got a larger transaction id than the oldest list
3771 flush_commit_only:
3772 if (journal_list_still_alive(inode->i_sb, id)) {
3774 * we only set ret to 1 when we know for sure
3775 * the barrier hasn't been started yet on the commit
3776 * block.
3778 if (atomic_read(&jl->j_commit_left) > 1)
3779 ret = 1;
3780 flush_commit_list(sb, jl, 1);
3781 if (journal->j_errno)
3782 ret = journal->j_errno;
3785 /* otherwise the list is gone, and long since committed */
3786 return ret;
3789 int reiserfs_commit_for_inode(struct inode *inode)
3791 unsigned long id = REISERFS_I(inode)->i_trans_id;
3792 struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
3794 /* for the whole inode, assume unset id means it was
3795 * changed in the current transaction. More conservative
3797 if (!id || !jl) {
3798 reiserfs_update_inode_transaction(inode);
3799 id = REISERFS_I(inode)->i_trans_id;
3800 /* jl will be updated in __commit_trans_jl */
3803 return __commit_trans_jl(inode, id, jl);
3806 void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb,
3807 struct buffer_head *bh)
3809 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3810 PROC_INFO_INC(p_s_sb, journal.restore_prepared);
3811 if (!bh) {
3812 return;
3814 if (test_clear_buffer_journal_restore_dirty(bh) &&
3815 buffer_journal_dirty(bh)) {
3816 struct reiserfs_journal_cnode *cn;
3817 cn = get_journal_hash_dev(p_s_sb,
3818 journal->j_list_hash_table,
3819 bh->b_blocknr);
3820 if (cn && can_dirty(cn)) {
3821 set_buffer_journal_test(bh);
3822 mark_buffer_dirty(bh);
3825 clear_buffer_journal_prepared(bh);
3828 extern struct tree_balance *cur_tb;
3830 ** before we can change a metadata block, we have to make sure it won't
3831 ** be written to disk while we are altering it. So, we must:
3832 ** clean it
3833 ** wait on it.
3836 int reiserfs_prepare_for_journal(struct super_block *p_s_sb,
3837 struct buffer_head *bh, int wait)
3839 PROC_INFO_INC(p_s_sb, journal.prepare);
3841 if (test_set_buffer_locked(bh)) {
3842 if (!wait)
3843 return 0;
3844 lock_buffer(bh);
3846 set_buffer_journal_prepared(bh);
3847 if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
3848 clear_buffer_journal_test(bh);
3849 set_buffer_journal_restore_dirty(bh);
3851 unlock_buffer(bh);
3852 return 1;
3855 static void flush_old_journal_lists(struct super_block *s)
3857 struct reiserfs_journal *journal = SB_JOURNAL(s);
3858 struct reiserfs_journal_list *jl;
3859 struct list_head *entry;
3860 time_t now = get_seconds();
3862 while (!list_empty(&journal->j_journal_list)) {
3863 entry = journal->j_journal_list.next;
3864 jl = JOURNAL_LIST_ENTRY(entry);
3865 /* this check should always be run, to send old lists to disk */
3866 if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4)) &&
3867 atomic_read(&jl->j_commit_left) == 0 &&
3868 test_transaction(s, jl)) {
3869 flush_used_journal_lists(s, jl);
3870 } else {
3871 break;
3877 ** long and ugly. If flush, will not return until all commit
3878 ** blocks and all real buffers in the trans are on disk.
3879 ** If no_async, won't return until all commit blocks are on disk.
3881 ** keep reading, there are comments as you go along
3883 ** If the journal is aborted, we just clean up. Things like flushing
3884 ** journal lists, etc just won't happen.
3886 static int do_journal_end(struct reiserfs_transaction_handle *th,
3887 struct super_block *p_s_sb, unsigned long nblocks,
3888 int flags)
3890 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3891 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
3892 struct reiserfs_journal_cnode *last_cn = NULL;
3893 struct reiserfs_journal_desc *desc;
3894 struct reiserfs_journal_commit *commit;
3895 struct buffer_head *c_bh; /* commit bh */
3896 struct buffer_head *d_bh; /* desc bh */
3897 int cur_write_start = 0; /* start index of current log write */
3898 int old_start;
3899 int i;
3900 int flush;
3901 int wait_on_commit;
3902 struct reiserfs_journal_list *jl, *temp_jl;
3903 struct list_head *entry, *safe;
3904 unsigned long jindex;
3905 unsigned long commit_trans_id;
3906 int trans_half;
3908 BUG_ON(th->t_refcount > 1);
3909 BUG_ON(!th->t_trans_id);
3911 /* protect flush_older_commits from doing mistakes if the
3912 transaction ID counter gets overflowed. */
3913 if (th->t_trans_id == ~0UL)
3914 flags |= FLUSH_ALL | COMMIT_NOW | WAIT;
3915 flush = flags & FLUSH_ALL;
3916 wait_on_commit = flags & WAIT;
3918 put_fs_excl();
3919 current->journal_info = th->t_handle_save;
3920 reiserfs_check_lock_depth(p_s_sb, "journal end");
3921 if (journal->j_len == 0) {
3922 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb),
3924 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb));
3927 lock_journal(p_s_sb);
3928 if (journal->j_next_full_flush) {
3929 flags |= FLUSH_ALL;
3930 flush = 1;
3932 if (journal->j_next_async_flush) {
3933 flags |= COMMIT_NOW | WAIT;
3934 wait_on_commit = 1;
3937 /* check_journal_end locks the journal, and unlocks if it does not return 1
3938 ** it tells us if we should continue with the journal_end, or just return
3940 if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
3941 p_s_sb->s_dirt = 1;
3942 wake_queued_writers(p_s_sb);
3943 reiserfs_async_progress_wait(p_s_sb);
3944 goto out;
3947 /* check_journal_end might set these, check again */
3948 if (journal->j_next_full_flush) {
3949 flush = 1;
3953 ** j must wait means we have to flush the log blocks, and the real blocks for
3954 ** this transaction
3956 if (journal->j_must_wait > 0) {
3957 flush = 1;
3959 #ifdef REISERFS_PREALLOCATE
3960 /* quota ops might need to nest, setup the journal_info pointer for them
3961 * and raise the refcount so that it is > 0. */
3962 current->journal_info = th;
3963 th->t_refcount++;
3964 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
3965 * the transaction */
3966 th->t_refcount--;
3967 current->journal_info = th->t_handle_save;
3968 #endif
3970 /* setup description block */
3971 d_bh =
3972 journal_getblk(p_s_sb,
3973 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3974 journal->j_start);
3975 set_buffer_uptodate(d_bh);
3976 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data;
3977 memset(d_bh->b_data, 0, d_bh->b_size);
3978 memcpy(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8);
3979 set_desc_trans_id(desc, journal->j_trans_id);
3981 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
3982 c_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3983 ((journal->j_start + journal->j_len +
3984 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
3985 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
3986 memset(c_bh->b_data, 0, c_bh->b_size);
3987 set_commit_trans_id(commit, journal->j_trans_id);
3988 set_buffer_uptodate(c_bh);
3990 /* init this journal list */
3991 jl = journal->j_current_jl;
3993 /* we lock the commit before doing anything because
3994 * we want to make sure nobody tries to run flush_commit_list until
3995 * the new transaction is fully setup, and we've already flushed the
3996 * ordered bh list
3998 down(&jl->j_commit_lock);
4000 /* save the transaction id in case we need to commit it later */
4001 commit_trans_id = jl->j_trans_id;
4003 atomic_set(&jl->j_older_commits_done, 0);
4004 jl->j_trans_id = journal->j_trans_id;
4005 jl->j_timestamp = journal->j_trans_start_time;
4006 jl->j_commit_bh = c_bh;
4007 jl->j_start = journal->j_start;
4008 jl->j_len = journal->j_len;
4009 atomic_set(&jl->j_nonzerolen, journal->j_len);
4010 atomic_set(&jl->j_commit_left, journal->j_len + 2);
4011 jl->j_realblock = NULL;
4013 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
4014 ** for each real block, add it to the journal list hash,
4015 ** copy into real block index array in the commit or desc block
4017 trans_half = journal_trans_half(p_s_sb->s_blocksize);
4018 for (i = 0, cn = journal->j_first; cn; cn = cn->next, i++) {
4019 if (buffer_journaled(cn->bh)) {
4020 jl_cn = get_cnode(p_s_sb);
4021 if (!jl_cn) {
4022 reiserfs_panic(p_s_sb,
4023 "journal-1676, get_cnode returned NULL\n");
4025 if (i == 0) {
4026 jl->j_realblock = jl_cn;
4028 jl_cn->prev = last_cn;
4029 jl_cn->next = NULL;
4030 if (last_cn) {
4031 last_cn->next = jl_cn;
4033 last_cn = jl_cn;
4034 /* make sure the block we are trying to log is not a block
4035 of journal or reserved area */
4037 if (is_block_in_log_or_reserved_area
4038 (p_s_sb, cn->bh->b_blocknr)) {
4039 reiserfs_panic(p_s_sb,
4040 "journal-2332: Trying to log block %lu, which is a log block\n",
4041 cn->bh->b_blocknr);
4043 jl_cn->blocknr = cn->bh->b_blocknr;
4044 jl_cn->state = 0;
4045 jl_cn->sb = p_s_sb;
4046 jl_cn->bh = cn->bh;
4047 jl_cn->jlist = jl;
4048 insert_journal_hash(journal->j_list_hash_table, jl_cn);
4049 if (i < trans_half) {
4050 desc->j_realblock[i] =
4051 cpu_to_le32(cn->bh->b_blocknr);
4052 } else {
4053 commit->j_realblock[i - trans_half] =
4054 cpu_to_le32(cn->bh->b_blocknr);
4056 } else {
4057 i--;
4060 set_desc_trans_len(desc, journal->j_len);
4061 set_desc_mount_id(desc, journal->j_mount_id);
4062 set_desc_trans_id(desc, journal->j_trans_id);
4063 set_commit_trans_len(commit, journal->j_len);
4065 /* special check in case all buffers in the journal were marked for not logging */
4066 BUG_ON(journal->j_len == 0);
4068 /* we're about to dirty all the log blocks, mark the description block
4069 * dirty now too. Don't mark the commit block dirty until all the
4070 * others are on disk
4072 mark_buffer_dirty(d_bh);
4074 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
4075 cur_write_start = journal->j_start;
4076 cn = journal->j_first;
4077 jindex = 1; /* start at one so we don't get the desc again */
4078 while (cn) {
4079 clear_buffer_journal_new(cn->bh);
4080 /* copy all the real blocks into log area. dirty log blocks */
4081 if (buffer_journaled(cn->bh)) {
4082 struct buffer_head *tmp_bh;
4083 char *addr;
4084 struct page *page;
4085 tmp_bh =
4086 journal_getblk(p_s_sb,
4087 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
4088 ((cur_write_start +
4089 jindex) %
4090 SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
4091 set_buffer_uptodate(tmp_bh);
4092 page = cn->bh->b_page;
4093 addr = kmap(page);
4094 memcpy(tmp_bh->b_data,
4095 addr + offset_in_page(cn->bh->b_data),
4096 cn->bh->b_size);
4097 kunmap(page);
4098 mark_buffer_dirty(tmp_bh);
4099 jindex++;
4100 set_buffer_journal_dirty(cn->bh);
4101 clear_buffer_journaled(cn->bh);
4102 } else {
4103 /* JDirty cleared sometime during transaction. don't log this one */
4104 reiserfs_warning(p_s_sb,
4105 "journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!");
4106 brelse(cn->bh);
4108 next = cn->next;
4109 free_cnode(p_s_sb, cn);
4110 cn = next;
4111 cond_resched();
4114 /* we are done with both the c_bh and d_bh, but
4115 ** c_bh must be written after all other commit blocks,
4116 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
4119 journal->j_current_jl = alloc_journal_list(p_s_sb);
4121 /* now it is safe to insert this transaction on the main list */
4122 list_add_tail(&jl->j_list, &journal->j_journal_list);
4123 list_add_tail(&jl->j_working_list, &journal->j_working_list);
4124 journal->j_num_work_lists++;
4126 /* reset journal values for the next transaction */
4127 old_start = journal->j_start;
4128 journal->j_start =
4129 (journal->j_start + journal->j_len +
4130 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb);
4131 atomic_set(&(journal->j_wcount), 0);
4132 journal->j_bcount = 0;
4133 journal->j_last = NULL;
4134 journal->j_first = NULL;
4135 journal->j_len = 0;
4136 journal->j_trans_start_time = 0;
4137 /* check for trans_id overflow */
4138 if (++journal->j_trans_id == 0)
4139 journal->j_trans_id = 10;
4140 journal->j_current_jl->j_trans_id = journal->j_trans_id;
4141 journal->j_must_wait = 0;
4142 journal->j_len_alloc = 0;
4143 journal->j_next_full_flush = 0;
4144 journal->j_next_async_flush = 0;
4145 init_journal_hash(p_s_sb);
4147 // make sure reiserfs_add_jh sees the new current_jl before we
4148 // write out the tails
4149 smp_mb();
4151 /* tail conversion targets have to hit the disk before we end the
4152 * transaction. Otherwise a later transaction might repack the tail
4153 * before this transaction commits, leaving the data block unflushed and
4154 * clean, if we crash before the later transaction commits, the data block
4155 * is lost.
4157 if (!list_empty(&jl->j_tail_bh_list)) {
4158 unlock_kernel();
4159 write_ordered_buffers(&journal->j_dirty_buffers_lock,
4160 journal, jl, &jl->j_tail_bh_list);
4161 lock_kernel();
4163 BUG_ON(!list_empty(&jl->j_tail_bh_list));
4164 up(&jl->j_commit_lock);
4166 /* honor the flush wishes from the caller, simple commits can
4167 ** be done outside the journal lock, they are done below
4169 ** if we don't flush the commit list right now, we put it into
4170 ** the work queue so the people waiting on the async progress work
4171 ** queue don't wait for this proc to flush journal lists and such.
4173 if (flush) {
4174 flush_commit_list(p_s_sb, jl, 1);
4175 flush_journal_list(p_s_sb, jl, 1);
4176 } else if (!(jl->j_state & LIST_COMMIT_PENDING))
4177 queue_delayed_work(commit_wq, &journal->j_work, HZ / 10);
4179 /* if the next transaction has any chance of wrapping, flush
4180 ** transactions that might get overwritten. If any journal lists are very
4181 ** old flush them as well.
4183 first_jl:
4184 list_for_each_safe(entry, safe, &journal->j_journal_list) {
4185 temp_jl = JOURNAL_LIST_ENTRY(entry);
4186 if (journal->j_start <= temp_jl->j_start) {
4187 if ((journal->j_start + journal->j_trans_max + 1) >=
4188 temp_jl->j_start) {
4189 flush_used_journal_lists(p_s_sb, temp_jl);
4190 goto first_jl;
4191 } else if ((journal->j_start +
4192 journal->j_trans_max + 1) <
4193 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
4194 /* if we don't cross into the next transaction and we don't
4195 * wrap, there is no way we can overlap any later transactions
4196 * break now
4198 break;
4200 } else if ((journal->j_start +
4201 journal->j_trans_max + 1) >
4202 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
4203 if (((journal->j_start + journal->j_trans_max + 1) %
4204 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) >=
4205 temp_jl->j_start) {
4206 flush_used_journal_lists(p_s_sb, temp_jl);
4207 goto first_jl;
4208 } else {
4209 /* we don't overlap anything from out start to the end of the
4210 * log, and our wrapped portion doesn't overlap anything at
4211 * the start of the log. We can break
4213 break;
4217 flush_old_journal_lists(p_s_sb);
4219 journal->j_current_jl->j_list_bitmap =
4220 get_list_bitmap(p_s_sb, journal->j_current_jl);
4222 if (!(journal->j_current_jl->j_list_bitmap)) {
4223 reiserfs_panic(p_s_sb,
4224 "journal-1996: do_journal_end, could not get a list bitmap\n");
4227 atomic_set(&(journal->j_jlock), 0);
4228 unlock_journal(p_s_sb);
4229 /* wake up any body waiting to join. */
4230 clear_bit(J_WRITERS_QUEUED, &journal->j_state);
4231 wake_up(&(journal->j_join_wait));
4233 if (!flush && wait_on_commit &&
4234 journal_list_still_alive(p_s_sb, commit_trans_id)) {
4235 flush_commit_list(p_s_sb, jl, 1);
4237 out:
4238 reiserfs_check_lock_depth(p_s_sb, "journal end2");
4240 memset(th, 0, sizeof(*th));
4241 /* Re-set th->t_super, so we can properly keep track of how many
4242 * persistent transactions there are. We need to do this so if this
4243 * call is part of a failed restart_transaction, we can free it later */
4244 th->t_super = p_s_sb;
4246 return journal->j_errno;
4249 static void __reiserfs_journal_abort_hard(struct super_block *sb)
4251 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4252 if (test_bit(J_ABORTED, &journal->j_state))
4253 return;
4255 printk(KERN_CRIT "REISERFS: Aborting journal for filesystem on %s\n",
4256 reiserfs_bdevname(sb));
4258 sb->s_flags |= MS_RDONLY;
4259 set_bit(J_ABORTED, &journal->j_state);
4261 #ifdef CONFIG_REISERFS_CHECK
4262 dump_stack();
4263 #endif
4266 static void __reiserfs_journal_abort_soft(struct super_block *sb, int errno)
4268 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4269 if (test_bit(J_ABORTED, &journal->j_state))
4270 return;
4272 if (!journal->j_errno)
4273 journal->j_errno = errno;
4275 __reiserfs_journal_abort_hard(sb);
4278 void reiserfs_journal_abort(struct super_block *sb, int errno)
4280 return __reiserfs_journal_abort_soft(sb, errno);