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