]>
Commit | Line | Data |
---|---|---|
17390c04 TT |
1 | /* |
2 | * journal.c --- code for handling the "ext3" journal | |
3b5386dc TT |
3 | * |
4 | * Copyright (C) 2000 Andreas Dilger | |
5 | * Copyright (C) 2000 Theodore Ts'o | |
6 | * | |
7 | * Parts of the code are based on fs/jfs/journal.c by Stephen C. Tweedie | |
8 | * Copyright (C) 1999 Red Hat Software | |
9 | * | |
10 | * This file may be redistributed under the terms of the | |
11 | * GNU General Public License version 2 or at your discretion | |
12 | * any later version. | |
17390c04 TT |
13 | */ |
14 | ||
3b5386dc TT |
15 | #ifdef HAVE_SYS_MOUNT_H |
16 | #include <sys/mount.h> | |
17 | #define MNT_FL (MS_MGC_VAL | MS_RDONLY) | |
18 | #endif | |
19 | #ifdef HAVE_SYS_STAT_H | |
20 | #include <sys/stat.h> | |
21 | #endif | |
17390c04 | 22 | |
53ef44c4 | 23 | #define E2FSCK_INCLUDE_INLINE_FUNCS |
0e8a9560 | 24 | #include "jfs_user.h" |
3b5386dc TT |
25 | #include "problem.h" |
26 | #include "uuid/uuid.h" | |
17390c04 | 27 | |
8cf93332 | 28 | #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */ |
3b5386dc | 29 | static int bh_count = 0; |
328a5ce4 | 30 | int journal_enable_debug = 2; |
3b5386dc TT |
31 | #endif |
32 | ||
b2f93192 TT |
33 | /* Kernel compatibility functions for handling the journal. These allow us |
34 | * to use the recovery.c file virtually unchanged from the kernel, so we | |
35 | * don't have to do much to keep kernel and user recovery in sync. | |
36 | */ | |
8cf93332 | 37 | int journal_bmap(journal_t *journal, blk_t block, unsigned long *phys) |
3b5386dc | 38 | { |
8cf93332 TT |
39 | struct inode *inode = journal->j_inode; |
40 | errcode_t retval; | |
41 | blk_t pblk; | |
3b5386dc | 42 | |
8cf93332 TT |
43 | if (!inode) { |
44 | *phys = block; | |
45 | return 0; | |
46 | } | |
3b5386dc | 47 | |
8cf93332 TT |
48 | retval= ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, |
49 | &inode->i_ext2, NULL, 0, block, &pblk); | |
50 | *phys = pblk; | |
51 | return (retval); | |
3b5386dc TT |
52 | } |
53 | ||
8cf93332 | 54 | struct buffer_head *getblk(kdev_t kdev, blk_t blocknr, int blocksize) |
3b5386dc TT |
55 | { |
56 | struct buffer_head *bh; | |
57 | ||
8cf93332 | 58 | bh = e2fsck_allocate_memory(kdev->k_ctx, sizeof(*bh), "block buffer"); |
3b5386dc TT |
59 | if (!bh) |
60 | return NULL; | |
61 | ||
62 | jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n", | |
53ef44c4 | 63 | (unsigned long) blocknr, blocksize, ++bh_count); |
3b5386dc | 64 | |
8cf93332 TT |
65 | bh->b_ctx = kdev->k_ctx; |
66 | if (kdev->k_dev == K_DEV_FS) | |
67 | bh->b_io = kdev->k_ctx->fs->io; | |
68 | else | |
69 | bh->b_io = kdev->k_ctx->journal_io; | |
3b5386dc TT |
70 | bh->b_size = blocksize; |
71 | bh->b_blocknr = blocknr; | |
72 | ||
73 | return bh; | |
74 | } | |
75 | ||
0e8a9560 | 76 | void ll_rw_block(int rw, int nr, struct buffer_head *bhp[]) |
3b5386dc TT |
77 | { |
78 | int retval; | |
0e8a9560 | 79 | struct buffer_head *bh; |
3b5386dc | 80 | |
0e8a9560 TT |
81 | for (; nr > 0; --nr) { |
82 | bh = *bhp++; | |
83 | if (rw == READ && !bh->b_uptodate) { | |
84 | jfs_debug(3, "reading block %lu/%p\n", | |
53ef44c4 | 85 | (unsigned long) bh->b_blocknr, (void *) bh); |
8cf93332 | 86 | retval = io_channel_read_blk(bh->b_io, |
0e8a9560 TT |
87 | bh->b_blocknr, |
88 | 1, bh->b_data); | |
89 | if (retval) { | |
90 | com_err(bh->b_ctx->device_name, retval, | |
91 | "while reading block %ld\n", | |
92 | bh->b_blocknr); | |
93 | bh->b_err = retval; | |
94 | continue; | |
95 | } | |
96 | bh->b_uptodate = 1; | |
97 | } else if (rw == WRITE && bh->b_dirty) { | |
98 | jfs_debug(3, "writing block %lu/%p\n", | |
53ef44c4 | 99 | (unsigned long) bh->b_blocknr, (void *) bh); |
8cf93332 | 100 | retval = io_channel_write_blk(bh->b_io, |
0e8a9560 TT |
101 | bh->b_blocknr, |
102 | 1, bh->b_data); | |
103 | if (retval) { | |
104 | com_err(bh->b_ctx->device_name, retval, | |
105 | "while writing block %ld\n", | |
106 | bh->b_blocknr); | |
107 | bh->b_err = retval; | |
108 | continue; | |
109 | } | |
110 | bh->b_dirty = 0; | |
111 | bh->b_uptodate = 1; | |
112 | } else | |
113 | jfs_debug(3, "no-op %s for block %lu\n", | |
114 | rw == READ ? "read" : "write", | |
53ef44c4 | 115 | (unsigned long) bh->b_blocknr); |
0e8a9560 | 116 | } |
3b5386dc TT |
117 | } |
118 | ||
8cf93332 | 119 | void mark_buffer_dirty(struct buffer_head *bh) |
3b5386dc | 120 | { |
8cf93332 | 121 | bh->b_dirty = 1; |
3b5386dc TT |
122 | } |
123 | ||
6a6d3d44 TT |
124 | static void mark_buffer_clean(struct buffer_head * bh) |
125 | { | |
126 | bh->b_dirty = 0; | |
127 | } | |
128 | ||
3b5386dc TT |
129 | void brelse(struct buffer_head *bh) |
130 | { | |
131 | if (bh->b_dirty) | |
0e8a9560 | 132 | ll_rw_block(WRITE, 1, &bh); |
3b5386dc | 133 | jfs_debug(3, "freeing block %lu/%p (total %d)\n", |
53ef44c4 | 134 | (unsigned long) bh->b_blocknr, (void *) bh, --bh_count); |
3b5386dc TT |
135 | ext2fs_free_mem((void **) &bh); |
136 | } | |
137 | ||
138 | int buffer_uptodate(struct buffer_head *bh) | |
139 | { | |
140 | return bh->b_uptodate; | |
141 | } | |
142 | ||
15986f79 TT |
143 | void mark_buffer_uptodate(struct buffer_head *bh, int val) |
144 | { | |
145 | bh->b_uptodate = val; | |
146 | } | |
147 | ||
3b5386dc TT |
148 | void wait_on_buffer(struct buffer_head *bh) |
149 | { | |
150 | if (!bh->b_uptodate) | |
0e8a9560 | 151 | ll_rw_block(READ, 1, &bh); |
3b5386dc TT |
152 | } |
153 | ||
80bfaa3e | 154 | |
3b5386dc TT |
155 | static void e2fsck_clear_recover(e2fsck_t ctx, int error) |
156 | { | |
5dd8f963 | 157 | ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER; |
3b5386dc TT |
158 | |
159 | /* if we had an error doing journal recovery, we need a full fsck */ | |
160 | if (error) | |
5dd8f963 | 161 | ctx->fs->super->s_state &= ~EXT2_VALID_FS; |
3b5386dc TT |
162 | ext2fs_mark_super_dirty(ctx->fs); |
163 | } | |
164 | ||
6a6d3d44 TT |
165 | static errcode_t e2fsck_journal_init_inode(e2fsck_t ctx, |
166 | struct ext2_super_block *s, | |
6a6d3d44 | 167 | journal_t **journal) |
3b5386dc | 168 | { |
8cf93332 | 169 | struct inode *inode = NULL; |
3b5386dc | 170 | struct buffer_head *bh; |
8cf93332 | 171 | unsigned long start; |
3b5386dc | 172 | int retval; |
8cf93332 | 173 | struct kdev_s *dev_fs = NULL, *dev_journal; |
3b5386dc | 174 | |
adee8d75 | 175 | jfs_debug(1, "Using journal inode %u\n", s->s_journal_inum); |
3b5386dc TT |
176 | *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal"); |
177 | if (!*journal) { | |
178 | return EXT2_ET_NO_MEMORY; | |
179 | } | |
180 | ||
8cf93332 TT |
181 | dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev"); |
182 | if (!dev_fs) { | |
183 | retval = EXT2_ET_NO_MEMORY; | |
184 | goto errout; | |
185 | } | |
186 | dev_journal = dev_fs+1; | |
187 | ||
3b5386dc TT |
188 | inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode"); |
189 | if (!inode) { | |
190 | retval = EXT2_ET_NO_MEMORY; | |
8cf93332 | 191 | goto errout; |
3b5386dc TT |
192 | } |
193 | ||
194 | inode->i_ctx = ctx; | |
adee8d75 TT |
195 | inode->i_ino = s->s_journal_inum; |
196 | retval = ext2fs_read_inode(ctx->fs, s->s_journal_inum, &inode->i_ext2); | |
3b5386dc | 197 | if (retval) |
8cf93332 | 198 | goto errout; |
3b5386dc | 199 | |
8cf93332 TT |
200 | dev_fs->k_ctx = dev_journal->k_ctx = ctx; |
201 | dev_fs->k_dev = K_DEV_FS; | |
202 | dev_journal->k_dev = K_DEV_JOURNAL; | |
203 | ||
204 | (*journal)->j_dev = dev_journal; | |
205 | (*journal)->j_fs_dev = dev_fs; | |
3b5386dc TT |
206 | (*journal)->j_inode = inode; |
207 | (*journal)->j_blocksize = ctx->fs->blocksize; | |
208 | (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize; | |
adee8d75 | 209 | ctx->journal_io = ctx->fs->io; |
3b5386dc TT |
210 | |
211 | if (!inode->i_ext2.i_links_count || | |
212 | !LINUX_S_ISREG(inode->i_ext2.i_mode) || | |
213 | (*journal)->j_maxlen < JFS_MIN_JOURNAL_BLOCKS || | |
8cf93332 TT |
214 | (retval = journal_bmap(*journal, 0, &start)) != 0) { |
215 | goto errout; | |
3b5386dc TT |
216 | } |
217 | ||
8cf93332 | 218 | bh = getblk(dev_journal, start, (*journal)->j_blocksize); |
3b5386dc TT |
219 | if (!bh) { |
220 | retval = EXT2_ET_NO_MEMORY; | |
8cf93332 | 221 | goto errout; |
3b5386dc TT |
222 | } |
223 | (*journal)->j_sb_buffer = bh; | |
224 | (*journal)->j_superblock = (journal_superblock_t *)bh->b_data; | |
adee8d75 | 225 | |
3b5386dc TT |
226 | return 0; |
227 | ||
8cf93332 TT |
228 | errout: |
229 | if (dev_fs) | |
230 | ext2fs_free_mem((void **)&dev_fs); | |
231 | if (inode) | |
232 | ext2fs_free_mem((void **)&inode); | |
233 | if (journal) | |
234 | ext2fs_free_mem((void **)journal); | |
3b5386dc TT |
235 | return retval; |
236 | } | |
237 | ||
adee8d75 TT |
238 | static errcode_t e2fsck_journal_init_dev(e2fsck_t ctx, |
239 | struct ext2_super_block *s, | |
240 | journal_t **journal) | |
3b5386dc | 241 | { |
adee8d75 TT |
242 | struct buffer_head *bh; |
243 | io_manager io_ptr; | |
244 | blk_t start; | |
245 | int retval; | |
246 | int blocksize = ctx->fs->blocksize; | |
247 | struct ext2_super_block jsuper; | |
3b5386dc | 248 | struct problem_context pctx; |
6d222f32 | 249 | const char *journal_name; |
8cf93332 | 250 | struct kdev_s *dev_fs = NULL, *dev_journal; |
3b5386dc | 251 | |
8cf93332 TT |
252 | dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev"); |
253 | if (!dev_fs) { | |
254 | return EXT2_ET_NO_MEMORY; | |
255 | } | |
256 | dev_journal = dev_fs+1; | |
257 | dev_fs->k_ctx = dev_journal->k_ctx = ctx; | |
258 | dev_fs->k_dev = K_DEV_FS; | |
259 | dev_journal->k_dev = K_DEV_JOURNAL; | |
260 | ||
3b5386dc | 261 | clear_problem_context(&pctx); |
6d222f32 TT |
262 | journal_name = ctx->journal_name; |
263 | if (!journal_name) | |
264 | journal_name = ext2fs_find_block_device(s->s_journal_dev); | |
3b5386dc | 265 | |
6d222f32 | 266 | if (!journal_name) { |
adee8d75 TT |
267 | fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx); |
268 | return EXT2_ET_LOAD_EXT_JOURNAL; | |
3b5386dc TT |
269 | } |
270 | ||
6d222f32 | 271 | jfs_debug(1, "Using journal file %s\n", journal_name); |
adee8d75 TT |
272 | |
273 | #if 1 | |
274 | io_ptr = unix_io_manager; | |
275 | #else | |
276 | io_ptr = test_io_manager; | |
277 | test_io_backing_manager = unix_io_manager; | |
278 | #endif | |
6d222f32 TT |
279 | retval = io_ptr->open(journal_name, IO_FLAG_RW, &ctx->journal_io); |
280 | if (!ctx->journal_name) | |
281 | free((void *) journal_name); | |
282 | if (retval) | |
adee8d75 TT |
283 | return retval; |
284 | ||
285 | io_channel_set_blksize(ctx->journal_io, blocksize); | |
286 | start = (blocksize == 1024) ? 1 : 0; | |
8cf93332 | 287 | bh = getblk(dev_journal, start, blocksize); |
adee8d75 TT |
288 | if (!bh) |
289 | return EXT2_ET_NO_MEMORY; | |
290 | ll_rw_block(READ, 1, &bh); | |
291 | if (bh->b_err) | |
292 | return bh->b_err; | |
293 | memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024, | |
294 | sizeof(jsuper)); | |
295 | brelse(bh); | |
296 | #ifdef EXT2FS_ENABLE_SWAPFS | |
297 | if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) | |
298 | ext2fs_swap_super(&jsuper); | |
299 | #endif | |
300 | if (jsuper.s_magic != EXT2_SUPER_MAGIC || | |
301 | !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { | |
302 | fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx); | |
303 | return EXT2_ET_LOAD_EXT_JOURNAL; | |
3b5386dc | 304 | } |
adee8d75 TT |
305 | /* Make sure the journal UUID is correct */ |
306 | if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid, | |
307 | sizeof(jsuper.s_uuid))) { | |
308 | fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx); | |
309 | return EXT2_ET_LOAD_EXT_JOURNAL; | |
310 | } | |
311 | ||
312 | *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal"); | |
313 | if (!*journal) { | |
314 | return EXT2_ET_NO_MEMORY; | |
3b5386dc TT |
315 | } |
316 | ||
8cf93332 TT |
317 | (*journal)->j_dev = dev_journal; |
318 | (*journal)->j_fs_dev = dev_fs; | |
adee8d75 TT |
319 | (*journal)->j_inode = NULL; |
320 | (*journal)->j_blocksize = ctx->fs->blocksize; | |
321 | (*journal)->j_maxlen = jsuper.s_blocks_count; | |
322 | ||
8cf93332 | 323 | bh = getblk(dev_journal, start+1, (*journal)->j_blocksize); |
adee8d75 TT |
324 | if (!bh) { |
325 | retval = EXT2_ET_NO_MEMORY; | |
326 | goto errout; | |
327 | } | |
328 | (*journal)->j_sb_buffer = bh; | |
329 | (*journal)->j_superblock = (journal_superblock_t *)bh->b_data; | |
330 | ||
331 | return 0; | |
332 | ||
333 | errout: | |
8cf93332 | 334 | ext2fs_free_mem((void **)&dev_fs); |
adee8d75 TT |
335 | ext2fs_free_mem((void **)journal); |
336 | return retval; | |
337 | } | |
338 | ||
339 | static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **journal) | |
340 | { | |
341 | struct ext2_super_block *sb = ctx->fs->super; | |
342 | ||
343 | if (uuid_is_null(sb->s_journal_uuid)) { | |
344 | if (!sb->s_journal_inum) | |
345 | return EXT2_ET_BAD_INODE_NUM; | |
346 | return e2fsck_journal_init_inode(ctx, sb, journal); | |
347 | } else { | |
348 | return e2fsck_journal_init_dev(ctx, sb, journal); | |
349 | } | |
3b5386dc TT |
350 | } |
351 | ||
6a6d3d44 TT |
352 | static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx, |
353 | struct problem_context *pctx) | |
3b5386dc | 354 | { |
5dd8f963 TT |
355 | struct ext2_super_block *sb = ctx->fs->super; |
356 | int recover = ctx->fs->super->s_feature_incompat & | |
357 | EXT3_FEATURE_INCOMPAT_RECOVER; | |
358 | int has_journal = ctx->fs->super->s_feature_compat & | |
359 | EXT3_FEATURE_COMPAT_HAS_JOURNAL; | |
3b5386dc | 360 | |
5dd8f963 | 361 | if (has_journal || sb->s_journal_inum) { |
3b5386dc | 362 | /* The journal inode is bogus, remove and force full fsck */ |
7e92dfae | 363 | pctx->ino = sb->s_journal_inum; |
3b5386dc | 364 | if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) { |
5dd8f963 | 365 | if (has_journal && sb->s_journal_inum) |
3b5386dc TT |
366 | printf("*** ext3 journal has been deleted - " |
367 | "filesystem is now ext2 only ***\n\n"); | |
5dd8f963 TT |
368 | sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
369 | sb->s_journal_inum = 0; | |
424cd2be | 370 | ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */ |
3b5386dc TT |
371 | e2fsck_clear_recover(ctx, 1); |
372 | return 0; | |
373 | } | |
374 | return EXT2_ET_BAD_INODE_NUM; | |
375 | } else if (recover) { | |
376 | if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) { | |
377 | e2fsck_clear_recover(ctx, 1); | |
378 | return 0; | |
379 | } | |
380 | return EXT2_ET_UNSUPP_FEATURE; | |
381 | } | |
382 | return 0; | |
383 | } | |
384 | ||
62e3e7fe TT |
385 | #define V1_SB_SIZE 0x0024 |
386 | static void clear_v2_journal_fields(journal_t *journal) | |
387 | { | |
8cf93332 | 388 | e2fsck_t ctx = journal->j_dev->k_ctx; |
62e3e7fe TT |
389 | struct problem_context pctx; |
390 | ||
391 | clear_problem_context(&pctx); | |
392 | ||
393 | if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx)) | |
394 | return; | |
395 | ||
396 | memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0, | |
397 | ctx->fs->blocksize-V1_SB_SIZE); | |
8cf93332 | 398 | mark_buffer_dirty(journal->j_sb_buffer); |
62e3e7fe TT |
399 | } |
400 | ||
401 | ||
6a6d3d44 | 402 | static errcode_t e2fsck_journal_load(journal_t *journal) |
3b5386dc | 403 | { |
8cf93332 | 404 | e2fsck_t ctx = journal->j_dev->k_ctx; |
3b5386dc TT |
405 | journal_superblock_t *jsb; |
406 | struct buffer_head *jbh = journal->j_sb_buffer; | |
407 | struct problem_context pctx; | |
408 | ||
409 | clear_problem_context(&pctx); | |
410 | ||
0e8a9560 | 411 | ll_rw_block(READ, 1, &jbh); |
3b5386dc TT |
412 | if (jbh->b_err) { |
413 | com_err(ctx->device_name, jbh->b_err, | |
414 | _("reading journal superblock\n")); | |
415 | return jbh->b_err; | |
416 | } | |
417 | ||
418 | jsb = journal->j_superblock; | |
419 | /* If we don't even have JFS_MAGIC, we probably have a wrong inode */ | |
420 | if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER)) | |
421 | return e2fsck_journal_fix_bad_inode(ctx, &pctx); | |
422 | ||
0e8a9560 TT |
423 | switch (ntohl(jsb->s_header.h_blocktype)) { |
424 | case JFS_SUPERBLOCK_V1: | |
425 | journal->j_format_version = 1; | |
62e3e7fe TT |
426 | if (jsb->s_feature_compat || |
427 | jsb->s_feature_incompat || | |
428 | jsb->s_feature_ro_compat || | |
429 | jsb->s_nr_users) | |
430 | clear_v2_journal_fields(journal); | |
0e8a9560 TT |
431 | break; |
432 | ||
433 | case JFS_SUPERBLOCK_V2: | |
434 | journal->j_format_version = 2; | |
b3b3d465 | 435 | if (ntohl(jsb->s_nr_users) > 1 && |
62e3e7fe TT |
436 | (ctx->fs->io == ctx->journal_io)) |
437 | clear_v2_journal_fields(journal); | |
adee8d75 TT |
438 | if (ntohl(jsb->s_nr_users) > 1) { |
439 | fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx); | |
440 | return EXT2_ET_JOURNAL_UNSUPP_VERSION; | |
441 | } | |
0e8a9560 | 442 | break; |
c7f23364 TT |
443 | |
444 | /* | |
445 | * These should never appear in a journal super block, so if | |
446 | * they do, the journal is badly corrupted. | |
447 | */ | |
448 | case JFS_DESCRIPTOR_BLOCK: | |
449 | case JFS_COMMIT_BLOCK: | |
450 | case JFS_REVOKE_BLOCK: | |
451 | return EXT2_ET_CORRUPT_SUPERBLOCK; | |
0e8a9560 TT |
452 | |
453 | /* If we don't understand the superblock major type, but there | |
454 | * is a magic number, then it is likely to be a new format we | |
455 | * just don't understand, so leave it alone. */ | |
456 | default: | |
c7f23364 | 457 | return EXT2_ET_JOURNAL_UNSUPP_VERSION; |
0e8a9560 TT |
458 | } |
459 | ||
2f686ace TT |
460 | if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) |
461 | return EXT2_ET_UNSUPP_FEATURE; | |
424cd2be | 462 | |
2f686ace TT |
463 | if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) |
464 | return EXT2_ET_RO_UNSUPP_FEATURE; | |
3b5386dc | 465 | |
0e8a9560 TT |
466 | /* We have now checked whether we know enough about the journal |
467 | * format to be able to proceed safely, so any other checks that | |
468 | * fail we should attempt to recover from. */ | |
469 | if (jsb->s_blocksize != htonl(journal->j_blocksize)) { | |
470 | com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK, | |
471 | _("%s: no valid journal superblock found\n"), | |
472 | ctx->device_name); | |
473 | return EXT2_ET_CORRUPT_SUPERBLOCK; | |
3b5386dc TT |
474 | } |
475 | ||
476 | if (ntohl(jsb->s_maxlen) < journal->j_maxlen) | |
477 | journal->j_maxlen = ntohl(jsb->s_maxlen); | |
478 | else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) { | |
0e8a9560 TT |
479 | com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK, |
480 | _("%s: journal too short\n"), | |
481 | ctx->device_name); | |
3b5386dc TT |
482 | return EXT2_ET_CORRUPT_SUPERBLOCK; |
483 | } | |
484 | ||
485 | journal->j_tail_sequence = ntohl(jsb->s_sequence); | |
ecf1b776 | 486 | journal->j_transaction_sequence = journal->j_tail_sequence; |
3b5386dc TT |
487 | journal->j_tail = ntohl(jsb->s_start); |
488 | journal->j_first = ntohl(jsb->s_first); | |
489 | journal->j_last = ntohl(jsb->s_maxlen); | |
490 | ||
491 | return 0; | |
492 | } | |
493 | ||
53ef44c4 | 494 | static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, |
6a6d3d44 | 495 | journal_t *journal) |
3b5386dc | 496 | { |
0e8a9560 | 497 | char *p; |
424cd2be TT |
498 | union { |
499 | uuid_t uuid; | |
500 | __u32 val[4]; | |
501 | } u; | |
502 | __u32 new_seq = 0; | |
503 | int i; | |
504 | ||
0e8a9560 TT |
505 | /* Leave a valid existing V1 superblock signature alone. |
506 | * Anything unrecognisable we overwrite with a new V2 | |
507 | * signature. */ | |
508 | ||
509 | if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) || | |
510 | jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) { | |
511 | jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER); | |
512 | jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2); | |
513 | } | |
514 | ||
515 | /* Zero out everything else beyond the superblock header */ | |
516 | ||
517 | p = ((char *) jsb) + sizeof(journal_header_t); | |
518 | memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t)); | |
519 | ||
3b5386dc | 520 | jsb->s_blocksize = htonl(ctx->fs->blocksize); |
0e8a9560 TT |
521 | jsb->s_maxlen = htonl(journal->j_maxlen); |
522 | jsb->s_first = htonl(1); | |
0e8a9560 | 523 | |
424cd2be TT |
524 | /* Initialize the journal sequence number so that there is "no" |
525 | * chance we will find old "valid" transactions in the journal. | |
526 | * This avoids the need to zero the whole journal (slow to do, | |
527 | * and risky when we are just recovering the filesystem). | |
528 | */ | |
529 | uuid_generate(u.uuid); | |
530 | for (i = 0; i < 4; i ++) | |
531 | new_seq ^= u.val[i]; | |
532 | jsb->s_sequence = htonl(new_seq); | |
0e8a9560 | 533 | |
8cf93332 | 534 | mark_buffer_dirty(journal->j_sb_buffer); |
0e8a9560 | 535 | ll_rw_block(WRITE, 1, &journal->j_sb_buffer); |
3b5386dc TT |
536 | } |
537 | ||
6a6d3d44 TT |
538 | static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, |
539 | journal_t *journal, | |
540 | struct problem_context *pctx) | |
3b5386dc | 541 | { |
5dd8f963 TT |
542 | struct ext2_super_block *sb = ctx->fs->super; |
543 | int recover = ctx->fs->super->s_feature_incompat & | |
544 | EXT3_FEATURE_INCOMPAT_RECOVER; | |
3b5386dc TT |
545 | |
546 | pctx->num = journal->j_inode->i_ino; | |
547 | ||
5dd8f963 | 548 | if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) { |
3b5386dc | 549 | if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) { |
b2f93192 TT |
550 | e2fsck_journal_reset_super(ctx, journal->j_superblock, |
551 | journal); | |
3b5386dc TT |
552 | journal->j_transaction_sequence = 1; |
553 | e2fsck_clear_recover(ctx, recover); | |
554 | return 0; | |
555 | } | |
556 | return EXT2_ET_CORRUPT_SUPERBLOCK; | |
557 | } else if (e2fsck_journal_fix_bad_inode(ctx, pctx)) | |
558 | return EXT2_ET_CORRUPT_SUPERBLOCK; | |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
6a6d3d44 TT |
563 | static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, |
564 | int reset, int drop) | |
3b5386dc TT |
565 | { |
566 | journal_superblock_t *jsb; | |
567 | ||
6a6d3d44 TT |
568 | if (drop) |
569 | mark_buffer_clean(journal->j_sb_buffer); | |
570 | else if (!(ctx->options & E2F_OPT_READONLY)) { | |
3b5386dc TT |
571 | jsb = journal->j_superblock; |
572 | jsb->s_sequence = htonl(journal->j_transaction_sequence); | |
573 | if (reset) | |
574 | jsb->s_start = 0; /* this marks the journal as empty */ | |
8cf93332 | 575 | mark_buffer_dirty(journal->j_sb_buffer); |
3b5386dc TT |
576 | } |
577 | brelse(journal->j_sb_buffer); | |
578 | ||
6d222f32 TT |
579 | if (ctx->journal_io) { |
580 | if (ctx->fs && ctx->fs->io != ctx->journal_io) | |
581 | io_channel_close(ctx->journal_io); | |
582 | ctx->journal_io = 0; | |
583 | } | |
584 | ||
3b5386dc | 585 | if (journal->j_inode) |
6a6d3d44 | 586 | ext2fs_free_mem((void **)&journal->j_inode); |
3b5386dc TT |
587 | ext2fs_free_mem((void **)&journal); |
588 | } | |
589 | ||
9b565759 TT |
590 | /* |
591 | * This function makes sure that the superblock fields regarding the | |
592 | * journal are consistent. | |
593 | */ | |
3b5386dc TT |
594 | int e2fsck_check_ext3_journal(e2fsck_t ctx) |
595 | { | |
5dd8f963 | 596 | struct ext2_super_block *sb = ctx->fs->super; |
3b5386dc | 597 | journal_t *journal; |
5dd8f963 TT |
598 | int recover = ctx->fs->super->s_feature_incompat & |
599 | EXT3_FEATURE_INCOMPAT_RECOVER; | |
3b5386dc | 600 | struct problem_context pctx; |
d37066a9 | 601 | problem_t problem; |
d3f35b64 | 602 | int reset = 0, force_fsck = 0; |
3b5386dc TT |
603 | int retval; |
604 | ||
605 | /* If we don't have any journal features, don't do anything more */ | |
5dd8f963 TT |
606 | if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && |
607 | !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 && | |
608 | uuid_is_null(sb->s_journal_uuid)) | |
9b565759 | 609 | return 0; |
3b5386dc TT |
610 | |
611 | clear_problem_context(&pctx); | |
5dd8f963 | 612 | pctx.num = sb->s_journal_inum; |
3b5386dc TT |
613 | |
614 | retval = e2fsck_get_journal(ctx, &journal); | |
615 | if (retval) { | |
616 | if (retval == EXT2_ET_BAD_INODE_NUM) | |
617 | return e2fsck_journal_fix_bad_inode(ctx, &pctx); | |
618 | return retval; | |
619 | } | |
620 | ||
621 | retval = e2fsck_journal_load(journal); | |
622 | if (retval) { | |
c7f23364 | 623 | if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) || |
2f686ace TT |
624 | ((retval == EXT2_ET_UNSUPP_FEATURE) && |
625 | (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT, | |
626 | &pctx))) || | |
627 | ((retval == EXT2_ET_RO_UNSUPP_FEATURE) && | |
628 | (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT, | |
629 | &pctx))) || | |
c7f23364 TT |
630 | ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) && |
631 | (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx)))) | |
6a6d3d44 TT |
632 | retval = e2fsck_journal_fix_corrupt_super(ctx, journal, |
633 | &pctx); | |
634 | e2fsck_journal_release(ctx, journal, 0, 1); | |
3b5386dc TT |
635 | return retval; |
636 | } | |
637 | ||
638 | /* | |
639 | * We want to make the flags consistent here. We will not leave with | |
640 | * needs_recovery set but has_journal clear. We can't get in a loop | |
641 | * with -y, -n, or -p, only if a user isn't making up their mind. | |
642 | */ | |
643 | no_has_journal: | |
5dd8f963 TT |
644 | if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { |
645 | recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER; | |
3b5386dc TT |
646 | pctx.str = "inode"; |
647 | if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) { | |
648 | if (recover && | |
649 | !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx)) | |
650 | goto no_has_journal; | |
d3f35b64 TT |
651 | /* |
652 | * Need a full fsck if we are releasing a | |
8c2dc521 | 653 | * journal stored on a reserved inode. |
d3f35b64 TT |
654 | */ |
655 | force_fsck = recover || | |
656 | (sb->s_journal_inum < EXT2_FIRST_INODE(sb)); | |
657 | /* Clear all of the journal fields */ | |
5dd8f963 | 658 | sb->s_journal_inum = 0; |
d3f35b64 TT |
659 | sb->s_journal_dev = 0; |
660 | memset(sb->s_journal_uuid, 0, | |
661 | sizeof(sb->s_journal_uuid)); | |
662 | e2fsck_clear_recover(ctx, force_fsck); | |
3b5386dc | 663 | } else if (!(ctx->options & E2F_OPT_READONLY)) { |
5dd8f963 | 664 | sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
3b5386dc TT |
665 | ext2fs_mark_super_dirty(ctx->fs); |
666 | } | |
667 | } | |
668 | ||
5dd8f963 TT |
669 | if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL && |
670 | !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) && | |
3b5386dc | 671 | journal->j_superblock->s_start != 0) { |
d37066a9 TT |
672 | /* Print status information */ |
673 | fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx); | |
674 | if (ctx->superblock) | |
675 | problem = PR_0_JOURNAL_RUN_DEFAULT; | |
676 | else | |
677 | problem = PR_0_JOURNAL_RUN; | |
678 | if (fix_problem(ctx, problem, &pctx)) { | |
679 | ctx->options |= E2F_OPT_FORCE; | |
680 | sb->s_feature_incompat |= | |
681 | EXT3_FEATURE_INCOMPAT_RECOVER; | |
682 | ext2fs_mark_super_dirty(ctx->fs); | |
683 | } else if (fix_problem(ctx, | |
684 | PR_0_JOURNAL_RESET_JOURNAL, &pctx)) { | |
3b5386dc | 685 | reset = 1; |
d3f35b64 TT |
686 | sb->s_state &= ~EXT2_VALID_FS; |
687 | ext2fs_mark_super_dirty(ctx->fs); | |
688 | } | |
689 | /* | |
690 | * If the user answers no to the above question, we | |
691 | * ignore the fact that journal apparently has data; | |
692 | * accidentally replaying over valid data would be far | |
693 | * worse than skipping a questionable recovery. | |
694 | * | |
695 | * XXX should we abort with a fatal error here? What | |
696 | * will the ext3 kernel code do if a filesystem with | |
697 | * !NEEDS_RECOVERY but with a non-zero | |
698 | * journal->j_superblock->s_start is mounted? | |
699 | */ | |
3b5386dc TT |
700 | } |
701 | ||
6a6d3d44 | 702 | e2fsck_journal_release(ctx, journal, reset, 0); |
3b5386dc TT |
703 | return retval; |
704 | } | |
705 | ||
6a6d3d44 | 706 | static errcode_t recover_ext3_journal(e2fsck_t ctx) |
3b5386dc | 707 | { |
3b5386dc TT |
708 | journal_t *journal; |
709 | int retval; | |
710 | ||
8cf93332 | 711 | journal_init_revoke_caches(); |
3b5386dc TT |
712 | retval = e2fsck_get_journal(ctx, &journal); |
713 | if (retval) | |
ecf1b776 TT |
714 | return retval; |
715 | ||
3b5386dc TT |
716 | retval = e2fsck_journal_load(journal); |
717 | if (retval) | |
6a6d3d44 | 718 | goto errout; |
3b5386dc | 719 | |
0e8a9560 TT |
720 | retval = journal_init_revoke(journal, 1024); |
721 | if (retval) | |
6a6d3d44 | 722 | goto errout; |
0e8a9560 | 723 | |
3b5386dc | 724 | retval = -journal_recover(journal); |
c0a083fa TT |
725 | if (retval) |
726 | goto errout; | |
727 | ||
728 | if (journal->j_superblock->s_errno) { | |
729 | ctx->fs->super->s_state |= EXT2_ERROR_FS; | |
730 | ext2fs_mark_super_dirty(ctx->fs); | |
731 | journal->j_superblock->s_errno = 0; | |
8cf93332 | 732 | mark_buffer_dirty(journal->j_sb_buffer); |
c0a083fa TT |
733 | } |
734 | ||
6a6d3d44 | 735 | errout: |
8cf93332 | 736 | journal_destroy_revoke_caches(); |
6a6d3d44 | 737 | e2fsck_journal_release(ctx, journal, 1, 0); |
3b5386dc TT |
738 | return retval; |
739 | } | |
740 | ||
80bfaa3e TT |
741 | int e2fsck_run_ext3_journal(e2fsck_t ctx) |
742 | { | |
743 | io_manager io_ptr = ctx->fs->io->manager; | |
744 | int blocksize = ctx->fs->blocksize; | |
ecf1b776 | 745 | errcode_t retval, recover_retval; |
8394902e TT |
746 | |
747 | printf(_("%s: recovering journal\n"), ctx->device_name); | |
ecf1b776 | 748 | if (ctx->options & E2F_OPT_READONLY) { |
8394902e | 749 | printf(_("%s: won't do journal recovery while read-only\n"), |
ecf1b776 TT |
750 | ctx->device_name); |
751 | return EXT2_ET_FILE_RO; | |
752 | } | |
753 | ||
0f2cfe25 | 754 | if (ctx->fs->flags & EXT2_FLAG_DIRTY) |
3c6b8977 | 755 | ext2fs_flush(ctx->fs); /* Force out any modifications */ |
d0515212 | 756 | |
ecf1b776 | 757 | recover_retval = recover_ext3_journal(ctx); |
80bfaa3e TT |
758 | |
759 | /* | |
760 | * Reload the filesystem context to get up-to-date data from disk | |
761 | * because journal recovery will change the filesystem under us. | |
762 | */ | |
763 | ext2fs_close(ctx->fs); | |
ecf1b776 | 764 | retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW, |
80bfaa3e TT |
765 | ctx->superblock, blocksize, io_ptr, |
766 | &ctx->fs); | |
767 | ||
768 | if (retval) { | |
769 | com_err(ctx->program_name, retval, | |
770 | _("while trying to re-open %s"), | |
771 | ctx->device_name); | |
99a2cc96 | 772 | fatal_error(ctx, 0); |
80bfaa3e TT |
773 | } |
774 | ctx->fs->priv_data = ctx; | |
17390c04 | 775 | |
ecf1b776 TT |
776 | /* Set the superblock flags */ |
777 | e2fsck_clear_recover(ctx, recover_retval); | |
778 | return recover_retval; | |
17390c04 | 779 | } |
773fd8a1 TT |
780 | |
781 | /* | |
782 | * This function will move the journal inode from a visible file in | |
783 | * the filesystem directory hierarchy to the reserved inode if necessary. | |
784 | */ | |
785 | const static char * const journal_names[] = { | |
786 | ".journal", "journal", ".journal.dat", "journal.dat", 0 }; | |
787 | ||
788 | void e2fsck_move_ext3_journal(e2fsck_t ctx) | |
789 | { | |
790 | struct ext2_super_block *sb = ctx->fs->super; | |
791 | struct problem_context pctx; | |
792 | struct ext2_inode inode; | |
793 | ext2_filsys fs = ctx->fs; | |
794 | ext2_ino_t ino; | |
795 | errcode_t retval; | |
796 | const char * const * cpp; | |
797 | int group, mount_flags; | |
798 | ||
799 | /* | |
800 | * If the filesystem is opened read-only, or there is no | |
801 | * journal, or the journal is already in the hidden inode, | |
802 | * then do nothing. | |
803 | */ | |
804 | if ((ctx->options & E2F_OPT_READONLY) || | |
805 | (sb->s_journal_inum == 0) || | |
806 | (sb->s_journal_inum == EXT2_JOURNAL_INO) || | |
807 | !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) | |
808 | return; | |
809 | ||
810 | /* | |
811 | * If the filesystem is mounted, or we can't tell whether | |
812 | * or not it's mounted, do nothing. | |
813 | */ | |
814 | retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); | |
815 | if (retval || (mount_flags & EXT2_MF_MOUNTED)) | |
816 | return; | |
817 | ||
818 | /* | |
819 | * If we can't find the name of the journal inode, then do | |
820 | * nothing. | |
821 | */ | |
822 | for (cpp = journal_names; *cpp; cpp++) { | |
823 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp, | |
824 | strlen(*cpp), 0, &ino); | |
825 | if ((retval == 0) && (ino == sb->s_journal_inum)) | |
826 | break; | |
827 | } | |
828 | if (*cpp == 0) | |
829 | return; | |
830 | ||
831 | /* | |
832 | * The inode had better have only one link and not be readable. | |
833 | */ | |
834 | if (ext2fs_read_inode(fs, ino, &inode) != 0) | |
835 | return; | |
836 | if (inode.i_links_count != 1) | |
837 | return; | |
838 | ||
839 | /* We need the inode bitmap to be loaded */ | |
840 | retval = ext2fs_read_bitmaps(fs); | |
841 | if (retval) | |
842 | return; | |
843 | ||
844 | clear_problem_context(&pctx); | |
845 | pctx.str = *cpp; | |
846 | if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx)) | |
847 | return; | |
848 | ||
849 | /* | |
850 | * OK, we've done all the checks, let's actually move the | |
851 | * journal inode. Errors at this point mean we need to force | |
852 | * an ext2 filesystem check. | |
853 | */ | |
854 | if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0) | |
855 | goto err_out; | |
856 | if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0) | |
857 | goto err_out; | |
858 | sb->s_journal_inum = EXT2_JOURNAL_INO; | |
859 | ext2fs_mark_super_dirty(fs); | |
860 | inode.i_links_count = 0; | |
861 | inode.i_dtime = time(0); | |
862 | if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0) | |
863 | goto err_out; | |
864 | ||
865 | group = ext2fs_group_of_ino(fs, ino); | |
866 | ext2fs_unmark_inode_bitmap(fs->inode_map, ino); | |
867 | ext2fs_mark_ib_dirty(fs); | |
868 | fs->group_desc[group].bg_free_inodes_count++; | |
869 | fs->super->s_free_inodes_count++; | |
870 | return; | |
871 | ||
872 | err_out: | |
873 | pctx.errcode = retval; | |
874 | fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx); | |
875 | fs->super->s_state &= ~EXT2_VALID_FS; | |
876 | ext2fs_mark_super_dirty(fs); | |
877 | return; | |
878 | } | |
879 |