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