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