blob: 9b3672d69367251b6109c6550c5e8e7f81986634 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2** Write ahead logging implementation copyright Chris Mason 2000
3**
4** The background commits make this code very interelated, and
5** overly complex. I need to rethink things a bit....The major players:
6**
7** journal_begin -- call with the number of blocks you expect to log.
8** If the current transaction is too
9** old, it will block until the current transaction is
10** finished, and then start a new one.
11** Usually, your transaction will get joined in with
12** previous ones for speed.
13**
14** journal_join -- same as journal_begin, but won't block on the current
15** transaction regardless of age. Don't ever call
16** this. Ever. There are only two places it should be
17** called from, and they are both inside this file.
18**
19** journal_mark_dirty -- adds blocks into this transaction. clears any flags
20** that might make them get sent to disk
21** and then marks them BH_JDirty. Puts the buffer head
22** into the current transaction hash.
23**
24** journal_end -- if the current transaction is batchable, it does nothing
25** otherwise, it could do an async/synchronous commit, or
26** a full flush of all log and real blocks in the
27** transaction.
28**
29** flush_old_commits -- if the current transaction is too old, it is ended and
30** commit blocks are sent to disk. Forces commit blocks
31** to disk for all backgrounded commits that have been
32** around too long.
33** -- Note, if you call this as an immediate flush from
34** from within kupdate, it will ignore the immediate flag
35*/
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38#include <asm/system.h>
39
40#include <linux/time.h>
41#include <asm/semaphore.h>
42
43#include <linux/vmalloc.h>
44#include <linux/reiserfs_fs.h>
45
46#include <linux/kernel.h>
47#include <linux/errno.h>
48#include <linux/fcntl.h>
49#include <linux/stat.h>
50#include <linux/string.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/workqueue.h>
54#include <linux/writeback.h>
55#include <linux/blkdev.h>
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* 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
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070070#define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
71 structs at 4k */
72#define BUFNR 64 /*read ahead */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* cnode stat bits. Move these into reiserfs_fs.h */
75
76#define BLOCK_FREED 2 /* this block was freed, and can't be written. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070077#define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
80#define BLOCK_DIRTIED 5
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* journal list state bits */
83#define LIST_TOUCHED 1
84#define LIST_DIRTY 2
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085#define LIST_COMMIT_PENDING 4 /* someone will commit this list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* flags for do_journal_end */
88#define FLUSH_ALL 1 /* flush commit and real blocks */
89#define COMMIT_NOW 2 /* end and commit this transaction */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090#define WAIT 4 /* wait for the log blocks to hit the disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070092static 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,
101 struct super_block *p_s_sb, unsigned long nblocks);
102static int release_journal_dev(struct super_block *super,
103 struct reiserfs_journal *journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int dirty_one_transaction(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700105 struct reiserfs_journal_list *jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static void flush_async_commits(void *p);
107static void queue_log_writer(struct super_block *s);
108
109/* values for join in do_journal_begin_r */
110enum {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 JBEGIN_REG = 0, /* regular journal begin */
112 JBEGIN_JOIN = 1, /* join the running transaction if at all possible */
113 JBEGIN_ABORT = 2, /* called from cleanup code, ignores aborted flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 struct super_block *p_s_sb,
118 unsigned long nblocks, int join);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120static void init_journal_hash(struct super_block *p_s_sb)
121{
122 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
123 memset(journal->j_hash_table, 0,
124 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/*
128** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
129** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
130** more details.
131*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700132static int reiserfs_clean_and_file_buffer(struct buffer_head *bh)
133{
134 if (bh) {
135 clear_buffer_dirty(bh);
136 clear_buffer_journal_test(bh);
137 }
138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141static void disable_barrier(struct super_block *s)
142{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_BARRIER_FLUSH);
144 printk("reiserfs: disabling flush barriers on %s\n",
145 reiserfs_bdevname(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148static struct reiserfs_bitmap_node *allocate_bitmap_node(struct super_block
149 *p_s_sb)
150{
151 struct reiserfs_bitmap_node *bn;
152 static int id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Pekka Enbergd739b422006-02-01 03:06:43 -0800154 bn = kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 if (!bn) {
156 return NULL;
157 }
Pekka Enbergd739b422006-02-01 03:06:43 -0800158 bn->data = kzalloc(p_s_sb->s_blocksize, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 if (!bn->data) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800160 kfree(bn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 return NULL;
162 }
163 bn->id = id++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 INIT_LIST_HEAD(&bn->list);
165 return bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168static struct reiserfs_bitmap_node *get_bitmap_node(struct super_block *p_s_sb)
169{
170 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
171 struct reiserfs_bitmap_node *bn = NULL;
172 struct list_head *entry = journal->j_bitmap_nodes.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700174 journal->j_used_bitmap_nodes++;
175 repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 if (entry != &journal->j_bitmap_nodes) {
178 bn = list_entry(entry, struct reiserfs_bitmap_node, list);
179 list_del(entry);
180 memset(bn->data, 0, p_s_sb->s_blocksize);
181 journal->j_free_bitmap_nodes--;
182 return bn;
183 }
184 bn = allocate_bitmap_node(p_s_sb);
185 if (!bn) {
186 yield();
187 goto repeat;
188 }
189 return bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191static inline void free_bitmap_node(struct super_block *p_s_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192 struct reiserfs_bitmap_node *bn)
193{
194 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
195 journal->j_used_bitmap_nodes--;
196 if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800197 kfree(bn->data);
198 kfree(bn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 } else {
200 list_add(&bn->list, &journal->j_bitmap_nodes);
201 journal->j_free_bitmap_nodes++;
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205static void allocate_bitmap_nodes(struct super_block *p_s_sb)
206{
207 int i;
208 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
209 struct reiserfs_bitmap_node *bn = NULL;
210 for (i = 0; i < REISERFS_MIN_BITMAP_NODES; i++) {
211 bn = allocate_bitmap_node(p_s_sb);
212 if (bn) {
213 list_add(&bn->list, &journal->j_bitmap_nodes);
214 journal->j_free_bitmap_nodes++;
215 } else {
216 break; // this is ok, we'll try again when more are needed
217 }
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static int set_bit_in_list_bitmap(struct super_block *p_s_sb, int block,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 struct reiserfs_list_bitmap *jb)
223{
224 int bmap_nr = block / (p_s_sb->s_blocksize << 3);
225 int bit_nr = block % (p_s_sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227 if (!jb->bitmaps[bmap_nr]) {
228 jb->bitmaps[bmap_nr] = get_bitmap_node(p_s_sb);
229 }
230 set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data);
231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static void cleanup_bitmap_list(struct super_block *p_s_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 struct reiserfs_list_bitmap *jb)
236{
237 int i;
238 if (jb->bitmaps == NULL)
239 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 for (i = 0; i < SB_BMAP_NR(p_s_sb); i++) {
242 if (jb->bitmaps[i]) {
243 free_bitmap_node(p_s_sb, jb->bitmaps[i]);
244 jb->bitmaps[i] = NULL;
245 }
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/*
250** only call this on FS unmount.
251*/
252static int free_list_bitmaps(struct super_block *p_s_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253 struct reiserfs_list_bitmap *jb_array)
254{
255 int i;
256 struct reiserfs_list_bitmap *jb;
257 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
258 jb = jb_array + i;
259 jb->journal_list = NULL;
260 cleanup_bitmap_list(p_s_sb, jb);
261 vfree(jb->bitmaps);
262 jb->bitmaps = NULL;
263 }
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700267static int free_bitmap_nodes(struct super_block *p_s_sb)
268{
269 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
270 struct list_head *next = journal->j_bitmap_nodes.next;
271 struct reiserfs_bitmap_node *bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273 while (next != &journal->j_bitmap_nodes) {
274 bn = list_entry(next, struct reiserfs_bitmap_node, list);
275 list_del(next);
Pekka Enbergd739b422006-02-01 03:06:43 -0800276 kfree(bn->data);
277 kfree(bn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700278 next = journal->j_bitmap_nodes.next;
279 journal->j_free_bitmap_nodes--;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
286** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
287** jb_array is the array to be filled in.
288*/
289int reiserfs_allocate_list_bitmaps(struct super_block *p_s_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290 struct reiserfs_list_bitmap *jb_array,
291 int bmap_nr)
292{
293 int i;
294 int failed = 0;
295 struct reiserfs_list_bitmap *jb;
296 int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
299 jb = jb_array + i;
300 jb->journal_list = NULL;
301 jb->bitmaps = vmalloc(mem);
302 if (!jb->bitmaps) {
303 reiserfs_warning(p_s_sb,
304 "clm-2000, unable to allocate bitmaps for journal lists");
305 failed = 1;
306 break;
307 }
308 memset(jb->bitmaps, 0, mem);
309 }
310 if (failed) {
311 free_list_bitmaps(p_s_sb, jb_array);
312 return -1;
313 }
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317/*
318** find an available list bitmap. If you can't find one, flush a commit list
319** and try again
320*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700321static struct reiserfs_list_bitmap *get_list_bitmap(struct super_block *p_s_sb,
322 struct reiserfs_journal_list
323 *jl)
324{
325 int i, j;
326 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
327 struct reiserfs_list_bitmap *jb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700329 for (j = 0; j < (JOURNAL_NUM_BITMAPS * 3); j++) {
330 i = journal->j_list_bitmap_index;
331 journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS;
332 jb = journal->j_list_bitmap + i;
333 if (journal->j_list_bitmap[i].journal_list) {
334 flush_commit_list(p_s_sb,
335 journal->j_list_bitmap[i].
336 journal_list, 1);
337 if (!journal->j_list_bitmap[i].journal_list) {
338 break;
339 }
340 } else {
341 break;
342 }
343 }
344 if (jb->journal_list) { /* double check to make sure if flushed correctly */
345 return NULL;
346 }
347 jb->journal_list = jl;
348 return jb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351/*
352** allocates a new chunk of X nodes, and links them all together as a list.
353** Uses the cnode->next and cnode->prev pointers
354** returns NULL on failure
355*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700356static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes)
357{
358 struct reiserfs_journal_cnode *head;
359 int i;
360 if (num_cnodes <= 0) {
361 return NULL;
362 }
363 head = vmalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode));
364 if (!head) {
365 return NULL;
366 }
367 memset(head, 0, num_cnodes * sizeof(struct reiserfs_journal_cnode));
368 head[0].prev = NULL;
369 head[0].next = head + 1;
370 for (i = 1; i < num_cnodes; i++) {
371 head[i].prev = head + (i - 1);
372 head[i].next = head + (i + 1); /* if last one, overwrite it after the if */
373 }
374 head[num_cnodes - 1].next = NULL;
375 return head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/*
379** pulls a cnode off the free list, or returns NULL on failure
380*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381static struct reiserfs_journal_cnode *get_cnode(struct super_block *p_s_sb)
382{
383 struct reiserfs_journal_cnode *cn;
384 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700386 reiserfs_check_lock_depth(p_s_sb, "get_cnode");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 if (journal->j_cnode_free <= 0) {
389 return NULL;
390 }
391 journal->j_cnode_used++;
392 journal->j_cnode_free--;
393 cn = journal->j_cnode_free_list;
394 if (!cn) {
395 return cn;
396 }
397 if (cn->next) {
398 cn->next->prev = NULL;
399 }
400 journal->j_cnode_free_list = cn->next;
401 memset(cn, 0, sizeof(struct reiserfs_journal_cnode));
402 return cn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/*
406** returns a cnode to the free list
407*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408static void free_cnode(struct super_block *p_s_sb,
409 struct reiserfs_journal_cnode *cn)
410{
411 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 reiserfs_check_lock_depth(p_s_sb, "free_cnode");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700415 journal->j_cnode_used--;
416 journal->j_cnode_free++;
417 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
418 cn->next = journal->j_cnode_free_list;
419 if (journal->j_cnode_free_list) {
420 journal->j_cnode_free_list->prev = cn;
421 }
422 cn->prev = NULL; /* not needed with the memset, but I might kill the memset, and forget to do this */
423 journal->j_cnode_free_list = cn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426static void clear_prepared_bits(struct buffer_head *bh)
427{
428 clear_buffer_journal_prepared(bh);
429 clear_buffer_journal_restore_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/* utility function to force a BUG if it is called without the big
433** kernel lock held. caller is the string printed just before calling BUG()
434*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700435void reiserfs_check_lock_depth(struct super_block *sb, char *caller)
436{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#ifdef CONFIG_SMP
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700438 if (current->lock_depth < 0) {
439 reiserfs_panic(sb, "%s called without kernel lock held",
440 caller);
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
445}
446
447/* return a cnode with same dev, block number and size in table, or null if not found */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct
449 super_block
450 *sb,
451 struct
452 reiserfs_journal_cnode
453 **table,
454 long bl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456 struct reiserfs_journal_cnode *cn;
457 cn = journal_hash(table, sb, bl);
458 while (cn) {
459 if (cn->blocknr == bl && cn->sb == sb)
460 return cn;
461 cn = cn->hnext;
462 }
463 return (struct reiserfs_journal_cnode *)0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
468** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
469** being overwritten by a replay after crashing.
470**
471** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
472** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
473** sure you never write the block without logging it.
474**
475** next_zero_bit is a suggestion about the next block to try for find_forward.
476** when bl is rejected because it is set in a journal list bitmap, we search
477** for the next zero bit in the bitmap that rejected bl. Then, we return that
478** through next_zero_bit for find_forward to try.
479**
480** Just because we return something in next_zero_bit does not mean we won't
481** reject it on the next call to reiserfs_in_journal
482**
483*/
484int reiserfs_in_journal(struct super_block *p_s_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 int bmap_nr, int bit_nr, int search_all,
486 b_blocknr_t * next_zero_bit)
487{
488 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
489 struct reiserfs_journal_cnode *cn;
490 struct reiserfs_list_bitmap *jb;
491 int i;
492 unsigned long bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700494 *next_zero_bit = 0; /* always start this at zero. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700496 PROC_INFO_INC(p_s_sb, journal.in_journal);
497 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
498 ** if we crash before the transaction that freed it commits, this transaction won't
499 ** have committed either, and the block will never be written
500 */
501 if (search_all) {
502 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
503 PROC_INFO_INC(p_s_sb, journal.in_journal_bitmap);
504 jb = journal->j_list_bitmap + i;
505 if (jb->journal_list && jb->bitmaps[bmap_nr] &&
506 test_bit(bit_nr,
507 (unsigned long *)jb->bitmaps[bmap_nr]->
508 data)) {
509 *next_zero_bit =
510 find_next_zero_bit((unsigned long *)
511 (jb->bitmaps[bmap_nr]->
512 data),
513 p_s_sb->s_blocksize << 3,
514 bit_nr + 1);
515 return 1;
516 }
517 }
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700520 bl = bmap_nr * (p_s_sb->s_blocksize << 3) + bit_nr;
521 /* is it in any old transactions? */
522 if (search_all
523 && (cn =
524 get_journal_hash_dev(p_s_sb, journal->j_list_hash_table, bl))) {
525 return 1;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700528 /* is it in the current transaction. This should never happen */
529 if ((cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, bl))) {
530 BUG();
531 return 1;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700534 PROC_INFO_INC(p_s_sb, journal.in_journal_reusable);
535 /* safe for reuse */
536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/* insert cn into table
540*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700541static inline void insert_journal_hash(struct reiserfs_journal_cnode **table,
542 struct reiserfs_journal_cnode *cn)
543{
544 struct reiserfs_journal_cnode *cn_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 cn_orig = journal_hash(table, cn->sb, cn->blocknr);
547 cn->hnext = cn_orig;
548 cn->hprev = NULL;
549 if (cn_orig) {
550 cn_orig->hprev = cn;
551 }
552 journal_hash(table, cn->sb, cn->blocknr) = cn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/* lock the current transaction */
Jesper Juhl77933d72005-07-27 11:46:09 -0700556static inline void lock_journal(struct super_block *p_s_sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557{
558 PROC_INFO_INC(p_s_sb, journal.lock_journal);
559 down(&SB_JOURNAL(p_s_sb)->j_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/* unlock the current transaction */
Jesper Juhl77933d72005-07-27 11:46:09 -0700563static inline void unlock_journal(struct super_block *p_s_sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700564{
565 up(&SB_JOURNAL(p_s_sb)->j_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static inline void get_journal_list(struct reiserfs_journal_list *jl)
569{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570 jl->j_refcount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573static inline void put_journal_list(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700574 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 if (jl->j_refcount < 1) {
577 reiserfs_panic(s, "trans id %lu, refcount at %d",
578 jl->j_trans_id, jl->j_refcount);
579 }
580 if (--jl->j_refcount == 0)
Pekka Enbergd739b422006-02-01 03:06:43 -0800581 kfree(jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/*
585** this used to be much more involved, and I'm keeping it just in case things get ugly again.
586** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
587** transaction.
588*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700589static void cleanup_freed_for_journal_list(struct super_block *p_s_sb,
590 struct reiserfs_journal_list *jl)
591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700593 struct reiserfs_list_bitmap *jb = jl->j_list_bitmap;
594 if (jb) {
595 cleanup_bitmap_list(p_s_sb, jb);
596 }
597 jl->j_list_bitmap->journal_list = NULL;
598 jl->j_list_bitmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601static int journal_list_still_alive(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700602 unsigned long trans_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604 struct reiserfs_journal *journal = SB_JOURNAL(s);
605 struct list_head *entry = &journal->j_journal_list;
606 struct reiserfs_journal_list *jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700608 if (!list_empty(entry)) {
609 jl = JOURNAL_LIST_ENTRY(entry->next);
610 if (jl->j_trans_id <= trans_id) {
611 return 1;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
618{
619 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700621 if (buffer_journaled(bh)) {
622 reiserfs_warning(NULL,
623 "clm-2084: pinned buffer %lu:%s sent to disk",
624 bh->b_blocknr, bdevname(bh->b_bdev, b));
625 }
626 if (uptodate)
627 set_buffer_uptodate(bh);
628 else
629 clear_buffer_uptodate(bh);
630 unlock_buffer(bh);
631 put_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700634static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate)
635{
636 if (uptodate)
637 set_buffer_uptodate(bh);
638 else
639 clear_buffer_uptodate(bh);
640 unlock_buffer(bh);
641 put_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700644static void submit_logged_buffer(struct buffer_head *bh)
645{
646 get_bh(bh);
647 bh->b_end_io = reiserfs_end_buffer_io_sync;
648 clear_buffer_journal_new(bh);
649 clear_buffer_dirty(bh);
650 if (!test_clear_buffer_journal_test(bh))
651 BUG();
652 if (!buffer_uptodate(bh))
653 BUG();
654 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657static void submit_ordered_buffer(struct buffer_head *bh)
658{
659 get_bh(bh);
660 bh->b_end_io = reiserfs_end_ordered_io;
661 clear_buffer_dirty(bh);
662 if (!buffer_uptodate(bh))
663 BUG();
664 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700667static int submit_barrier_buffer(struct buffer_head *bh)
668{
669 get_bh(bh);
670 bh->b_end_io = reiserfs_end_ordered_io;
671 clear_buffer_dirty(bh);
672 if (!buffer_uptodate(bh))
673 BUG();
674 return submit_bh(WRITE_BARRIER, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677static void check_barrier_completion(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700678 struct buffer_head *bh)
679{
680 if (buffer_eopnotsupp(bh)) {
681 clear_buffer_eopnotsupp(bh);
682 disable_barrier(s);
683 set_buffer_uptodate(bh);
684 set_buffer_dirty(bh);
685 sync_dirty_buffer(bh);
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689#define CHUNK_SIZE 32
690struct buffer_chunk {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700691 struct buffer_head *bh[CHUNK_SIZE];
692 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693};
694
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700695static void write_chunk(struct buffer_chunk *chunk)
696{
697 int i;
698 get_fs_excl();
699 for (i = 0; i < chunk->nr; i++) {
700 submit_logged_buffer(chunk->bh[i]);
701 }
702 chunk->nr = 0;
703 put_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700706static void write_ordered_chunk(struct buffer_chunk *chunk)
707{
708 int i;
709 get_fs_excl();
710 for (i = 0; i < chunk->nr; i++) {
711 submit_ordered_buffer(chunk->bh[i]);
712 }
713 chunk->nr = 0;
714 put_fs_excl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700718 spinlock_t * lock, void (fn) (struct buffer_chunk *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 int ret = 0;
721 if (chunk->nr >= CHUNK_SIZE)
722 BUG();
723 chunk->bh[chunk->nr++] = bh;
724 if (chunk->nr >= CHUNK_SIZE) {
725 ret = 1;
726 if (lock)
727 spin_unlock(lock);
728 fn(chunk);
729 if (lock)
730 spin_lock(lock);
731 }
732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736static struct reiserfs_jh *alloc_jh(void)
737{
738 struct reiserfs_jh *jh;
739 while (1) {
740 jh = kmalloc(sizeof(*jh), GFP_NOFS);
741 if (jh) {
742 atomic_inc(&nr_reiserfs_jh);
743 return jh;
744 }
745 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * we want to free the jh when the buffer has been written
751 * and waited on
752 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753void reiserfs_free_jh(struct buffer_head *bh)
754{
755 struct reiserfs_jh *jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700757 jh = bh->b_private;
758 if (jh) {
759 bh->b_private = NULL;
760 jh->bh = NULL;
761 list_del_init(&jh->list);
762 kfree(jh);
763 if (atomic_read(&nr_reiserfs_jh) <= 0)
764 BUG();
765 atomic_dec(&nr_reiserfs_jh);
766 put_bh(bh);
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700771 int tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773 struct reiserfs_jh *jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700775 if (bh->b_private) {
776 spin_lock(&j->j_dirty_buffers_lock);
777 if (!bh->b_private) {
778 spin_unlock(&j->j_dirty_buffers_lock);
779 goto no_jh;
780 }
781 jh = bh->b_private;
782 list_del_init(&jh->list);
783 } else {
784 no_jh:
785 get_bh(bh);
786 jh = alloc_jh();
787 spin_lock(&j->j_dirty_buffers_lock);
788 /* buffer must be locked for __add_jh, should be able to have
789 * two adds at the same time
790 */
791 if (bh->b_private)
792 BUG();
793 jh->bh = bh;
794 bh->b_private = jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700796 jh->jl = j->j_current_jl;
797 if (tail)
798 list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
799 else {
800 list_add_tail(&jh->list, &jh->jl->j_bh_list);
801 }
802 spin_unlock(&j->j_dirty_buffers_lock);
803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh)
807{
808 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700810int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh)
811{
812 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815#define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700816static int write_ordered_buffers(spinlock_t * lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct reiserfs_journal *j,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700818 struct reiserfs_journal_list *jl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct list_head *list)
820{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700821 struct buffer_head *bh;
822 struct reiserfs_jh *jh;
823 int ret = j->j_errno;
824 struct buffer_chunk chunk;
825 struct list_head tmp;
826 INIT_LIST_HEAD(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700828 chunk.nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 spin_lock(lock);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700830 while (!list_empty(list)) {
831 jh = JH_ENTRY(list->next);
832 bh = jh->bh;
833 get_bh(bh);
834 if (test_set_buffer_locked(bh)) {
835 if (!buffer_dirty(bh)) {
Akinobu Mitaf1166292006-06-26 00:24:46 -0700836 list_move(&jh->list, &tmp);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 goto loop_next;
838 }
839 spin_unlock(lock);
840 if (chunk.nr)
841 write_ordered_chunk(&chunk);
842 wait_on_buffer(bh);
843 cond_resched();
844 spin_lock(lock);
845 goto loop_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Chris Mason3d4492f2006-02-01 03:06:49 -0800847 /* in theory, dirty non-uptodate buffers should never get here,
848 * but the upper layer io error paths still have a few quirks.
849 * Handle them here as gracefully as we can
850 */
851 if (!buffer_uptodate(bh) && buffer_dirty(bh)) {
852 clear_buffer_dirty(bh);
853 ret = -EIO;
854 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700855 if (buffer_dirty(bh)) {
Akinobu Mitaf1166292006-06-26 00:24:46 -0700856 list_move(&jh->list, &tmp);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700857 add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
858 } else {
859 reiserfs_free_jh(bh);
860 unlock_buffer(bh);
861 }
862 loop_next:
863 put_bh(bh);
864 cond_resched_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700866 if (chunk.nr) {
867 spin_unlock(lock);
868 write_ordered_chunk(&chunk);
869 spin_lock(lock);
870 }
871 while (!list_empty(&tmp)) {
872 jh = JH_ENTRY(tmp.prev);
873 bh = jh->bh;
874 get_bh(bh);
875 reiserfs_free_jh(bh);
876
877 if (buffer_locked(bh)) {
878 spin_unlock(lock);
879 wait_on_buffer(bh);
880 spin_lock(lock);
881 }
882 if (!buffer_uptodate(bh)) {
883 ret = -EIO;
884 }
Chris Masond62b1b82006-02-01 03:06:47 -0800885 /* ugly interaction with invalidatepage here.
886 * reiserfs_invalidate_page will pin any buffer that has a valid
887 * journal head from an older transaction. If someone else sets
888 * our buffer dirty after we write it in the first loop, and
889 * then someone truncates the page away, nobody will ever write
890 * the buffer. We're safe if we write the page one last time
891 * after freeing the journal header.
892 */
893 if (buffer_dirty(bh) && unlikely(bh->b_page->mapping == NULL)) {
894 spin_unlock(lock);
895 ll_rw_block(WRITE, 1, &bh);
896 spin_lock(lock);
897 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700898 put_bh(bh);
899 cond_resched_lock(lock);
900 }
901 spin_unlock(lock);
902 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700904
905static int flush_older_commits(struct super_block *s,
906 struct reiserfs_journal_list *jl)
907{
908 struct reiserfs_journal *journal = SB_JOURNAL(s);
909 struct reiserfs_journal_list *other_jl;
910 struct reiserfs_journal_list *first_jl;
911 struct list_head *entry;
912 unsigned long trans_id = jl->j_trans_id;
913 unsigned long other_trans_id;
914 unsigned long first_trans_id;
915
916 find_first:
917 /*
918 * first we walk backwards to find the oldest uncommitted transation
919 */
920 first_jl = jl;
921 entry = jl->j_list.prev;
922 while (1) {
923 other_jl = JOURNAL_LIST_ENTRY(entry);
924 if (entry == &journal->j_journal_list ||
925 atomic_read(&other_jl->j_older_commits_done))
926 break;
927
928 first_jl = other_jl;
929 entry = other_jl->j_list.prev;
930 }
931
932 /* if we didn't find any older uncommitted transactions, return now */
933 if (first_jl == jl) {
934 return 0;
935 }
936
937 first_trans_id = first_jl->j_trans_id;
938
939 entry = &first_jl->j_list;
940 while (1) {
941 other_jl = JOURNAL_LIST_ENTRY(entry);
942 other_trans_id = other_jl->j_trans_id;
943
944 if (other_trans_id < trans_id) {
945 if (atomic_read(&other_jl->j_commit_left) != 0) {
946 flush_commit_list(s, other_jl, 0);
947
948 /* list we were called with is gone, return */
949 if (!journal_list_still_alive(s, trans_id))
950 return 1;
951
952 /* the one we just flushed is gone, this means all
953 * older lists are also gone, so first_jl is no longer
954 * valid either. Go back to the beginning.
955 */
956 if (!journal_list_still_alive
957 (s, other_trans_id)) {
958 goto find_first;
959 }
960 }
961 entry = entry->next;
962 if (entry == &journal->j_journal_list)
963 return 0;
964 } else {
965 return 0;
966 }
967 }
968 return 0;
969}
970int reiserfs_async_progress_wait(struct super_block *s)
971{
972 DEFINE_WAIT(wait);
973 struct reiserfs_journal *j = SB_JOURNAL(s);
974 if (atomic_read(&j->j_async_throttle))
975 blk_congestion_wait(WRITE, HZ / 10);
976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979/*
980** if this journal list still has commit blocks unflushed, send them to disk.
981**
982** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
983** Before the commit block can by written, every other log block must be safely on disk
984**
985*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700986static int flush_commit_list(struct super_block *s,
987 struct reiserfs_journal_list *jl, int flushall)
988{
989 int i;
990 int bn;
991 struct buffer_head *tbh = NULL;
992 unsigned long trans_id = jl->j_trans_id;
993 struct reiserfs_journal *journal = SB_JOURNAL(s);
994 int barrier = 0;
995 int retval = 0;
Chris Masone0e851c2006-02-01 03:06:49 -0800996 int write_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700998 reiserfs_check_lock_depth(s, "flush_commit_list");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001000 if (atomic_read(&jl->j_older_commits_done)) {
1001 return 0;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001004 get_fs_excl();
Jens Axboe22e2c502005-06-27 10:55:12 +02001005
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001006 /* before we can put our commit blocks on disk, we have to make sure everyone older than
1007 ** us is on disk too
1008 */
1009 BUG_ON(jl->j_len <= 0);
1010 BUG_ON(trans_id == journal->j_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 get_journal_list(jl);
1013 if (flushall) {
1014 if (flush_older_commits(s, jl) == 1) {
1015 /* list disappeared during flush_older_commits. return */
1016 goto put_jl;
1017 }
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 /* make sure nobody is trying to flush this one at the same time */
1021 down(&jl->j_commit_lock);
1022 if (!journal_list_still_alive(s, trans_id)) {
1023 up(&jl->j_commit_lock);
1024 goto put_jl;
1025 }
1026 BUG_ON(jl->j_trans_id == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001028 /* this commit is done, exit */
1029 if (atomic_read(&(jl->j_commit_left)) <= 0) {
1030 if (flushall) {
1031 atomic_set(&(jl->j_older_commits_done), 1);
1032 }
1033 up(&jl->j_commit_lock);
1034 goto put_jl;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001037 if (!list_empty(&jl->j_bh_list)) {
Chris Mason3d4492f2006-02-01 03:06:49 -08001038 int ret;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039 unlock_kernel();
Chris Mason3d4492f2006-02-01 03:06:49 -08001040 ret = write_ordered_buffers(&journal->j_dirty_buffers_lock,
1041 journal, jl, &jl->j_bh_list);
1042 if (ret < 0 && retval == 0)
1043 retval = ret;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 lock_kernel();
1045 }
1046 BUG_ON(!list_empty(&jl->j_bh_list));
1047 /*
1048 * for the description block and all the log blocks, submit any buffers
Chris Masone0e851c2006-02-01 03:06:49 -08001049 * that haven't already reached the disk. Try to write at least 256
1050 * log blocks. later on, we will only wait on blocks that correspond
1051 * to this transaction, but while we're unplugging we might as well
1052 * get a chunk of data on there.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 */
1054 atomic_inc(&journal->j_async_throttle);
Chris Masone0e851c2006-02-01 03:06:49 -08001055 write_len = jl->j_len + 1;
1056 if (write_len < 256)
1057 write_len = 256;
1058 for (i = 0 ; i < write_len ; i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001059 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start + i) %
1060 SB_ONDISK_JOURNAL_SIZE(s);
1061 tbh = journal_find_get_block(s, bn);
Chris Masone0e851c2006-02-01 03:06:49 -08001062 if (tbh) {
1063 if (buffer_dirty(tbh))
1064 ll_rw_block(WRITE, 1, &tbh) ;
1065 put_bh(tbh) ;
1066 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001067 }
1068 atomic_dec(&journal->j_async_throttle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Jeff Mahoney5d5e81562005-12-14 14:38:36 -05001070 /* We're skipping the commit if there's an error */
1071 if (retval || reiserfs_is_journal_aborted(journal))
1072 barrier = 0;
1073
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001074 /* wait on everything written so far before writing the commit
1075 * if we are in barrier mode, send the commit down now
1076 */
1077 barrier = reiserfs_barrier_flush(s);
1078 if (barrier) {
1079 int ret;
1080 lock_buffer(jl->j_commit_bh);
1081 ret = submit_barrier_buffer(jl->j_commit_bh);
1082 if (ret == -EOPNOTSUPP) {
1083 set_buffer_uptodate(jl->j_commit_bh);
1084 disable_barrier(s);
1085 barrier = 0;
1086 }
1087 }
1088 for (i = 0; i < (jl->j_len + 1); i++) {
1089 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
1090 (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s);
1091 tbh = journal_find_get_block(s, bn);
1092 wait_on_buffer(tbh);
1093 // since we're using ll_rw_blk above, it might have skipped over
1094 // a locked buffer. Double check here
1095 //
1096 if (buffer_dirty(tbh)) /* redundant, sync_dirty_buffer() checks */
1097 sync_dirty_buffer(tbh);
1098 if (unlikely(!buffer_uptodate(tbh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001100 reiserfs_warning(s, "journal-601, buffer write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001102 retval = -EIO;
1103 }
1104 put_bh(tbh); /* once for journal_find_get_block */
1105 put_bh(tbh); /* once due to original getblk in do_journal_end */
1106 atomic_dec(&(jl->j_commit_left));
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 BUG_ON(atomic_read(&(jl->j_commit_left)) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001111 if (!barrier) {
Jeff Mahoney5d5e81562005-12-14 14:38:36 -05001112 /* If there was a write error in the journal - we can't commit
1113 * this transaction - it will be invalid and, if successful,
1114 * will just end up propogating the write error out to
1115 * the file system. */
1116 if (likely(!retval && !reiserfs_is_journal_aborted (journal))) {
1117 if (buffer_dirty(jl->j_commit_bh))
1118 BUG();
1119 mark_buffer_dirty(jl->j_commit_bh) ;
1120 sync_dirty_buffer(jl->j_commit_bh) ;
1121 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001122 } else
1123 wait_on_buffer(jl->j_commit_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001125 check_barrier_completion(s, jl->j_commit_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001127 /* If there was a write error in the journal - we can't commit this
1128 * transaction - it will be invalid and, if successful, will just end
1129 * up propogating the write error out to the filesystem. */
1130 if (unlikely(!buffer_uptodate(jl->j_commit_bh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001132 reiserfs_warning(s, "journal-615: buffer write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001134 retval = -EIO;
1135 }
1136 bforget(jl->j_commit_bh);
1137 if (journal->j_last_commit_id != 0 &&
1138 (jl->j_trans_id - journal->j_last_commit_id) != 1) {
1139 reiserfs_warning(s, "clm-2200: last commit %lu, current %lu",
1140 journal->j_last_commit_id, jl->j_trans_id);
1141 }
1142 journal->j_last_commit_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001144 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1145 cleanup_freed_for_journal_list(s, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001147 retval = retval ? retval : journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 /* mark the metadata dirty */
1150 if (!retval)
1151 dirty_one_transaction(s, jl);
1152 atomic_dec(&(jl->j_commit_left));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001154 if (flushall) {
1155 atomic_set(&(jl->j_older_commits_done), 1);
1156 }
1157 up(&jl->j_commit_lock);
1158 put_jl:
1159 put_journal_list(s, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161 if (retval)
1162 reiserfs_abort(s, retval, "Journal write error in %s",
1163 __FUNCTION__);
1164 put_fs_excl();
1165 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168/*
1169** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1170** returns NULL if it can't find anything
1171*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001172static struct reiserfs_journal_list *find_newer_jl_for_cn(struct
1173 reiserfs_journal_cnode
1174 *cn)
1175{
1176 struct super_block *sb = cn->sb;
1177 b_blocknr_t blocknr = cn->blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001179 cn = cn->hprev;
1180 while (cn) {
1181 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
1182 return cn->jlist;
1183 }
1184 cn = cn->hprev;
1185 }
1186 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001189static void remove_journal_hash(struct super_block *,
1190 struct reiserfs_journal_cnode **,
1191 struct reiserfs_journal_list *, unsigned long,
1192 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
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*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001199static void remove_all_from_journal_list(struct super_block *p_s_sb,
1200 struct reiserfs_journal_list *jl,
1201 int debug)
1202{
1203 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1204 struct reiserfs_journal_cnode *cn, *last;
1205 cn = jl->j_realblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001207 /* 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) {
1213 reiserfs_warning(p_s_sb,
1214 "block %u, bh is %d, state %ld",
1215 cn->blocknr, cn->bh ? 1 : 0,
1216 cn->state);
1217 }
1218 cn->state = 0;
1219 remove_journal_hash(p_s_sb, journal->j_list_hash_table,
1220 jl, cn->blocknr, 1);
1221 }
1222 last = cn;
1223 cn = cn->next;
1224 free_cnode(p_s_sb, last);
1225 }
1226 jl->j_realblock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
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*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001236static int _update_journal_header_block(struct super_block *p_s_sb,
1237 unsigned long offset,
1238 unsigned long trans_id)
1239{
1240 struct reiserfs_journal_header *jh;
1241 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001243 if (reiserfs_is_journal_aborted(journal))
1244 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 if (trans_id >= journal->j_last_flush_trans_id) {
1247 if (buffer_locked((journal->j_header_bh))) {
1248 wait_on_buffer((journal->j_header_bh));
1249 if (unlikely(!buffer_uptodate(journal->j_header_bh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001251 reiserfs_warning(p_s_sb,
1252 "journal-699: buffer write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001254 return -EIO;
1255 }
1256 }
1257 journal->j_last_flush_trans_id = trans_id;
1258 journal->j_first_unflushed_offset = offset;
1259 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->
1260 b_data);
1261 jh->j_last_flush_trans_id = cpu_to_le32(trans_id);
1262 jh->j_first_unflushed_offset = cpu_to_le32(offset);
1263 jh->j_mount_id = cpu_to_le32(journal->j_mount_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 if (reiserfs_barrier_flush(p_s_sb)) {
1266 int ret;
1267 lock_buffer(journal->j_header_bh);
1268 ret = submit_barrier_buffer(journal->j_header_bh);
1269 if (ret == -EOPNOTSUPP) {
1270 set_buffer_uptodate(journal->j_header_bh);
1271 disable_barrier(p_s_sb);
1272 goto sync;
1273 }
1274 wait_on_buffer(journal->j_header_bh);
1275 check_barrier_completion(p_s_sb, journal->j_header_bh);
1276 } else {
1277 sync:
1278 set_buffer_dirty(journal->j_header_bh);
1279 sync_dirty_buffer(journal->j_header_bh);
1280 }
1281 if (!buffer_uptodate(journal->j_header_bh)) {
1282 reiserfs_warning(p_s_sb,
1283 "journal-837: IO error during journal replay");
1284 return -EIO;
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001290static int update_journal_header_block(struct super_block *p_s_sb,
1291 unsigned long offset,
1292 unsigned long trans_id)
1293{
1294 return _update_journal_header_block(p_s_sb, offset, trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297/*
1298** flush any and all journal lists older than you are
1299** can only be called from flush_journal_list
1300*/
1301static int flush_older_journal_lists(struct super_block *p_s_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001302 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001304 struct list_head *entry;
1305 struct reiserfs_journal_list *other_jl;
1306 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
1307 unsigned long trans_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001309 /* we know we are the only ones flushing things, no extra race
1310 * protection is required.
1311 */
1312 restart:
1313 entry = journal->j_journal_list.next;
1314 /* Did we wrap? */
1315 if (entry == &journal->j_journal_list)
1316 return 0;
1317 other_jl = JOURNAL_LIST_ENTRY(entry);
1318 if (other_jl->j_trans_id < trans_id) {
1319 BUG_ON(other_jl->j_refcount <= 0);
1320 /* do not flush all */
1321 flush_journal_list(p_s_sb, other_jl, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001323 /* other_jl is now deleted from the list */
1324 goto restart;
1325 }
1326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329static void del_from_work_list(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001330 struct reiserfs_journal_list *jl)
1331{
1332 struct reiserfs_journal *journal = SB_JOURNAL(s);
1333 if (!list_empty(&jl->j_working_list)) {
1334 list_del_init(&jl->j_working_list);
1335 journal->j_num_work_lists--;
1336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339/* flush a journal list, both commit and real blocks
1340**
1341** always set flushall to 1, unless you are calling from inside
1342** flush_journal_list
1343**
1344** IMPORTANT. This can only be called while there are no journal writers,
1345** and the journal is locked. That means it can only be called from
1346** do_journal_end, or by journal_release
1347*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001348static int flush_journal_list(struct super_block *s,
1349 struct reiserfs_journal_list *jl, int flushall)
1350{
1351 struct reiserfs_journal_list *pjl;
1352 struct reiserfs_journal_cnode *cn, *last;
1353 int count;
1354 int was_jwait = 0;
1355 int was_dirty = 0;
1356 struct buffer_head *saved_bh;
1357 unsigned long j_len_saved = jl->j_len;
1358 struct reiserfs_journal *journal = SB_JOURNAL(s);
1359 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001361 BUG_ON(j_len_saved <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001363 if (atomic_read(&journal->j_wcount) != 0) {
1364 reiserfs_warning(s,
1365 "clm-2048: flush_journal_list called with wcount %d",
1366 atomic_read(&journal->j_wcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001368 BUG_ON(jl->j_trans_id == 0);
1369
1370 /* if flushall == 0, the lock is already held */
1371 if (flushall) {
1372 down(&journal->j_flush_sem);
1373 } else if (!down_trylock(&journal->j_flush_sem)) {
1374 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001376
1377 count = 0;
1378 if (j_len_saved > journal->j_trans_max) {
1379 reiserfs_panic(s,
1380 "journal-715: flush_journal_list, length is %lu, trans id %lu\n",
1381 j_len_saved, jl->j_trans_id);
1382 return 0;
1383 }
1384
1385 get_fs_excl();
1386
1387 /* if all the work is already done, get out of here */
1388 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1389 atomic_read(&(jl->j_commit_left)) <= 0) {
1390 goto flush_older_and_return;
1391 }
1392
1393 /* start by putting the commit list on disk. This will also flush
1394 ** the commit lists of any olders transactions
1395 */
1396 flush_commit_list(s, jl, 1);
1397
1398 if (!(jl->j_state & LIST_DIRTY)
1399 && !reiserfs_is_journal_aborted(journal))
1400 BUG();
1401
1402 /* are we done now? */
1403 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1404 atomic_read(&(jl->j_commit_left)) <= 0) {
1405 goto flush_older_and_return;
1406 }
1407
1408 /* loop through each cnode, see if we need to write it,
1409 ** or wait on a more recent transaction, or just ignore it
1410 */
1411 if (atomic_read(&(journal->j_wcount)) != 0) {
1412 reiserfs_panic(s,
1413 "journal-844: panic journal list is flushing, wcount is not 0\n");
1414 }
1415 cn = jl->j_realblock;
1416 while (cn) {
1417 was_jwait = 0;
1418 was_dirty = 0;
1419 saved_bh = NULL;
1420 /* blocknr of 0 is no longer in the hash, ignore it */
1421 if (cn->blocknr == 0) {
1422 goto free_cnode;
1423 }
1424
1425 /* This transaction failed commit. Don't write out to the disk */
1426 if (!(jl->j_state & LIST_DIRTY))
1427 goto free_cnode;
1428
1429 pjl = find_newer_jl_for_cn(cn);
1430 /* the order is important here. We check pjl to make sure we
1431 ** don't clear BH_JDirty_wait if we aren't the one writing this
1432 ** block to disk
1433 */
1434 if (!pjl && cn->bh) {
1435 saved_bh = cn->bh;
1436
1437 /* we do this to make sure nobody releases the buffer while
1438 ** we are working with it
1439 */
1440 get_bh(saved_bh);
1441
1442 if (buffer_journal_dirty(saved_bh)) {
1443 BUG_ON(!can_dirty(cn));
1444 was_jwait = 1;
1445 was_dirty = 1;
1446 } else if (can_dirty(cn)) {
1447 /* everything with !pjl && jwait should be writable */
1448 BUG();
1449 }
1450 }
1451
1452 /* if someone has this block in a newer transaction, just make
1453 ** sure they are commited, and don't try writing it to disk
1454 */
1455 if (pjl) {
1456 if (atomic_read(&pjl->j_commit_left))
1457 flush_commit_list(s, pjl, 1);
1458 goto free_cnode;
1459 }
1460
1461 /* bh == NULL when the block got to disk on its own, OR,
1462 ** the block got freed in a future transaction
1463 */
1464 if (saved_bh == NULL) {
1465 goto free_cnode;
1466 }
1467
1468 /* this should never happen. kupdate_one_transaction has this list
1469 ** locked while it works, so we should never see a buffer here that
1470 ** is not marked JDirty_wait
1471 */
1472 if ((!was_jwait) && !buffer_locked(saved_bh)) {
1473 reiserfs_warning(s,
1474 "journal-813: BAD! buffer %llu %cdirty %cjwait, "
1475 "not in a newer tranasction",
1476 (unsigned long long)saved_bh->
1477 b_blocknr, was_dirty ? ' ' : '!',
1478 was_jwait ? ' ' : '!');
1479 }
1480 if (was_dirty) {
1481 /* we inc again because saved_bh gets decremented at free_cnode */
1482 get_bh(saved_bh);
1483 set_bit(BLOCK_NEEDS_FLUSH, &cn->state);
1484 lock_buffer(saved_bh);
1485 BUG_ON(cn->blocknr != saved_bh->b_blocknr);
1486 if (buffer_dirty(saved_bh))
1487 submit_logged_buffer(saved_bh);
1488 else
1489 unlock_buffer(saved_bh);
1490 count++;
1491 } else {
1492 reiserfs_warning(s,
1493 "clm-2082: Unable to flush buffer %llu in %s",
1494 (unsigned long long)saved_bh->
1495 b_blocknr, __FUNCTION__);
1496 }
1497 free_cnode:
1498 last = cn;
1499 cn = cn->next;
1500 if (saved_bh) {
1501 /* we incremented this to keep others from taking the buffer head away */
1502 put_bh(saved_bh);
1503 if (atomic_read(&(saved_bh->b_count)) < 0) {
1504 reiserfs_warning(s,
1505 "journal-945: saved_bh->b_count < 0");
1506 }
1507 }
1508 }
1509 if (count > 0) {
1510 cn = jl->j_realblock;
1511 while (cn) {
1512 if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1513 if (!cn->bh) {
1514 reiserfs_panic(s,
1515 "journal-1011: cn->bh is NULL\n");
1516 }
1517 wait_on_buffer(cn->bh);
1518 if (!cn->bh) {
1519 reiserfs_panic(s,
1520 "journal-1012: cn->bh is NULL\n");
1521 }
1522 if (unlikely(!buffer_uptodate(cn->bh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001524 reiserfs_warning(s,
1525 "journal-949: buffer write failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001527 err = -EIO;
1528 }
1529 /* note, we must clear the JDirty_wait bit after the up to date
1530 ** check, otherwise we race against our flushpage routine
1531 */
1532 BUG_ON(!test_clear_buffer_journal_dirty
1533 (cn->bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001535 /* undo the inc from journal_mark_dirty */
1536 put_bh(cn->bh);
1537 brelse(cn->bh);
1538 }
1539 cn = cn->next;
1540 }
1541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001543 if (err)
1544 reiserfs_abort(s, -EIO,
1545 "Write error while pushing transaction to disk in %s",
1546 __FUNCTION__);
1547 flush_older_and_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001549 /* before we can update the journal header block, we _must_ flush all
1550 ** real blocks from all older transactions to disk. This is because
1551 ** once the header block is updated, this transaction will not be
1552 ** replayed after a crash
1553 */
1554 if (flushall) {
1555 flush_older_journal_lists(s, jl);
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001558 err = journal->j_errno;
1559 /* before we can remove everything from the hash tables for this
1560 ** transaction, we must make sure it can never be replayed
1561 **
1562 ** since we are only called from do_journal_end, we know for sure there
1563 ** are no allocations going on while we are flushing journal lists. So,
1564 ** we only need to update the journal header block for the last list
1565 ** being flushed
1566 */
1567 if (!err && flushall) {
1568 err =
1569 update_journal_header_block(s,
1570 (jl->j_start + jl->j_len +
1571 2) % SB_ONDISK_JOURNAL_SIZE(s),
1572 jl->j_trans_id);
1573 if (err)
1574 reiserfs_abort(s, -EIO,
1575 "Write error while updating journal header in %s",
1576 __FUNCTION__);
1577 }
1578 remove_all_from_journal_list(s, jl, 0);
1579 list_del_init(&jl->j_list);
1580 journal->j_num_lists--;
1581 del_from_work_list(s, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001583 if (journal->j_last_flush_id != 0 &&
1584 (jl->j_trans_id - journal->j_last_flush_id) != 1) {
1585 reiserfs_warning(s, "clm-2201: last flush %lu, current %lu",
1586 journal->j_last_flush_id, jl->j_trans_id);
1587 }
1588 journal->j_last_flush_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001590 /* not strictly required since we are freeing the list, but it should
1591 * help find code using dead lists later on
1592 */
1593 jl->j_len = 0;
1594 atomic_set(&(jl->j_nonzerolen), 0);
1595 jl->j_start = 0;
1596 jl->j_realblock = NULL;
1597 jl->j_commit_bh = NULL;
1598 jl->j_trans_id = 0;
1599 jl->j_state = 0;
1600 put_journal_list(s, jl);
1601 if (flushall)
1602 up(&journal->j_flush_sem);
1603 put_fs_excl();
1604 return err;
1605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607static int write_one_transaction(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001608 struct reiserfs_journal_list *jl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct buffer_chunk *chunk)
1610{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001611 struct reiserfs_journal_cnode *cn;
1612 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001614 jl->j_state |= LIST_TOUCHED;
1615 del_from_work_list(s, jl);
1616 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
1617 return 0;
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001620 cn = jl->j_realblock;
1621 while (cn) {
1622 /* if the blocknr == 0, this has been cleared from the hash,
1623 ** skip it
1624 */
1625 if (cn->blocknr == 0) {
1626 goto next;
1627 }
1628 if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
1629 struct buffer_head *tmp_bh;
1630 /* we can race against journal_mark_freed when we try
1631 * to lock_buffer(cn->bh), so we have to inc the buffer
1632 * count, and recheck things after locking
1633 */
1634 tmp_bh = cn->bh;
1635 get_bh(tmp_bh);
1636 lock_buffer(tmp_bh);
1637 if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
1638 if (!buffer_journal_dirty(tmp_bh) ||
1639 buffer_journal_prepared(tmp_bh))
1640 BUG();
1641 add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
1642 ret++;
1643 } else {
1644 /* note, cn->bh might be null now */
1645 unlock_buffer(tmp_bh);
1646 }
1647 put_bh(tmp_bh);
1648 }
1649 next:
1650 cn = cn->next;
1651 cond_resched();
1652 }
1653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656/* used by flush_commit_list */
1657static int dirty_one_transaction(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001658 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001660 struct reiserfs_journal_cnode *cn;
1661 struct reiserfs_journal_list *pjl;
1662 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001664 jl->j_state |= LIST_DIRTY;
1665 cn = jl->j_realblock;
1666 while (cn) {
1667 /* look for a more recent transaction that logged this
1668 ** buffer. Only the most recent transaction with a buffer in
1669 ** it is allowed to send that buffer to disk
1670 */
1671 pjl = find_newer_jl_for_cn(cn);
1672 if (!pjl && cn->blocknr && cn->bh
1673 && buffer_journal_dirty(cn->bh)) {
1674 BUG_ON(!can_dirty(cn));
1675 /* if the buffer is prepared, it will either be logged
1676 * or restored. If restored, we need to make sure
1677 * it actually gets marked dirty
1678 */
1679 clear_buffer_journal_new(cn->bh);
1680 if (buffer_journal_prepared(cn->bh)) {
1681 set_buffer_journal_restore_dirty(cn->bh);
1682 } else {
1683 set_buffer_journal_test(cn->bh);
1684 mark_buffer_dirty(cn->bh);
1685 }
1686 }
1687 cn = cn->next;
1688 }
1689 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692static int kupdate_transactions(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001693 struct reiserfs_journal_list *jl,
1694 struct reiserfs_journal_list **next_jl,
1695 unsigned long *next_trans_id,
1696 int num_blocks, int num_trans)
1697{
1698 int ret = 0;
1699 int written = 0;
1700 int transactions_flushed = 0;
1701 unsigned long orig_trans_id = jl->j_trans_id;
1702 struct buffer_chunk chunk;
1703 struct list_head *entry;
1704 struct reiserfs_journal *journal = SB_JOURNAL(s);
1705 chunk.nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001707 down(&journal->j_flush_sem);
1708 if (!journal_list_still_alive(s, orig_trans_id)) {
1709 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001712 /* we've got j_flush_sem held, nobody is going to delete any
1713 * of these lists out from underneath us
1714 */
1715 while ((num_trans && transactions_flushed < num_trans) ||
1716 (!num_trans && written < num_blocks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001718 if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
1719 atomic_read(&jl->j_commit_left)
1720 || !(jl->j_state & LIST_DIRTY)) {
1721 del_from_work_list(s, jl);
1722 break;
1723 }
1724 ret = write_one_transaction(s, jl, &chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001726 if (ret < 0)
1727 goto done;
1728 transactions_flushed++;
1729 written += ret;
1730 entry = jl->j_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001732 /* did we wrap? */
1733 if (entry == &journal->j_journal_list) {
1734 break;
1735 }
1736 jl = JOURNAL_LIST_ENTRY(entry);
1737
1738 /* don't bother with older transactions */
1739 if (jl->j_trans_id <= orig_trans_id)
1740 break;
1741 }
1742 if (chunk.nr) {
1743 write_chunk(&chunk);
1744 }
1745
1746 done:
1747 up(&journal->j_flush_sem);
1748 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
1751/* for o_sync and fsync heavy applications, they tend to use
1752** all the journa list slots with tiny transactions. These
1753** trigger lots and lots of calls to update the header block, which
1754** adds seeks and slows things down.
1755**
1756** This function tries to clear out a large chunk of the journal lists
1757** at once, which makes everything faster since only the newest journal
1758** list updates the header block
1759*/
1760static int flush_used_journal_lists(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001761 struct reiserfs_journal_list *jl)
1762{
1763 unsigned long len = 0;
1764 unsigned long cur_len;
1765 int ret;
1766 int i;
1767 int limit = 256;
1768 struct reiserfs_journal_list *tjl;
1769 struct reiserfs_journal_list *flush_jl;
1770 unsigned long trans_id;
1771 struct reiserfs_journal *journal = SB_JOURNAL(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001773 flush_jl = tjl = jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001775 /* in data logging mode, try harder to flush a lot of blocks */
1776 if (reiserfs_data_log(s))
1777 limit = 1024;
1778 /* flush for 256 transactions or limit blocks, whichever comes first */
1779 for (i = 0; i < 256 && len < limit; i++) {
1780 if (atomic_read(&tjl->j_commit_left) ||
1781 tjl->j_trans_id < jl->j_trans_id) {
1782 break;
1783 }
1784 cur_len = atomic_read(&tjl->j_nonzerolen);
1785 if (cur_len > 0) {
1786 tjl->j_state &= ~LIST_TOUCHED;
1787 }
1788 len += cur_len;
1789 flush_jl = tjl;
1790 if (tjl->j_list.next == &journal->j_journal_list)
1791 break;
1792 tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001794 /* try to find a group of blocks we can flush across all the
1795 ** transactions, but only bother if we've actually spanned
1796 ** across multiple lists
1797 */
1798 if (flush_jl != jl) {
1799 ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001801 flush_journal_list(s, flush_jl, 1);
1802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805/*
1806** removes any nodes in table with name block and dev as bh.
1807** only touchs the hnext and hprev pointers.
1808*/
1809void remove_journal_hash(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001810 struct reiserfs_journal_cnode **table,
1811 struct reiserfs_journal_list *jl,
1812 unsigned long block, int remove_freed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001814 struct reiserfs_journal_cnode *cur;
1815 struct reiserfs_journal_cnode **head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001817 head = &(journal_hash(table, sb, block));
1818 if (!head) {
1819 return;
1820 }
1821 cur = *head;
1822 while (cur) {
1823 if (cur->blocknr == block && cur->sb == sb
1824 && (jl == NULL || jl == cur->jlist)
1825 && (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1826 if (cur->hnext) {
1827 cur->hnext->hprev = cur->hprev;
1828 }
1829 if (cur->hprev) {
1830 cur->hprev->hnext = cur->hnext;
1831 } else {
1832 *head = cur->hnext;
1833 }
1834 cur->blocknr = 0;
1835 cur->sb = NULL;
1836 cur->state = 0;
1837 if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1838 atomic_dec(&(cur->jlist->j_nonzerolen));
1839 cur->bh = NULL;
1840 cur->jlist = NULL;
1841 }
1842 cur = cur->hnext;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001846static void free_journal_ram(struct super_block *p_s_sb)
1847{
1848 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Pekka Enbergd739b422006-02-01 03:06:43 -08001849 kfree(journal->j_current_jl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001850 journal->j_num_lists--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001852 vfree(journal->j_cnode_free_orig);
1853 free_list_bitmaps(p_s_sb, journal->j_list_bitmap);
1854 free_bitmap_nodes(p_s_sb); /* must be after free_list_bitmaps */
1855 if (journal->j_header_bh) {
1856 brelse(journal->j_header_bh);
1857 }
1858 /* j_header_bh is on the journal dev, make sure not to release the journal
1859 * dev until we brelse j_header_bh
1860 */
1861 release_journal_dev(p_s_sb, journal);
1862 vfree(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865/*
1866** call on unmount. Only set error to 1 if you haven't made your way out
1867** of read_super() yet. Any other caller must keep error at 0.
1868*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001869static int do_journal_release(struct reiserfs_transaction_handle *th,
1870 struct super_block *p_s_sb, int error)
1871{
1872 struct reiserfs_transaction_handle myth;
1873 int flushed = 0;
1874 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001876 /* we only want to flush out transactions if we were called with error == 0
1877 */
1878 if (!error && !(p_s_sb->s_flags & MS_RDONLY)) {
1879 /* end the current trans */
1880 BUG_ON(!th->t_trans_id);
1881 do_journal_end(th, p_s_sb, 10, FLUSH_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001883 /* make sure something gets logged to force our way into the flush code */
1884 if (!journal_join(&myth, p_s_sb, 1)) {
1885 reiserfs_prepare_for_journal(p_s_sb,
1886 SB_BUFFER_WITH_SB(p_s_sb),
1887 1);
1888 journal_mark_dirty(&myth, p_s_sb,
1889 SB_BUFFER_WITH_SB(p_s_sb));
1890 do_journal_end(&myth, p_s_sb, 1, FLUSH_ALL);
1891 flushed = 1;
1892 }
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001895 /* this also catches errors during the do_journal_end above */
1896 if (!error && reiserfs_is_journal_aborted(journal)) {
1897 memset(&myth, 0, sizeof(myth));
1898 if (!journal_join_abort(&myth, p_s_sb, 1)) {
1899 reiserfs_prepare_for_journal(p_s_sb,
1900 SB_BUFFER_WITH_SB(p_s_sb),
1901 1);
1902 journal_mark_dirty(&myth, p_s_sb,
1903 SB_BUFFER_WITH_SB(p_s_sb));
1904 do_journal_end(&myth, p_s_sb, 1, FLUSH_ALL);
1905 }
1906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001908 reiserfs_mounted_fs_count--;
1909 /* wait for all commits to finish */
1910 cancel_delayed_work(&SB_JOURNAL(p_s_sb)->j_work);
1911 flush_workqueue(commit_wq);
1912 if (!reiserfs_mounted_fs_count) {
1913 destroy_workqueue(commit_wq);
1914 commit_wq = NULL;
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001917 free_journal_ram(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
1922/*
1923** call on unmount. flush all journal trans, release all alloc'd ram
1924*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001925int journal_release(struct reiserfs_transaction_handle *th,
1926 struct super_block *p_s_sb)
1927{
1928 return do_journal_release(th, p_s_sb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/*
1932** only call from an error condition inside reiserfs_read_super!
1933*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001934int journal_release_error(struct reiserfs_transaction_handle *th,
1935 struct super_block *p_s_sb)
1936{
1937 return do_journal_release(th, p_s_sb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
1940/* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001941static int journal_compare_desc_commit(struct super_block *p_s_sb,
1942 struct reiserfs_journal_desc *desc,
1943 struct reiserfs_journal_commit *commit)
1944{
1945 if (get_commit_trans_id(commit) != get_desc_trans_id(desc) ||
1946 get_commit_trans_len(commit) != get_desc_trans_len(desc) ||
1947 get_commit_trans_len(commit) > SB_JOURNAL(p_s_sb)->j_trans_max ||
1948 get_commit_trans_len(commit) <= 0) {
1949 return 1;
1950 }
1951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/* returns 0 if it did not find a description block
1955** returns -1 if it found a corrupt commit block
1956** returns 1 if both desc and commit were valid
1957*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001958static int journal_transaction_is_valid(struct super_block *p_s_sb,
1959 struct buffer_head *d_bh,
1960 unsigned long *oldest_invalid_trans_id,
1961 unsigned long *newest_mount_id)
1962{
1963 struct reiserfs_journal_desc *desc;
1964 struct reiserfs_journal_commit *commit;
1965 struct buffer_head *c_bh;
1966 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001968 if (!d_bh)
1969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001971 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
1972 if (get_desc_trans_len(desc) > 0
1973 && !memcmp(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8)) {
1974 if (oldest_invalid_trans_id && *oldest_invalid_trans_id
1975 && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
1976 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1977 "journal-986: transaction "
1978 "is valid returning because trans_id %d is greater than "
1979 "oldest_invalid %lu",
1980 get_desc_trans_id(desc),
1981 *oldest_invalid_trans_id);
1982 return 0;
1983 }
1984 if (newest_mount_id
1985 && *newest_mount_id > get_desc_mount_id(desc)) {
1986 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1987 "journal-1087: transaction "
1988 "is valid returning because mount_id %d is less than "
1989 "newest_mount_id %lu",
1990 get_desc_mount_id(desc),
1991 *newest_mount_id);
1992 return -1;
1993 }
1994 if (get_desc_trans_len(desc) > SB_JOURNAL(p_s_sb)->j_trans_max) {
1995 reiserfs_warning(p_s_sb,
1996 "journal-2018: Bad transaction length %d encountered, ignoring transaction",
1997 get_desc_trans_len(desc));
1998 return -1;
1999 }
2000 offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002002 /* ok, we have a journal description block, lets see if the transaction was valid */
2003 c_bh =
2004 journal_bread(p_s_sb,
2005 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2006 ((offset + get_desc_trans_len(desc) +
2007 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
2008 if (!c_bh)
2009 return 0;
2010 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
2011 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
2012 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2013 "journal_transaction_is_valid, commit offset %ld had bad "
2014 "time %d or length %d",
2015 c_bh->b_blocknr -
2016 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2017 get_commit_trans_id(commit),
2018 get_commit_trans_len(commit));
2019 brelse(c_bh);
2020 if (oldest_invalid_trans_id) {
2021 *oldest_invalid_trans_id =
2022 get_desc_trans_id(desc);
2023 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2024 "journal-1004: "
2025 "transaction_is_valid setting oldest invalid trans_id "
2026 "to %d",
2027 get_desc_trans_id(desc));
2028 }
2029 return -1;
2030 }
2031 brelse(c_bh);
2032 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2033 "journal-1006: found valid "
2034 "transaction start offset %llu, len %d id %d",
2035 d_bh->b_blocknr -
2036 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2037 get_desc_trans_len(desc),
2038 get_desc_trans_id(desc));
2039 return 1;
2040 } else {
2041 return 0;
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002045static void brelse_array(struct buffer_head **heads, int num)
2046{
2047 int i;
2048 for (i = 0; i < num; i++) {
2049 brelse(heads[i]);
2050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
2053/*
2054** given the start, and values for the oldest acceptable transactions,
2055** this either reads in a replays a transaction, or returns because the transaction
2056** is invalid, or too old.
2057*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002058static int journal_read_transaction(struct super_block *p_s_sb,
2059 unsigned long cur_dblock,
2060 unsigned long oldest_start,
2061 unsigned long oldest_trans_id,
2062 unsigned long newest_mount_id)
2063{
2064 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2065 struct reiserfs_journal_desc *desc;
2066 struct reiserfs_journal_commit *commit;
2067 unsigned long trans_id = 0;
2068 struct buffer_head *c_bh;
2069 struct buffer_head *d_bh;
2070 struct buffer_head **log_blocks = NULL;
2071 struct buffer_head **real_blocks = NULL;
2072 unsigned long trans_offset;
2073 int i;
2074 int trans_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002076 d_bh = journal_bread(p_s_sb, cur_dblock);
2077 if (!d_bh)
2078 return 1;
2079 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2080 trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2081 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1037: "
2082 "journal_read_transaction, offset %llu, len %d mount_id %d",
2083 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2084 get_desc_trans_len(desc), get_desc_mount_id(desc));
2085 if (get_desc_trans_id(desc) < oldest_trans_id) {
2086 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1039: "
2087 "journal_read_trans skipping because %lu is too old",
2088 cur_dblock -
2089 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb));
2090 brelse(d_bh);
2091 return 1;
2092 }
2093 if (get_desc_mount_id(desc) != newest_mount_id) {
2094 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1146: "
2095 "journal_read_trans skipping because %d is != "
2096 "newest_mount_id %lu", get_desc_mount_id(desc),
2097 newest_mount_id);
2098 brelse(d_bh);
2099 return 1;
2100 }
2101 c_bh = journal_bread(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2102 ((trans_offset + get_desc_trans_len(desc) + 1) %
2103 SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
2104 if (!c_bh) {
2105 brelse(d_bh);
2106 return 1;
2107 }
2108 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
2109 if (journal_compare_desc_commit(p_s_sb, desc, commit)) {
2110 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2111 "journal_read_transaction, "
2112 "commit offset %llu had bad time %d or length %d",
2113 c_bh->b_blocknr -
2114 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2115 get_commit_trans_id(commit),
2116 get_commit_trans_len(commit));
2117 brelse(c_bh);
2118 brelse(d_bh);
2119 return 1;
2120 }
2121 trans_id = get_desc_trans_id(desc);
2122 /* now we know we've got a good transaction, and it was inside the valid time ranges */
Pekka Enbergd739b422006-02-01 03:06:43 -08002123 log_blocks = kmalloc(get_desc_trans_len(desc) *
2124 sizeof(struct buffer_head *), GFP_NOFS);
2125 real_blocks = kmalloc(get_desc_trans_len(desc) *
2126 sizeof(struct buffer_head *), GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002127 if (!log_blocks || !real_blocks) {
2128 brelse(c_bh);
2129 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002130 kfree(log_blocks);
2131 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002132 reiserfs_warning(p_s_sb,
2133 "journal-1169: kmalloc failed, unable to mount FS");
2134 return -1;
2135 }
2136 /* get all the buffer heads */
2137 trans_half = journal_trans_half(p_s_sb->s_blocksize);
2138 for (i = 0; i < get_desc_trans_len(desc); i++) {
2139 log_blocks[i] =
2140 journal_getblk(p_s_sb,
2141 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2142 (trans_offset + 1 +
2143 i) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2144 if (i < trans_half) {
2145 real_blocks[i] =
2146 sb_getblk(p_s_sb,
2147 le32_to_cpu(desc->j_realblock[i]));
2148 } else {
2149 real_blocks[i] =
2150 sb_getblk(p_s_sb,
2151 le32_to_cpu(commit->
2152 j_realblock[i - trans_half]));
2153 }
2154 if (real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(p_s_sb)) {
2155 reiserfs_warning(p_s_sb,
2156 "journal-1207: REPLAY FAILURE fsck required! Block to replay is outside of filesystem");
2157 goto abort_replay;
2158 }
2159 /* make sure we don't try to replay onto log or reserved area */
2160 if (is_block_in_log_or_reserved_area
2161 (p_s_sb, real_blocks[i]->b_blocknr)) {
2162 reiserfs_warning(p_s_sb,
2163 "journal-1204: REPLAY FAILURE fsck required! Trying to replay onto a log block");
2164 abort_replay:
2165 brelse_array(log_blocks, i);
2166 brelse_array(real_blocks, i);
2167 brelse(c_bh);
2168 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002169 kfree(log_blocks);
2170 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002171 return -1;
2172 }
2173 }
2174 /* read in the log blocks, memcpy to the corresponding real block */
2175 ll_rw_block(READ, get_desc_trans_len(desc), log_blocks);
2176 for (i = 0; i < get_desc_trans_len(desc); i++) {
2177 wait_on_buffer(log_blocks[i]);
2178 if (!buffer_uptodate(log_blocks[i])) {
2179 reiserfs_warning(p_s_sb,
2180 "journal-1212: REPLAY FAILURE fsck required! buffer write failed");
2181 brelse_array(log_blocks + i,
2182 get_desc_trans_len(desc) - i);
2183 brelse_array(real_blocks, get_desc_trans_len(desc));
2184 brelse(c_bh);
2185 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002186 kfree(log_blocks);
2187 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002188 return -1;
2189 }
2190 memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data,
2191 real_blocks[i]->b_size);
2192 set_buffer_uptodate(real_blocks[i]);
2193 brelse(log_blocks[i]);
2194 }
2195 /* flush out the real blocks */
2196 for (i = 0; i < get_desc_trans_len(desc); i++) {
2197 set_buffer_dirty(real_blocks[i]);
Jan Kara53778ff2005-09-06 15:19:14 -07002198 ll_rw_block(SWRITE, 1, real_blocks + i);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002199 }
2200 for (i = 0; i < get_desc_trans_len(desc); i++) {
2201 wait_on_buffer(real_blocks[i]);
2202 if (!buffer_uptodate(real_blocks[i])) {
2203 reiserfs_warning(p_s_sb,
2204 "journal-1226: REPLAY FAILURE, fsck required! buffer write failed");
2205 brelse_array(real_blocks + i,
2206 get_desc_trans_len(desc) - i);
2207 brelse(c_bh);
2208 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002209 kfree(log_blocks);
2210 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002211 return -1;
2212 }
2213 brelse(real_blocks[i]);
2214 }
2215 cur_dblock =
2216 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2217 ((trans_offset + get_desc_trans_len(desc) +
2218 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2219 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2220 "journal-1095: setting journal " "start to offset %ld",
2221 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb));
2222
2223 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
2224 journal->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2225 journal->j_last_flush_trans_id = trans_id;
2226 journal->j_trans_id = trans_id + 1;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08002227 /* check for trans_id overflow */
2228 if (journal->j_trans_id == 0)
2229 journal->j_trans_id = 10;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002230 brelse(c_bh);
2231 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002232 kfree(log_blocks);
2233 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
2237/* This function reads blocks starting from block and to max_block of bufsize
2238 size (but no more than BUFNR blocks at a time). This proved to improve
2239 mounting speed on self-rebuilding raid5 arrays at least.
2240 Right now it is only used from journal code. But later we might use it
2241 from other places.
2242 Note: Do not use journal_getblk/sb_getblk functions here! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002243static struct buffer_head *reiserfs_breada(struct block_device *dev, int block,
2244 int bufsize, unsigned int max_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002246 struct buffer_head *bhlist[BUFNR];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 unsigned int blocks = BUFNR;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002248 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 int i, j;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002250
2251 bh = __getblk(dev, block, bufsize);
2252 if (buffer_uptodate(bh))
2253 return (bh);
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (block + BUFNR > max_block) {
2256 blocks = max_block - block;
2257 }
2258 bhlist[0] = bh;
2259 j = 1;
2260 for (i = 1; i < blocks; i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002261 bh = __getblk(dev, block + i, bufsize);
2262 if (buffer_uptodate(bh)) {
2263 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002265 } else
2266 bhlist[j++] = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002268 ll_rw_block(READ, j, bhlist);
2269 for (i = 1; i < j; i++)
2270 brelse(bhlist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 bh = bhlist[0];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002272 wait_on_buffer(bh);
2273 if (buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return bh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002275 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return NULL;
2277}
2278
2279/*
2280** read and replay the log
2281** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2282** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
2283**
2284** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2285**
2286** On exit, it sets things up so the first transaction will work correctly.
2287*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002288static int journal_read(struct super_block *p_s_sb)
2289{
2290 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2291 struct reiserfs_journal_desc *desc;
2292 unsigned long oldest_trans_id = 0;
2293 unsigned long oldest_invalid_trans_id = 0;
2294 time_t start;
2295 unsigned long oldest_start = 0;
2296 unsigned long cur_dblock = 0;
2297 unsigned long newest_mount_id = 9;
2298 struct buffer_head *d_bh;
2299 struct reiserfs_journal_header *jh;
2300 int valid_journal_header = 0;
2301 int replay_count = 0;
2302 int continue_replay = 1;
2303 int ret;
2304 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002306 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb);
2307 reiserfs_info(p_s_sb, "checking transaction log (%s)\n",
2308 bdevname(journal->j_dev_bd, b));
2309 start = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002311 /* step 1, read in the journal header block. Check the transaction it says
2312 ** is the first unflushed, and if that transaction is not valid,
2313 ** replay is done
2314 */
2315 journal->j_header_bh = journal_bread(p_s_sb,
2316 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb)
2317 + SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2318 if (!journal->j_header_bh) {
2319 return 1;
2320 }
2321 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data);
Vladimir V. Savelievc499ec22006-03-02 02:54:39 -08002322 if (le32_to_cpu(jh->j_first_unflushed_offset) <
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002323 SB_ONDISK_JOURNAL_SIZE(p_s_sb)
2324 && le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
2325 oldest_start =
2326 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2327 le32_to_cpu(jh->j_first_unflushed_offset);
2328 oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2329 newest_mount_id = le32_to_cpu(jh->j_mount_id);
2330 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2331 "journal-1153: found in "
2332 "header: first_unflushed_offset %d, last_flushed_trans_id "
2333 "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
2334 le32_to_cpu(jh->j_last_flush_trans_id));
2335 valid_journal_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002337 /* now, we try to read the first unflushed offset. If it is not valid,
2338 ** there is nothing more we can do, and it makes no sense to read
2339 ** through the whole log.
2340 */
2341 d_bh =
2342 journal_bread(p_s_sb,
2343 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2344 le32_to_cpu(jh->j_first_unflushed_offset));
2345 ret = journal_transaction_is_valid(p_s_sb, d_bh, NULL, NULL);
2346 if (!ret) {
2347 continue_replay = 0;
2348 }
2349 brelse(d_bh);
2350 goto start_log_replay;
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002353 if (continue_replay && bdev_read_only(p_s_sb->s_bdev)) {
2354 reiserfs_warning(p_s_sb,
2355 "clm-2076: device is readonly, unable to replay log");
2356 return -1;
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002359 /* ok, there are transactions that need to be replayed. start with the first log block, find
2360 ** all the valid transactions, and pick out the oldest.
2361 */
2362 while (continue_replay
2363 && cur_dblock <
2364 (SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2365 SB_ONDISK_JOURNAL_SIZE(p_s_sb))) {
2366 /* Note that it is required for blocksize of primary fs device and journal
2367 device to be the same */
2368 d_bh =
2369 reiserfs_breada(journal->j_dev_bd, cur_dblock,
2370 p_s_sb->s_blocksize,
2371 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2372 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2373 ret =
2374 journal_transaction_is_valid(p_s_sb, d_bh,
2375 &oldest_invalid_trans_id,
2376 &newest_mount_id);
2377 if (ret == 1) {
2378 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2379 if (oldest_start == 0) { /* init all oldest_ values */
2380 oldest_trans_id = get_desc_trans_id(desc);
2381 oldest_start = d_bh->b_blocknr;
2382 newest_mount_id = get_desc_mount_id(desc);
2383 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2384 "journal-1179: Setting "
2385 "oldest_start to offset %llu, trans_id %lu",
2386 oldest_start -
2387 SB_ONDISK_JOURNAL_1st_BLOCK
2388 (p_s_sb), oldest_trans_id);
2389 } else if (oldest_trans_id > get_desc_trans_id(desc)) {
2390 /* one we just read was older */
2391 oldest_trans_id = get_desc_trans_id(desc);
2392 oldest_start = d_bh->b_blocknr;
2393 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2394 "journal-1180: Resetting "
2395 "oldest_start to offset %lu, trans_id %lu",
2396 oldest_start -
2397 SB_ONDISK_JOURNAL_1st_BLOCK
2398 (p_s_sb), oldest_trans_id);
2399 }
2400 if (newest_mount_id < get_desc_mount_id(desc)) {
2401 newest_mount_id = get_desc_mount_id(desc);
2402 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2403 "journal-1299: Setting "
2404 "newest_mount_id to %d",
2405 get_desc_mount_id(desc));
2406 }
2407 cur_dblock += get_desc_trans_len(desc) + 2;
2408 } else {
2409 cur_dblock++;
2410 }
2411 brelse(d_bh);
2412 }
2413
2414 start_log_replay:
2415 cur_dblock = oldest_start;
2416 if (oldest_trans_id) {
2417 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2418 "journal-1206: Starting replay "
2419 "from offset %llu, trans_id %lu",
2420 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2421 oldest_trans_id);
2422
2423 }
2424 replay_count = 0;
2425 while (continue_replay && oldest_trans_id > 0) {
2426 ret =
2427 journal_read_transaction(p_s_sb, cur_dblock, oldest_start,
2428 oldest_trans_id, newest_mount_id);
2429 if (ret < 0) {
2430 return ret;
2431 } else if (ret != 0) {
2432 break;
2433 }
2434 cur_dblock =
2435 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) + journal->j_start;
2436 replay_count++;
2437 if (cur_dblock == oldest_start)
2438 break;
2439 }
2440
2441 if (oldest_trans_id == 0) {
2442 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
2443 "journal-1225: No valid " "transactions found");
2444 }
2445 /* j_start does not get set correctly if we don't replay any transactions.
2446 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2447 ** copy the trans_id from the header
2448 */
2449 if (valid_journal_header && replay_count == 0) {
2450 journal->j_start = le32_to_cpu(jh->j_first_unflushed_offset);
2451 journal->j_trans_id =
2452 le32_to_cpu(jh->j_last_flush_trans_id) + 1;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08002453 /* check for trans_id overflow */
2454 if (journal->j_trans_id == 0)
2455 journal->j_trans_id = 10;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002456 journal->j_last_flush_trans_id =
2457 le32_to_cpu(jh->j_last_flush_trans_id);
2458 journal->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
2459 } else {
2460 journal->j_mount_id = newest_mount_id + 1;
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002463 "newest_mount_id to %lu", journal->j_mount_id);
2464 journal->j_first_unflushed_offset = journal->j_start;
2465 if (replay_count > 0) {
2466 reiserfs_info(p_s_sb,
2467 "replayed %d transactions in %lu seconds\n",
2468 replay_count, get_seconds() - start);
2469 }
2470 if (!bdev_read_only(p_s_sb->s_bdev) &&
2471 _update_journal_header_block(p_s_sb, journal->j_start,
2472 journal->j_last_flush_trans_id)) {
2473 /* replay failed, caller must call free_journal_ram and abort
2474 ** the mount
2475 */
2476 return -1;
2477 }
2478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
2481static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
2482{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002483 struct reiserfs_journal_list *jl;
Pekka Enberg8c777cc2006-02-01 03:06:43 -08002484 jl = kzalloc(sizeof(struct reiserfs_journal_list),
2485 GFP_NOFS | __GFP_NOFAIL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002486 INIT_LIST_HEAD(&jl->j_list);
2487 INIT_LIST_HEAD(&jl->j_working_list);
2488 INIT_LIST_HEAD(&jl->j_tail_bh_list);
2489 INIT_LIST_HEAD(&jl->j_bh_list);
2490 sema_init(&jl->j_commit_lock, 1);
2491 SB_JOURNAL(s)->j_num_lists++;
2492 get_journal_list(jl);
2493 return jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002496static void journal_list_init(struct super_block *p_s_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002498 SB_JOURNAL(p_s_sb)->j_current_jl = alloc_journal_list(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002501static int release_journal_dev(struct super_block *super,
2502 struct reiserfs_journal *journal)
2503{
2504 int result;
2505
2506 result = 0;
2507
2508 if (journal->j_dev_file != NULL) {
2509 result = filp_close(journal->j_dev_file, NULL);
2510 journal->j_dev_file = NULL;
2511 journal->j_dev_bd = NULL;
2512 } else if (journal->j_dev_bd != NULL) {
2513 result = blkdev_put(journal->j_dev_bd);
2514 journal->j_dev_bd = NULL;
2515 }
2516
2517 if (result != 0) {
2518 reiserfs_warning(super,
2519 "sh-457: release_journal_dev: Cannot release journal device: %i",
2520 result);
2521 }
2522 return result;
2523}
2524
2525static int journal_init_dev(struct super_block *super,
2526 struct reiserfs_journal *journal,
2527 const char *jdev_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
2529 int result;
2530 dev_t jdev;
2531 int blkdev_mode = FMODE_READ | FMODE_WRITE;
2532 char b[BDEVNAME_SIZE];
2533
2534 result = 0;
2535
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002536 journal->j_dev_bd = NULL;
2537 journal->j_dev_file = NULL;
2538 jdev = SB_ONDISK_JOURNAL_DEVICE(super) ?
2539 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 if (bdev_read_only(super->s_bdev))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002542 blkdev_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 /* there is no "jdev" option and journal is on separate device */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002545 if ((!jdev_name || !jdev_name[0])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode);
2547 if (IS_ERR(journal->j_dev_bd)) {
2548 result = PTR_ERR(journal->j_dev_bd);
2549 journal->j_dev_bd = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002550 reiserfs_warning(super, "sh-458: journal_init_dev: "
2551 "cannot init journal device '%s': %i",
2552 __bdevname(jdev, b), result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return result;
2554 } else if (jdev != super->s_dev)
2555 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2556 return 0;
2557 }
2558
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002559 journal->j_dev_file = filp_open(jdev_name, 0, 0);
2560 if (!IS_ERR(journal->j_dev_file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 struct inode *jdev_inode = journal->j_dev_file->f_mapping->host;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002562 if (!S_ISBLK(jdev_inode->i_mode)) {
Edward Shishkin74f9f972005-05-01 08:59:09 -07002563 reiserfs_warning(super, "journal_init_dev: '%s' is "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002564 "not a block device", jdev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 result = -ENOTBLK;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002566 release_journal_dev(super, journal);
2567 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 /* ok */
2569 journal->j_dev_bd = I_BDEV(jdev_inode);
2570 set_blocksize(journal->j_dev_bd, super->s_blocksize);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002571 reiserfs_info(super,
2572 "journal_init_dev: journal device: %s\n",
Edward Shishkin74f9f972005-05-01 08:59:09 -07002573 bdevname(journal->j_dev_bd, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575 } else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002576 result = PTR_ERR(journal->j_dev_file);
2577 journal->j_dev_file = NULL;
2578 reiserfs_warning(super,
2579 "journal_init_dev: Cannot open '%s': %i",
2580 jdev_name, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return result;
2583}
2584
2585/*
2586** must be called once on fs mount. calls journal_read for you
2587*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002588int journal_init(struct super_block *p_s_sb, const char *j_dev_name,
2589 int old_format, unsigned int commit_max_age)
2590{
2591 int num_cnodes = SB_ONDISK_JOURNAL_SIZE(p_s_sb) * 2;
2592 struct buffer_head *bhjh;
2593 struct reiserfs_super_block *rs;
2594 struct reiserfs_journal_header *jh;
2595 struct reiserfs_journal *journal;
2596 struct reiserfs_journal_list *jl;
2597 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002599 journal = SB_JOURNAL(p_s_sb) = vmalloc(sizeof(struct reiserfs_journal));
2600 if (!journal) {
2601 reiserfs_warning(p_s_sb,
2602 "journal-1256: unable to get memory for journal structure");
2603 return 1;
2604 }
2605 memset(journal, 0, sizeof(struct reiserfs_journal));
2606 INIT_LIST_HEAD(&journal->j_bitmap_nodes);
2607 INIT_LIST_HEAD(&journal->j_prealloc_list);
2608 INIT_LIST_HEAD(&journal->j_working_list);
2609 INIT_LIST_HEAD(&journal->j_journal_list);
2610 journal->j_persistent_trans = 0;
2611 if (reiserfs_allocate_list_bitmaps(p_s_sb,
2612 journal->j_list_bitmap,
2613 SB_BMAP_NR(p_s_sb)))
2614 goto free_and_return;
2615 allocate_bitmap_nodes(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002617 /* reserved for journal area support */
2618 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) = (old_format ?
2619 REISERFS_OLD_DISK_OFFSET_IN_BYTES
2620 / p_s_sb->s_blocksize +
2621 SB_BMAP_NR(p_s_sb) +
2622 1 :
2623 REISERFS_DISK_OFFSET_IN_BYTES /
2624 p_s_sb->s_blocksize + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002626 /* Sanity check to see is the standard journal fitting withing first bitmap
2627 (actual for small blocksizes) */
2628 if (!SB_ONDISK_JOURNAL_DEVICE(p_s_sb) &&
2629 (SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) +
2630 SB_ONDISK_JOURNAL_SIZE(p_s_sb) > p_s_sb->s_blocksize * 8)) {
2631 reiserfs_warning(p_s_sb,
2632 "journal-1393: journal does not fit for area "
2633 "addressed by first of bitmap blocks. It starts at "
2634 "%u and its size is %u. Block size %ld",
2635 SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb),
2636 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2637 p_s_sb->s_blocksize);
2638 goto free_and_return;
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002641 if (journal_init_dev(p_s_sb, journal, j_dev_name) != 0) {
2642 reiserfs_warning(p_s_sb,
2643 "sh-462: unable to initialize jornal device");
2644 goto free_and_return;
2645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002647 rs = SB_DISK_SUPER_BLOCK(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002649 /* read journal header */
2650 bhjh = journal_bread(p_s_sb,
2651 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
2652 SB_ONDISK_JOURNAL_SIZE(p_s_sb));
2653 if (!bhjh) {
2654 reiserfs_warning(p_s_sb,
2655 "sh-459: unable to read journal header");
2656 goto free_and_return;
2657 }
2658 jh = (struct reiserfs_journal_header *)(bhjh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002660 /* make sure that journal matches to the super block */
2661 if (is_reiserfs_jr(rs)
2662 && (le32_to_cpu(jh->jh_journal.jp_journal_magic) !=
2663 sb_jp_journal_magic(rs))) {
2664 reiserfs_warning(p_s_sb,
2665 "sh-460: journal header magic %x "
2666 "(device %s) does not match to magic found in super "
2667 "block %x", jh->jh_journal.jp_journal_magic,
2668 bdevname(journal->j_dev_bd, b),
2669 sb_jp_journal_magic(rs));
2670 brelse(bhjh);
2671 goto free_and_return;
2672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002674 journal->j_trans_max = le32_to_cpu(jh->jh_journal.jp_journal_trans_max);
2675 journal->j_max_batch = le32_to_cpu(jh->jh_journal.jp_journal_max_batch);
2676 journal->j_max_commit_age =
2677 le32_to_cpu(jh->jh_journal.jp_journal_max_commit_age);
2678 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002680 if (journal->j_trans_max) {
2681 /* make sure these parameters are available, assign it if they are not */
2682 __u32 initial = journal->j_trans_max;
2683 __u32 ratio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002685 if (p_s_sb->s_blocksize < 4096)
2686 ratio = 4096 / p_s_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002688 if (SB_ONDISK_JOURNAL_SIZE(p_s_sb) / journal->j_trans_max <
2689 JOURNAL_MIN_RATIO)
2690 journal->j_trans_max =
2691 SB_ONDISK_JOURNAL_SIZE(p_s_sb) / JOURNAL_MIN_RATIO;
2692 if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio)
2693 journal->j_trans_max =
2694 JOURNAL_TRANS_MAX_DEFAULT / ratio;
2695 if (journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio)
2696 journal->j_trans_max =
2697 JOURNAL_TRANS_MIN_DEFAULT / ratio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002699 if (journal->j_trans_max != initial)
2700 reiserfs_warning(p_s_sb,
2701 "sh-461: journal_init: wrong transaction max size (%u). Changed to %u",
2702 initial, journal->j_trans_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002704 journal->j_max_batch = journal->j_trans_max *
2705 JOURNAL_MAX_BATCH_DEFAULT / JOURNAL_TRANS_MAX_DEFAULT;
2706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002708 if (!journal->j_trans_max) {
2709 /*we have the file system was created by old version of mkreiserfs
2710 so this field contains zero value */
2711 journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT;
2712 journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT;
2713 journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002715 /* for blocksize >= 4096 - max transaction size is 1024. For block size < 4096
2716 trans max size is decreased proportionally */
2717 if (p_s_sb->s_blocksize < 4096) {
2718 journal->j_trans_max /= (4096 / p_s_sb->s_blocksize);
2719 journal->j_max_batch = (journal->j_trans_max) * 9 / 10;
2720 }
2721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002723 journal->j_default_max_commit_age = journal->j_max_commit_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002725 if (commit_max_age != 0) {
2726 journal->j_max_commit_age = commit_max_age;
2727 journal->j_max_trans_age = commit_max_age;
2728 }
2729
2730 reiserfs_info(p_s_sb, "journal params: device %s, size %u, "
2731 "journal first block %u, max trans len %u, max batch %u, "
2732 "max commit age %u, max trans age %u\n",
2733 bdevname(journal->j_dev_bd, b),
2734 SB_ONDISK_JOURNAL_SIZE(p_s_sb),
2735 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb),
2736 journal->j_trans_max,
2737 journal->j_max_batch,
2738 journal->j_max_commit_age, journal->j_max_trans_age);
2739
2740 brelse(bhjh);
2741
2742 journal->j_list_bitmap_index = 0;
2743 journal_list_init(p_s_sb);
2744
2745 memset(journal->j_list_hash_table, 0,
2746 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
2747
2748 INIT_LIST_HEAD(&journal->j_dirty_buffers);
2749 spin_lock_init(&journal->j_dirty_buffers_lock);
2750
2751 journal->j_start = 0;
2752 journal->j_len = 0;
2753 journal->j_len_alloc = 0;
2754 atomic_set(&(journal->j_wcount), 0);
2755 atomic_set(&(journal->j_async_throttle), 0);
2756 journal->j_bcount = 0;
2757 journal->j_trans_start_time = 0;
2758 journal->j_last = NULL;
2759 journal->j_first = NULL;
2760 init_waitqueue_head(&(journal->j_join_wait));
2761 sema_init(&journal->j_lock, 1);
2762 sema_init(&journal->j_flush_sem, 1);
2763
2764 journal->j_trans_id = 10;
2765 journal->j_mount_id = 10;
2766 journal->j_state = 0;
2767 atomic_set(&(journal->j_jlock), 0);
2768 journal->j_cnode_free_list = allocate_cnodes(num_cnodes);
2769 journal->j_cnode_free_orig = journal->j_cnode_free_list;
2770 journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0;
2771 journal->j_cnode_used = 0;
2772 journal->j_must_wait = 0;
2773
Jeff Mahoney576f6d72005-11-29 19:34:39 -08002774 if (journal->j_cnode_free == 0) {
2775 reiserfs_warning(p_s_sb, "journal-2004: Journal cnode memory "
2776 "allocation failed (%ld bytes). Journal is "
2777 "too large for available memory. Usually "
2778 "this is due to a journal that is too large.",
2779 sizeof (struct reiserfs_journal_cnode) * num_cnodes);
2780 goto free_and_return;
2781 }
2782
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002783 init_journal_hash(p_s_sb);
2784 jl = journal->j_current_jl;
2785 jl->j_list_bitmap = get_list_bitmap(p_s_sb, jl);
2786 if (!jl->j_list_bitmap) {
2787 reiserfs_warning(p_s_sb,
2788 "journal-2005, get_list_bitmap failed for journal list 0");
2789 goto free_and_return;
2790 }
2791 if (journal_read(p_s_sb) < 0) {
2792 reiserfs_warning(p_s_sb, "Replay Failure, unable to mount");
2793 goto free_and_return;
2794 }
2795
2796 reiserfs_mounted_fs_count++;
2797 if (reiserfs_mounted_fs_count <= 1)
2798 commit_wq = create_workqueue("reiserfs");
2799
2800 INIT_WORK(&journal->j_work, flush_async_commits, p_s_sb);
2801 return 0;
2802 free_and_return:
2803 free_journal_ram(p_s_sb);
2804 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
2807/*
2808** test for a polite end of the current transaction. Used by file_write, and should
2809** be used by delete to make sure they don't write more than can fit inside a single
2810** transaction
2811*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002812int journal_transaction_should_end(struct reiserfs_transaction_handle *th,
2813 int new_alloc)
2814{
2815 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2816 time_t now = get_seconds();
2817 /* cannot restart while nested */
2818 BUG_ON(!th->t_trans_id);
2819 if (th->t_refcount > 1)
2820 return 0;
2821 if (journal->j_must_wait > 0 ||
2822 (journal->j_len_alloc + new_alloc) >= journal->j_max_batch ||
2823 atomic_read(&(journal->j_jlock)) ||
2824 (now - journal->j_trans_start_time) > journal->j_max_trans_age ||
2825 journal->j_cnode_free < (journal->j_trans_max * 3)) {
2826 return 1;
2827 }
Chris Mason6ae1ea42006-02-01 03:06:50 -08002828 /* protected by the BKL here */
2829 journal->j_len_alloc += new_alloc;
2830 th->t_blocks_allocated += new_alloc ;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
2834/* this must be called inside a transaction, and requires the
2835** kernel_lock to be held
2836*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002837void reiserfs_block_writes(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002839 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2840 BUG_ON(!th->t_trans_id);
2841 journal->j_must_wait = 1;
2842 set_bit(J_WRITERS_BLOCKED, &journal->j_state);
2843 return;
2844}
2845
2846/* this must be called without a transaction started, and does not
2847** require BKL
2848*/
2849void reiserfs_allow_writes(struct super_block *s)
2850{
2851 struct reiserfs_journal *journal = SB_JOURNAL(s);
2852 clear_bit(J_WRITERS_BLOCKED, &journal->j_state);
2853 wake_up(&journal->j_join_wait);
2854}
2855
2856/* this must be called without a transaction started, and does not
2857** require BKL
2858*/
2859void reiserfs_wait_on_write_block(struct super_block *s)
2860{
2861 struct reiserfs_journal *journal = SB_JOURNAL(s);
2862 wait_event(journal->j_join_wait,
2863 !test_bit(J_WRITERS_BLOCKED, &journal->j_state));
2864}
2865
2866static void queue_log_writer(struct super_block *s)
2867{
2868 wait_queue_t wait;
2869 struct reiserfs_journal *journal = SB_JOURNAL(s);
2870 set_bit(J_WRITERS_QUEUED, &journal->j_state);
2871
2872 /*
2873 * we don't want to use wait_event here because
2874 * we only want to wait once.
2875 */
2876 init_waitqueue_entry(&wait, current);
2877 add_wait_queue(&journal->j_join_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002879 if (test_bit(J_WRITERS_QUEUED, &journal->j_state))
2880 schedule();
2881 current->state = TASK_RUNNING;
2882 remove_wait_queue(&journal->j_join_wait, &wait);
2883}
2884
2885static void wake_queued_writers(struct super_block *s)
2886{
2887 struct reiserfs_journal *journal = SB_JOURNAL(s);
2888 if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state))
2889 wake_up(&journal->j_join_wait);
2890}
2891
2892static void let_transaction_grow(struct super_block *sb, unsigned long trans_id)
2893{
2894 struct reiserfs_journal *journal = SB_JOURNAL(sb);
2895 unsigned long bcount = journal->j_bcount;
2896 while (1) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002897 schedule_timeout_uninterruptible(1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002898 journal->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2899 while ((atomic_read(&journal->j_wcount) > 0 ||
2900 atomic_read(&journal->j_jlock)) &&
2901 journal->j_trans_id == trans_id) {
2902 queue_log_writer(sb);
2903 }
2904 if (journal->j_trans_id != trans_id)
2905 break;
2906 if (bcount == journal->j_bcount)
2907 break;
2908 bcount = journal->j_bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910}
2911
2912/* join == true if you must join an existing transaction.
2913** join == false if you can deal with waiting for others to finish
2914**
2915** this will block until the transaction is joinable. send the number of blocks you
2916** expect to use in nblocks.
2917*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002918static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
2919 struct super_block *p_s_sb, unsigned long nblocks,
2920 int join)
2921{
2922 time_t now = get_seconds();
2923 int old_trans_id;
2924 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
2925 struct reiserfs_transaction_handle myth;
2926 int sched_count = 0;
2927 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002929 reiserfs_check_lock_depth(p_s_sb, "journal_begin");
2930 if (nblocks > journal->j_trans_max)
2931 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002933 PROC_INFO_INC(p_s_sb, journal.journal_being);
2934 /* set here for journal_join */
2935 th->t_refcount = 1;
2936 th->t_super = p_s_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002938 relock:
2939 lock_journal(p_s_sb);
2940 if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted(journal)) {
2941 unlock_journal(p_s_sb);
2942 retval = journal->j_errno;
2943 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002945 journal->j_bcount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002947 if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) {
2948 unlock_journal(p_s_sb);
2949 reiserfs_wait_on_write_block(p_s_sb);
2950 PROC_INFO_INC(p_s_sb, journal.journal_relock_writers);
2951 goto relock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002953 now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002955 /* if there is no room in the journal OR
2956 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
2957 ** we don't sleep if there aren't other writers
2958 */
2959
2960 if ((!join && journal->j_must_wait > 0) ||
2961 (!join
2962 && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch)
2963 || (!join && atomic_read(&journal->j_wcount) > 0
2964 && journal->j_trans_start_time > 0
2965 && (now - journal->j_trans_start_time) >
2966 journal->j_max_trans_age) || (!join
2967 && atomic_read(&journal->j_jlock))
2968 || (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) {
2969
2970 old_trans_id = journal->j_trans_id;
2971 unlock_journal(p_s_sb); /* allow others to finish this transaction */
2972
2973 if (!join && (journal->j_len_alloc + nblocks + 2) >=
2974 journal->j_max_batch &&
2975 ((journal->j_len + nblocks + 2) * 100) <
2976 (journal->j_len_alloc * 75)) {
2977 if (atomic_read(&journal->j_wcount) > 10) {
2978 sched_count++;
2979 queue_log_writer(p_s_sb);
2980 goto relock;
2981 }
2982 }
2983 /* don't mess with joining the transaction if all we have to do is
2984 * wait for someone else to do a commit
2985 */
2986 if (atomic_read(&journal->j_jlock)) {
2987 while (journal->j_trans_id == old_trans_id &&
2988 atomic_read(&journal->j_jlock)) {
2989 queue_log_writer(p_s_sb);
2990 }
2991 goto relock;
2992 }
2993 retval = journal_join(&myth, p_s_sb, 1);
2994 if (retval)
2995 goto out_fail;
2996
2997 /* someone might have ended the transaction while we joined */
2998 if (old_trans_id != journal->j_trans_id) {
2999 retval = do_journal_end(&myth, p_s_sb, 1, 0);
3000 } else {
3001 retval = do_journal_end(&myth, p_s_sb, 1, COMMIT_NOW);
3002 }
3003
3004 if (retval)
3005 goto out_fail;
3006
3007 PROC_INFO_INC(p_s_sb, journal.journal_relock_wcount);
3008 goto relock;
3009 }
3010 /* we are the first writer, set trans_id */
3011 if (journal->j_trans_start_time == 0) {
3012 journal->j_trans_start_time = get_seconds();
3013 }
3014 atomic_inc(&(journal->j_wcount));
3015 journal->j_len_alloc += nblocks;
3016 th->t_blocks_logged = 0;
3017 th->t_blocks_allocated = nblocks;
3018 th->t_trans_id = journal->j_trans_id;
3019 unlock_journal(p_s_sb);
3020 INIT_LIST_HEAD(&th->t_list);
3021 get_fs_excl();
3022 return 0;
3023
3024 out_fail:
3025 memset(th, 0, sizeof(*th));
3026 /* Re-set th->t_super, so we can properly keep track of how many
3027 * persistent transactions there are. We need to do this so if this
3028 * call is part of a failed restart_transaction, we can free it later */
3029 th->t_super = p_s_sb;
3030 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003033struct reiserfs_transaction_handle *reiserfs_persistent_transaction(struct
3034 super_block
3035 *s,
3036 int nblocks)
3037{
3038 int ret;
3039 struct reiserfs_transaction_handle *th;
3040
3041 /* if we're nesting into an existing transaction. It will be
3042 ** persistent on its own
3043 */
3044 if (reiserfs_transaction_running(s)) {
3045 th = current->journal_info;
3046 th->t_refcount++;
3047 if (th->t_refcount < 2) {
3048 BUG();
3049 }
3050 return th;
3051 }
Pekka Enbergd739b422006-02-01 03:06:43 -08003052 th = kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003053 if (!th)
3054 return NULL;
3055 ret = journal_begin(th, s, nblocks);
3056 if (ret) {
Pekka Enbergd739b422006-02-01 03:06:43 -08003057 kfree(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003058 return NULL;
3059 }
3060
3061 SB_JOURNAL(s)->j_persistent_trans++;
3062 return th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003065int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th)
3066{
3067 struct super_block *s = th->t_super;
3068 int ret = 0;
3069 if (th->t_trans_id)
3070 ret = journal_end(th, th->t_super, th->t_blocks_allocated);
3071 else
3072 ret = -EIO;
3073 if (th->t_refcount == 0) {
3074 SB_JOURNAL(s)->j_persistent_trans--;
Pekka Enbergd739b422006-02-01 03:06:43 -08003075 kfree(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003076 }
3077 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003080static int journal_join(struct reiserfs_transaction_handle *th,
3081 struct super_block *p_s_sb, unsigned long nblocks)
3082{
3083 struct reiserfs_transaction_handle *cur_th = current->journal_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003085 /* this keeps do_journal_end from NULLing out the current->journal_info
3086 ** pointer
3087 */
3088 th->t_handle_save = cur_th;
3089 if (cur_th && cur_th->t_refcount > 1) {
3090 BUG();
3091 }
3092 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_JOIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003095int journal_join_abort(struct reiserfs_transaction_handle *th,
3096 struct super_block *p_s_sb, unsigned long nblocks)
3097{
3098 struct reiserfs_transaction_handle *cur_th = current->journal_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003100 /* this keeps do_journal_end from NULLing out the current->journal_info
3101 ** pointer
3102 */
3103 th->t_handle_save = cur_th;
3104 if (cur_th && cur_th->t_refcount > 1) {
3105 BUG();
3106 }
3107 return do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_ABORT);
3108}
3109
3110int journal_begin(struct reiserfs_transaction_handle *th,
3111 struct super_block *p_s_sb, unsigned long nblocks)
3112{
3113 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3114 int ret;
3115
3116 th->t_handle_save = NULL;
3117 if (cur_th) {
3118 /* we are nesting into the current transaction */
3119 if (cur_th->t_super == p_s_sb) {
3120 BUG_ON(!cur_th->t_refcount);
3121 cur_th->t_refcount++;
3122 memcpy(th, cur_th, sizeof(*th));
3123 if (th->t_refcount <= 1)
3124 reiserfs_warning(p_s_sb,
3125 "BAD: refcount <= 1, but journal_info != 0");
3126 return 0;
3127 } else {
3128 /* we've ended up with a handle from a different filesystem.
3129 ** save it and restore on journal_end. This should never
3130 ** really happen...
3131 */
3132 reiserfs_warning(p_s_sb,
3133 "clm-2100: nesting info a different FS");
3134 th->t_handle_save = current->journal_info;
3135 current->journal_info = th;
3136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 } else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003138 current->journal_info = th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003140 ret = do_journal_begin_r(th, p_s_sb, nblocks, JBEGIN_REG);
3141 if (current->journal_info != th)
3142 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003144 /* I guess this boils down to being the reciprocal of clm-2100 above.
3145 * If do_journal_begin_r fails, we need to put it back, since journal_end
3146 * won't be called to do it. */
3147 if (ret)
3148 current->journal_info = th->t_handle_save;
3149 else
3150 BUG_ON(!th->t_refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003152 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153}
3154
3155/*
3156** puts bh into the current transaction. If it was already there, reorders removes the
3157** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
3158**
3159** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
3160** transaction is committed.
3161**
3162** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
3163*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003164int journal_mark_dirty(struct reiserfs_transaction_handle *th,
3165 struct super_block *p_s_sb, struct buffer_head *bh)
3166{
3167 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3168 struct reiserfs_journal_cnode *cn = NULL;
3169 int count_already_incd = 0;
3170 int prepared = 0;
3171 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003173 PROC_INFO_INC(p_s_sb, journal.mark_dirty);
3174 if (th->t_trans_id != journal->j_trans_id) {
3175 reiserfs_panic(th->t_super,
3176 "journal-1577: handle trans id %ld != current trans id %ld\n",
3177 th->t_trans_id, journal->j_trans_id);
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003180 p_s_sb->s_dirt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003182 prepared = test_clear_buffer_journal_prepared(bh);
3183 clear_buffer_journal_restore_dirty(bh);
3184 /* already in this transaction, we are done */
3185 if (buffer_journaled(bh)) {
3186 PROC_INFO_INC(p_s_sb, journal.mark_dirty_already);
3187 return 0;
3188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003190 /* this must be turned into a panic instead of a warning. We can't allow
3191 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
3192 ** could get to disk too early. NOT GOOD.
3193 */
3194 if (!prepared || buffer_dirty(bh)) {
3195 reiserfs_warning(p_s_sb, "journal-1777: buffer %llu bad state "
3196 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
3197 (unsigned long long)bh->b_blocknr,
3198 prepared ? ' ' : '!',
3199 buffer_locked(bh) ? ' ' : '!',
3200 buffer_dirty(bh) ? ' ' : '!',
3201 buffer_journal_dirty(bh) ? ' ' : '!');
3202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003204 if (atomic_read(&(journal->j_wcount)) <= 0) {
3205 reiserfs_warning(p_s_sb,
3206 "journal-1409: journal_mark_dirty returning because j_wcount was %d",
3207 atomic_read(&(journal->j_wcount)));
3208 return 1;
3209 }
3210 /* this error means I've screwed up, and we've overflowed the transaction.
3211 ** Nothing can be done here, except make the FS readonly or panic.
3212 */
3213 if (journal->j_len >= journal->j_trans_max) {
3214 reiserfs_panic(th->t_super,
3215 "journal-1413: journal_mark_dirty: j_len (%lu) is too big\n",
3216 journal->j_len);
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003219 if (buffer_journal_dirty(bh)) {
3220 count_already_incd = 1;
3221 PROC_INFO_INC(p_s_sb, journal.mark_dirty_notjournal);
3222 clear_buffer_journal_dirty(bh);
3223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003225 if (journal->j_len > journal->j_len_alloc) {
3226 journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT;
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003229 set_buffer_journaled(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003231 /* now put this guy on the end */
3232 if (!cn) {
3233 cn = get_cnode(p_s_sb);
3234 if (!cn) {
3235 reiserfs_panic(p_s_sb, "get_cnode failed!\n");
3236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003238 if (th->t_blocks_logged == th->t_blocks_allocated) {
3239 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT;
3240 journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT;
3241 }
3242 th->t_blocks_logged++;
3243 journal->j_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003245 cn->bh = bh;
3246 cn->blocknr = bh->b_blocknr;
3247 cn->sb = p_s_sb;
3248 cn->jlist = NULL;
3249 insert_journal_hash(journal->j_hash_table, cn);
3250 if (!count_already_incd) {
3251 get_bh(bh);
3252 }
3253 }
3254 cn->next = NULL;
3255 cn->prev = journal->j_last;
3256 cn->bh = bh;
3257 if (journal->j_last) {
3258 journal->j_last->next = cn;
3259 journal->j_last = cn;
3260 } else {
3261 journal->j_first = cn;
3262 journal->j_last = cn;
3263 }
3264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003267int journal_end(struct reiserfs_transaction_handle *th,
3268 struct super_block *p_s_sb, unsigned long nblocks)
3269{
3270 if (!current->journal_info && th->t_refcount > 1)
3271 reiserfs_warning(p_s_sb, "REISER-NESTING: th NULL, refcount %d",
3272 th->t_refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003274 if (!th->t_trans_id) {
3275 WARN_ON(1);
3276 return -EIO;
3277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003279 th->t_refcount--;
3280 if (th->t_refcount > 0) {
3281 struct reiserfs_transaction_handle *cur_th =
3282 current->journal_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003284 /* we aren't allowed to close a nested transaction on a different
3285 ** filesystem from the one in the task struct
3286 */
3287 if (cur_th->t_super != th->t_super)
3288 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003290 if (th != cur_th) {
3291 memcpy(current->journal_info, th, sizeof(*th));
3292 th->t_trans_id = 0;
3293 }
3294 return 0;
3295 } else {
3296 return do_journal_end(th, p_s_sb, nblocks, 0);
3297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300/* removes from the current transaction, relsing and descrementing any counters.
3301** also files the removed buffer directly onto the clean list
3302**
3303** called by journal_mark_freed when a block has been deleted
3304**
3305** returns 1 if it cleaned and relsed the buffer. 0 otherwise
3306*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003307static int remove_from_transaction(struct super_block *p_s_sb,
3308 b_blocknr_t blocknr, int already_cleaned)
3309{
3310 struct buffer_head *bh;
3311 struct reiserfs_journal_cnode *cn;
3312 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3313 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003315 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr);
3316 if (!cn || !cn->bh) {
3317 return ret;
3318 }
3319 bh = cn->bh;
3320 if (cn->prev) {
3321 cn->prev->next = cn->next;
3322 }
3323 if (cn->next) {
3324 cn->next->prev = cn->prev;
3325 }
3326 if (cn == journal->j_first) {
3327 journal->j_first = cn->next;
3328 }
3329 if (cn == journal->j_last) {
3330 journal->j_last = cn->prev;
3331 }
3332 if (bh)
3333 remove_journal_hash(p_s_sb, journal->j_hash_table, NULL,
3334 bh->b_blocknr, 0);
3335 clear_buffer_journaled(bh); /* don't log this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003337 if (!already_cleaned) {
3338 clear_buffer_journal_dirty(bh);
3339 clear_buffer_dirty(bh);
3340 clear_buffer_journal_test(bh);
3341 put_bh(bh);
3342 if (atomic_read(&(bh->b_count)) < 0) {
3343 reiserfs_warning(p_s_sb,
3344 "journal-1752: remove from trans, b_count < 0");
3345 }
3346 ret = 1;
3347 }
3348 journal->j_len--;
3349 journal->j_len_alloc--;
3350 free_cnode(p_s_sb, cn);
3351 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354/*
3355** for any cnode in a journal list, it can only be dirtied of all the
3356** transactions that include it are commited to disk.
3357** this checks through each transaction, and returns 1 if you are allowed to dirty,
3358** and 0 if you aren't
3359**
3360** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3361** blocks for a given transaction on disk
3362**
3363*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003364static int can_dirty(struct reiserfs_journal_cnode *cn)
3365{
3366 struct super_block *sb = cn->sb;
3367 b_blocknr_t blocknr = cn->blocknr;
3368 struct reiserfs_journal_cnode *cur = cn->hprev;
3369 int can_dirty = 1;
3370
3371 /* first test hprev. These are all newer than cn, so any node here
3372 ** with the same block number and dev means this node can't be sent
3373 ** to disk right now.
3374 */
3375 while (cur && can_dirty) {
3376 if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
3377 cur->blocknr == blocknr) {
3378 can_dirty = 0;
3379 }
3380 cur = cur->hprev;
3381 }
3382 /* then test hnext. These are all older than cn. As long as they
3383 ** are committed to the log, it is safe to write cn to disk
3384 */
3385 cur = cn->hnext;
3386 while (cur && can_dirty) {
3387 if (cur->jlist && cur->jlist->j_len > 0 &&
3388 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
3389 cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
3390 can_dirty = 0;
3391 }
3392 cur = cur->hnext;
3393 }
3394 return can_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397/* syncs the commit blocks, but does not force the real buffers to disk
3398** will wait until the current transaction is done/commited before returning
3399*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003400int journal_end_sync(struct reiserfs_transaction_handle *th,
3401 struct super_block *p_s_sb, unsigned long nblocks)
3402{
3403 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003405 BUG_ON(!th->t_trans_id);
3406 /* you can sync while nested, very, very bad */
3407 if (th->t_refcount > 1) {
3408 BUG();
3409 }
3410 if (journal->j_len == 0) {
3411 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb),
3412 1);
3413 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb));
3414 }
3415 return do_journal_end(th, p_s_sb, nblocks, COMMIT_NOW | WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
3418/*
3419** writeback the pending async commits to disk
3420*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003421static void flush_async_commits(void *p)
3422{
3423 struct super_block *p_s_sb = p;
3424 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3425 struct reiserfs_journal_list *jl;
3426 struct list_head *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003428 lock_kernel();
3429 if (!list_empty(&journal->j_journal_list)) {
3430 /* last entry is the youngest, commit it and you get everything */
3431 entry = journal->j_journal_list.prev;
3432 jl = JOURNAL_LIST_ENTRY(entry);
3433 flush_commit_list(p_s_sb, jl, 1);
3434 }
3435 unlock_kernel();
3436 /*
3437 * this is a little racey, but there's no harm in missing
3438 * the filemap_fdata_write
3439 */
3440 if (!atomic_read(&journal->j_async_throttle)
3441 && !reiserfs_is_journal_aborted(journal)) {
3442 atomic_inc(&journal->j_async_throttle);
3443 filemap_fdatawrite(p_s_sb->s_bdev->bd_inode->i_mapping);
3444 atomic_dec(&journal->j_async_throttle);
3445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447
3448/*
3449** flushes any old transactions to disk
3450** ends the current transaction if it is too old
3451*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003452int reiserfs_flush_old_commits(struct super_block *p_s_sb)
3453{
3454 time_t now;
3455 struct reiserfs_transaction_handle th;
3456 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003458 now = get_seconds();
3459 /* safety check so we don't flush while we are replaying the log during
3460 * mount
3461 */
3462 if (list_empty(&journal->j_journal_list)) {
3463 return 0;
3464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003466 /* check the current transaction. If there are no writers, and it is
3467 * too old, finish it, and force the commit blocks to disk
3468 */
3469 if (atomic_read(&journal->j_wcount) <= 0 &&
3470 journal->j_trans_start_time > 0 &&
3471 journal->j_len > 0 &&
3472 (now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3473 if (!journal_join(&th, p_s_sb, 1)) {
3474 reiserfs_prepare_for_journal(p_s_sb,
3475 SB_BUFFER_WITH_SB(p_s_sb),
3476 1);
3477 journal_mark_dirty(&th, p_s_sb,
3478 SB_BUFFER_WITH_SB(p_s_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003480 /* we're only being called from kreiserfsd, it makes no sense to do
3481 ** an async commit so that kreiserfsd can do it later
3482 */
3483 do_journal_end(&th, p_s_sb, 1, COMMIT_NOW | WAIT);
3484 }
3485 }
3486 return p_s_sb->s_dirt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
3489/*
3490** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
3491**
3492** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
3493** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3494** flushes the commit list and returns 0.
3495**
3496** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
3497**
3498** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3499*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003500static int check_journal_end(struct reiserfs_transaction_handle *th,
3501 struct super_block *p_s_sb, unsigned long nblocks,
3502 int flags)
3503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003505 time_t now;
3506 int flush = flags & FLUSH_ALL;
3507 int commit_now = flags & COMMIT_NOW;
3508 int wait_on_commit = flags & WAIT;
3509 struct reiserfs_journal_list *jl;
3510 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003512 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003514 if (th->t_trans_id != journal->j_trans_id) {
3515 reiserfs_panic(th->t_super,
3516 "journal-1577: handle trans id %ld != current trans id %ld\n",
3517 th->t_trans_id, journal->j_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003520 journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged);
3521 if (atomic_read(&(journal->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3522 atomic_dec(&(journal->j_wcount));
3523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003525 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
3526 ** will be dealt with by next transaction that actually writes something, but should be taken
3527 ** care of in this trans
3528 */
3529 if (journal->j_len == 0) {
3530 BUG();
3531 }
3532 /* if wcount > 0, and we are called to with flush or commit_now,
3533 ** we wait on j_join_wait. We will wake up when the last writer has
3534 ** finished the transaction, and started it on its way to the disk.
3535 ** Then, we flush the commit or journal list, and just return 0
3536 ** because the rest of journal end was already done for this transaction.
3537 */
3538 if (atomic_read(&(journal->j_wcount)) > 0) {
3539 if (flush || commit_now) {
3540 unsigned trans_id;
3541
3542 jl = journal->j_current_jl;
3543 trans_id = jl->j_trans_id;
3544 if (wait_on_commit)
3545 jl->j_state |= LIST_COMMIT_PENDING;
3546 atomic_set(&(journal->j_jlock), 1);
3547 if (flush) {
3548 journal->j_next_full_flush = 1;
3549 }
3550 unlock_journal(p_s_sb);
3551
3552 /* sleep while the current transaction is still j_jlocked */
3553 while (journal->j_trans_id == trans_id) {
3554 if (atomic_read(&journal->j_jlock)) {
3555 queue_log_writer(p_s_sb);
3556 } else {
3557 lock_journal(p_s_sb);
3558 if (journal->j_trans_id == trans_id) {
3559 atomic_set(&(journal->j_jlock),
3560 1);
3561 }
3562 unlock_journal(p_s_sb);
3563 }
3564 }
3565 if (journal->j_trans_id == trans_id) {
3566 BUG();
3567 }
3568 if (commit_now
3569 && journal_list_still_alive(p_s_sb, trans_id)
3570 && wait_on_commit) {
3571 flush_commit_list(p_s_sb, jl, 1);
3572 }
3573 return 0;
3574 }
3575 unlock_journal(p_s_sb);
3576 return 0;
3577 }
3578
3579 /* deal with old transactions where we are the last writers */
3580 now = get_seconds();
3581 if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3582 commit_now = 1;
3583 journal->j_next_async_flush = 1;
3584 }
3585 /* don't batch when someone is waiting on j_join_wait */
3586 /* don't batch when syncing the commit or flushing the whole trans */
3587 if (!(journal->j_must_wait > 0) && !(atomic_read(&(journal->j_jlock)))
3588 && !flush && !commit_now && (journal->j_len < journal->j_max_batch)
3589 && journal->j_len_alloc < journal->j_max_batch
3590 && journal->j_cnode_free > (journal->j_trans_max * 3)) {
3591 journal->j_bcount++;
3592 unlock_journal(p_s_sb);
3593 return 0;
3594 }
3595
3596 if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
3597 reiserfs_panic(p_s_sb,
3598 "journal-003: journal_end: j_start (%ld) is too high\n",
3599 journal->j_start);
3600 }
3601 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
3604/*
3605** Does all the work that makes deleting blocks safe.
3606** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
3607**
3608** otherwise:
3609** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3610** before this transaction has finished.
3611**
3612** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3613** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3614** the block can't be reallocated yet.
3615**
3616** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3617*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003618int journal_mark_freed(struct reiserfs_transaction_handle *th,
3619 struct super_block *p_s_sb, b_blocknr_t blocknr)
3620{
3621 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3622 struct reiserfs_journal_cnode *cn = NULL;
3623 struct buffer_head *bh = NULL;
3624 struct reiserfs_list_bitmap *jb = NULL;
3625 int cleaned = 0;
3626 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003628 cn = get_journal_hash_dev(p_s_sb, journal->j_hash_table, blocknr);
3629 if (cn && cn->bh) {
3630 bh = cn->bh;
3631 get_bh(bh);
3632 }
3633 /* if it is journal new, we just remove it from this transaction */
3634 if (bh && buffer_journal_new(bh)) {
3635 clear_buffer_journal_new(bh);
3636 clear_prepared_bits(bh);
3637 reiserfs_clean_and_file_buffer(bh);
3638 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned);
3639 } else {
3640 /* set the bit for this block in the journal bitmap for this transaction */
3641 jb = journal->j_current_jl->j_list_bitmap;
3642 if (!jb) {
3643 reiserfs_panic(p_s_sb,
3644 "journal-1702: journal_mark_freed, journal_list_bitmap is NULL\n");
3645 }
3646 set_bit_in_list_bitmap(p_s_sb, blocknr, jb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003648 /* Note, the entire while loop is not allowed to schedule. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003650 if (bh) {
3651 clear_prepared_bits(bh);
3652 reiserfs_clean_and_file_buffer(bh);
3653 }
3654 cleaned = remove_from_transaction(p_s_sb, blocknr, cleaned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003656 /* find all older transactions with this block, make sure they don't try to write it out */
3657 cn = get_journal_hash_dev(p_s_sb, journal->j_list_hash_table,
3658 blocknr);
3659 while (cn) {
3660 if (p_s_sb == cn->sb && blocknr == cn->blocknr) {
3661 set_bit(BLOCK_FREED, &cn->state);
3662 if (cn->bh) {
3663 if (!cleaned) {
3664 /* remove_from_transaction will brelse the buffer if it was
3665 ** in the current trans
3666 */
3667 clear_buffer_journal_dirty(cn->
3668 bh);
3669 clear_buffer_dirty(cn->bh);
3670 clear_buffer_journal_test(cn->
3671 bh);
3672 cleaned = 1;
3673 put_bh(cn->bh);
3674 if (atomic_read
3675 (&(cn->bh->b_count)) < 0) {
3676 reiserfs_warning(p_s_sb,
3677 "journal-2138: cn->bh->b_count < 0");
3678 }
3679 }
3680 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3681 atomic_dec(&
3682 (cn->jlist->
3683 j_nonzerolen));
3684 }
3685 cn->bh = NULL;
3686 }
3687 }
3688 cn = cn->hnext;
3689 }
3690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003692 if (bh) {
3693 put_bh(bh); /* get_hash grabs the buffer */
3694 if (atomic_read(&(bh->b_count)) < 0) {
3695 reiserfs_warning(p_s_sb,
3696 "journal-2165: bh->b_count < 0");
3697 }
3698 }
3699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003702void reiserfs_update_inode_transaction(struct inode *inode)
3703{
3704 struct reiserfs_journal *journal = SB_JOURNAL(inode->i_sb);
3705 REISERFS_I(inode)->i_jl = journal->j_current_jl;
3706 REISERFS_I(inode)->i_trans_id = journal->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
3709/*
3710 * returns -1 on error, 0 if no commits/barriers were done and 1
3711 * if a transaction was actually committed and the barrier was done
3712 */
3713static int __commit_trans_jl(struct inode *inode, unsigned long id,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003714 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003716 struct reiserfs_transaction_handle th;
3717 struct super_block *sb = inode->i_sb;
3718 struct reiserfs_journal *journal = SB_JOURNAL(sb);
3719 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003721 /* is it from the current transaction, or from an unknown transaction? */
3722 if (id == journal->j_trans_id) {
3723 jl = journal->j_current_jl;
3724 /* try to let other writers come in and grow this transaction */
3725 let_transaction_grow(sb, id);
3726 if (journal->j_trans_id != id) {
3727 goto flush_commit_only;
3728 }
3729
3730 ret = journal_begin(&th, sb, 1);
3731 if (ret)
3732 return ret;
3733
3734 /* someone might have ended this transaction while we joined */
3735 if (journal->j_trans_id != id) {
3736 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
3737 1);
3738 journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb));
3739 ret = journal_end(&th, sb, 1);
3740 goto flush_commit_only;
3741 }
3742
3743 ret = journal_end_sync(&th, sb, 1);
3744 if (!ret)
3745 ret = 1;
3746
3747 } else {
3748 /* this gets tricky, we have to make sure the journal list in
3749 * the inode still exists. We know the list is still around
3750 * if we've got a larger transaction id than the oldest list
3751 */
3752 flush_commit_only:
3753 if (journal_list_still_alive(inode->i_sb, id)) {
3754 /*
3755 * we only set ret to 1 when we know for sure
3756 * the barrier hasn't been started yet on the commit
3757 * block.
3758 */
3759 if (atomic_read(&jl->j_commit_left) > 1)
3760 ret = 1;
3761 flush_commit_list(sb, jl, 1);
3762 if (journal->j_errno)
3763 ret = journal->j_errno;
3764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003766 /* otherwise the list is gone, and long since committed */
3767 return ret;
3768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003770int reiserfs_commit_for_inode(struct inode *inode)
3771{
3772 unsigned long id = REISERFS_I(inode)->i_trans_id;
3773 struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003775 /* for the whole inode, assume unset id means it was
3776 * changed in the current transaction. More conservative
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003778 if (!id || !jl) {
3779 reiserfs_update_inode_transaction(inode);
3780 id = REISERFS_I(inode)->i_trans_id;
3781 /* jl will be updated in __commit_trans_jl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003783
3784 return __commit_trans_jl(inode, id, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785}
3786
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003787void reiserfs_restore_prepared_buffer(struct super_block *p_s_sb,
3788 struct buffer_head *bh)
3789{
3790 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3791 PROC_INFO_INC(p_s_sb, journal.restore_prepared);
3792 if (!bh) {
3793 return;
3794 }
3795 if (test_clear_buffer_journal_restore_dirty(bh) &&
3796 buffer_journal_dirty(bh)) {
3797 struct reiserfs_journal_cnode *cn;
3798 cn = get_journal_hash_dev(p_s_sb,
3799 journal->j_list_hash_table,
3800 bh->b_blocknr);
3801 if (cn && can_dirty(cn)) {
3802 set_buffer_journal_test(bh);
3803 mark_buffer_dirty(bh);
3804 }
3805 }
3806 clear_buffer_journal_prepared(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003809extern struct tree_balance *cur_tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810/*
3811** before we can change a metadata block, we have to make sure it won't
3812** be written to disk while we are altering it. So, we must:
3813** clean it
3814** wait on it.
3815**
3816*/
3817int reiserfs_prepare_for_journal(struct super_block *p_s_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003818 struct buffer_head *bh, int wait)
3819{
3820 PROC_INFO_INC(p_s_sb, journal.prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003822 if (test_set_buffer_locked(bh)) {
3823 if (!wait)
3824 return 0;
3825 lock_buffer(bh);
3826 }
3827 set_buffer_journal_prepared(bh);
3828 if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
3829 clear_buffer_journal_test(bh);
3830 set_buffer_journal_restore_dirty(bh);
3831 }
3832 unlock_buffer(bh);
3833 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
3835
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003836static void flush_old_journal_lists(struct super_block *s)
3837{
3838 struct reiserfs_journal *journal = SB_JOURNAL(s);
3839 struct reiserfs_journal_list *jl;
3840 struct list_head *entry;
3841 time_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003843 while (!list_empty(&journal->j_journal_list)) {
3844 entry = journal->j_journal_list.next;
3845 jl = JOURNAL_LIST_ENTRY(entry);
3846 /* this check should always be run, to send old lists to disk */
3847 if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4))) {
3848 flush_used_journal_lists(s, jl);
3849 } else {
3850 break;
3851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
3855/*
3856** long and ugly. If flush, will not return until all commit
3857** blocks and all real buffers in the trans are on disk.
3858** If no_async, won't return until all commit blocks are on disk.
3859**
3860** keep reading, there are comments as you go along
3861**
3862** If the journal is aborted, we just clean up. Things like flushing
3863** journal lists, etc just won't happen.
3864*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003865static int do_journal_end(struct reiserfs_transaction_handle *th,
3866 struct super_block *p_s_sb, unsigned long nblocks,
3867 int flags)
3868{
3869 struct reiserfs_journal *journal = SB_JOURNAL(p_s_sb);
3870 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
3871 struct reiserfs_journal_cnode *last_cn = NULL;
3872 struct reiserfs_journal_desc *desc;
3873 struct reiserfs_journal_commit *commit;
3874 struct buffer_head *c_bh; /* commit bh */
3875 struct buffer_head *d_bh; /* desc bh */
3876 int cur_write_start = 0; /* start index of current log write */
3877 int old_start;
3878 int i;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08003879 int flush;
3880 int wait_on_commit;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003881 struct reiserfs_journal_list *jl, *temp_jl;
3882 struct list_head *entry, *safe;
3883 unsigned long jindex;
3884 unsigned long commit_trans_id;
3885 int trans_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003887 BUG_ON(th->t_refcount > 1);
3888 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08003890 /* protect flush_older_commits from doing mistakes if the
3891 transaction ID counter gets overflowed. */
3892 if (th->t_trans_id == ~0UL)
3893 flags |= FLUSH_ALL | COMMIT_NOW | WAIT;
3894 flush = flags & FLUSH_ALL;
3895 wait_on_commit = flags & WAIT;
3896
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003897 put_fs_excl();
3898 current->journal_info = th->t_handle_save;
3899 reiserfs_check_lock_depth(p_s_sb, "journal end");
3900 if (journal->j_len == 0) {
3901 reiserfs_prepare_for_journal(p_s_sb, SB_BUFFER_WITH_SB(p_s_sb),
3902 1);
3903 journal_mark_dirty(th, p_s_sb, SB_BUFFER_WITH_SB(p_s_sb));
3904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003906 lock_journal(p_s_sb);
3907 if (journal->j_next_full_flush) {
3908 flags |= FLUSH_ALL;
3909 flush = 1;
3910 }
3911 if (journal->j_next_async_flush) {
3912 flags |= COMMIT_NOW | WAIT;
3913 wait_on_commit = 1;
3914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003916 /* check_journal_end locks the journal, and unlocks if it does not return 1
3917 ** it tells us if we should continue with the journal_end, or just return
3918 */
3919 if (!check_journal_end(th, p_s_sb, nblocks, flags)) {
3920 p_s_sb->s_dirt = 1;
3921 wake_queued_writers(p_s_sb);
3922 reiserfs_async_progress_wait(p_s_sb);
3923 goto out;
3924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003926 /* check_journal_end might set these, check again */
3927 if (journal->j_next_full_flush) {
3928 flush = 1;
3929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003931 /*
3932 ** j must wait means we have to flush the log blocks, and the real blocks for
3933 ** this transaction
3934 */
3935 if (journal->j_must_wait > 0) {
3936 flush = 1;
3937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938#ifdef REISERFS_PREALLOCATE
Jan Karaef43bc42006-01-11 12:17:40 -08003939 /* quota ops might need to nest, setup the journal_info pointer for them
3940 * and raise the refcount so that it is > 0. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003941 current->journal_info = th;
Jan Karaef43bc42006-01-11 12:17:40 -08003942 th->t_refcount++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003943 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
3944 * the transaction */
Jan Karaef43bc42006-01-11 12:17:40 -08003945 th->t_refcount--;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003946 current->journal_info = th->t_handle_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003949 /* setup description block */
3950 d_bh =
3951 journal_getblk(p_s_sb,
3952 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3953 journal->j_start);
3954 set_buffer_uptodate(d_bh);
3955 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data;
3956 memset(d_bh->b_data, 0, d_bh->b_size);
3957 memcpy(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8);
3958 set_desc_trans_id(desc, journal->j_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003960 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
3961 c_bh = journal_getblk(p_s_sb, SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
3962 ((journal->j_start + journal->j_len +
3963 1) % SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
3964 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
3965 memset(c_bh->b_data, 0, c_bh->b_size);
3966 set_commit_trans_id(commit, journal->j_trans_id);
3967 set_buffer_uptodate(c_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003969 /* init this journal list */
3970 jl = journal->j_current_jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003972 /* we lock the commit before doing anything because
3973 * we want to make sure nobody tries to run flush_commit_list until
3974 * the new transaction is fully setup, and we've already flushed the
3975 * ordered bh list
3976 */
3977 down(&jl->j_commit_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003979 /* save the transaction id in case we need to commit it later */
3980 commit_trans_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003982 atomic_set(&jl->j_older_commits_done, 0);
3983 jl->j_trans_id = journal->j_trans_id;
3984 jl->j_timestamp = journal->j_trans_start_time;
3985 jl->j_commit_bh = c_bh;
3986 jl->j_start = journal->j_start;
3987 jl->j_len = journal->j_len;
3988 atomic_set(&jl->j_nonzerolen, journal->j_len);
3989 atomic_set(&jl->j_commit_left, journal->j_len + 2);
3990 jl->j_realblock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003992 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
3993 ** for each real block, add it to the journal list hash,
3994 ** copy into real block index array in the commit or desc block
3995 */
3996 trans_half = journal_trans_half(p_s_sb->s_blocksize);
3997 for (i = 0, cn = journal->j_first; cn; cn = cn->next, i++) {
3998 if (buffer_journaled(cn->bh)) {
3999 jl_cn = get_cnode(p_s_sb);
4000 if (!jl_cn) {
4001 reiserfs_panic(p_s_sb,
4002 "journal-1676, get_cnode returned NULL\n");
4003 }
4004 if (i == 0) {
4005 jl->j_realblock = jl_cn;
4006 }
4007 jl_cn->prev = last_cn;
4008 jl_cn->next = NULL;
4009 if (last_cn) {
4010 last_cn->next = jl_cn;
4011 }
4012 last_cn = jl_cn;
4013 /* make sure the block we are trying to log is not a block
4014 of journal or reserved area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004016 if (is_block_in_log_or_reserved_area
4017 (p_s_sb, cn->bh->b_blocknr)) {
4018 reiserfs_panic(p_s_sb,
4019 "journal-2332: Trying to log block %lu, which is a log block\n",
4020 cn->bh->b_blocknr);
4021 }
4022 jl_cn->blocknr = cn->bh->b_blocknr;
4023 jl_cn->state = 0;
4024 jl_cn->sb = p_s_sb;
4025 jl_cn->bh = cn->bh;
4026 jl_cn->jlist = jl;
4027 insert_journal_hash(journal->j_list_hash_table, jl_cn);
4028 if (i < trans_half) {
4029 desc->j_realblock[i] =
4030 cpu_to_le32(cn->bh->b_blocknr);
4031 } else {
4032 commit->j_realblock[i - trans_half] =
4033 cpu_to_le32(cn->bh->b_blocknr);
4034 }
4035 } else {
4036 i--;
4037 }
4038 }
4039 set_desc_trans_len(desc, journal->j_len);
4040 set_desc_mount_id(desc, journal->j_mount_id);
4041 set_desc_trans_id(desc, journal->j_trans_id);
4042 set_commit_trans_len(commit, journal->j_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004044 /* special check in case all buffers in the journal were marked for not logging */
4045 if (journal->j_len == 0) {
4046 BUG();
4047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004049 /* we're about to dirty all the log blocks, mark the description block
4050 * dirty now too. Don't mark the commit block dirty until all the
4051 * others are on disk
4052 */
4053 mark_buffer_dirty(d_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004055 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
4056 cur_write_start = journal->j_start;
4057 cn = journal->j_first;
4058 jindex = 1; /* start at one so we don't get the desc again */
4059 while (cn) {
4060 clear_buffer_journal_new(cn->bh);
4061 /* copy all the real blocks into log area. dirty log blocks */
4062 if (buffer_journaled(cn->bh)) {
4063 struct buffer_head *tmp_bh;
4064 char *addr;
4065 struct page *page;
4066 tmp_bh =
4067 journal_getblk(p_s_sb,
4068 SB_ONDISK_JOURNAL_1st_BLOCK(p_s_sb) +
4069 ((cur_write_start +
4070 jindex) %
4071 SB_ONDISK_JOURNAL_SIZE(p_s_sb)));
4072 set_buffer_uptodate(tmp_bh);
4073 page = cn->bh->b_page;
4074 addr = kmap(page);
4075 memcpy(tmp_bh->b_data,
4076 addr + offset_in_page(cn->bh->b_data),
4077 cn->bh->b_size);
4078 kunmap(page);
4079 mark_buffer_dirty(tmp_bh);
4080 jindex++;
4081 set_buffer_journal_dirty(cn->bh);
4082 clear_buffer_journaled(cn->bh);
4083 } else {
4084 /* JDirty cleared sometime during transaction. don't log this one */
4085 reiserfs_warning(p_s_sb,
4086 "journal-2048: do_journal_end: BAD, buffer in journal hash, but not JDirty!");
4087 brelse(cn->bh);
4088 }
4089 next = cn->next;
4090 free_cnode(p_s_sb, cn);
4091 cn = next;
4092 cond_resched();
4093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004095 /* we are done with both the c_bh and d_bh, but
4096 ** c_bh must be written after all other commit blocks,
4097 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
4098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004100 journal->j_current_jl = alloc_journal_list(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004102 /* now it is safe to insert this transaction on the main list */
4103 list_add_tail(&jl->j_list, &journal->j_journal_list);
4104 list_add_tail(&jl->j_working_list, &journal->j_working_list);
4105 journal->j_num_work_lists++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004107 /* reset journal values for the next transaction */
4108 old_start = journal->j_start;
4109 journal->j_start =
4110 (journal->j_start + journal->j_len +
4111 2) % SB_ONDISK_JOURNAL_SIZE(p_s_sb);
4112 atomic_set(&(journal->j_wcount), 0);
4113 journal->j_bcount = 0;
4114 journal->j_last = NULL;
4115 journal->j_first = NULL;
4116 journal->j_len = 0;
4117 journal->j_trans_start_time = 0;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08004118 /* check for trans_id overflow */
4119 if (++journal->j_trans_id == 0)
4120 journal->j_trans_id = 10;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004121 journal->j_current_jl->j_trans_id = journal->j_trans_id;
4122 journal->j_must_wait = 0;
4123 journal->j_len_alloc = 0;
4124 journal->j_next_full_flush = 0;
4125 journal->j_next_async_flush = 0;
4126 init_journal_hash(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004128 // make sure reiserfs_add_jh sees the new current_jl before we
4129 // write out the tails
4130 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004132 /* tail conversion targets have to hit the disk before we end the
4133 * transaction. Otherwise a later transaction might repack the tail
4134 * before this transaction commits, leaving the data block unflushed and
4135 * clean, if we crash before the later transaction commits, the data block
4136 * is lost.
4137 */
4138 if (!list_empty(&jl->j_tail_bh_list)) {
4139 unlock_kernel();
4140 write_ordered_buffers(&journal->j_dirty_buffers_lock,
4141 journal, jl, &jl->j_tail_bh_list);
4142 lock_kernel();
4143 }
4144 if (!list_empty(&jl->j_tail_bh_list))
4145 BUG();
4146 up(&jl->j_commit_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004148 /* honor the flush wishes from the caller, simple commits can
4149 ** be done outside the journal lock, they are done below
4150 **
4151 ** if we don't flush the commit list right now, we put it into
4152 ** the work queue so the people waiting on the async progress work
4153 ** queue don't wait for this proc to flush journal lists and such.
4154 */
4155 if (flush) {
4156 flush_commit_list(p_s_sb, jl, 1);
4157 flush_journal_list(p_s_sb, jl, 1);
4158 } else if (!(jl->j_state & LIST_COMMIT_PENDING))
4159 queue_delayed_work(commit_wq, &journal->j_work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004161 /* if the next transaction has any chance of wrapping, flush
4162 ** transactions that might get overwritten. If any journal lists are very
4163 ** old flush them as well.
4164 */
4165 first_jl:
4166 list_for_each_safe(entry, safe, &journal->j_journal_list) {
4167 temp_jl = JOURNAL_LIST_ENTRY(entry);
4168 if (journal->j_start <= temp_jl->j_start) {
4169 if ((journal->j_start + journal->j_trans_max + 1) >=
4170 temp_jl->j_start) {
4171 flush_used_journal_lists(p_s_sb, temp_jl);
4172 goto first_jl;
4173 } else if ((journal->j_start +
4174 journal->j_trans_max + 1) <
4175 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
4176 /* if we don't cross into the next transaction and we don't
4177 * wrap, there is no way we can overlap any later transactions
4178 * break now
4179 */
4180 break;
4181 }
4182 } else if ((journal->j_start +
4183 journal->j_trans_max + 1) >
4184 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) {
4185 if (((journal->j_start + journal->j_trans_max + 1) %
4186 SB_ONDISK_JOURNAL_SIZE(p_s_sb)) >=
4187 temp_jl->j_start) {
4188 flush_used_journal_lists(p_s_sb, temp_jl);
4189 goto first_jl;
4190 } else {
4191 /* we don't overlap anything from out start to the end of the
4192 * log, and our wrapped portion doesn't overlap anything at
4193 * the start of the log. We can break
4194 */
4195 break;
4196 }
4197 }
4198 }
4199 flush_old_journal_lists(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004201 journal->j_current_jl->j_list_bitmap =
4202 get_list_bitmap(p_s_sb, journal->j_current_jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004204 if (!(journal->j_current_jl->j_list_bitmap)) {
4205 reiserfs_panic(p_s_sb,
4206 "journal-1996: do_journal_end, could not get a list bitmap\n");
4207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004209 atomic_set(&(journal->j_jlock), 0);
4210 unlock_journal(p_s_sb);
4211 /* wake up any body waiting to join. */
4212 clear_bit(J_WRITERS_QUEUED, &journal->j_state);
4213 wake_up(&(journal->j_join_wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004215 if (!flush && wait_on_commit &&
4216 journal_list_still_alive(p_s_sb, commit_trans_id)) {
4217 flush_commit_list(p_s_sb, jl, 1);
4218 }
4219 out:
4220 reiserfs_check_lock_depth(p_s_sb, "journal end2");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004222 memset(th, 0, sizeof(*th));
4223 /* Re-set th->t_super, so we can properly keep track of how many
4224 * persistent transactions there are. We need to do this so if this
4225 * call is part of a failed restart_transaction, we can free it later */
4226 th->t_super = p_s_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004228 return journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229}
4230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004231static void __reiserfs_journal_abort_hard(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004233 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4234 if (test_bit(J_ABORTED, &journal->j_state))
4235 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004237 printk(KERN_CRIT "REISERFS: Aborting journal for filesystem on %s\n",
4238 reiserfs_bdevname(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004240 sb->s_flags |= MS_RDONLY;
4241 set_bit(J_ABORTED, &journal->j_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
4243#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004244 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245#endif
4246}
4247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004248static void __reiserfs_journal_abort_soft(struct super_block *sb, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004250 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4251 if (test_bit(J_ABORTED, &journal->j_state))
4252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004254 if (!journal->j_errno)
4255 journal->j_errno = errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004257 __reiserfs_journal_abort_hard(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004260void reiserfs_journal_abort(struct super_block *sb, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004262 return __reiserfs_journal_abort_soft(sb, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}