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