X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=e2fsck%2Fjournal.c;h=6d350ee17dd1427a196e163313deac010e549594;hb=0bd0e5932046401049502ee99529b984d7cd316e;hp=dbfa1186a219d816601a97f7d5f7baf805e7780d;hpb=2f686ace1c8e2f680ffd450d143cdd01a649687a;p=tools%2Fe2fsprogs.git diff --git a/e2fsck/journal.c b/e2fsck/journal.c index dbfa118..6d350ee 100644 --- a/e2fsck/journal.c +++ b/e2fsck/journal.c @@ -13,6 +13,7 @@ */ #ifdef HAVE_SYS_MOUNT_H +#include #include #define MNT_FL (MS_MGC_VAL | MS_RDONLY) #endif @@ -25,49 +26,85 @@ #include "problem.h" #include "uuid/uuid.h" -#ifdef JFS_DEBUG /* Enabled by configure --enable-jfs-debug */ +#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */ static int bh_count = 0; -int journal_enable_debug = 2; #endif +/* + * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths. + * This creates a larger static binary, and a smaller binary using + * shared libraries. It's also probably slightly less CPU-efficient, + * which is why it's not on by default. But, it's a good way of + * testing the functions in inode_io.c and fileio.c. + */ +#undef USE_INODE_IO + /* Kernel compatibility functions for handling the journal. These allow us * to use the recovery.c file virtually unchanged from the kernel, so we * don't have to do much to keep kernel and user recovery in sync. */ -int bmap(struct inode *inode, int block) +int journal_bmap(journal_t *journal, blk64_t block, unsigned long *phys) { - int retval; - blk_t phys; - - retval = ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, &inode->i_ext2, - NULL, 0, block, &phys); +#ifdef USE_INODE_IO + *phys = block; + return 0; +#else + struct inode *inode = journal->j_inode; + errcode_t retval; + blk64_t pblk; - if (retval) - com_err(inode->i_ctx->device_name, retval, - _("bmap journal inode %ld, block %d\n"), - inode->i_ino, block); + if (!inode) { + *phys = block; + return 0; + } - return phys; + retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino, + &inode->i_ext2, NULL, 0, block, 0, &pblk); + *phys = pblk; + return (retval); +#endif } -struct buffer_head *getblk(e2fsck_t ctx, blk_t blocknr, int blocksize) +struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize) { struct buffer_head *bh; + int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize - + sizeof(bh->b_data); - bh = e2fsck_allocate_memory(ctx, sizeof(*bh), "block buffer"); + bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer"); if (!bh) return NULL; +#ifdef CONFIG_JBD_DEBUG + if (journal_enable_debug >= 3) + bh_count++; +#endif jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n", - (unsigned long) blocknr, blocksize, ++bh_count); + (unsigned long) blocknr, blocksize, bh_count); - bh->b_ctx = ctx; + bh->b_ctx = kdev->k_ctx; + if (kdev->k_dev == K_DEV_FS) + bh->b_io = kdev->k_ctx->fs->io; + else + bh->b_io = kdev->k_ctx->journal_io; bh->b_size = blocksize; bh->b_blocknr = blocknr; return bh; } +void sync_blockdev(kdev_t kdev) +{ + io_channel io; + + if (kdev->k_dev == K_DEV_FS) + io = kdev->k_ctx->fs->io; + else + io = kdev->k_ctx->journal_io; + + io_channel_flush(io); +} + void ll_rw_block(int rw, int nr, struct buffer_head *bhp[]) { int retval; @@ -76,44 +113,45 @@ void ll_rw_block(int rw, int nr, struct buffer_head *bhp[]) for (; nr > 0; --nr) { bh = *bhp++; if (rw == READ && !bh->b_uptodate) { - jfs_debug(3, "reading block %lu/%p\n", + jfs_debug(3, "reading block %lu/%p\n", (unsigned long) bh->b_blocknr, (void *) bh); - retval = io_channel_read_blk(bh->b_ctx->fs->io, + retval = io_channel_read_blk64(bh->b_io, bh->b_blocknr, 1, bh->b_data); if (retval) { com_err(bh->b_ctx->device_name, retval, - "while reading block %ld\n", - bh->b_blocknr); + "while reading block %lu\n", + (unsigned long) bh->b_blocknr); bh->b_err = retval; continue; } bh->b_uptodate = 1; } else if (rw == WRITE && bh->b_dirty) { - jfs_debug(3, "writing block %lu/%p\n", + jfs_debug(3, "writing block %lu/%p\n", (unsigned long) bh->b_blocknr, (void *) bh); - retval = io_channel_write_blk(bh->b_ctx->fs->io, + retval = io_channel_write_blk64(bh->b_io, bh->b_blocknr, 1, bh->b_data); if (retval) { com_err(bh->b_ctx->device_name, retval, - "while writing block %ld\n", - bh->b_blocknr); + "while writing block %lu\n", + (unsigned long) bh->b_blocknr); bh->b_err = retval; continue; } bh->b_dirty = 0; bh->b_uptodate = 1; - } else + } else { jfs_debug(3, "no-op %s for block %lu\n", - rw == READ ? "read" : "write", + rw == READ ? "read" : "write", (unsigned long) bh->b_blocknr); + } } } -void mark_buffer_dirty(struct buffer_head *bh, int dummy) +void mark_buffer_dirty(struct buffer_head *bh) { - bh->b_dirty = dummy | 1; /* use dummy to avoid unused variable */ + bh->b_dirty = 1; } static void mark_buffer_clean(struct buffer_head * bh) @@ -127,7 +165,7 @@ void brelse(struct buffer_head *bh) ll_rw_block(WRITE, 1, &bh); jfs_debug(3, "freeing block %lu/%p (total %d)\n", (unsigned long) bh->b_blocknr, (void *) bh, --bh_count); - ext2fs_free_mem((void **) &bh); + ext2fs_free_mem(&bh); } int buffer_uptodate(struct buffer_head *bh) @@ -157,118 +195,253 @@ static void e2fsck_clear_recover(e2fsck_t ctx, int error) ext2fs_mark_super_dirty(ctx->fs); } -static errcode_t e2fsck_journal_init_inode(e2fsck_t ctx, - struct ext2_super_block *s, - ext2_ino_t journal_inum, - journal_t **journal) +/* + * This is a helper function to check the validity of the journal. + */ +struct process_block_struct { + e2_blkcnt_t last_block; +}; + +static int process_journal_block(ext2_filsys fs, + blk64_t *block_nr, + e2_blkcnt_t blockcnt, + blk64_t ref_block EXT2FS_ATTR((unused)), + int ref_offset EXT2FS_ATTR((unused)), + void *priv_data) { - struct inode *inode; - struct buffer_head *bh; - blk_t start; - int retval; + struct process_block_struct *p; + blk64_t blk = *block_nr; + + p = (struct process_block_struct *) priv_data; + + if (!blk || blk < fs->super->s_first_data_block || + blk >= ext2fs_blocks_count(fs->super)) + return BLOCK_ABORT; + + if (blockcnt >= 0) + p->last_block = blockcnt; + return 0; +} - jfs_debug(1, "Using journal inode %u\n", journal_inum); - *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal"); - if (!*journal) { +static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) +{ + struct process_block_struct pb; + struct ext2_super_block *sb = ctx->fs->super; + struct ext2_super_block jsuper; + struct problem_context pctx; + struct buffer_head *bh; + struct inode *j_inode = NULL; + struct kdev_s *dev_fs = NULL, *dev_journal; + const char *journal_name = 0; + journal_t *journal = NULL; + errcode_t retval = 0; + io_manager io_ptr = 0; + unsigned long start = 0; + int ext_journal = 0; + int tried_backup_jnl = 0; + + clear_problem_context(&pctx); + + journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal"); + if (!journal) { return EXT2_ET_NO_MEMORY; } - inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode"); - if (!inode) { + dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev"); + if (!dev_fs) { retval = EXT2_ET_NO_MEMORY; - goto exit_journal; + goto errout; } + dev_journal = dev_fs+1; - inode->i_ctx = ctx; - inode->i_ino = journal_inum; - retval = ext2fs_read_inode(ctx->fs, journal_inum, &inode->i_ext2); - if (retval) - goto exit_inode; - - (*journal)->j_dev = ctx; - (*journal)->j_inode = inode; - (*journal)->j_blocksize = ctx->fs->blocksize; - (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize; - - if (!inode->i_ext2.i_links_count || - !LINUX_S_ISREG(inode->i_ext2.i_mode) || - (*journal)->j_maxlen < JFS_MIN_JOURNAL_BLOCKS || - (start = bmap(inode, 0)) == 0) { - retval = EXT2_ET_BAD_INODE_NUM; - goto exit_inode; - } + dev_fs->k_ctx = dev_journal->k_ctx = ctx; + dev_fs->k_dev = K_DEV_FS; + dev_journal->k_dev = K_DEV_JOURNAL; - bh = getblk(ctx, start, (*journal)->j_blocksize); - if (!bh) { - retval = EXT2_ET_NO_MEMORY; - goto exit_inode; - } - (*journal)->j_sb_buffer = bh; - (*journal)->j_superblock = (journal_superblock_t *)bh->b_data; + journal->j_dev = dev_journal; + journal->j_fs_dev = dev_fs; + journal->j_inode = NULL; + journal->j_blocksize = ctx->fs->blocksize; - return 0; + if (uuid_is_null(sb->s_journal_uuid)) { + if (!sb->s_journal_inum) { + retval = EXT2_ET_BAD_INODE_NUM; + goto errout; + } + j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode), + "journal inode"); + if (!j_inode) { + retval = EXT2_ET_NO_MEMORY; + goto errout; + } -exit_inode: - ext2fs_free_mem((void **)&inode); -exit_journal: - ext2fs_free_mem((void **)journal); + j_inode->i_ctx = ctx; + j_inode->i_ino = sb->s_journal_inum; + + if ((retval = ext2fs_read_inode(ctx->fs, + sb->s_journal_inum, + &j_inode->i_ext2))) { + try_backup_journal: + if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS || + tried_backup_jnl) + goto errout; + memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode)); + memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks, + EXT2_N_BLOCKS*4); + j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15]; + j_inode->i_ext2.i_size = sb->s_jnl_blocks[16]; + j_inode->i_ext2.i_links_count = 1; + j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600; + e2fsck_use_inode_shortcuts(ctx, 1); + ctx->stashed_ino = j_inode->i_ino; + ctx->stashed_inode = &j_inode->i_ext2; + tried_backup_jnl++; + } + if (!j_inode->i_ext2.i_links_count || + !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) { + retval = EXT2_ET_NO_JOURNAL; + goto try_backup_journal; + } + if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize < + JFS_MIN_JOURNAL_BLOCKS) { + retval = EXT2_ET_JOURNAL_TOO_SMALL; + goto try_backup_journal; + } + pb.last_block = -1; + retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino, + BLOCK_FLAG_HOLE, 0, + process_journal_block, &pb); + if ((pb.last_block + 1) * ctx->fs->blocksize < + EXT2_I_SIZE(&j_inode->i_ext2)) { + retval = EXT2_ET_JOURNAL_TOO_SMALL; + goto try_backup_journal; + } + if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) { + retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum, + &j_inode->i_ext2); + if (retval) + goto errout; + } - return retval; -} + journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) / + journal->j_blocksize; + +#ifdef USE_INODE_IO + retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum, + &j_inode->i_ext2, + &journal_name); + if (retval) + goto errout; + + io_ptr = inode_io_manager; +#else + journal->j_inode = j_inode; + ctx->journal_io = ctx->fs->io; + if ((retval = journal_bmap(journal, 0, &start)) != 0) + goto errout; +#endif + } else { + ext_journal = 1; + if (!ctx->journal_name) { + char uuid[37]; + + uuid_unparse(sb->s_journal_uuid, uuid); + ctx->journal_name = blkid_get_devname(ctx->blkid, + "UUID", uuid); + if (!ctx->journal_name) + ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev); + } + journal_name = ctx->journal_name; -static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **journal) -{ - char uuid_str[40]; - struct problem_context pctx; - struct ext2_super_block *sb = ctx->fs->super; + if (!journal_name) { + fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx); + retval = EXT2_ET_LOAD_EXT_JOURNAL; + goto errout; + } - clear_problem_context(&pctx); + jfs_debug(1, "Using journal file %s\n", journal_name); + io_ptr = unix_io_manager; + } - if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) { - /* FIXME: check if dev is valid block dev, has a journal */ - if (sb->s_journal_dev) { - pctx.num = sb->s_journal_dev; - /* this problem aborts on -y, -p, unsupported on -n */ - if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_DEV, &pctx)) - return EXT2_ET_UNSUPP_FEATURE; - sb->s_journal_dev = 0; - sb->s_state &= ~EXT2_VALID_FS; - ext2fs_mark_super_dirty(ctx->fs); +#if 0 + test_io_backing_manager = io_ptr; + io_ptr = test_io_manager; +#endif +#ifndef USE_INODE_IO + if (ext_journal) +#endif + retval = io_ptr->open(journal_name, + IO_FLAG_RW | IO_FLAG_EXCLUSIVE, + &ctx->journal_io); + if (retval) + goto errout; + + io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize); + + if (ext_journal) { + if (ctx->fs->blocksize == 1024) + start = 1; + bh = getblk(dev_journal, start, ctx->fs->blocksize); + if (!bh) { + retval = EXT2_ET_NO_MEMORY; + goto errout; } - /* FIXME: check if UUID is valid block dev, has a journal */ - if (!uuid_is_null(sb->s_journal_uuid)) { - uuid_unparse(sb->s_journal_uuid, uuid_str); - pctx.str = uuid_str; - /* this problem aborts on -y, -p, unsupported on -n */ - if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_UUID, &pctx)) - return EXT2_ET_UNSUPP_FEATURE; - uuid_clear(sb->s_journal_uuid); - sb->s_state &= ~EXT2_VALID_FS; - ext2fs_mark_super_dirty(ctx->fs); + ll_rw_block(READ, 1, &bh); + if ((retval = bh->b_err) != 0) { + brelse(bh); + goto errout; + } + memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024, + sizeof(jsuper)); + brelse(bh); +#ifdef WORDS_BIGENDIAN + if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) + ext2fs_swap_super(&jsuper); +#endif + if (jsuper.s_magic != EXT2_SUPER_MAGIC || + !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { + fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx); + retval = EXT2_ET_LOAD_EXT_JOURNAL; + goto errout; + } + /* Make sure the journal UUID is correct */ + if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid, + sizeof(jsuper.s_uuid))) { + fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx); + retval = EXT2_ET_LOAD_EXT_JOURNAL; + goto errout; } - if (!sb->s_journal_inum) - return EXT2_ET_BAD_INODE_NUM; - } - if (sb->s_journal_dev) { - pctx.num = sb->s_journal_dev; - if (!fix_problem(ctx, PR_0_JOURNAL_BAD_DEV, &pctx)) - return EXT2_ET_UNSUPP_FEATURE; - sb->s_journal_dev = 0; - sb->s_state &= ~EXT2_VALID_FS; - ext2fs_mark_super_dirty(ctx->fs); + journal->j_maxlen = ext2fs_blocks_count(&jsuper); + start++; } - if (!uuid_is_null(sb->s_journal_uuid)) { - uuid_unparse(sb->s_journal_uuid, uuid_str); - pctx.str = uuid_str; - if (!fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx)) - return EXT2_ET_UNSUPP_FEATURE; - uuid_clear(sb->s_journal_uuid); - sb->s_state &= ~EXT2_VALID_FS; - ext2fs_mark_super_dirty(ctx->fs); + + if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) { + retval = EXT2_ET_NO_MEMORY; + goto errout; } - return e2fsck_journal_init_inode(ctx, sb, sb->s_journal_inum, journal); + journal->j_sb_buffer = bh; + journal->j_superblock = (journal_superblock_t *)bh->b_data; + +#ifdef USE_INODE_IO + if (j_inode) + ext2fs_free_mem(&j_inode); +#endif + + *ret_journal = journal; + e2fsck_use_inode_shortcuts(ctx, 0); + return 0; + +errout: + e2fsck_use_inode_shortcuts(ctx, 0); + if (dev_fs) + ext2fs_free_mem(&dev_fs); + if (j_inode) + ext2fs_free_mem(&j_inode); + if (journal) + ext2fs_free_mem(&journal); + return retval; } static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx, @@ -289,7 +462,8 @@ static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx, "filesystem is now ext2 only ***\n\n"); sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; sb->s_journal_inum = 0; - ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */ + ctx->flags |= E2F_FLAG_JOURNAL_INODE; + ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; e2fsck_clear_recover(ctx, 1); return 0; } @@ -304,9 +478,26 @@ static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx, return 0; } +#define V1_SB_SIZE 0x0024 +static void clear_v2_journal_fields(journal_t *journal) +{ + e2fsck_t ctx = journal->j_dev->k_ctx; + struct problem_context pctx; + + clear_problem_context(&pctx); + + if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx)) + return; + + memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0, + ctx->fs->blocksize-V1_SB_SIZE); + mark_buffer_dirty(journal->j_sb_buffer); +} + + static errcode_t e2fsck_journal_load(journal_t *journal) { - e2fsck_t ctx = journal->j_dev; + e2fsck_t ctx = journal->j_dev->k_ctx; journal_superblock_t *jsb; struct buffer_head *jbh = journal->j_sb_buffer; struct problem_context pctx; @@ -328,10 +519,22 @@ static errcode_t e2fsck_journal_load(journal_t *journal) switch (ntohl(jsb->s_header.h_blocktype)) { case JFS_SUPERBLOCK_V1: journal->j_format_version = 1; + if (jsb->s_feature_compat || + jsb->s_feature_incompat || + jsb->s_feature_ro_compat || + jsb->s_nr_users) + clear_v2_journal_fields(journal); break; - + case JFS_SUPERBLOCK_V2: journal->j_format_version = 2; + if (ntohl(jsb->s_nr_users) > 1 && + uuid_is_null(ctx->fs->super->s_journal_uuid)) + clear_v2_journal_fields(journal); + if (ntohl(jsb->s_nr_users) > 1) { + fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx); + return EXT2_ET_JOURNAL_UNSUPP_VERSION; + } break; /* @@ -342,7 +545,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal) case JFS_COMMIT_BLOCK: case JFS_REVOKE_BLOCK: return EXT2_ET_CORRUPT_SUPERBLOCK; - + /* If we don't understand the superblock major type, but there * is a magic number, then it is likely to be a new format we * just don't understand, so leave it alone. */ @@ -352,7 +555,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal) if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) return EXT2_ET_UNSUPP_FEATURE; - + if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) return EXT2_ET_RO_UNSUPP_FEATURE; @@ -398,7 +601,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, /* Leave a valid existing V1 superblock signature alone. * Anything unrecognisable we overwrite with a new V2 * signature. */ - + if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) || jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) { jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER); @@ -406,7 +609,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, } /* Zero out everything else beyond the superblock header */ - + p = ((char *) jsb) + sizeof(journal_header_t); memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t)); @@ -424,7 +627,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, new_seq ^= u.val[i]; jsb->s_sequence = htonl(new_seq); - mark_buffer_dirty(journal->j_sb_buffer, 1); + mark_buffer_dirty(journal->j_sb_buffer); ll_rw_block(WRITE, 1, &journal->j_sb_buffer); } @@ -436,8 +639,6 @@ static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, int recover = ctx->fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER; - pctx->num = journal->j_inode->i_ino; - if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) { if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) { e2fsck_journal_reset_super(ctx, journal->j_superblock, @@ -465,13 +666,23 @@ static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, jsb->s_sequence = htonl(journal->j_transaction_sequence); if (reset) jsb->s_start = 0; /* this marks the journal as empty */ - mark_buffer_dirty(journal->j_sb_buffer, 1); + mark_buffer_dirty(journal->j_sb_buffer); } brelse(journal->j_sb_buffer); + if (ctx->journal_io) { + if (ctx->fs && ctx->fs->io != ctx->journal_io) + io_channel_close(ctx->journal_io); + ctx->journal_io = 0; + } + +#ifndef USE_INODE_IO if (journal->j_inode) - ext2fs_free_mem((void **)&journal->j_inode); - ext2fs_free_mem((void **)&journal); + ext2fs_free_mem(&journal->j_inode); +#endif + if (journal->j_fs_dev) + ext2fs_free_mem(&journal->j_fs_dev); + ext2fs_free_mem(&journal); } /* @@ -485,6 +696,7 @@ int e2fsck_check_ext3_journal(e2fsck_t ctx) int recover = ctx->fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER; struct problem_context pctx; + problem_t problem; int reset = 0, force_fsck = 0; int retval; @@ -499,7 +711,10 @@ int e2fsck_check_ext3_journal(e2fsck_t ctx) retval = e2fsck_get_journal(ctx, &journal); if (retval) { - if (retval == EXT2_ET_BAD_INODE_NUM) + if ((retval == EXT2_ET_BAD_INODE_NUM) || + (retval == EXT2_ET_BAD_BLOCK_NUM) || + (retval == EXT2_ET_JOURNAL_TOO_SMALL) || + (retval == EXT2_ET_NO_JOURNAL)) return e2fsck_journal_fix_bad_inode(ctx, &pctx); return retval; } @@ -548,6 +763,7 @@ no_has_journal: e2fsck_clear_recover(ctx, force_fsck); } else if (!(ctx->options & E2F_OPT_READONLY)) { sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; + ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; ext2fs_mark_super_dirty(ctx->fs); } } @@ -555,7 +771,19 @@ no_has_journal: if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL && !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) && journal->j_superblock->s_start != 0) { - if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) { + /* Print status information */ + fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx); + if (ctx->superblock) + problem = PR_0_JOURNAL_RUN_DEFAULT; + else + problem = PR_0_JOURNAL_RUN; + if (fix_problem(ctx, problem, &pctx)) { + ctx->options |= E2F_OPT_FORCE; + sb->s_feature_incompat |= + EXT3_FEATURE_INCOMPAT_RECOVER; + ext2fs_mark_super_dirty(ctx->fs); + } else if (fix_problem(ctx, + PR_0_JOURNAL_RESET_JOURNAL, &pctx)) { reset = 1; sb->s_state &= ~EXT2_VALID_FS; ext2fs_mark_super_dirty(ctx->fs); @@ -565,7 +793,7 @@ no_has_journal: * ignore the fact that journal apparently has data; * accidentally replaying over valid data would be far * worse than skipping a questionable recovery. - * + * * XXX should we abort with a fatal error here? What * will the ext3 kernel code do if a filesystem with * !NEEDS_RECOVERY but with a non-zero @@ -579,9 +807,13 @@ no_has_journal: static errcode_t recover_ext3_journal(e2fsck_t ctx) { + struct problem_context pctx; journal_t *journal; int retval; + clear_problem_context(&pctx); + + journal_init_revoke_caches(); retval = e2fsck_get_journal(ctx, &journal); if (retval) return retval; @@ -593,121 +825,40 @@ static errcode_t recover_ext3_journal(e2fsck_t ctx) retval = journal_init_revoke(journal, 1024); if (retval) goto errout; - - retval = -journal_recover(journal); -errout: - e2fsck_journal_release(ctx, journal, 1, 0); - return retval; -} - -#if 0 -#define TEMPLATE "/tmp/ext3.XXXXXX" - -/* - * This function attempts to mount and unmount an ext3 filesystem, - * which is a cheap way to force the kernel to run the journal and - * handle the recovery for us. - */ -static errcode_t recover_ext3_journal_via_mount(e2fsck_t ctx) -{ - ext2_filsys fs = ctx->fs; - char *dirlist[] = {"/mnt","/lost+found","/tmp","/root","/boot",0}; - errcode_t retval, retval2; - int count = 0; - char template[] = TEMPLATE; - struct stat buf; - char *tmpdir; - - if (ctx->options & E2F_OPT_READONLY) { - printf("%s: won't do journal recovery while read-only\n", - ctx->device_name); - return EXT2_ET_FILE_RO; - } + retval = -journal_recover(journal); + if (retval) + goto errout; - printf(_("%s: trying for ext3 kernel journal recovery\n"), - ctx->device_name); - /* - * First try to make a temporary directory. This may fail if - * the root partition is still mounted read-only. - */ -newtemp: - tmpdir = mktemp(template); - if (tmpdir) { - jfs_debug(2, "trying %s as ext3 temp mount point\n", tmpdir); - if (mkdir(template, 0700)) { - if (errno == EROFS) { - tmpdir = NULL; - template[0] = '\0'; - } else if (errno == EEXIST && count++ < 10) { - strcpy(template, TEMPLATE); - goto newtemp; - } - return errno; - } + if (journal->j_failed_commit) { + pctx.ino = journal->j_failed_commit; + fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx); + ctx->fs->super->s_state |= EXT2_ERROR_FS; + ext2fs_mark_super_dirty(ctx->fs); } - /* - * OK, creating a temporary directory didn't work. - * Let's try a list of possible temporary mountpoints. - */ - if (!tmpdir) { - dev_t rootdev; - char **cpp, *dir; - if (stat("/", &buf)) - return errno; - - rootdev = buf.st_dev; - - /* - * Check that dir is on the same device as root (no other - * filesystem is mounted there), and it's a directory. - */ - for (cpp = dirlist; (dir = *cpp); cpp++) - if (stat(dir, &buf) == 0 && buf.st_dev == rootdev && - S_ISDIR(buf.st_mode)) { - tmpdir = dir; - break; - } + if (journal->j_superblock->s_errno) { + ctx->fs->super->s_state |= EXT2_ERROR_FS; + ext2fs_mark_super_dirty(ctx->fs); + journal->j_superblock->s_errno = 0; + mark_buffer_dirty(journal->j_sb_buffer); } - if (tmpdir) { - io_manager io_ptr = fs->io->manager; - int blocksize = fs->blocksize; - - jfs_debug(2, "using %s for ext3 mount\n", tmpdir); - /* FIXME - need to handle loop devices here */ - if (mount(ctx->device_name, tmpdir, "ext3", MNT_FL, NULL)) { - retval = errno; - com_err(ctx->program_name, errno, - "when mounting %s", ctx->device_name); - if (template[0]) - rmdir(tmpdir); - return retval; - } - /* - * Now that it mounted cleanly, the filesystem will have been - * recovered, so we can now unmount it. - */ - if (umount(tmpdir)) - return errno; - - /* - * Remove the temporary directory, if it was created. - */ - if (template[0]) - rmdir(tmpdir); - return 0; - } +errout: + journal_destroy_revoke(journal); + journal_destroy_revoke_caches(); + e2fsck_journal_release(ctx, journal, 1, 0); + return retval; } -#endif int e2fsck_run_ext3_journal(e2fsck_t ctx) { io_manager io_ptr = ctx->fs->io->manager; int blocksize = ctx->fs->blocksize; errcode_t retval, recover_retval; + io_stats stats = 0; + unsigned long long kbytes_written = 0; printf(_("%s: recovering journal\n"), ctx->device_name); if (ctx->options & E2F_OPT_READONLY) { @@ -716,19 +867,24 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx) return EXT2_ET_FILE_RO; } - ext2fs_flush(ctx->fs); /* Force out any modifications */ + if (ctx->fs->flags & EXT2_FLAG_DIRTY) + ext2fs_flush(ctx->fs); /* Force out any modifications */ recover_retval = recover_ext3_journal(ctx); - + /* * Reload the filesystem context to get up-to-date data from disk * because journal recovery will change the filesystem under us. */ - ext2fs_close(ctx->fs); + if (ctx->fs->super->s_kbytes_written && + ctx->fs->io->manager->get_stats) + ctx->fs->io->manager->get_stats(ctx->fs->io, &stats); + if (stats && stats->bytes_written) + kbytes_written = stats->bytes_written >> 10; + ext2fs_free(ctx->fs); retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW, ctx->superblock, blocksize, io_ptr, &ctx->fs); - if (retval) { com_err(ctx->program_name, retval, _("while trying to re-open %s"), @@ -736,8 +892,174 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx) fatal_error(ctx, 0); } ctx->fs->priv_data = ctx; + ctx->fs->now = ctx->now; + ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; + ctx->fs->super->s_kbytes_written += kbytes_written; /* Set the superblock flags */ e2fsck_clear_recover(ctx, recover_retval); return recover_retval; } + +/* + * This function will move the journal inode from a visible file in + * the filesystem directory hierarchy to the reserved inode if necessary. + */ +static const char * const journal_names[] = { + ".journal", "journal", ".journal.dat", "journal.dat", 0 }; + +void e2fsck_move_ext3_journal(e2fsck_t ctx) +{ + struct ext2_super_block *sb = ctx->fs->super; + struct problem_context pctx; + struct ext2_inode inode; + ext2_filsys fs = ctx->fs; + ext2_ino_t ino; + errcode_t retval; + const char * const * cpp; + int group, mount_flags; + + clear_problem_context(&pctx); + + /* + * If the filesystem is opened read-only, or there is no + * journal, then do nothing. + */ + if ((ctx->options & E2F_OPT_READONLY) || + (sb->s_journal_inum == 0) || + !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) + return; + + /* + * Read in the journal inode + */ + if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0) + return; + + /* + * If it's necessary to backup the journal inode, do so. + */ + if ((sb->s_jnl_backup_type == 0) || + ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) && + memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) { + if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) { + memcpy(sb->s_jnl_blocks, inode.i_block, + EXT2_N_BLOCKS*4); + sb->s_jnl_blocks[15] = inode.i_size_high; + sb->s_jnl_blocks[16] = inode.i_size; + sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS; + ext2fs_mark_super_dirty(fs); + fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; + } + } + + /* + * If the journal is already the hidden inode, then do nothing + */ + if (sb->s_journal_inum == EXT2_JOURNAL_INO) + return; + + /* + * The journal inode had better have only one link and not be readable. + */ + if (inode.i_links_count != 1) + return; + + /* + * If the filesystem is mounted, or we can't tell whether + * or not it's mounted, do nothing. + */ + retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); + if (retval || (mount_flags & EXT2_MF_MOUNTED)) + return; + + /* + * If we can't find the name of the journal inode, then do + * nothing. + */ + for (cpp = journal_names; *cpp; cpp++) { + retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp, + strlen(*cpp), 0, &ino); + if ((retval == 0) && (ino == sb->s_journal_inum)) + break; + } + if (*cpp == 0) + return; + + /* We need the inode bitmap to be loaded */ + retval = ext2fs_read_bitmaps(fs); + if (retval) + return; + + pctx.str = *cpp; + if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx)) + return; + + /* + * OK, we've done all the checks, let's actually move the + * journal inode. Errors at this point mean we need to force + * an ext2 filesystem check. + */ + if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0) + goto err_out; + if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0) + goto err_out; + sb->s_journal_inum = EXT2_JOURNAL_INO; + ext2fs_mark_super_dirty(fs); + fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; + inode.i_links_count = 0; + inode.i_dtime = ctx->now; + if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0) + goto err_out; + + group = ext2fs_group_of_ino(fs, ino); + ext2fs_unmark_inode_bitmap2(fs->inode_map, ino); + ext2fs_mark_ib_dirty(fs); + ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1); + ext2fs_group_desc_csum_set(fs, group); + fs->super->s_free_inodes_count++; + return; + +err_out: + pctx.errcode = retval; + fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx); + fs->super->s_state &= ~EXT2_VALID_FS; + ext2fs_mark_super_dirty(fs); + return; +} + +/* + * This function makes sure the superblock hint for the external + * journal is correct. + */ +int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx) +{ + struct ext2_super_block *sb = ctx->fs->super; + struct problem_context pctx; + char uuid[37], *journal_name; + struct stat st; + + if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) || + uuid_is_null(sb->s_journal_uuid)) + return 0; + + uuid_unparse(sb->s_journal_uuid, uuid); + journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid); + if (!journal_name) + return 0; + + if (stat(journal_name, &st) < 0) + return 0; + + if (st.st_rdev != sb->s_journal_dev) { + clear_problem_context(&pctx); + pctx.num = st.st_rdev; + if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) { + sb->s_journal_dev = st.st_rdev; + ext2fs_mark_super_dirty(ctx->fs); + } + } + + free(journal_name); + return 0; +}