]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/reiserfs/journal.c
8139cp: fix checksum broken
[net-next-2.6.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
CH
2554 if (journal->j_dev_bd != NULL) {
2555 if (journal->j_dev_bd->bd_dev != super->s_dev)
2556 bd_release(journal->j_dev_bd);
e5eb8caa 2557 result = blkdev_put(journal->j_dev_bd, journal->j_dev_mode);
bd4c625c
LT
2558 journal->j_dev_bd = NULL;
2559 }
2560
2561 if (result != 0) {
45b03d5e
JM
2562 reiserfs_warning(super, "sh-457",
2563 "Cannot release journal device: %i", result);
bd4c625c
LT
2564 }
2565 return result;
2566}
2567
2568static int journal_init_dev(struct super_block *super,
2569 struct reiserfs_journal *journal,
2570 const char *jdev_name)
1da177e4
LT
2571{
2572 int result;
2573 dev_t jdev;
aeb5d727 2574 fmode_t blkdev_mode = FMODE_READ | FMODE_WRITE;
1da177e4
LT
2575 char b[BDEVNAME_SIZE];
2576
2577 result = 0;
2578
bd4c625c 2579 journal->j_dev_bd = NULL;
bd4c625c
LT
2580 jdev = SB_ONDISK_JOURNAL_DEVICE(super) ?
2581 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
1da177e4
LT
2582
2583 if (bdev_read_only(super->s_bdev))
bd4c625c 2584 blkdev_mode = FMODE_READ;
1da177e4
LT
2585
2586 /* there is no "jdev" option and journal is on separate device */
bd4c625c 2587 if ((!jdev_name || !jdev_name[0])) {
1da177e4 2588 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode);
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;
86098fa0
CH
2597 } else if (jdev != super->s_dev) {
2598 result = bd_claim(journal->j_dev_bd, journal);
2599 if (result) {
9a1c3542 2600 blkdev_put(journal->j_dev_bd, blkdev_mode);
86098fa0
CH
2601 return result;
2602 }
2603
1da177e4 2604 set_blocksize(journal->j_dev_bd, super->s_blocksize);
86098fa0
CH
2605 }
2606
1da177e4
LT
2607 return 0;
2608 }
2609
e5eb8caa 2610 journal->j_dev_mode = blkdev_mode;
30c40d2c 2611 journal->j_dev_bd = open_bdev_exclusive(jdev_name,
e5eb8caa 2612 blkdev_mode, journal);
86098fa0
CH
2613 if (IS_ERR(journal->j_dev_bd)) {
2614 result = PTR_ERR(journal->j_dev_bd);
2615 journal->j_dev_bd = NULL;
bd4c625c
LT
2616 reiserfs_warning(super,
2617 "journal_init_dev: Cannot open '%s': %i",
2618 jdev_name, result);
86098fa0 2619 return result;
1da177e4 2620 }
86098fa0
CH
2621
2622 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2623 reiserfs_info(super,
2624 "journal_init_dev: journal device: %s\n",
2625 bdevname(journal->j_dev_bd, b));
2626 return 0;
1da177e4
LT
2627}
2628
cf3d0b81
ES
2629/**
2630 * When creating/tuning a file system user can assign some
2631 * journal params within boundaries which depend on the ratio
2632 * blocksize/standard_blocksize.
2633 *
2634 * For blocks >= standard_blocksize transaction size should
2635 * be not less then JOURNAL_TRANS_MIN_DEFAULT, and not more
2636 * then JOURNAL_TRANS_MAX_DEFAULT.
2637 *
2638 * For blocks < standard_blocksize these boundaries should be
2639 * decreased proportionally.
2640 */
2641#define REISERFS_STANDARD_BLKSIZE (4096)
2642
a9dd3643 2643static int check_advise_trans_params(struct super_block *sb,
cf3d0b81
ES
2644 struct reiserfs_journal *journal)
2645{
2646 if (journal->j_trans_max) {
2647 /* Non-default journal params.
2648 Do sanity check for them. */
2649 int ratio = 1;
a9dd3643
JM
2650 if (sb->s_blocksize < REISERFS_STANDARD_BLKSIZE)
2651 ratio = REISERFS_STANDARD_BLKSIZE / sb->s_blocksize;
cf3d0b81
ES
2652
2653 if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio ||
2654 journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio ||
a9dd3643 2655 SB_ONDISK_JOURNAL_SIZE(sb) / journal->j_trans_max <
cf3d0b81 2656 JOURNAL_MIN_RATIO) {
a9dd3643 2657 reiserfs_warning(sb, "sh-462",
45b03d5e
JM
2658 "bad transaction max size (%u). "
2659 "FSCK?", journal->j_trans_max);
cf3d0b81
ES
2660 return 1;
2661 }
2662 if (journal->j_max_batch != (journal->j_trans_max) *
2663 JOURNAL_MAX_BATCH_DEFAULT/JOURNAL_TRANS_MAX_DEFAULT) {
a9dd3643 2664 reiserfs_warning(sb, "sh-463",
45b03d5e
JM
2665 "bad transaction max batch (%u). "
2666 "FSCK?", journal->j_max_batch);
cf3d0b81
ES
2667 return 1;
2668 }
2669 } else {
2670 /* Default journal params.
2671 The file system was created by old version
2672 of mkreiserfs, so some fields contain zeros,
2673 and we need to advise proper values for them */
a9dd3643
JM
2674 if (sb->s_blocksize != REISERFS_STANDARD_BLKSIZE) {
2675 reiserfs_warning(sb, "sh-464", "bad blocksize (%u)",
2676 sb->s_blocksize);
45b03d5e
JM
2677 return 1;
2678 }
cf3d0b81
ES
2679 journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT;
2680 journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT;
2681 journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE;
2682 }
2683 return 0;
2684}
2685
1da177e4
LT
2686/*
2687** must be called once on fs mount. calls journal_read for you
2688*/
a9dd3643 2689int journal_init(struct super_block *sb, const char *j_dev_name,
bd4c625c
LT
2690 int old_format, unsigned int commit_max_age)
2691{
a9dd3643 2692 int num_cnodes = SB_ONDISK_JOURNAL_SIZE(sb) * 2;
bd4c625c
LT
2693 struct buffer_head *bhjh;
2694 struct reiserfs_super_block *rs;
2695 struct reiserfs_journal_header *jh;
2696 struct reiserfs_journal *journal;
2697 struct reiserfs_journal_list *jl;
2698 char b[BDEVNAME_SIZE];
98ea3f50 2699 int ret;
bd4c625c 2700
98ea3f50
FW
2701 /*
2702 * Unlock here to avoid various RECLAIM-FS-ON <-> IN-RECLAIM-FS
2703 * dependency inversion warnings.
2704 */
2705 reiserfs_write_unlock(sb);
a9dd3643 2706 journal = SB_JOURNAL(sb) = vmalloc(sizeof(struct reiserfs_journal));
bd4c625c 2707 if (!journal) {
a9dd3643 2708 reiserfs_warning(sb, "journal-1256",
45b03d5e 2709 "unable to get memory for journal structure");
98ea3f50 2710 reiserfs_write_lock(sb);
bd4c625c
LT
2711 return 1;
2712 }
2713 memset(journal, 0, sizeof(struct reiserfs_journal));
2714 INIT_LIST_HEAD(&journal->j_bitmap_nodes);
2715 INIT_LIST_HEAD(&journal->j_prealloc_list);
2716 INIT_LIST_HEAD(&journal->j_working_list);
2717 INIT_LIST_HEAD(&journal->j_journal_list);
2718 journal->j_persistent_trans = 0;
98ea3f50
FW
2719 ret = reiserfs_allocate_list_bitmaps(sb, journal->j_list_bitmap,
2720 reiserfs_bmap_count(sb));
2721 reiserfs_write_lock(sb);
2722 if (ret)
bd4c625c 2723 goto free_and_return;
98ea3f50 2724
a9dd3643 2725 allocate_bitmap_nodes(sb);
bd4c625c
LT
2726
2727 /* reserved for journal area support */
a9dd3643 2728 SB_JOURNAL_1st_RESERVED_BLOCK(sb) = (old_format ?
bd4c625c 2729 REISERFS_OLD_DISK_OFFSET_IN_BYTES
a9dd3643
JM
2730 / sb->s_blocksize +
2731 reiserfs_bmap_count(sb) +
bd4c625c
LT
2732 1 :
2733 REISERFS_DISK_OFFSET_IN_BYTES /
a9dd3643 2734 sb->s_blocksize + 2);
bd4c625c
LT
2735
2736 /* Sanity check to see is the standard journal fitting withing first bitmap
2737 (actual for small blocksizes) */
a9dd3643
JM
2738 if (!SB_ONDISK_JOURNAL_DEVICE(sb) &&
2739 (SB_JOURNAL_1st_RESERVED_BLOCK(sb) +
2740 SB_ONDISK_JOURNAL_SIZE(sb) > sb->s_blocksize * 8)) {
2741 reiserfs_warning(sb, "journal-1393",
45b03d5e
JM
2742 "journal does not fit for area addressed "
2743 "by first of bitmap blocks. It starts at "
bd4c625c 2744 "%u and its size is %u. Block size %ld",
a9dd3643
JM
2745 SB_JOURNAL_1st_RESERVED_BLOCK(sb),
2746 SB_ONDISK_JOURNAL_SIZE(sb),
2747 sb->s_blocksize);
bd4c625c
LT
2748 goto free_and_return;
2749 }
2750
193be0ee
FW
2751 /*
2752 * We need to unlock here to avoid creating the following
2753 * dependency:
2754 * reiserfs_lock -> sysfs_mutex
2755 * Because the reiserfs mmap path creates the following dependency:
2756 * mm->mmap -> reiserfs_lock, hence we have
2757 * mm->mmap -> reiserfs_lock ->sysfs_mutex
2758 * This would ends up in a circular dependency with sysfs readdir path
2759 * which does sysfs_mutex -> mm->mmap_sem
2760 * This is fine because the reiserfs lock is useless in mount path,
2761 * at least until we call journal_begin. We keep it for paranoid
2762 * reasons.
2763 */
2764 reiserfs_write_unlock(sb);
a9dd3643 2765 if (journal_init_dev(sb, journal, j_dev_name) != 0) {
193be0ee 2766 reiserfs_write_lock(sb);
a9dd3643 2767 reiserfs_warning(sb, "sh-462",
45b03d5e 2768 "unable to initialize jornal device");
bd4c625c
LT
2769 goto free_and_return;
2770 }
193be0ee 2771 reiserfs_write_lock(sb);
bd4c625c 2772
a9dd3643 2773 rs = SB_DISK_SUPER_BLOCK(sb);
bd4c625c
LT
2774
2775 /* read journal header */
a9dd3643
JM
2776 bhjh = journal_bread(sb,
2777 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
2778 SB_ONDISK_JOURNAL_SIZE(sb));
bd4c625c 2779 if (!bhjh) {
a9dd3643 2780 reiserfs_warning(sb, "sh-459",
45b03d5e 2781 "unable to read journal header");
bd4c625c
LT
2782 goto free_and_return;
2783 }
2784 jh = (struct reiserfs_journal_header *)(bhjh->b_data);
2785
2786 /* make sure that journal matches to the super block */
2787 if (is_reiserfs_jr(rs)
2788 && (le32_to_cpu(jh->jh_journal.jp_journal_magic) !=
2789 sb_jp_journal_magic(rs))) {
a9dd3643 2790 reiserfs_warning(sb, "sh-460",
45b03d5e
JM
2791 "journal header magic %x (device %s) does "
2792 "not match to magic found in super block %x",
2793 jh->jh_journal.jp_journal_magic,
bd4c625c
LT
2794 bdevname(journal->j_dev_bd, b),
2795 sb_jp_journal_magic(rs));
2796 brelse(bhjh);
2797 goto free_and_return;
2798 }
2799
2800 journal->j_trans_max = le32_to_cpu(jh->jh_journal.jp_journal_trans_max);
2801 journal->j_max_batch = le32_to_cpu(jh->jh_journal.jp_journal_max_batch);
2802 journal->j_max_commit_age =
2803 le32_to_cpu(jh->jh_journal.jp_journal_max_commit_age);
2804 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
2805
a9dd3643 2806 if (check_advise_trans_params(sb, journal) != 0)
cf3d0b81 2807 goto free_and_return;
bd4c625c
LT
2808 journal->j_default_max_commit_age = journal->j_max_commit_age;
2809
2810 if (commit_max_age != 0) {
2811 journal->j_max_commit_age = commit_max_age;
2812 journal->j_max_trans_age = commit_max_age;
2813 }
2814
a9dd3643 2815 reiserfs_info(sb, "journal params: device %s, size %u, "
bd4c625c
LT
2816 "journal first block %u, max trans len %u, max batch %u, "
2817 "max commit age %u, max trans age %u\n",
2818 bdevname(journal->j_dev_bd, b),
a9dd3643
JM
2819 SB_ONDISK_JOURNAL_SIZE(sb),
2820 SB_ONDISK_JOURNAL_1st_BLOCK(sb),
bd4c625c
LT
2821 journal->j_trans_max,
2822 journal->j_max_batch,
2823 journal->j_max_commit_age, journal->j_max_trans_age);
2824
2825 brelse(bhjh);
2826
2827 journal->j_list_bitmap_index = 0;
a9dd3643 2828 journal_list_init(sb);
bd4c625c
LT
2829
2830 memset(journal->j_list_hash_table, 0,
2831 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
2832
2833 INIT_LIST_HEAD(&journal->j_dirty_buffers);
2834 spin_lock_init(&journal->j_dirty_buffers_lock);
2835
2836 journal->j_start = 0;
2837 journal->j_len = 0;
2838 journal->j_len_alloc = 0;
2839 atomic_set(&(journal->j_wcount), 0);
2840 atomic_set(&(journal->j_async_throttle), 0);
2841 journal->j_bcount = 0;
2842 journal->j_trans_start_time = 0;
2843 journal->j_last = NULL;
2844 journal->j_first = NULL;
2845 init_waitqueue_head(&(journal->j_join_wait));
f68215c4 2846 mutex_init(&journal->j_mutex);
afe70259 2847 mutex_init(&journal->j_flush_mutex);
bd4c625c
LT
2848
2849 journal->j_trans_id = 10;
2850 journal->j_mount_id = 10;
2851 journal->j_state = 0;
2852 atomic_set(&(journal->j_jlock), 0);
bbec9191 2853 reiserfs_write_unlock(sb);
bd4c625c 2854 journal->j_cnode_free_list = allocate_cnodes(num_cnodes);
bbec9191 2855 reiserfs_write_lock(sb);
bd4c625c
LT
2856 journal->j_cnode_free_orig = journal->j_cnode_free_list;
2857 journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0;
2858 journal->j_cnode_used = 0;
2859 journal->j_must_wait = 0;
2860
576f6d79 2861 if (journal->j_cnode_free == 0) {
a9dd3643 2862 reiserfs_warning(sb, "journal-2004", "Journal cnode memory "
576f6d79
JM
2863 "allocation failed (%ld bytes). Journal is "
2864 "too large for available memory. Usually "
2865 "this is due to a journal that is too large.",
2866 sizeof (struct reiserfs_journal_cnode) * num_cnodes);
2867 goto free_and_return;
2868 }
2869
a9dd3643 2870 init_journal_hash(sb);
bd4c625c 2871 jl = journal->j_current_jl;
a9dd3643 2872 jl->j_list_bitmap = get_list_bitmap(sb, jl);
bd4c625c 2873 if (!jl->j_list_bitmap) {
a9dd3643 2874 reiserfs_warning(sb, "journal-2005",
45b03d5e 2875 "get_list_bitmap failed for journal list 0");
bd4c625c
LT
2876 goto free_and_return;
2877 }
a9dd3643
JM
2878 if (journal_read(sb) < 0) {
2879 reiserfs_warning(sb, "reiserfs-2006",
45b03d5e 2880 "Replay Failure, unable to mount");
bd4c625c
LT
2881 goto free_and_return;
2882 }
2883
2884 reiserfs_mounted_fs_count++;
48f6ba5e
FW
2885 if (reiserfs_mounted_fs_count <= 1) {
2886 reiserfs_write_unlock(sb);
bd4c625c 2887 commit_wq = create_workqueue("reiserfs");
48f6ba5e
FW
2888 reiserfs_write_lock(sb);
2889 }
bd4c625c 2890
c4028958 2891 INIT_DELAYED_WORK(&journal->j_work, flush_async_commits);
a9dd3643 2892 journal->j_work_sb = sb;
bd4c625c
LT
2893 return 0;
2894 free_and_return:
a9dd3643 2895 free_journal_ram(sb);
bd4c625c 2896 return 1;
1da177e4
LT
2897}
2898
2899/*
2900** test for a polite end of the current transaction. Used by file_write, and should
2901** be used by delete to make sure they don't write more than can fit inside a single
2902** transaction
2903*/
bd4c625c
LT
2904int journal_transaction_should_end(struct reiserfs_transaction_handle *th,
2905 int new_alloc)
2906{
2907 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2908 time_t now = get_seconds();
2909 /* cannot restart while nested */
2910 BUG_ON(!th->t_trans_id);
2911 if (th->t_refcount > 1)
2912 return 0;
2913 if (journal->j_must_wait > 0 ||
2914 (journal->j_len_alloc + new_alloc) >= journal->j_max_batch ||
2915 atomic_read(&(journal->j_jlock)) ||
2916 (now - journal->j_trans_start_time) > journal->j_max_trans_age ||
2917 journal->j_cnode_free < (journal->j_trans_max * 3)) {
2918 return 1;
2919 }
6ae1ea44
CM
2920 /* protected by the BKL here */
2921 journal->j_len_alloc += new_alloc;
2922 th->t_blocks_allocated += new_alloc ;
bd4c625c 2923 return 0;
1da177e4
LT
2924}
2925
0222e657 2926/* this must be called inside a transaction, and requires the
1da177e4
LT
2927** kernel_lock to be held
2928*/
bd4c625c
LT
2929void reiserfs_block_writes(struct reiserfs_transaction_handle *th)
2930{
2931 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2932 BUG_ON(!th->t_trans_id);
2933 journal->j_must_wait = 1;
2934 set_bit(J_WRITERS_BLOCKED, &journal->j_state);
2935 return;
1da177e4
LT
2936}
2937
2938/* this must be called without a transaction started, and does not
2939** require BKL
2940*/
bd4c625c
LT
2941void reiserfs_allow_writes(struct super_block *s)
2942{
2943 struct reiserfs_journal *journal = SB_JOURNAL(s);
2944 clear_bit(J_WRITERS_BLOCKED, &journal->j_state);
2945 wake_up(&journal->j_join_wait);
1da177e4
LT
2946}
2947
2948/* this must be called without a transaction started, and does not
2949** require BKL
2950*/
bd4c625c
LT
2951void reiserfs_wait_on_write_block(struct super_block *s)
2952{
2953 struct reiserfs_journal *journal = SB_JOURNAL(s);
2954 wait_event(journal->j_join_wait,
2955 !test_bit(J_WRITERS_BLOCKED, &journal->j_state));
2956}
2957
2958static void queue_log_writer(struct super_block *s)
2959{
2960 wait_queue_t wait;
2961 struct reiserfs_journal *journal = SB_JOURNAL(s);
2962 set_bit(J_WRITERS_QUEUED, &journal->j_state);
2963
2964 /*
2965 * we don't want to use wait_event here because
2966 * we only want to wait once.
2967 */
2968 init_waitqueue_entry(&wait, current);
2969 add_wait_queue(&journal->j_join_wait, &wait);
1da177e4 2970 set_current_state(TASK_UNINTERRUPTIBLE);
8ebc4232
FW
2971 if (test_bit(J_WRITERS_QUEUED, &journal->j_state)) {
2972 reiserfs_write_unlock(s);
bd4c625c 2973 schedule();
8ebc4232
FW
2974 reiserfs_write_lock(s);
2975 }
5ab2f7e0 2976 __set_current_state(TASK_RUNNING);
bd4c625c
LT
2977 remove_wait_queue(&journal->j_join_wait, &wait);
2978}
2979
2980static void wake_queued_writers(struct super_block *s)
2981{
2982 struct reiserfs_journal *journal = SB_JOURNAL(s);
2983 if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state))
2984 wake_up(&journal->j_join_wait);
2985}
2986
600ed416 2987static void let_transaction_grow(struct super_block *sb, unsigned int trans_id)
bd4c625c
LT
2988{
2989 struct reiserfs_journal *journal = SB_JOURNAL(sb);
2990 unsigned long bcount = journal->j_bcount;
2991 while (1) {
8ebc4232 2992 reiserfs_write_unlock(sb);
041e0e3b 2993 schedule_timeout_uninterruptible(1);
8ebc4232 2994 reiserfs_write_lock(sb);
bd4c625c
LT
2995 journal->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2996 while ((atomic_read(&journal->j_wcount) > 0 ||
2997 atomic_read(&journal->j_jlock)) &&
2998 journal->j_trans_id == trans_id) {
2999 queue_log_writer(sb);
3000 }
3001 if (journal->j_trans_id != trans_id)
3002 break;
3003 if (bcount == journal->j_bcount)
3004 break;
3005 bcount = journal->j_bcount;
1da177e4 3006 }
1da177e4
LT
3007}
3008
3009/* join == true if you must join an existing transaction.
3010** join == false if you can deal with waiting for others to finish
3011**
3012** this will block until the transaction is joinable. send the number of blocks you
3013** expect to use in nblocks.
3014*/
bd4c625c 3015static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
a9dd3643 3016 struct super_block *sb, unsigned long nblocks,
bd4c625c
LT
3017 int join)
3018{
3019 time_t now = get_seconds();
600ed416 3020 unsigned int old_trans_id;
a9dd3643 3021 struct reiserfs_journal *journal = SB_JOURNAL(sb);
bd4c625c
LT
3022 struct reiserfs_transaction_handle myth;
3023 int sched_count = 0;
3024 int retval;
3025
a9dd3643 3026 reiserfs_check_lock_depth(sb, "journal_begin");
14a61442 3027 BUG_ON(nblocks > journal->j_trans_max);
bd4c625c 3028
a9dd3643 3029 PROC_INFO_INC(sb, journal.journal_being);
bd4c625c
LT
3030 /* set here for journal_join */
3031 th->t_refcount = 1;
a9dd3643 3032 th->t_super = sb;
bd4c625c
LT
3033
3034 relock:
a9dd3643 3035 lock_journal(sb);
bd4c625c 3036 if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted(journal)) {
a9dd3643 3037 unlock_journal(sb);
bd4c625c
LT
3038 retval = journal->j_errno;
3039 goto out_fail;
3040 }
3041 journal->j_bcount++;
3042
3043 if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) {
a9dd3643 3044 unlock_journal(sb);
8ebc4232 3045 reiserfs_write_unlock(sb);
a9dd3643 3046 reiserfs_wait_on_write_block(sb);
8ebc4232 3047 reiserfs_write_lock(sb);
a9dd3643 3048 PROC_INFO_INC(sb, journal.journal_relock_writers);
bd4c625c
LT
3049 goto relock;
3050 }
3051 now = get_seconds();
3052
3053 /* if there is no room in the journal OR
0222e657 3054 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
bd4c625c
LT
3055 ** we don't sleep if there aren't other writers
3056 */
3057
3058 if ((!join && journal->j_must_wait > 0) ||
3059 (!join
3060 && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch)
3061 || (!join && atomic_read(&journal->j_wcount) > 0
3062 && journal->j_trans_start_time > 0
3063 && (now - journal->j_trans_start_time) >
3064 journal->j_max_trans_age) || (!join
3065 && atomic_read(&journal->j_jlock))
3066 || (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) {
3067
3068 old_trans_id = journal->j_trans_id;
a9dd3643 3069 unlock_journal(sb); /* allow others to finish this transaction */
bd4c625c
LT
3070
3071 if (!join && (journal->j_len_alloc + nblocks + 2) >=
3072 journal->j_max_batch &&
3073 ((journal->j_len + nblocks + 2) * 100) <
3074 (journal->j_len_alloc * 75)) {
3075 if (atomic_read(&journal->j_wcount) > 10) {
3076 sched_count++;
a9dd3643 3077 queue_log_writer(sb);
bd4c625c
LT
3078 goto relock;
3079 }
3080 }
3081 /* don't mess with joining the transaction if all we have to do is
3082 * wait for someone else to do a commit
3083 */
3084 if (atomic_read(&journal->j_jlock)) {
3085 while (journal->j_trans_id == old_trans_id &&
3086 atomic_read(&journal->j_jlock)) {
a9dd3643 3087 queue_log_writer(sb);
bd4c625c
LT
3088 }
3089 goto relock;
3090 }
a9dd3643 3091 retval = journal_join(&myth, sb, 1);
bd4c625c
LT
3092 if (retval)
3093 goto out_fail;
3094
3095 /* someone might have ended the transaction while we joined */
3096 if (old_trans_id != journal->j_trans_id) {
a9dd3643 3097 retval = do_journal_end(&myth, sb, 1, 0);
bd4c625c 3098 } else {
a9dd3643 3099 retval = do_journal_end(&myth, sb, 1, COMMIT_NOW);
bd4c625c
LT
3100 }
3101
3102 if (retval)
3103 goto out_fail;
3104
a9dd3643 3105 PROC_INFO_INC(sb, journal.journal_relock_wcount);
bd4c625c
LT
3106 goto relock;
3107 }
3108 /* we are the first writer, set trans_id */
3109 if (journal->j_trans_start_time == 0) {
3110 journal->j_trans_start_time = get_seconds();
3111 }
3112 atomic_inc(&(journal->j_wcount));
3113 journal->j_len_alloc += nblocks;
3114 th->t_blocks_logged = 0;
3115 th->t_blocks_allocated = nblocks;
3116 th->t_trans_id = journal->j_trans_id;
a9dd3643 3117 unlock_journal(sb);
bd4c625c
LT
3118 INIT_LIST_HEAD(&th->t_list);
3119 get_fs_excl();
3120 return 0;
3121
3122 out_fail:
3123 memset(th, 0, sizeof(*th));
3124 /* Re-set th->t_super, so we can properly keep track of how many
3125 * persistent transactions there are. We need to do this so if this
3126 * call is part of a failed restart_transaction, we can free it later */
a9dd3643 3127 th->t_super = sb;
bd4c625c
LT
3128 return retval;
3129}
3130
3131struct reiserfs_transaction_handle *reiserfs_persistent_transaction(struct
3132 super_block
3133 *s,
3134 int nblocks)
3135{
3136 int ret;
3137 struct reiserfs_transaction_handle *th;
3138
3139 /* if we're nesting into an existing transaction. It will be
3140 ** persistent on its own
3141 */
3142 if (reiserfs_transaction_running(s)) {
3143 th = current->journal_info;
3144 th->t_refcount++;
14a61442
ES
3145 BUG_ON(th->t_refcount < 2);
3146
bd4c625c
LT
3147 return th;
3148 }
d739b42b 3149 th = kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS);
bd4c625c
LT
3150 if (!th)
3151 return NULL;
3152 ret = journal_begin(th, s, nblocks);
3153 if (ret) {
d739b42b 3154 kfree(th);
bd4c625c
LT
3155 return NULL;
3156 }
3157
3158 SB_JOURNAL(s)->j_persistent_trans++;
3159 return th;
3160}
3161
3162int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th)
3163{
3164 struct super_block *s = th->t_super;
3165 int ret = 0;
3166 if (th->t_trans_id)
3167 ret = journal_end(th, th->t_super, th->t_blocks_allocated);
3168 else
3169 ret = -EIO;
3170 if (th->t_refcount == 0) {
3171 SB_JOURNAL(s)->j_persistent_trans--;
d739b42b 3172 kfree(th);
bd4c625c
LT
3173 }
3174 return ret;
3175}
3176
3177static int journal_join(struct reiserfs_transaction_handle *th,
a9dd3643 3178 struct super_block *sb, unsigned long nblocks)
bd4c625c
LT
3179{
3180 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3181
3182 /* this keeps do_journal_end from NULLing out the current->journal_info
3183 ** pointer
3184 */
3185 th->t_handle_save = cur_th;
14a61442 3186 BUG_ON(cur_th && cur_th->t_refcount > 1);
a9dd3643 3187 return do_journal_begin_r(th, sb, nblocks, JBEGIN_JOIN);
bd4c625c
LT
3188}
3189
3190int journal_join_abort(struct reiserfs_transaction_handle *th,
a9dd3643 3191 struct super_block *sb, unsigned long nblocks)
bd4c625c
LT
3192{
3193 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3194
3195 /* this keeps do_journal_end from NULLing out the current->journal_info
3196 ** pointer
3197 */
3198 th->t_handle_save = cur_th;
14a61442 3199 BUG_ON(cur_th && cur_th->t_refcount > 1);
a9dd3643 3200 return do_journal_begin_r(th, sb, nblocks, JBEGIN_ABORT);
bd4c625c
LT
3201}
3202
3203int journal_begin(struct reiserfs_transaction_handle *th,
a9dd3643 3204 struct super_block *sb, unsigned long nblocks)
bd4c625c
LT
3205{
3206 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3207 int ret;
3208
3209 th->t_handle_save = NULL;
3210 if (cur_th) {
3211 /* we are nesting into the current transaction */
a9dd3643 3212 if (cur_th->t_super == sb) {
bd4c625c
LT
3213 BUG_ON(!cur_th->t_refcount);
3214 cur_th->t_refcount++;
3215 memcpy(th, cur_th, sizeof(*th));
3216 if (th->t_refcount <= 1)
a9dd3643 3217 reiserfs_warning(sb, "reiserfs-2005",
45b03d5e
JM
3218 "BAD: refcount <= 1, but "
3219 "journal_info != 0");
bd4c625c
LT
3220 return 0;
3221 } else {
3222 /* we've ended up with a handle from a different filesystem.
3223 ** save it and restore on journal_end. This should never
3224 ** really happen...
3225 */
a9dd3643 3226 reiserfs_warning(sb, "clm-2100",
45b03d5e 3227 "nesting info a different FS");
bd4c625c
LT
3228 th->t_handle_save = current->journal_info;
3229 current->journal_info = th;
3230 }
1da177e4 3231 } else {
bd4c625c
LT
3232 current->journal_info = th;
3233 }
a9dd3643 3234 ret = do_journal_begin_r(th, sb, nblocks, JBEGIN_REG);
14a61442 3235 BUG_ON(current->journal_info != th);
1da177e4 3236
bd4c625c
LT
3237 /* I guess this boils down to being the reciprocal of clm-2100 above.
3238 * If do_journal_begin_r fails, we need to put it back, since journal_end
3239 * won't be called to do it. */
3240 if (ret)
3241 current->journal_info = th->t_handle_save;
3242 else
3243 BUG_ON(!th->t_refcount);
1da177e4 3244
bd4c625c 3245 return ret;
1da177e4
LT
3246}
3247
3248/*
3249** puts bh into the current transaction. If it was already there, reorders removes the
3250** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
3251**
3252** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
3253** transaction is committed.
0222e657 3254**
1da177e4
LT
3255** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
3256*/
bd4c625c 3257int journal_mark_dirty(struct reiserfs_transaction_handle *th,
a9dd3643 3258 struct super_block *sb, struct buffer_head *bh)
bd4c625c 3259{
a9dd3643 3260 struct reiserfs_journal *journal = SB_JOURNAL(sb);
bd4c625c
LT
3261 struct reiserfs_journal_cnode *cn = NULL;
3262 int count_already_incd = 0;
3263 int prepared = 0;
3264 BUG_ON(!th->t_trans_id);
3265
a9dd3643 3266 PROC_INFO_INC(sb, journal.mark_dirty);
bd4c625c 3267 if (th->t_trans_id != journal->j_trans_id) {
c3a9c210
JM
3268 reiserfs_panic(th->t_super, "journal-1577",
3269 "handle trans id %ld != current trans id %ld",
bd4c625c
LT
3270 th->t_trans_id, journal->j_trans_id);
3271 }
3272
a9dd3643 3273 sb->s_dirt = 1;
bd4c625c
LT
3274
3275 prepared = test_clear_buffer_journal_prepared(bh);
3276 clear_buffer_journal_restore_dirty(bh);
3277 /* already in this transaction, we are done */
3278 if (buffer_journaled(bh)) {
a9dd3643 3279 PROC_INFO_INC(sb, journal.mark_dirty_already);
bd4c625c
LT
3280 return 0;
3281 }
3282
3283 /* this must be turned into a panic instead of a warning. We can't allow
3284 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
3285 ** could get to disk too early. NOT GOOD.
3286 */
3287 if (!prepared || buffer_dirty(bh)) {
a9dd3643 3288 reiserfs_warning(sb, "journal-1777",
45b03d5e 3289 "buffer %llu bad state "
bd4c625c
LT
3290 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
3291 (unsigned long long)bh->b_blocknr,
3292 prepared ? ' ' : '!',
3293 buffer_locked(bh) ? ' ' : '!',
3294 buffer_dirty(bh) ? ' ' : '!',
3295 buffer_journal_dirty(bh) ? ' ' : '!');
3296 }
3297
3298 if (atomic_read(&(journal->j_wcount)) <= 0) {
a9dd3643 3299 reiserfs_warning(sb, "journal-1409",
45b03d5e 3300 "returning because j_wcount was %d",
bd4c625c
LT
3301 atomic_read(&(journal->j_wcount)));
3302 return 1;
3303 }
0222e657 3304 /* this error means I've screwed up, and we've overflowed the transaction.
bd4c625c
LT
3305 ** Nothing can be done here, except make the FS readonly or panic.
3306 */
3307 if (journal->j_len >= journal->j_trans_max) {
c3a9c210
JM
3308 reiserfs_panic(th->t_super, "journal-1413",
3309 "j_len (%lu) is too big",
bd4c625c
LT
3310 journal->j_len);
3311 }
3312
3313 if (buffer_journal_dirty(bh)) {
3314 count_already_incd = 1;
a9dd3643 3315 PROC_INFO_INC(sb, journal.mark_dirty_notjournal);
bd4c625c
LT
3316 clear_buffer_journal_dirty(bh);
3317 }
3318
3319 if (journal->j_len > journal->j_len_alloc) {
3320 journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT;
3321 }
3322
3323 set_buffer_journaled(bh);
3324
3325 /* now put this guy on the end */
3326 if (!cn) {
a9dd3643 3327 cn = get_cnode(sb);
bd4c625c 3328 if (!cn) {
a9dd3643 3329 reiserfs_panic(sb, "journal-4", "get_cnode failed!");
bd4c625c
LT
3330 }
3331
3332 if (th->t_blocks_logged == th->t_blocks_allocated) {
3333 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT;
3334 journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT;
3335 }
3336 th->t_blocks_logged++;
3337 journal->j_len++;
3338
3339 cn->bh = bh;
3340 cn->blocknr = bh->b_blocknr;
a9dd3643 3341 cn->sb = sb;
bd4c625c
LT
3342 cn->jlist = NULL;
3343 insert_journal_hash(journal->j_hash_table, cn);
3344 if (!count_already_incd) {
3345 get_bh(bh);
3346 }
3347 }
3348 cn->next = NULL;
3349 cn->prev = journal->j_last;
3350 cn->bh = bh;
3351 if (journal->j_last) {
3352 journal->j_last->next = cn;
3353 journal->j_last = cn;
3354 } else {
3355 journal->j_first = cn;
3356 journal->j_last = cn;
3357 }
3358 return 0;
3359}
3360
3361int journal_end(struct reiserfs_transaction_handle *th,
a9dd3643 3362 struct super_block *sb, unsigned long nblocks)
bd4c625c
LT
3363{
3364 if (!current->journal_info && th->t_refcount > 1)
a9dd3643 3365 reiserfs_warning(sb, "REISER-NESTING",
45b03d5e 3366 "th NULL, refcount %d", th->t_refcount);
bd4c625c
LT
3367
3368 if (!th->t_trans_id) {
3369 WARN_ON(1);
3370 return -EIO;
3371 }
3372
3373 th->t_refcount--;
3374 if (th->t_refcount > 0) {
3375 struct reiserfs_transaction_handle *cur_th =
3376 current->journal_info;
3377
3378 /* we aren't allowed to close a nested transaction on a different
3379 ** filesystem from the one in the task struct
3380 */
14a61442 3381 BUG_ON(cur_th->t_super != th->t_super);
bd4c625c
LT
3382
3383 if (th != cur_th) {
3384 memcpy(current->journal_info, th, sizeof(*th));
3385 th->t_trans_id = 0;
3386 }
3387 return 0;
3388 } else {
a9dd3643 3389 return do_journal_end(th, sb, nblocks, 0);
bd4c625c 3390 }
1da177e4
LT
3391}
3392
0222e657 3393/* removes from the current transaction, relsing and descrementing any counters.
1da177e4
LT
3394** also files the removed buffer directly onto the clean list
3395**
3396** called by journal_mark_freed when a block has been deleted
3397**
3398** returns 1 if it cleaned and relsed the buffer. 0 otherwise
3399*/
a9dd3643 3400static int remove_from_transaction(struct super_block *sb,
bd4c625c
LT
3401 b_blocknr_t blocknr, int already_cleaned)
3402{
3403 struct buffer_head *bh;
3404 struct reiserfs_journal_cnode *cn;
a9dd3643 3405 struct reiserfs_journal *journal = SB_JOURNAL(sb);
bd4c625c
LT
3406 int ret = 0;
3407
a9dd3643 3408 cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr);
bd4c625c
LT
3409 if (!cn || !cn->bh) {
3410 return ret;
3411 }
3412 bh = cn->bh;
3413 if (cn->prev) {
3414 cn->prev->next = cn->next;
3415 }
3416 if (cn->next) {
3417 cn->next->prev = cn->prev;
3418 }
3419 if (cn == journal->j_first) {
3420 journal->j_first = cn->next;
3421 }
3422 if (cn == journal->j_last) {
3423 journal->j_last = cn->prev;
3424 }
3425 if (bh)
a9dd3643 3426 remove_journal_hash(sb, journal->j_hash_table, NULL,
bd4c625c
LT
3427 bh->b_blocknr, 0);
3428 clear_buffer_journaled(bh); /* don't log this one */
3429
3430 if (!already_cleaned) {
3431 clear_buffer_journal_dirty(bh);
3432 clear_buffer_dirty(bh);
3433 clear_buffer_journal_test(bh);
3434 put_bh(bh);
3435 if (atomic_read(&(bh->b_count)) < 0) {
a9dd3643 3436 reiserfs_warning(sb, "journal-1752",
45b03d5e 3437 "b_count < 0");
bd4c625c
LT
3438 }
3439 ret = 1;
3440 }
3441 journal->j_len--;
3442 journal->j_len_alloc--;
a9dd3643 3443 free_cnode(sb, cn);
bd4c625c 3444 return ret;
1da177e4
LT
3445}
3446
3447/*
3448** for any cnode in a journal list, it can only be dirtied of all the
0779bf2d 3449** transactions that include it are committed to disk.
1da177e4
LT
3450** this checks through each transaction, and returns 1 if you are allowed to dirty,
3451** and 0 if you aren't
3452**
3453** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3454** blocks for a given transaction on disk
3455**
3456*/
bd4c625c
LT
3457static int can_dirty(struct reiserfs_journal_cnode *cn)
3458{
3459 struct super_block *sb = cn->sb;
3460 b_blocknr_t blocknr = cn->blocknr;
3461 struct reiserfs_journal_cnode *cur = cn->hprev;
3462 int can_dirty = 1;
3463
3464 /* first test hprev. These are all newer than cn, so any node here
3465 ** with the same block number and dev means this node can't be sent
3466 ** to disk right now.
3467 */
3468 while (cur && can_dirty) {
3469 if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
3470 cur->blocknr == blocknr) {
3471 can_dirty = 0;
3472 }
3473 cur = cur->hprev;
3474 }
3475 /* then test hnext. These are all older than cn. As long as they
3476 ** are committed to the log, it is safe to write cn to disk
3477 */
3478 cur = cn->hnext;
3479 while (cur && can_dirty) {
3480 if (cur->jlist && cur->jlist->j_len > 0 &&
3481 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
3482 cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
3483 can_dirty = 0;
3484 }
3485 cur = cur->hnext;
3486 }
3487 return can_dirty;
1da177e4
LT
3488}
3489
3490/* syncs the commit blocks, but does not force the real buffers to disk
0222e657 3491** will wait until the current transaction is done/committed before returning
1da177e4 3492*/
bd4c625c 3493int journal_end_sync(struct reiserfs_transaction_handle *th,
a9dd3643 3494 struct super_block *sb, unsigned long nblocks)
bd4c625c 3495{
a9dd3643 3496 struct reiserfs_journal *journal = SB_JOURNAL(sb);
1da177e4 3497
bd4c625c
LT
3498 BUG_ON(!th->t_trans_id);
3499 /* you can sync while nested, very, very bad */
14a61442 3500 BUG_ON(th->t_refcount > 1);
bd4c625c 3501 if (journal->j_len == 0) {
a9dd3643 3502 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
bd4c625c 3503 1);
a9dd3643 3504 journal_mark_dirty(th, sb, SB_BUFFER_WITH_SB(sb));
bd4c625c 3505 }
a9dd3643 3506 return do_journal_end(th, sb, nblocks, COMMIT_NOW | WAIT);
1da177e4
LT
3507}
3508
3509/*
3510** writeback the pending async commits to disk
3511*/
c4028958 3512static void flush_async_commits(struct work_struct *work)
bd4c625c 3513{
c4028958
DH
3514 struct reiserfs_journal *journal =
3515 container_of(work, struct reiserfs_journal, j_work.work);
a9dd3643 3516 struct super_block *sb = journal->j_work_sb;
bd4c625c
LT
3517 struct reiserfs_journal_list *jl;
3518 struct list_head *entry;
3519
8ebc4232 3520 reiserfs_write_lock(sb);
bd4c625c
LT
3521 if (!list_empty(&journal->j_journal_list)) {
3522 /* last entry is the youngest, commit it and you get everything */
3523 entry = journal->j_journal_list.prev;
3524 jl = JOURNAL_LIST_ENTRY(entry);
a9dd3643 3525 flush_commit_list(sb, jl, 1);
bd4c625c 3526 }
8ebc4232 3527 reiserfs_write_unlock(sb);
1da177e4
LT
3528}
3529
3530/*
3531** flushes any old transactions to disk
3532** ends the current transaction if it is too old
3533*/
a9dd3643 3534int reiserfs_flush_old_commits(struct super_block *sb)
bd4c625c
LT
3535{
3536 time_t now;
3537 struct reiserfs_transaction_handle th;
a9dd3643 3538 struct reiserfs_journal *journal = SB_JOURNAL(sb);
bd4c625c
LT
3539
3540 now = get_seconds();
3541 /* safety check so we don't flush while we are replaying the log during
3542 * mount
3543 */
3544 if (list_empty(&journal->j_journal_list)) {
3545 return 0;
3546 }
3547
3548 /* check the current transaction. If there are no writers, and it is
3549 * too old, finish it, and force the commit blocks to disk
3550 */
3551 if (atomic_read(&journal->j_wcount) <= 0 &&
3552 journal->j_trans_start_time > 0 &&
3553 journal->j_len > 0 &&
3554 (now - journal->j_trans_start_time) > journal->j_max_trans_age) {
a9dd3643
JM
3555 if (!journal_join(&th, sb, 1)) {
3556 reiserfs_prepare_for_journal(sb,
3557 SB_BUFFER_WITH_SB(sb),
bd4c625c 3558 1);
a9dd3643
JM
3559 journal_mark_dirty(&th, sb,
3560 SB_BUFFER_WITH_SB(sb));
bd4c625c
LT
3561
3562 /* we're only being called from kreiserfsd, it makes no sense to do
3563 ** an async commit so that kreiserfsd can do it later
3564 */
a9dd3643 3565 do_journal_end(&th, sb, 1, COMMIT_NOW | WAIT);
bd4c625c
LT
3566 }
3567 }
a9dd3643 3568 return sb->s_dirt;
1da177e4
LT
3569}
3570
3571/*
3572** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
0222e657
JM
3573**
3574** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
1da177e4
LT
3575** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3576** flushes the commit list and returns 0.
3577**
3578** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
0222e657 3579**
1da177e4
LT
3580** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3581*/
bd4c625c 3582static int check_journal_end(struct reiserfs_transaction_handle *th,
a9dd3643 3583 struct super_block *sb, unsigned long nblocks,
bd4c625c
LT
3584 int flags)
3585{
3586
3587 time_t now;
3588 int flush = flags & FLUSH_ALL;
3589 int commit_now = flags & COMMIT_NOW;
3590 int wait_on_commit = flags & WAIT;
3591 struct reiserfs_journal_list *jl;
a9dd3643 3592 struct reiserfs_journal *journal = SB_JOURNAL(sb);
bd4c625c
LT
3593
3594 BUG_ON(!th->t_trans_id);
3595
3596 if (th->t_trans_id != journal->j_trans_id) {
c3a9c210
JM
3597 reiserfs_panic(th->t_super, "journal-1577",
3598 "handle trans id %ld != current trans id %ld",
bd4c625c
LT
3599 th->t_trans_id, journal->j_trans_id);
3600 }
3601
3602 journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged);
3603 if (atomic_read(&(journal->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3604 atomic_dec(&(journal->j_wcount));
3605 }
3606
0222e657 3607 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
bd4c625c
LT
3608 ** will be dealt with by next transaction that actually writes something, but should be taken
3609 ** care of in this trans
3610 */
14a61442
ES
3611 BUG_ON(journal->j_len == 0);
3612
bd4c625c
LT
3613 /* if wcount > 0, and we are called to with flush or commit_now,
3614 ** we wait on j_join_wait. We will wake up when the last writer has
3615 ** finished the transaction, and started it on its way to the disk.
0222e657 3616 ** Then, we flush the commit or journal list, and just return 0
bd4c625c
LT
3617 ** because the rest of journal end was already done for this transaction.
3618 */
3619 if (atomic_read(&(journal->j_wcount)) > 0) {
3620 if (flush || commit_now) {
3621 unsigned trans_id;
3622
3623 jl = journal->j_current_jl;
3624 trans_id = jl->j_trans_id;
3625 if (wait_on_commit)
3626 jl->j_state |= LIST_COMMIT_PENDING;
3627 atomic_set(&(journal->j_jlock), 1);
3628 if (flush) {
3629 journal->j_next_full_flush = 1;
3630 }
a9dd3643 3631 unlock_journal(sb);
bd4c625c
LT
3632
3633 /* sleep while the current transaction is still j_jlocked */
3634 while (journal->j_trans_id == trans_id) {
3635 if (atomic_read(&journal->j_jlock)) {
a9dd3643 3636 queue_log_writer(sb);
bd4c625c 3637 } else {
a9dd3643 3638 lock_journal(sb);
bd4c625c
LT
3639 if (journal->j_trans_id == trans_id) {
3640 atomic_set(&(journal->j_jlock),
3641 1);
3642 }
a9dd3643 3643 unlock_journal(sb);
bd4c625c
LT
3644 }
3645 }
14a61442
ES
3646 BUG_ON(journal->j_trans_id == trans_id);
3647
bd4c625c 3648 if (commit_now
a9dd3643 3649 && journal_list_still_alive(sb, trans_id)
bd4c625c 3650 && wait_on_commit) {
a9dd3643 3651 flush_commit_list(sb, jl, 1);
bd4c625c
LT
3652 }
3653 return 0;
3654 }
a9dd3643 3655 unlock_journal(sb);
bd4c625c
LT
3656 return 0;
3657 }
3658
3659 /* deal with old transactions where we are the last writers */
3660 now = get_seconds();
3661 if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3662 commit_now = 1;
3663 journal->j_next_async_flush = 1;
3664 }
3665 /* don't batch when someone is waiting on j_join_wait */
3666 /* don't batch when syncing the commit or flushing the whole trans */
3667 if (!(journal->j_must_wait > 0) && !(atomic_read(&(journal->j_jlock)))
3668 && !flush && !commit_now && (journal->j_len < journal->j_max_batch)
3669 && journal->j_len_alloc < journal->j_max_batch
3670 && journal->j_cnode_free > (journal->j_trans_max * 3)) {
3671 journal->j_bcount++;
a9dd3643 3672 unlock_journal(sb);
bd4c625c
LT
3673 return 0;
3674 }
3675
a9dd3643
JM
3676 if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(sb)) {
3677 reiserfs_panic(sb, "journal-003",
c3a9c210 3678 "j_start (%ld) is too high",
bd4c625c
LT
3679 journal->j_start);
3680 }
3681 return 1;
1da177e4
LT
3682}
3683
3684/*
3685** Does all the work that makes deleting blocks safe.
3686** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
0222e657 3687**
1da177e4
LT
3688** otherwise:
3689** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3690** before this transaction has finished.
3691**
3692** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3693** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3694** the block can't be reallocated yet.
3695**
3696** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3697*/
bd4c625c 3698int journal_mark_freed(struct reiserfs_transaction_handle *th,
a9dd3643 3699 struct super_block *sb, b_blocknr_t blocknr)
bd4c625c 3700{
a9dd3643 3701 struct reiserfs_journal *journal = SB_JOURNAL(sb);
bd4c625c
LT
3702 struct reiserfs_journal_cnode *cn = NULL;
3703 struct buffer_head *bh = NULL;
3704 struct reiserfs_list_bitmap *jb = NULL;
3705 int cleaned = 0;
3706 BUG_ON(!th->t_trans_id);
3707
a9dd3643 3708 cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr);
bd4c625c
LT
3709 if (cn && cn->bh) {
3710 bh = cn->bh;
3711 get_bh(bh);
3712 }
3713 /* if it is journal new, we just remove it from this transaction */
3714 if (bh && buffer_journal_new(bh)) {
3715 clear_buffer_journal_new(bh);
3716 clear_prepared_bits(bh);
3717 reiserfs_clean_and_file_buffer(bh);
a9dd3643 3718 cleaned = remove_from_transaction(sb, blocknr, cleaned);
bd4c625c
LT
3719 } else {
3720 /* set the bit for this block in the journal bitmap for this transaction */
3721 jb = journal->j_current_jl->j_list_bitmap;
3722 if (!jb) {
a9dd3643 3723 reiserfs_panic(sb, "journal-1702",
c3a9c210 3724 "journal_list_bitmap is NULL");
bd4c625c 3725 }
a9dd3643 3726 set_bit_in_list_bitmap(sb, blocknr, jb);
bd4c625c
LT
3727
3728 /* Note, the entire while loop is not allowed to schedule. */
3729
3730 if (bh) {
3731 clear_prepared_bits(bh);
3732 reiserfs_clean_and_file_buffer(bh);
3733 }
a9dd3643 3734 cleaned = remove_from_transaction(sb, blocknr, cleaned);
bd4c625c
LT
3735
3736 /* find all older transactions with this block, make sure they don't try to write it out */
a9dd3643 3737 cn = get_journal_hash_dev(sb, journal->j_list_hash_table,
bd4c625c
LT
3738 blocknr);
3739 while (cn) {
a9dd3643 3740 if (sb == cn->sb && blocknr == cn->blocknr) {
bd4c625c
LT
3741 set_bit(BLOCK_FREED, &cn->state);
3742 if (cn->bh) {
3743 if (!cleaned) {
3744 /* remove_from_transaction will brelse the buffer if it was
3745 ** in the current trans
3746 */
3747 clear_buffer_journal_dirty(cn->
3748 bh);
3749 clear_buffer_dirty(cn->bh);
3750 clear_buffer_journal_test(cn->
3751 bh);
3752 cleaned = 1;
3753 put_bh(cn->bh);
3754 if (atomic_read
3755 (&(cn->bh->b_count)) < 0) {
a9dd3643 3756 reiserfs_warning(sb,
45b03d5e
JM
3757 "journal-2138",
3758 "cn->bh->b_count < 0");
bd4c625c
LT
3759 }
3760 }
3761 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3762 atomic_dec(&
3763 (cn->jlist->
3764 j_nonzerolen));
3765 }
3766 cn->bh = NULL;
3767 }
3768 }
3769 cn = cn->hnext;
3770 }
3771 }
3772
398c95bd
CM
3773 if (bh)
3774 release_buffer_page(bh); /* get_hash grabs the buffer */
bd4c625c
LT
3775 return 0;
3776}
3777
3778void reiserfs_update_inode_transaction(struct inode *inode)
3779{
3780 struct reiserfs_journal *journal = SB_JOURNAL(inode->i_sb);
3781 REISERFS_I(inode)->i_jl = journal->j_current_jl;
3782 REISERFS_I(inode)->i_trans_id = journal->j_trans_id;
1da177e4
LT
3783}
3784
3785/*
3786 * returns -1 on error, 0 if no commits/barriers were done and 1
3787 * if a transaction was actually committed and the barrier was done
3788 */
3789static int __commit_trans_jl(struct inode *inode, unsigned long id,
bd4c625c 3790 struct reiserfs_journal_list *jl)
1da177e4 3791{
bd4c625c
LT
3792 struct reiserfs_transaction_handle th;
3793 struct super_block *sb = inode->i_sb;
3794 struct reiserfs_journal *journal = SB_JOURNAL(sb);
3795 int ret = 0;
3796
3797 /* is it from the current transaction, or from an unknown transaction? */
3798 if (id == journal->j_trans_id) {
3799 jl = journal->j_current_jl;
3800 /* try to let other writers come in and grow this transaction */
3801 let_transaction_grow(sb, id);
3802 if (journal->j_trans_id != id) {
3803 goto flush_commit_only;
3804 }
1da177e4 3805
bd4c625c
LT
3806 ret = journal_begin(&th, sb, 1);
3807 if (ret)
3808 return ret;
3809
3810 /* someone might have ended this transaction while we joined */
3811 if (journal->j_trans_id != id) {
3812 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
3813 1);
3814 journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb));
3815 ret = journal_end(&th, sb, 1);
3816 goto flush_commit_only;
3817 }
1da177e4 3818
bd4c625c
LT
3819 ret = journal_end_sync(&th, sb, 1);
3820 if (!ret)
3821 ret = 1;
1da177e4 3822
bd4c625c
LT
3823 } else {
3824 /* this gets tricky, we have to make sure the journal list in
3825 * the inode still exists. We know the list is still around
3826 * if we've got a larger transaction id than the oldest list
3827 */
3828 flush_commit_only:
3829 if (journal_list_still_alive(inode->i_sb, id)) {
3830 /*
3831 * we only set ret to 1 when we know for sure
3832 * the barrier hasn't been started yet on the commit
3833 * block.
3834 */
3835 if (atomic_read(&jl->j_commit_left) > 1)
3836 ret = 1;
3837 flush_commit_list(sb, jl, 1);
3838 if (journal->j_errno)
3839 ret = journal->j_errno;
3840 }
1da177e4 3841 }
bd4c625c
LT
3842 /* otherwise the list is gone, and long since committed */
3843 return ret;
3844}
1da177e4 3845
bd4c625c
LT
3846int reiserfs_commit_for_inode(struct inode *inode)
3847{
600ed416 3848 unsigned int id = REISERFS_I(inode)->i_trans_id;
bd4c625c 3849 struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
1da177e4 3850
bd4c625c
LT
3851 /* for the whole inode, assume unset id means it was
3852 * changed in the current transaction. More conservative
1da177e4 3853 */
bd4c625c
LT
3854 if (!id || !jl) {
3855 reiserfs_update_inode_transaction(inode);
3856 id = REISERFS_I(inode)->i_trans_id;
3857 /* jl will be updated in __commit_trans_jl */
3858 }
3859
3860 return __commit_trans_jl(inode, id, jl);
3861}
3862
a9dd3643 3863void reiserfs_restore_prepared_buffer(struct super_block *sb,
bd4c625c
LT
3864 struct buffer_head *bh)
3865{
a9dd3643
JM
3866 struct reiserfs_journal *journal = SB_JOURNAL(sb);
3867 PROC_INFO_INC(sb, journal.restore_prepared);
bd4c625c
LT
3868 if (!bh) {
3869 return;
3870 }
3871 if (test_clear_buffer_journal_restore_dirty(bh) &&
3872 buffer_journal_dirty(bh)) {
3873 struct reiserfs_journal_cnode *cn;
a9dd3643 3874 cn = get_journal_hash_dev(sb,
bd4c625c
LT
3875 journal->j_list_hash_table,
3876 bh->b_blocknr);
3877 if (cn && can_dirty(cn)) {
3878 set_buffer_journal_test(bh);
3879 mark_buffer_dirty(bh);
3880 }
3881 }
3882 clear_buffer_journal_prepared(bh);
3883}
3884
3885extern struct tree_balance *cur_tb;
1da177e4
LT
3886/*
3887** before we can change a metadata block, we have to make sure it won't
3888** be written to disk while we are altering it. So, we must:
3889** clean it
3890** wait on it.
0222e657 3891**
1da177e4 3892*/
a9dd3643 3893int reiserfs_prepare_for_journal(struct super_block *sb,
bd4c625c
LT
3894 struct buffer_head *bh, int wait)
3895{
a9dd3643 3896 PROC_INFO_INC(sb, journal.prepare);
bd4c625c 3897
ca5de404 3898 if (!trylock_buffer(bh)) {
bd4c625c
LT
3899 if (!wait)
3900 return 0;
3901 lock_buffer(bh);
3902 }
3903 set_buffer_journal_prepared(bh);
3904 if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
3905 clear_buffer_journal_test(bh);
3906 set_buffer_journal_restore_dirty(bh);
3907 }
3908 unlock_buffer(bh);
3909 return 1;
3910}
3911
3912static void flush_old_journal_lists(struct super_block *s)
3913{
3914 struct reiserfs_journal *journal = SB_JOURNAL(s);
3915 struct reiserfs_journal_list *jl;
3916 struct list_head *entry;
3917 time_t now = get_seconds();
3918
3919 while (!list_empty(&journal->j_journal_list)) {
3920 entry = journal->j_journal_list.next;
3921 jl = JOURNAL_LIST_ENTRY(entry);
3922 /* this check should always be run, to send old lists to disk */
a3172027
CM
3923 if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4)) &&
3924 atomic_read(&jl->j_commit_left) == 0 &&
3925 test_transaction(s, jl)) {
bd4c625c
LT
3926 flush_used_journal_lists(s, jl);
3927 } else {
3928 break;
3929 }
1da177e4 3930 }
1da177e4
LT
3931}
3932
0222e657 3933/*
1da177e4
LT
3934** long and ugly. If flush, will not return until all commit
3935** blocks and all real buffers in the trans are on disk.
3936** If no_async, won't return until all commit blocks are on disk.
3937**
3938** keep reading, there are comments as you go along
3939**
3940** If the journal is aborted, we just clean up. Things like flushing
3941** journal lists, etc just won't happen.
3942*/
bd4c625c 3943static int do_journal_end(struct reiserfs_transaction_handle *th,
a9dd3643 3944 struct super_block *sb, unsigned long nblocks,
bd4c625c
LT
3945 int flags)
3946{
a9dd3643 3947 struct reiserfs_journal *journal = SB_JOURNAL(sb);
bd4c625c
LT
3948 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
3949 struct reiserfs_journal_cnode *last_cn = NULL;
3950 struct reiserfs_journal_desc *desc;
3951 struct reiserfs_journal_commit *commit;
3952 struct buffer_head *c_bh; /* commit bh */
3953 struct buffer_head *d_bh; /* desc bh */
3954 int cur_write_start = 0; /* start index of current log write */
3955 int old_start;
3956 int i;
a44c94a7
AZ
3957 int flush;
3958 int wait_on_commit;
bd4c625c
LT
3959 struct reiserfs_journal_list *jl, *temp_jl;
3960 struct list_head *entry, *safe;
3961 unsigned long jindex;
600ed416 3962 unsigned int commit_trans_id;
bd4c625c
LT
3963 int trans_half;
3964
3965 BUG_ON(th->t_refcount > 1);
3966 BUG_ON(!th->t_trans_id);
3967
a44c94a7
AZ
3968 /* protect flush_older_commits from doing mistakes if the
3969 transaction ID counter gets overflowed. */
600ed416 3970 if (th->t_trans_id == ~0U)
a44c94a7
AZ
3971 flags |= FLUSH_ALL | COMMIT_NOW | WAIT;
3972 flush = flags & FLUSH_ALL;
3973 wait_on_commit = flags & WAIT;
3974
bd4c625c
LT
3975 put_fs_excl();
3976 current->journal_info = th->t_handle_save;
a9dd3643 3977 reiserfs_check_lock_depth(sb, "journal end");
bd4c625c 3978 if (journal->j_len == 0) {
a9dd3643 3979 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
bd4c625c 3980 1);
a9dd3643 3981 journal_mark_dirty(th, sb, SB_BUFFER_WITH_SB(sb));
bd4c625c 3982 }
1da177e4 3983
a9dd3643 3984 lock_journal(sb);
bd4c625c
LT
3985 if (journal->j_next_full_flush) {
3986 flags |= FLUSH_ALL;
3987 flush = 1;
3988 }
3989 if (journal->j_next_async_flush) {
3990 flags |= COMMIT_NOW | WAIT;
3991 wait_on_commit = 1;
3992 }
3993
0222e657 3994 /* check_journal_end locks the journal, and unlocks if it does not return 1
bd4c625c
LT
3995 ** it tells us if we should continue with the journal_end, or just return
3996 */
a9dd3643
JM
3997 if (!check_journal_end(th, sb, nblocks, flags)) {
3998 sb->s_dirt = 1;
3999 wake_queued_writers(sb);
4000 reiserfs_async_progress_wait(sb);
bd4c625c
LT
4001 goto out;
4002 }
4003
4004 /* check_journal_end might set these, check again */
4005 if (journal->j_next_full_flush) {
4006 flush = 1;
4007 }
4008
4009 /*
4010 ** j must wait means we have to flush the log blocks, and the real blocks for
4011 ** this transaction
4012 */
4013 if (journal->j_must_wait > 0) {
4014 flush = 1;
4015 }
1da177e4 4016#ifdef REISERFS_PREALLOCATE
ef43bc4f
JK
4017 /* quota ops might need to nest, setup the journal_info pointer for them
4018 * and raise the refcount so that it is > 0. */
bd4c625c 4019 current->journal_info = th;
ef43bc4f 4020 th->t_refcount++;
bd4c625c
LT
4021 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
4022 * the transaction */
ef43bc4f 4023 th->t_refcount--;
bd4c625c 4024 current->journal_info = th->t_handle_save;
1da177e4 4025#endif
bd4c625c
LT
4026
4027 /* setup description block */
4028 d_bh =
a9dd3643
JM
4029 journal_getblk(sb,
4030 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
bd4c625c
LT
4031 journal->j_start);
4032 set_buffer_uptodate(d_bh);
4033 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data;
4034 memset(d_bh->b_data, 0, d_bh->b_size);
4035 memcpy(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8);
4036 set_desc_trans_id(desc, journal->j_trans_id);
4037
4038 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
a9dd3643 4039 c_bh = journal_getblk(sb, SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
bd4c625c 4040 ((journal->j_start + journal->j_len +
a9dd3643 4041 1) % SB_ONDISK_JOURNAL_SIZE(sb)));
bd4c625c
LT
4042 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
4043 memset(c_bh->b_data, 0, c_bh->b_size);
4044 set_commit_trans_id(commit, journal->j_trans_id);
4045 set_buffer_uptodate(c_bh);
4046
4047 /* init this journal list */
4048 jl = journal->j_current_jl;
4049
4050 /* we lock the commit before doing anything because
4051 * we want to make sure nobody tries to run flush_commit_list until
4052 * the new transaction is fully setup, and we've already flushed the
4053 * ordered bh list
4054 */
8ebc4232 4055 reiserfs_mutex_lock_safe(&jl->j_commit_mutex, sb);
bd4c625c
LT
4056
4057 /* save the transaction id in case we need to commit it later */
4058 commit_trans_id = jl->j_trans_id;
4059
4060 atomic_set(&jl->j_older_commits_done, 0);
4061 jl->j_trans_id = journal->j_trans_id;
4062 jl->j_timestamp = journal->j_trans_start_time;
4063 jl->j_commit_bh = c_bh;
4064 jl->j_start = journal->j_start;
4065 jl->j_len = journal->j_len;
4066 atomic_set(&jl->j_nonzerolen, journal->j_len);
4067 atomic_set(&jl->j_commit_left, journal->j_len + 2);
4068 jl->j_realblock = NULL;
4069
4070 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
4071 ** for each real block, add it to the journal list hash,
4072 ** copy into real block index array in the commit or desc block
4073 */
a9dd3643 4074 trans_half = journal_trans_half(sb->s_blocksize);
bd4c625c
LT
4075 for (i = 0, cn = journal->j_first; cn; cn = cn->next, i++) {
4076 if (buffer_journaled(cn->bh)) {
a9dd3643 4077 jl_cn = get_cnode(sb);
bd4c625c 4078 if (!jl_cn) {
a9dd3643 4079 reiserfs_panic(sb, "journal-1676",
c3a9c210 4080 "get_cnode returned NULL");
bd4c625c
LT
4081 }
4082 if (i == 0) {
4083 jl->j_realblock = jl_cn;
4084 }
4085 jl_cn->prev = last_cn;
4086 jl_cn->next = NULL;
4087 if (last_cn) {
4088 last_cn->next = jl_cn;
4089 }
4090 last_cn = jl_cn;
0222e657 4091 /* make sure the block we are trying to log is not a block
bd4c625c
LT
4092 of journal or reserved area */
4093
4094 if (is_block_in_log_or_reserved_area
a9dd3643
JM
4095 (sb, cn->bh->b_blocknr)) {
4096 reiserfs_panic(sb, "journal-2332",
c3a9c210
JM
4097 "Trying to log block %lu, "
4098 "which is a log block",
bd4c625c
LT
4099 cn->bh->b_blocknr);
4100 }
4101 jl_cn->blocknr = cn->bh->b_blocknr;
4102 jl_cn->state = 0;
a9dd3643 4103 jl_cn->sb = sb;
bd4c625c
LT
4104 jl_cn->bh = cn->bh;
4105 jl_cn->jlist = jl;
4106 insert_journal_hash(journal->j_list_hash_table, jl_cn);
4107 if (i < trans_half) {
4108 desc->j_realblock[i] =
4109 cpu_to_le32(cn->bh->b_blocknr);
4110 } else {
4111 commit->j_realblock[i - trans_half] =
4112 cpu_to_le32(cn->bh->b_blocknr);
4113 }
4114 } else {
4115 i--;
4116 }
4117 }
4118 set_desc_trans_len(desc, journal->j_len);
4119 set_desc_mount_id(desc, journal->j_mount_id);
4120 set_desc_trans_id(desc, journal->j_trans_id);
4121 set_commit_trans_len(commit, journal->j_len);
4122
4123 /* special check in case all buffers in the journal were marked for not logging */
14a61442 4124 BUG_ON(journal->j_len == 0);
bd4c625c
LT
4125
4126 /* we're about to dirty all the log blocks, mark the description block
4127 * dirty now too. Don't mark the commit block dirty until all the
4128 * others are on disk
4129 */
4130 mark_buffer_dirty(d_bh);
4131
4132 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
4133 cur_write_start = journal->j_start;
4134 cn = journal->j_first;
4135 jindex = 1; /* start at one so we don't get the desc again */
4136 while (cn) {
4137 clear_buffer_journal_new(cn->bh);
4138 /* copy all the real blocks into log area. dirty log blocks */
4139 if (buffer_journaled(cn->bh)) {
4140 struct buffer_head *tmp_bh;
4141 char *addr;
4142 struct page *page;
4143 tmp_bh =
a9dd3643
JM
4144 journal_getblk(sb,
4145 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
bd4c625c
LT
4146 ((cur_write_start +
4147 jindex) %
a9dd3643 4148 SB_ONDISK_JOURNAL_SIZE(sb)));
bd4c625c
LT
4149 set_buffer_uptodate(tmp_bh);
4150 page = cn->bh->b_page;
4151 addr = kmap(page);
4152 memcpy(tmp_bh->b_data,
4153 addr + offset_in_page(cn->bh->b_data),
4154 cn->bh->b_size);
4155 kunmap(page);
4156 mark_buffer_dirty(tmp_bh);
4157 jindex++;
4158 set_buffer_journal_dirty(cn->bh);
4159 clear_buffer_journaled(cn->bh);
4160 } else {
4161 /* JDirty cleared sometime during transaction. don't log this one */
a9dd3643 4162 reiserfs_warning(sb, "journal-2048",
45b03d5e
JM
4163 "BAD, buffer in journal hash, "
4164 "but not JDirty!");
bd4c625c
LT
4165 brelse(cn->bh);
4166 }
4167 next = cn->next;
a9dd3643 4168 free_cnode(sb, cn);
bd4c625c 4169 cn = next;
e6950a4d 4170 reiserfs_write_unlock(sb);
bd4c625c 4171 cond_resched();
e6950a4d 4172 reiserfs_write_lock(sb);
bd4c625c
LT
4173 }
4174
4175 /* we are done with both the c_bh and d_bh, but
4176 ** c_bh must be written after all other commit blocks,
4177 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
4178 */
4179
a9dd3643 4180 journal->j_current_jl = alloc_journal_list(sb);
bd4c625c
LT
4181
4182 /* now it is safe to insert this transaction on the main list */
4183 list_add_tail(&jl->j_list, &journal->j_journal_list);
4184 list_add_tail(&jl->j_working_list, &journal->j_working_list);
4185 journal->j_num_work_lists++;
4186
4187 /* reset journal values for the next transaction */
4188 old_start = journal->j_start;
4189 journal->j_start =
4190 (journal->j_start + journal->j_len +
a9dd3643 4191 2) % SB_ONDISK_JOURNAL_SIZE(sb);
bd4c625c
LT
4192 atomic_set(&(journal->j_wcount), 0);
4193 journal->j_bcount = 0;
4194 journal->j_last = NULL;
4195 journal->j_first = NULL;
4196 journal->j_len = 0;
4197 journal->j_trans_start_time = 0;
a44c94a7
AZ
4198 /* check for trans_id overflow */
4199 if (++journal->j_trans_id == 0)
4200 journal->j_trans_id = 10;
bd4c625c
LT
4201 journal->j_current_jl->j_trans_id = journal->j_trans_id;
4202 journal->j_must_wait = 0;
4203 journal->j_len_alloc = 0;
4204 journal->j_next_full_flush = 0;
4205 journal->j_next_async_flush = 0;
a9dd3643 4206 init_journal_hash(sb);
bd4c625c
LT
4207
4208 // make sure reiserfs_add_jh sees the new current_jl before we
4209 // write out the tails
4210 smp_mb();
4211
4212 /* tail conversion targets have to hit the disk before we end the
4213 * transaction. Otherwise a later transaction might repack the tail
4214 * before this transaction commits, leaving the data block unflushed and
4215 * clean, if we crash before the later transaction commits, the data block
4216 * is lost.
4217 */
4218 if (!list_empty(&jl->j_tail_bh_list)) {
8ebc4232 4219 reiserfs_write_unlock(sb);
bd4c625c
LT
4220 write_ordered_buffers(&journal->j_dirty_buffers_lock,
4221 journal, jl, &jl->j_tail_bh_list);
8ebc4232 4222 reiserfs_write_lock(sb);
bd4c625c 4223 }
14a61442 4224 BUG_ON(!list_empty(&jl->j_tail_bh_list));
90415dea 4225 mutex_unlock(&jl->j_commit_mutex);
bd4c625c
LT
4226
4227 /* honor the flush wishes from the caller, simple commits can
4228 ** be done outside the journal lock, they are done below
4229 **
4230 ** if we don't flush the commit list right now, we put it into
4231 ** the work queue so the people waiting on the async progress work
4232 ** queue don't wait for this proc to flush journal lists and such.
4233 */
4234 if (flush) {
a9dd3643
JM
4235 flush_commit_list(sb, jl, 1);
4236 flush_journal_list(sb, jl, 1);
bd4c625c
LT
4237 } else if (!(jl->j_state & LIST_COMMIT_PENDING))
4238 queue_delayed_work(commit_wq, &journal->j_work, HZ / 10);
4239
0222e657
JM
4240 /* if the next transaction has any chance of wrapping, flush
4241 ** transactions that might get overwritten. If any journal lists are very
4242 ** old flush them as well.
bd4c625c
LT
4243 */
4244 first_jl:
4245 list_for_each_safe(entry, safe, &journal->j_journal_list) {
4246 temp_jl = JOURNAL_LIST_ENTRY(entry);
4247 if (journal->j_start <= temp_jl->j_start) {
4248 if ((journal->j_start + journal->j_trans_max + 1) >=
4249 temp_jl->j_start) {
a9dd3643 4250 flush_used_journal_lists(sb, temp_jl);
bd4c625c
LT
4251 goto first_jl;
4252 } else if ((journal->j_start +
4253 journal->j_trans_max + 1) <
a9dd3643 4254 SB_ONDISK_JOURNAL_SIZE(sb)) {
bd4c625c
LT
4255 /* if we don't cross into the next transaction and we don't
4256 * wrap, there is no way we can overlap any later transactions
4257 * break now
4258 */
4259 break;
4260 }
4261 } else if ((journal->j_start +
4262 journal->j_trans_max + 1) >
a9dd3643 4263 SB_ONDISK_JOURNAL_SIZE(sb)) {
bd4c625c 4264 if (((journal->j_start + journal->j_trans_max + 1) %
a9dd3643 4265 SB_ONDISK_JOURNAL_SIZE(sb)) >=
bd4c625c 4266 temp_jl->j_start) {
a9dd3643 4267 flush_used_journal_lists(sb, temp_jl);
bd4c625c
LT
4268 goto first_jl;
4269 } else {
4270 /* we don't overlap anything from out start to the end of the
4271 * log, and our wrapped portion doesn't overlap anything at
4272 * the start of the log. We can break
4273 */
4274 break;
4275 }
4276 }
4277 }
a9dd3643 4278 flush_old_journal_lists(sb);
bd4c625c
LT
4279
4280 journal->j_current_jl->j_list_bitmap =
a9dd3643 4281 get_list_bitmap(sb, journal->j_current_jl);
bd4c625c
LT
4282
4283 if (!(journal->j_current_jl->j_list_bitmap)) {
a9dd3643 4284 reiserfs_panic(sb, "journal-1996",
c3a9c210 4285 "could not get a list bitmap");
bd4c625c
LT
4286 }
4287
4288 atomic_set(&(journal->j_jlock), 0);
a9dd3643 4289 unlock_journal(sb);
bd4c625c
LT
4290 /* wake up any body waiting to join. */
4291 clear_bit(J_WRITERS_QUEUED, &journal->j_state);
4292 wake_up(&(journal->j_join_wait));
4293
4294 if (!flush && wait_on_commit &&
a9dd3643
JM
4295 journal_list_still_alive(sb, commit_trans_id)) {
4296 flush_commit_list(sb, jl, 1);
bd4c625c
LT
4297 }
4298 out:
a9dd3643 4299 reiserfs_check_lock_depth(sb, "journal end2");
bd4c625c
LT
4300
4301 memset(th, 0, sizeof(*th));
4302 /* Re-set th->t_super, so we can properly keep track of how many
4303 * persistent transactions there are. We need to do this so if this
4304 * call is part of a failed restart_transaction, we can free it later */
a9dd3643 4305 th->t_super = sb;
bd4c625c
LT
4306
4307 return journal->j_errno;
4308}
4309
32e8b106
JM
4310/* Send the file system read only and refuse new transactions */
4311void reiserfs_abort_journal(struct super_block *sb, int errno)
bd4c625c
LT
4312{
4313 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4314 if (test_bit(J_ABORTED, &journal->j_state))
4315 return;
4316
32e8b106
JM
4317 if (!journal->j_errno)
4318 journal->j_errno = errno;
bd4c625c
LT
4319
4320 sb->s_flags |= MS_RDONLY;
4321 set_bit(J_ABORTED, &journal->j_state);
1da177e4
LT
4322
4323#ifdef CONFIG_REISERFS_CHECK
bd4c625c 4324 dump_stack();
1da177e4
LT
4325#endif
4326}
4327