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