Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Write ahead logging implementation copyright Chris Mason 2000 |
| 3 | ** |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 4 | ** The background commits make this code very interelated, and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | ** overly complex. I need to rethink things a bit....The major players: |
| 6 | ** |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 7 | ** journal_begin -- call with the number of blocks you expect to log. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | ** If the current transaction is too |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 9 | ** old, it will block until the current transaction is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | ** finished, and then start a new one. |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 11 | ** Usually, your transaction will get joined in with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | ** previous ones for speed. |
| 13 | ** |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 14 | ** journal_join -- same as journal_begin, but won't block on the current |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | ** transaction regardless of age. Don't ever call |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 16 | ** this. Ever. There are only two places it should be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | ** called from, and they are both inside this file. |
| 18 | ** |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 19 | ** journal_mark_dirty -- adds blocks into this transaction. clears any flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | ** that might make them get sent to disk |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 21 | ** and then marks them BH_JDirty. Puts the buffer head |
| 22 | ** into the current transaction hash. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | ** |
| 24 | ** journal_end -- if the current transaction is batchable, it does nothing |
| 25 | ** otherwise, it could do an async/synchronous commit, or |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 26 | ** a full flush of all log and real blocks in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | ** transaction. |
| 28 | ** |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | ** around too long. |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 33 | ** -- Note, if you call this as an immediate flush from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | ** from within kupdate, it will ignore the immediate flag |
| 35 | */ |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/time.h> |
Matthew Wilcox | 6188e10 | 2008-04-18 22:21:05 -0400 | [diff] [blame] | 38 | #include <linux/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/vmalloc.h> |
| 40 | #include <linux/reiserfs_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/kernel.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/fcntl.h> |
| 44 | #include <linux/stat.h> |
| 45 | #include <linux/string.h> |
| 46 | #include <linux/smp_lock.h> |
| 47 | #include <linux/buffer_head.h> |
| 48 | #include <linux/workqueue.h> |
| 49 | #include <linux/writeback.h> |
| 50 | #include <linux/blkdev.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 51 | #include <linux/backing-dev.h> |
Jeff Mahoney | 90415de | 2008-07-25 01:46:40 -0700 | [diff] [blame] | 52 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
Jeff Mahoney | 90415de | 2008-07-25 01:46:40 -0700 | [diff] [blame] | 54 | |
| 55 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* gets a struct reiserfs_journal_list * from a list head */ |
| 58 | #define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \ |
| 59 | j_list)) |
| 60 | #define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \ |
| 61 | j_working_list)) |
| 62 | |
| 63 | /* the number of mounted filesystems. This is used to decide when to |
| 64 | ** start and kill the commit workqueue |
| 65 | */ |
| 66 | static int reiserfs_mounted_fs_count; |
| 67 | |
| 68 | static struct workqueue_struct *commit_wq; |
| 69 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 70 | #define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit |
| 71 | structs at 4k */ |
| 72 | #define BUFNR 64 /*read ahead */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* cnode stat bits. Move these into reiserfs_fs.h */ |
| 75 | |
| 76 | #define BLOCK_FREED 2 /* this block was freed, and can't be written. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 77 | #define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | #define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */ |
| 80 | #define BLOCK_DIRTIED 5 |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* journal list state bits */ |
| 83 | #define LIST_TOUCHED 1 |
| 84 | #define LIST_DIRTY 2 |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 85 | #define LIST_COMMIT_PENDING 4 /* someone will commit this list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* flags for do_journal_end */ |
| 88 | #define FLUSH_ALL 1 /* flush commit and real blocks */ |
| 89 | #define COMMIT_NOW 2 /* end and commit this transaction */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 90 | #define WAIT 4 /* wait for the log blocks to hit the disk */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 92 | static int do_journal_end(struct reiserfs_transaction_handle *, |
| 93 | struct super_block *, unsigned long nblocks, |
| 94 | int flags); |
| 95 | static int flush_journal_list(struct super_block *s, |
| 96 | struct reiserfs_journal_list *jl, int flushall); |
| 97 | static int flush_commit_list(struct super_block *s, |
| 98 | struct reiserfs_journal_list *jl, int flushall); |
| 99 | static int can_dirty(struct reiserfs_journal_cnode *cn); |
| 100 | static int journal_join(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 101 | struct super_block *sb, unsigned long nblocks); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 102 | static int release_journal_dev(struct super_block *super, |
| 103 | struct reiserfs_journal *journal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static int dirty_one_transaction(struct super_block *s, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 105 | struct reiserfs_journal_list *jl); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 106 | static void flush_async_commits(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static void queue_log_writer(struct super_block *s); |
| 108 | |
| 109 | /* values for join in do_journal_begin_r */ |
| 110 | enum { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 111 | JBEGIN_REG = 0, /* regular journal begin */ |
| 112 | JBEGIN_JOIN = 1, /* join the running transaction if at all possible */ |
| 113 | JBEGIN_ABORT = 2, /* called from cleanup code, ignores aborted flag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static int do_journal_begin_r(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 117 | struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 118 | unsigned long nblocks, int join); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 120 | static void init_journal_hash(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 121 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 122 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 123 | memset(journal->j_hash_table, 0, |
| 124 | JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | ** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to |
| 129 | ** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for |
| 130 | ** more details. |
| 131 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 132 | static int reiserfs_clean_and_file_buffer(struct buffer_head *bh) |
| 133 | { |
| 134 | if (bh) { |
| 135 | clear_buffer_dirty(bh); |
| 136 | clear_buffer_journal_test(bh); |
| 137 | } |
| 138 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 141 | static struct reiserfs_bitmap_node *allocate_bitmap_node(struct super_block |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 142 | *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 143 | { |
| 144 | struct reiserfs_bitmap_node *bn; |
| 145 | static int id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 147 | bn = kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 148 | if (!bn) { |
| 149 | return NULL; |
| 150 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 151 | bn->data = kzalloc(sb->s_blocksize, GFP_NOFS); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 152 | if (!bn->data) { |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 153 | kfree(bn); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
| 156 | bn->id = id++; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 157 | INIT_LIST_HEAD(&bn->list); |
| 158 | return bn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 161 | static struct reiserfs_bitmap_node *get_bitmap_node(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 162 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 163 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 164 | struct reiserfs_bitmap_node *bn = NULL; |
| 165 | struct list_head *entry = journal->j_bitmap_nodes.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 167 | journal->j_used_bitmap_nodes++; |
| 168 | repeat: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 170 | if (entry != &journal->j_bitmap_nodes) { |
| 171 | bn = list_entry(entry, struct reiserfs_bitmap_node, list); |
| 172 | list_del(entry); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 173 | memset(bn->data, 0, sb->s_blocksize); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 174 | journal->j_free_bitmap_nodes--; |
| 175 | return bn; |
| 176 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 177 | bn = allocate_bitmap_node(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 178 | if (!bn) { |
| 179 | yield(); |
| 180 | goto repeat; |
| 181 | } |
| 182 | return bn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 184 | static inline void free_bitmap_node(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 185 | struct reiserfs_bitmap_node *bn) |
| 186 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 187 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 188 | journal->j_used_bitmap_nodes--; |
| 189 | if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) { |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 190 | kfree(bn->data); |
| 191 | kfree(bn); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 192 | } else { |
| 193 | list_add(&bn->list, &journal->j_bitmap_nodes); |
| 194 | journal->j_free_bitmap_nodes++; |
| 195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 198 | static void allocate_bitmap_nodes(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 199 | { |
| 200 | int i; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 201 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 202 | struct reiserfs_bitmap_node *bn = NULL; |
| 203 | for (i = 0; i < REISERFS_MIN_BITMAP_NODES; i++) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 204 | bn = allocate_bitmap_node(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 205 | if (bn) { |
| 206 | list_add(&bn->list, &journal->j_bitmap_nodes); |
| 207 | journal->j_free_bitmap_nodes++; |
| 208 | } else { |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 209 | break; /* this is ok, we'll try again when more are needed */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 214 | static int set_bit_in_list_bitmap(struct super_block *sb, |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 215 | b_blocknr_t block, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 216 | struct reiserfs_list_bitmap *jb) |
| 217 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 218 | unsigned int bmap_nr = block / (sb->s_blocksize << 3); |
| 219 | unsigned int bit_nr = block % (sb->s_blocksize << 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 221 | if (!jb->bitmaps[bmap_nr]) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 222 | jb->bitmaps[bmap_nr] = get_bitmap_node(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 223 | } |
| 224 | set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data); |
| 225 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 228 | static void cleanup_bitmap_list(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 229 | struct reiserfs_list_bitmap *jb) |
| 230 | { |
| 231 | int i; |
| 232 | if (jb->bitmaps == NULL) |
| 233 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 235 | for (i = 0; i < reiserfs_bmap_count(sb); i++) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 236 | if (jb->bitmaps[i]) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 237 | free_bitmap_node(sb, jb->bitmaps[i]); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 238 | jb->bitmaps[i] = NULL; |
| 239 | } |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | ** only call this on FS unmount. |
| 245 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 246 | static int free_list_bitmaps(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 247 | struct reiserfs_list_bitmap *jb_array) |
| 248 | { |
| 249 | int i; |
| 250 | struct reiserfs_list_bitmap *jb; |
| 251 | for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) { |
| 252 | jb = jb_array + i; |
| 253 | jb->journal_list = NULL; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 254 | cleanup_bitmap_list(sb, jb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 255 | vfree(jb->bitmaps); |
| 256 | jb->bitmaps = NULL; |
| 257 | } |
| 258 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 261 | static int free_bitmap_nodes(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 262 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 263 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 264 | struct list_head *next = journal->j_bitmap_nodes.next; |
| 265 | struct reiserfs_bitmap_node *bn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 267 | while (next != &journal->j_bitmap_nodes) { |
| 268 | bn = list_entry(next, struct reiserfs_bitmap_node, list); |
| 269 | list_del(next); |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 270 | kfree(bn->data); |
| 271 | kfree(bn); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 272 | next = journal->j_bitmap_nodes.next; |
| 273 | journal->j_free_bitmap_nodes--; |
| 274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 276 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 280 | ** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | ** jb_array is the array to be filled in. |
| 282 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 283 | int reiserfs_allocate_list_bitmaps(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 284 | struct reiserfs_list_bitmap *jb_array, |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 285 | unsigned int bmap_nr) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 286 | { |
| 287 | int i; |
| 288 | int failed = 0; |
| 289 | struct reiserfs_list_bitmap *jb; |
| 290 | int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 292 | for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) { |
| 293 | jb = jb_array + i; |
| 294 | jb->journal_list = NULL; |
| 295 | jb->bitmaps = vmalloc(mem); |
| 296 | if (!jb->bitmaps) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 297 | reiserfs_warning(sb, "clm-2000", "unable to " |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 298 | "allocate bitmaps for journal lists"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 299 | failed = 1; |
| 300 | break; |
| 301 | } |
| 302 | memset(jb->bitmaps, 0, mem); |
| 303 | } |
| 304 | if (failed) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 305 | free_list_bitmaps(sb, jb_array); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 312 | ** find an available list bitmap. If you can't find one, flush a commit list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | ** and try again |
| 314 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 315 | static struct reiserfs_list_bitmap *get_list_bitmap(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 316 | struct reiserfs_journal_list |
| 317 | *jl) |
| 318 | { |
| 319 | int i, j; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 320 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 321 | struct reiserfs_list_bitmap *jb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 323 | for (j = 0; j < (JOURNAL_NUM_BITMAPS * 3); j++) { |
| 324 | i = journal->j_list_bitmap_index; |
| 325 | journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS; |
| 326 | jb = journal->j_list_bitmap + i; |
| 327 | if (journal->j_list_bitmap[i].journal_list) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 328 | flush_commit_list(sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 329 | journal->j_list_bitmap[i]. |
| 330 | journal_list, 1); |
| 331 | if (!journal->j_list_bitmap[i].journal_list) { |
| 332 | break; |
| 333 | } |
| 334 | } else { |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | if (jb->journal_list) { /* double check to make sure if flushed correctly */ |
| 339 | return NULL; |
| 340 | } |
| 341 | jb->journal_list = jl; |
| 342 | return jb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 345 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | ** allocates a new chunk of X nodes, and links them all together as a list. |
| 347 | ** Uses the cnode->next and cnode->prev pointers |
| 348 | ** returns NULL on failure |
| 349 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 350 | static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes) |
| 351 | { |
| 352 | struct reiserfs_journal_cnode *head; |
| 353 | int i; |
| 354 | if (num_cnodes <= 0) { |
| 355 | return NULL; |
| 356 | } |
| 357 | head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode)); |
| 358 | if (!head) { |
| 359 | return NULL; |
| 360 | } |
| 361 | memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode)); |
| 362 | head[0].prev = NULL; |
| 363 | head[0].next = head + 1; |
| 364 | for (i = 1; i < num_cnodes; i++) { |
| 365 | head[i].prev = head + (i - 1); |
| 366 | head[i].next = head + (i + 1); /* if last one, overwrite it after the if */ |
| 367 | } |
| 368 | head[num_cnodes - 1].next = NULL; |
| 369 | return head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 373 | ** pulls a cnode off the free list, or returns NULL on failure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 375 | static struct reiserfs_journal_cnode *get_cnode(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 376 | { |
| 377 | struct reiserfs_journal_cnode *cn; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 378 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 380 | reiserfs_check_lock_depth(sb, "get_cnode"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 382 | if (journal->j_cnode_free <= 0) { |
| 383 | return NULL; |
| 384 | } |
| 385 | journal->j_cnode_used++; |
| 386 | journal->j_cnode_free--; |
| 387 | cn = journal->j_cnode_free_list; |
| 388 | if (!cn) { |
| 389 | return cn; |
| 390 | } |
| 391 | if (cn->next) { |
| 392 | cn->next->prev = NULL; |
| 393 | } |
| 394 | journal->j_cnode_free_list = cn->next; |
| 395 | memset(cn, 0, sizeof(struct reiserfs_journal_cnode)); |
| 396 | return cn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 400 | ** returns a cnode to the free list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 402 | static void free_cnode(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 403 | struct reiserfs_journal_cnode *cn) |
| 404 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 405 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 407 | reiserfs_check_lock_depth(sb, "free_cnode"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 409 | journal->j_cnode_used--; |
| 410 | journal->j_cnode_free++; |
| 411 | /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */ |
| 412 | cn->next = journal->j_cnode_free_list; |
| 413 | if (journal->j_cnode_free_list) { |
| 414 | journal->j_cnode_free_list->prev = cn; |
| 415 | } |
| 416 | cn->prev = NULL; /* not needed with the memset, but I might kill the memset, and forget to do this */ |
| 417 | journal->j_cnode_free_list = cn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 420 | static void clear_prepared_bits(struct buffer_head *bh) |
| 421 | { |
| 422 | clear_buffer_journal_prepared(bh); |
| 423 | clear_buffer_journal_restore_dirty(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | /* return a cnode with same dev, block number and size in table, or null if not found */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 427 | static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct |
| 428 | super_block |
| 429 | *sb, |
| 430 | struct |
| 431 | reiserfs_journal_cnode |
| 432 | **table, |
| 433 | long bl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 435 | struct reiserfs_journal_cnode *cn; |
| 436 | cn = journal_hash(table, sb, bl); |
| 437 | while (cn) { |
| 438 | if (cn->blocknr == bl && cn->sb == sb) |
| 439 | return cn; |
| 440 | cn = cn->hnext; |
| 441 | } |
| 442 | return (struct reiserfs_journal_cnode *)0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
| 446 | ** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated |
| 447 | ** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever |
| 448 | ** being overwritten by a replay after crashing. |
| 449 | ** |
| 450 | ** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting |
| 451 | ** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make |
| 452 | ** sure you never write the block without logging it. |
| 453 | ** |
| 454 | ** next_zero_bit is a suggestion about the next block to try for find_forward. |
| 455 | ** when bl is rejected because it is set in a journal list bitmap, we search |
| 456 | ** for the next zero bit in the bitmap that rejected bl. Then, we return that |
| 457 | ** through next_zero_bit for find_forward to try. |
| 458 | ** |
| 459 | ** Just because we return something in next_zero_bit does not mean we won't |
| 460 | ** reject it on the next call to reiserfs_in_journal |
| 461 | ** |
| 462 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 463 | int reiserfs_in_journal(struct super_block *sb, |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 464 | unsigned int bmap_nr, int bit_nr, int search_all, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 465 | b_blocknr_t * next_zero_bit) |
| 466 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 467 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 468 | struct reiserfs_journal_cnode *cn; |
| 469 | struct reiserfs_list_bitmap *jb; |
| 470 | int i; |
| 471 | unsigned long bl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 473 | *next_zero_bit = 0; /* always start this at zero. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 475 | PROC_INFO_INC(sb, journal.in_journal); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 476 | /* If we aren't doing a search_all, this is a metablock, and it will be logged before use. |
| 477 | ** if we crash before the transaction that freed it commits, this transaction won't |
| 478 | ** have committed either, and the block will never be written |
| 479 | */ |
| 480 | if (search_all) { |
| 481 | for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 482 | PROC_INFO_INC(sb, journal.in_journal_bitmap); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 483 | jb = journal->j_list_bitmap + i; |
| 484 | if (jb->journal_list && jb->bitmaps[bmap_nr] && |
| 485 | test_bit(bit_nr, |
| 486 | (unsigned long *)jb->bitmaps[bmap_nr]-> |
| 487 | data)) { |
| 488 | *next_zero_bit = |
| 489 | find_next_zero_bit((unsigned long *) |
| 490 | (jb->bitmaps[bmap_nr]-> |
| 491 | data), |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 492 | sb->s_blocksize << 3, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 493 | bit_nr + 1); |
| 494 | return 1; |
| 495 | } |
| 496 | } |
| 497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 499 | bl = bmap_nr * (sb->s_blocksize << 3) + bit_nr; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 500 | /* is it in any old transactions? */ |
| 501 | if (search_all |
| 502 | && (cn = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 503 | get_journal_hash_dev(sb, journal->j_list_hash_table, bl))) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 504 | return 1; |
| 505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 507 | /* is it in the current transaction. This should never happen */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 508 | if ((cn = get_journal_hash_dev(sb, journal->j_hash_table, bl))) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 509 | BUG(); |
| 510 | return 1; |
| 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 513 | PROC_INFO_INC(sb, journal.in_journal_reusable); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 514 | /* safe for reuse */ |
| 515 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* insert cn into table |
| 519 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 520 | static inline void insert_journal_hash(struct reiserfs_journal_cnode **table, |
| 521 | struct reiserfs_journal_cnode *cn) |
| 522 | { |
| 523 | struct reiserfs_journal_cnode *cn_orig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 525 | cn_orig = journal_hash(table, cn->sb, cn->blocknr); |
| 526 | cn->hnext = cn_orig; |
| 527 | cn->hprev = NULL; |
| 528 | if (cn_orig) { |
| 529 | cn_orig->hprev = cn; |
| 530 | } |
| 531 | journal_hash(table, cn->sb, cn->blocknr) = cn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* lock the current transaction */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 535 | static inline void lock_journal(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 536 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 537 | PROC_INFO_INC(sb, journal.lock_journal); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 538 | |
| 539 | reiserfs_mutex_lock_safe(&SB_JOURNAL(sb)->j_mutex, sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* unlock the current transaction */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 543 | static inline void unlock_journal(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 544 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 545 | mutex_unlock(&SB_JOURNAL(sb)->j_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static inline void get_journal_list(struct reiserfs_journal_list *jl) |
| 549 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 550 | jl->j_refcount++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static inline void put_journal_list(struct super_block *s, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 554 | struct reiserfs_journal_list *jl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 556 | if (jl->j_refcount < 1) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 557 | reiserfs_panic(s, "journal-2", "trans id %u, refcount at %d", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 558 | jl->j_trans_id, jl->j_refcount); |
| 559 | } |
| 560 | if (--jl->j_refcount == 0) |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 561 | kfree(jl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
| 565 | ** this used to be much more involved, and I'm keeping it just in case things get ugly again. |
| 566 | ** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a |
| 567 | ** transaction. |
| 568 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 569 | static void cleanup_freed_for_journal_list(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 570 | struct reiserfs_journal_list *jl) |
| 571 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 573 | struct reiserfs_list_bitmap *jb = jl->j_list_bitmap; |
| 574 | if (jb) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 575 | cleanup_bitmap_list(sb, jb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 576 | } |
| 577 | jl->j_list_bitmap->journal_list = NULL; |
| 578 | jl->j_list_bitmap = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static int journal_list_still_alive(struct super_block *s, |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 582 | unsigned int trans_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 584 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 585 | struct list_head *entry = &journal->j_journal_list; |
| 586 | struct reiserfs_journal_list *jl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 588 | if (!list_empty(entry)) { |
| 589 | jl = JOURNAL_LIST_ENTRY(entry->next); |
| 590 | if (jl->j_trans_id <= trans_id) { |
| 591 | return 1; |
| 592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 594 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Chris Mason | 398c95b | 2007-10-16 23:29:44 -0700 | [diff] [blame] | 597 | /* |
| 598 | * If page->mapping was null, we failed to truncate this page for |
| 599 | * some reason. Most likely because it was truncated after being |
| 600 | * logged via data=journal. |
| 601 | * |
| 602 | * This does a check to see if the buffer belongs to one of these |
| 603 | * lost pages before doing the final put_bh. If page->mapping was |
| 604 | * null, it tries to free buffers on the page, which should make the |
| 605 | * final page_cache_release drop the page from the lru. |
| 606 | */ |
| 607 | static void release_buffer_page(struct buffer_head *bh) |
| 608 | { |
| 609 | struct page *page = bh->b_page; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 610 | if (!page->mapping && trylock_page(page)) { |
Chris Mason | 398c95b | 2007-10-16 23:29:44 -0700 | [diff] [blame] | 611 | page_cache_get(page); |
| 612 | put_bh(bh); |
| 613 | if (!page->mapping) |
| 614 | try_to_free_buffers(page); |
| 615 | unlock_page(page); |
| 616 | page_cache_release(page); |
| 617 | } else { |
| 618 | put_bh(bh); |
| 619 | } |
| 620 | } |
| 621 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 622 | static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate) |
| 623 | { |
| 624 | char b[BDEVNAME_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 626 | if (buffer_journaled(bh)) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 627 | reiserfs_warning(NULL, "clm-2084", |
| 628 | "pinned buffer %lu:%s sent to disk", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 629 | bh->b_blocknr, bdevname(bh->b_bdev, b)); |
| 630 | } |
| 631 | if (uptodate) |
| 632 | set_buffer_uptodate(bh); |
| 633 | else |
| 634 | clear_buffer_uptodate(bh); |
Chris Mason | 398c95b | 2007-10-16 23:29:44 -0700 | [diff] [blame] | 635 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 636 | unlock_buffer(bh); |
Chris Mason | 398c95b | 2007-10-16 23:29:44 -0700 | [diff] [blame] | 637 | release_buffer_page(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 640 | static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate) |
| 641 | { |
| 642 | if (uptodate) |
| 643 | set_buffer_uptodate(bh); |
| 644 | else |
| 645 | clear_buffer_uptodate(bh); |
| 646 | unlock_buffer(bh); |
| 647 | put_bh(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 650 | static void submit_logged_buffer(struct buffer_head *bh) |
| 651 | { |
| 652 | get_bh(bh); |
| 653 | bh->b_end_io = reiserfs_end_buffer_io_sync; |
| 654 | clear_buffer_journal_new(bh); |
| 655 | clear_buffer_dirty(bh); |
| 656 | if (!test_clear_buffer_journal_test(bh)) |
| 657 | BUG(); |
| 658 | if (!buffer_uptodate(bh)) |
| 659 | BUG(); |
| 660 | submit_bh(WRITE, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 663 | static void submit_ordered_buffer(struct buffer_head *bh) |
| 664 | { |
| 665 | get_bh(bh); |
| 666 | bh->b_end_io = reiserfs_end_ordered_io; |
| 667 | clear_buffer_dirty(bh); |
| 668 | if (!buffer_uptodate(bh)) |
| 669 | BUG(); |
| 670 | submit_bh(WRITE, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | #define CHUNK_SIZE 32 |
| 674 | struct buffer_chunk { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 675 | struct buffer_head *bh[CHUNK_SIZE]; |
| 676 | int nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | }; |
| 678 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 679 | static void write_chunk(struct buffer_chunk *chunk) |
| 680 | { |
| 681 | int i; |
| 682 | get_fs_excl(); |
| 683 | for (i = 0; i < chunk->nr; i++) { |
| 684 | submit_logged_buffer(chunk->bh[i]); |
| 685 | } |
| 686 | chunk->nr = 0; |
| 687 | put_fs_excl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 690 | static void write_ordered_chunk(struct buffer_chunk *chunk) |
| 691 | { |
| 692 | int i; |
| 693 | get_fs_excl(); |
| 694 | for (i = 0; i < chunk->nr; i++) { |
| 695 | submit_ordered_buffer(chunk->bh[i]); |
| 696 | } |
| 697 | chunk->nr = 0; |
| 698 | put_fs_excl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 702 | spinlock_t * lock, void (fn) (struct buffer_chunk *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 704 | int ret = 0; |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 705 | BUG_ON(chunk->nr >= CHUNK_SIZE); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 706 | chunk->bh[chunk->nr++] = bh; |
| 707 | if (chunk->nr >= CHUNK_SIZE) { |
| 708 | ret = 1; |
| 709 | if (lock) |
| 710 | spin_unlock(lock); |
| 711 | fn(chunk); |
| 712 | if (lock) |
| 713 | spin_lock(lock); |
| 714 | } |
| 715 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 719 | static struct reiserfs_jh *alloc_jh(void) |
| 720 | { |
| 721 | struct reiserfs_jh *jh; |
| 722 | while (1) { |
| 723 | jh = kmalloc(sizeof(*jh), GFP_NOFS); |
| 724 | if (jh) { |
| 725 | atomic_inc(&nr_reiserfs_jh); |
| 726 | return jh; |
| 727 | } |
| 728 | yield(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* |
| 733 | * we want to free the jh when the buffer has been written |
| 734 | * and waited on |
| 735 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 736 | void reiserfs_free_jh(struct buffer_head *bh) |
| 737 | { |
| 738 | struct reiserfs_jh *jh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 740 | jh = bh->b_private; |
| 741 | if (jh) { |
| 742 | bh->b_private = NULL; |
| 743 | jh->bh = NULL; |
| 744 | list_del_init(&jh->list); |
| 745 | kfree(jh); |
| 746 | if (atomic_read(&nr_reiserfs_jh) <= 0) |
| 747 | BUG(); |
| 748 | atomic_dec(&nr_reiserfs_jh); |
| 749 | put_bh(bh); |
| 750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 754 | int tail) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 756 | struct reiserfs_jh *jh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 758 | if (bh->b_private) { |
| 759 | spin_lock(&j->j_dirty_buffers_lock); |
| 760 | if (!bh->b_private) { |
| 761 | spin_unlock(&j->j_dirty_buffers_lock); |
| 762 | goto no_jh; |
| 763 | } |
| 764 | jh = bh->b_private; |
| 765 | list_del_init(&jh->list); |
| 766 | } else { |
| 767 | no_jh: |
| 768 | get_bh(bh); |
| 769 | jh = alloc_jh(); |
| 770 | spin_lock(&j->j_dirty_buffers_lock); |
| 771 | /* buffer must be locked for __add_jh, should be able to have |
| 772 | * two adds at the same time |
| 773 | */ |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 774 | BUG_ON(bh->b_private); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 775 | jh->bh = bh; |
| 776 | bh->b_private = jh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 778 | jh->jl = j->j_current_jl; |
| 779 | if (tail) |
| 780 | list_add_tail(&jh->list, &jh->jl->j_tail_bh_list); |
| 781 | else { |
| 782 | list_add_tail(&jh->list, &jh->jl->j_bh_list); |
| 783 | } |
| 784 | spin_unlock(&j->j_dirty_buffers_lock); |
| 785 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 788 | int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh) |
| 789 | { |
| 790 | return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 792 | int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh) |
| 793 | { |
| 794 | return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | #define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 798 | static int write_ordered_buffers(spinlock_t * lock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | struct reiserfs_journal *j, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 800 | struct reiserfs_journal_list *jl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | struct list_head *list) |
| 802 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 803 | struct buffer_head *bh; |
| 804 | struct reiserfs_jh *jh; |
| 805 | int ret = j->j_errno; |
| 806 | struct buffer_chunk chunk; |
| 807 | struct list_head tmp; |
| 808 | INIT_LIST_HEAD(&tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 810 | chunk.nr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | spin_lock(lock); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 812 | while (!list_empty(list)) { |
| 813 | jh = JH_ENTRY(list->next); |
| 814 | bh = jh->bh; |
| 815 | get_bh(bh); |
Nick Piggin | ca5de40 | 2008-08-02 12:02:13 +0200 | [diff] [blame] | 816 | if (!trylock_buffer(bh)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 817 | if (!buffer_dirty(bh)) { |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 818 | list_move(&jh->list, &tmp); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 819 | goto loop_next; |
| 820 | } |
| 821 | spin_unlock(lock); |
| 822 | if (chunk.nr) |
| 823 | write_ordered_chunk(&chunk); |
| 824 | wait_on_buffer(bh); |
| 825 | cond_resched(); |
| 826 | spin_lock(lock); |
| 827 | goto loop_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
Chris Mason | 3d4492f | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 829 | /* in theory, dirty non-uptodate buffers should never get here, |
| 830 | * but the upper layer io error paths still have a few quirks. |
| 831 | * Handle them here as gracefully as we can |
| 832 | */ |
| 833 | if (!buffer_uptodate(bh) && buffer_dirty(bh)) { |
| 834 | clear_buffer_dirty(bh); |
| 835 | ret = -EIO; |
| 836 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 837 | if (buffer_dirty(bh)) { |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 838 | list_move(&jh->list, &tmp); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 839 | add_to_chunk(&chunk, bh, lock, write_ordered_chunk); |
| 840 | } else { |
| 841 | reiserfs_free_jh(bh); |
| 842 | unlock_buffer(bh); |
| 843 | } |
| 844 | loop_next: |
| 845 | put_bh(bh); |
| 846 | cond_resched_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 848 | if (chunk.nr) { |
| 849 | spin_unlock(lock); |
| 850 | write_ordered_chunk(&chunk); |
| 851 | spin_lock(lock); |
| 852 | } |
| 853 | while (!list_empty(&tmp)) { |
| 854 | jh = JH_ENTRY(tmp.prev); |
| 855 | bh = jh->bh; |
| 856 | get_bh(bh); |
| 857 | reiserfs_free_jh(bh); |
| 858 | |
| 859 | if (buffer_locked(bh)) { |
| 860 | spin_unlock(lock); |
| 861 | wait_on_buffer(bh); |
| 862 | spin_lock(lock); |
| 863 | } |
| 864 | if (!buffer_uptodate(bh)) { |
| 865 | ret = -EIO; |
| 866 | } |
Chris Mason | d62b1b8 | 2006-02-01 03:06:47 -0800 | [diff] [blame] | 867 | /* ugly interaction with invalidatepage here. |
| 868 | * reiserfs_invalidate_page will pin any buffer that has a valid |
| 869 | * journal head from an older transaction. If someone else sets |
| 870 | * our buffer dirty after we write it in the first loop, and |
| 871 | * then someone truncates the page away, nobody will ever write |
| 872 | * the buffer. We're safe if we write the page one last time |
| 873 | * after freeing the journal header. |
| 874 | */ |
| 875 | if (buffer_dirty(bh) && unlikely(bh->b_page->mapping == NULL)) { |
| 876 | spin_unlock(lock); |
| 877 | ll_rw_block(WRITE, 1, &bh); |
| 878 | spin_lock(lock); |
| 879 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 880 | put_bh(bh); |
| 881 | cond_resched_lock(lock); |
| 882 | } |
| 883 | spin_unlock(lock); |
| 884 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 886 | |
| 887 | static int flush_older_commits(struct super_block *s, |
| 888 | struct reiserfs_journal_list *jl) |
| 889 | { |
| 890 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 891 | struct reiserfs_journal_list *other_jl; |
| 892 | struct reiserfs_journal_list *first_jl; |
| 893 | struct list_head *entry; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 894 | unsigned int trans_id = jl->j_trans_id; |
| 895 | unsigned int other_trans_id; |
| 896 | unsigned int first_trans_id; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 897 | |
| 898 | find_first: |
| 899 | /* |
| 900 | * first we walk backwards to find the oldest uncommitted transation |
| 901 | */ |
| 902 | first_jl = jl; |
| 903 | entry = jl->j_list.prev; |
| 904 | while (1) { |
| 905 | other_jl = JOURNAL_LIST_ENTRY(entry); |
| 906 | if (entry == &journal->j_journal_list || |
| 907 | atomic_read(&other_jl->j_older_commits_done)) |
| 908 | break; |
| 909 | |
| 910 | first_jl = other_jl; |
| 911 | entry = other_jl->j_list.prev; |
| 912 | } |
| 913 | |
| 914 | /* if we didn't find any older uncommitted transactions, return now */ |
| 915 | if (first_jl == jl) { |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | first_trans_id = first_jl->j_trans_id; |
| 920 | |
| 921 | entry = &first_jl->j_list; |
| 922 | while (1) { |
| 923 | other_jl = JOURNAL_LIST_ENTRY(entry); |
| 924 | other_trans_id = other_jl->j_trans_id; |
| 925 | |
| 926 | if (other_trans_id < trans_id) { |
| 927 | if (atomic_read(&other_jl->j_commit_left) != 0) { |
| 928 | flush_commit_list(s, other_jl, 0); |
| 929 | |
| 930 | /* list we were called with is gone, return */ |
| 931 | if (!journal_list_still_alive(s, trans_id)) |
| 932 | return 1; |
| 933 | |
| 934 | /* the one we just flushed is gone, this means all |
| 935 | * older lists are also gone, so first_jl is no longer |
| 936 | * valid either. Go back to the beginning. |
| 937 | */ |
| 938 | if (!journal_list_still_alive |
| 939 | (s, other_trans_id)) { |
| 940 | goto find_first; |
| 941 | } |
| 942 | } |
| 943 | entry = entry->next; |
| 944 | if (entry == &journal->j_journal_list) |
| 945 | return 0; |
| 946 | } else { |
| 947 | return 0; |
| 948 | } |
| 949 | } |
| 950 | return 0; |
| 951 | } |
Adrian Bunk | deba0f4 | 2007-10-16 23:26:03 -0700 | [diff] [blame] | 952 | |
| 953 | static int reiserfs_async_progress_wait(struct super_block *s) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 954 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 955 | struct reiserfs_journal *j = SB_JOURNAL(s); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 956 | |
| 957 | if (atomic_read(&j->j_async_throttle)) { |
| 958 | reiserfs_write_unlock(s); |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 959 | congestion_wait(BLK_RW_ASYNC, HZ / 10); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 960 | reiserfs_write_lock(s); |
| 961 | } |
| 962 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 963 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /* |
| 967 | ** if this journal list still has commit blocks unflushed, send them to disk. |
| 968 | ** |
| 969 | ** log areas must be flushed in order (transaction 2 can't commit before transaction 1) |
| 970 | ** Before the commit block can by written, every other log block must be safely on disk |
| 971 | ** |
| 972 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 973 | static int flush_commit_list(struct super_block *s, |
| 974 | struct reiserfs_journal_list *jl, int flushall) |
| 975 | { |
| 976 | int i; |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 977 | b_blocknr_t bn; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 978 | struct buffer_head *tbh = NULL; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 979 | unsigned int trans_id = jl->j_trans_id; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 980 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 981 | int retval = 0; |
Chris Mason | e0e851c | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 982 | int write_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 984 | reiserfs_check_lock_depth(s, "flush_commit_list"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 986 | if (atomic_read(&jl->j_older_commits_done)) { |
| 987 | return 0; |
| 988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 990 | get_fs_excl(); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 991 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 992 | /* before we can put our commit blocks on disk, we have to make sure everyone older than |
| 993 | ** us is on disk too |
| 994 | */ |
| 995 | BUG_ON(jl->j_len <= 0); |
| 996 | BUG_ON(trans_id == journal->j_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 998 | get_journal_list(jl); |
| 999 | if (flushall) { |
| 1000 | if (flush_older_commits(s, jl) == 1) { |
| 1001 | /* list disappeared during flush_older_commits. return */ |
| 1002 | goto put_jl; |
| 1003 | } |
| 1004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1006 | /* make sure nobody is trying to flush this one at the same time */ |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1007 | reiserfs_mutex_lock_safe(&jl->j_commit_mutex, s); |
| 1008 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1009 | if (!journal_list_still_alive(s, trans_id)) { |
Jeff Mahoney | 90415de | 2008-07-25 01:46:40 -0700 | [diff] [blame] | 1010 | mutex_unlock(&jl->j_commit_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1011 | goto put_jl; |
| 1012 | } |
| 1013 | BUG_ON(jl->j_trans_id == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1015 | /* this commit is done, exit */ |
| 1016 | if (atomic_read(&(jl->j_commit_left)) <= 0) { |
| 1017 | if (flushall) { |
| 1018 | atomic_set(&(jl->j_older_commits_done), 1); |
| 1019 | } |
Jeff Mahoney | 90415de | 2008-07-25 01:46:40 -0700 | [diff] [blame] | 1020 | mutex_unlock(&jl->j_commit_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1021 | goto put_jl; |
| 1022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1024 | if (!list_empty(&jl->j_bh_list)) { |
Chris Mason | 3d4492f | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 1025 | int ret; |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* |
| 1028 | * We might sleep in numerous places inside |
| 1029 | * write_ordered_buffers. Relax the write lock. |
| 1030 | */ |
| 1031 | reiserfs_write_unlock(s); |
Chris Mason | 3d4492f | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 1032 | ret = write_ordered_buffers(&journal->j_dirty_buffers_lock, |
| 1033 | journal, jl, &jl->j_bh_list); |
| 1034 | if (ret < 0 && retval == 0) |
| 1035 | retval = ret; |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1036 | reiserfs_write_lock(s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1037 | } |
| 1038 | BUG_ON(!list_empty(&jl->j_bh_list)); |
| 1039 | /* |
| 1040 | * for the description block and all the log blocks, submit any buffers |
Chris Mason | e0e851c | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 1041 | * that haven't already reached the disk. Try to write at least 256 |
| 1042 | * log blocks. later on, we will only wait on blocks that correspond |
| 1043 | * to this transaction, but while we're unplugging we might as well |
| 1044 | * get a chunk of data on there. |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1045 | */ |
| 1046 | atomic_inc(&journal->j_async_throttle); |
Chris Mason | e0e851c | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 1047 | write_len = jl->j_len + 1; |
| 1048 | if (write_len < 256) |
| 1049 | write_len = 256; |
| 1050 | for (i = 0 ; i < write_len ; i++) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1051 | bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start + i) % |
| 1052 | SB_ONDISK_JOURNAL_SIZE(s); |
| 1053 | tbh = journal_find_get_block(s, bn); |
Chris Mason | e0e851c | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 1054 | if (tbh) { |
Frederic Weisbecker | 6e3647a | 2009-05-01 02:27:39 +0200 | [diff] [blame] | 1055 | if (buffer_dirty(tbh)) { |
| 1056 | reiserfs_write_unlock(s); |
| 1057 | ll_rw_block(WRITE, 1, &tbh); |
| 1058 | reiserfs_write_lock(s); |
| 1059 | } |
Chris Mason | e0e851c | 2006-02-01 03:06:49 -0800 | [diff] [blame] | 1060 | put_bh(tbh) ; |
| 1061 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1062 | } |
| 1063 | atomic_dec(&journal->j_async_throttle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1065 | for (i = 0; i < (jl->j_len + 1); i++) { |
| 1066 | bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + |
| 1067 | (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s); |
| 1068 | tbh = journal_find_get_block(s, bn); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1069 | |
| 1070 | reiserfs_write_unlock(s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1071 | wait_on_buffer(tbh); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1072 | reiserfs_write_lock(s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1073 | // since we're using ll_rw_blk above, it might have skipped over |
| 1074 | // a locked buffer. Double check here |
| 1075 | // |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1076 | /* redundant, sync_dirty_buffer() checks */ |
| 1077 | if (buffer_dirty(tbh)) { |
| 1078 | reiserfs_write_unlock(s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1079 | sync_dirty_buffer(tbh); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1080 | reiserfs_write_lock(s); |
| 1081 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1082 | if (unlikely(!buffer_uptodate(tbh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | #ifdef CONFIG_REISERFS_CHECK |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1084 | reiserfs_warning(s, "journal-601", |
| 1085 | "buffer write failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1087 | retval = -EIO; |
| 1088 | } |
| 1089 | put_bh(tbh); /* once for journal_find_get_block */ |
| 1090 | put_bh(tbh); /* once due to original getblk in do_journal_end */ |
| 1091 | atomic_dec(&(jl->j_commit_left)); |
| 1092 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1094 | BUG_ON(atomic_read(&(jl->j_commit_left)) != 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Christoph Hellwig | 7cd33ad | 2010-08-18 05:29:14 -0400 | [diff] [blame] | 1096 | /* If there was a write error in the journal - we can't commit |
| 1097 | * this transaction - it will be invalid and, if successful, |
| 1098 | * will just end up propagating the write error out to |
| 1099 | * the file system. */ |
| 1100 | if (likely(!retval && !reiserfs_is_journal_aborted (journal))) { |
| 1101 | if (buffer_dirty(jl->j_commit_bh)) |
| 1102 | BUG(); |
| 1103 | mark_buffer_dirty(jl->j_commit_bh) ; |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1104 | reiserfs_write_unlock(s); |
Christoph Hellwig | 7cd33ad | 2010-08-18 05:29:14 -0400 | [diff] [blame] | 1105 | if (reiserfs_barrier_flush(s)) |
| 1106 | __sync_dirty_buffer(jl->j_commit_bh, WRITE_FLUSH_FUA); |
| 1107 | else |
| 1108 | sync_dirty_buffer(jl->j_commit_bh); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1109 | reiserfs_write_lock(s); |
| 1110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1112 | /* If there was a write error in the journal - we can't commit this |
| 1113 | * transaction - it will be invalid and, if successful, will just end |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 1114 | * up propagating the write error out to the filesystem. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1115 | if (unlikely(!buffer_uptodate(jl->j_commit_bh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | #ifdef CONFIG_REISERFS_CHECK |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1117 | reiserfs_warning(s, "journal-615", "buffer write failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1119 | retval = -EIO; |
| 1120 | } |
| 1121 | bforget(jl->j_commit_bh); |
| 1122 | if (journal->j_last_commit_id != 0 && |
| 1123 | (jl->j_trans_id - journal->j_last_commit_id) != 1) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1124 | reiserfs_warning(s, "clm-2200", "last commit %lu, current %lu", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1125 | journal->j_last_commit_id, jl->j_trans_id); |
| 1126 | } |
| 1127 | journal->j_last_commit_id = jl->j_trans_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1129 | /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */ |
| 1130 | cleanup_freed_for_journal_list(s, jl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1132 | retval = retval ? retval : journal->j_errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1134 | /* mark the metadata dirty */ |
| 1135 | if (!retval) |
| 1136 | dirty_one_transaction(s, jl); |
| 1137 | atomic_dec(&(jl->j_commit_left)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1139 | if (flushall) { |
| 1140 | atomic_set(&(jl->j_older_commits_done), 1); |
| 1141 | } |
Jeff Mahoney | 90415de | 2008-07-25 01:46:40 -0700 | [diff] [blame] | 1142 | mutex_unlock(&jl->j_commit_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1143 | put_jl: |
| 1144 | put_journal_list(s, jl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1146 | if (retval) |
| 1147 | reiserfs_abort(s, retval, "Journal write error in %s", |
Harvey Harrison | fbe5498 | 2008-04-28 02:16:22 -0700 | [diff] [blame] | 1148 | __func__); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1149 | put_fs_excl(); |
| 1150 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /* |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1154 | ** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or |
| 1155 | ** returns NULL if it can't find anything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1157 | static struct reiserfs_journal_list *find_newer_jl_for_cn(struct |
| 1158 | reiserfs_journal_cnode |
| 1159 | *cn) |
| 1160 | { |
| 1161 | struct super_block *sb = cn->sb; |
| 1162 | b_blocknr_t blocknr = cn->blocknr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1164 | cn = cn->hprev; |
| 1165 | while (cn) { |
| 1166 | if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) { |
| 1167 | return cn->jlist; |
| 1168 | } |
| 1169 | cn = cn->hprev; |
| 1170 | } |
| 1171 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Chris Mason | a317202 | 2006-09-29 01:59:56 -0700 | [diff] [blame] | 1174 | static int newer_jl_done(struct reiserfs_journal_cnode *cn) |
| 1175 | { |
| 1176 | struct super_block *sb = cn->sb; |
| 1177 | b_blocknr_t blocknr = cn->blocknr; |
| 1178 | |
| 1179 | cn = cn->hprev; |
| 1180 | while (cn) { |
| 1181 | if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist && |
| 1182 | atomic_read(&cn->jlist->j_commit_left) != 0) |
| 1183 | return 0; |
| 1184 | cn = cn->hprev; |
| 1185 | } |
| 1186 | return 1; |
| 1187 | } |
| 1188 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1189 | static void remove_journal_hash(struct super_block *, |
| 1190 | struct reiserfs_journal_cnode **, |
| 1191 | struct reiserfs_journal_list *, unsigned long, |
| 1192 | int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
| 1194 | /* |
| 1195 | ** once all the real blocks have been flushed, it is safe to remove them from the |
| 1196 | ** journal list for this transaction. Aside from freeing the cnode, this also allows the |
| 1197 | ** block to be reallocated for data blocks if it had been deleted. |
| 1198 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1199 | static void remove_all_from_journal_list(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1200 | struct reiserfs_journal_list *jl, |
| 1201 | int debug) |
| 1202 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1203 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1204 | struct reiserfs_journal_cnode *cn, *last; |
| 1205 | cn = jl->j_realblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1207 | /* which is better, to lock once around the whole loop, or |
| 1208 | ** to lock for each call to remove_journal_hash? |
| 1209 | */ |
| 1210 | while (cn) { |
| 1211 | if (cn->blocknr != 0) { |
| 1212 | if (debug) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1213 | reiserfs_warning(sb, "reiserfs-2201", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1214 | "block %u, bh is %d, state %ld", |
| 1215 | cn->blocknr, cn->bh ? 1 : 0, |
| 1216 | cn->state); |
| 1217 | } |
| 1218 | cn->state = 0; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1219 | remove_journal_hash(sb, journal->j_list_hash_table, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1220 | jl, cn->blocknr, 1); |
| 1221 | } |
| 1222 | last = cn; |
| 1223 | cn = cn->next; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1224 | free_cnode(sb, last); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1225 | } |
| 1226 | jl->j_realblock = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | ** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block. |
| 1231 | ** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start |
| 1232 | ** releasing blocks in this transaction for reuse as data blocks. |
| 1233 | ** called by flush_journal_list, before it calls remove_all_from_journal_list |
| 1234 | ** |
| 1235 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1236 | static int _update_journal_header_block(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1237 | unsigned long offset, |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 1238 | unsigned int trans_id) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1239 | { |
| 1240 | struct reiserfs_journal_header *jh; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1241 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1243 | if (reiserfs_is_journal_aborted(journal)) |
| 1244 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1246 | if (trans_id >= journal->j_last_flush_trans_id) { |
| 1247 | if (buffer_locked((journal->j_header_bh))) { |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1248 | reiserfs_write_unlock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1249 | wait_on_buffer((journal->j_header_bh)); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1250 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1251 | if (unlikely(!buffer_uptodate(journal->j_header_bh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | #ifdef CONFIG_REISERFS_CHECK |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1253 | reiserfs_warning(sb, "journal-699", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1254 | "buffer write failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1256 | return -EIO; |
| 1257 | } |
| 1258 | } |
| 1259 | journal->j_last_flush_trans_id = trans_id; |
| 1260 | journal->j_first_unflushed_offset = offset; |
| 1261 | jh = (struct reiserfs_journal_header *)(journal->j_header_bh-> |
| 1262 | b_data); |
| 1263 | jh->j_last_flush_trans_id = cpu_to_le32(trans_id); |
| 1264 | jh->j_first_unflushed_offset = cpu_to_le32(offset); |
| 1265 | jh->j_mount_id = cpu_to_le32(journal->j_mount_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
Christoph Hellwig | 7cd33ad | 2010-08-18 05:29:14 -0400 | [diff] [blame] | 1267 | set_buffer_dirty(journal->j_header_bh); |
| 1268 | reiserfs_write_unlock(sb); |
| 1269 | |
| 1270 | if (reiserfs_barrier_flush(sb)) |
| 1271 | __sync_dirty_buffer(journal->j_header_bh, WRITE_FLUSH_FUA); |
| 1272 | else |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1273 | sync_dirty_buffer(journal->j_header_bh); |
Christoph Hellwig | 7cd33ad | 2010-08-18 05:29:14 -0400 | [diff] [blame] | 1274 | |
| 1275 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1276 | if (!buffer_uptodate(journal->j_header_bh)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1277 | reiserfs_warning(sb, "journal-837", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1278 | "IO error during journal replay"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1279 | return -EIO; |
| 1280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1282 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1285 | static int update_journal_header_block(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1286 | unsigned long offset, |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 1287 | unsigned int trans_id) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1288 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1289 | return _update_journal_header_block(sb, offset, trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1291 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1292 | /* |
| 1293 | ** flush any and all journal lists older than you are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | ** can only be called from flush_journal_list |
| 1295 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1296 | static int flush_older_journal_lists(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1297 | struct reiserfs_journal_list *jl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1299 | struct list_head *entry; |
| 1300 | struct reiserfs_journal_list *other_jl; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1301 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 1302 | unsigned int trans_id = jl->j_trans_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1304 | /* we know we are the only ones flushing things, no extra race |
| 1305 | * protection is required. |
| 1306 | */ |
| 1307 | restart: |
| 1308 | entry = journal->j_journal_list.next; |
| 1309 | /* Did we wrap? */ |
| 1310 | if (entry == &journal->j_journal_list) |
| 1311 | return 0; |
| 1312 | other_jl = JOURNAL_LIST_ENTRY(entry); |
| 1313 | if (other_jl->j_trans_id < trans_id) { |
| 1314 | BUG_ON(other_jl->j_refcount <= 0); |
| 1315 | /* do not flush all */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1316 | flush_journal_list(sb, other_jl, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1318 | /* other_jl is now deleted from the list */ |
| 1319 | goto restart; |
| 1320 | } |
| 1321 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | static void del_from_work_list(struct super_block *s, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1325 | struct reiserfs_journal_list *jl) |
| 1326 | { |
| 1327 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 1328 | if (!list_empty(&jl->j_working_list)) { |
| 1329 | list_del_init(&jl->j_working_list); |
| 1330 | journal->j_num_work_lists--; |
| 1331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* flush a journal list, both commit and real blocks |
| 1335 | ** |
| 1336 | ** always set flushall to 1, unless you are calling from inside |
| 1337 | ** flush_journal_list |
| 1338 | ** |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1339 | ** IMPORTANT. This can only be called while there are no journal writers, |
| 1340 | ** and the journal is locked. That means it can only be called from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | ** do_journal_end, or by journal_release |
| 1342 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1343 | static int flush_journal_list(struct super_block *s, |
| 1344 | struct reiserfs_journal_list *jl, int flushall) |
| 1345 | { |
| 1346 | struct reiserfs_journal_list *pjl; |
| 1347 | struct reiserfs_journal_cnode *cn, *last; |
| 1348 | int count; |
| 1349 | int was_jwait = 0; |
| 1350 | int was_dirty = 0; |
| 1351 | struct buffer_head *saved_bh; |
| 1352 | unsigned long j_len_saved = jl->j_len; |
| 1353 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 1354 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1356 | BUG_ON(j_len_saved <= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1358 | if (atomic_read(&journal->j_wcount) != 0) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1359 | reiserfs_warning(s, "clm-2048", "called with wcount %d", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1360 | atomic_read(&journal->j_wcount)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1362 | BUG_ON(jl->j_trans_id == 0); |
| 1363 | |
| 1364 | /* if flushall == 0, the lock is already held */ |
| 1365 | if (flushall) { |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1366 | reiserfs_mutex_lock_safe(&journal->j_flush_mutex, s); |
Jeff Mahoney | afe7025 | 2008-07-25 01:46:39 -0700 | [diff] [blame] | 1367 | } else if (mutex_trylock(&journal->j_flush_mutex)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1368 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1370 | |
| 1371 | count = 0; |
| 1372 | if (j_len_saved > journal->j_trans_max) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1373 | reiserfs_panic(s, "journal-715", "length is %lu, trans id %lu", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1374 | j_len_saved, jl->j_trans_id); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | get_fs_excl(); |
| 1379 | |
| 1380 | /* if all the work is already done, get out of here */ |
| 1381 | if (atomic_read(&(jl->j_nonzerolen)) <= 0 && |
| 1382 | atomic_read(&(jl->j_commit_left)) <= 0) { |
| 1383 | goto flush_older_and_return; |
| 1384 | } |
| 1385 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1386 | /* start by putting the commit list on disk. This will also flush |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1387 | ** the commit lists of any olders transactions |
| 1388 | */ |
| 1389 | flush_commit_list(s, jl, 1); |
| 1390 | |
| 1391 | if (!(jl->j_state & LIST_DIRTY) |
| 1392 | && !reiserfs_is_journal_aborted(journal)) |
| 1393 | BUG(); |
| 1394 | |
| 1395 | /* are we done now? */ |
| 1396 | if (atomic_read(&(jl->j_nonzerolen)) <= 0 && |
| 1397 | atomic_read(&(jl->j_commit_left)) <= 0) { |
| 1398 | goto flush_older_and_return; |
| 1399 | } |
| 1400 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1401 | /* loop through each cnode, see if we need to write it, |
| 1402 | ** or wait on a more recent transaction, or just ignore it |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1403 | */ |
| 1404 | if (atomic_read(&(journal->j_wcount)) != 0) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1405 | reiserfs_panic(s, "journal-844", "journal list is flushing, " |
| 1406 | "wcount is not 0"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1407 | } |
| 1408 | cn = jl->j_realblock; |
| 1409 | while (cn) { |
| 1410 | was_jwait = 0; |
| 1411 | was_dirty = 0; |
| 1412 | saved_bh = NULL; |
| 1413 | /* blocknr of 0 is no longer in the hash, ignore it */ |
| 1414 | if (cn->blocknr == 0) { |
| 1415 | goto free_cnode; |
| 1416 | } |
| 1417 | |
| 1418 | /* This transaction failed commit. Don't write out to the disk */ |
| 1419 | if (!(jl->j_state & LIST_DIRTY)) |
| 1420 | goto free_cnode; |
| 1421 | |
| 1422 | pjl = find_newer_jl_for_cn(cn); |
| 1423 | /* the order is important here. We check pjl to make sure we |
| 1424 | ** don't clear BH_JDirty_wait if we aren't the one writing this |
| 1425 | ** block to disk |
| 1426 | */ |
| 1427 | if (!pjl && cn->bh) { |
| 1428 | saved_bh = cn->bh; |
| 1429 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1430 | /* we do this to make sure nobody releases the buffer while |
| 1431 | ** we are working with it |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1432 | */ |
| 1433 | get_bh(saved_bh); |
| 1434 | |
| 1435 | if (buffer_journal_dirty(saved_bh)) { |
| 1436 | BUG_ON(!can_dirty(cn)); |
| 1437 | was_jwait = 1; |
| 1438 | was_dirty = 1; |
| 1439 | } else if (can_dirty(cn)) { |
| 1440 | /* everything with !pjl && jwait should be writable */ |
| 1441 | BUG(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /* if someone has this block in a newer transaction, just make |
Matt LaPlante | 0779bf2 | 2006-11-30 05:24:39 +0100 | [diff] [blame] | 1446 | ** sure they are committed, and don't try writing it to disk |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1447 | */ |
| 1448 | if (pjl) { |
| 1449 | if (atomic_read(&pjl->j_commit_left)) |
| 1450 | flush_commit_list(s, pjl, 1); |
| 1451 | goto free_cnode; |
| 1452 | } |
| 1453 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1454 | /* bh == NULL when the block got to disk on its own, OR, |
| 1455 | ** the block got freed in a future transaction |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1456 | */ |
| 1457 | if (saved_bh == NULL) { |
| 1458 | goto free_cnode; |
| 1459 | } |
| 1460 | |
| 1461 | /* this should never happen. kupdate_one_transaction has this list |
| 1462 | ** locked while it works, so we should never see a buffer here that |
| 1463 | ** is not marked JDirty_wait |
| 1464 | */ |
| 1465 | if ((!was_jwait) && !buffer_locked(saved_bh)) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1466 | reiserfs_warning(s, "journal-813", |
| 1467 | "BAD! buffer %llu %cdirty %cjwait, " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1468 | "not in a newer tranasction", |
| 1469 | (unsigned long long)saved_bh-> |
| 1470 | b_blocknr, was_dirty ? ' ' : '!', |
| 1471 | was_jwait ? ' ' : '!'); |
| 1472 | } |
| 1473 | if (was_dirty) { |
| 1474 | /* we inc again because saved_bh gets decremented at free_cnode */ |
| 1475 | get_bh(saved_bh); |
| 1476 | set_bit(BLOCK_NEEDS_FLUSH, &cn->state); |
| 1477 | lock_buffer(saved_bh); |
| 1478 | BUG_ON(cn->blocknr != saved_bh->b_blocknr); |
| 1479 | if (buffer_dirty(saved_bh)) |
| 1480 | submit_logged_buffer(saved_bh); |
| 1481 | else |
| 1482 | unlock_buffer(saved_bh); |
| 1483 | count++; |
| 1484 | } else { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1485 | reiserfs_warning(s, "clm-2082", |
| 1486 | "Unable to flush buffer %llu in %s", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1487 | (unsigned long long)saved_bh-> |
Harvey Harrison | fbe5498 | 2008-04-28 02:16:22 -0700 | [diff] [blame] | 1488 | b_blocknr, __func__); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1489 | } |
| 1490 | free_cnode: |
| 1491 | last = cn; |
| 1492 | cn = cn->next; |
| 1493 | if (saved_bh) { |
| 1494 | /* we incremented this to keep others from taking the buffer head away */ |
| 1495 | put_bh(saved_bh); |
| 1496 | if (atomic_read(&(saved_bh->b_count)) < 0) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1497 | reiserfs_warning(s, "journal-945", |
| 1498 | "saved_bh->b_count < 0"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | if (count > 0) { |
| 1503 | cn = jl->j_realblock; |
| 1504 | while (cn) { |
| 1505 | if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) { |
| 1506 | if (!cn->bh) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1507 | reiserfs_panic(s, "journal-1011", |
| 1508 | "cn->bh is NULL"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1509 | } |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1510 | |
| 1511 | reiserfs_write_unlock(s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1512 | wait_on_buffer(cn->bh); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1513 | reiserfs_write_lock(s); |
| 1514 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1515 | if (!cn->bh) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1516 | reiserfs_panic(s, "journal-1012", |
| 1517 | "cn->bh is NULL"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1518 | } |
| 1519 | if (unlikely(!buffer_uptodate(cn->bh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | #ifdef CONFIG_REISERFS_CHECK |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1521 | reiserfs_warning(s, "journal-949", |
| 1522 | "buffer write failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1524 | err = -EIO; |
| 1525 | } |
| 1526 | /* note, we must clear the JDirty_wait bit after the up to date |
| 1527 | ** check, otherwise we race against our flushpage routine |
| 1528 | */ |
| 1529 | BUG_ON(!test_clear_buffer_journal_dirty |
| 1530 | (cn->bh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | |
Chris Mason | 398c95b | 2007-10-16 23:29:44 -0700 | [diff] [blame] | 1532 | /* drop one ref for us */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1533 | put_bh(cn->bh); |
Chris Mason | 398c95b | 2007-10-16 23:29:44 -0700 | [diff] [blame] | 1534 | /* drop one ref for journal_mark_dirty */ |
| 1535 | release_buffer_page(cn->bh); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1536 | } |
| 1537 | cn = cn->next; |
| 1538 | } |
| 1539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1541 | if (err) |
| 1542 | reiserfs_abort(s, -EIO, |
| 1543 | "Write error while pushing transaction to disk in %s", |
Harvey Harrison | fbe5498 | 2008-04-28 02:16:22 -0700 | [diff] [blame] | 1544 | __func__); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1545 | flush_older_and_return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1547 | /* before we can update the journal header block, we _must_ flush all |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1548 | ** real blocks from all older transactions to disk. This is because |
| 1549 | ** once the header block is updated, this transaction will not be |
| 1550 | ** replayed after a crash |
| 1551 | */ |
| 1552 | if (flushall) { |
| 1553 | flush_older_journal_lists(s, jl); |
| 1554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1556 | err = journal->j_errno; |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1557 | /* before we can remove everything from the hash tables for this |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1558 | ** transaction, we must make sure it can never be replayed |
| 1559 | ** |
| 1560 | ** since we are only called from do_journal_end, we know for sure there |
| 1561 | ** are no allocations going on while we are flushing journal lists. So, |
| 1562 | ** we only need to update the journal header block for the last list |
| 1563 | ** being flushed |
| 1564 | */ |
| 1565 | if (!err && flushall) { |
| 1566 | err = |
| 1567 | update_journal_header_block(s, |
| 1568 | (jl->j_start + jl->j_len + |
| 1569 | 2) % SB_ONDISK_JOURNAL_SIZE(s), |
| 1570 | jl->j_trans_id); |
| 1571 | if (err) |
| 1572 | reiserfs_abort(s, -EIO, |
| 1573 | "Write error while updating journal header in %s", |
Harvey Harrison | fbe5498 | 2008-04-28 02:16:22 -0700 | [diff] [blame] | 1574 | __func__); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1575 | } |
| 1576 | remove_all_from_journal_list(s, jl, 0); |
| 1577 | list_del_init(&jl->j_list); |
| 1578 | journal->j_num_lists--; |
| 1579 | del_from_work_list(s, jl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1581 | if (journal->j_last_flush_id != 0 && |
| 1582 | (jl->j_trans_id - journal->j_last_flush_id) != 1) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1583 | reiserfs_warning(s, "clm-2201", "last flush %lu, current %lu", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1584 | journal->j_last_flush_id, jl->j_trans_id); |
| 1585 | } |
| 1586 | journal->j_last_flush_id = jl->j_trans_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1588 | /* not strictly required since we are freeing the list, but it should |
| 1589 | * help find code using dead lists later on |
| 1590 | */ |
| 1591 | jl->j_len = 0; |
| 1592 | atomic_set(&(jl->j_nonzerolen), 0); |
| 1593 | jl->j_start = 0; |
| 1594 | jl->j_realblock = NULL; |
| 1595 | jl->j_commit_bh = NULL; |
| 1596 | jl->j_trans_id = 0; |
| 1597 | jl->j_state = 0; |
| 1598 | put_journal_list(s, jl); |
| 1599 | if (flushall) |
Jeff Mahoney | afe7025 | 2008-07-25 01:46:39 -0700 | [diff] [blame] | 1600 | mutex_unlock(&journal->j_flush_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1601 | put_fs_excl(); |
| 1602 | return err; |
| 1603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
Chris Mason | a317202 | 2006-09-29 01:59:56 -0700 | [diff] [blame] | 1605 | static int test_transaction(struct super_block *s, |
| 1606 | struct reiserfs_journal_list *jl) |
| 1607 | { |
| 1608 | struct reiserfs_journal_cnode *cn; |
| 1609 | |
| 1610 | if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) |
| 1611 | return 1; |
| 1612 | |
| 1613 | cn = jl->j_realblock; |
| 1614 | while (cn) { |
| 1615 | /* if the blocknr == 0, this has been cleared from the hash, |
| 1616 | ** skip it |
| 1617 | */ |
| 1618 | if (cn->blocknr == 0) { |
| 1619 | goto next; |
| 1620 | } |
| 1621 | if (cn->bh && !newer_jl_done(cn)) |
| 1622 | return 0; |
| 1623 | next: |
| 1624 | cn = cn->next; |
| 1625 | cond_resched(); |
| 1626 | } |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | static int write_one_transaction(struct super_block *s, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1631 | struct reiserfs_journal_list *jl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | struct buffer_chunk *chunk) |
| 1633 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1634 | struct reiserfs_journal_cnode *cn; |
| 1635 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1637 | jl->j_state |= LIST_TOUCHED; |
| 1638 | del_from_work_list(s, jl); |
| 1639 | if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) { |
| 1640 | return 0; |
| 1641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1643 | cn = jl->j_realblock; |
| 1644 | while (cn) { |
| 1645 | /* if the blocknr == 0, this has been cleared from the hash, |
| 1646 | ** skip it |
| 1647 | */ |
| 1648 | if (cn->blocknr == 0) { |
| 1649 | goto next; |
| 1650 | } |
| 1651 | if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) { |
| 1652 | struct buffer_head *tmp_bh; |
| 1653 | /* we can race against journal_mark_freed when we try |
| 1654 | * to lock_buffer(cn->bh), so we have to inc the buffer |
| 1655 | * count, and recheck things after locking |
| 1656 | */ |
| 1657 | tmp_bh = cn->bh; |
| 1658 | get_bh(tmp_bh); |
| 1659 | lock_buffer(tmp_bh); |
| 1660 | if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) { |
| 1661 | if (!buffer_journal_dirty(tmp_bh) || |
| 1662 | buffer_journal_prepared(tmp_bh)) |
| 1663 | BUG(); |
| 1664 | add_to_chunk(chunk, tmp_bh, NULL, write_chunk); |
| 1665 | ret++; |
| 1666 | } else { |
| 1667 | /* note, cn->bh might be null now */ |
| 1668 | unlock_buffer(tmp_bh); |
| 1669 | } |
| 1670 | put_bh(tmp_bh); |
| 1671 | } |
| 1672 | next: |
| 1673 | cn = cn->next; |
| 1674 | cond_resched(); |
| 1675 | } |
| 1676 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /* used by flush_commit_list */ |
| 1680 | static int dirty_one_transaction(struct super_block *s, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1681 | struct reiserfs_journal_list *jl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1683 | struct reiserfs_journal_cnode *cn; |
| 1684 | struct reiserfs_journal_list *pjl; |
| 1685 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1687 | jl->j_state |= LIST_DIRTY; |
| 1688 | cn = jl->j_realblock; |
| 1689 | while (cn) { |
| 1690 | /* look for a more recent transaction that logged this |
| 1691 | ** buffer. Only the most recent transaction with a buffer in |
| 1692 | ** it is allowed to send that buffer to disk |
| 1693 | */ |
| 1694 | pjl = find_newer_jl_for_cn(cn); |
| 1695 | if (!pjl && cn->blocknr && cn->bh |
| 1696 | && buffer_journal_dirty(cn->bh)) { |
| 1697 | BUG_ON(!can_dirty(cn)); |
| 1698 | /* if the buffer is prepared, it will either be logged |
| 1699 | * or restored. If restored, we need to make sure |
| 1700 | * it actually gets marked dirty |
| 1701 | */ |
| 1702 | clear_buffer_journal_new(cn->bh); |
| 1703 | if (buffer_journal_prepared(cn->bh)) { |
| 1704 | set_buffer_journal_restore_dirty(cn->bh); |
| 1705 | } else { |
| 1706 | set_buffer_journal_test(cn->bh); |
| 1707 | mark_buffer_dirty(cn->bh); |
| 1708 | } |
| 1709 | } |
| 1710 | cn = cn->next; |
| 1711 | } |
| 1712 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | static int kupdate_transactions(struct super_block *s, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1716 | struct reiserfs_journal_list *jl, |
| 1717 | struct reiserfs_journal_list **next_jl, |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 1718 | unsigned int *next_trans_id, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1719 | int num_blocks, int num_trans) |
| 1720 | { |
| 1721 | int ret = 0; |
| 1722 | int written = 0; |
| 1723 | int transactions_flushed = 0; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 1724 | unsigned int orig_trans_id = jl->j_trans_id; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1725 | struct buffer_chunk chunk; |
| 1726 | struct list_head *entry; |
| 1727 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 1728 | chunk.nr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
Frederic Weisbecker | a412f9e | 2009-04-14 00:10:35 +0200 | [diff] [blame] | 1730 | reiserfs_mutex_lock_safe(&journal->j_flush_mutex, s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1731 | if (!journal_list_still_alive(s, orig_trans_id)) { |
| 1732 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
Jeff Mahoney | afe7025 | 2008-07-25 01:46:39 -0700 | [diff] [blame] | 1735 | /* we've got j_flush_mutex held, nobody is going to delete any |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1736 | * of these lists out from underneath us |
| 1737 | */ |
| 1738 | while ((num_trans && transactions_flushed < num_trans) || |
| 1739 | (!num_trans && written < num_blocks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1741 | if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) || |
| 1742 | atomic_read(&jl->j_commit_left) |
| 1743 | || !(jl->j_state & LIST_DIRTY)) { |
| 1744 | del_from_work_list(s, jl); |
| 1745 | break; |
| 1746 | } |
| 1747 | ret = write_one_transaction(s, jl, &chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1749 | if (ret < 0) |
| 1750 | goto done; |
| 1751 | transactions_flushed++; |
| 1752 | written += ret; |
| 1753 | entry = jl->j_list.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1755 | /* did we wrap? */ |
| 1756 | if (entry == &journal->j_journal_list) { |
| 1757 | break; |
| 1758 | } |
| 1759 | jl = JOURNAL_LIST_ENTRY(entry); |
| 1760 | |
| 1761 | /* don't bother with older transactions */ |
| 1762 | if (jl->j_trans_id <= orig_trans_id) |
| 1763 | break; |
| 1764 | } |
| 1765 | if (chunk.nr) { |
| 1766 | write_chunk(&chunk); |
| 1767 | } |
| 1768 | |
| 1769 | done: |
Jeff Mahoney | afe7025 | 2008-07-25 01:46:39 -0700 | [diff] [blame] | 1770 | mutex_unlock(&journal->j_flush_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1771 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | /* for o_sync and fsync heavy applications, they tend to use |
| 1775 | ** all the journa list slots with tiny transactions. These |
| 1776 | ** trigger lots and lots of calls to update the header block, which |
| 1777 | ** adds seeks and slows things down. |
| 1778 | ** |
| 1779 | ** This function tries to clear out a large chunk of the journal lists |
| 1780 | ** at once, which makes everything faster since only the newest journal |
| 1781 | ** list updates the header block |
| 1782 | */ |
| 1783 | static int flush_used_journal_lists(struct super_block *s, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1784 | struct reiserfs_journal_list *jl) |
| 1785 | { |
| 1786 | unsigned long len = 0; |
| 1787 | unsigned long cur_len; |
| 1788 | int ret; |
| 1789 | int i; |
| 1790 | int limit = 256; |
| 1791 | struct reiserfs_journal_list *tjl; |
| 1792 | struct reiserfs_journal_list *flush_jl; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 1793 | unsigned int trans_id; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1794 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1796 | flush_jl = tjl = jl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1798 | /* in data logging mode, try harder to flush a lot of blocks */ |
| 1799 | if (reiserfs_data_log(s)) |
| 1800 | limit = 1024; |
| 1801 | /* flush for 256 transactions or limit blocks, whichever comes first */ |
| 1802 | for (i = 0; i < 256 && len < limit; i++) { |
| 1803 | if (atomic_read(&tjl->j_commit_left) || |
| 1804 | tjl->j_trans_id < jl->j_trans_id) { |
| 1805 | break; |
| 1806 | } |
| 1807 | cur_len = atomic_read(&tjl->j_nonzerolen); |
| 1808 | if (cur_len > 0) { |
| 1809 | tjl->j_state &= ~LIST_TOUCHED; |
| 1810 | } |
| 1811 | len += cur_len; |
| 1812 | flush_jl = tjl; |
| 1813 | if (tjl->j_list.next == &journal->j_journal_list) |
| 1814 | break; |
| 1815 | tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1817 | /* try to find a group of blocks we can flush across all the |
| 1818 | ** transactions, but only bother if we've actually spanned |
| 1819 | ** across multiple lists |
| 1820 | */ |
| 1821 | if (flush_jl != jl) { |
| 1822 | ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1824 | flush_journal_list(s, flush_jl, 1); |
| 1825 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | ** removes any nodes in table with name block and dev as bh. |
| 1830 | ** only touchs the hnext and hprev pointers. |
| 1831 | */ |
| 1832 | void remove_journal_hash(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1833 | struct reiserfs_journal_cnode **table, |
| 1834 | struct reiserfs_journal_list *jl, |
| 1835 | unsigned long block, int remove_freed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1837 | struct reiserfs_journal_cnode *cur; |
| 1838 | struct reiserfs_journal_cnode **head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1840 | head = &(journal_hash(table, sb, block)); |
| 1841 | if (!head) { |
| 1842 | return; |
| 1843 | } |
| 1844 | cur = *head; |
| 1845 | while (cur) { |
| 1846 | if (cur->blocknr == block && cur->sb == sb |
| 1847 | && (jl == NULL || jl == cur->jlist) |
| 1848 | && (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) { |
| 1849 | if (cur->hnext) { |
| 1850 | cur->hnext->hprev = cur->hprev; |
| 1851 | } |
| 1852 | if (cur->hprev) { |
| 1853 | cur->hprev->hnext = cur->hnext; |
| 1854 | } else { |
| 1855 | *head = cur->hnext; |
| 1856 | } |
| 1857 | cur->blocknr = 0; |
| 1858 | cur->sb = NULL; |
| 1859 | cur->state = 0; |
| 1860 | if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */ |
| 1861 | atomic_dec(&(cur->jlist->j_nonzerolen)); |
| 1862 | cur->bh = NULL; |
| 1863 | cur->jlist = NULL; |
| 1864 | } |
| 1865 | cur = cur->hnext; |
| 1866 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1869 | static void free_journal_ram(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1870 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1871 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 1872 | kfree(journal->j_current_jl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1873 | journal->j_num_lists--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1875 | vfree(journal->j_cnode_free_orig); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1876 | free_list_bitmaps(sb, journal->j_list_bitmap); |
| 1877 | free_bitmap_nodes(sb); /* must be after free_list_bitmaps */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1878 | if (journal->j_header_bh) { |
| 1879 | brelse(journal->j_header_bh); |
| 1880 | } |
| 1881 | /* j_header_bh is on the journal dev, make sure not to release the journal |
| 1882 | * dev until we brelse j_header_bh |
| 1883 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1884 | release_journal_dev(sb, journal); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1885 | vfree(journal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | ** call on unmount. Only set error to 1 if you haven't made your way out |
| 1890 | ** of read_super() yet. Any other caller must keep error at 0. |
| 1891 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1892 | static int do_journal_release(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1893 | struct super_block *sb, int error) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1894 | { |
| 1895 | struct reiserfs_transaction_handle myth; |
| 1896 | int flushed = 0; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1897 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1899 | /* we only want to flush out transactions if we were called with error == 0 |
| 1900 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1901 | if (!error && !(sb->s_flags & MS_RDONLY)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1902 | /* end the current trans */ |
| 1903 | BUG_ON(!th->t_trans_id); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1904 | do_journal_end(th, sb, 10, FLUSH_ALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1906 | /* make sure something gets logged to force our way into the flush code */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1907 | if (!journal_join(&myth, sb, 1)) { |
| 1908 | reiserfs_prepare_for_journal(sb, |
| 1909 | SB_BUFFER_WITH_SB(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1910 | 1); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1911 | journal_mark_dirty(&myth, sb, |
| 1912 | SB_BUFFER_WITH_SB(sb)); |
| 1913 | do_journal_end(&myth, sb, 1, FLUSH_ALL); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1914 | flushed = 1; |
| 1915 | } |
| 1916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1918 | /* this also catches errors during the do_journal_end above */ |
| 1919 | if (!error && reiserfs_is_journal_aborted(journal)) { |
| 1920 | memset(&myth, 0, sizeof(myth)); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1921 | if (!journal_join_abort(&myth, sb, 1)) { |
| 1922 | reiserfs_prepare_for_journal(sb, |
| 1923 | SB_BUFFER_WITH_SB(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1924 | 1); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1925 | journal_mark_dirty(&myth, sb, |
| 1926 | SB_BUFFER_WITH_SB(sb)); |
| 1927 | do_journal_end(&myth, sb, 1, FLUSH_ALL); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1931 | reiserfs_mounted_fs_count--; |
| 1932 | /* wait for all commits to finish */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1933 | cancel_delayed_work(&SB_JOURNAL(sb)->j_work); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1934 | |
| 1935 | /* |
| 1936 | * We must release the write lock here because |
| 1937 | * the workqueue job (flush_async_commit) needs this lock |
| 1938 | */ |
| 1939 | reiserfs_write_unlock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1940 | flush_workqueue(commit_wq); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 1941 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1942 | if (!reiserfs_mounted_fs_count) { |
| 1943 | destroy_workqueue(commit_wq); |
| 1944 | commit_wq = NULL; |
| 1945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1947 | free_journal_ram(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | |
Frederic Weisbecker | 0523676 | 2009-12-30 05:56:08 +0100 | [diff] [blame] | 1949 | reiserfs_write_lock(sb); |
| 1950 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1951 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | ** call on unmount. flush all journal trans, release all alloc'd ram |
| 1956 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1957 | int journal_release(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1958 | struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1959 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1960 | return do_journal_release(th, sb, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | /* |
| 1964 | ** only call from an error condition inside reiserfs_read_super! |
| 1965 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1966 | int journal_release_error(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1967 | struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1968 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1969 | return do_journal_release(th, sb, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /* compares description block with commit block. returns 1 if they differ, 0 if they are the same */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1973 | static int journal_compare_desc_commit(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1974 | struct reiserfs_journal_desc *desc, |
| 1975 | struct reiserfs_journal_commit *commit) |
| 1976 | { |
| 1977 | if (get_commit_trans_id(commit) != get_desc_trans_id(desc) || |
| 1978 | get_commit_trans_len(commit) != get_desc_trans_len(desc) || |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1979 | get_commit_trans_len(commit) > SB_JOURNAL(sb)->j_trans_max || |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1980 | get_commit_trans_len(commit) <= 0) { |
| 1981 | return 1; |
| 1982 | } |
| 1983 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1985 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1986 | /* returns 0 if it did not find a description block |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | ** returns -1 if it found a corrupt commit block |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 1988 | ** returns 1 if both desc and commit were valid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 1990 | static int journal_transaction_is_valid(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1991 | struct buffer_head *d_bh, |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 1992 | unsigned int *oldest_invalid_trans_id, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1993 | unsigned long *newest_mount_id) |
| 1994 | { |
| 1995 | struct reiserfs_journal_desc *desc; |
| 1996 | struct reiserfs_journal_commit *commit; |
| 1997 | struct buffer_head *c_bh; |
| 1998 | unsigned long offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2000 | if (!d_bh) |
| 2001 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2003 | desc = (struct reiserfs_journal_desc *)d_bh->b_data; |
| 2004 | if (get_desc_trans_len(desc) > 0 |
| 2005 | && !memcmp(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8)) { |
| 2006 | if (oldest_invalid_trans_id && *oldest_invalid_trans_id |
| 2007 | && get_desc_trans_id(desc) > *oldest_invalid_trans_id) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2008 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2009 | "journal-986: transaction " |
| 2010 | "is valid returning because trans_id %d is greater than " |
| 2011 | "oldest_invalid %lu", |
| 2012 | get_desc_trans_id(desc), |
| 2013 | *oldest_invalid_trans_id); |
| 2014 | return 0; |
| 2015 | } |
| 2016 | if (newest_mount_id |
| 2017 | && *newest_mount_id > get_desc_mount_id(desc)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2018 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2019 | "journal-1087: transaction " |
| 2020 | "is valid returning because mount_id %d is less than " |
| 2021 | "newest_mount_id %lu", |
| 2022 | get_desc_mount_id(desc), |
| 2023 | *newest_mount_id); |
| 2024 | return -1; |
| 2025 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2026 | if (get_desc_trans_len(desc) > SB_JOURNAL(sb)->j_trans_max) { |
| 2027 | reiserfs_warning(sb, "journal-2018", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2028 | "Bad transaction length %d " |
| 2029 | "encountered, ignoring transaction", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2030 | get_desc_trans_len(desc)); |
| 2031 | return -1; |
| 2032 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2033 | offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2035 | /* ok, we have a journal description block, lets see if the transaction was valid */ |
| 2036 | c_bh = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2037 | journal_bread(sb, |
| 2038 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2039 | ((offset + get_desc_trans_len(desc) + |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2040 | 1) % SB_ONDISK_JOURNAL_SIZE(sb))); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2041 | if (!c_bh) |
| 2042 | return 0; |
| 2043 | commit = (struct reiserfs_journal_commit *)c_bh->b_data; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2044 | if (journal_compare_desc_commit(sb, desc, commit)) { |
| 2045 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2046 | "journal_transaction_is_valid, commit offset %ld had bad " |
| 2047 | "time %d or length %d", |
| 2048 | c_bh->b_blocknr - |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2049 | SB_ONDISK_JOURNAL_1st_BLOCK(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2050 | get_commit_trans_id(commit), |
| 2051 | get_commit_trans_len(commit)); |
| 2052 | brelse(c_bh); |
| 2053 | if (oldest_invalid_trans_id) { |
| 2054 | *oldest_invalid_trans_id = |
| 2055 | get_desc_trans_id(desc); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2056 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2057 | "journal-1004: " |
| 2058 | "transaction_is_valid setting oldest invalid trans_id " |
| 2059 | "to %d", |
| 2060 | get_desc_trans_id(desc)); |
| 2061 | } |
| 2062 | return -1; |
| 2063 | } |
| 2064 | brelse(c_bh); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2065 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2066 | "journal-1006: found valid " |
| 2067 | "transaction start offset %llu, len %d id %d", |
| 2068 | d_bh->b_blocknr - |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2069 | SB_ONDISK_JOURNAL_1st_BLOCK(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2070 | get_desc_trans_len(desc), |
| 2071 | get_desc_trans_id(desc)); |
| 2072 | return 1; |
| 2073 | } else { |
| 2074 | return 0; |
| 2075 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2078 | static void brelse_array(struct buffer_head **heads, int num) |
| 2079 | { |
| 2080 | int i; |
| 2081 | for (i = 0; i < num; i++) { |
| 2082 | brelse(heads[i]); |
| 2083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /* |
| 2087 | ** given the start, and values for the oldest acceptable transactions, |
| 2088 | ** this either reads in a replays a transaction, or returns because the transaction |
| 2089 | ** is invalid, or too old. |
| 2090 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2091 | static int journal_read_transaction(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2092 | unsigned long cur_dblock, |
| 2093 | unsigned long oldest_start, |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 2094 | unsigned int oldest_trans_id, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2095 | unsigned long newest_mount_id) |
| 2096 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2097 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2098 | struct reiserfs_journal_desc *desc; |
| 2099 | struct reiserfs_journal_commit *commit; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 2100 | unsigned int trans_id = 0; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2101 | struct buffer_head *c_bh; |
| 2102 | struct buffer_head *d_bh; |
| 2103 | struct buffer_head **log_blocks = NULL; |
| 2104 | struct buffer_head **real_blocks = NULL; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 2105 | unsigned int trans_offset; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2106 | int i; |
| 2107 | int trans_half; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2109 | d_bh = journal_bread(sb, cur_dblock); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2110 | if (!d_bh) |
| 2111 | return 1; |
| 2112 | desc = (struct reiserfs_journal_desc *)d_bh->b_data; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2113 | trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb); |
| 2114 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1037: " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2115 | "journal_read_transaction, offset %llu, len %d mount_id %d", |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2116 | d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2117 | get_desc_trans_len(desc), get_desc_mount_id(desc)); |
| 2118 | if (get_desc_trans_id(desc) < oldest_trans_id) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2119 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1039: " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2120 | "journal_read_trans skipping because %lu is too old", |
| 2121 | cur_dblock - |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2122 | SB_ONDISK_JOURNAL_1st_BLOCK(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2123 | brelse(d_bh); |
| 2124 | return 1; |
| 2125 | } |
| 2126 | if (get_desc_mount_id(desc) != newest_mount_id) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2127 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1146: " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2128 | "journal_read_trans skipping because %d is != " |
| 2129 | "newest_mount_id %lu", get_desc_mount_id(desc), |
| 2130 | newest_mount_id); |
| 2131 | brelse(d_bh); |
| 2132 | return 1; |
| 2133 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2134 | c_bh = journal_bread(sb, SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2135 | ((trans_offset + get_desc_trans_len(desc) + 1) % |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2136 | SB_ONDISK_JOURNAL_SIZE(sb))); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2137 | if (!c_bh) { |
| 2138 | brelse(d_bh); |
| 2139 | return 1; |
| 2140 | } |
| 2141 | commit = (struct reiserfs_journal_commit *)c_bh->b_data; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2142 | if (journal_compare_desc_commit(sb, desc, commit)) { |
| 2143 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2144 | "journal_read_transaction, " |
| 2145 | "commit offset %llu had bad time %d or length %d", |
| 2146 | c_bh->b_blocknr - |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2147 | SB_ONDISK_JOURNAL_1st_BLOCK(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2148 | get_commit_trans_id(commit), |
| 2149 | get_commit_trans_len(commit)); |
| 2150 | brelse(c_bh); |
| 2151 | brelse(d_bh); |
| 2152 | return 1; |
| 2153 | } |
Jeff Mahoney | 3f8b5ee | 2010-03-23 13:35:39 -0700 | [diff] [blame] | 2154 | |
| 2155 | if (bdev_read_only(sb->s_bdev)) { |
| 2156 | reiserfs_warning(sb, "clm-2076", |
| 2157 | "device is readonly, unable to replay log"); |
| 2158 | brelse(c_bh); |
| 2159 | brelse(d_bh); |
| 2160 | return -EROFS; |
| 2161 | } |
| 2162 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2163 | trans_id = get_desc_trans_id(desc); |
| 2164 | /* now we know we've got a good transaction, and it was inside the valid time ranges */ |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 2165 | log_blocks = kmalloc(get_desc_trans_len(desc) * |
| 2166 | sizeof(struct buffer_head *), GFP_NOFS); |
| 2167 | real_blocks = kmalloc(get_desc_trans_len(desc) * |
| 2168 | sizeof(struct buffer_head *), GFP_NOFS); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2169 | if (!log_blocks || !real_blocks) { |
| 2170 | brelse(c_bh); |
| 2171 | brelse(d_bh); |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 2172 | kfree(log_blocks); |
| 2173 | kfree(real_blocks); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2174 | reiserfs_warning(sb, "journal-1169", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2175 | "kmalloc failed, unable to mount FS"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2176 | return -1; |
| 2177 | } |
| 2178 | /* get all the buffer heads */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2179 | trans_half = journal_trans_half(sb->s_blocksize); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2180 | for (i = 0; i < get_desc_trans_len(desc); i++) { |
| 2181 | log_blocks[i] = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2182 | journal_getblk(sb, |
| 2183 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2184 | (trans_offset + 1 + |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2185 | i) % SB_ONDISK_JOURNAL_SIZE(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2186 | if (i < trans_half) { |
| 2187 | real_blocks[i] = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2188 | sb_getblk(sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2189 | le32_to_cpu(desc->j_realblock[i])); |
| 2190 | } else { |
| 2191 | real_blocks[i] = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2192 | sb_getblk(sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2193 | le32_to_cpu(commit-> |
| 2194 | j_realblock[i - trans_half])); |
| 2195 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2196 | if (real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(sb)) { |
| 2197 | reiserfs_warning(sb, "journal-1207", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2198 | "REPLAY FAILURE fsck required! " |
| 2199 | "Block to replay is outside of " |
| 2200 | "filesystem"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2201 | goto abort_replay; |
| 2202 | } |
| 2203 | /* make sure we don't try to replay onto log or reserved area */ |
| 2204 | if (is_block_in_log_or_reserved_area |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2205 | (sb, real_blocks[i]->b_blocknr)) { |
| 2206 | reiserfs_warning(sb, "journal-1204", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2207 | "REPLAY FAILURE fsck required! " |
| 2208 | "Trying to replay onto a log block"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2209 | abort_replay: |
| 2210 | brelse_array(log_blocks, i); |
| 2211 | brelse_array(real_blocks, i); |
| 2212 | brelse(c_bh); |
| 2213 | brelse(d_bh); |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 2214 | kfree(log_blocks); |
| 2215 | kfree(real_blocks); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2216 | return -1; |
| 2217 | } |
| 2218 | } |
| 2219 | /* read in the log blocks, memcpy to the corresponding real block */ |
| 2220 | ll_rw_block(READ, get_desc_trans_len(desc), log_blocks); |
| 2221 | for (i = 0; i < get_desc_trans_len(desc); i++) { |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 2222 | |
| 2223 | reiserfs_write_unlock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2224 | wait_on_buffer(log_blocks[i]); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 2225 | reiserfs_write_lock(sb); |
| 2226 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2227 | if (!buffer_uptodate(log_blocks[i])) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2228 | reiserfs_warning(sb, "journal-1212", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2229 | "REPLAY FAILURE fsck required! " |
| 2230 | "buffer write failed"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2231 | brelse_array(log_blocks + i, |
| 2232 | get_desc_trans_len(desc) - i); |
| 2233 | brelse_array(real_blocks, get_desc_trans_len(desc)); |
| 2234 | brelse(c_bh); |
| 2235 | brelse(d_bh); |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 2236 | kfree(log_blocks); |
| 2237 | kfree(real_blocks); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2238 | return -1; |
| 2239 | } |
| 2240 | memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data, |
| 2241 | real_blocks[i]->b_size); |
| 2242 | set_buffer_uptodate(real_blocks[i]); |
| 2243 | brelse(log_blocks[i]); |
| 2244 | } |
| 2245 | /* flush out the real blocks */ |
| 2246 | for (i = 0; i < get_desc_trans_len(desc); i++) { |
| 2247 | set_buffer_dirty(real_blocks[i]); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 2248 | write_dirty_buffer(real_blocks[i], WRITE); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2249 | } |
| 2250 | for (i = 0; i < get_desc_trans_len(desc); i++) { |
| 2251 | wait_on_buffer(real_blocks[i]); |
| 2252 | if (!buffer_uptodate(real_blocks[i])) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2253 | reiserfs_warning(sb, "journal-1226", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2254 | "REPLAY FAILURE, fsck required! " |
| 2255 | "buffer write failed"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2256 | brelse_array(real_blocks + i, |
| 2257 | get_desc_trans_len(desc) - i); |
| 2258 | brelse(c_bh); |
| 2259 | brelse(d_bh); |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 2260 | kfree(log_blocks); |
| 2261 | kfree(real_blocks); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2262 | return -1; |
| 2263 | } |
| 2264 | brelse(real_blocks[i]); |
| 2265 | } |
| 2266 | cur_dblock = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2267 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2268 | ((trans_offset + get_desc_trans_len(desc) + |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2269 | 2) % SB_ONDISK_JOURNAL_SIZE(sb)); |
| 2270 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2271 | "journal-1095: setting journal " "start to offset %ld", |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2272 | cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2273 | |
| 2274 | /* init starting values for the first transaction, in case this is the last transaction to be replayed. */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2275 | journal->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2276 | journal->j_last_flush_trans_id = trans_id; |
| 2277 | journal->j_trans_id = trans_id + 1; |
Alexander Zarochentsev | a44c94a | 2006-03-25 03:06:59 -0800 | [diff] [blame] | 2278 | /* check for trans_id overflow */ |
| 2279 | if (journal->j_trans_id == 0) |
| 2280 | journal->j_trans_id = 10; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2281 | brelse(c_bh); |
| 2282 | brelse(d_bh); |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 2283 | kfree(log_blocks); |
| 2284 | kfree(real_blocks); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2285 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | /* This function reads blocks starting from block and to max_block of bufsize |
| 2289 | size (but no more than BUFNR blocks at a time). This proved to improve |
| 2290 | mounting speed on self-rebuilding raid5 arrays at least. |
| 2291 | Right now it is only used from journal code. But later we might use it |
| 2292 | from other places. |
| 2293 | Note: Do not use journal_getblk/sb_getblk functions here! */ |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 2294 | static struct buffer_head *reiserfs_breada(struct block_device *dev, |
| 2295 | b_blocknr_t block, int bufsize, |
| 2296 | b_blocknr_t max_block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2298 | struct buffer_head *bhlist[BUFNR]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | unsigned int blocks = BUFNR; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2300 | struct buffer_head *bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | int i, j; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2302 | |
| 2303 | bh = __getblk(dev, block, bufsize); |
| 2304 | if (buffer_uptodate(bh)) |
| 2305 | return (bh); |
| 2306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | if (block + BUFNR > max_block) { |
| 2308 | blocks = max_block - block; |
| 2309 | } |
| 2310 | bhlist[0] = bh; |
| 2311 | j = 1; |
| 2312 | for (i = 1; i < blocks; i++) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2313 | bh = __getblk(dev, block + i, bufsize); |
| 2314 | if (buffer_uptodate(bh)) { |
| 2315 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | break; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2317 | } else |
| 2318 | bhlist[j++] = bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2320 | ll_rw_block(READ, j, bhlist); |
| 2321 | for (i = 1; i < j; i++) |
| 2322 | brelse(bhlist[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | bh = bhlist[0]; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2324 | wait_on_buffer(bh); |
| 2325 | if (buffer_uptodate(bh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | return bh; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2327 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | return NULL; |
| 2329 | } |
| 2330 | |
| 2331 | /* |
| 2332 | ** read and replay the log |
| 2333 | ** on a clean unmount, the journal header's next unflushed pointer will be to an invalid |
| 2334 | ** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast. |
| 2335 | ** |
| 2336 | ** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid. |
| 2337 | ** |
| 2338 | ** On exit, it sets things up so the first transaction will work correctly. |
| 2339 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2340 | static int journal_read(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2341 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2342 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2343 | struct reiserfs_journal_desc *desc; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 2344 | unsigned int oldest_trans_id = 0; |
| 2345 | unsigned int oldest_invalid_trans_id = 0; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2346 | time_t start; |
| 2347 | unsigned long oldest_start = 0; |
| 2348 | unsigned long cur_dblock = 0; |
| 2349 | unsigned long newest_mount_id = 9; |
| 2350 | struct buffer_head *d_bh; |
| 2351 | struct reiserfs_journal_header *jh; |
| 2352 | int valid_journal_header = 0; |
| 2353 | int replay_count = 0; |
| 2354 | int continue_replay = 1; |
| 2355 | int ret; |
| 2356 | char b[BDEVNAME_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2358 | cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(sb); |
| 2359 | reiserfs_info(sb, "checking transaction log (%s)\n", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2360 | bdevname(journal->j_dev_bd, b)); |
| 2361 | start = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 2363 | /* step 1, read in the journal header block. Check the transaction it says |
| 2364 | ** is the first unflushed, and if that transaction is not valid, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2365 | ** replay is done |
| 2366 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2367 | journal->j_header_bh = journal_bread(sb, |
| 2368 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) |
| 2369 | + SB_ONDISK_JOURNAL_SIZE(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2370 | if (!journal->j_header_bh) { |
| 2371 | return 1; |
| 2372 | } |
| 2373 | jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data); |
Vladimir V. Saveliev | c499ec2 | 2006-03-02 02:54:39 -0800 | [diff] [blame] | 2374 | if (le32_to_cpu(jh->j_first_unflushed_offset) < |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2375 | SB_ONDISK_JOURNAL_SIZE(sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2376 | && le32_to_cpu(jh->j_last_flush_trans_id) > 0) { |
| 2377 | oldest_start = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2378 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2379 | le32_to_cpu(jh->j_first_unflushed_offset); |
| 2380 | oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1; |
| 2381 | newest_mount_id = le32_to_cpu(jh->j_mount_id); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2382 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2383 | "journal-1153: found in " |
| 2384 | "header: first_unflushed_offset %d, last_flushed_trans_id " |
| 2385 | "%lu", le32_to_cpu(jh->j_first_unflushed_offset), |
| 2386 | le32_to_cpu(jh->j_last_flush_trans_id)); |
| 2387 | valid_journal_header = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 2389 | /* now, we try to read the first unflushed offset. If it is not valid, |
| 2390 | ** there is nothing more we can do, and it makes no sense to read |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2391 | ** through the whole log. |
| 2392 | */ |
| 2393 | d_bh = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2394 | journal_bread(sb, |
| 2395 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2396 | le32_to_cpu(jh->j_first_unflushed_offset)); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2397 | ret = journal_transaction_is_valid(sb, d_bh, NULL, NULL); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2398 | if (!ret) { |
| 2399 | continue_replay = 0; |
| 2400 | } |
| 2401 | brelse(d_bh); |
| 2402 | goto start_log_replay; |
| 2403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2405 | /* ok, there are transactions that need to be replayed. start with the first log block, find |
| 2406 | ** all the valid transactions, and pick out the oldest. |
| 2407 | */ |
| 2408 | while (continue_replay |
| 2409 | && cur_dblock < |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2410 | (SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
| 2411 | SB_ONDISK_JOURNAL_SIZE(sb))) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2412 | /* Note that it is required for blocksize of primary fs device and journal |
| 2413 | device to be the same */ |
| 2414 | d_bh = |
| 2415 | reiserfs_breada(journal->j_dev_bd, cur_dblock, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2416 | sb->s_blocksize, |
| 2417 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
| 2418 | SB_ONDISK_JOURNAL_SIZE(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2419 | ret = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2420 | journal_transaction_is_valid(sb, d_bh, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2421 | &oldest_invalid_trans_id, |
| 2422 | &newest_mount_id); |
| 2423 | if (ret == 1) { |
| 2424 | desc = (struct reiserfs_journal_desc *)d_bh->b_data; |
| 2425 | if (oldest_start == 0) { /* init all oldest_ values */ |
| 2426 | oldest_trans_id = get_desc_trans_id(desc); |
| 2427 | oldest_start = d_bh->b_blocknr; |
| 2428 | newest_mount_id = get_desc_mount_id(desc); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2429 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2430 | "journal-1179: Setting " |
| 2431 | "oldest_start to offset %llu, trans_id %lu", |
| 2432 | oldest_start - |
| 2433 | SB_ONDISK_JOURNAL_1st_BLOCK |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2434 | (sb), oldest_trans_id); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2435 | } else if (oldest_trans_id > get_desc_trans_id(desc)) { |
| 2436 | /* one we just read was older */ |
| 2437 | oldest_trans_id = get_desc_trans_id(desc); |
| 2438 | oldest_start = d_bh->b_blocknr; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2439 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2440 | "journal-1180: Resetting " |
| 2441 | "oldest_start to offset %lu, trans_id %lu", |
| 2442 | oldest_start - |
| 2443 | SB_ONDISK_JOURNAL_1st_BLOCK |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2444 | (sb), oldest_trans_id); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2445 | } |
| 2446 | if (newest_mount_id < get_desc_mount_id(desc)) { |
| 2447 | newest_mount_id = get_desc_mount_id(desc); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2448 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2449 | "journal-1299: Setting " |
| 2450 | "newest_mount_id to %d", |
| 2451 | get_desc_mount_id(desc)); |
| 2452 | } |
| 2453 | cur_dblock += get_desc_trans_len(desc) + 2; |
| 2454 | } else { |
| 2455 | cur_dblock++; |
| 2456 | } |
| 2457 | brelse(d_bh); |
| 2458 | } |
| 2459 | |
| 2460 | start_log_replay: |
| 2461 | cur_dblock = oldest_start; |
| 2462 | if (oldest_trans_id) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2463 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2464 | "journal-1206: Starting replay " |
| 2465 | "from offset %llu, trans_id %lu", |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2466 | cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2467 | oldest_trans_id); |
| 2468 | |
| 2469 | } |
| 2470 | replay_count = 0; |
| 2471 | while (continue_replay && oldest_trans_id > 0) { |
| 2472 | ret = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2473 | journal_read_transaction(sb, cur_dblock, oldest_start, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2474 | oldest_trans_id, newest_mount_id); |
| 2475 | if (ret < 0) { |
| 2476 | return ret; |
| 2477 | } else if (ret != 0) { |
| 2478 | break; |
| 2479 | } |
| 2480 | cur_dblock = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2481 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + journal->j_start; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2482 | replay_count++; |
| 2483 | if (cur_dblock == oldest_start) |
| 2484 | break; |
| 2485 | } |
| 2486 | |
| 2487 | if (oldest_trans_id == 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2488 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2489 | "journal-1225: No valid " "transactions found"); |
| 2490 | } |
| 2491 | /* j_start does not get set correctly if we don't replay any transactions. |
| 2492 | ** if we had a valid journal_header, set j_start to the first unflushed transaction value, |
| 2493 | ** copy the trans_id from the header |
| 2494 | */ |
| 2495 | if (valid_journal_header && replay_count == 0) { |
| 2496 | journal->j_start = le32_to_cpu(jh->j_first_unflushed_offset); |
| 2497 | journal->j_trans_id = |
| 2498 | le32_to_cpu(jh->j_last_flush_trans_id) + 1; |
Alexander Zarochentsev | a44c94a | 2006-03-25 03:06:59 -0800 | [diff] [blame] | 2499 | /* check for trans_id overflow */ |
| 2500 | if (journal->j_trans_id == 0) |
| 2501 | journal->j_trans_id = 10; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2502 | journal->j_last_flush_trans_id = |
| 2503 | le32_to_cpu(jh->j_last_flush_trans_id); |
| 2504 | journal->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1; |
| 2505 | } else { |
| 2506 | journal->j_mount_id = newest_mount_id + 1; |
| 2507 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2508 | reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1299: Setting " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2509 | "newest_mount_id to %lu", journal->j_mount_id); |
| 2510 | journal->j_first_unflushed_offset = journal->j_start; |
| 2511 | if (replay_count > 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2512 | reiserfs_info(sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2513 | "replayed %d transactions in %lu seconds\n", |
| 2514 | replay_count, get_seconds() - start); |
| 2515 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2516 | if (!bdev_read_only(sb->s_bdev) && |
| 2517 | _update_journal_header_block(sb, journal->j_start, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2518 | journal->j_last_flush_trans_id)) { |
| 2519 | /* replay failed, caller must call free_journal_ram and abort |
| 2520 | ** the mount |
| 2521 | */ |
| 2522 | return -1; |
| 2523 | } |
| 2524 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s) |
| 2528 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2529 | struct reiserfs_journal_list *jl; |
Pekka Enberg | 8c777cc | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 2530 | jl = kzalloc(sizeof(struct reiserfs_journal_list), |
| 2531 | GFP_NOFS | __GFP_NOFAIL); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2532 | INIT_LIST_HEAD(&jl->j_list); |
| 2533 | INIT_LIST_HEAD(&jl->j_working_list); |
| 2534 | INIT_LIST_HEAD(&jl->j_tail_bh_list); |
| 2535 | INIT_LIST_HEAD(&jl->j_bh_list); |
Jeff Mahoney | 90415de | 2008-07-25 01:46:40 -0700 | [diff] [blame] | 2536 | mutex_init(&jl->j_commit_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2537 | SB_JOURNAL(s)->j_num_lists++; |
| 2538 | get_journal_list(jl); |
| 2539 | return jl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2542 | static void journal_list_init(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2544 | SB_JOURNAL(sb)->j_current_jl = alloc_journal_list(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | } |
| 2546 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2547 | static int release_journal_dev(struct super_block *super, |
| 2548 | struct reiserfs_journal *journal) |
| 2549 | { |
| 2550 | int result; |
| 2551 | |
| 2552 | result = 0; |
| 2553 | |
Christoph Hellwig | 86098fa | 2008-04-30 00:54:46 -0700 | [diff] [blame] | 2554 | if (journal->j_dev_bd != NULL) { |
Al Viro | e5eb8ca | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 2555 | result = blkdev_put(journal->j_dev_bd, journal->j_dev_mode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2556 | journal->j_dev_bd = NULL; |
| 2557 | } |
| 2558 | |
| 2559 | if (result != 0) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2560 | reiserfs_warning(super, "sh-457", |
| 2561 | "Cannot release journal device: %i", result); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2562 | } |
| 2563 | return result; |
| 2564 | } |
| 2565 | |
| 2566 | static int journal_init_dev(struct super_block *super, |
| 2567 | struct reiserfs_journal *journal, |
| 2568 | const char *jdev_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | { |
| 2570 | int result; |
| 2571 | dev_t jdev; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2572 | fmode_t blkdev_mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | char b[BDEVNAME_SIZE]; |
| 2574 | |
| 2575 | result = 0; |
| 2576 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2577 | journal->j_dev_bd = NULL; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2578 | jdev = SB_ONDISK_JOURNAL_DEVICE(super) ? |
| 2579 | new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | |
| 2581 | if (bdev_read_only(super->s_bdev)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2582 | blkdev_mode = FMODE_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | |
| 2584 | /* there is no "jdev" option and journal is on separate device */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2585 | if ((!jdev_name || !jdev_name[0])) { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2586 | if (jdev == super->s_dev) |
| 2587 | blkdev_mode &= ~FMODE_EXCL; |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame^] | 2588 | journal->j_dev_bd = blkdev_get_by_dev(jdev, blkdev_mode, |
| 2589 | journal); |
Al Viro | e5eb8ca | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 2590 | journal->j_dev_mode = blkdev_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | if (IS_ERR(journal->j_dev_bd)) { |
| 2592 | result = PTR_ERR(journal->j_dev_bd); |
| 2593 | journal->j_dev_bd = NULL; |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2594 | reiserfs_warning(super, "sh-458", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2595 | "cannot init journal device '%s': %i", |
| 2596 | __bdevname(jdev, b), result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | return result; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2598 | } else if (jdev != super->s_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | set_blocksize(journal->j_dev_bd, super->s_blocksize); |
Christoph Hellwig | 86098fa | 2008-04-30 00:54:46 -0700 | [diff] [blame] | 2600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | return 0; |
| 2602 | } |
| 2603 | |
Al Viro | e5eb8ca | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 2604 | journal->j_dev_mode = blkdev_mode; |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame^] | 2605 | journal->j_dev_bd = blkdev_get_by_path(jdev_name, blkdev_mode, journal); |
Christoph Hellwig | 86098fa | 2008-04-30 00:54:46 -0700 | [diff] [blame] | 2606 | if (IS_ERR(journal->j_dev_bd)) { |
| 2607 | result = PTR_ERR(journal->j_dev_bd); |
| 2608 | journal->j_dev_bd = NULL; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2609 | reiserfs_warning(super, |
| 2610 | "journal_init_dev: Cannot open '%s': %i", |
| 2611 | jdev_name, result); |
Christoph Hellwig | 86098fa | 2008-04-30 00:54:46 -0700 | [diff] [blame] | 2612 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } |
Christoph Hellwig | 86098fa | 2008-04-30 00:54:46 -0700 | [diff] [blame] | 2614 | |
| 2615 | set_blocksize(journal->j_dev_bd, super->s_blocksize); |
| 2616 | reiserfs_info(super, |
| 2617 | "journal_init_dev: journal device: %s\n", |
| 2618 | bdevname(journal->j_dev_bd, b)); |
| 2619 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2622 | /** |
| 2623 | * When creating/tuning a file system user can assign some |
| 2624 | * journal params within boundaries which depend on the ratio |
| 2625 | * blocksize/standard_blocksize. |
| 2626 | * |
| 2627 | * For blocks >= standard_blocksize transaction size should |
| 2628 | * be not less then JOURNAL_TRANS_MIN_DEFAULT, and not more |
| 2629 | * then JOURNAL_TRANS_MAX_DEFAULT. |
| 2630 | * |
| 2631 | * For blocks < standard_blocksize these boundaries should be |
| 2632 | * decreased proportionally. |
| 2633 | */ |
| 2634 | #define REISERFS_STANDARD_BLKSIZE (4096) |
| 2635 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2636 | static int check_advise_trans_params(struct super_block *sb, |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2637 | struct reiserfs_journal *journal) |
| 2638 | { |
| 2639 | if (journal->j_trans_max) { |
| 2640 | /* Non-default journal params. |
| 2641 | Do sanity check for them. */ |
| 2642 | int ratio = 1; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2643 | if (sb->s_blocksize < REISERFS_STANDARD_BLKSIZE) |
| 2644 | ratio = REISERFS_STANDARD_BLKSIZE / sb->s_blocksize; |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2645 | |
| 2646 | if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio || |
| 2647 | journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio || |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2648 | SB_ONDISK_JOURNAL_SIZE(sb) / journal->j_trans_max < |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2649 | JOURNAL_MIN_RATIO) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2650 | reiserfs_warning(sb, "sh-462", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2651 | "bad transaction max size (%u). " |
| 2652 | "FSCK?", journal->j_trans_max); |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2653 | return 1; |
| 2654 | } |
| 2655 | if (journal->j_max_batch != (journal->j_trans_max) * |
| 2656 | JOURNAL_MAX_BATCH_DEFAULT/JOURNAL_TRANS_MAX_DEFAULT) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2657 | reiserfs_warning(sb, "sh-463", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2658 | "bad transaction max batch (%u). " |
| 2659 | "FSCK?", journal->j_max_batch); |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2660 | return 1; |
| 2661 | } |
| 2662 | } else { |
| 2663 | /* Default journal params. |
| 2664 | The file system was created by old version |
| 2665 | of mkreiserfs, so some fields contain zeros, |
| 2666 | and we need to advise proper values for them */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2667 | if (sb->s_blocksize != REISERFS_STANDARD_BLKSIZE) { |
| 2668 | reiserfs_warning(sb, "sh-464", "bad blocksize (%u)", |
| 2669 | sb->s_blocksize); |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2670 | return 1; |
| 2671 | } |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2672 | journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT; |
| 2673 | journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT; |
| 2674 | journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE; |
| 2675 | } |
| 2676 | return 0; |
| 2677 | } |
| 2678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | /* |
| 2680 | ** must be called once on fs mount. calls journal_read for you |
| 2681 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2682 | int journal_init(struct super_block *sb, const char *j_dev_name, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2683 | int old_format, unsigned int commit_max_age) |
| 2684 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2685 | int num_cnodes = SB_ONDISK_JOURNAL_SIZE(sb) * 2; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2686 | struct buffer_head *bhjh; |
| 2687 | struct reiserfs_super_block *rs; |
| 2688 | struct reiserfs_journal_header *jh; |
| 2689 | struct reiserfs_journal *journal; |
| 2690 | struct reiserfs_journal_list *jl; |
| 2691 | char b[BDEVNAME_SIZE]; |
Frederic Weisbecker | 98ea3f5 | 2009-12-29 21:51:15 +0100 | [diff] [blame] | 2692 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | |
Frederic Weisbecker | 98ea3f5 | 2009-12-29 21:51:15 +0100 | [diff] [blame] | 2694 | /* |
| 2695 | * Unlock here to avoid various RECLAIM-FS-ON <-> IN-RECLAIM-FS |
| 2696 | * dependency inversion warnings. |
| 2697 | */ |
| 2698 | reiserfs_write_unlock(sb); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2699 | journal = SB_JOURNAL(sb) = vmalloc(sizeof(struct reiserfs_journal)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2700 | if (!journal) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2701 | reiserfs_warning(sb, "journal-1256", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2702 | "unable to get memory for journal structure"); |
Frederic Weisbecker | 98ea3f5 | 2009-12-29 21:51:15 +0100 | [diff] [blame] | 2703 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2704 | return 1; |
| 2705 | } |
| 2706 | memset(journal, 0, sizeof(struct reiserfs_journal)); |
| 2707 | INIT_LIST_HEAD(&journal->j_bitmap_nodes); |
| 2708 | INIT_LIST_HEAD(&journal->j_prealloc_list); |
| 2709 | INIT_LIST_HEAD(&journal->j_working_list); |
| 2710 | INIT_LIST_HEAD(&journal->j_journal_list); |
| 2711 | journal->j_persistent_trans = 0; |
Frederic Weisbecker | 98ea3f5 | 2009-12-29 21:51:15 +0100 | [diff] [blame] | 2712 | ret = reiserfs_allocate_list_bitmaps(sb, journal->j_list_bitmap, |
| 2713 | reiserfs_bmap_count(sb)); |
| 2714 | reiserfs_write_lock(sb); |
| 2715 | if (ret) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2716 | goto free_and_return; |
Frederic Weisbecker | 98ea3f5 | 2009-12-29 21:51:15 +0100 | [diff] [blame] | 2717 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2718 | allocate_bitmap_nodes(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2720 | /* reserved for journal area support */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2721 | SB_JOURNAL_1st_RESERVED_BLOCK(sb) = (old_format ? |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2722 | REISERFS_OLD_DISK_OFFSET_IN_BYTES |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2723 | / sb->s_blocksize + |
| 2724 | reiserfs_bmap_count(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2725 | 1 : |
| 2726 | REISERFS_DISK_OFFSET_IN_BYTES / |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2727 | sb->s_blocksize + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2729 | /* Sanity check to see is the standard journal fitting withing first bitmap |
| 2730 | (actual for small blocksizes) */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2731 | if (!SB_ONDISK_JOURNAL_DEVICE(sb) && |
| 2732 | (SB_JOURNAL_1st_RESERVED_BLOCK(sb) + |
| 2733 | SB_ONDISK_JOURNAL_SIZE(sb) > sb->s_blocksize * 8)) { |
| 2734 | reiserfs_warning(sb, "journal-1393", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2735 | "journal does not fit for area addressed " |
| 2736 | "by first of bitmap blocks. It starts at " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2737 | "%u and its size is %u. Block size %ld", |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2738 | SB_JOURNAL_1st_RESERVED_BLOCK(sb), |
| 2739 | SB_ONDISK_JOURNAL_SIZE(sb), |
| 2740 | sb->s_blocksize); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2741 | goto free_and_return; |
| 2742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | |
Frederic Weisbecker | 193be0e | 2009-09-17 05:31:37 +0200 | [diff] [blame] | 2744 | /* |
| 2745 | * We need to unlock here to avoid creating the following |
| 2746 | * dependency: |
| 2747 | * reiserfs_lock -> sysfs_mutex |
| 2748 | * Because the reiserfs mmap path creates the following dependency: |
| 2749 | * mm->mmap -> reiserfs_lock, hence we have |
| 2750 | * mm->mmap -> reiserfs_lock ->sysfs_mutex |
| 2751 | * This would ends up in a circular dependency with sysfs readdir path |
| 2752 | * which does sysfs_mutex -> mm->mmap_sem |
| 2753 | * This is fine because the reiserfs lock is useless in mount path, |
| 2754 | * at least until we call journal_begin. We keep it for paranoid |
| 2755 | * reasons. |
| 2756 | */ |
| 2757 | reiserfs_write_unlock(sb); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2758 | if (journal_init_dev(sb, journal, j_dev_name) != 0) { |
Frederic Weisbecker | 193be0e | 2009-09-17 05:31:37 +0200 | [diff] [blame] | 2759 | reiserfs_write_lock(sb); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2760 | reiserfs_warning(sb, "sh-462", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2761 | "unable to initialize jornal device"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2762 | goto free_and_return; |
| 2763 | } |
Frederic Weisbecker | 193be0e | 2009-09-17 05:31:37 +0200 | [diff] [blame] | 2764 | reiserfs_write_lock(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2766 | rs = SB_DISK_SUPER_BLOCK(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2768 | /* read journal header */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2769 | bhjh = journal_bread(sb, |
| 2770 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
| 2771 | SB_ONDISK_JOURNAL_SIZE(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2772 | if (!bhjh) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2773 | reiserfs_warning(sb, "sh-459", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2774 | "unable to read journal header"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2775 | goto free_and_return; |
| 2776 | } |
| 2777 | jh = (struct reiserfs_journal_header *)(bhjh->b_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2779 | /* make sure that journal matches to the super block */ |
| 2780 | if (is_reiserfs_jr(rs) |
| 2781 | && (le32_to_cpu(jh->jh_journal.jp_journal_magic) != |
| 2782 | sb_jp_journal_magic(rs))) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2783 | reiserfs_warning(sb, "sh-460", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2784 | "journal header magic %x (device %s) does " |
| 2785 | "not match to magic found in super block %x", |
| 2786 | jh->jh_journal.jp_journal_magic, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2787 | bdevname(journal->j_dev_bd, b), |
| 2788 | sb_jp_journal_magic(rs)); |
| 2789 | brelse(bhjh); |
| 2790 | goto free_and_return; |
| 2791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2793 | journal->j_trans_max = le32_to_cpu(jh->jh_journal.jp_journal_trans_max); |
| 2794 | journal->j_max_batch = le32_to_cpu(jh->jh_journal.jp_journal_max_batch); |
| 2795 | journal->j_max_commit_age = |
| 2796 | le32_to_cpu(jh->jh_journal.jp_journal_max_commit_age); |
| 2797 | journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2799 | if (check_advise_trans_params(sb, journal) != 0) |
Edward Shishkin | cf3d0b8 | 2007-10-16 23:30:30 -0700 | [diff] [blame] | 2800 | goto free_and_return; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2801 | journal->j_default_max_commit_age = journal->j_max_commit_age; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2803 | if (commit_max_age != 0) { |
| 2804 | journal->j_max_commit_age = commit_max_age; |
| 2805 | journal->j_max_trans_age = commit_max_age; |
| 2806 | } |
| 2807 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2808 | reiserfs_info(sb, "journal params: device %s, size %u, " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2809 | "journal first block %u, max trans len %u, max batch %u, " |
| 2810 | "max commit age %u, max trans age %u\n", |
| 2811 | bdevname(journal->j_dev_bd, b), |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2812 | SB_ONDISK_JOURNAL_SIZE(sb), |
| 2813 | SB_ONDISK_JOURNAL_1st_BLOCK(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2814 | journal->j_trans_max, |
| 2815 | journal->j_max_batch, |
| 2816 | journal->j_max_commit_age, journal->j_max_trans_age); |
| 2817 | |
| 2818 | brelse(bhjh); |
| 2819 | |
| 2820 | journal->j_list_bitmap_index = 0; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2821 | journal_list_init(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2822 | |
| 2823 | memset(journal->j_list_hash_table, 0, |
| 2824 | JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *)); |
| 2825 | |
| 2826 | INIT_LIST_HEAD(&journal->j_dirty_buffers); |
| 2827 | spin_lock_init(&journal->j_dirty_buffers_lock); |
| 2828 | |
| 2829 | journal->j_start = 0; |
| 2830 | journal->j_len = 0; |
| 2831 | journal->j_len_alloc = 0; |
| 2832 | atomic_set(&(journal->j_wcount), 0); |
| 2833 | atomic_set(&(journal->j_async_throttle), 0); |
| 2834 | journal->j_bcount = 0; |
| 2835 | journal->j_trans_start_time = 0; |
| 2836 | journal->j_last = NULL; |
| 2837 | journal->j_first = NULL; |
| 2838 | init_waitqueue_head(&(journal->j_join_wait)); |
Jeff Mahoney | f68215c | 2008-07-25 01:46:38 -0700 | [diff] [blame] | 2839 | mutex_init(&journal->j_mutex); |
Jeff Mahoney | afe7025 | 2008-07-25 01:46:39 -0700 | [diff] [blame] | 2840 | mutex_init(&journal->j_flush_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2841 | |
| 2842 | journal->j_trans_id = 10; |
| 2843 | journal->j_mount_id = 10; |
| 2844 | journal->j_state = 0; |
| 2845 | atomic_set(&(journal->j_jlock), 0); |
Frederic Weisbecker | bbec919 | 2010-01-28 13:43:50 +0100 | [diff] [blame] | 2846 | reiserfs_write_unlock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2847 | journal->j_cnode_free_list = allocate_cnodes(num_cnodes); |
Frederic Weisbecker | bbec919 | 2010-01-28 13:43:50 +0100 | [diff] [blame] | 2848 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2849 | journal->j_cnode_free_orig = journal->j_cnode_free_list; |
| 2850 | journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0; |
| 2851 | journal->j_cnode_used = 0; |
| 2852 | journal->j_must_wait = 0; |
| 2853 | |
Jeff Mahoney | 576f6d7 | 2005-11-29 19:34:39 -0800 | [diff] [blame] | 2854 | if (journal->j_cnode_free == 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2855 | reiserfs_warning(sb, "journal-2004", "Journal cnode memory " |
Jeff Mahoney | 576f6d7 | 2005-11-29 19:34:39 -0800 | [diff] [blame] | 2856 | "allocation failed (%ld bytes). Journal is " |
| 2857 | "too large for available memory. Usually " |
| 2858 | "this is due to a journal that is too large.", |
| 2859 | sizeof (struct reiserfs_journal_cnode) * num_cnodes); |
| 2860 | goto free_and_return; |
| 2861 | } |
| 2862 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2863 | init_journal_hash(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2864 | jl = journal->j_current_jl; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2865 | jl->j_list_bitmap = get_list_bitmap(sb, jl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2866 | if (!jl->j_list_bitmap) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2867 | reiserfs_warning(sb, "journal-2005", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2868 | "get_list_bitmap failed for journal list 0"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2869 | goto free_and_return; |
| 2870 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2871 | if (journal_read(sb) < 0) { |
| 2872 | reiserfs_warning(sb, "reiserfs-2006", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2873 | "Replay Failure, unable to mount"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2874 | goto free_and_return; |
| 2875 | } |
| 2876 | |
| 2877 | reiserfs_mounted_fs_count++; |
Frederic Weisbecker | 48f6ba5 | 2009-10-05 16:31:37 +0200 | [diff] [blame] | 2878 | if (reiserfs_mounted_fs_count <= 1) { |
| 2879 | reiserfs_write_unlock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2880 | commit_wq = create_workqueue("reiserfs"); |
Frederic Weisbecker | 48f6ba5 | 2009-10-05 16:31:37 +0200 | [diff] [blame] | 2881 | reiserfs_write_lock(sb); |
| 2882 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2883 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2884 | INIT_DELAYED_WORK(&journal->j_work, flush_async_commits); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2885 | journal->j_work_sb = sb; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2886 | return 0; |
| 2887 | free_and_return: |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 2888 | free_journal_ram(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2889 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | /* |
| 2893 | ** test for a polite end of the current transaction. Used by file_write, and should |
| 2894 | ** be used by delete to make sure they don't write more than can fit inside a single |
| 2895 | ** transaction |
| 2896 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2897 | int journal_transaction_should_end(struct reiserfs_transaction_handle *th, |
| 2898 | int new_alloc) |
| 2899 | { |
| 2900 | struct reiserfs_journal *journal = SB_JOURNAL(th->t_super); |
| 2901 | time_t now = get_seconds(); |
| 2902 | /* cannot restart while nested */ |
| 2903 | BUG_ON(!th->t_trans_id); |
| 2904 | if (th->t_refcount > 1) |
| 2905 | return 0; |
| 2906 | if (journal->j_must_wait > 0 || |
| 2907 | (journal->j_len_alloc + new_alloc) >= journal->j_max_batch || |
| 2908 | atomic_read(&(journal->j_jlock)) || |
| 2909 | (now - journal->j_trans_start_time) > journal->j_max_trans_age || |
| 2910 | journal->j_cnode_free < (journal->j_trans_max * 3)) { |
| 2911 | return 1; |
| 2912 | } |
Chris Mason | 6ae1ea4 | 2006-02-01 03:06:50 -0800 | [diff] [blame] | 2913 | /* protected by the BKL here */ |
| 2914 | journal->j_len_alloc += new_alloc; |
| 2915 | th->t_blocks_allocated += new_alloc ; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2916 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 2919 | /* this must be called inside a transaction, and requires the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | ** kernel_lock to be held |
| 2921 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2922 | void reiserfs_block_writes(struct reiserfs_transaction_handle *th) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2924 | struct reiserfs_journal *journal = SB_JOURNAL(th->t_super); |
| 2925 | BUG_ON(!th->t_trans_id); |
| 2926 | journal->j_must_wait = 1; |
| 2927 | set_bit(J_WRITERS_BLOCKED, &journal->j_state); |
| 2928 | return; |
| 2929 | } |
| 2930 | |
| 2931 | /* this must be called without a transaction started, and does not |
| 2932 | ** require BKL |
| 2933 | */ |
| 2934 | void reiserfs_allow_writes(struct super_block *s) |
| 2935 | { |
| 2936 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 2937 | clear_bit(J_WRITERS_BLOCKED, &journal->j_state); |
| 2938 | wake_up(&journal->j_join_wait); |
| 2939 | } |
| 2940 | |
| 2941 | /* this must be called without a transaction started, and does not |
| 2942 | ** require BKL |
| 2943 | */ |
| 2944 | void reiserfs_wait_on_write_block(struct super_block *s) |
| 2945 | { |
| 2946 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 2947 | wait_event(journal->j_join_wait, |
| 2948 | !test_bit(J_WRITERS_BLOCKED, &journal->j_state)); |
| 2949 | } |
| 2950 | |
| 2951 | static void queue_log_writer(struct super_block *s) |
| 2952 | { |
| 2953 | wait_queue_t wait; |
| 2954 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 2955 | set_bit(J_WRITERS_QUEUED, &journal->j_state); |
| 2956 | |
| 2957 | /* |
| 2958 | * we don't want to use wait_event here because |
| 2959 | * we only want to wait once. |
| 2960 | */ |
| 2961 | init_waitqueue_entry(&wait, current); |
| 2962 | add_wait_queue(&journal->j_join_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | set_current_state(TASK_UNINTERRUPTIBLE); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 2964 | if (test_bit(J_WRITERS_QUEUED, &journal->j_state)) { |
| 2965 | reiserfs_write_unlock(s); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2966 | schedule(); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 2967 | reiserfs_write_lock(s); |
| 2968 | } |
Milind Arun Choudhary | 5ab2f7e | 2007-05-08 00:30:51 -0700 | [diff] [blame] | 2969 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2970 | remove_wait_queue(&journal->j_join_wait, &wait); |
| 2971 | } |
| 2972 | |
| 2973 | static void wake_queued_writers(struct super_block *s) |
| 2974 | { |
| 2975 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 2976 | if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state)) |
| 2977 | wake_up(&journal->j_join_wait); |
| 2978 | } |
| 2979 | |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 2980 | static void let_transaction_grow(struct super_block *sb, unsigned int trans_id) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2981 | { |
| 2982 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
| 2983 | unsigned long bcount = journal->j_bcount; |
| 2984 | while (1) { |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 2985 | reiserfs_write_unlock(sb); |
Nishanth Aravamudan | 041e0e3 | 2005-09-10 00:27:23 -0700 | [diff] [blame] | 2986 | schedule_timeout_uninterruptible(1); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 2987 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2988 | journal->j_current_jl->j_state |= LIST_COMMIT_PENDING; |
| 2989 | while ((atomic_read(&journal->j_wcount) > 0 || |
| 2990 | atomic_read(&journal->j_jlock)) && |
| 2991 | journal->j_trans_id == trans_id) { |
| 2992 | queue_log_writer(sb); |
| 2993 | } |
| 2994 | if (journal->j_trans_id != trans_id) |
| 2995 | break; |
| 2996 | if (bcount == journal->j_bcount) |
| 2997 | break; |
| 2998 | bcount = journal->j_bcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | /* join == true if you must join an existing transaction. |
| 3003 | ** join == false if you can deal with waiting for others to finish |
| 3004 | ** |
| 3005 | ** this will block until the transaction is joinable. send the number of blocks you |
| 3006 | ** expect to use in nblocks. |
| 3007 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3008 | static int do_journal_begin_r(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3009 | struct super_block *sb, unsigned long nblocks, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3010 | int join) |
| 3011 | { |
| 3012 | time_t now = get_seconds(); |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 3013 | unsigned int old_trans_id; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3014 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3015 | struct reiserfs_transaction_handle myth; |
| 3016 | int sched_count = 0; |
| 3017 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3019 | reiserfs_check_lock_depth(sb, "journal_begin"); |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3020 | BUG_ON(nblocks > journal->j_trans_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3022 | PROC_INFO_INC(sb, journal.journal_being); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3023 | /* set here for journal_join */ |
| 3024 | th->t_refcount = 1; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3025 | th->t_super = sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3027 | relock: |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3028 | lock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3029 | if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted(journal)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3030 | unlock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3031 | retval = journal->j_errno; |
| 3032 | goto out_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3034 | journal->j_bcount++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3036 | if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3037 | unlock_journal(sb); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 3038 | reiserfs_write_unlock(sb); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3039 | reiserfs_wait_on_write_block(sb); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 3040 | reiserfs_write_lock(sb); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3041 | PROC_INFO_INC(sb, journal.journal_relock_writers); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3042 | goto relock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3044 | now = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3046 | /* if there is no room in the journal OR |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3047 | ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3048 | ** we don't sleep if there aren't other writers |
| 3049 | */ |
| 3050 | |
| 3051 | if ((!join && journal->j_must_wait > 0) || |
| 3052 | (!join |
| 3053 | && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch) |
| 3054 | || (!join && atomic_read(&journal->j_wcount) > 0 |
| 3055 | && journal->j_trans_start_time > 0 |
| 3056 | && (now - journal->j_trans_start_time) > |
| 3057 | journal->j_max_trans_age) || (!join |
| 3058 | && atomic_read(&journal->j_jlock)) |
| 3059 | || (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) { |
| 3060 | |
| 3061 | old_trans_id = journal->j_trans_id; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3062 | unlock_journal(sb); /* allow others to finish this transaction */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3063 | |
| 3064 | if (!join && (journal->j_len_alloc + nblocks + 2) >= |
| 3065 | journal->j_max_batch && |
| 3066 | ((journal->j_len + nblocks + 2) * 100) < |
| 3067 | (journal->j_len_alloc * 75)) { |
| 3068 | if (atomic_read(&journal->j_wcount) > 10) { |
| 3069 | sched_count++; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3070 | queue_log_writer(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3071 | goto relock; |
| 3072 | } |
| 3073 | } |
| 3074 | /* don't mess with joining the transaction if all we have to do is |
| 3075 | * wait for someone else to do a commit |
| 3076 | */ |
| 3077 | if (atomic_read(&journal->j_jlock)) { |
| 3078 | while (journal->j_trans_id == old_trans_id && |
| 3079 | atomic_read(&journal->j_jlock)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3080 | queue_log_writer(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3081 | } |
| 3082 | goto relock; |
| 3083 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3084 | retval = journal_join(&myth, sb, 1); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3085 | if (retval) |
| 3086 | goto out_fail; |
| 3087 | |
| 3088 | /* someone might have ended the transaction while we joined */ |
| 3089 | if (old_trans_id != journal->j_trans_id) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3090 | retval = do_journal_end(&myth, sb, 1, 0); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3091 | } else { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3092 | retval = do_journal_end(&myth, sb, 1, COMMIT_NOW); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | if (retval) |
| 3096 | goto out_fail; |
| 3097 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3098 | PROC_INFO_INC(sb, journal.journal_relock_wcount); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3099 | goto relock; |
| 3100 | } |
| 3101 | /* we are the first writer, set trans_id */ |
| 3102 | if (journal->j_trans_start_time == 0) { |
| 3103 | journal->j_trans_start_time = get_seconds(); |
| 3104 | } |
| 3105 | atomic_inc(&(journal->j_wcount)); |
| 3106 | journal->j_len_alloc += nblocks; |
| 3107 | th->t_blocks_logged = 0; |
| 3108 | th->t_blocks_allocated = nblocks; |
| 3109 | th->t_trans_id = journal->j_trans_id; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3110 | unlock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3111 | INIT_LIST_HEAD(&th->t_list); |
| 3112 | get_fs_excl(); |
| 3113 | return 0; |
| 3114 | |
| 3115 | out_fail: |
| 3116 | memset(th, 0, sizeof(*th)); |
| 3117 | /* Re-set th->t_super, so we can properly keep track of how many |
| 3118 | * persistent transactions there are. We need to do this so if this |
| 3119 | * call is part of a failed restart_transaction, we can free it later */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3120 | th->t_super = sb; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3121 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3124 | struct reiserfs_transaction_handle *reiserfs_persistent_transaction(struct |
| 3125 | super_block |
| 3126 | *s, |
| 3127 | int nblocks) |
| 3128 | { |
| 3129 | int ret; |
| 3130 | struct reiserfs_transaction_handle *th; |
| 3131 | |
| 3132 | /* if we're nesting into an existing transaction. It will be |
| 3133 | ** persistent on its own |
| 3134 | */ |
| 3135 | if (reiserfs_transaction_running(s)) { |
| 3136 | th = current->journal_info; |
| 3137 | th->t_refcount++; |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3138 | BUG_ON(th->t_refcount < 2); |
| 3139 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3140 | return th; |
| 3141 | } |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 3142 | th = kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3143 | if (!th) |
| 3144 | return NULL; |
| 3145 | ret = journal_begin(th, s, nblocks); |
| 3146 | if (ret) { |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 3147 | kfree(th); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3148 | return NULL; |
| 3149 | } |
| 3150 | |
| 3151 | SB_JOURNAL(s)->j_persistent_trans++; |
| 3152 | return th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | } |
| 3154 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3155 | int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th) |
| 3156 | { |
| 3157 | struct super_block *s = th->t_super; |
| 3158 | int ret = 0; |
| 3159 | if (th->t_trans_id) |
| 3160 | ret = journal_end(th, th->t_super, th->t_blocks_allocated); |
| 3161 | else |
| 3162 | ret = -EIO; |
| 3163 | if (th->t_refcount == 0) { |
| 3164 | SB_JOURNAL(s)->j_persistent_trans--; |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 3165 | kfree(th); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3166 | } |
| 3167 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | } |
| 3169 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3170 | static int journal_join(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3171 | struct super_block *sb, unsigned long nblocks) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3172 | { |
| 3173 | struct reiserfs_transaction_handle *cur_th = current->journal_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3175 | /* this keeps do_journal_end from NULLing out the current->journal_info |
| 3176 | ** pointer |
| 3177 | */ |
| 3178 | th->t_handle_save = cur_th; |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3179 | BUG_ON(cur_th && cur_th->t_refcount > 1); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3180 | return do_journal_begin_r(th, sb, nblocks, JBEGIN_JOIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3183 | int journal_join_abort(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3184 | struct super_block *sb, unsigned long nblocks) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3185 | { |
| 3186 | struct reiserfs_transaction_handle *cur_th = current->journal_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3188 | /* this keeps do_journal_end from NULLing out the current->journal_info |
| 3189 | ** pointer |
| 3190 | */ |
| 3191 | th->t_handle_save = cur_th; |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3192 | BUG_ON(cur_th && cur_th->t_refcount > 1); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3193 | return do_journal_begin_r(th, sb, nblocks, JBEGIN_ABORT); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | int journal_begin(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3197 | struct super_block *sb, unsigned long nblocks) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3198 | { |
| 3199 | struct reiserfs_transaction_handle *cur_th = current->journal_info; |
| 3200 | int ret; |
| 3201 | |
| 3202 | th->t_handle_save = NULL; |
| 3203 | if (cur_th) { |
| 3204 | /* we are nesting into the current transaction */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3205 | if (cur_th->t_super == sb) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3206 | BUG_ON(!cur_th->t_refcount); |
| 3207 | cur_th->t_refcount++; |
| 3208 | memcpy(th, cur_th, sizeof(*th)); |
| 3209 | if (th->t_refcount <= 1) |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3210 | reiserfs_warning(sb, "reiserfs-2005", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 3211 | "BAD: refcount <= 1, but " |
| 3212 | "journal_info != 0"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3213 | return 0; |
| 3214 | } else { |
| 3215 | /* we've ended up with a handle from a different filesystem. |
| 3216 | ** save it and restore on journal_end. This should never |
| 3217 | ** really happen... |
| 3218 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3219 | reiserfs_warning(sb, "clm-2100", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 3220 | "nesting info a different FS"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3221 | th->t_handle_save = current->journal_info; |
| 3222 | current->journal_info = th; |
| 3223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | } else { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3225 | current->journal_info = th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3227 | ret = do_journal_begin_r(th, sb, nblocks, JBEGIN_REG); |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3228 | BUG_ON(current->journal_info != th); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3230 | /* I guess this boils down to being the reciprocal of clm-2100 above. |
| 3231 | * If do_journal_begin_r fails, we need to put it back, since journal_end |
| 3232 | * won't be called to do it. */ |
| 3233 | if (ret) |
| 3234 | current->journal_info = th->t_handle_save; |
| 3235 | else |
| 3236 | BUG_ON(!th->t_refcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3238 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | /* |
| 3242 | ** puts bh into the current transaction. If it was already there, reorders removes the |
| 3243 | ** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order). |
| 3244 | ** |
| 3245 | ** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the |
| 3246 | ** transaction is committed. |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3247 | ** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | ** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len. |
| 3249 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3250 | int journal_mark_dirty(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3251 | struct super_block *sb, struct buffer_head *bh) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3252 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3253 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3254 | struct reiserfs_journal_cnode *cn = NULL; |
| 3255 | int count_already_incd = 0; |
| 3256 | int prepared = 0; |
| 3257 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3259 | PROC_INFO_INC(sb, journal.mark_dirty); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3260 | if (th->t_trans_id != journal->j_trans_id) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 3261 | reiserfs_panic(th->t_super, "journal-1577", |
| 3262 | "handle trans id %ld != current trans id %ld", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3263 | th->t_trans_id, journal->j_trans_id); |
| 3264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3266 | sb->s_dirt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3268 | prepared = test_clear_buffer_journal_prepared(bh); |
| 3269 | clear_buffer_journal_restore_dirty(bh); |
| 3270 | /* already in this transaction, we are done */ |
| 3271 | if (buffer_journaled(bh)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3272 | PROC_INFO_INC(sb, journal.mark_dirty_already); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3273 | return 0; |
| 3274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3276 | /* this must be turned into a panic instead of a warning. We can't allow |
| 3277 | ** a dirty or journal_dirty or locked buffer to be logged, as some changes |
| 3278 | ** could get to disk too early. NOT GOOD. |
| 3279 | */ |
| 3280 | if (!prepared || buffer_dirty(bh)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3281 | reiserfs_warning(sb, "journal-1777", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 3282 | "buffer %llu bad state " |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3283 | "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT", |
| 3284 | (unsigned long long)bh->b_blocknr, |
| 3285 | prepared ? ' ' : '!', |
| 3286 | buffer_locked(bh) ? ' ' : '!', |
| 3287 | buffer_dirty(bh) ? ' ' : '!', |
| 3288 | buffer_journal_dirty(bh) ? ' ' : '!'); |
| 3289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3291 | if (atomic_read(&(journal->j_wcount)) <= 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3292 | reiserfs_warning(sb, "journal-1409", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 3293 | "returning because j_wcount was %d", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3294 | atomic_read(&(journal->j_wcount))); |
| 3295 | return 1; |
| 3296 | } |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3297 | /* this error means I've screwed up, and we've overflowed the transaction. |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3298 | ** Nothing can be done here, except make the FS readonly or panic. |
| 3299 | */ |
| 3300 | if (journal->j_len >= journal->j_trans_max) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 3301 | reiserfs_panic(th->t_super, "journal-1413", |
| 3302 | "j_len (%lu) is too big", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3303 | journal->j_len); |
| 3304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3306 | if (buffer_journal_dirty(bh)) { |
| 3307 | count_already_incd = 1; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3308 | PROC_INFO_INC(sb, journal.mark_dirty_notjournal); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3309 | clear_buffer_journal_dirty(bh); |
| 3310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3312 | if (journal->j_len > journal->j_len_alloc) { |
| 3313 | journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT; |
| 3314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3316 | set_buffer_journaled(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3318 | /* now put this guy on the end */ |
| 3319 | if (!cn) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3320 | cn = get_cnode(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3321 | if (!cn) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3322 | reiserfs_panic(sb, "journal-4", "get_cnode failed!"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3325 | if (th->t_blocks_logged == th->t_blocks_allocated) { |
| 3326 | th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT; |
| 3327 | journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT; |
| 3328 | } |
| 3329 | th->t_blocks_logged++; |
| 3330 | journal->j_len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3332 | cn->bh = bh; |
| 3333 | cn->blocknr = bh->b_blocknr; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3334 | cn->sb = sb; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3335 | cn->jlist = NULL; |
| 3336 | insert_journal_hash(journal->j_hash_table, cn); |
| 3337 | if (!count_already_incd) { |
| 3338 | get_bh(bh); |
| 3339 | } |
| 3340 | } |
| 3341 | cn->next = NULL; |
| 3342 | cn->prev = journal->j_last; |
| 3343 | cn->bh = bh; |
| 3344 | if (journal->j_last) { |
| 3345 | journal->j_last->next = cn; |
| 3346 | journal->j_last = cn; |
| 3347 | } else { |
| 3348 | journal->j_first = cn; |
| 3349 | journal->j_last = cn; |
| 3350 | } |
| 3351 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | } |
| 3353 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3354 | int journal_end(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3355 | struct super_block *sb, unsigned long nblocks) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3356 | { |
| 3357 | if (!current->journal_info && th->t_refcount > 1) |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3358 | reiserfs_warning(sb, "REISER-NESTING", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 3359 | "th NULL, refcount %d", th->t_refcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3361 | if (!th->t_trans_id) { |
| 3362 | WARN_ON(1); |
| 3363 | return -EIO; |
| 3364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3366 | th->t_refcount--; |
| 3367 | if (th->t_refcount > 0) { |
| 3368 | struct reiserfs_transaction_handle *cur_th = |
| 3369 | current->journal_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3371 | /* we aren't allowed to close a nested transaction on a different |
| 3372 | ** filesystem from the one in the task struct |
| 3373 | */ |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3374 | BUG_ON(cur_th->t_super != th->t_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3376 | if (th != cur_th) { |
| 3377 | memcpy(current->journal_info, th, sizeof(*th)); |
| 3378 | th->t_trans_id = 0; |
| 3379 | } |
| 3380 | return 0; |
| 3381 | } else { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3382 | return do_journal_end(th, sb, nblocks, 0); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | } |
| 3385 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3386 | /* removes from the current transaction, relsing and descrementing any counters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | ** also files the removed buffer directly onto the clean list |
| 3388 | ** |
| 3389 | ** called by journal_mark_freed when a block has been deleted |
| 3390 | ** |
| 3391 | ** returns 1 if it cleaned and relsed the buffer. 0 otherwise |
| 3392 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3393 | static int remove_from_transaction(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3394 | b_blocknr_t blocknr, int already_cleaned) |
| 3395 | { |
| 3396 | struct buffer_head *bh; |
| 3397 | struct reiserfs_journal_cnode *cn; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3398 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3399 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3401 | cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3402 | if (!cn || !cn->bh) { |
| 3403 | return ret; |
| 3404 | } |
| 3405 | bh = cn->bh; |
| 3406 | if (cn->prev) { |
| 3407 | cn->prev->next = cn->next; |
| 3408 | } |
| 3409 | if (cn->next) { |
| 3410 | cn->next->prev = cn->prev; |
| 3411 | } |
| 3412 | if (cn == journal->j_first) { |
| 3413 | journal->j_first = cn->next; |
| 3414 | } |
| 3415 | if (cn == journal->j_last) { |
| 3416 | journal->j_last = cn->prev; |
| 3417 | } |
| 3418 | if (bh) |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3419 | remove_journal_hash(sb, journal->j_hash_table, NULL, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3420 | bh->b_blocknr, 0); |
| 3421 | clear_buffer_journaled(bh); /* don't log this one */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3423 | if (!already_cleaned) { |
| 3424 | clear_buffer_journal_dirty(bh); |
| 3425 | clear_buffer_dirty(bh); |
| 3426 | clear_buffer_journal_test(bh); |
| 3427 | put_bh(bh); |
| 3428 | if (atomic_read(&(bh->b_count)) < 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3429 | reiserfs_warning(sb, "journal-1752", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 3430 | "b_count < 0"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3431 | } |
| 3432 | ret = 1; |
| 3433 | } |
| 3434 | journal->j_len--; |
| 3435 | journal->j_len_alloc--; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3436 | free_cnode(sb, cn); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3437 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | /* |
| 3441 | ** for any cnode in a journal list, it can only be dirtied of all the |
Matt LaPlante | 0779bf2 | 2006-11-30 05:24:39 +0100 | [diff] [blame] | 3442 | ** transactions that include it are committed to disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | ** this checks through each transaction, and returns 1 if you are allowed to dirty, |
| 3444 | ** and 0 if you aren't |
| 3445 | ** |
| 3446 | ** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log |
| 3447 | ** blocks for a given transaction on disk |
| 3448 | ** |
| 3449 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3450 | static int can_dirty(struct reiserfs_journal_cnode *cn) |
| 3451 | { |
| 3452 | struct super_block *sb = cn->sb; |
| 3453 | b_blocknr_t blocknr = cn->blocknr; |
| 3454 | struct reiserfs_journal_cnode *cur = cn->hprev; |
| 3455 | int can_dirty = 1; |
| 3456 | |
| 3457 | /* first test hprev. These are all newer than cn, so any node here |
| 3458 | ** with the same block number and dev means this node can't be sent |
| 3459 | ** to disk right now. |
| 3460 | */ |
| 3461 | while (cur && can_dirty) { |
| 3462 | if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb && |
| 3463 | cur->blocknr == blocknr) { |
| 3464 | can_dirty = 0; |
| 3465 | } |
| 3466 | cur = cur->hprev; |
| 3467 | } |
| 3468 | /* then test hnext. These are all older than cn. As long as they |
| 3469 | ** are committed to the log, it is safe to write cn to disk |
| 3470 | */ |
| 3471 | cur = cn->hnext; |
| 3472 | while (cur && can_dirty) { |
| 3473 | if (cur->jlist && cur->jlist->j_len > 0 && |
| 3474 | atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh && |
| 3475 | cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) { |
| 3476 | can_dirty = 0; |
| 3477 | } |
| 3478 | cur = cur->hnext; |
| 3479 | } |
| 3480 | return can_dirty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | /* syncs the commit blocks, but does not force the real buffers to disk |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3484 | ** will wait until the current transaction is done/committed before returning |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3486 | int journal_end_sync(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3487 | struct super_block *sb, unsigned long nblocks) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3488 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3489 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3491 | BUG_ON(!th->t_trans_id); |
| 3492 | /* you can sync while nested, very, very bad */ |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3493 | BUG_ON(th->t_refcount > 1); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3494 | if (journal->j_len == 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3495 | reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3496 | 1); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3497 | journal_mark_dirty(th, sb, SB_BUFFER_WITH_SB(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3498 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3499 | return do_journal_end(th, sb, nblocks, COMMIT_NOW | WAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | /* |
| 3503 | ** writeback the pending async commits to disk |
| 3504 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3505 | static void flush_async_commits(struct work_struct *work) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3506 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3507 | struct reiserfs_journal *journal = |
| 3508 | container_of(work, struct reiserfs_journal, j_work.work); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3509 | struct super_block *sb = journal->j_work_sb; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3510 | struct reiserfs_journal_list *jl; |
| 3511 | struct list_head *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 3513 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3514 | if (!list_empty(&journal->j_journal_list)) { |
| 3515 | /* last entry is the youngest, commit it and you get everything */ |
| 3516 | entry = journal->j_journal_list.prev; |
| 3517 | jl = JOURNAL_LIST_ENTRY(entry); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3518 | flush_commit_list(sb, jl, 1); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3519 | } |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 3520 | reiserfs_write_unlock(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | /* |
| 3524 | ** flushes any old transactions to disk |
| 3525 | ** ends the current transaction if it is too old |
| 3526 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3527 | int reiserfs_flush_old_commits(struct super_block *sb) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3528 | { |
| 3529 | time_t now; |
| 3530 | struct reiserfs_transaction_handle th; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3531 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3533 | now = get_seconds(); |
| 3534 | /* safety check so we don't flush while we are replaying the log during |
| 3535 | * mount |
| 3536 | */ |
| 3537 | if (list_empty(&journal->j_journal_list)) { |
| 3538 | return 0; |
| 3539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3541 | /* check the current transaction. If there are no writers, and it is |
| 3542 | * too old, finish it, and force the commit blocks to disk |
| 3543 | */ |
| 3544 | if (atomic_read(&journal->j_wcount) <= 0 && |
| 3545 | journal->j_trans_start_time > 0 && |
| 3546 | journal->j_len > 0 && |
| 3547 | (now - journal->j_trans_start_time) > journal->j_max_trans_age) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3548 | if (!journal_join(&th, sb, 1)) { |
| 3549 | reiserfs_prepare_for_journal(sb, |
| 3550 | SB_BUFFER_WITH_SB(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3551 | 1); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3552 | journal_mark_dirty(&th, sb, |
| 3553 | SB_BUFFER_WITH_SB(sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3555 | /* we're only being called from kreiserfsd, it makes no sense to do |
| 3556 | ** an async commit so that kreiserfsd can do it later |
| 3557 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3558 | do_journal_end(&th, sb, 1, COMMIT_NOW | WAIT); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3559 | } |
| 3560 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3561 | return sb->s_dirt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | /* |
| 3565 | ** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3566 | ** |
| 3567 | ** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | ** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just |
| 3569 | ** flushes the commit list and returns 0. |
| 3570 | ** |
| 3571 | ** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait. |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3572 | ** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | ** Note, we can't allow the journal_end to proceed while there are still writers in the log. |
| 3574 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3575 | static int check_journal_end(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3576 | struct super_block *sb, unsigned long nblocks, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3577 | int flags) |
| 3578 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3580 | time_t now; |
| 3581 | int flush = flags & FLUSH_ALL; |
| 3582 | int commit_now = flags & COMMIT_NOW; |
| 3583 | int wait_on_commit = flags & WAIT; |
| 3584 | struct reiserfs_journal_list *jl; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3585 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3587 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3589 | if (th->t_trans_id != journal->j_trans_id) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 3590 | reiserfs_panic(th->t_super, "journal-1577", |
| 3591 | "handle trans id %ld != current trans id %ld", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3592 | th->t_trans_id, journal->j_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3595 | journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged); |
| 3596 | if (atomic_read(&(journal->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */ |
| 3597 | atomic_dec(&(journal->j_wcount)); |
| 3598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3600 | /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3601 | ** will be dealt with by next transaction that actually writes something, but should be taken |
| 3602 | ** care of in this trans |
| 3603 | */ |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3604 | BUG_ON(journal->j_len == 0); |
| 3605 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3606 | /* if wcount > 0, and we are called to with flush or commit_now, |
| 3607 | ** we wait on j_join_wait. We will wake up when the last writer has |
| 3608 | ** finished the transaction, and started it on its way to the disk. |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3609 | ** Then, we flush the commit or journal list, and just return 0 |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3610 | ** because the rest of journal end was already done for this transaction. |
| 3611 | */ |
| 3612 | if (atomic_read(&(journal->j_wcount)) > 0) { |
| 3613 | if (flush || commit_now) { |
| 3614 | unsigned trans_id; |
| 3615 | |
| 3616 | jl = journal->j_current_jl; |
| 3617 | trans_id = jl->j_trans_id; |
| 3618 | if (wait_on_commit) |
| 3619 | jl->j_state |= LIST_COMMIT_PENDING; |
| 3620 | atomic_set(&(journal->j_jlock), 1); |
| 3621 | if (flush) { |
| 3622 | journal->j_next_full_flush = 1; |
| 3623 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3624 | unlock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3625 | |
| 3626 | /* sleep while the current transaction is still j_jlocked */ |
| 3627 | while (journal->j_trans_id == trans_id) { |
| 3628 | if (atomic_read(&journal->j_jlock)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3629 | queue_log_writer(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3630 | } else { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3631 | lock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3632 | if (journal->j_trans_id == trans_id) { |
| 3633 | atomic_set(&(journal->j_jlock), |
| 3634 | 1); |
| 3635 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3636 | unlock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3637 | } |
| 3638 | } |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 3639 | BUG_ON(journal->j_trans_id == trans_id); |
| 3640 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3641 | if (commit_now |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3642 | && journal_list_still_alive(sb, trans_id) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3643 | && wait_on_commit) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3644 | flush_commit_list(sb, jl, 1); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3645 | } |
| 3646 | return 0; |
| 3647 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3648 | unlock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3649 | return 0; |
| 3650 | } |
| 3651 | |
| 3652 | /* deal with old transactions where we are the last writers */ |
| 3653 | now = get_seconds(); |
| 3654 | if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) { |
| 3655 | commit_now = 1; |
| 3656 | journal->j_next_async_flush = 1; |
| 3657 | } |
| 3658 | /* don't batch when someone is waiting on j_join_wait */ |
| 3659 | /* don't batch when syncing the commit or flushing the whole trans */ |
| 3660 | if (!(journal->j_must_wait > 0) && !(atomic_read(&(journal->j_jlock))) |
| 3661 | && !flush && !commit_now && (journal->j_len < journal->j_max_batch) |
| 3662 | && journal->j_len_alloc < journal->j_max_batch |
| 3663 | && journal->j_cnode_free > (journal->j_trans_max * 3)) { |
| 3664 | journal->j_bcount++; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3665 | unlock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3666 | return 0; |
| 3667 | } |
| 3668 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3669 | if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(sb)) { |
| 3670 | reiserfs_panic(sb, "journal-003", |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 3671 | "j_start (%ld) is too high", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3672 | journal->j_start); |
| 3673 | } |
| 3674 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | /* |
| 3678 | ** Does all the work that makes deleting blocks safe. |
| 3679 | ** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on. |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3680 | ** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | ** otherwise: |
| 3682 | ** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes |
| 3683 | ** before this transaction has finished. |
| 3684 | ** |
| 3685 | ** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with |
| 3686 | ** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash, |
| 3687 | ** the block can't be reallocated yet. |
| 3688 | ** |
| 3689 | ** Then remove it from the current transaction, decrementing any counters and filing it on the clean list. |
| 3690 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3691 | int journal_mark_freed(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3692 | struct super_block *sb, b_blocknr_t blocknr) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3693 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3694 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3695 | struct reiserfs_journal_cnode *cn = NULL; |
| 3696 | struct buffer_head *bh = NULL; |
| 3697 | struct reiserfs_list_bitmap *jb = NULL; |
| 3698 | int cleaned = 0; |
| 3699 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3701 | cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3702 | if (cn && cn->bh) { |
| 3703 | bh = cn->bh; |
| 3704 | get_bh(bh); |
| 3705 | } |
| 3706 | /* if it is journal new, we just remove it from this transaction */ |
| 3707 | if (bh && buffer_journal_new(bh)) { |
| 3708 | clear_buffer_journal_new(bh); |
| 3709 | clear_prepared_bits(bh); |
| 3710 | reiserfs_clean_and_file_buffer(bh); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3711 | cleaned = remove_from_transaction(sb, blocknr, cleaned); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3712 | } else { |
| 3713 | /* set the bit for this block in the journal bitmap for this transaction */ |
| 3714 | jb = journal->j_current_jl->j_list_bitmap; |
| 3715 | if (!jb) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3716 | reiserfs_panic(sb, "journal-1702", |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 3717 | "journal_list_bitmap is NULL"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3718 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3719 | set_bit_in_list_bitmap(sb, blocknr, jb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3721 | /* Note, the entire while loop is not allowed to schedule. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3723 | if (bh) { |
| 3724 | clear_prepared_bits(bh); |
| 3725 | reiserfs_clean_and_file_buffer(bh); |
| 3726 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3727 | cleaned = remove_from_transaction(sb, blocknr, cleaned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3729 | /* find all older transactions with this block, make sure they don't try to write it out */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3730 | cn = get_journal_hash_dev(sb, journal->j_list_hash_table, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3731 | blocknr); |
| 3732 | while (cn) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3733 | if (sb == cn->sb && blocknr == cn->blocknr) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3734 | set_bit(BLOCK_FREED, &cn->state); |
| 3735 | if (cn->bh) { |
| 3736 | if (!cleaned) { |
| 3737 | /* remove_from_transaction will brelse the buffer if it was |
| 3738 | ** in the current trans |
| 3739 | */ |
| 3740 | clear_buffer_journal_dirty(cn-> |
| 3741 | bh); |
| 3742 | clear_buffer_dirty(cn->bh); |
| 3743 | clear_buffer_journal_test(cn-> |
| 3744 | bh); |
| 3745 | cleaned = 1; |
| 3746 | put_bh(cn->bh); |
| 3747 | if (atomic_read |
| 3748 | (&(cn->bh->b_count)) < 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3749 | reiserfs_warning(sb, |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 3750 | "journal-2138", |
| 3751 | "cn->bh->b_count < 0"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3752 | } |
| 3753 | } |
| 3754 | if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */ |
| 3755 | atomic_dec(& |
| 3756 | (cn->jlist-> |
| 3757 | j_nonzerolen)); |
| 3758 | } |
| 3759 | cn->bh = NULL; |
| 3760 | } |
| 3761 | } |
| 3762 | cn = cn->hnext; |
| 3763 | } |
| 3764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | |
Chris Mason | 398c95b | 2007-10-16 23:29:44 -0700 | [diff] [blame] | 3766 | if (bh) |
| 3767 | release_buffer_page(bh); /* get_hash grabs the buffer */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3768 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | } |
| 3770 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3771 | void reiserfs_update_inode_transaction(struct inode *inode) |
| 3772 | { |
| 3773 | struct reiserfs_journal *journal = SB_JOURNAL(inode->i_sb); |
| 3774 | REISERFS_I(inode)->i_jl = journal->j_current_jl; |
| 3775 | REISERFS_I(inode)->i_trans_id = journal->j_trans_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | /* |
| 3779 | * returns -1 on error, 0 if no commits/barriers were done and 1 |
| 3780 | * if a transaction was actually committed and the barrier was done |
| 3781 | */ |
| 3782 | static int __commit_trans_jl(struct inode *inode, unsigned long id, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3783 | struct reiserfs_journal_list *jl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3785 | struct reiserfs_transaction_handle th; |
| 3786 | struct super_block *sb = inode->i_sb; |
| 3787 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
| 3788 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3790 | /* is it from the current transaction, or from an unknown transaction? */ |
| 3791 | if (id == journal->j_trans_id) { |
| 3792 | jl = journal->j_current_jl; |
| 3793 | /* try to let other writers come in and grow this transaction */ |
| 3794 | let_transaction_grow(sb, id); |
| 3795 | if (journal->j_trans_id != id) { |
| 3796 | goto flush_commit_only; |
| 3797 | } |
| 3798 | |
| 3799 | ret = journal_begin(&th, sb, 1); |
| 3800 | if (ret) |
| 3801 | return ret; |
| 3802 | |
| 3803 | /* someone might have ended this transaction while we joined */ |
| 3804 | if (journal->j_trans_id != id) { |
| 3805 | reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb), |
| 3806 | 1); |
| 3807 | journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb)); |
| 3808 | ret = journal_end(&th, sb, 1); |
| 3809 | goto flush_commit_only; |
| 3810 | } |
| 3811 | |
| 3812 | ret = journal_end_sync(&th, sb, 1); |
| 3813 | if (!ret) |
| 3814 | ret = 1; |
| 3815 | |
| 3816 | } else { |
| 3817 | /* this gets tricky, we have to make sure the journal list in |
| 3818 | * the inode still exists. We know the list is still around |
| 3819 | * if we've got a larger transaction id than the oldest list |
| 3820 | */ |
| 3821 | flush_commit_only: |
| 3822 | if (journal_list_still_alive(inode->i_sb, id)) { |
| 3823 | /* |
| 3824 | * we only set ret to 1 when we know for sure |
| 3825 | * the barrier hasn't been started yet on the commit |
| 3826 | * block. |
| 3827 | */ |
| 3828 | if (atomic_read(&jl->j_commit_left) > 1) |
| 3829 | ret = 1; |
| 3830 | flush_commit_list(sb, jl, 1); |
| 3831 | if (journal->j_errno) |
| 3832 | ret = journal->j_errno; |
| 3833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3835 | /* otherwise the list is gone, and long since committed */ |
| 3836 | return ret; |
| 3837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3839 | int reiserfs_commit_for_inode(struct inode *inode) |
| 3840 | { |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 3841 | unsigned int id = REISERFS_I(inode)->i_trans_id; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3842 | struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3844 | /* for the whole inode, assume unset id means it was |
| 3845 | * changed in the current transaction. More conservative |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3847 | if (!id || !jl) { |
| 3848 | reiserfs_update_inode_transaction(inode); |
| 3849 | id = REISERFS_I(inode)->i_trans_id; |
| 3850 | /* jl will be updated in __commit_trans_jl */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3852 | |
| 3853 | return __commit_trans_jl(inode, id, jl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3856 | void reiserfs_restore_prepared_buffer(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3857 | struct buffer_head *bh) |
| 3858 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3859 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
| 3860 | PROC_INFO_INC(sb, journal.restore_prepared); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3861 | if (!bh) { |
| 3862 | return; |
| 3863 | } |
| 3864 | if (test_clear_buffer_journal_restore_dirty(bh) && |
| 3865 | buffer_journal_dirty(bh)) { |
| 3866 | struct reiserfs_journal_cnode *cn; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3867 | cn = get_journal_hash_dev(sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3868 | journal->j_list_hash_table, |
| 3869 | bh->b_blocknr); |
| 3870 | if (cn && can_dirty(cn)) { |
| 3871 | set_buffer_journal_test(bh); |
| 3872 | mark_buffer_dirty(bh); |
| 3873 | } |
| 3874 | } |
| 3875 | clear_buffer_journal_prepared(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | } |
| 3877 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3878 | extern struct tree_balance *cur_tb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | /* |
| 3880 | ** before we can change a metadata block, we have to make sure it won't |
| 3881 | ** be written to disk while we are altering it. So, we must: |
| 3882 | ** clean it |
| 3883 | ** wait on it. |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3884 | ** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3886 | int reiserfs_prepare_for_journal(struct super_block *sb, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3887 | struct buffer_head *bh, int wait) |
| 3888 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3889 | PROC_INFO_INC(sb, journal.prepare); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | |
Nick Piggin | ca5de40 | 2008-08-02 12:02:13 +0200 | [diff] [blame] | 3891 | if (!trylock_buffer(bh)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3892 | if (!wait) |
| 3893 | return 0; |
| 3894 | lock_buffer(bh); |
| 3895 | } |
| 3896 | set_buffer_journal_prepared(bh); |
| 3897 | if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) { |
| 3898 | clear_buffer_journal_test(bh); |
| 3899 | set_buffer_journal_restore_dirty(bh); |
| 3900 | } |
| 3901 | unlock_buffer(bh); |
| 3902 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3905 | static void flush_old_journal_lists(struct super_block *s) |
| 3906 | { |
| 3907 | struct reiserfs_journal *journal = SB_JOURNAL(s); |
| 3908 | struct reiserfs_journal_list *jl; |
| 3909 | struct list_head *entry; |
| 3910 | time_t now = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3912 | while (!list_empty(&journal->j_journal_list)) { |
| 3913 | entry = journal->j_journal_list.next; |
| 3914 | jl = JOURNAL_LIST_ENTRY(entry); |
| 3915 | /* this check should always be run, to send old lists to disk */ |
Chris Mason | a317202 | 2006-09-29 01:59:56 -0700 | [diff] [blame] | 3916 | if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4)) && |
| 3917 | atomic_read(&jl->j_commit_left) == 0 && |
| 3918 | test_transaction(s, jl)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3919 | flush_used_journal_lists(s, jl); |
| 3920 | } else { |
| 3921 | break; |
| 3922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | } |
| 3925 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3926 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | ** long and ugly. If flush, will not return until all commit |
| 3928 | ** blocks and all real buffers in the trans are on disk. |
| 3929 | ** If no_async, won't return until all commit blocks are on disk. |
| 3930 | ** |
| 3931 | ** keep reading, there are comments as you go along |
| 3932 | ** |
| 3933 | ** If the journal is aborted, we just clean up. Things like flushing |
| 3934 | ** journal lists, etc just won't happen. |
| 3935 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3936 | static int do_journal_end(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3937 | struct super_block *sb, unsigned long nblocks, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3938 | int flags) |
| 3939 | { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3940 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3941 | struct reiserfs_journal_cnode *cn, *next, *jl_cn; |
| 3942 | struct reiserfs_journal_cnode *last_cn = NULL; |
| 3943 | struct reiserfs_journal_desc *desc; |
| 3944 | struct reiserfs_journal_commit *commit; |
| 3945 | struct buffer_head *c_bh; /* commit bh */ |
| 3946 | struct buffer_head *d_bh; /* desc bh */ |
| 3947 | int cur_write_start = 0; /* start index of current log write */ |
| 3948 | int old_start; |
| 3949 | int i; |
Alexander Zarochentsev | a44c94a | 2006-03-25 03:06:59 -0800 | [diff] [blame] | 3950 | int flush; |
| 3951 | int wait_on_commit; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3952 | struct reiserfs_journal_list *jl, *temp_jl; |
| 3953 | struct list_head *entry, *safe; |
| 3954 | unsigned long jindex; |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 3955 | unsigned int commit_trans_id; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3956 | int trans_half; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3958 | BUG_ON(th->t_refcount > 1); |
| 3959 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | |
Alexander Zarochentsev | a44c94a | 2006-03-25 03:06:59 -0800 | [diff] [blame] | 3961 | /* protect flush_older_commits from doing mistakes if the |
| 3962 | transaction ID counter gets overflowed. */ |
Jeff Mahoney | 600ed41 | 2009-03-30 14:02:17 -0400 | [diff] [blame] | 3963 | if (th->t_trans_id == ~0U) |
Alexander Zarochentsev | a44c94a | 2006-03-25 03:06:59 -0800 | [diff] [blame] | 3964 | flags |= FLUSH_ALL | COMMIT_NOW | WAIT; |
| 3965 | flush = flags & FLUSH_ALL; |
| 3966 | wait_on_commit = flags & WAIT; |
| 3967 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3968 | put_fs_excl(); |
| 3969 | current->journal_info = th->t_handle_save; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3970 | reiserfs_check_lock_depth(sb, "journal end"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3971 | if (journal->j_len == 0) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3972 | reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3973 | 1); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3974 | journal_mark_dirty(th, sb, SB_BUFFER_WITH_SB(sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3977 | lock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3978 | if (journal->j_next_full_flush) { |
| 3979 | flags |= FLUSH_ALL; |
| 3980 | flush = 1; |
| 3981 | } |
| 3982 | if (journal->j_next_async_flush) { |
| 3983 | flags |= COMMIT_NOW | WAIT; |
| 3984 | wait_on_commit = 1; |
| 3985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 3987 | /* check_journal_end locks the journal, and unlocks if it does not return 1 |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3988 | ** it tells us if we should continue with the journal_end, or just return |
| 3989 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 3990 | if (!check_journal_end(th, sb, nblocks, flags)) { |
| 3991 | sb->s_dirt = 1; |
| 3992 | wake_queued_writers(sb); |
| 3993 | reiserfs_async_progress_wait(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3994 | goto out; |
| 3995 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 3997 | /* check_journal_end might set these, check again */ |
| 3998 | if (journal->j_next_full_flush) { |
| 3999 | flush = 1; |
| 4000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4002 | /* |
| 4003 | ** j must wait means we have to flush the log blocks, and the real blocks for |
| 4004 | ** this transaction |
| 4005 | */ |
| 4006 | if (journal->j_must_wait > 0) { |
| 4007 | flush = 1; |
| 4008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | #ifdef REISERFS_PREALLOCATE |
Jan Kara | ef43bc4 | 2006-01-11 12:17:40 -0800 | [diff] [blame] | 4010 | /* quota ops might need to nest, setup the journal_info pointer for them |
| 4011 | * and raise the refcount so that it is > 0. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4012 | current->journal_info = th; |
Jan Kara | ef43bc4 | 2006-01-11 12:17:40 -0800 | [diff] [blame] | 4013 | th->t_refcount++; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4014 | reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into |
| 4015 | * the transaction */ |
Jan Kara | ef43bc4 | 2006-01-11 12:17:40 -0800 | [diff] [blame] | 4016 | th->t_refcount--; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4017 | current->journal_info = th->t_handle_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4020 | /* setup description block */ |
| 4021 | d_bh = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4022 | journal_getblk(sb, |
| 4023 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4024 | journal->j_start); |
| 4025 | set_buffer_uptodate(d_bh); |
| 4026 | desc = (struct reiserfs_journal_desc *)(d_bh)->b_data; |
| 4027 | memset(d_bh->b_data, 0, d_bh->b_size); |
| 4028 | memcpy(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8); |
| 4029 | set_desc_trans_id(desc, journal->j_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4031 | /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4032 | c_bh = journal_getblk(sb, SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4033 | ((journal->j_start + journal->j_len + |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4034 | 1) % SB_ONDISK_JOURNAL_SIZE(sb))); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4035 | commit = (struct reiserfs_journal_commit *)c_bh->b_data; |
| 4036 | memset(c_bh->b_data, 0, c_bh->b_size); |
| 4037 | set_commit_trans_id(commit, journal->j_trans_id); |
| 4038 | set_buffer_uptodate(c_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4040 | /* init this journal list */ |
| 4041 | jl = journal->j_current_jl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4043 | /* we lock the commit before doing anything because |
| 4044 | * we want to make sure nobody tries to run flush_commit_list until |
| 4045 | * the new transaction is fully setup, and we've already flushed the |
| 4046 | * ordered bh list |
| 4047 | */ |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 4048 | reiserfs_mutex_lock_safe(&jl->j_commit_mutex, sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4050 | /* save the transaction id in case we need to commit it later */ |
| 4051 | commit_trans_id = jl->j_trans_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4053 | atomic_set(&jl->j_older_commits_done, 0); |
| 4054 | jl->j_trans_id = journal->j_trans_id; |
| 4055 | jl->j_timestamp = journal->j_trans_start_time; |
| 4056 | jl->j_commit_bh = c_bh; |
| 4057 | jl->j_start = journal->j_start; |
| 4058 | jl->j_len = journal->j_len; |
| 4059 | atomic_set(&jl->j_nonzerolen, journal->j_len); |
| 4060 | atomic_set(&jl->j_commit_left, journal->j_len + 2); |
| 4061 | jl->j_realblock = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4063 | /* The ENTIRE FOR LOOP MUST not cause schedule to occur. |
| 4064 | ** for each real block, add it to the journal list hash, |
| 4065 | ** copy into real block index array in the commit or desc block |
| 4066 | */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4067 | trans_half = journal_trans_half(sb->s_blocksize); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4068 | for (i = 0, cn = journal->j_first; cn; cn = cn->next, i++) { |
| 4069 | if (buffer_journaled(cn->bh)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4070 | jl_cn = get_cnode(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4071 | if (!jl_cn) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4072 | reiserfs_panic(sb, "journal-1676", |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 4073 | "get_cnode returned NULL"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4074 | } |
| 4075 | if (i == 0) { |
| 4076 | jl->j_realblock = jl_cn; |
| 4077 | } |
| 4078 | jl_cn->prev = last_cn; |
| 4079 | jl_cn->next = NULL; |
| 4080 | if (last_cn) { |
| 4081 | last_cn->next = jl_cn; |
| 4082 | } |
| 4083 | last_cn = jl_cn; |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 4084 | /* make sure the block we are trying to log is not a block |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4085 | of journal or reserved area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4086 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4087 | if (is_block_in_log_or_reserved_area |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4088 | (sb, cn->bh->b_blocknr)) { |
| 4089 | reiserfs_panic(sb, "journal-2332", |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 4090 | "Trying to log block %lu, " |
| 4091 | "which is a log block", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4092 | cn->bh->b_blocknr); |
| 4093 | } |
| 4094 | jl_cn->blocknr = cn->bh->b_blocknr; |
| 4095 | jl_cn->state = 0; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4096 | jl_cn->sb = sb; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4097 | jl_cn->bh = cn->bh; |
| 4098 | jl_cn->jlist = jl; |
| 4099 | insert_journal_hash(journal->j_list_hash_table, jl_cn); |
| 4100 | if (i < trans_half) { |
| 4101 | desc->j_realblock[i] = |
| 4102 | cpu_to_le32(cn->bh->b_blocknr); |
| 4103 | } else { |
| 4104 | commit->j_realblock[i - trans_half] = |
| 4105 | cpu_to_le32(cn->bh->b_blocknr); |
| 4106 | } |
| 4107 | } else { |
| 4108 | i--; |
| 4109 | } |
| 4110 | } |
| 4111 | set_desc_trans_len(desc, journal->j_len); |
| 4112 | set_desc_mount_id(desc, journal->j_mount_id); |
| 4113 | set_desc_trans_id(desc, journal->j_trans_id); |
| 4114 | set_commit_trans_len(commit, journal->j_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4116 | /* special check in case all buffers in the journal were marked for not logging */ |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 4117 | BUG_ON(journal->j_len == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4119 | /* we're about to dirty all the log blocks, mark the description block |
| 4120 | * dirty now too. Don't mark the commit block dirty until all the |
| 4121 | * others are on disk |
| 4122 | */ |
| 4123 | mark_buffer_dirty(d_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4125 | /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */ |
| 4126 | cur_write_start = journal->j_start; |
| 4127 | cn = journal->j_first; |
| 4128 | jindex = 1; /* start at one so we don't get the desc again */ |
| 4129 | while (cn) { |
| 4130 | clear_buffer_journal_new(cn->bh); |
| 4131 | /* copy all the real blocks into log area. dirty log blocks */ |
| 4132 | if (buffer_journaled(cn->bh)) { |
| 4133 | struct buffer_head *tmp_bh; |
| 4134 | char *addr; |
| 4135 | struct page *page; |
| 4136 | tmp_bh = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4137 | journal_getblk(sb, |
| 4138 | SB_ONDISK_JOURNAL_1st_BLOCK(sb) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4139 | ((cur_write_start + |
| 4140 | jindex) % |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4141 | SB_ONDISK_JOURNAL_SIZE(sb))); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4142 | set_buffer_uptodate(tmp_bh); |
| 4143 | page = cn->bh->b_page; |
| 4144 | addr = kmap(page); |
| 4145 | memcpy(tmp_bh->b_data, |
| 4146 | addr + offset_in_page(cn->bh->b_data), |
| 4147 | cn->bh->b_size); |
| 4148 | kunmap(page); |
| 4149 | mark_buffer_dirty(tmp_bh); |
| 4150 | jindex++; |
| 4151 | set_buffer_journal_dirty(cn->bh); |
| 4152 | clear_buffer_journaled(cn->bh); |
| 4153 | } else { |
| 4154 | /* JDirty cleared sometime during transaction. don't log this one */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4155 | reiserfs_warning(sb, "journal-2048", |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 4156 | "BAD, buffer in journal hash, " |
| 4157 | "but not JDirty!"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4158 | brelse(cn->bh); |
| 4159 | } |
| 4160 | next = cn->next; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4161 | free_cnode(sb, cn); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4162 | cn = next; |
Frederic Weisbecker | e6950a4 | 2009-04-30 23:04:32 +0200 | [diff] [blame] | 4163 | reiserfs_write_unlock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4164 | cond_resched(); |
Frederic Weisbecker | e6950a4 | 2009-04-30 23:04:32 +0200 | [diff] [blame] | 4165 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4168 | /* we are done with both the c_bh and d_bh, but |
| 4169 | ** c_bh must be written after all other commit blocks, |
| 4170 | ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1. |
| 4171 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4173 | journal->j_current_jl = alloc_journal_list(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4175 | /* now it is safe to insert this transaction on the main list */ |
| 4176 | list_add_tail(&jl->j_list, &journal->j_journal_list); |
| 4177 | list_add_tail(&jl->j_working_list, &journal->j_working_list); |
| 4178 | journal->j_num_work_lists++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4180 | /* reset journal values for the next transaction */ |
| 4181 | old_start = journal->j_start; |
| 4182 | journal->j_start = |
| 4183 | (journal->j_start + journal->j_len + |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4184 | 2) % SB_ONDISK_JOURNAL_SIZE(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4185 | atomic_set(&(journal->j_wcount), 0); |
| 4186 | journal->j_bcount = 0; |
| 4187 | journal->j_last = NULL; |
| 4188 | journal->j_first = NULL; |
| 4189 | journal->j_len = 0; |
| 4190 | journal->j_trans_start_time = 0; |
Alexander Zarochentsev | a44c94a | 2006-03-25 03:06:59 -0800 | [diff] [blame] | 4191 | /* check for trans_id overflow */ |
| 4192 | if (++journal->j_trans_id == 0) |
| 4193 | journal->j_trans_id = 10; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4194 | journal->j_current_jl->j_trans_id = journal->j_trans_id; |
| 4195 | journal->j_must_wait = 0; |
| 4196 | journal->j_len_alloc = 0; |
| 4197 | journal->j_next_full_flush = 0; |
| 4198 | journal->j_next_async_flush = 0; |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4199 | init_journal_hash(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4201 | // make sure reiserfs_add_jh sees the new current_jl before we |
| 4202 | // write out the tails |
| 4203 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4205 | /* tail conversion targets have to hit the disk before we end the |
| 4206 | * transaction. Otherwise a later transaction might repack the tail |
| 4207 | * before this transaction commits, leaving the data block unflushed and |
| 4208 | * clean, if we crash before the later transaction commits, the data block |
| 4209 | * is lost. |
| 4210 | */ |
| 4211 | if (!list_empty(&jl->j_tail_bh_list)) { |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 4212 | reiserfs_write_unlock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4213 | write_ordered_buffers(&journal->j_dirty_buffers_lock, |
| 4214 | journal, jl, &jl->j_tail_bh_list); |
Frederic Weisbecker | 8ebc423 | 2009-04-07 04:19:49 +0200 | [diff] [blame] | 4215 | reiserfs_write_lock(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4216 | } |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 4217 | BUG_ON(!list_empty(&jl->j_tail_bh_list)); |
Jeff Mahoney | 90415de | 2008-07-25 01:46:40 -0700 | [diff] [blame] | 4218 | mutex_unlock(&jl->j_commit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4220 | /* honor the flush wishes from the caller, simple commits can |
| 4221 | ** be done outside the journal lock, they are done below |
| 4222 | ** |
| 4223 | ** if we don't flush the commit list right now, we put it into |
| 4224 | ** the work queue so the people waiting on the async progress work |
| 4225 | ** queue don't wait for this proc to flush journal lists and such. |
| 4226 | */ |
| 4227 | if (flush) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4228 | flush_commit_list(sb, jl, 1); |
| 4229 | flush_journal_list(sb, jl, 1); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4230 | } else if (!(jl->j_state & LIST_COMMIT_PENDING)) |
| 4231 | queue_delayed_work(commit_wq, &journal->j_work, HZ / 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | |
Jeff Mahoney | 0222e65 | 2009-03-30 14:02:44 -0400 | [diff] [blame] | 4233 | /* if the next transaction has any chance of wrapping, flush |
| 4234 | ** transactions that might get overwritten. If any journal lists are very |
| 4235 | ** old flush them as well. |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4236 | */ |
| 4237 | first_jl: |
| 4238 | list_for_each_safe(entry, safe, &journal->j_journal_list) { |
| 4239 | temp_jl = JOURNAL_LIST_ENTRY(entry); |
| 4240 | if (journal->j_start <= temp_jl->j_start) { |
| 4241 | if ((journal->j_start + journal->j_trans_max + 1) >= |
| 4242 | temp_jl->j_start) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4243 | flush_used_journal_lists(sb, temp_jl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4244 | goto first_jl; |
| 4245 | } else if ((journal->j_start + |
| 4246 | journal->j_trans_max + 1) < |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4247 | SB_ONDISK_JOURNAL_SIZE(sb)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4248 | /* if we don't cross into the next transaction and we don't |
| 4249 | * wrap, there is no way we can overlap any later transactions |
| 4250 | * break now |
| 4251 | */ |
| 4252 | break; |
| 4253 | } |
| 4254 | } else if ((journal->j_start + |
| 4255 | journal->j_trans_max + 1) > |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4256 | SB_ONDISK_JOURNAL_SIZE(sb)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4257 | if (((journal->j_start + journal->j_trans_max + 1) % |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4258 | SB_ONDISK_JOURNAL_SIZE(sb)) >= |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4259 | temp_jl->j_start) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4260 | flush_used_journal_lists(sb, temp_jl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4261 | goto first_jl; |
| 4262 | } else { |
| 4263 | /* we don't overlap anything from out start to the end of the |
| 4264 | * log, and our wrapped portion doesn't overlap anything at |
| 4265 | * the start of the log. We can break |
| 4266 | */ |
| 4267 | break; |
| 4268 | } |
| 4269 | } |
| 4270 | } |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4271 | flush_old_journal_lists(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4273 | journal->j_current_jl->j_list_bitmap = |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4274 | get_list_bitmap(sb, journal->j_current_jl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4276 | if (!(journal->j_current_jl->j_list_bitmap)) { |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4277 | reiserfs_panic(sb, "journal-1996", |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 4278 | "could not get a list bitmap"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4281 | atomic_set(&(journal->j_jlock), 0); |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4282 | unlock_journal(sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4283 | /* wake up any body waiting to join. */ |
| 4284 | clear_bit(J_WRITERS_QUEUED, &journal->j_state); |
| 4285 | wake_up(&(journal->j_join_wait)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4287 | if (!flush && wait_on_commit && |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4288 | journal_list_still_alive(sb, commit_trans_id)) { |
| 4289 | flush_commit_list(sb, jl, 1); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4290 | } |
| 4291 | out: |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4292 | reiserfs_check_lock_depth(sb, "journal end2"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4294 | memset(th, 0, sizeof(*th)); |
| 4295 | /* Re-set th->t_super, so we can properly keep track of how many |
| 4296 | * persistent transactions there are. We need to do this so if this |
| 4297 | * call is part of a failed restart_transaction, we can free it later */ |
Jeff Mahoney | a9dd364 | 2009-03-30 14:02:45 -0400 | [diff] [blame] | 4298 | th->t_super = sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4300 | return journal->j_errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | } |
| 4302 | |
Jeff Mahoney | 32e8b10 | 2009-03-30 14:02:26 -0400 | [diff] [blame] | 4303 | /* Send the file system read only and refuse new transactions */ |
| 4304 | void reiserfs_abort_journal(struct super_block *sb, int errno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4306 | struct reiserfs_journal *journal = SB_JOURNAL(sb); |
| 4307 | if (test_bit(J_ABORTED, &journal->j_state)) |
| 4308 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | |
Jeff Mahoney | 32e8b10 | 2009-03-30 14:02:26 -0400 | [diff] [blame] | 4310 | if (!journal->j_errno) |
| 4311 | journal->j_errno = errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4313 | sb->s_flags |= MS_RDONLY; |
| 4314 | set_bit(J_ABORTED, &journal->j_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | |
| 4316 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 4317 | dump_stack(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | #endif |
| 4319 | } |
| 4320 | |