blob: eb711060a6f2b771d4b4e68613ea4890273633dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2** Write ahead logging implementation copyright Chris Mason 2000
3**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004** The background commits make this code very interrelated, and
Linus Torvalds1da177e2005-04-16 15:20:36 -07005** overly complex. I need to rethink things a bit....The major players:
6**
Jeff Mahoney0222e652009-03-30 14:02:44 -04007** journal_begin -- call with the number of blocks you expect to log.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008** If the current transaction is too
Jeff Mahoney0222e652009-03-30 14:02:44 -04009** old, it will block until the current transaction is
Linus Torvalds1da177e2005-04-16 15:20:36 -070010** finished, and then start a new one.
Jeff Mahoney0222e652009-03-30 14:02:44 -040011** Usually, your transaction will get joined in with
Linus Torvalds1da177e2005-04-16 15:20:36 -070012** previous ones for speed.
13**
Jeff Mahoney0222e652009-03-30 14:02:44 -040014** journal_join -- same as journal_begin, but won't block on the current
Linus Torvalds1da177e2005-04-16 15:20:36 -070015** transaction regardless of age. Don't ever call
Jeff Mahoney0222e652009-03-30 14:02:44 -040016** this. Ever. There are only two places it should be
Linus Torvalds1da177e2005-04-16 15:20:36 -070017** called from, and they are both inside this file.
18**
Jeff Mahoney0222e652009-03-30 14:02:44 -040019** journal_mark_dirty -- adds blocks into this transaction. clears any flags
Linus Torvalds1da177e2005-04-16 15:20:36 -070020** that might make them get sent to disk
Jeff Mahoney0222e652009-03-30 14:02:44 -040021** and then marks them BH_JDirty. Puts the buffer head
22** into the current transaction hash.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023**
24** journal_end -- if the current transaction is batchable, it does nothing
25** otherwise, it could do an async/synchronous commit, or
Jeff Mahoney0222e652009-03-30 14:02:44 -040026** a full flush of all log and real blocks in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070027** transaction.
28**
Jeff Mahoney0222e652009-03-30 14:02:44 -040029** flush_old_commits -- if the current transaction is too old, it is ended and
30** commit blocks are sent to disk. Forces commit blocks
31** to disk for all backgrounded commits that have been
Linus Torvalds1da177e2005-04-16 15:20:36 -070032** around too long.
Jeff Mahoney0222e652009-03-30 14:02:44 -040033** -- Note, if you call this as an immediate flush from
Linus Torvalds1da177e2005-04-16 15:20:36 -070034** from within kupdate, it will ignore the immediate flag
35*/
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/time.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040038#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/vmalloc.h>
40#include <linux/reiserfs_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/kernel.h>
42#include <linux/errno.h>
43#include <linux/fcntl.h>
44#include <linux/stat.h>
45#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/buffer_head.h>
47#include <linux/workqueue.h>
48#include <linux/writeback.h>
49#include <linux/blkdev.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070050#include <linux/backing-dev.h>
Jeff Mahoney90415de2008-07-25 01:46:40 -070051#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Jeff Mahoney90415de2008-07-25 01:46:40 -070053
54#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* gets a struct reiserfs_journal_list * from a list head */
57#define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
58 j_list))
59#define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
60 j_working_list))
61
62/* the number of mounted filesystems. This is used to decide when to
63** start and kill the commit workqueue
64*/
65static int reiserfs_mounted_fs_count;
66
67static struct workqueue_struct *commit_wq;
68
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070069#define JOURNAL_TRANS_HALF 1018 /* must be correct to keep the desc and commit
70 structs at 4k */
71#define BUFNR 64 /*read ahead */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* cnode stat bits. Move these into reiserfs_fs.h */
74
75#define BLOCK_FREED 2 /* this block was freed, and can't be written. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070076#define BLOCK_FREED_HOLDER 3 /* this block was freed during this transaction, and can't be written */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#define BLOCK_NEEDS_FLUSH 4 /* used in flush_journal_list */
79#define BLOCK_DIRTIED 5
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* journal list state bits */
82#define LIST_TOUCHED 1
83#define LIST_DIRTY 2
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084#define LIST_COMMIT_PENDING 4 /* someone will commit this list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* flags for do_journal_end */
87#define FLUSH_ALL 1 /* flush commit and real blocks */
88#define COMMIT_NOW 2 /* end and commit this transaction */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089#define WAIT 4 /* wait for the log blocks to hit the disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070091static int do_journal_end(struct reiserfs_transaction_handle *,
92 struct super_block *, unsigned long nblocks,
93 int flags);
94static int flush_journal_list(struct super_block *s,
95 struct reiserfs_journal_list *jl, int flushall);
96static int flush_commit_list(struct super_block *s,
97 struct reiserfs_journal_list *jl, int flushall);
98static int can_dirty(struct reiserfs_journal_cnode *cn);
99static int journal_join(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400100 struct super_block *sb, unsigned long nblocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700101static int release_journal_dev(struct super_block *super,
102 struct reiserfs_journal *journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int dirty_one_transaction(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700104 struct reiserfs_journal_list *jl);
David Howellsc4028952006-11-22 14:57:56 +0000105static void flush_async_commits(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static void queue_log_writer(struct super_block *s);
107
108/* values for join in do_journal_begin_r */
109enum {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110 JBEGIN_REG = 0, /* regular journal begin */
111 JBEGIN_JOIN = 1, /* join the running transaction if at all possible */
112 JBEGIN_ABORT = 2, /* called from cleanup code, ignores aborted flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400116 struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 unsigned long nblocks, int join);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400119static void init_journal_hash(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400121 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700122 memset(journal->j_hash_table, 0,
123 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
127** clears BH_Dirty and sticks the buffer on the clean list. Called because I can't allow refile_buffer to
128** make schedule happen after I've freed a block. Look at remove_from_transaction and journal_mark_freed for
129** more details.
130*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700131static int reiserfs_clean_and_file_buffer(struct buffer_head *bh)
132{
133 if (bh) {
134 clear_buffer_dirty(bh);
135 clear_buffer_journal_test(bh);
136 }
137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700140static struct reiserfs_bitmap_node *allocate_bitmap_node(struct super_block
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400141 *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142{
143 struct reiserfs_bitmap_node *bn;
144 static int id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Pekka Enbergd739b422006-02-01 03:06:43 -0800146 bn = kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 if (!bn) {
148 return NULL;
149 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400150 bn->data = kzalloc(sb->s_blocksize, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151 if (!bn->data) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800152 kfree(bn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 return NULL;
154 }
155 bn->id = id++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 INIT_LIST_HEAD(&bn->list);
157 return bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400160static struct reiserfs_bitmap_node *get_bitmap_node(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400162 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700163 struct reiserfs_bitmap_node *bn = NULL;
164 struct list_head *entry = journal->j_bitmap_nodes.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700166 journal->j_used_bitmap_nodes++;
167 repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 if (entry != &journal->j_bitmap_nodes) {
170 bn = list_entry(entry, struct reiserfs_bitmap_node, list);
171 list_del(entry);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400172 memset(bn->data, 0, sb->s_blocksize);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 journal->j_free_bitmap_nodes--;
174 return bn;
175 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400176 bn = allocate_bitmap_node(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 if (!bn) {
178 yield();
179 goto repeat;
180 }
181 return bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400183static inline void free_bitmap_node(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 struct reiserfs_bitmap_node *bn)
185{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400186 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 journal->j_used_bitmap_nodes--;
188 if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800189 kfree(bn->data);
190 kfree(bn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 } else {
192 list_add(&bn->list, &journal->j_bitmap_nodes);
193 journal->j_free_bitmap_nodes++;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400197static void allocate_bitmap_nodes(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198{
199 int i;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400200 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 struct reiserfs_bitmap_node *bn = NULL;
202 for (i = 0; i < REISERFS_MIN_BITMAP_NODES; i++) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400203 bn = allocate_bitmap_node(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 if (bn) {
205 list_add(&bn->list, &journal->j_bitmap_nodes);
206 journal->j_free_bitmap_nodes++;
207 } else {
Jeff Mahoney0222e652009-03-30 14:02:44 -0400208 break; /* this is ok, we'll try again when more are needed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700209 }
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400213static int set_bit_in_list_bitmap(struct super_block *sb,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700214 b_blocknr_t block,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 struct reiserfs_list_bitmap *jb)
216{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400217 unsigned int bmap_nr = block / (sb->s_blocksize << 3);
218 unsigned int bit_nr = block % (sb->s_blocksize << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 if (!jb->bitmaps[bmap_nr]) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400221 jb->bitmaps[bmap_nr] = get_bitmap_node(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 }
223 set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data);
224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400227static void cleanup_bitmap_list(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228 struct reiserfs_list_bitmap *jb)
229{
230 int i;
231 if (jb->bitmaps == NULL)
232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400234 for (i = 0; i < reiserfs_bmap_count(sb); i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 if (jb->bitmaps[i]) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400236 free_bitmap_node(sb, jb->bitmaps[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 jb->bitmaps[i] = NULL;
238 }
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/*
243** only call this on FS unmount.
244*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400245static int free_list_bitmaps(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246 struct reiserfs_list_bitmap *jb_array)
247{
248 int i;
249 struct reiserfs_list_bitmap *jb;
250 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
251 jb = jb_array + i;
252 jb->journal_list = NULL;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400253 cleanup_bitmap_list(sb, jb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700254 vfree(jb->bitmaps);
255 jb->bitmaps = NULL;
256 }
257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400260static int free_bitmap_nodes(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400262 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700263 struct list_head *next = journal->j_bitmap_nodes.next;
264 struct reiserfs_bitmap_node *bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700266 while (next != &journal->j_bitmap_nodes) {
267 bn = list_entry(next, struct reiserfs_bitmap_node, list);
268 list_del(next);
Pekka Enbergd739b422006-02-01 03:06:43 -0800269 kfree(bn->data);
270 kfree(bn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700271 next = journal->j_bitmap_nodes.next;
272 journal->j_free_bitmap_nodes--;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
Jeff Mahoney0222e652009-03-30 14:02:44 -0400279** get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280** jb_array is the array to be filled in.
281*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400282int reiserfs_allocate_list_bitmaps(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700283 struct reiserfs_list_bitmap *jb_array,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700284 unsigned int bmap_nr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700285{
286 int i;
287 int failed = 0;
288 struct reiserfs_list_bitmap *jb;
289 int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
292 jb = jb_array + i;
293 jb->journal_list = NULL;
Joe Perches558feb02011-05-28 10:36:33 -0700294 jb->bitmaps = vzalloc(mem);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 if (!jb->bitmaps) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400296 reiserfs_warning(sb, "clm-2000", "unable to "
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400297 "allocate bitmaps for journal lists");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298 failed = 1;
299 break;
300 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700301 }
302 if (failed) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400303 free_list_bitmaps(sb, jb_array);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700304 return -1;
305 }
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
Jeff Mahoney0222e652009-03-30 14:02:44 -0400310** find an available list bitmap. If you can't find one, flush a commit list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311** and try again
312*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400313static struct reiserfs_list_bitmap *get_list_bitmap(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 struct reiserfs_journal_list
315 *jl)
316{
317 int i, j;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400318 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700319 struct reiserfs_list_bitmap *jb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700321 for (j = 0; j < (JOURNAL_NUM_BITMAPS * 3); j++) {
322 i = journal->j_list_bitmap_index;
323 journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS;
324 jb = journal->j_list_bitmap + i;
325 if (journal->j_list_bitmap[i].journal_list) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400326 flush_commit_list(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700327 journal->j_list_bitmap[i].
328 journal_list, 1);
329 if (!journal->j_list_bitmap[i].journal_list) {
330 break;
331 }
332 } else {
333 break;
334 }
335 }
336 if (jb->journal_list) { /* double check to make sure if flushed correctly */
337 return NULL;
338 }
339 jb->journal_list = jl;
340 return jb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Jeff Mahoney0222e652009-03-30 14:02:44 -0400343/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344** allocates a new chunk of X nodes, and links them all together as a list.
345** Uses the cnode->next and cnode->prev pointers
346** returns NULL on failure
347*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700348static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes)
349{
350 struct reiserfs_journal_cnode *head;
351 int i;
352 if (num_cnodes <= 0) {
353 return NULL;
354 }
Joe Perches558feb02011-05-28 10:36:33 -0700355 head = vzalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700356 if (!head) {
357 return NULL;
358 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700359 head[0].prev = NULL;
360 head[0].next = head + 1;
361 for (i = 1; i < num_cnodes; i++) {
362 head[i].prev = head + (i - 1);
363 head[i].next = head + (i + 1); /* if last one, overwrite it after the if */
364 }
365 head[num_cnodes - 1].next = NULL;
366 return head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/*
Jeff Mahoney0222e652009-03-30 14:02:44 -0400370** pulls a cnode off the free list, or returns NULL on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400372static struct reiserfs_journal_cnode *get_cnode(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700373{
374 struct reiserfs_journal_cnode *cn;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400375 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400377 reiserfs_check_lock_depth(sb, "get_cnode");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700379 if (journal->j_cnode_free <= 0) {
380 return NULL;
381 }
382 journal->j_cnode_used++;
383 journal->j_cnode_free--;
384 cn = journal->j_cnode_free_list;
385 if (!cn) {
386 return cn;
387 }
388 if (cn->next) {
389 cn->next->prev = NULL;
390 }
391 journal->j_cnode_free_list = cn->next;
392 memset(cn, 0, sizeof(struct reiserfs_journal_cnode));
393 return cn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
Jeff Mahoney0222e652009-03-30 14:02:44 -0400397** returns a cnode to the free list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400399static void free_cnode(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700400 struct reiserfs_journal_cnode *cn)
401{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400402 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400404 reiserfs_check_lock_depth(sb, "free_cnode");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406 journal->j_cnode_used--;
407 journal->j_cnode_free++;
408 /* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
409 cn->next = journal->j_cnode_free_list;
410 if (journal->j_cnode_free_list) {
411 journal->j_cnode_free_list->prev = cn;
412 }
413 cn->prev = NULL; /* not needed with the memset, but I might kill the memset, and forget to do this */
414 journal->j_cnode_free_list = cn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700417static void clear_prepared_bits(struct buffer_head *bh)
418{
419 clear_buffer_journal_prepared(bh);
420 clear_buffer_journal_restore_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/* return a cnode with same dev, block number and size in table, or null if not found */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700424static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct
425 super_block
426 *sb,
427 struct
428 reiserfs_journal_cnode
429 **table,
430 long bl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700432 struct reiserfs_journal_cnode *cn;
433 cn = journal_hash(table, sb, bl);
434 while (cn) {
435 if (cn->blocknr == bl && cn->sb == sb)
436 return cn;
437 cn = cn->hnext;
438 }
439 return (struct reiserfs_journal_cnode *)0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/*
443** this actually means 'can this block be reallocated yet?'. If you set search_all, a block can only be allocated
444** if it is not in the current transaction, was not freed by the current transaction, and has no chance of ever
445** being overwritten by a replay after crashing.
446**
447** If you don't set search_all, a block can only be allocated if it is not in the current transaction. Since deleting
448** a block removes it from the current transaction, this case should never happen. If you don't set search_all, make
449** sure you never write the block without logging it.
450**
451** next_zero_bit is a suggestion about the next block to try for find_forward.
452** when bl is rejected because it is set in a journal list bitmap, we search
453** for the next zero bit in the bitmap that rejected bl. Then, we return that
454** through next_zero_bit for find_forward to try.
455**
456** Just because we return something in next_zero_bit does not mean we won't
457** reject it on the next call to reiserfs_in_journal
458**
459*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400460int reiserfs_in_journal(struct super_block *sb,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700461 unsigned int bmap_nr, int bit_nr, int search_all,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700462 b_blocknr_t * next_zero_bit)
463{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400464 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465 struct reiserfs_journal_cnode *cn;
466 struct reiserfs_list_bitmap *jb;
467 int i;
468 unsigned long bl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470 *next_zero_bit = 0; /* always start this at zero. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400472 PROC_INFO_INC(sb, journal.in_journal);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 /* If we aren't doing a search_all, this is a metablock, and it will be logged before use.
474 ** if we crash before the transaction that freed it commits, this transaction won't
475 ** have committed either, and the block will never be written
476 */
477 if (search_all) {
478 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400479 PROC_INFO_INC(sb, journal.in_journal_bitmap);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700480 jb = journal->j_list_bitmap + i;
481 if (jb->journal_list && jb->bitmaps[bmap_nr] &&
482 test_bit(bit_nr,
483 (unsigned long *)jb->bitmaps[bmap_nr]->
484 data)) {
485 *next_zero_bit =
486 find_next_zero_bit((unsigned long *)
487 (jb->bitmaps[bmap_nr]->
488 data),
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400489 sb->s_blocksize << 3,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700490 bit_nr + 1);
491 return 1;
492 }
493 }
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400496 bl = bmap_nr * (sb->s_blocksize << 3) + bit_nr;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700497 /* is it in any old transactions? */
498 if (search_all
499 && (cn =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400500 get_journal_hash_dev(sb, journal->j_list_hash_table, bl))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700501 return 1;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 /* is it in the current transaction. This should never happen */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400505 if ((cn = get_journal_hash_dev(sb, journal->j_hash_table, bl))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700506 BUG();
507 return 1;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400510 PROC_INFO_INC(sb, journal.in_journal_reusable);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700511 /* safe for reuse */
512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/* insert cn into table
516*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700517static inline void insert_journal_hash(struct reiserfs_journal_cnode **table,
518 struct reiserfs_journal_cnode *cn)
519{
520 struct reiserfs_journal_cnode *cn_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700522 cn_orig = journal_hash(table, cn->sb, cn->blocknr);
523 cn->hnext = cn_orig;
524 cn->hprev = NULL;
525 if (cn_orig) {
526 cn_orig->hprev = cn;
527 }
528 journal_hash(table, cn->sb, cn->blocknr) = cn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/* lock the current transaction */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400532static inline void lock_journal(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400534 PROC_INFO_INC(sb, journal.lock_journal);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200535
536 reiserfs_mutex_lock_safe(&SB_JOURNAL(sb)->j_mutex, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/* unlock the current transaction */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400540static inline void unlock_journal(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700541{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400542 mutex_unlock(&SB_JOURNAL(sb)->j_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static inline void get_journal_list(struct reiserfs_journal_list *jl)
546{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700547 jl->j_refcount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static inline void put_journal_list(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700551 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700553 if (jl->j_refcount < 1) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400554 reiserfs_panic(s, "journal-2", "trans id %u, refcount at %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555 jl->j_trans_id, jl->j_refcount);
556 }
557 if (--jl->j_refcount == 0)
Pekka Enbergd739b422006-02-01 03:06:43 -0800558 kfree(jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
562** this used to be much more involved, and I'm keeping it just in case things get ugly again.
563** it gets called by flush_commit_list, and cleans up any data stored about blocks freed during a
564** transaction.
565*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400566static void cleanup_freed_for_journal_list(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700567 struct reiserfs_journal_list *jl)
568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570 struct reiserfs_list_bitmap *jb = jl->j_list_bitmap;
571 if (jb) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400572 cleanup_bitmap_list(sb, jb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 }
574 jl->j_list_bitmap->journal_list = NULL;
575 jl->j_list_bitmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578static int journal_list_still_alive(struct super_block *s,
Jeff Mahoney600ed412009-03-30 14:02:17 -0400579 unsigned int trans_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700581 struct reiserfs_journal *journal = SB_JOURNAL(s);
582 struct list_head *entry = &journal->j_journal_list;
583 struct reiserfs_journal_list *jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700585 if (!list_empty(entry)) {
586 jl = JOURNAL_LIST_ENTRY(entry->next);
587 if (jl->j_trans_id <= trans_id) {
588 return 1;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Chris Mason398c95b2007-10-16 23:29:44 -0700594/*
595 * If page->mapping was null, we failed to truncate this page for
596 * some reason. Most likely because it was truncated after being
597 * logged via data=journal.
598 *
599 * This does a check to see if the buffer belongs to one of these
600 * lost pages before doing the final put_bh. If page->mapping was
601 * null, it tries to free buffers on the page, which should make the
602 * final page_cache_release drop the page from the lru.
603 */
604static void release_buffer_page(struct buffer_head *bh)
605{
606 struct page *page = bh->b_page;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200607 if (!page->mapping && trylock_page(page)) {
Chris Mason398c95b2007-10-16 23:29:44 -0700608 page_cache_get(page);
609 put_bh(bh);
610 if (!page->mapping)
611 try_to_free_buffers(page);
612 unlock_page(page);
613 page_cache_release(page);
614 } else {
615 put_bh(bh);
616 }
617}
618
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
620{
621 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 if (buffer_journaled(bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400624 reiserfs_warning(NULL, "clm-2084",
625 "pinned buffer %lu:%s sent to disk",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700626 bh->b_blocknr, bdevname(bh->b_bdev, b));
627 }
628 if (uptodate)
629 set_buffer_uptodate(bh);
630 else
631 clear_buffer_uptodate(bh);
Chris Mason398c95b2007-10-16 23:29:44 -0700632
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 unlock_buffer(bh);
Chris Mason398c95b2007-10-16 23:29:44 -0700634 release_buffer_page(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700637static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate)
638{
639 if (uptodate)
640 set_buffer_uptodate(bh);
641 else
642 clear_buffer_uptodate(bh);
643 unlock_buffer(bh);
644 put_bh(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700647static void submit_logged_buffer(struct buffer_head *bh)
648{
649 get_bh(bh);
650 bh->b_end_io = reiserfs_end_buffer_io_sync;
651 clear_buffer_journal_new(bh);
652 clear_buffer_dirty(bh);
653 if (!test_clear_buffer_journal_test(bh))
654 BUG();
655 if (!buffer_uptodate(bh))
656 BUG();
657 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700660static void submit_ordered_buffer(struct buffer_head *bh)
661{
662 get_bh(bh);
663 bh->b_end_io = reiserfs_end_ordered_io;
664 clear_buffer_dirty(bh);
665 if (!buffer_uptodate(bh))
666 BUG();
667 submit_bh(WRITE, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#define CHUNK_SIZE 32
671struct buffer_chunk {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 struct buffer_head *bh[CHUNK_SIZE];
673 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674};
675
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700676static void write_chunk(struct buffer_chunk *chunk)
677{
678 int i;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679 for (i = 0; i < chunk->nr; i++) {
680 submit_logged_buffer(chunk->bh[i]);
681 }
682 chunk->nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685static void write_ordered_chunk(struct buffer_chunk *chunk)
686{
687 int i;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688 for (i = 0; i < chunk->nr; i++) {
689 submit_ordered_buffer(chunk->bh[i]);
690 }
691 chunk->nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700695 spinlock_t * lock, void (fn) (struct buffer_chunk *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697 int ret = 0;
Eric Sesterhenn14a61442006-10-03 23:36:38 +0200698 BUG_ON(chunk->nr >= CHUNK_SIZE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700699 chunk->bh[chunk->nr++] = bh;
700 if (chunk->nr >= CHUNK_SIZE) {
701 ret = 1;
702 if (lock)
703 spin_unlock(lock);
704 fn(chunk);
705 if (lock)
706 spin_lock(lock);
707 }
708 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700712static struct reiserfs_jh *alloc_jh(void)
713{
714 struct reiserfs_jh *jh;
715 while (1) {
716 jh = kmalloc(sizeof(*jh), GFP_NOFS);
717 if (jh) {
718 atomic_inc(&nr_reiserfs_jh);
719 return jh;
720 }
721 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/*
726 * we want to free the jh when the buffer has been written
727 * and waited on
728 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700729void reiserfs_free_jh(struct buffer_head *bh)
730{
731 struct reiserfs_jh *jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700733 jh = bh->b_private;
734 if (jh) {
735 bh->b_private = NULL;
736 jh->bh = NULL;
737 list_del_init(&jh->list);
738 kfree(jh);
739 if (atomic_read(&nr_reiserfs_jh) <= 0)
740 BUG();
741 atomic_dec(&nr_reiserfs_jh);
742 put_bh(bh);
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700747 int tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700749 struct reiserfs_jh *jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751 if (bh->b_private) {
752 spin_lock(&j->j_dirty_buffers_lock);
753 if (!bh->b_private) {
754 spin_unlock(&j->j_dirty_buffers_lock);
755 goto no_jh;
756 }
757 jh = bh->b_private;
758 list_del_init(&jh->list);
759 } else {
760 no_jh:
761 get_bh(bh);
762 jh = alloc_jh();
763 spin_lock(&j->j_dirty_buffers_lock);
764 /* buffer must be locked for __add_jh, should be able to have
765 * two adds at the same time
766 */
Eric Sesterhenn14a61442006-10-03 23:36:38 +0200767 BUG_ON(bh->b_private);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 jh->bh = bh;
769 bh->b_private = jh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700771 jh->jl = j->j_current_jl;
772 if (tail)
773 list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
774 else {
775 list_add_tail(&jh->list, &jh->jl->j_bh_list);
776 }
777 spin_unlock(&j->j_dirty_buffers_lock);
778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh)
782{
783 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700785int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh)
786{
787 return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790#define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791static int write_ordered_buffers(spinlock_t * lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct reiserfs_journal *j,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 struct reiserfs_journal_list *jl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct list_head *list)
795{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700796 struct buffer_head *bh;
797 struct reiserfs_jh *jh;
798 int ret = j->j_errno;
799 struct buffer_chunk chunk;
800 struct list_head tmp;
801 INIT_LIST_HEAD(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700803 chunk.nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 spin_lock(lock);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700805 while (!list_empty(list)) {
806 jh = JH_ENTRY(list->next);
807 bh = jh->bh;
808 get_bh(bh);
Nick Pigginca5de402008-08-02 12:02:13 +0200809 if (!trylock_buffer(bh)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700810 if (!buffer_dirty(bh)) {
Akinobu Mitaf1166292006-06-26 00:24:46 -0700811 list_move(&jh->list, &tmp);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700812 goto loop_next;
813 }
814 spin_unlock(lock);
815 if (chunk.nr)
816 write_ordered_chunk(&chunk);
817 wait_on_buffer(bh);
818 cond_resched();
819 spin_lock(lock);
820 goto loop_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Chris Mason3d4492f2006-02-01 03:06:49 -0800822 /* in theory, dirty non-uptodate buffers should never get here,
823 * but the upper layer io error paths still have a few quirks.
824 * Handle them here as gracefully as we can
825 */
826 if (!buffer_uptodate(bh) && buffer_dirty(bh)) {
827 clear_buffer_dirty(bh);
828 ret = -EIO;
829 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700830 if (buffer_dirty(bh)) {
Akinobu Mitaf1166292006-06-26 00:24:46 -0700831 list_move(&jh->list, &tmp);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700832 add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
833 } else {
834 reiserfs_free_jh(bh);
835 unlock_buffer(bh);
836 }
837 loop_next:
838 put_bh(bh);
839 cond_resched_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700841 if (chunk.nr) {
842 spin_unlock(lock);
843 write_ordered_chunk(&chunk);
844 spin_lock(lock);
845 }
846 while (!list_empty(&tmp)) {
847 jh = JH_ENTRY(tmp.prev);
848 bh = jh->bh;
849 get_bh(bh);
850 reiserfs_free_jh(bh);
851
852 if (buffer_locked(bh)) {
853 spin_unlock(lock);
854 wait_on_buffer(bh);
855 spin_lock(lock);
856 }
857 if (!buffer_uptodate(bh)) {
858 ret = -EIO;
859 }
Chris Masond62b1b82006-02-01 03:06:47 -0800860 /* ugly interaction with invalidatepage here.
861 * reiserfs_invalidate_page will pin any buffer that has a valid
862 * journal head from an older transaction. If someone else sets
863 * our buffer dirty after we write it in the first loop, and
864 * then someone truncates the page away, nobody will ever write
865 * the buffer. We're safe if we write the page one last time
866 * after freeing the journal header.
867 */
868 if (buffer_dirty(bh) && unlikely(bh->b_page->mapping == NULL)) {
869 spin_unlock(lock);
870 ll_rw_block(WRITE, 1, &bh);
871 spin_lock(lock);
872 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700873 put_bh(bh);
874 cond_resched_lock(lock);
875 }
876 spin_unlock(lock);
877 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700879
880static int flush_older_commits(struct super_block *s,
881 struct reiserfs_journal_list *jl)
882{
883 struct reiserfs_journal *journal = SB_JOURNAL(s);
884 struct reiserfs_journal_list *other_jl;
885 struct reiserfs_journal_list *first_jl;
886 struct list_head *entry;
Jeff Mahoney600ed412009-03-30 14:02:17 -0400887 unsigned int trans_id = jl->j_trans_id;
888 unsigned int other_trans_id;
889 unsigned int first_trans_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700890
891 find_first:
892 /*
893 * first we walk backwards to find the oldest uncommitted transation
894 */
895 first_jl = jl;
896 entry = jl->j_list.prev;
897 while (1) {
898 other_jl = JOURNAL_LIST_ENTRY(entry);
899 if (entry == &journal->j_journal_list ||
900 atomic_read(&other_jl->j_older_commits_done))
901 break;
902
903 first_jl = other_jl;
904 entry = other_jl->j_list.prev;
905 }
906
907 /* if we didn't find any older uncommitted transactions, return now */
908 if (first_jl == jl) {
909 return 0;
910 }
911
912 first_trans_id = first_jl->j_trans_id;
913
914 entry = &first_jl->j_list;
915 while (1) {
916 other_jl = JOURNAL_LIST_ENTRY(entry);
917 other_trans_id = other_jl->j_trans_id;
918
919 if (other_trans_id < trans_id) {
920 if (atomic_read(&other_jl->j_commit_left) != 0) {
921 flush_commit_list(s, other_jl, 0);
922
923 /* list we were called with is gone, return */
924 if (!journal_list_still_alive(s, trans_id))
925 return 1;
926
927 /* the one we just flushed is gone, this means all
928 * older lists are also gone, so first_jl is no longer
929 * valid either. Go back to the beginning.
930 */
931 if (!journal_list_still_alive
932 (s, other_trans_id)) {
933 goto find_first;
934 }
935 }
936 entry = entry->next;
937 if (entry == &journal->j_journal_list)
938 return 0;
939 } else {
940 return 0;
941 }
942 }
943 return 0;
944}
Adrian Bunkdeba0f42007-10-16 23:26:03 -0700945
946static int reiserfs_async_progress_wait(struct super_block *s)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700948 struct reiserfs_journal *j = SB_JOURNAL(s);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200949
950 if (atomic_read(&j->j_async_throttle)) {
951 reiserfs_write_unlock(s);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200952 congestion_wait(BLK_RW_ASYNC, HZ / 10);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200953 reiserfs_write_lock(s);
954 }
955
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/*
960** if this journal list still has commit blocks unflushed, send them to disk.
961**
962** log areas must be flushed in order (transaction 2 can't commit before transaction 1)
963** Before the commit block can by written, every other log block must be safely on disk
964**
965*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966static int flush_commit_list(struct super_block *s,
967 struct reiserfs_journal_list *jl, int flushall)
968{
969 int i;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700970 b_blocknr_t bn;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700971 struct buffer_head *tbh = NULL;
Jeff Mahoney600ed412009-03-30 14:02:17 -0400972 unsigned int trans_id = jl->j_trans_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700973 struct reiserfs_journal *journal = SB_JOURNAL(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700974 int retval = 0;
Chris Masone0e851c2006-02-01 03:06:49 -0800975 int write_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700977 reiserfs_check_lock_depth(s, "flush_commit_list");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 if (atomic_read(&jl->j_older_commits_done)) {
980 return 0;
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700983 /* before we can put our commit blocks on disk, we have to make sure everyone older than
984 ** us is on disk too
985 */
986 BUG_ON(jl->j_len <= 0);
987 BUG_ON(trans_id == journal->j_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700989 get_journal_list(jl);
990 if (flushall) {
991 if (flush_older_commits(s, jl) == 1) {
992 /* list disappeared during flush_older_commits. return */
993 goto put_jl;
994 }
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700997 /* make sure nobody is trying to flush this one at the same time */
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200998 reiserfs_mutex_lock_safe(&jl->j_commit_mutex, s);
999
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001000 if (!journal_list_still_alive(s, trans_id)) {
Jeff Mahoney90415de2008-07-25 01:46:40 -07001001 mutex_unlock(&jl->j_commit_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001002 goto put_jl;
1003 }
1004 BUG_ON(jl->j_trans_id == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001006 /* this commit is done, exit */
1007 if (atomic_read(&(jl->j_commit_left)) <= 0) {
1008 if (flushall) {
1009 atomic_set(&(jl->j_older_commits_done), 1);
1010 }
Jeff Mahoney90415de2008-07-25 01:46:40 -07001011 mutex_unlock(&jl->j_commit_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 goto put_jl;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001015 if (!list_empty(&jl->j_bh_list)) {
Chris Mason3d4492f2006-02-01 03:06:49 -08001016 int ret;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001017
1018 /*
1019 * We might sleep in numerous places inside
1020 * write_ordered_buffers. Relax the write lock.
1021 */
1022 reiserfs_write_unlock(s);
Chris Mason3d4492f2006-02-01 03:06:49 -08001023 ret = write_ordered_buffers(&journal->j_dirty_buffers_lock,
1024 journal, jl, &jl->j_bh_list);
1025 if (ret < 0 && retval == 0)
1026 retval = ret;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001027 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001028 }
1029 BUG_ON(!list_empty(&jl->j_bh_list));
1030 /*
1031 * for the description block and all the log blocks, submit any buffers
Chris Masone0e851c2006-02-01 03:06:49 -08001032 * that haven't already reached the disk. Try to write at least 256
1033 * log blocks. later on, we will only wait on blocks that correspond
1034 * to this transaction, but while we're unplugging we might as well
1035 * get a chunk of data on there.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001036 */
1037 atomic_inc(&journal->j_async_throttle);
Chris Masone0e851c2006-02-01 03:06:49 -08001038 write_len = jl->j_len + 1;
1039 if (write_len < 256)
1040 write_len = 256;
1041 for (i = 0 ; i < write_len ; i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001042 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) + (jl->j_start + i) %
1043 SB_ONDISK_JOURNAL_SIZE(s);
1044 tbh = journal_find_get_block(s, bn);
Chris Masone0e851c2006-02-01 03:06:49 -08001045 if (tbh) {
Frederic Weisbecker6e3647a2009-05-01 02:27:39 +02001046 if (buffer_dirty(tbh)) {
1047 reiserfs_write_unlock(s);
1048 ll_rw_block(WRITE, 1, &tbh);
1049 reiserfs_write_lock(s);
1050 }
Chris Masone0e851c2006-02-01 03:06:49 -08001051 put_bh(tbh) ;
1052 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 }
1054 atomic_dec(&journal->j_async_throttle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001056 for (i = 0; i < (jl->j_len + 1); i++) {
1057 bn = SB_ONDISK_JOURNAL_1st_BLOCK(s) +
1058 (jl->j_start + i) % SB_ONDISK_JOURNAL_SIZE(s);
1059 tbh = journal_find_get_block(s, bn);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001060
1061 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062 wait_on_buffer(tbh);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001063 reiserfs_write_lock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001064 // since we're using ll_rw_blk above, it might have skipped over
1065 // a locked buffer. Double check here
1066 //
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001067 /* redundant, sync_dirty_buffer() checks */
1068 if (buffer_dirty(tbh)) {
1069 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001070 sync_dirty_buffer(tbh);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001071 reiserfs_write_lock(s);
1072 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073 if (unlikely(!buffer_uptodate(tbh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001075 reiserfs_warning(s, "journal-601",
1076 "buffer write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001078 retval = -EIO;
1079 }
1080 put_bh(tbh); /* once for journal_find_get_block */
1081 put_bh(tbh); /* once due to original getblk in do_journal_end */
1082 atomic_dec(&(jl->j_commit_left));
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001085 BUG_ON(atomic_read(&(jl->j_commit_left)) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Christoph Hellwig7cd33ad2010-08-18 05:29:14 -04001087 /* If there was a write error in the journal - we can't commit
1088 * this transaction - it will be invalid and, if successful,
1089 * will just end up propagating the write error out to
1090 * the file system. */
1091 if (likely(!retval && !reiserfs_is_journal_aborted (journal))) {
1092 if (buffer_dirty(jl->j_commit_bh))
1093 BUG();
1094 mark_buffer_dirty(jl->j_commit_bh) ;
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001095 reiserfs_write_unlock(s);
Christoph Hellwig7cd33ad2010-08-18 05:29:14 -04001096 if (reiserfs_barrier_flush(s))
1097 __sync_dirty_buffer(jl->j_commit_bh, WRITE_FLUSH_FUA);
1098 else
1099 sync_dirty_buffer(jl->j_commit_bh);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001100 reiserfs_write_lock(s);
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001103 /* If there was a write error in the journal - we can't commit this
1104 * transaction - it will be invalid and, if successful, will just end
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001105 * up propagating the write error out to the filesystem. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001106 if (unlikely(!buffer_uptodate(jl->j_commit_bh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001108 reiserfs_warning(s, "journal-615", "buffer write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001110 retval = -EIO;
1111 }
1112 bforget(jl->j_commit_bh);
1113 if (journal->j_last_commit_id != 0 &&
1114 (jl->j_trans_id - journal->j_last_commit_id) != 1) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001115 reiserfs_warning(s, "clm-2200", "last commit %lu, current %lu",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001116 journal->j_last_commit_id, jl->j_trans_id);
1117 }
1118 journal->j_last_commit_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001120 /* now, every commit block is on the disk. It is safe to allow blocks freed during this transaction to be reallocated */
1121 cleanup_freed_for_journal_list(s, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001123 retval = retval ? retval : journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001125 /* mark the metadata dirty */
1126 if (!retval)
1127 dirty_one_transaction(s, jl);
1128 atomic_dec(&(jl->j_commit_left));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001130 if (flushall) {
1131 atomic_set(&(jl->j_older_commits_done), 1);
1132 }
Jeff Mahoney90415de2008-07-25 01:46:40 -07001133 mutex_unlock(&jl->j_commit_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001134 put_jl:
1135 put_journal_list(s, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001137 if (retval)
1138 reiserfs_abort(s, retval, "Journal write error in %s",
Harvey Harrisonfbe54982008-04-28 02:16:22 -07001139 __func__);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001140 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143/*
Jeff Mahoney0222e652009-03-30 14:02:44 -04001144** flush_journal_list frequently needs to find a newer transaction for a given block. This does that, or
1145** returns NULL if it can't find anything
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001147static struct reiserfs_journal_list *find_newer_jl_for_cn(struct
1148 reiserfs_journal_cnode
1149 *cn)
1150{
1151 struct super_block *sb = cn->sb;
1152 b_blocknr_t blocknr = cn->blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001154 cn = cn->hprev;
1155 while (cn) {
1156 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist) {
1157 return cn->jlist;
1158 }
1159 cn = cn->hprev;
1160 }
1161 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Chris Masona3172022006-09-29 01:59:56 -07001164static int newer_jl_done(struct reiserfs_journal_cnode *cn)
1165{
1166 struct super_block *sb = cn->sb;
1167 b_blocknr_t blocknr = cn->blocknr;
1168
1169 cn = cn->hprev;
1170 while (cn) {
1171 if (cn->sb == sb && cn->blocknr == blocknr && cn->jlist &&
1172 atomic_read(&cn->jlist->j_commit_left) != 0)
1173 return 0;
1174 cn = cn->hprev;
1175 }
1176 return 1;
1177}
1178
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001179static void remove_journal_hash(struct super_block *,
1180 struct reiserfs_journal_cnode **,
1181 struct reiserfs_journal_list *, unsigned long,
1182 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184/*
1185** once all the real blocks have been flushed, it is safe to remove them from the
1186** journal list for this transaction. Aside from freeing the cnode, this also allows the
1187** block to be reallocated for data blocks if it had been deleted.
1188*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001189static void remove_all_from_journal_list(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001190 struct reiserfs_journal_list *jl,
1191 int debug)
1192{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001193 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001194 struct reiserfs_journal_cnode *cn, *last;
1195 cn = jl->j_realblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197 /* which is better, to lock once around the whole loop, or
1198 ** to lock for each call to remove_journal_hash?
1199 */
1200 while (cn) {
1201 if (cn->blocknr != 0) {
1202 if (debug) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001203 reiserfs_warning(sb, "reiserfs-2201",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001204 "block %u, bh is %d, state %ld",
1205 cn->blocknr, cn->bh ? 1 : 0,
1206 cn->state);
1207 }
1208 cn->state = 0;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001209 remove_journal_hash(sb, journal->j_list_hash_table,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001210 jl, cn->blocknr, 1);
1211 }
1212 last = cn;
1213 cn = cn->next;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001214 free_cnode(sb, last);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215 }
1216 jl->j_realblock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219/*
1220** if this timestamp is greater than the timestamp we wrote last to the header block, write it to the header block.
1221** once this is done, I can safely say the log area for this transaction won't ever be replayed, and I can start
1222** releasing blocks in this transaction for reuse as data blocks.
1223** called by flush_journal_list, before it calls remove_all_from_journal_list
1224**
1225*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001226static int _update_journal_header_block(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227 unsigned long offset,
Jeff Mahoney600ed412009-03-30 14:02:17 -04001228 unsigned int trans_id)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001229{
1230 struct reiserfs_journal_header *jh;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001231 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001233 if (reiserfs_is_journal_aborted(journal))
1234 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001236 if (trans_id >= journal->j_last_flush_trans_id) {
1237 if (buffer_locked((journal->j_header_bh))) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001238 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001239 wait_on_buffer((journal->j_header_bh));
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001240 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001241 if (unlikely(!buffer_uptodate(journal->j_header_bh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001243 reiserfs_warning(sb, "journal-699",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001244 "buffer write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 return -EIO;
1247 }
1248 }
1249 journal->j_last_flush_trans_id = trans_id;
1250 journal->j_first_unflushed_offset = offset;
1251 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->
1252 b_data);
1253 jh->j_last_flush_trans_id = cpu_to_le32(trans_id);
1254 jh->j_first_unflushed_offset = cpu_to_le32(offset);
1255 jh->j_mount_id = cpu_to_le32(journal->j_mount_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Christoph Hellwig7cd33ad2010-08-18 05:29:14 -04001257 set_buffer_dirty(journal->j_header_bh);
1258 reiserfs_write_unlock(sb);
1259
1260 if (reiserfs_barrier_flush(sb))
1261 __sync_dirty_buffer(journal->j_header_bh, WRITE_FLUSH_FUA);
1262 else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001263 sync_dirty_buffer(journal->j_header_bh);
Christoph Hellwig7cd33ad2010-08-18 05:29:14 -04001264
1265 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001266 if (!buffer_uptodate(journal->j_header_bh)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001267 reiserfs_warning(sb, "journal-837",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001268 "IO error during journal replay");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001269 return -EIO;
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001275static int update_journal_header_block(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001276 unsigned long offset,
Jeff Mahoney600ed412009-03-30 14:02:17 -04001277 unsigned int trans_id)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001278{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001279 return _update_journal_header_block(sb, offset, trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001281
Jeff Mahoney0222e652009-03-30 14:02:44 -04001282/*
1283** flush any and all journal lists older than you are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284** can only be called from flush_journal_list
1285*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001286static int flush_older_journal_lists(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001289 struct list_head *entry;
1290 struct reiserfs_journal_list *other_jl;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001291 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Jeff Mahoney600ed412009-03-30 14:02:17 -04001292 unsigned int trans_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001294 /* we know we are the only ones flushing things, no extra race
1295 * protection is required.
1296 */
1297 restart:
1298 entry = journal->j_journal_list.next;
1299 /* Did we wrap? */
1300 if (entry == &journal->j_journal_list)
1301 return 0;
1302 other_jl = JOURNAL_LIST_ENTRY(entry);
1303 if (other_jl->j_trans_id < trans_id) {
1304 BUG_ON(other_jl->j_refcount <= 0);
1305 /* do not flush all */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001306 flush_journal_list(sb, other_jl, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001308 /* other_jl is now deleted from the list */
1309 goto restart;
1310 }
1311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314static void del_from_work_list(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 struct reiserfs_journal_list *jl)
1316{
1317 struct reiserfs_journal *journal = SB_JOURNAL(s);
1318 if (!list_empty(&jl->j_working_list)) {
1319 list_del_init(&jl->j_working_list);
1320 journal->j_num_work_lists--;
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
1324/* flush a journal list, both commit and real blocks
1325**
1326** always set flushall to 1, unless you are calling from inside
1327** flush_journal_list
1328**
Jeff Mahoney0222e652009-03-30 14:02:44 -04001329** IMPORTANT. This can only be called while there are no journal writers,
1330** and the journal is locked. That means it can only be called from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331** do_journal_end, or by journal_release
1332*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001333static int flush_journal_list(struct super_block *s,
1334 struct reiserfs_journal_list *jl, int flushall)
1335{
1336 struct reiserfs_journal_list *pjl;
1337 struct reiserfs_journal_cnode *cn, *last;
1338 int count;
1339 int was_jwait = 0;
1340 int was_dirty = 0;
1341 struct buffer_head *saved_bh;
1342 unsigned long j_len_saved = jl->j_len;
1343 struct reiserfs_journal *journal = SB_JOURNAL(s);
1344 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001346 BUG_ON(j_len_saved <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001348 if (atomic_read(&journal->j_wcount) != 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001349 reiserfs_warning(s, "clm-2048", "called with wcount %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001350 atomic_read(&journal->j_wcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001352 BUG_ON(jl->j_trans_id == 0);
1353
1354 /* if flushall == 0, the lock is already held */
1355 if (flushall) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001356 reiserfs_mutex_lock_safe(&journal->j_flush_mutex, s);
Jeff Mahoneyafe70252008-07-25 01:46:39 -07001357 } else if (mutex_trylock(&journal->j_flush_mutex)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001358 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001360
1361 count = 0;
1362 if (j_len_saved > journal->j_trans_max) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001363 reiserfs_panic(s, "journal-715", "length is %lu, trans id %lu",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001364 j_len_saved, jl->j_trans_id);
1365 return 0;
1366 }
1367
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001368 /* if all the work is already done, get out of here */
1369 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1370 atomic_read(&(jl->j_commit_left)) <= 0) {
1371 goto flush_older_and_return;
1372 }
1373
Jeff Mahoney0222e652009-03-30 14:02:44 -04001374 /* start by putting the commit list on disk. This will also flush
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001375 ** the commit lists of any olders transactions
1376 */
1377 flush_commit_list(s, jl, 1);
1378
1379 if (!(jl->j_state & LIST_DIRTY)
1380 && !reiserfs_is_journal_aborted(journal))
1381 BUG();
1382
1383 /* are we done now? */
1384 if (atomic_read(&(jl->j_nonzerolen)) <= 0 &&
1385 atomic_read(&(jl->j_commit_left)) <= 0) {
1386 goto flush_older_and_return;
1387 }
1388
Jeff Mahoney0222e652009-03-30 14:02:44 -04001389 /* loop through each cnode, see if we need to write it,
1390 ** or wait on a more recent transaction, or just ignore it
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001391 */
1392 if (atomic_read(&(journal->j_wcount)) != 0) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001393 reiserfs_panic(s, "journal-844", "journal list is flushing, "
1394 "wcount is not 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001395 }
1396 cn = jl->j_realblock;
1397 while (cn) {
1398 was_jwait = 0;
1399 was_dirty = 0;
1400 saved_bh = NULL;
1401 /* blocknr of 0 is no longer in the hash, ignore it */
1402 if (cn->blocknr == 0) {
1403 goto free_cnode;
1404 }
1405
1406 /* This transaction failed commit. Don't write out to the disk */
1407 if (!(jl->j_state & LIST_DIRTY))
1408 goto free_cnode;
1409
1410 pjl = find_newer_jl_for_cn(cn);
1411 /* the order is important here. We check pjl to make sure we
1412 ** don't clear BH_JDirty_wait if we aren't the one writing this
1413 ** block to disk
1414 */
1415 if (!pjl && cn->bh) {
1416 saved_bh = cn->bh;
1417
Jeff Mahoney0222e652009-03-30 14:02:44 -04001418 /* we do this to make sure nobody releases the buffer while
1419 ** we are working with it
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001420 */
1421 get_bh(saved_bh);
1422
1423 if (buffer_journal_dirty(saved_bh)) {
1424 BUG_ON(!can_dirty(cn));
1425 was_jwait = 1;
1426 was_dirty = 1;
1427 } else if (can_dirty(cn)) {
1428 /* everything with !pjl && jwait should be writable */
1429 BUG();
1430 }
1431 }
1432
1433 /* if someone has this block in a newer transaction, just make
Matt LaPlante0779bf22006-11-30 05:24:39 +01001434 ** sure they are committed, and don't try writing it to disk
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001435 */
1436 if (pjl) {
1437 if (atomic_read(&pjl->j_commit_left))
1438 flush_commit_list(s, pjl, 1);
1439 goto free_cnode;
1440 }
1441
Jeff Mahoney0222e652009-03-30 14:02:44 -04001442 /* bh == NULL when the block got to disk on its own, OR,
1443 ** the block got freed in a future transaction
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001444 */
1445 if (saved_bh == NULL) {
1446 goto free_cnode;
1447 }
1448
1449 /* this should never happen. kupdate_one_transaction has this list
1450 ** locked while it works, so we should never see a buffer here that
1451 ** is not marked JDirty_wait
1452 */
1453 if ((!was_jwait) && !buffer_locked(saved_bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001454 reiserfs_warning(s, "journal-813",
1455 "BAD! buffer %llu %cdirty %cjwait, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001456 "not in a newer tranasction",
1457 (unsigned long long)saved_bh->
1458 b_blocknr, was_dirty ? ' ' : '!',
1459 was_jwait ? ' ' : '!');
1460 }
1461 if (was_dirty) {
1462 /* we inc again because saved_bh gets decremented at free_cnode */
1463 get_bh(saved_bh);
1464 set_bit(BLOCK_NEEDS_FLUSH, &cn->state);
1465 lock_buffer(saved_bh);
1466 BUG_ON(cn->blocknr != saved_bh->b_blocknr);
1467 if (buffer_dirty(saved_bh))
1468 submit_logged_buffer(saved_bh);
1469 else
1470 unlock_buffer(saved_bh);
1471 count++;
1472 } else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001473 reiserfs_warning(s, "clm-2082",
1474 "Unable to flush buffer %llu in %s",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001475 (unsigned long long)saved_bh->
Harvey Harrisonfbe54982008-04-28 02:16:22 -07001476 b_blocknr, __func__);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001477 }
1478 free_cnode:
1479 last = cn;
1480 cn = cn->next;
1481 if (saved_bh) {
1482 /* we incremented this to keep others from taking the buffer head away */
1483 put_bh(saved_bh);
1484 if (atomic_read(&(saved_bh->b_count)) < 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001485 reiserfs_warning(s, "journal-945",
1486 "saved_bh->b_count < 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001487 }
1488 }
1489 }
1490 if (count > 0) {
1491 cn = jl->j_realblock;
1492 while (cn) {
1493 if (test_bit(BLOCK_NEEDS_FLUSH, &cn->state)) {
1494 if (!cn->bh) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001495 reiserfs_panic(s, "journal-1011",
1496 "cn->bh is NULL");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001497 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001498
1499 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001500 wait_on_buffer(cn->bh);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001501 reiserfs_write_lock(s);
1502
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001503 if (!cn->bh) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001504 reiserfs_panic(s, "journal-1012",
1505 "cn->bh is NULL");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001506 }
1507 if (unlikely(!buffer_uptodate(cn->bh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001509 reiserfs_warning(s, "journal-949",
1510 "buffer write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001512 err = -EIO;
1513 }
1514 /* note, we must clear the JDirty_wait bit after the up to date
1515 ** check, otherwise we race against our flushpage routine
1516 */
1517 BUG_ON(!test_clear_buffer_journal_dirty
1518 (cn->bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Chris Mason398c95b2007-10-16 23:29:44 -07001520 /* drop one ref for us */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001521 put_bh(cn->bh);
Chris Mason398c95b2007-10-16 23:29:44 -07001522 /* drop one ref for journal_mark_dirty */
1523 release_buffer_page(cn->bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001524 }
1525 cn = cn->next;
1526 }
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529 if (err)
1530 reiserfs_abort(s, -EIO,
1531 "Write error while pushing transaction to disk in %s",
Harvey Harrisonfbe54982008-04-28 02:16:22 -07001532 __func__);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001533 flush_older_and_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Jeff Mahoney0222e652009-03-30 14:02:44 -04001535 /* before we can update the journal header block, we _must_ flush all
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001536 ** real blocks from all older transactions to disk. This is because
1537 ** once the header block is updated, this transaction will not be
1538 ** replayed after a crash
1539 */
1540 if (flushall) {
1541 flush_older_journal_lists(s, jl);
1542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001544 err = journal->j_errno;
Jeff Mahoney0222e652009-03-30 14:02:44 -04001545 /* before we can remove everything from the hash tables for this
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001546 ** transaction, we must make sure it can never be replayed
1547 **
1548 ** since we are only called from do_journal_end, we know for sure there
1549 ** are no allocations going on while we are flushing journal lists. So,
1550 ** we only need to update the journal header block for the last list
1551 ** being flushed
1552 */
1553 if (!err && flushall) {
1554 err =
1555 update_journal_header_block(s,
1556 (jl->j_start + jl->j_len +
1557 2) % SB_ONDISK_JOURNAL_SIZE(s),
1558 jl->j_trans_id);
1559 if (err)
1560 reiserfs_abort(s, -EIO,
1561 "Write error while updating journal header in %s",
Harvey Harrisonfbe54982008-04-28 02:16:22 -07001562 __func__);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001563 }
1564 remove_all_from_journal_list(s, jl, 0);
1565 list_del_init(&jl->j_list);
1566 journal->j_num_lists--;
1567 del_from_work_list(s, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001569 if (journal->j_last_flush_id != 0 &&
1570 (jl->j_trans_id - journal->j_last_flush_id) != 1) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001571 reiserfs_warning(s, "clm-2201", "last flush %lu, current %lu",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001572 journal->j_last_flush_id, jl->j_trans_id);
1573 }
1574 journal->j_last_flush_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001576 /* not strictly required since we are freeing the list, but it should
1577 * help find code using dead lists later on
1578 */
1579 jl->j_len = 0;
1580 atomic_set(&(jl->j_nonzerolen), 0);
1581 jl->j_start = 0;
1582 jl->j_realblock = NULL;
1583 jl->j_commit_bh = NULL;
1584 jl->j_trans_id = 0;
1585 jl->j_state = 0;
1586 put_journal_list(s, jl);
1587 if (flushall)
Jeff Mahoneyafe70252008-07-25 01:46:39 -07001588 mutex_unlock(&journal->j_flush_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001589 return err;
1590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Chris Masona3172022006-09-29 01:59:56 -07001592static int test_transaction(struct super_block *s,
1593 struct reiserfs_journal_list *jl)
1594{
1595 struct reiserfs_journal_cnode *cn;
1596
1597 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0)
1598 return 1;
1599
1600 cn = jl->j_realblock;
1601 while (cn) {
1602 /* if the blocknr == 0, this has been cleared from the hash,
1603 ** skip it
1604 */
1605 if (cn->blocknr == 0) {
1606 goto next;
1607 }
1608 if (cn->bh && !newer_jl_done(cn))
1609 return 0;
1610 next:
1611 cn = cn->next;
1612 cond_resched();
1613 }
1614 return 0;
1615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617static int write_one_transaction(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001618 struct reiserfs_journal_list *jl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct buffer_chunk *chunk)
1620{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001621 struct reiserfs_journal_cnode *cn;
1622 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001624 jl->j_state |= LIST_TOUCHED;
1625 del_from_work_list(s, jl);
1626 if (jl->j_len == 0 || atomic_read(&jl->j_nonzerolen) == 0) {
1627 return 0;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001630 cn = jl->j_realblock;
1631 while (cn) {
1632 /* if the blocknr == 0, this has been cleared from the hash,
1633 ** skip it
1634 */
1635 if (cn->blocknr == 0) {
1636 goto next;
1637 }
1638 if (cn->bh && can_dirty(cn) && buffer_dirty(cn->bh)) {
1639 struct buffer_head *tmp_bh;
1640 /* we can race against journal_mark_freed when we try
1641 * to lock_buffer(cn->bh), so we have to inc the buffer
1642 * count, and recheck things after locking
1643 */
1644 tmp_bh = cn->bh;
1645 get_bh(tmp_bh);
1646 lock_buffer(tmp_bh);
1647 if (cn->bh && can_dirty(cn) && buffer_dirty(tmp_bh)) {
1648 if (!buffer_journal_dirty(tmp_bh) ||
1649 buffer_journal_prepared(tmp_bh))
1650 BUG();
1651 add_to_chunk(chunk, tmp_bh, NULL, write_chunk);
1652 ret++;
1653 } else {
1654 /* note, cn->bh might be null now */
1655 unlock_buffer(tmp_bh);
1656 }
1657 put_bh(tmp_bh);
1658 }
1659 next:
1660 cn = cn->next;
1661 cond_resched();
1662 }
1663 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666/* used by flush_commit_list */
1667static int dirty_one_transaction(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001668 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001670 struct reiserfs_journal_cnode *cn;
1671 struct reiserfs_journal_list *pjl;
1672 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001674 jl->j_state |= LIST_DIRTY;
1675 cn = jl->j_realblock;
1676 while (cn) {
1677 /* look for a more recent transaction that logged this
1678 ** buffer. Only the most recent transaction with a buffer in
1679 ** it is allowed to send that buffer to disk
1680 */
1681 pjl = find_newer_jl_for_cn(cn);
1682 if (!pjl && cn->blocknr && cn->bh
1683 && buffer_journal_dirty(cn->bh)) {
1684 BUG_ON(!can_dirty(cn));
1685 /* if the buffer is prepared, it will either be logged
1686 * or restored. If restored, we need to make sure
1687 * it actually gets marked dirty
1688 */
1689 clear_buffer_journal_new(cn->bh);
1690 if (buffer_journal_prepared(cn->bh)) {
1691 set_buffer_journal_restore_dirty(cn->bh);
1692 } else {
1693 set_buffer_journal_test(cn->bh);
1694 mark_buffer_dirty(cn->bh);
1695 }
1696 }
1697 cn = cn->next;
1698 }
1699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
1702static int kupdate_transactions(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001703 struct reiserfs_journal_list *jl,
1704 struct reiserfs_journal_list **next_jl,
Jeff Mahoney600ed412009-03-30 14:02:17 -04001705 unsigned int *next_trans_id,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001706 int num_blocks, int num_trans)
1707{
1708 int ret = 0;
1709 int written = 0;
1710 int transactions_flushed = 0;
Jeff Mahoney600ed412009-03-30 14:02:17 -04001711 unsigned int orig_trans_id = jl->j_trans_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001712 struct buffer_chunk chunk;
1713 struct list_head *entry;
1714 struct reiserfs_journal *journal = SB_JOURNAL(s);
1715 chunk.nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Frederic Weisbeckera412f9e2009-04-14 00:10:35 +02001717 reiserfs_mutex_lock_safe(&journal->j_flush_mutex, s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001718 if (!journal_list_still_alive(s, orig_trans_id)) {
1719 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Jeff Mahoneyafe70252008-07-25 01:46:39 -07001722 /* we've got j_flush_mutex held, nobody is going to delete any
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001723 * of these lists out from underneath us
1724 */
1725 while ((num_trans && transactions_flushed < num_trans) ||
1726 (!num_trans && written < num_blocks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001728 if (jl->j_len == 0 || (jl->j_state & LIST_TOUCHED) ||
1729 atomic_read(&jl->j_commit_left)
1730 || !(jl->j_state & LIST_DIRTY)) {
1731 del_from_work_list(s, jl);
1732 break;
1733 }
1734 ret = write_one_transaction(s, jl, &chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001736 if (ret < 0)
1737 goto done;
1738 transactions_flushed++;
1739 written += ret;
1740 entry = jl->j_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001742 /* did we wrap? */
1743 if (entry == &journal->j_journal_list) {
1744 break;
1745 }
1746 jl = JOURNAL_LIST_ENTRY(entry);
1747
1748 /* don't bother with older transactions */
1749 if (jl->j_trans_id <= orig_trans_id)
1750 break;
1751 }
1752 if (chunk.nr) {
1753 write_chunk(&chunk);
1754 }
1755
1756 done:
Jeff Mahoneyafe70252008-07-25 01:46:39 -07001757 mutex_unlock(&journal->j_flush_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001758 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
1761/* for o_sync and fsync heavy applications, they tend to use
1762** all the journa list slots with tiny transactions. These
1763** trigger lots and lots of calls to update the header block, which
1764** adds seeks and slows things down.
1765**
1766** This function tries to clear out a large chunk of the journal lists
1767** at once, which makes everything faster since only the newest journal
1768** list updates the header block
1769*/
1770static int flush_used_journal_lists(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001771 struct reiserfs_journal_list *jl)
1772{
1773 unsigned long len = 0;
1774 unsigned long cur_len;
1775 int ret;
1776 int i;
1777 int limit = 256;
1778 struct reiserfs_journal_list *tjl;
1779 struct reiserfs_journal_list *flush_jl;
Jeff Mahoney600ed412009-03-30 14:02:17 -04001780 unsigned int trans_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001781 struct reiserfs_journal *journal = SB_JOURNAL(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001783 flush_jl = tjl = jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001785 /* in data logging mode, try harder to flush a lot of blocks */
1786 if (reiserfs_data_log(s))
1787 limit = 1024;
1788 /* flush for 256 transactions or limit blocks, whichever comes first */
1789 for (i = 0; i < 256 && len < limit; i++) {
1790 if (atomic_read(&tjl->j_commit_left) ||
1791 tjl->j_trans_id < jl->j_trans_id) {
1792 break;
1793 }
1794 cur_len = atomic_read(&tjl->j_nonzerolen);
1795 if (cur_len > 0) {
1796 tjl->j_state &= ~LIST_TOUCHED;
1797 }
1798 len += cur_len;
1799 flush_jl = tjl;
1800 if (tjl->j_list.next == &journal->j_journal_list)
1801 break;
1802 tjl = JOURNAL_LIST_ENTRY(tjl->j_list.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001804 /* try to find a group of blocks we can flush across all the
1805 ** transactions, but only bother if we've actually spanned
1806 ** across multiple lists
1807 */
1808 if (flush_jl != jl) {
1809 ret = kupdate_transactions(s, jl, &tjl, &trans_id, len, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001811 flush_journal_list(s, flush_jl, 1);
1812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815/*
1816** removes any nodes in table with name block and dev as bh.
1817** only touchs the hnext and hprev pointers.
1818*/
1819void remove_journal_hash(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001820 struct reiserfs_journal_cnode **table,
1821 struct reiserfs_journal_list *jl,
1822 unsigned long block, int remove_freed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001824 struct reiserfs_journal_cnode *cur;
1825 struct reiserfs_journal_cnode **head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001827 head = &(journal_hash(table, sb, block));
1828 if (!head) {
1829 return;
1830 }
1831 cur = *head;
1832 while (cur) {
1833 if (cur->blocknr == block && cur->sb == sb
1834 && (jl == NULL || jl == cur->jlist)
1835 && (!test_bit(BLOCK_FREED, &cur->state) || remove_freed)) {
1836 if (cur->hnext) {
1837 cur->hnext->hprev = cur->hprev;
1838 }
1839 if (cur->hprev) {
1840 cur->hprev->hnext = cur->hnext;
1841 } else {
1842 *head = cur->hnext;
1843 }
1844 cur->blocknr = 0;
1845 cur->sb = NULL;
1846 cur->state = 0;
1847 if (cur->bh && cur->jlist) /* anybody who clears the cur->bh will also dec the nonzerolen */
1848 atomic_dec(&(cur->jlist->j_nonzerolen));
1849 cur->bh = NULL;
1850 cur->jlist = NULL;
1851 }
1852 cur = cur->hnext;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001856static void free_journal_ram(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001857{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001858 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Pekka Enbergd739b422006-02-01 03:06:43 -08001859 kfree(journal->j_current_jl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001860 journal->j_num_lists--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001862 vfree(journal->j_cnode_free_orig);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001863 free_list_bitmaps(sb, journal->j_list_bitmap);
1864 free_bitmap_nodes(sb); /* must be after free_list_bitmaps */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001865 if (journal->j_header_bh) {
1866 brelse(journal->j_header_bh);
1867 }
1868 /* j_header_bh is on the journal dev, make sure not to release the journal
1869 * dev until we brelse j_header_bh
1870 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001871 release_journal_dev(sb, journal);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001872 vfree(journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875/*
1876** call on unmount. Only set error to 1 if you haven't made your way out
1877** of read_super() yet. Any other caller must keep error at 0.
1878*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001879static int do_journal_release(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001880 struct super_block *sb, int error)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001881{
1882 struct reiserfs_transaction_handle myth;
1883 int flushed = 0;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001884 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001886 /* we only want to flush out transactions if we were called with error == 0
1887 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001888 if (!error && !(sb->s_flags & MS_RDONLY)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001889 /* end the current trans */
1890 BUG_ON(!th->t_trans_id);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001891 do_journal_end(th, sb, 10, FLUSH_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001893 /* make sure something gets logged to force our way into the flush code */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001894 if (!journal_join(&myth, sb, 1)) {
1895 reiserfs_prepare_for_journal(sb,
1896 SB_BUFFER_WITH_SB(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001897 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001898 journal_mark_dirty(&myth, sb,
1899 SB_BUFFER_WITH_SB(sb));
1900 do_journal_end(&myth, sb, 1, FLUSH_ALL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001901 flushed = 1;
1902 }
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001905 /* this also catches errors during the do_journal_end above */
1906 if (!error && reiserfs_is_journal_aborted(journal)) {
1907 memset(&myth, 0, sizeof(myth));
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001908 if (!journal_join_abort(&myth, sb, 1)) {
1909 reiserfs_prepare_for_journal(sb,
1910 SB_BUFFER_WITH_SB(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001911 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001912 journal_mark_dirty(&myth, sb,
1913 SB_BUFFER_WITH_SB(sb));
1914 do_journal_end(&myth, sb, 1, FLUSH_ALL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001915 }
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001918 reiserfs_mounted_fs_count--;
1919 /* wait for all commits to finish */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001920 cancel_delayed_work(&SB_JOURNAL(sb)->j_work);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001921
1922 /*
1923 * We must release the write lock here because
1924 * the workqueue job (flush_async_commit) needs this lock
1925 */
1926 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001927 flush_workqueue(commit_wq);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02001928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001929 if (!reiserfs_mounted_fs_count) {
1930 destroy_workqueue(commit_wq);
1931 commit_wq = NULL;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001934 free_journal_ram(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Frederic Weisbecker05236762009-12-30 05:56:08 +01001936 reiserfs_write_lock(sb);
1937
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941/*
1942** call on unmount. flush all journal trans, release all alloc'd ram
1943*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001944int journal_release(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001945 struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001946{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001947 return do_journal_release(th, sb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950/*
1951** only call from an error condition inside reiserfs_read_super!
1952*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001953int journal_release_error(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001954 struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001955{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001956 return do_journal_release(th, sb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
1959/* compares description block with commit block. returns 1 if they differ, 0 if they are the same */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001960static int journal_compare_desc_commit(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001961 struct reiserfs_journal_desc *desc,
1962 struct reiserfs_journal_commit *commit)
1963{
1964 if (get_commit_trans_id(commit) != get_desc_trans_id(desc) ||
1965 get_commit_trans_len(commit) != get_desc_trans_len(desc) ||
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001966 get_commit_trans_len(commit) > SB_JOURNAL(sb)->j_trans_max ||
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001967 get_commit_trans_len(commit) <= 0) {
1968 return 1;
1969 }
1970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001972
Jeff Mahoney0222e652009-03-30 14:02:44 -04001973/* returns 0 if it did not find a description block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974** returns -1 if it found a corrupt commit block
Jeff Mahoney0222e652009-03-30 14:02:44 -04001975** returns 1 if both desc and commit were valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001977static int journal_transaction_is_valid(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001978 struct buffer_head *d_bh,
Jeff Mahoney600ed412009-03-30 14:02:17 -04001979 unsigned int *oldest_invalid_trans_id,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001980 unsigned long *newest_mount_id)
1981{
1982 struct reiserfs_journal_desc *desc;
1983 struct reiserfs_journal_commit *commit;
1984 struct buffer_head *c_bh;
1985 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001987 if (!d_bh)
1988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001990 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
1991 if (get_desc_trans_len(desc) > 0
1992 && !memcmp(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8)) {
1993 if (oldest_invalid_trans_id && *oldest_invalid_trans_id
1994 && get_desc_trans_id(desc) > *oldest_invalid_trans_id) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001995 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001996 "journal-986: transaction "
1997 "is valid returning because trans_id %d is greater than "
1998 "oldest_invalid %lu",
1999 get_desc_trans_id(desc),
2000 *oldest_invalid_trans_id);
2001 return 0;
2002 }
2003 if (newest_mount_id
2004 && *newest_mount_id > get_desc_mount_id(desc)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002005 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002006 "journal-1087: transaction "
2007 "is valid returning because mount_id %d is less than "
2008 "newest_mount_id %lu",
2009 get_desc_mount_id(desc),
2010 *newest_mount_id);
2011 return -1;
2012 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002013 if (get_desc_trans_len(desc) > SB_JOURNAL(sb)->j_trans_max) {
2014 reiserfs_warning(sb, "journal-2018",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002015 "Bad transaction length %d "
2016 "encountered, ignoring transaction",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002017 get_desc_trans_len(desc));
2018 return -1;
2019 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002020 offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002022 /* ok, we have a journal description block, lets see if the transaction was valid */
2023 c_bh =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002024 journal_bread(sb,
2025 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002026 ((offset + get_desc_trans_len(desc) +
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002027 1) % SB_ONDISK_JOURNAL_SIZE(sb)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002028 if (!c_bh)
2029 return 0;
2030 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002031 if (journal_compare_desc_commit(sb, desc, commit)) {
2032 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002033 "journal_transaction_is_valid, commit offset %ld had bad "
2034 "time %d or length %d",
2035 c_bh->b_blocknr -
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002036 SB_ONDISK_JOURNAL_1st_BLOCK(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002037 get_commit_trans_id(commit),
2038 get_commit_trans_len(commit));
2039 brelse(c_bh);
2040 if (oldest_invalid_trans_id) {
2041 *oldest_invalid_trans_id =
2042 get_desc_trans_id(desc);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002043 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002044 "journal-1004: "
2045 "transaction_is_valid setting oldest invalid trans_id "
2046 "to %d",
2047 get_desc_trans_id(desc));
2048 }
2049 return -1;
2050 }
2051 brelse(c_bh);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002052 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002053 "journal-1006: found valid "
2054 "transaction start offset %llu, len %d id %d",
2055 d_bh->b_blocknr -
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002056 SB_ONDISK_JOURNAL_1st_BLOCK(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002057 get_desc_trans_len(desc),
2058 get_desc_trans_id(desc));
2059 return 1;
2060 } else {
2061 return 0;
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002065static void brelse_array(struct buffer_head **heads, int num)
2066{
2067 int i;
2068 for (i = 0; i < num; i++) {
2069 brelse(heads[i]);
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
2073/*
2074** given the start, and values for the oldest acceptable transactions,
2075** this either reads in a replays a transaction, or returns because the transaction
2076** is invalid, or too old.
2077*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002078static int journal_read_transaction(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002079 unsigned long cur_dblock,
2080 unsigned long oldest_start,
Jeff Mahoney600ed412009-03-30 14:02:17 -04002081 unsigned int oldest_trans_id,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002082 unsigned long newest_mount_id)
2083{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002084 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002085 struct reiserfs_journal_desc *desc;
2086 struct reiserfs_journal_commit *commit;
Jeff Mahoney600ed412009-03-30 14:02:17 -04002087 unsigned int trans_id = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002088 struct buffer_head *c_bh;
2089 struct buffer_head *d_bh;
2090 struct buffer_head **log_blocks = NULL;
2091 struct buffer_head **real_blocks = NULL;
Jeff Mahoney600ed412009-03-30 14:02:17 -04002092 unsigned int trans_offset;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002093 int i;
2094 int trans_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002096 d_bh = journal_bread(sb, cur_dblock);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002097 if (!d_bh)
2098 return 1;
2099 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002100 trans_offset = d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb);
2101 reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1037: "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002102 "journal_read_transaction, offset %llu, len %d mount_id %d",
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002103 d_bh->b_blocknr - SB_ONDISK_JOURNAL_1st_BLOCK(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002104 get_desc_trans_len(desc), get_desc_mount_id(desc));
2105 if (get_desc_trans_id(desc) < oldest_trans_id) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002106 reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1039: "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002107 "journal_read_trans skipping because %lu is too old",
2108 cur_dblock -
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002109 SB_ONDISK_JOURNAL_1st_BLOCK(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002110 brelse(d_bh);
2111 return 1;
2112 }
2113 if (get_desc_mount_id(desc) != newest_mount_id) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002114 reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1146: "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002115 "journal_read_trans skipping because %d is != "
2116 "newest_mount_id %lu", get_desc_mount_id(desc),
2117 newest_mount_id);
2118 brelse(d_bh);
2119 return 1;
2120 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002121 c_bh = journal_bread(sb, SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002122 ((trans_offset + get_desc_trans_len(desc) + 1) %
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002123 SB_ONDISK_JOURNAL_SIZE(sb)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002124 if (!c_bh) {
2125 brelse(d_bh);
2126 return 1;
2127 }
2128 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002129 if (journal_compare_desc_commit(sb, desc, commit)) {
2130 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002131 "journal_read_transaction, "
2132 "commit offset %llu had bad time %d or length %d",
2133 c_bh->b_blocknr -
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002134 SB_ONDISK_JOURNAL_1st_BLOCK(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002135 get_commit_trans_id(commit),
2136 get_commit_trans_len(commit));
2137 brelse(c_bh);
2138 brelse(d_bh);
2139 return 1;
2140 }
Jeff Mahoney3f8b5ee2010-03-23 13:35:39 -07002141
2142 if (bdev_read_only(sb->s_bdev)) {
2143 reiserfs_warning(sb, "clm-2076",
2144 "device is readonly, unable to replay log");
2145 brelse(c_bh);
2146 brelse(d_bh);
2147 return -EROFS;
2148 }
2149
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002150 trans_id = get_desc_trans_id(desc);
2151 /* now we know we've got a good transaction, and it was inside the valid time ranges */
Pekka Enbergd739b422006-02-01 03:06:43 -08002152 log_blocks = kmalloc(get_desc_trans_len(desc) *
2153 sizeof(struct buffer_head *), GFP_NOFS);
2154 real_blocks = kmalloc(get_desc_trans_len(desc) *
2155 sizeof(struct buffer_head *), GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002156 if (!log_blocks || !real_blocks) {
2157 brelse(c_bh);
2158 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002159 kfree(log_blocks);
2160 kfree(real_blocks);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002161 reiserfs_warning(sb, "journal-1169",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002162 "kmalloc failed, unable to mount FS");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002163 return -1;
2164 }
2165 /* get all the buffer heads */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002166 trans_half = journal_trans_half(sb->s_blocksize);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002167 for (i = 0; i < get_desc_trans_len(desc); i++) {
2168 log_blocks[i] =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002169 journal_getblk(sb,
2170 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002171 (trans_offset + 1 +
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002172 i) % SB_ONDISK_JOURNAL_SIZE(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002173 if (i < trans_half) {
2174 real_blocks[i] =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002175 sb_getblk(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002176 le32_to_cpu(desc->j_realblock[i]));
2177 } else {
2178 real_blocks[i] =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002179 sb_getblk(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002180 le32_to_cpu(commit->
2181 j_realblock[i - trans_half]));
2182 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002183 if (real_blocks[i]->b_blocknr > SB_BLOCK_COUNT(sb)) {
2184 reiserfs_warning(sb, "journal-1207",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002185 "REPLAY FAILURE fsck required! "
2186 "Block to replay is outside of "
2187 "filesystem");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002188 goto abort_replay;
2189 }
2190 /* make sure we don't try to replay onto log or reserved area */
2191 if (is_block_in_log_or_reserved_area
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002192 (sb, real_blocks[i]->b_blocknr)) {
2193 reiserfs_warning(sb, "journal-1204",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002194 "REPLAY FAILURE fsck required! "
2195 "Trying to replay onto a log block");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002196 abort_replay:
2197 brelse_array(log_blocks, i);
2198 brelse_array(real_blocks, i);
2199 brelse(c_bh);
2200 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002201 kfree(log_blocks);
2202 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002203 return -1;
2204 }
2205 }
2206 /* read in the log blocks, memcpy to the corresponding real block */
2207 ll_rw_block(READ, get_desc_trans_len(desc), log_blocks);
2208 for (i = 0; i < get_desc_trans_len(desc); i++) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002209
2210 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002211 wait_on_buffer(log_blocks[i]);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002212 reiserfs_write_lock(sb);
2213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002214 if (!buffer_uptodate(log_blocks[i])) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002215 reiserfs_warning(sb, "journal-1212",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002216 "REPLAY FAILURE fsck required! "
2217 "buffer write failed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002218 brelse_array(log_blocks + i,
2219 get_desc_trans_len(desc) - i);
2220 brelse_array(real_blocks, get_desc_trans_len(desc));
2221 brelse(c_bh);
2222 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002223 kfree(log_blocks);
2224 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002225 return -1;
2226 }
2227 memcpy(real_blocks[i]->b_data, log_blocks[i]->b_data,
2228 real_blocks[i]->b_size);
2229 set_buffer_uptodate(real_blocks[i]);
2230 brelse(log_blocks[i]);
2231 }
2232 /* flush out the real blocks */
2233 for (i = 0; i < get_desc_trans_len(desc); i++) {
2234 set_buffer_dirty(real_blocks[i]);
Christoph Hellwig9cb569d2010-08-11 17:06:24 +02002235 write_dirty_buffer(real_blocks[i], WRITE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002236 }
2237 for (i = 0; i < get_desc_trans_len(desc); i++) {
2238 wait_on_buffer(real_blocks[i]);
2239 if (!buffer_uptodate(real_blocks[i])) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002240 reiserfs_warning(sb, "journal-1226",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002241 "REPLAY FAILURE, fsck required! "
2242 "buffer write failed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002243 brelse_array(real_blocks + i,
2244 get_desc_trans_len(desc) - i);
2245 brelse(c_bh);
2246 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002247 kfree(log_blocks);
2248 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002249 return -1;
2250 }
2251 brelse(real_blocks[i]);
2252 }
2253 cur_dblock =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002254 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002255 ((trans_offset + get_desc_trans_len(desc) +
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002256 2) % SB_ONDISK_JOURNAL_SIZE(sb));
2257 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002258 "journal-1095: setting journal " "start to offset %ld",
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002259 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002260
2261 /* init starting values for the first transaction, in case this is the last transaction to be replayed. */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002262 journal->j_start = cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002263 journal->j_last_flush_trans_id = trans_id;
2264 journal->j_trans_id = trans_id + 1;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08002265 /* check for trans_id overflow */
2266 if (journal->j_trans_id == 0)
2267 journal->j_trans_id = 10;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002268 brelse(c_bh);
2269 brelse(d_bh);
Pekka Enbergd739b422006-02-01 03:06:43 -08002270 kfree(log_blocks);
2271 kfree(real_blocks);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
2275/* This function reads blocks starting from block and to max_block of bufsize
2276 size (but no more than BUFNR blocks at a time). This proved to improve
2277 mounting speed on self-rebuilding raid5 arrays at least.
2278 Right now it is only used from journal code. But later we might use it
2279 from other places.
2280 Note: Do not use journal_getblk/sb_getblk functions here! */
Jeff Mahoney3ee16672007-10-18 23:39:25 -07002281static struct buffer_head *reiserfs_breada(struct block_device *dev,
2282 b_blocknr_t block, int bufsize,
2283 b_blocknr_t max_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002285 struct buffer_head *bhlist[BUFNR];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 unsigned int blocks = BUFNR;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002287 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 int i, j;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002289
2290 bh = __getblk(dev, block, bufsize);
2291 if (buffer_uptodate(bh))
2292 return (bh);
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (block + BUFNR > max_block) {
2295 blocks = max_block - block;
2296 }
2297 bhlist[0] = bh;
2298 j = 1;
2299 for (i = 1; i < blocks; i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002300 bh = __getblk(dev, block + i, bufsize);
2301 if (buffer_uptodate(bh)) {
2302 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002304 } else
2305 bhlist[j++] = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002307 ll_rw_block(READ, j, bhlist);
2308 for (i = 1; i < j; i++)
2309 brelse(bhlist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 bh = bhlist[0];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002311 wait_on_buffer(bh);
2312 if (buffer_uptodate(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return bh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002314 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return NULL;
2316}
2317
2318/*
2319** read and replay the log
2320** on a clean unmount, the journal header's next unflushed pointer will be to an invalid
2321** transaction. This tests that before finding all the transactions in the log, which makes normal mount times fast.
2322**
2323** After a crash, this starts with the next unflushed transaction, and replays until it finds one too old, or invalid.
2324**
2325** On exit, it sets things up so the first transaction will work correctly.
2326*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002327static int journal_read(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002328{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002329 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002330 struct reiserfs_journal_desc *desc;
Jeff Mahoney600ed412009-03-30 14:02:17 -04002331 unsigned int oldest_trans_id = 0;
2332 unsigned int oldest_invalid_trans_id = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002333 time_t start;
2334 unsigned long oldest_start = 0;
2335 unsigned long cur_dblock = 0;
2336 unsigned long newest_mount_id = 9;
2337 struct buffer_head *d_bh;
2338 struct reiserfs_journal_header *jh;
2339 int valid_journal_header = 0;
2340 int replay_count = 0;
2341 int continue_replay = 1;
2342 int ret;
2343 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002345 cur_dblock = SB_ONDISK_JOURNAL_1st_BLOCK(sb);
2346 reiserfs_info(sb, "checking transaction log (%s)\n",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002347 bdevname(journal->j_dev_bd, b));
2348 start = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Jeff Mahoney0222e652009-03-30 14:02:44 -04002350 /* step 1, read in the journal header block. Check the transaction it says
2351 ** is the first unflushed, and if that transaction is not valid,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002352 ** replay is done
2353 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002354 journal->j_header_bh = journal_bread(sb,
2355 SB_ONDISK_JOURNAL_1st_BLOCK(sb)
2356 + SB_ONDISK_JOURNAL_SIZE(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002357 if (!journal->j_header_bh) {
2358 return 1;
2359 }
2360 jh = (struct reiserfs_journal_header *)(journal->j_header_bh->b_data);
Vladimir V. Savelievc499ec22006-03-02 02:54:39 -08002361 if (le32_to_cpu(jh->j_first_unflushed_offset) <
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002362 SB_ONDISK_JOURNAL_SIZE(sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002363 && le32_to_cpu(jh->j_last_flush_trans_id) > 0) {
2364 oldest_start =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002365 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002366 le32_to_cpu(jh->j_first_unflushed_offset);
2367 oldest_trans_id = le32_to_cpu(jh->j_last_flush_trans_id) + 1;
2368 newest_mount_id = le32_to_cpu(jh->j_mount_id);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002369 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002370 "journal-1153: found in "
2371 "header: first_unflushed_offset %d, last_flushed_trans_id "
2372 "%lu", le32_to_cpu(jh->j_first_unflushed_offset),
2373 le32_to_cpu(jh->j_last_flush_trans_id));
2374 valid_journal_header = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Jeff Mahoney0222e652009-03-30 14:02:44 -04002376 /* now, we try to read the first unflushed offset. If it is not valid,
2377 ** there is nothing more we can do, and it makes no sense to read
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002378 ** through the whole log.
2379 */
2380 d_bh =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002381 journal_bread(sb,
2382 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002383 le32_to_cpu(jh->j_first_unflushed_offset));
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002384 ret = journal_transaction_is_valid(sb, d_bh, NULL, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002385 if (!ret) {
2386 continue_replay = 0;
2387 }
2388 brelse(d_bh);
2389 goto start_log_replay;
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002392 /* ok, there are transactions that need to be replayed. start with the first log block, find
2393 ** all the valid transactions, and pick out the oldest.
2394 */
2395 while (continue_replay
2396 && cur_dblock <
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002397 (SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
2398 SB_ONDISK_JOURNAL_SIZE(sb))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002399 /* Note that it is required for blocksize of primary fs device and journal
2400 device to be the same */
2401 d_bh =
2402 reiserfs_breada(journal->j_dev_bd, cur_dblock,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002403 sb->s_blocksize,
2404 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
2405 SB_ONDISK_JOURNAL_SIZE(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002406 ret =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002407 journal_transaction_is_valid(sb, d_bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002408 &oldest_invalid_trans_id,
2409 &newest_mount_id);
2410 if (ret == 1) {
2411 desc = (struct reiserfs_journal_desc *)d_bh->b_data;
2412 if (oldest_start == 0) { /* init all oldest_ values */
2413 oldest_trans_id = get_desc_trans_id(desc);
2414 oldest_start = d_bh->b_blocknr;
2415 newest_mount_id = get_desc_mount_id(desc);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002416 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002417 "journal-1179: Setting "
2418 "oldest_start to offset %llu, trans_id %lu",
2419 oldest_start -
2420 SB_ONDISK_JOURNAL_1st_BLOCK
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002421 (sb), oldest_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002422 } else if (oldest_trans_id > get_desc_trans_id(desc)) {
2423 /* one we just read was older */
2424 oldest_trans_id = get_desc_trans_id(desc);
2425 oldest_start = d_bh->b_blocknr;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002426 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002427 "journal-1180: Resetting "
2428 "oldest_start to offset %lu, trans_id %lu",
2429 oldest_start -
2430 SB_ONDISK_JOURNAL_1st_BLOCK
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002431 (sb), oldest_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002432 }
2433 if (newest_mount_id < get_desc_mount_id(desc)) {
2434 newest_mount_id = get_desc_mount_id(desc);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002435 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002436 "journal-1299: Setting "
2437 "newest_mount_id to %d",
2438 get_desc_mount_id(desc));
2439 }
2440 cur_dblock += get_desc_trans_len(desc) + 2;
2441 } else {
2442 cur_dblock++;
2443 }
2444 brelse(d_bh);
2445 }
2446
2447 start_log_replay:
2448 cur_dblock = oldest_start;
2449 if (oldest_trans_id) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002450 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002451 "journal-1206: Starting replay "
2452 "from offset %llu, trans_id %lu",
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002453 cur_dblock - SB_ONDISK_JOURNAL_1st_BLOCK(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002454 oldest_trans_id);
2455
2456 }
2457 replay_count = 0;
2458 while (continue_replay && oldest_trans_id > 0) {
2459 ret =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002460 journal_read_transaction(sb, cur_dblock, oldest_start,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002461 oldest_trans_id, newest_mount_id);
2462 if (ret < 0) {
2463 return ret;
2464 } else if (ret != 0) {
2465 break;
2466 }
2467 cur_dblock =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002468 SB_ONDISK_JOURNAL_1st_BLOCK(sb) + journal->j_start;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002469 replay_count++;
2470 if (cur_dblock == oldest_start)
2471 break;
2472 }
2473
2474 if (oldest_trans_id == 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002475 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002476 "journal-1225: No valid " "transactions found");
2477 }
2478 /* j_start does not get set correctly if we don't replay any transactions.
2479 ** if we had a valid journal_header, set j_start to the first unflushed transaction value,
2480 ** copy the trans_id from the header
2481 */
2482 if (valid_journal_header && replay_count == 0) {
2483 journal->j_start = le32_to_cpu(jh->j_first_unflushed_offset);
2484 journal->j_trans_id =
2485 le32_to_cpu(jh->j_last_flush_trans_id) + 1;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08002486 /* check for trans_id overflow */
2487 if (journal->j_trans_id == 0)
2488 journal->j_trans_id = 10;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002489 journal->j_last_flush_trans_id =
2490 le32_to_cpu(jh->j_last_flush_trans_id);
2491 journal->j_mount_id = le32_to_cpu(jh->j_mount_id) + 1;
2492 } else {
2493 journal->j_mount_id = newest_mount_id + 1;
2494 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002495 reiserfs_debug(sb, REISERFS_DEBUG_CODE, "journal-1299: Setting "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002496 "newest_mount_id to %lu", journal->j_mount_id);
2497 journal->j_first_unflushed_offset = journal->j_start;
2498 if (replay_count > 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002499 reiserfs_info(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002500 "replayed %d transactions in %lu seconds\n",
2501 replay_count, get_seconds() - start);
2502 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002503 if (!bdev_read_only(sb->s_bdev) &&
2504 _update_journal_header_block(sb, journal->j_start,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002505 journal->j_last_flush_trans_id)) {
2506 /* replay failed, caller must call free_journal_ram and abort
2507 ** the mount
2508 */
2509 return -1;
2510 }
2511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static struct reiserfs_journal_list *alloc_journal_list(struct super_block *s)
2515{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002516 struct reiserfs_journal_list *jl;
Pekka Enberg8c777cc2006-02-01 03:06:43 -08002517 jl = kzalloc(sizeof(struct reiserfs_journal_list),
2518 GFP_NOFS | __GFP_NOFAIL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002519 INIT_LIST_HEAD(&jl->j_list);
2520 INIT_LIST_HEAD(&jl->j_working_list);
2521 INIT_LIST_HEAD(&jl->j_tail_bh_list);
2522 INIT_LIST_HEAD(&jl->j_bh_list);
Jeff Mahoney90415de2008-07-25 01:46:40 -07002523 mutex_init(&jl->j_commit_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002524 SB_JOURNAL(s)->j_num_lists++;
2525 get_journal_list(jl);
2526 return jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002529static void journal_list_init(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002531 SB_JOURNAL(sb)->j_current_jl = alloc_journal_list(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002534static int release_journal_dev(struct super_block *super,
2535 struct reiserfs_journal *journal)
2536{
2537 int result;
2538
2539 result = 0;
2540
Christoph Hellwig86098fa2008-04-30 00:54:46 -07002541 if (journal->j_dev_bd != NULL) {
Al Viroe5eb8ca2007-10-08 13:24:05 -04002542 result = blkdev_put(journal->j_dev_bd, journal->j_dev_mode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002543 journal->j_dev_bd = NULL;
2544 }
2545
2546 if (result != 0) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002547 reiserfs_warning(super, "sh-457",
2548 "Cannot release journal device: %i", result);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002549 }
2550 return result;
2551}
2552
2553static int journal_init_dev(struct super_block *super,
2554 struct reiserfs_journal *journal,
2555 const char *jdev_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 int result;
2558 dev_t jdev;
Tejun Heoe525fd82010-11-13 11:55:17 +01002559 fmode_t blkdev_mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 char b[BDEVNAME_SIZE];
2561
2562 result = 0;
2563
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002564 journal->j_dev_bd = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002565 jdev = SB_ONDISK_JOURNAL_DEVICE(super) ?
2566 new_decode_dev(SB_ONDISK_JOURNAL_DEVICE(super)) : super->s_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 if (bdev_read_only(super->s_bdev))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002569 blkdev_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 /* there is no "jdev" option and journal is on separate device */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002572 if ((!jdev_name || !jdev_name[0])) {
Tejun Heoe525fd82010-11-13 11:55:17 +01002573 if (jdev == super->s_dev)
2574 blkdev_mode &= ~FMODE_EXCL;
Tejun Heod4d77622010-11-13 11:55:18 +01002575 journal->j_dev_bd = blkdev_get_by_dev(jdev, blkdev_mode,
2576 journal);
Al Viroe5eb8ca2007-10-08 13:24:05 -04002577 journal->j_dev_mode = blkdev_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (IS_ERR(journal->j_dev_bd)) {
2579 result = PTR_ERR(journal->j_dev_bd);
2580 journal->j_dev_bd = NULL;
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002581 reiserfs_warning(super, "sh-458",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002582 "cannot init journal device '%s': %i",
2583 __bdevname(jdev, b), result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return result;
Tejun Heoe525fd82010-11-13 11:55:17 +01002585 } else if (jdev != super->s_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 set_blocksize(journal->j_dev_bd, super->s_blocksize);
Christoph Hellwig86098fa2008-04-30 00:54:46 -07002587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return 0;
2589 }
2590
Al Viroe5eb8ca2007-10-08 13:24:05 -04002591 journal->j_dev_mode = blkdev_mode;
Tejun Heod4d77622010-11-13 11:55:18 +01002592 journal->j_dev_bd = blkdev_get_by_path(jdev_name, blkdev_mode, journal);
Christoph Hellwig86098fa2008-04-30 00:54:46 -07002593 if (IS_ERR(journal->j_dev_bd)) {
2594 result = PTR_ERR(journal->j_dev_bd);
2595 journal->j_dev_bd = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002596 reiserfs_warning(super,
2597 "journal_init_dev: Cannot open '%s': %i",
2598 jdev_name, result);
Christoph Hellwig86098fa2008-04-30 00:54:46 -07002599 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Christoph Hellwig86098fa2008-04-30 00:54:46 -07002601
2602 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2603 reiserfs_info(super,
2604 "journal_init_dev: journal device: %s\n",
2605 bdevname(journal->j_dev_bd, b));
2606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002609/**
2610 * When creating/tuning a file system user can assign some
2611 * journal params within boundaries which depend on the ratio
2612 * blocksize/standard_blocksize.
2613 *
2614 * For blocks >= standard_blocksize transaction size should
2615 * be not less then JOURNAL_TRANS_MIN_DEFAULT, and not more
2616 * then JOURNAL_TRANS_MAX_DEFAULT.
2617 *
2618 * For blocks < standard_blocksize these boundaries should be
2619 * decreased proportionally.
2620 */
2621#define REISERFS_STANDARD_BLKSIZE (4096)
2622
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002623static int check_advise_trans_params(struct super_block *sb,
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002624 struct reiserfs_journal *journal)
2625{
2626 if (journal->j_trans_max) {
2627 /* Non-default journal params.
2628 Do sanity check for them. */
2629 int ratio = 1;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002630 if (sb->s_blocksize < REISERFS_STANDARD_BLKSIZE)
2631 ratio = REISERFS_STANDARD_BLKSIZE / sb->s_blocksize;
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002632
2633 if (journal->j_trans_max > JOURNAL_TRANS_MAX_DEFAULT / ratio ||
2634 journal->j_trans_max < JOURNAL_TRANS_MIN_DEFAULT / ratio ||
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002635 SB_ONDISK_JOURNAL_SIZE(sb) / journal->j_trans_max <
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002636 JOURNAL_MIN_RATIO) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002637 reiserfs_warning(sb, "sh-462",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002638 "bad transaction max size (%u). "
2639 "FSCK?", journal->j_trans_max);
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002640 return 1;
2641 }
2642 if (journal->j_max_batch != (journal->j_trans_max) *
2643 JOURNAL_MAX_BATCH_DEFAULT/JOURNAL_TRANS_MAX_DEFAULT) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002644 reiserfs_warning(sb, "sh-463",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002645 "bad transaction max batch (%u). "
2646 "FSCK?", journal->j_max_batch);
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002647 return 1;
2648 }
2649 } else {
2650 /* Default journal params.
2651 The file system was created by old version
2652 of mkreiserfs, so some fields contain zeros,
2653 and we need to advise proper values for them */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002654 if (sb->s_blocksize != REISERFS_STANDARD_BLKSIZE) {
2655 reiserfs_warning(sb, "sh-464", "bad blocksize (%u)",
2656 sb->s_blocksize);
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002657 return 1;
2658 }
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002659 journal->j_trans_max = JOURNAL_TRANS_MAX_DEFAULT;
2660 journal->j_max_batch = JOURNAL_MAX_BATCH_DEFAULT;
2661 journal->j_max_commit_age = JOURNAL_MAX_COMMIT_AGE;
2662 }
2663 return 0;
2664}
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666/*
2667** must be called once on fs mount. calls journal_read for you
2668*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002669int journal_init(struct super_block *sb, const char *j_dev_name,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002670 int old_format, unsigned int commit_max_age)
2671{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002672 int num_cnodes = SB_ONDISK_JOURNAL_SIZE(sb) * 2;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002673 struct buffer_head *bhjh;
2674 struct reiserfs_super_block *rs;
2675 struct reiserfs_journal_header *jh;
2676 struct reiserfs_journal *journal;
2677 struct reiserfs_journal_list *jl;
2678 char b[BDEVNAME_SIZE];
Frederic Weisbecker98ea3f52009-12-29 21:51:15 +01002679 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Frederic Weisbecker98ea3f52009-12-29 21:51:15 +01002681 /*
2682 * Unlock here to avoid various RECLAIM-FS-ON <-> IN-RECLAIM-FS
2683 * dependency inversion warnings.
2684 */
2685 reiserfs_write_unlock(sb);
Joe Perches558feb02011-05-28 10:36:33 -07002686 journal = SB_JOURNAL(sb) = vzalloc(sizeof(struct reiserfs_journal));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002687 if (!journal) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002688 reiserfs_warning(sb, "journal-1256",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002689 "unable to get memory for journal structure");
Frederic Weisbecker98ea3f52009-12-29 21:51:15 +01002690 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002691 return 1;
2692 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002693 INIT_LIST_HEAD(&journal->j_bitmap_nodes);
2694 INIT_LIST_HEAD(&journal->j_prealloc_list);
2695 INIT_LIST_HEAD(&journal->j_working_list);
2696 INIT_LIST_HEAD(&journal->j_journal_list);
2697 journal->j_persistent_trans = 0;
Frederic Weisbecker98ea3f52009-12-29 21:51:15 +01002698 ret = reiserfs_allocate_list_bitmaps(sb, journal->j_list_bitmap,
2699 reiserfs_bmap_count(sb));
2700 reiserfs_write_lock(sb);
2701 if (ret)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002702 goto free_and_return;
Frederic Weisbecker98ea3f52009-12-29 21:51:15 +01002703
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002704 allocate_bitmap_nodes(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002706 /* reserved for journal area support */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002707 SB_JOURNAL_1st_RESERVED_BLOCK(sb) = (old_format ?
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002708 REISERFS_OLD_DISK_OFFSET_IN_BYTES
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002709 / sb->s_blocksize +
2710 reiserfs_bmap_count(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002711 1 :
2712 REISERFS_DISK_OFFSET_IN_BYTES /
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002713 sb->s_blocksize + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002715 /* Sanity check to see is the standard journal fitting within first bitmap
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002716 (actual for small blocksizes) */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002717 if (!SB_ONDISK_JOURNAL_DEVICE(sb) &&
2718 (SB_JOURNAL_1st_RESERVED_BLOCK(sb) +
2719 SB_ONDISK_JOURNAL_SIZE(sb) > sb->s_blocksize * 8)) {
2720 reiserfs_warning(sb, "journal-1393",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002721 "journal does not fit for area addressed "
2722 "by first of bitmap blocks. It starts at "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002723 "%u and its size is %u. Block size %ld",
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002724 SB_JOURNAL_1st_RESERVED_BLOCK(sb),
2725 SB_ONDISK_JOURNAL_SIZE(sb),
2726 sb->s_blocksize);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002727 goto free_and_return;
2728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Frederic Weisbecker193be0e2009-09-17 05:31:37 +02002730 /*
2731 * We need to unlock here to avoid creating the following
2732 * dependency:
2733 * reiserfs_lock -> sysfs_mutex
2734 * Because the reiserfs mmap path creates the following dependency:
2735 * mm->mmap -> reiserfs_lock, hence we have
2736 * mm->mmap -> reiserfs_lock ->sysfs_mutex
2737 * This would ends up in a circular dependency with sysfs readdir path
2738 * which does sysfs_mutex -> mm->mmap_sem
2739 * This is fine because the reiserfs lock is useless in mount path,
2740 * at least until we call journal_begin. We keep it for paranoid
2741 * reasons.
2742 */
2743 reiserfs_write_unlock(sb);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002744 if (journal_init_dev(sb, journal, j_dev_name) != 0) {
Frederic Weisbecker193be0e2009-09-17 05:31:37 +02002745 reiserfs_write_lock(sb);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002746 reiserfs_warning(sb, "sh-462",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002747 "unable to initialize jornal device");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002748 goto free_and_return;
2749 }
Frederic Weisbecker193be0e2009-09-17 05:31:37 +02002750 reiserfs_write_lock(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002752 rs = SB_DISK_SUPER_BLOCK(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002754 /* read journal header */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002755 bhjh = journal_bread(sb,
2756 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
2757 SB_ONDISK_JOURNAL_SIZE(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002758 if (!bhjh) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002759 reiserfs_warning(sb, "sh-459",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002760 "unable to read journal header");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002761 goto free_and_return;
2762 }
2763 jh = (struct reiserfs_journal_header *)(bhjh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002765 /* make sure that journal matches to the super block */
2766 if (is_reiserfs_jr(rs)
2767 && (le32_to_cpu(jh->jh_journal.jp_journal_magic) !=
2768 sb_jp_journal_magic(rs))) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002769 reiserfs_warning(sb, "sh-460",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002770 "journal header magic %x (device %s) does "
2771 "not match to magic found in super block %x",
2772 jh->jh_journal.jp_journal_magic,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002773 bdevname(journal->j_dev_bd, b),
2774 sb_jp_journal_magic(rs));
2775 brelse(bhjh);
2776 goto free_and_return;
2777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002779 journal->j_trans_max = le32_to_cpu(jh->jh_journal.jp_journal_trans_max);
2780 journal->j_max_batch = le32_to_cpu(jh->jh_journal.jp_journal_max_batch);
2781 journal->j_max_commit_age =
2782 le32_to_cpu(jh->jh_journal.jp_journal_max_commit_age);
2783 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002785 if (check_advise_trans_params(sb, journal) != 0)
Edward Shishkincf3d0b82007-10-16 23:30:30 -07002786 goto free_and_return;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002787 journal->j_default_max_commit_age = journal->j_max_commit_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002789 if (commit_max_age != 0) {
2790 journal->j_max_commit_age = commit_max_age;
2791 journal->j_max_trans_age = commit_max_age;
2792 }
2793
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002794 reiserfs_info(sb, "journal params: device %s, size %u, "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002795 "journal first block %u, max trans len %u, max batch %u, "
2796 "max commit age %u, max trans age %u\n",
2797 bdevname(journal->j_dev_bd, b),
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002798 SB_ONDISK_JOURNAL_SIZE(sb),
2799 SB_ONDISK_JOURNAL_1st_BLOCK(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002800 journal->j_trans_max,
2801 journal->j_max_batch,
2802 journal->j_max_commit_age, journal->j_max_trans_age);
2803
2804 brelse(bhjh);
2805
2806 journal->j_list_bitmap_index = 0;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002807 journal_list_init(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002808
2809 memset(journal->j_list_hash_table, 0,
2810 JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
2811
2812 INIT_LIST_HEAD(&journal->j_dirty_buffers);
2813 spin_lock_init(&journal->j_dirty_buffers_lock);
2814
2815 journal->j_start = 0;
2816 journal->j_len = 0;
2817 journal->j_len_alloc = 0;
2818 atomic_set(&(journal->j_wcount), 0);
2819 atomic_set(&(journal->j_async_throttle), 0);
2820 journal->j_bcount = 0;
2821 journal->j_trans_start_time = 0;
2822 journal->j_last = NULL;
2823 journal->j_first = NULL;
2824 init_waitqueue_head(&(journal->j_join_wait));
Jeff Mahoneyf68215c2008-07-25 01:46:38 -07002825 mutex_init(&journal->j_mutex);
Jeff Mahoneyafe70252008-07-25 01:46:39 -07002826 mutex_init(&journal->j_flush_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002827
2828 journal->j_trans_id = 10;
2829 journal->j_mount_id = 10;
2830 journal->j_state = 0;
2831 atomic_set(&(journal->j_jlock), 0);
Frederic Weisbeckerbbec9192010-01-28 13:43:50 +01002832 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002833 journal->j_cnode_free_list = allocate_cnodes(num_cnodes);
Frederic Weisbeckerbbec9192010-01-28 13:43:50 +01002834 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002835 journal->j_cnode_free_orig = journal->j_cnode_free_list;
2836 journal->j_cnode_free = journal->j_cnode_free_list ? num_cnodes : 0;
2837 journal->j_cnode_used = 0;
2838 journal->j_must_wait = 0;
2839
Jeff Mahoney576f6d72005-11-29 19:34:39 -08002840 if (journal->j_cnode_free == 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002841 reiserfs_warning(sb, "journal-2004", "Journal cnode memory "
Jeff Mahoney576f6d72005-11-29 19:34:39 -08002842 "allocation failed (%ld bytes). Journal is "
2843 "too large for available memory. Usually "
2844 "this is due to a journal that is too large.",
2845 sizeof (struct reiserfs_journal_cnode) * num_cnodes);
2846 goto free_and_return;
2847 }
2848
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002849 init_journal_hash(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002850 jl = journal->j_current_jl;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002851 jl->j_list_bitmap = get_list_bitmap(sb, jl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002852 if (!jl->j_list_bitmap) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002853 reiserfs_warning(sb, "journal-2005",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002854 "get_list_bitmap failed for journal list 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002855 goto free_and_return;
2856 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002857 if (journal_read(sb) < 0) {
2858 reiserfs_warning(sb, "reiserfs-2006",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002859 "Replay Failure, unable to mount");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002860 goto free_and_return;
2861 }
2862
2863 reiserfs_mounted_fs_count++;
Frederic Weisbecker48f6ba52009-10-05 16:31:37 +02002864 if (reiserfs_mounted_fs_count <= 1) {
2865 reiserfs_write_unlock(sb);
Tejun Heo28aadf52011-02-01 11:42:42 +01002866 commit_wq = alloc_workqueue("reiserfs", WQ_MEM_RECLAIM, 0);
Frederic Weisbecker48f6ba52009-10-05 16:31:37 +02002867 reiserfs_write_lock(sb);
2868 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002869
David Howellsc4028952006-11-22 14:57:56 +00002870 INIT_DELAYED_WORK(&journal->j_work, flush_async_commits);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002871 journal->j_work_sb = sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002872 return 0;
2873 free_and_return:
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002874 free_journal_ram(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002875 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
2878/*
2879** test for a polite end of the current transaction. Used by file_write, and should
2880** be used by delete to make sure they don't write more than can fit inside a single
2881** transaction
2882*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002883int journal_transaction_should_end(struct reiserfs_transaction_handle *th,
2884 int new_alloc)
2885{
2886 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2887 time_t now = get_seconds();
2888 /* cannot restart while nested */
2889 BUG_ON(!th->t_trans_id);
2890 if (th->t_refcount > 1)
2891 return 0;
2892 if (journal->j_must_wait > 0 ||
2893 (journal->j_len_alloc + new_alloc) >= journal->j_max_batch ||
2894 atomic_read(&(journal->j_jlock)) ||
2895 (now - journal->j_trans_start_time) > journal->j_max_trans_age ||
2896 journal->j_cnode_free < (journal->j_trans_max * 3)) {
2897 return 1;
2898 }
Chris Mason6ae1ea42006-02-01 03:06:50 -08002899 /* protected by the BKL here */
2900 journal->j_len_alloc += new_alloc;
2901 th->t_blocks_allocated += new_alloc ;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
Jeff Mahoney0222e652009-03-30 14:02:44 -04002905/* this must be called inside a transaction, and requires the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906** kernel_lock to be held
2907*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002908void reiserfs_block_writes(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002910 struct reiserfs_journal *journal = SB_JOURNAL(th->t_super);
2911 BUG_ON(!th->t_trans_id);
2912 journal->j_must_wait = 1;
2913 set_bit(J_WRITERS_BLOCKED, &journal->j_state);
2914 return;
2915}
2916
2917/* this must be called without a transaction started, and does not
2918** require BKL
2919*/
2920void reiserfs_allow_writes(struct super_block *s)
2921{
2922 struct reiserfs_journal *journal = SB_JOURNAL(s);
2923 clear_bit(J_WRITERS_BLOCKED, &journal->j_state);
2924 wake_up(&journal->j_join_wait);
2925}
2926
2927/* this must be called without a transaction started, and does not
2928** require BKL
2929*/
2930void reiserfs_wait_on_write_block(struct super_block *s)
2931{
2932 struct reiserfs_journal *journal = SB_JOURNAL(s);
2933 wait_event(journal->j_join_wait,
2934 !test_bit(J_WRITERS_BLOCKED, &journal->j_state));
2935}
2936
2937static void queue_log_writer(struct super_block *s)
2938{
2939 wait_queue_t wait;
2940 struct reiserfs_journal *journal = SB_JOURNAL(s);
2941 set_bit(J_WRITERS_QUEUED, &journal->j_state);
2942
2943 /*
2944 * we don't want to use wait_event here because
2945 * we only want to wait once.
2946 */
2947 init_waitqueue_entry(&wait, current);
2948 add_wait_queue(&journal->j_join_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 set_current_state(TASK_UNINTERRUPTIBLE);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002950 if (test_bit(J_WRITERS_QUEUED, &journal->j_state)) {
2951 reiserfs_write_unlock(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002952 schedule();
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002953 reiserfs_write_lock(s);
2954 }
Milind Arun Choudhary5ab2f7e2007-05-08 00:30:51 -07002955 __set_current_state(TASK_RUNNING);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002956 remove_wait_queue(&journal->j_join_wait, &wait);
2957}
2958
2959static void wake_queued_writers(struct super_block *s)
2960{
2961 struct reiserfs_journal *journal = SB_JOURNAL(s);
2962 if (test_and_clear_bit(J_WRITERS_QUEUED, &journal->j_state))
2963 wake_up(&journal->j_join_wait);
2964}
2965
Jeff Mahoney600ed412009-03-30 14:02:17 -04002966static void let_transaction_grow(struct super_block *sb, unsigned int trans_id)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002967{
2968 struct reiserfs_journal *journal = SB_JOURNAL(sb);
2969 unsigned long bcount = journal->j_bcount;
2970 while (1) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002971 reiserfs_write_unlock(sb);
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002972 schedule_timeout_uninterruptible(1);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02002973 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002974 journal->j_current_jl->j_state |= LIST_COMMIT_PENDING;
2975 while ((atomic_read(&journal->j_wcount) > 0 ||
2976 atomic_read(&journal->j_jlock)) &&
2977 journal->j_trans_id == trans_id) {
2978 queue_log_writer(sb);
2979 }
2980 if (journal->j_trans_id != trans_id)
2981 break;
2982 if (bcount == journal->j_bcount)
2983 break;
2984 bcount = journal->j_bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988/* join == true if you must join an existing transaction.
2989** join == false if you can deal with waiting for others to finish
2990**
2991** this will block until the transaction is joinable. send the number of blocks you
2992** expect to use in nblocks.
2993*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002994static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04002995 struct super_block *sb, unsigned long nblocks,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002996 int join)
2997{
2998 time_t now = get_seconds();
Jeff Mahoney600ed412009-03-30 14:02:17 -04002999 unsigned int old_trans_id;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003000 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003001 struct reiserfs_transaction_handle myth;
3002 int sched_count = 0;
3003 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003005 reiserfs_check_lock_depth(sb, "journal_begin");
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003006 BUG_ON(nblocks > journal->j_trans_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003008 PROC_INFO_INC(sb, journal.journal_being);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003009 /* set here for journal_join */
3010 th->t_refcount = 1;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003011 th->t_super = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003013 relock:
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003014 lock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003015 if (join != JBEGIN_ABORT && reiserfs_is_journal_aborted(journal)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003016 unlock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003017 retval = journal->j_errno;
3018 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003020 journal->j_bcount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003022 if (test_bit(J_WRITERS_BLOCKED, &journal->j_state)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003023 unlock_journal(sb);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02003024 reiserfs_write_unlock(sb);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003025 reiserfs_wait_on_write_block(sb);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02003026 reiserfs_write_lock(sb);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003027 PROC_INFO_INC(sb, journal.journal_relock_writers);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003028 goto relock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003030 now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003032 /* if there is no room in the journal OR
Jeff Mahoney0222e652009-03-30 14:02:44 -04003033 ** if this transaction is too old, and we weren't called joinable, wait for it to finish before beginning
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003034 ** we don't sleep if there aren't other writers
3035 */
3036
3037 if ((!join && journal->j_must_wait > 0) ||
3038 (!join
3039 && (journal->j_len_alloc + nblocks + 2) >= journal->j_max_batch)
3040 || (!join && atomic_read(&journal->j_wcount) > 0
3041 && journal->j_trans_start_time > 0
3042 && (now - journal->j_trans_start_time) >
3043 journal->j_max_trans_age) || (!join
3044 && atomic_read(&journal->j_jlock))
3045 || (!join && journal->j_cnode_free < (journal->j_trans_max * 3))) {
3046
3047 old_trans_id = journal->j_trans_id;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003048 unlock_journal(sb); /* allow others to finish this transaction */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003049
3050 if (!join && (journal->j_len_alloc + nblocks + 2) >=
3051 journal->j_max_batch &&
3052 ((journal->j_len + nblocks + 2) * 100) <
3053 (journal->j_len_alloc * 75)) {
3054 if (atomic_read(&journal->j_wcount) > 10) {
3055 sched_count++;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003056 queue_log_writer(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003057 goto relock;
3058 }
3059 }
3060 /* don't mess with joining the transaction if all we have to do is
3061 * wait for someone else to do a commit
3062 */
3063 if (atomic_read(&journal->j_jlock)) {
3064 while (journal->j_trans_id == old_trans_id &&
3065 atomic_read(&journal->j_jlock)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003066 queue_log_writer(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003067 }
3068 goto relock;
3069 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003070 retval = journal_join(&myth, sb, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003071 if (retval)
3072 goto out_fail;
3073
3074 /* someone might have ended the transaction while we joined */
3075 if (old_trans_id != journal->j_trans_id) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003076 retval = do_journal_end(&myth, sb, 1, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003077 } else {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003078 retval = do_journal_end(&myth, sb, 1, COMMIT_NOW);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003079 }
3080
3081 if (retval)
3082 goto out_fail;
3083
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003084 PROC_INFO_INC(sb, journal.journal_relock_wcount);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003085 goto relock;
3086 }
3087 /* we are the first writer, set trans_id */
3088 if (journal->j_trans_start_time == 0) {
3089 journal->j_trans_start_time = get_seconds();
3090 }
3091 atomic_inc(&(journal->j_wcount));
3092 journal->j_len_alloc += nblocks;
3093 th->t_blocks_logged = 0;
3094 th->t_blocks_allocated = nblocks;
3095 th->t_trans_id = journal->j_trans_id;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003096 unlock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003097 INIT_LIST_HEAD(&th->t_list);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003098 return 0;
3099
3100 out_fail:
3101 memset(th, 0, sizeof(*th));
3102 /* Re-set th->t_super, so we can properly keep track of how many
3103 * persistent transactions there are. We need to do this so if this
3104 * call is part of a failed restart_transaction, we can free it later */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003105 th->t_super = sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003106 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003109struct reiserfs_transaction_handle *reiserfs_persistent_transaction(struct
3110 super_block
3111 *s,
3112 int nblocks)
3113{
3114 int ret;
3115 struct reiserfs_transaction_handle *th;
3116
3117 /* if we're nesting into an existing transaction. It will be
3118 ** persistent on its own
3119 */
3120 if (reiserfs_transaction_running(s)) {
3121 th = current->journal_info;
3122 th->t_refcount++;
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003123 BUG_ON(th->t_refcount < 2);
3124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003125 return th;
3126 }
Pekka Enbergd739b422006-02-01 03:06:43 -08003127 th = kmalloc(sizeof(struct reiserfs_transaction_handle), GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003128 if (!th)
3129 return NULL;
3130 ret = journal_begin(th, s, nblocks);
3131 if (ret) {
Pekka Enbergd739b422006-02-01 03:06:43 -08003132 kfree(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003133 return NULL;
3134 }
3135
3136 SB_JOURNAL(s)->j_persistent_trans++;
3137 return th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003140int reiserfs_end_persistent_transaction(struct reiserfs_transaction_handle *th)
3141{
3142 struct super_block *s = th->t_super;
3143 int ret = 0;
3144 if (th->t_trans_id)
3145 ret = journal_end(th, th->t_super, th->t_blocks_allocated);
3146 else
3147 ret = -EIO;
3148 if (th->t_refcount == 0) {
3149 SB_JOURNAL(s)->j_persistent_trans--;
Pekka Enbergd739b422006-02-01 03:06:43 -08003150 kfree(th);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003151 }
3152 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153}
3154
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003155static int journal_join(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003156 struct super_block *sb, unsigned long nblocks)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003157{
3158 struct reiserfs_transaction_handle *cur_th = current->journal_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003160 /* this keeps do_journal_end from NULLing out the current->journal_info
3161 ** pointer
3162 */
3163 th->t_handle_save = cur_th;
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003164 BUG_ON(cur_th && cur_th->t_refcount > 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003165 return do_journal_begin_r(th, sb, nblocks, JBEGIN_JOIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003168int journal_join_abort(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003169 struct super_block *sb, unsigned long nblocks)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003170{
3171 struct reiserfs_transaction_handle *cur_th = current->journal_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003173 /* this keeps do_journal_end from NULLing out the current->journal_info
3174 ** pointer
3175 */
3176 th->t_handle_save = cur_th;
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003177 BUG_ON(cur_th && cur_th->t_refcount > 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003178 return do_journal_begin_r(th, sb, nblocks, JBEGIN_ABORT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003179}
3180
3181int journal_begin(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003182 struct super_block *sb, unsigned long nblocks)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003183{
3184 struct reiserfs_transaction_handle *cur_th = current->journal_info;
3185 int ret;
3186
3187 th->t_handle_save = NULL;
3188 if (cur_th) {
3189 /* we are nesting into the current transaction */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003190 if (cur_th->t_super == sb) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003191 BUG_ON(!cur_th->t_refcount);
3192 cur_th->t_refcount++;
3193 memcpy(th, cur_th, sizeof(*th));
3194 if (th->t_refcount <= 1)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003195 reiserfs_warning(sb, "reiserfs-2005",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04003196 "BAD: refcount <= 1, but "
3197 "journal_info != 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003198 return 0;
3199 } else {
3200 /* we've ended up with a handle from a different filesystem.
3201 ** save it and restore on journal_end. This should never
3202 ** really happen...
3203 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003204 reiserfs_warning(sb, "clm-2100",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04003205 "nesting info a different FS");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003206 th->t_handle_save = current->journal_info;
3207 current->journal_info = th;
3208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 } else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003210 current->journal_info = th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003212 ret = do_journal_begin_r(th, sb, nblocks, JBEGIN_REG);
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003213 BUG_ON(current->journal_info != th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003215 /* I guess this boils down to being the reciprocal of clm-2100 above.
3216 * If do_journal_begin_r fails, we need to put it back, since journal_end
3217 * won't be called to do it. */
3218 if (ret)
3219 current->journal_info = th->t_handle_save;
3220 else
3221 BUG_ON(!th->t_refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
3226/*
3227** puts bh into the current transaction. If it was already there, reorders removes the
3228** old pointers from the hash, and puts new ones in (to make sure replay happen in the right order).
3229**
3230** if it was dirty, cleans and files onto the clean list. I can't let it be dirty again until the
3231** transaction is committed.
Jeff Mahoney0222e652009-03-30 14:02:44 -04003232**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233** if j_len, is bigger than j_len_alloc, it pushes j_len_alloc to 10 + j_len.
3234*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003235int journal_mark_dirty(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003236 struct super_block *sb, struct buffer_head *bh)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003237{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003238 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003239 struct reiserfs_journal_cnode *cn = NULL;
3240 int count_already_incd = 0;
3241 int prepared = 0;
3242 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003244 PROC_INFO_INC(sb, journal.mark_dirty);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003245 if (th->t_trans_id != journal->j_trans_id) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04003246 reiserfs_panic(th->t_super, "journal-1577",
3247 "handle trans id %ld != current trans id %ld",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003248 th->t_trans_id, journal->j_trans_id);
3249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003251 sb->s_dirt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003253 prepared = test_clear_buffer_journal_prepared(bh);
3254 clear_buffer_journal_restore_dirty(bh);
3255 /* already in this transaction, we are done */
3256 if (buffer_journaled(bh)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003257 PROC_INFO_INC(sb, journal.mark_dirty_already);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003258 return 0;
3259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003261 /* this must be turned into a panic instead of a warning. We can't allow
3262 ** a dirty or journal_dirty or locked buffer to be logged, as some changes
3263 ** could get to disk too early. NOT GOOD.
3264 */
3265 if (!prepared || buffer_dirty(bh)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003266 reiserfs_warning(sb, "journal-1777",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04003267 "buffer %llu bad state "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003268 "%cPREPARED %cLOCKED %cDIRTY %cJDIRTY_WAIT",
3269 (unsigned long long)bh->b_blocknr,
3270 prepared ? ' ' : '!',
3271 buffer_locked(bh) ? ' ' : '!',
3272 buffer_dirty(bh) ? ' ' : '!',
3273 buffer_journal_dirty(bh) ? ' ' : '!');
3274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003276 if (atomic_read(&(journal->j_wcount)) <= 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003277 reiserfs_warning(sb, "journal-1409",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04003278 "returning because j_wcount was %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003279 atomic_read(&(journal->j_wcount)));
3280 return 1;
3281 }
Jeff Mahoney0222e652009-03-30 14:02:44 -04003282 /* this error means I've screwed up, and we've overflowed the transaction.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003283 ** Nothing can be done here, except make the FS readonly or panic.
3284 */
3285 if (journal->j_len >= journal->j_trans_max) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04003286 reiserfs_panic(th->t_super, "journal-1413",
3287 "j_len (%lu) is too big",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003288 journal->j_len);
3289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003291 if (buffer_journal_dirty(bh)) {
3292 count_already_incd = 1;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003293 PROC_INFO_INC(sb, journal.mark_dirty_notjournal);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003294 clear_buffer_journal_dirty(bh);
3295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003297 if (journal->j_len > journal->j_len_alloc) {
3298 journal->j_len_alloc = journal->j_len + JOURNAL_PER_BALANCE_CNT;
3299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003301 set_buffer_journaled(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003303 /* now put this guy on the end */
3304 if (!cn) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003305 cn = get_cnode(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003306 if (!cn) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003307 reiserfs_panic(sb, "journal-4", "get_cnode failed!");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003310 if (th->t_blocks_logged == th->t_blocks_allocated) {
3311 th->t_blocks_allocated += JOURNAL_PER_BALANCE_CNT;
3312 journal->j_len_alloc += JOURNAL_PER_BALANCE_CNT;
3313 }
3314 th->t_blocks_logged++;
3315 journal->j_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003317 cn->bh = bh;
3318 cn->blocknr = bh->b_blocknr;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003319 cn->sb = sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003320 cn->jlist = NULL;
3321 insert_journal_hash(journal->j_hash_table, cn);
3322 if (!count_already_incd) {
3323 get_bh(bh);
3324 }
3325 }
3326 cn->next = NULL;
3327 cn->prev = journal->j_last;
3328 cn->bh = bh;
3329 if (journal->j_last) {
3330 journal->j_last->next = cn;
3331 journal->j_last = cn;
3332 } else {
3333 journal->j_first = cn;
3334 journal->j_last = cn;
3335 }
3336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337}
3338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003339int journal_end(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003340 struct super_block *sb, unsigned long nblocks)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003341{
3342 if (!current->journal_info && th->t_refcount > 1)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003343 reiserfs_warning(sb, "REISER-NESTING",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04003344 "th NULL, refcount %d", th->t_refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003346 if (!th->t_trans_id) {
3347 WARN_ON(1);
3348 return -EIO;
3349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003351 th->t_refcount--;
3352 if (th->t_refcount > 0) {
3353 struct reiserfs_transaction_handle *cur_th =
3354 current->journal_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003356 /* we aren't allowed to close a nested transaction on a different
3357 ** filesystem from the one in the task struct
3358 */
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003359 BUG_ON(cur_th->t_super != th->t_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003361 if (th != cur_th) {
3362 memcpy(current->journal_info, th, sizeof(*th));
3363 th->t_trans_id = 0;
3364 }
3365 return 0;
3366 } else {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003367 return do_journal_end(th, sb, nblocks, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Jeff Mahoney0222e652009-03-30 14:02:44 -04003371/* removes from the current transaction, relsing and descrementing any counters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372** also files the removed buffer directly onto the clean list
3373**
3374** called by journal_mark_freed when a block has been deleted
3375**
3376** returns 1 if it cleaned and relsed the buffer. 0 otherwise
3377*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003378static int remove_from_transaction(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003379 b_blocknr_t blocknr, int already_cleaned)
3380{
3381 struct buffer_head *bh;
3382 struct reiserfs_journal_cnode *cn;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003383 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003384 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003386 cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003387 if (!cn || !cn->bh) {
3388 return ret;
3389 }
3390 bh = cn->bh;
3391 if (cn->prev) {
3392 cn->prev->next = cn->next;
3393 }
3394 if (cn->next) {
3395 cn->next->prev = cn->prev;
3396 }
3397 if (cn == journal->j_first) {
3398 journal->j_first = cn->next;
3399 }
3400 if (cn == journal->j_last) {
3401 journal->j_last = cn->prev;
3402 }
3403 if (bh)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003404 remove_journal_hash(sb, journal->j_hash_table, NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003405 bh->b_blocknr, 0);
3406 clear_buffer_journaled(bh); /* don't log this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003408 if (!already_cleaned) {
3409 clear_buffer_journal_dirty(bh);
3410 clear_buffer_dirty(bh);
3411 clear_buffer_journal_test(bh);
3412 put_bh(bh);
3413 if (atomic_read(&(bh->b_count)) < 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003414 reiserfs_warning(sb, "journal-1752",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04003415 "b_count < 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003416 }
3417 ret = 1;
3418 }
3419 journal->j_len--;
3420 journal->j_len_alloc--;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003421 free_cnode(sb, cn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003422 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
3425/*
3426** for any cnode in a journal list, it can only be dirtied of all the
Matt LaPlante0779bf22006-11-30 05:24:39 +01003427** transactions that include it are committed to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428** this checks through each transaction, and returns 1 if you are allowed to dirty,
3429** and 0 if you aren't
3430**
3431** it is called by dirty_journal_list, which is called after flush_commit_list has gotten all the log
3432** blocks for a given transaction on disk
3433**
3434*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003435static int can_dirty(struct reiserfs_journal_cnode *cn)
3436{
3437 struct super_block *sb = cn->sb;
3438 b_blocknr_t blocknr = cn->blocknr;
3439 struct reiserfs_journal_cnode *cur = cn->hprev;
3440 int can_dirty = 1;
3441
3442 /* first test hprev. These are all newer than cn, so any node here
3443 ** with the same block number and dev means this node can't be sent
3444 ** to disk right now.
3445 */
3446 while (cur && can_dirty) {
3447 if (cur->jlist && cur->bh && cur->blocknr && cur->sb == sb &&
3448 cur->blocknr == blocknr) {
3449 can_dirty = 0;
3450 }
3451 cur = cur->hprev;
3452 }
3453 /* then test hnext. These are all older than cn. As long as they
3454 ** are committed to the log, it is safe to write cn to disk
3455 */
3456 cur = cn->hnext;
3457 while (cur && can_dirty) {
3458 if (cur->jlist && cur->jlist->j_len > 0 &&
3459 atomic_read(&(cur->jlist->j_commit_left)) > 0 && cur->bh &&
3460 cur->blocknr && cur->sb == sb && cur->blocknr == blocknr) {
3461 can_dirty = 0;
3462 }
3463 cur = cur->hnext;
3464 }
3465 return can_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466}
3467
3468/* syncs the commit blocks, but does not force the real buffers to disk
Jeff Mahoney0222e652009-03-30 14:02:44 -04003469** will wait until the current transaction is done/committed before returning
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003471int journal_end_sync(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003472 struct super_block *sb, unsigned long nblocks)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003473{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003474 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003476 BUG_ON(!th->t_trans_id);
3477 /* you can sync while nested, very, very bad */
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003478 BUG_ON(th->t_refcount > 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003479 if (journal->j_len == 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003480 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003481 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003482 journal_mark_dirty(th, sb, SB_BUFFER_WITH_SB(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003483 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003484 return do_journal_end(th, sb, nblocks, COMMIT_NOW | WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
3487/*
3488** writeback the pending async commits to disk
3489*/
David Howellsc4028952006-11-22 14:57:56 +00003490static void flush_async_commits(struct work_struct *work)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003491{
David Howellsc4028952006-11-22 14:57:56 +00003492 struct reiserfs_journal *journal =
3493 container_of(work, struct reiserfs_journal, j_work.work);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003494 struct super_block *sb = journal->j_work_sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003495 struct reiserfs_journal_list *jl;
3496 struct list_head *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02003498 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003499 if (!list_empty(&journal->j_journal_list)) {
3500 /* last entry is the youngest, commit it and you get everything */
3501 entry = journal->j_journal_list.prev;
3502 jl = JOURNAL_LIST_ENTRY(entry);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003503 flush_commit_list(sb, jl, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003504 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02003505 reiserfs_write_unlock(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
3508/*
3509** flushes any old transactions to disk
3510** ends the current transaction if it is too old
3511*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003512int reiserfs_flush_old_commits(struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003513{
3514 time_t now;
3515 struct reiserfs_transaction_handle th;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003516 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003518 now = get_seconds();
3519 /* safety check so we don't flush while we are replaying the log during
3520 * mount
3521 */
3522 if (list_empty(&journal->j_journal_list)) {
3523 return 0;
3524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003526 /* check the current transaction. If there are no writers, and it is
3527 * too old, finish it, and force the commit blocks to disk
3528 */
3529 if (atomic_read(&journal->j_wcount) <= 0 &&
3530 journal->j_trans_start_time > 0 &&
3531 journal->j_len > 0 &&
3532 (now - journal->j_trans_start_time) > journal->j_max_trans_age) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003533 if (!journal_join(&th, sb, 1)) {
3534 reiserfs_prepare_for_journal(sb,
3535 SB_BUFFER_WITH_SB(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003536 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003537 journal_mark_dirty(&th, sb,
3538 SB_BUFFER_WITH_SB(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003540 /* we're only being called from kreiserfsd, it makes no sense to do
3541 ** an async commit so that kreiserfsd can do it later
3542 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003543 do_journal_end(&th, sb, 1, COMMIT_NOW | WAIT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003544 }
3545 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003546 return sb->s_dirt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
3549/*
3550** returns 0 if do_journal_end should return right away, returns 1 if do_journal_end should finish the commit
Jeff Mahoney0222e652009-03-30 14:02:44 -04003551**
3552** if the current transaction is too old, but still has writers, this will wait on j_join_wait until all
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553** the writers are done. By the time it wakes up, the transaction it was called has already ended, so it just
3554** flushes the commit list and returns 0.
3555**
3556** Won't batch when flush or commit_now is set. Also won't batch when others are waiting on j_join_wait.
Jeff Mahoney0222e652009-03-30 14:02:44 -04003557**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558** Note, we can't allow the journal_end to proceed while there are still writers in the log.
3559*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003560static int check_journal_end(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003561 struct super_block *sb, unsigned long nblocks,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003562 int flags)
3563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003565 time_t now;
3566 int flush = flags & FLUSH_ALL;
3567 int commit_now = flags & COMMIT_NOW;
3568 int wait_on_commit = flags & WAIT;
3569 struct reiserfs_journal_list *jl;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003570 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003572 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003574 if (th->t_trans_id != journal->j_trans_id) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04003575 reiserfs_panic(th->t_super, "journal-1577",
3576 "handle trans id %ld != current trans id %ld",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003577 th->t_trans_id, journal->j_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003580 journal->j_len_alloc -= (th->t_blocks_allocated - th->t_blocks_logged);
3581 if (atomic_read(&(journal->j_wcount)) > 0) { /* <= 0 is allowed. unmounting might not call begin */
3582 atomic_dec(&(journal->j_wcount));
3583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Jeff Mahoney0222e652009-03-30 14:02:44 -04003585 /* BUG, deal with case where j_len is 0, but people previously freed blocks need to be released
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003586 ** will be dealt with by next transaction that actually writes something, but should be taken
3587 ** care of in this trans
3588 */
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003589 BUG_ON(journal->j_len == 0);
3590
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003591 /* if wcount > 0, and we are called to with flush or commit_now,
3592 ** we wait on j_join_wait. We will wake up when the last writer has
3593 ** finished the transaction, and started it on its way to the disk.
Jeff Mahoney0222e652009-03-30 14:02:44 -04003594 ** Then, we flush the commit or journal list, and just return 0
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003595 ** because the rest of journal end was already done for this transaction.
3596 */
3597 if (atomic_read(&(journal->j_wcount)) > 0) {
3598 if (flush || commit_now) {
3599 unsigned trans_id;
3600
3601 jl = journal->j_current_jl;
3602 trans_id = jl->j_trans_id;
3603 if (wait_on_commit)
3604 jl->j_state |= LIST_COMMIT_PENDING;
3605 atomic_set(&(journal->j_jlock), 1);
3606 if (flush) {
3607 journal->j_next_full_flush = 1;
3608 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003609 unlock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003610
3611 /* sleep while the current transaction is still j_jlocked */
3612 while (journal->j_trans_id == trans_id) {
3613 if (atomic_read(&journal->j_jlock)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003614 queue_log_writer(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003615 } else {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003616 lock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003617 if (journal->j_trans_id == trans_id) {
3618 atomic_set(&(journal->j_jlock),
3619 1);
3620 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003621 unlock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003622 }
3623 }
Eric Sesterhenn14a61442006-10-03 23:36:38 +02003624 BUG_ON(journal->j_trans_id == trans_id);
3625
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003626 if (commit_now
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003627 && journal_list_still_alive(sb, trans_id)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003628 && wait_on_commit) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003629 flush_commit_list(sb, jl, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003630 }
3631 return 0;
3632 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003633 unlock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003634 return 0;
3635 }
3636
3637 /* deal with old transactions where we are the last writers */
3638 now = get_seconds();
3639 if ((now - journal->j_trans_start_time) > journal->j_max_trans_age) {
3640 commit_now = 1;
3641 journal->j_next_async_flush = 1;
3642 }
3643 /* don't batch when someone is waiting on j_join_wait */
3644 /* don't batch when syncing the commit or flushing the whole trans */
3645 if (!(journal->j_must_wait > 0) && !(atomic_read(&(journal->j_jlock)))
3646 && !flush && !commit_now && (journal->j_len < journal->j_max_batch)
3647 && journal->j_len_alloc < journal->j_max_batch
3648 && journal->j_cnode_free > (journal->j_trans_max * 3)) {
3649 journal->j_bcount++;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003650 unlock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003651 return 0;
3652 }
3653
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003654 if (journal->j_start > SB_ONDISK_JOURNAL_SIZE(sb)) {
3655 reiserfs_panic(sb, "journal-003",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04003656 "j_start (%ld) is too high",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003657 journal->j_start);
3658 }
3659 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
3662/*
3663** Does all the work that makes deleting blocks safe.
3664** when deleting a block mark BH_JNew, just remove it from the current transaction, clean it's buffer_head and move on.
Jeff Mahoney0222e652009-03-30 14:02:44 -04003665**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666** otherwise:
3667** set a bit for the block in the journal bitmap. That will prevent it from being allocated for unformatted nodes
3668** before this transaction has finished.
3669**
3670** mark any cnodes for this block as BLOCK_FREED, and clear their bh pointers. That will prevent any old transactions with
3671** this block from trying to flush to the real location. Since we aren't removing the cnode from the journal_list_hash,
3672** the block can't be reallocated yet.
3673**
3674** Then remove it from the current transaction, decrementing any counters and filing it on the clean list.
3675*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003676int journal_mark_freed(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003677 struct super_block *sb, b_blocknr_t blocknr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003678{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003679 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003680 struct reiserfs_journal_cnode *cn = NULL;
3681 struct buffer_head *bh = NULL;
3682 struct reiserfs_list_bitmap *jb = NULL;
3683 int cleaned = 0;
3684 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003686 cn = get_journal_hash_dev(sb, journal->j_hash_table, blocknr);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003687 if (cn && cn->bh) {
3688 bh = cn->bh;
3689 get_bh(bh);
3690 }
3691 /* if it is journal new, we just remove it from this transaction */
3692 if (bh && buffer_journal_new(bh)) {
3693 clear_buffer_journal_new(bh);
3694 clear_prepared_bits(bh);
3695 reiserfs_clean_and_file_buffer(bh);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003696 cleaned = remove_from_transaction(sb, blocknr, cleaned);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003697 } else {
3698 /* set the bit for this block in the journal bitmap for this transaction */
3699 jb = journal->j_current_jl->j_list_bitmap;
3700 if (!jb) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003701 reiserfs_panic(sb, "journal-1702",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04003702 "journal_list_bitmap is NULL");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003703 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003704 set_bit_in_list_bitmap(sb, blocknr, jb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003706 /* Note, the entire while loop is not allowed to schedule. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003708 if (bh) {
3709 clear_prepared_bits(bh);
3710 reiserfs_clean_and_file_buffer(bh);
3711 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003712 cleaned = remove_from_transaction(sb, blocknr, cleaned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003714 /* find all older transactions with this block, make sure they don't try to write it out */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003715 cn = get_journal_hash_dev(sb, journal->j_list_hash_table,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003716 blocknr);
3717 while (cn) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003718 if (sb == cn->sb && blocknr == cn->blocknr) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003719 set_bit(BLOCK_FREED, &cn->state);
3720 if (cn->bh) {
3721 if (!cleaned) {
3722 /* remove_from_transaction will brelse the buffer if it was
3723 ** in the current trans
3724 */
3725 clear_buffer_journal_dirty(cn->
3726 bh);
3727 clear_buffer_dirty(cn->bh);
3728 clear_buffer_journal_test(cn->
3729 bh);
3730 cleaned = 1;
3731 put_bh(cn->bh);
3732 if (atomic_read
3733 (&(cn->bh->b_count)) < 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003734 reiserfs_warning(sb,
Jeff Mahoney45b03d52009-03-30 14:02:21 -04003735 "journal-2138",
3736 "cn->bh->b_count < 0");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003737 }
3738 }
3739 if (cn->jlist) { /* since we are clearing the bh, we MUST dec nonzerolen */
3740 atomic_dec(&
3741 (cn->jlist->
3742 j_nonzerolen));
3743 }
3744 cn->bh = NULL;
3745 }
3746 }
3747 cn = cn->hnext;
3748 }
3749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Chris Mason398c95b2007-10-16 23:29:44 -07003751 if (bh)
3752 release_buffer_page(bh); /* get_hash grabs the buffer */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003756void reiserfs_update_inode_transaction(struct inode *inode)
3757{
3758 struct reiserfs_journal *journal = SB_JOURNAL(inode->i_sb);
3759 REISERFS_I(inode)->i_jl = journal->j_current_jl;
3760 REISERFS_I(inode)->i_trans_id = journal->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}
3762
3763/*
3764 * returns -1 on error, 0 if no commits/barriers were done and 1
3765 * if a transaction was actually committed and the barrier was done
3766 */
3767static int __commit_trans_jl(struct inode *inode, unsigned long id,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003768 struct reiserfs_journal_list *jl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003770 struct reiserfs_transaction_handle th;
3771 struct super_block *sb = inode->i_sb;
3772 struct reiserfs_journal *journal = SB_JOURNAL(sb);
3773 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003775 /* is it from the current transaction, or from an unknown transaction? */
3776 if (id == journal->j_trans_id) {
3777 jl = journal->j_current_jl;
3778 /* try to let other writers come in and grow this transaction */
3779 let_transaction_grow(sb, id);
3780 if (journal->j_trans_id != id) {
3781 goto flush_commit_only;
3782 }
3783
3784 ret = journal_begin(&th, sb, 1);
3785 if (ret)
3786 return ret;
3787
3788 /* someone might have ended this transaction while we joined */
3789 if (journal->j_trans_id != id) {
3790 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
3791 1);
3792 journal_mark_dirty(&th, sb, SB_BUFFER_WITH_SB(sb));
3793 ret = journal_end(&th, sb, 1);
3794 goto flush_commit_only;
3795 }
3796
3797 ret = journal_end_sync(&th, sb, 1);
3798 if (!ret)
3799 ret = 1;
3800
3801 } else {
3802 /* this gets tricky, we have to make sure the journal list in
3803 * the inode still exists. We know the list is still around
3804 * if we've got a larger transaction id than the oldest list
3805 */
3806 flush_commit_only:
3807 if (journal_list_still_alive(inode->i_sb, id)) {
3808 /*
3809 * we only set ret to 1 when we know for sure
3810 * the barrier hasn't been started yet on the commit
3811 * block.
3812 */
3813 if (atomic_read(&jl->j_commit_left) > 1)
3814 ret = 1;
3815 flush_commit_list(sb, jl, 1);
3816 if (journal->j_errno)
3817 ret = journal->j_errno;
3818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003820 /* otherwise the list is gone, and long since committed */
3821 return ret;
3822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003824int reiserfs_commit_for_inode(struct inode *inode)
3825{
Jeff Mahoney600ed412009-03-30 14:02:17 -04003826 unsigned int id = REISERFS_I(inode)->i_trans_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003827 struct reiserfs_journal_list *jl = REISERFS_I(inode)->i_jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003829 /* for the whole inode, assume unset id means it was
3830 * changed in the current transaction. More conservative
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003832 if (!id || !jl) {
3833 reiserfs_update_inode_transaction(inode);
3834 id = REISERFS_I(inode)->i_trans_id;
3835 /* jl will be updated in __commit_trans_jl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003837
3838 return __commit_trans_jl(inode, id, jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003841void reiserfs_restore_prepared_buffer(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003842 struct buffer_head *bh)
3843{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003844 struct reiserfs_journal *journal = SB_JOURNAL(sb);
3845 PROC_INFO_INC(sb, journal.restore_prepared);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003846 if (!bh) {
3847 return;
3848 }
3849 if (test_clear_buffer_journal_restore_dirty(bh) &&
3850 buffer_journal_dirty(bh)) {
3851 struct reiserfs_journal_cnode *cn;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003852 cn = get_journal_hash_dev(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003853 journal->j_list_hash_table,
3854 bh->b_blocknr);
3855 if (cn && can_dirty(cn)) {
3856 set_buffer_journal_test(bh);
3857 mark_buffer_dirty(bh);
3858 }
3859 }
3860 clear_buffer_journal_prepared(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861}
3862
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003863extern struct tree_balance *cur_tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864/*
3865** before we can change a metadata block, we have to make sure it won't
3866** be written to disk while we are altering it. So, we must:
3867** clean it
3868** wait on it.
Jeff Mahoney0222e652009-03-30 14:02:44 -04003869**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870*/
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003871int reiserfs_prepare_for_journal(struct super_block *sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003872 struct buffer_head *bh, int wait)
3873{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003874 PROC_INFO_INC(sb, journal.prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Nick Pigginca5de402008-08-02 12:02:13 +02003876 if (!trylock_buffer(bh)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003877 if (!wait)
3878 return 0;
3879 lock_buffer(bh);
3880 }
3881 set_buffer_journal_prepared(bh);
3882 if (test_clear_buffer_dirty(bh) && buffer_journal_dirty(bh)) {
3883 clear_buffer_journal_test(bh);
3884 set_buffer_journal_restore_dirty(bh);
3885 }
3886 unlock_buffer(bh);
3887 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888}
3889
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003890static void flush_old_journal_lists(struct super_block *s)
3891{
3892 struct reiserfs_journal *journal = SB_JOURNAL(s);
3893 struct reiserfs_journal_list *jl;
3894 struct list_head *entry;
3895 time_t now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003897 while (!list_empty(&journal->j_journal_list)) {
3898 entry = journal->j_journal_list.next;
3899 jl = JOURNAL_LIST_ENTRY(entry);
3900 /* this check should always be run, to send old lists to disk */
Chris Masona3172022006-09-29 01:59:56 -07003901 if (jl->j_timestamp < (now - (JOURNAL_MAX_TRANS_AGE * 4)) &&
3902 atomic_read(&jl->j_commit_left) == 0 &&
3903 test_transaction(s, jl)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003904 flush_used_journal_lists(s, jl);
3905 } else {
3906 break;
3907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909}
3910
Jeff Mahoney0222e652009-03-30 14:02:44 -04003911/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912** long and ugly. If flush, will not return until all commit
3913** blocks and all real buffers in the trans are on disk.
3914** If no_async, won't return until all commit blocks are on disk.
3915**
3916** keep reading, there are comments as you go along
3917**
3918** If the journal is aborted, we just clean up. Things like flushing
3919** journal lists, etc just won't happen.
3920*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003921static int do_journal_end(struct reiserfs_transaction_handle *th,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003922 struct super_block *sb, unsigned long nblocks,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003923 int flags)
3924{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003925 struct reiserfs_journal *journal = SB_JOURNAL(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003926 struct reiserfs_journal_cnode *cn, *next, *jl_cn;
3927 struct reiserfs_journal_cnode *last_cn = NULL;
3928 struct reiserfs_journal_desc *desc;
3929 struct reiserfs_journal_commit *commit;
3930 struct buffer_head *c_bh; /* commit bh */
3931 struct buffer_head *d_bh; /* desc bh */
3932 int cur_write_start = 0; /* start index of current log write */
3933 int old_start;
3934 int i;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08003935 int flush;
3936 int wait_on_commit;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003937 struct reiserfs_journal_list *jl, *temp_jl;
3938 struct list_head *entry, *safe;
3939 unsigned long jindex;
Jeff Mahoney600ed412009-03-30 14:02:17 -04003940 unsigned int commit_trans_id;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003941 int trans_half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003943 BUG_ON(th->t_refcount > 1);
3944 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08003946 /* protect flush_older_commits from doing mistakes if the
3947 transaction ID counter gets overflowed. */
Jeff Mahoney600ed412009-03-30 14:02:17 -04003948 if (th->t_trans_id == ~0U)
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08003949 flags |= FLUSH_ALL | COMMIT_NOW | WAIT;
3950 flush = flags & FLUSH_ALL;
3951 wait_on_commit = flags & WAIT;
3952
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003953 current->journal_info = th->t_handle_save;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003954 reiserfs_check_lock_depth(sb, "journal end");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003955 if (journal->j_len == 0) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003956 reiserfs_prepare_for_journal(sb, SB_BUFFER_WITH_SB(sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003957 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003958 journal_mark_dirty(th, sb, SB_BUFFER_WITH_SB(sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003961 lock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003962 if (journal->j_next_full_flush) {
3963 flags |= FLUSH_ALL;
3964 flush = 1;
3965 }
3966 if (journal->j_next_async_flush) {
3967 flags |= COMMIT_NOW | WAIT;
3968 wait_on_commit = 1;
3969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Jeff Mahoney0222e652009-03-30 14:02:44 -04003971 /* check_journal_end locks the journal, and unlocks if it does not return 1
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003972 ** it tells us if we should continue with the journal_end, or just return
3973 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04003974 if (!check_journal_end(th, sb, nblocks, flags)) {
3975 sb->s_dirt = 1;
3976 wake_queued_writers(sb);
3977 reiserfs_async_progress_wait(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003978 goto out;
3979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003981 /* check_journal_end might set these, check again */
3982 if (journal->j_next_full_flush) {
3983 flush = 1;
3984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003986 /*
3987 ** j must wait means we have to flush the log blocks, and the real blocks for
3988 ** this transaction
3989 */
3990 if (journal->j_must_wait > 0) {
3991 flush = 1;
3992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993#ifdef REISERFS_PREALLOCATE
Jan Karaef43bc42006-01-11 12:17:40 -08003994 /* quota ops might need to nest, setup the journal_info pointer for them
3995 * and raise the refcount so that it is > 0. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003996 current->journal_info = th;
Jan Karaef43bc42006-01-11 12:17:40 -08003997 th->t_refcount++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07003998 reiserfs_discard_all_prealloc(th); /* it should not involve new blocks into
3999 * the transaction */
Jan Karaef43bc42006-01-11 12:17:40 -08004000 th->t_refcount--;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004001 current->journal_info = th->t_handle_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004004 /* setup description block */
4005 d_bh =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004006 journal_getblk(sb,
4007 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004008 journal->j_start);
4009 set_buffer_uptodate(d_bh);
4010 desc = (struct reiserfs_journal_desc *)(d_bh)->b_data;
4011 memset(d_bh->b_data, 0, d_bh->b_size);
4012 memcpy(get_journal_desc_magic(d_bh), JOURNAL_DESC_MAGIC, 8);
4013 set_desc_trans_id(desc, journal->j_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004015 /* setup commit block. Don't write (keep it clean too) this one until after everyone else is written */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004016 c_bh = journal_getblk(sb, SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004017 ((journal->j_start + journal->j_len +
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004018 1) % SB_ONDISK_JOURNAL_SIZE(sb)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004019 commit = (struct reiserfs_journal_commit *)c_bh->b_data;
4020 memset(c_bh->b_data, 0, c_bh->b_size);
4021 set_commit_trans_id(commit, journal->j_trans_id);
4022 set_buffer_uptodate(c_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004024 /* init this journal list */
4025 jl = journal->j_current_jl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004027 /* we lock the commit before doing anything because
4028 * we want to make sure nobody tries to run flush_commit_list until
4029 * the new transaction is fully setup, and we've already flushed the
4030 * ordered bh list
4031 */
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02004032 reiserfs_mutex_lock_safe(&jl->j_commit_mutex, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004034 /* save the transaction id in case we need to commit it later */
4035 commit_trans_id = jl->j_trans_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004037 atomic_set(&jl->j_older_commits_done, 0);
4038 jl->j_trans_id = journal->j_trans_id;
4039 jl->j_timestamp = journal->j_trans_start_time;
4040 jl->j_commit_bh = c_bh;
4041 jl->j_start = journal->j_start;
4042 jl->j_len = journal->j_len;
4043 atomic_set(&jl->j_nonzerolen, journal->j_len);
4044 atomic_set(&jl->j_commit_left, journal->j_len + 2);
4045 jl->j_realblock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004047 /* The ENTIRE FOR LOOP MUST not cause schedule to occur.
4048 ** for each real block, add it to the journal list hash,
4049 ** copy into real block index array in the commit or desc block
4050 */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004051 trans_half = journal_trans_half(sb->s_blocksize);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004052 for (i = 0, cn = journal->j_first; cn; cn = cn->next, i++) {
4053 if (buffer_journaled(cn->bh)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004054 jl_cn = get_cnode(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004055 if (!jl_cn) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004056 reiserfs_panic(sb, "journal-1676",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04004057 "get_cnode returned NULL");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004058 }
4059 if (i == 0) {
4060 jl->j_realblock = jl_cn;
4061 }
4062 jl_cn->prev = last_cn;
4063 jl_cn->next = NULL;
4064 if (last_cn) {
4065 last_cn->next = jl_cn;
4066 }
4067 last_cn = jl_cn;
Jeff Mahoney0222e652009-03-30 14:02:44 -04004068 /* make sure the block we are trying to log is not a block
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004069 of journal or reserved area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004071 if (is_block_in_log_or_reserved_area
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004072 (sb, cn->bh->b_blocknr)) {
4073 reiserfs_panic(sb, "journal-2332",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04004074 "Trying to log block %lu, "
4075 "which is a log block",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004076 cn->bh->b_blocknr);
4077 }
4078 jl_cn->blocknr = cn->bh->b_blocknr;
4079 jl_cn->state = 0;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004080 jl_cn->sb = sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004081 jl_cn->bh = cn->bh;
4082 jl_cn->jlist = jl;
4083 insert_journal_hash(journal->j_list_hash_table, jl_cn);
4084 if (i < trans_half) {
4085 desc->j_realblock[i] =
4086 cpu_to_le32(cn->bh->b_blocknr);
4087 } else {
4088 commit->j_realblock[i - trans_half] =
4089 cpu_to_le32(cn->bh->b_blocknr);
4090 }
4091 } else {
4092 i--;
4093 }
4094 }
4095 set_desc_trans_len(desc, journal->j_len);
4096 set_desc_mount_id(desc, journal->j_mount_id);
4097 set_desc_trans_id(desc, journal->j_trans_id);
4098 set_commit_trans_len(commit, journal->j_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004100 /* special check in case all buffers in the journal were marked for not logging */
Eric Sesterhenn14a61442006-10-03 23:36:38 +02004101 BUG_ON(journal->j_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004103 /* we're about to dirty all the log blocks, mark the description block
4104 * dirty now too. Don't mark the commit block dirty until all the
4105 * others are on disk
4106 */
4107 mark_buffer_dirty(d_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004109 /* first data block is j_start + 1, so add one to cur_write_start wherever you use it */
4110 cur_write_start = journal->j_start;
4111 cn = journal->j_first;
4112 jindex = 1; /* start at one so we don't get the desc again */
4113 while (cn) {
4114 clear_buffer_journal_new(cn->bh);
4115 /* copy all the real blocks into log area. dirty log blocks */
4116 if (buffer_journaled(cn->bh)) {
4117 struct buffer_head *tmp_bh;
4118 char *addr;
4119 struct page *page;
4120 tmp_bh =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004121 journal_getblk(sb,
4122 SB_ONDISK_JOURNAL_1st_BLOCK(sb) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004123 ((cur_write_start +
4124 jindex) %
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004125 SB_ONDISK_JOURNAL_SIZE(sb)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004126 set_buffer_uptodate(tmp_bh);
4127 page = cn->bh->b_page;
4128 addr = kmap(page);
4129 memcpy(tmp_bh->b_data,
4130 addr + offset_in_page(cn->bh->b_data),
4131 cn->bh->b_size);
4132 kunmap(page);
4133 mark_buffer_dirty(tmp_bh);
4134 jindex++;
4135 set_buffer_journal_dirty(cn->bh);
4136 clear_buffer_journaled(cn->bh);
4137 } else {
4138 /* JDirty cleared sometime during transaction. don't log this one */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004139 reiserfs_warning(sb, "journal-2048",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04004140 "BAD, buffer in journal hash, "
4141 "but not JDirty!");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004142 brelse(cn->bh);
4143 }
4144 next = cn->next;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004145 free_cnode(sb, cn);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004146 cn = next;
Frederic Weisbeckere6950a42009-04-30 23:04:32 +02004147 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004148 cond_resched();
Frederic Weisbeckere6950a42009-04-30 23:04:32 +02004149 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004152 /* we are done with both the c_bh and d_bh, but
4153 ** c_bh must be written after all other commit blocks,
4154 ** so we dirty/relse c_bh in flush_commit_list, with commit_left <= 1.
4155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004157 journal->j_current_jl = alloc_journal_list(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004159 /* now it is safe to insert this transaction on the main list */
4160 list_add_tail(&jl->j_list, &journal->j_journal_list);
4161 list_add_tail(&jl->j_working_list, &journal->j_working_list);
4162 journal->j_num_work_lists++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004164 /* reset journal values for the next transaction */
4165 old_start = journal->j_start;
4166 journal->j_start =
4167 (journal->j_start + journal->j_len +
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004168 2) % SB_ONDISK_JOURNAL_SIZE(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004169 atomic_set(&(journal->j_wcount), 0);
4170 journal->j_bcount = 0;
4171 journal->j_last = NULL;
4172 journal->j_first = NULL;
4173 journal->j_len = 0;
4174 journal->j_trans_start_time = 0;
Alexander Zarochentseva44c94a2006-03-25 03:06:59 -08004175 /* check for trans_id overflow */
4176 if (++journal->j_trans_id == 0)
4177 journal->j_trans_id = 10;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004178 journal->j_current_jl->j_trans_id = journal->j_trans_id;
4179 journal->j_must_wait = 0;
4180 journal->j_len_alloc = 0;
4181 journal->j_next_full_flush = 0;
4182 journal->j_next_async_flush = 0;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004183 init_journal_hash(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004185 // make sure reiserfs_add_jh sees the new current_jl before we
4186 // write out the tails
4187 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004189 /* tail conversion targets have to hit the disk before we end the
4190 * transaction. Otherwise a later transaction might repack the tail
4191 * before this transaction commits, leaving the data block unflushed and
4192 * clean, if we crash before the later transaction commits, the data block
4193 * is lost.
4194 */
4195 if (!list_empty(&jl->j_tail_bh_list)) {
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02004196 reiserfs_write_unlock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004197 write_ordered_buffers(&journal->j_dirty_buffers_lock,
4198 journal, jl, &jl->j_tail_bh_list);
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +02004199 reiserfs_write_lock(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004200 }
Eric Sesterhenn14a61442006-10-03 23:36:38 +02004201 BUG_ON(!list_empty(&jl->j_tail_bh_list));
Jeff Mahoney90415de2008-07-25 01:46:40 -07004202 mutex_unlock(&jl->j_commit_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004204 /* honor the flush wishes from the caller, simple commits can
4205 ** be done outside the journal lock, they are done below
4206 **
4207 ** if we don't flush the commit list right now, we put it into
4208 ** the work queue so the people waiting on the async progress work
4209 ** queue don't wait for this proc to flush journal lists and such.
4210 */
4211 if (flush) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004212 flush_commit_list(sb, jl, 1);
4213 flush_journal_list(sb, jl, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004214 } else if (!(jl->j_state & LIST_COMMIT_PENDING))
4215 queue_delayed_work(commit_wq, &journal->j_work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Jeff Mahoney0222e652009-03-30 14:02:44 -04004217 /* if the next transaction has any chance of wrapping, flush
4218 ** transactions that might get overwritten. If any journal lists are very
4219 ** old flush them as well.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004220 */
4221 first_jl:
4222 list_for_each_safe(entry, safe, &journal->j_journal_list) {
4223 temp_jl = JOURNAL_LIST_ENTRY(entry);
4224 if (journal->j_start <= temp_jl->j_start) {
4225 if ((journal->j_start + journal->j_trans_max + 1) >=
4226 temp_jl->j_start) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004227 flush_used_journal_lists(sb, temp_jl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004228 goto first_jl;
4229 } else if ((journal->j_start +
4230 journal->j_trans_max + 1) <
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004231 SB_ONDISK_JOURNAL_SIZE(sb)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004232 /* if we don't cross into the next transaction and we don't
4233 * wrap, there is no way we can overlap any later transactions
4234 * break now
4235 */
4236 break;
4237 }
4238 } else if ((journal->j_start +
4239 journal->j_trans_max + 1) >
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004240 SB_ONDISK_JOURNAL_SIZE(sb)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004241 if (((journal->j_start + journal->j_trans_max + 1) %
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004242 SB_ONDISK_JOURNAL_SIZE(sb)) >=
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004243 temp_jl->j_start) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004244 flush_used_journal_lists(sb, temp_jl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004245 goto first_jl;
4246 } else {
4247 /* we don't overlap anything from out start to the end of the
4248 * log, and our wrapped portion doesn't overlap anything at
4249 * the start of the log. We can break
4250 */
4251 break;
4252 }
4253 }
4254 }
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004255 flush_old_journal_lists(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004257 journal->j_current_jl->j_list_bitmap =
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004258 get_list_bitmap(sb, journal->j_current_jl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004260 if (!(journal->j_current_jl->j_list_bitmap)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004261 reiserfs_panic(sb, "journal-1996",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04004262 "could not get a list bitmap");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004265 atomic_set(&(journal->j_jlock), 0);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004266 unlock_journal(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004267 /* wake up any body waiting to join. */
4268 clear_bit(J_WRITERS_QUEUED, &journal->j_state);
4269 wake_up(&(journal->j_join_wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004271 if (!flush && wait_on_commit &&
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004272 journal_list_still_alive(sb, commit_trans_id)) {
4273 flush_commit_list(sb, jl, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004274 }
4275 out:
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004276 reiserfs_check_lock_depth(sb, "journal end2");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004278 memset(th, 0, sizeof(*th));
4279 /* Re-set th->t_super, so we can properly keep track of how many
4280 * persistent transactions there are. We need to do this so if this
4281 * call is part of a failed restart_transaction, we can free it later */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04004282 th->t_super = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004284 return journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285}
4286
Jeff Mahoney32e8b102009-03-30 14:02:26 -04004287/* Send the file system read only and refuse new transactions */
4288void reiserfs_abort_journal(struct super_block *sb, int errno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004290 struct reiserfs_journal *journal = SB_JOURNAL(sb);
4291 if (test_bit(J_ABORTED, &journal->j_state))
4292 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Jeff Mahoney32e8b102009-03-30 14:02:26 -04004294 if (!journal->j_errno)
4295 journal->j_errno = errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004297 sb->s_flags |= MS_RDONLY;
4298 set_bit(J_ABORTED, &journal->j_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07004301 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302#endif
4303}