X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=e2fsck%2Fjournal.c;h=fe4e018df0593a84253cb315f5f04adfa64f797c;hb=a64e0fd23845719dbb0c6c4736d6a9984a2a4a2d;hp=2509303aa7afa73ec445f8491b84bc7d97e57897;hpb=e337e7fad8d9f29ea6a7436b40361f3a1a17a6af;p=tools%2Fe2fsprogs.git diff --git a/e2fsck/journal.c b/e2fsck/journal.c index 2509303..fe4e018 100644 --- a/e2fsck/journal.c +++ b/e2fsck/journal.c @@ -27,9 +27,7 @@ #include "problem.h" #include "uuid/uuid.h" -#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */ static int bh_count = 0; -#endif /* * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths. @@ -44,7 +42,7 @@ static int bh_count = 0; static int e2fsck_journal_verify_csum_type(journal_t *j, journal_superblock_t *jsb) { - if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2)) + if (!jbd2_journal_has_csum_v2or3(j)) return 1; return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM; @@ -68,7 +66,7 @@ static int e2fsck_journal_sb_csum_verify(journal_t *j, { __u32 provided, calculated; - if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2)) + if (!jbd2_journal_has_csum_v2or3(j)) return 1; provided = ext2fs_be32_to_cpu(jsb->s_checksum); @@ -82,7 +80,7 @@ static errcode_t e2fsck_journal_sb_csum_set(journal_t *j, { __u32 crc; - if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2)) + if (!jbd2_journal_has_csum_v2or3(j)) return 0; crc = e2fsck_journal_sb_csum(jsb); @@ -94,7 +92,8 @@ static errcode_t e2fsck_journal_sb_csum_set(journal_t *j, * 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 journal_bmap(journal_t *journal, blk64_t block, unsigned long long *phys) +int jbd2_journal_bmap(journal_t *journal, unsigned long block, + unsigned long long *phys) { #ifdef USE_INODE_IO *phys = block; @@ -110,13 +109,15 @@ int journal_bmap(journal_t *journal, blk64_t block, unsigned long long *phys) } retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino, - &inode->i_ext2, NULL, 0, block, 0, &pblk); + &inode->i_ext2, NULL, 0, (blk64_t) block, + 0, &pblk); *phys = pblk; - return (retval); + return -1 * ((int) retval); #endif } -struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize) +struct buffer_head *getblk(kdev_t kdev, unsigned long long blocknr, + int blocksize) { struct buffer_head *bh; int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize - @@ -126,12 +127,10 @@ struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize) if (!bh) return NULL; -#ifdef CONFIG_JBD_DEBUG if (journal_enable_debug >= 3) bh_count++; -#endif jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n", - (unsigned long long) blocknr, blocksize, bh_count); + blocknr, blocksize, bh_count); bh->b_ctx = kdev->k_ctx; if (kdev->k_dev == K_DEV_FS) @@ -144,7 +143,7 @@ struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize) return bh; } -void sync_blockdev(kdev_t kdev) +int sync_blockdev(kdev_t kdev) { io_channel io; @@ -153,17 +152,18 @@ void sync_blockdev(kdev_t kdev) else io = kdev->k_ctx->journal_io; - io_channel_flush(io); + return io_channel_flush(io) ? -EIO : 0; } -void ll_rw_block(int rw, int nr, struct buffer_head *bhp[]) +void ll_rw_block(int rw, int op_flags EXT2FS_ATTR((unused)), int nr, + struct buffer_head *bhp[]) { - int retval; + errcode_t retval; struct buffer_head *bh; for (; nr > 0; --nr) { bh = *bhp++; - if (rw == READ && !bh->b_uptodate) { + if (rw == REQ_OP_READ && !bh->b_uptodate) { jfs_debug(3, "reading block %llu/%p\n", bh->b_blocknr, (void *) bh); retval = io_channel_read_blk64(bh->b_io, @@ -173,11 +173,11 @@ void ll_rw_block(int rw, int nr, struct buffer_head *bhp[]) com_err(bh->b_ctx->device_name, retval, "while reading block %llu\n", bh->b_blocknr); - bh->b_err = retval; + bh->b_err = (int) retval; continue; } bh->b_uptodate = 1; - } else if (rw == WRITE && bh->b_dirty) { + } else if (rw == REQ_OP_WRITE && bh->b_dirty) { jfs_debug(3, "writing block %llu/%p\n", bh->b_blocknr, (void *) bh); @@ -188,14 +188,14 @@ void ll_rw_block(int rw, int nr, struct buffer_head *bhp[]) com_err(bh->b_ctx->device_name, retval, "while writing block %llu\n", bh->b_blocknr); - bh->b_err = retval; + bh->b_err = (int) retval; continue; } bh->b_dirty = 0; bh->b_uptodate = 1; } else { jfs_debug(3, "no-op %s for block %llu\n", - rw == READ ? "read" : "write", + rw == REQ_OP_READ ? "read" : "write", bh->b_blocknr); } } @@ -214,7 +214,7 @@ static void mark_buffer_clean(struct buffer_head * bh) void brelse(struct buffer_head *bh) { if (bh->b_dirty) - ll_rw_block(WRITE, 1, &bh); + ll_rw_block(REQ_OP_WRITE, 0, 1, &bh); jfs_debug(3, "freeing block %llu/%p (total %d)\n", bh->b_blocknr, (void *) bh, --bh_count); ext2fs_free_mem(&bh); @@ -233,13 +233,13 @@ void mark_buffer_uptodate(struct buffer_head *bh, int val) void wait_on_buffer(struct buffer_head *bh) { if (!bh->b_uptodate) - ll_rw_block(READ, 1, &bh); + ll_rw_block(REQ_OP_READ, 0, 1, &bh); } static void e2fsck_clear_recover(e2fsck_t ctx, int error) { - ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER; + ext2fs_clear_feature_journal_needs_recovery(ctx->fs->super); /* if we had an error doing journal recovery, we need a full fsck */ if (error) @@ -275,6 +275,675 @@ static int process_journal_block(ext2_filsys fs, return 0; } +static int ext4_fc_replay_scan(journal_t *j, struct buffer_head *bh, + int off, tid_t expected_tid) +{ + e2fsck_t ctx = j->j_fs_dev->k_ctx; + struct e2fsck_fc_replay_state *state; + int ret = JBD2_FC_REPLAY_CONTINUE; + struct ext4_fc_add_range ext; + struct ext4_fc_tl tl; + struct ext4_fc_tail tail; + __u8 *start, *cur, *end, *val; + struct ext4_fc_head head; + struct ext2fs_extent ext2fs_ex = {0}; + + state = &ctx->fc_replay_state; + + start = (__u8 *)bh->b_data; + end = (__u8 *)bh->b_data + j->j_blocksize - 1; + + jbd_debug(1, "Scan phase starting, expected %d", expected_tid); + if (state->fc_replay_expected_off == 0) { + memset(state, 0, sizeof(*state)); + /* Check if we can stop early */ + if (le16_to_cpu(((struct ext4_fc_tl *)start)->fc_tag) + != EXT4_FC_TAG_HEAD) { + jbd_debug(1, "Ending early!, not a head tag"); + return 0; + } + } + + if (off != state->fc_replay_expected_off) { + ret = -EFSCORRUPTED; + goto out_err; + } + + state->fc_replay_expected_off++; + for (cur = start; cur < end; cur = cur + le16_to_cpu(tl.fc_len) + sizeof(tl)) { + memcpy(&tl, cur, sizeof(tl)); + val = cur + sizeof(tl); + + jbd_debug(3, "Scan phase, tag:%s, blk %lld\n", + tag2str(le16_to_cpu(tl.fc_tag)), bh->b_blocknr); + switch (le16_to_cpu(tl.fc_tag)) { + case EXT4_FC_TAG_ADD_RANGE: + memcpy(&ext, val, sizeof(ext)); + ret = ext2fs_decode_extent(&ext2fs_ex, + (void *)&ext.fc_ex, + sizeof(ext.fc_ex)); + if (ret) + ret = JBD2_FC_REPLAY_STOP; + else + ret = JBD2_FC_REPLAY_CONTINUE; + /* fallthrough */ + case EXT4_FC_TAG_DEL_RANGE: + case EXT4_FC_TAG_LINK: + case EXT4_FC_TAG_UNLINK: + case EXT4_FC_TAG_CREAT: + case EXT4_FC_TAG_INODE: + case EXT4_FC_TAG_PAD: + state->fc_cur_tag++; + state->fc_crc = jbd2_chksum(j, state->fc_crc, cur, + sizeof(tl) + ext4_fc_tag_len(&tl)); + break; + case EXT4_FC_TAG_TAIL: + state->fc_cur_tag++; + memcpy(&tail, val, sizeof(tail)); + state->fc_crc = jbd2_chksum(j, state->fc_crc, cur, + sizeof(tl) + + offsetof(struct ext4_fc_tail, + fc_crc)); + jbd_debug(1, "tail tid %d, expected %d\n", + le32_to_cpu(tail.fc_tid), expected_tid); + if (le32_to_cpu(tail.fc_tid) == expected_tid && + le32_to_cpu(tail.fc_crc) == state->fc_crc) { + state->fc_replay_num_tags = state->fc_cur_tag; + } else { + ret = state->fc_replay_num_tags ? + JBD2_FC_REPLAY_STOP : -EFSBADCRC; + } + state->fc_crc = 0; + break; + case EXT4_FC_TAG_HEAD: + memcpy(&head, val, sizeof(head)); + if (le32_to_cpu(head.fc_features) & + ~EXT4_FC_SUPPORTED_FEATURES) { + ret = -EOPNOTSUPP; + break; + } + if (le32_to_cpu(head.fc_tid) != expected_tid) { + ret = -EINVAL; + break; + } + state->fc_cur_tag++; + state->fc_crc = jbd2_chksum(j, state->fc_crc, cur, + sizeof(tl) + ext4_fc_tag_len(&tl)); + break; + default: + ret = state->fc_replay_num_tags ? + JBD2_FC_REPLAY_STOP : -ECANCELED; + } + if (ret < 0 || ret == JBD2_FC_REPLAY_STOP) + break; + } + +out_err: + return ret; +} + +static int __errcode_to_errno(errcode_t err, const char *func, int line) +{ + if (err == 0) + return 0; + fprintf(stderr, "Error \"%s\" encountered in function %s at line %d\n", + error_message(err), func, line); + if (err <= 256) + return -err; + return -EFAULT; +} + +#define errcode_to_errno(err) __errcode_to_errno(err, __func__, __LINE__) + +#define ex_end(__ex) ((__ex)->e_lblk + (__ex)->e_len - 1) +#define ex_pend(__ex) ((__ex)->e_pblk + (__ex)->e_len - 1) + +static int make_room(struct extent_list *list, int i) +{ + int ret; + + if (list->count == list->size) { + unsigned int new_size = (list->size + 341) * + sizeof(struct ext2fs_extent); + ret = errcode_to_errno(ext2fs_resize_mem(0, new_size, &list->extents)); + if (ret) + return ret; + list->size += 341; + } + + memmove(&list->extents[i + 1], &list->extents[i], + sizeof(list->extents[0]) * (list->count - i)); + list->count++; + return 0; +} + +static int ex_compar(const void *arg1, const void *arg2) +{ + const struct ext2fs_extent *ex1 = (const struct ext2fs_extent *)arg1; + const struct ext2fs_extent *ex2 = (const struct ext2fs_extent *)arg2; + + if (ex1->e_lblk < ex2->e_lblk) + return -1; + if (ex1->e_lblk > ex2->e_lblk) + return 1; + return ex1->e_len - ex2->e_len; +} + +static int ex_len_compar(const void *arg1, const void *arg2) +{ + const struct ext2fs_extent *ex1 = (const struct ext2fs_extent *)arg1; + const struct ext2fs_extent *ex2 = (const struct ext2fs_extent *)arg2; + + if (ex1->e_len < ex2->e_len) + return 1; + + if (ex1->e_lblk > ex2->e_lblk) + return -1; + + return 0; +} + +static void ex_sort_and_merge(struct extent_list *list) +{ + unsigned int i, j; + + if (list->count < 2) + return; + + /* + * Reverse sort by length, that way we strip off all the 0 length + * extents + */ + qsort(list->extents, list->count, sizeof(struct ext2fs_extent), + ex_len_compar); + + for (i = 0; i < list->count; i++) { + if (list->extents[i].e_len == 0) { + list->count = i; + break; + } + } + + /* Now sort by logical offset */ + qsort(list->extents, list->count, sizeof(list->extents[0]), + ex_compar); + + /* Merge adjacent extents if they are logically and physically contiguous */ + i = 0; + while (i < list->count - 1) { + if (ex_end(&list->extents[i]) + 1 != list->extents[i + 1].e_lblk || + ex_pend(&list->extents[i]) + 1 != list->extents[i + 1].e_pblk || + (list->extents[i].e_flags & EXT2_EXTENT_FLAGS_UNINIT) != + (list->extents[i + 1].e_flags & EXT2_EXTENT_FLAGS_UNINIT)) { + i++; + continue; + } + + list->extents[i].e_len += list->extents[i + 1].e_len; + for (j = i + 1; j < list->count - 1; j++) + list->extents[j] = list->extents[j + 1]; + list->count--; + } +} + +/* must free blocks that are released */ +static int ext4_modify_extent_list(e2fsck_t ctx, struct extent_list *list, + struct ext2fs_extent *ex, int del) +{ + int ret, offset; + unsigned int i; + struct ext2fs_extent add_ex = *ex; + + /* First let's create a hole from ex->e_lblk of length ex->e_len */ + for (i = 0; i < list->count; i++) { + if (ex_end(&list->extents[i]) < add_ex.e_lblk) + continue; + + /* Case 1: No overlap */ + if (list->extents[i].e_lblk > ex_end(&add_ex)) + break; + /* + * Unmark all the blocks in bb now. All the blocks get marked + * before we exit this function. + */ + ext2fs_unmark_block_bitmap_range2(ctx->fs->block_map, + list->extents[i].e_pblk, list->extents[i].e_len); + /* Case 2: Split */ + if (list->extents[i].e_lblk < add_ex.e_lblk && + ex_end(&list->extents[i]) > ex_end(&add_ex)) { + ret = make_room(list, i + 1); + if (ret) + return ret; + list->extents[i + 1] = list->extents[i]; + offset = ex_end(&add_ex) + 1 - list->extents[i].e_lblk; + list->extents[i + 1].e_lblk += offset; + list->extents[i + 1].e_pblk += offset; + list->extents[i + 1].e_len -= offset; + list->extents[i].e_len = + add_ex.e_lblk - list->extents[i].e_lblk; + break; + } + + /* Case 3: Exact overlap */ + if (add_ex.e_lblk <= list->extents[i].e_lblk && + ex_end(&list->extents[i]) <= ex_end(&add_ex)) { + + list->extents[i].e_len = 0; + continue; + } + + /* Case 4: Partial overlap */ + if (ex_end(&list->extents[i]) > ex_end(&add_ex)) { + offset = ex_end(&add_ex) + 1 - list->extents[i].e_lblk; + list->extents[i].e_lblk += offset; + list->extents[i].e_pblk += offset; + list->extents[i].e_len -= offset; + break; + } + + if (ex_end(&add_ex) >= ex_end(&list->extents[i])) + list->extents[i].e_len = + add_ex.e_lblk > list->extents[i].e_lblk ? + add_ex.e_lblk - list->extents[i].e_lblk : 0; + } + + if (add_ex.e_len && !del) { + make_room(list, list->count); + list->extents[list->count - 1] = add_ex; + } + + ex_sort_and_merge(list); + + /* Mark all occupied blocks allocated */ + for (i = 0; i < list->count; i++) + ext2fs_mark_block_bitmap_range2(ctx->fs->block_map, + list->extents[i].e_pblk, list->extents[i].e_len); + ext2fs_mark_bb_dirty(ctx->fs); + + return 0; +} + +static int ext4_add_extent_to_list(e2fsck_t ctx, struct extent_list *list, + struct ext2fs_extent *ex) +{ + return ext4_modify_extent_list(ctx, list, ex, 0 /* add */); +} + +static int ext4_del_extent_from_list(e2fsck_t ctx, struct extent_list *list, + struct ext2fs_extent *ex) +{ + return ext4_modify_extent_list(ctx, list, ex, 1 /* delete */); +} + +static int ext4_fc_read_extents(e2fsck_t ctx, ino_t ino) +{ + struct extent_list *extent_list = &ctx->fc_replay_state.fc_extent_list; + + if (extent_list->ino == ino) + return 0; + + extent_list->ino = ino; + return errcode_to_errno(e2fsck_read_extents(ctx, extent_list)); +} + +/* + * Flush extents in replay state on disk. @ino is the inode that is going + * to be processed next. So, we hold back flushing of the extent list + * if the next inode that's going to be processed is same as the one with + * cached extents in our replay state. That allows us to gather multiple extents + * for the inode so that we can flush all of them at once and it also saves us + * from continuously growing and shrinking the extent tree. + */ +static void ext4_fc_flush_extents(e2fsck_t ctx, ino_t ino) +{ + struct extent_list *extent_list = &ctx->fc_replay_state.fc_extent_list; + + if (extent_list->ino == ino || extent_list->ino == 0) + return; + e2fsck_rewrite_extent_tree(ctx, extent_list); + ext2fs_free_mem(&extent_list->extents); + memset(extent_list, 0, sizeof(*extent_list)); +} + +/* Helper struct for dentry replay routines */ +struct dentry_info_args { + ino_t parent_ino; + int dname_len; + ino_t ino; + char *dname; +}; + +static inline int tl_to_darg(struct dentry_info_args *darg, + struct ext4_fc_tl *tl, __u8 *val) +{ + struct ext4_fc_dentry_info fcd; + int tag = le16_to_cpu(tl->fc_tag); + + memcpy(&fcd, val, sizeof(fcd)); + + darg->parent_ino = le32_to_cpu(fcd.fc_parent_ino); + darg->ino = le32_to_cpu(fcd.fc_ino); + darg->dname_len = ext4_fc_tag_len(tl) - + sizeof(struct ext4_fc_dentry_info); + darg->dname = malloc(darg->dname_len + 1); + if (!darg->dname) + return -ENOMEM; + memcpy(darg->dname, + val + sizeof(struct ext4_fc_dentry_info), + darg->dname_len); + darg->dname[darg->dname_len] = 0; + jbd_debug(1, "%s: %s, ino %lu, parent %lu\n", + tag == EXT4_FC_TAG_CREAT ? "create" : + (tag == EXT4_FC_TAG_LINK ? "link" : + (tag == EXT4_FC_TAG_UNLINK ? "unlink" : "error")), + darg->dname, darg->ino, darg->parent_ino); + return 0; +} + +static int ext4_fc_handle_unlink(e2fsck_t ctx, struct ext4_fc_tl *tl, __u8 *val) +{ + struct dentry_info_args darg; + int ret; + + ret = tl_to_darg(&darg, tl, val); + if (ret) + return ret; + ext4_fc_flush_extents(ctx, darg.ino); + ret = errcode_to_errno( + ext2fs_unlink(ctx->fs, darg.parent_ino, + darg.dname, darg.ino, 0)); + /* It's okay if the above call fails */ + free(darg.dname); + return ret; +} + +static int ext4_fc_handle_link_and_create(e2fsck_t ctx, struct ext4_fc_tl *tl, __u8 *val) +{ + struct dentry_info_args darg; + ext2_filsys fs = ctx->fs; + struct ext2_inode_large inode_large; + int ret, filetype, mode; + + ret = tl_to_darg(&darg, tl, val); + if (ret) + return ret; + ext4_fc_flush_extents(ctx, 0); + ret = errcode_to_errno(ext2fs_read_inode(fs, darg.ino, + (struct ext2_inode *)&inode_large)); + if (ret) + goto out; + + mode = inode_large.i_mode; + + if (LINUX_S_ISREG(mode)) + filetype = EXT2_FT_REG_FILE; + else if (LINUX_S_ISDIR(mode)) + filetype = EXT2_FT_DIR; + else if (LINUX_S_ISCHR(mode)) + filetype = EXT2_FT_CHRDEV; + else if (LINUX_S_ISBLK(mode)) + filetype = EXT2_FT_BLKDEV; + else if (LINUX_S_ISLNK(mode)) + return EXT2_FT_SYMLINK; + else if (LINUX_S_ISFIFO(mode)) + filetype = EXT2_FT_FIFO; + else if (LINUX_S_ISSOCK(mode)) + filetype = EXT2_FT_SOCK; + else { + ret = -EINVAL; + goto out; + } + + /* + * Forcefully unlink if the same name is present and ignore the error + * if any, since this dirent might not exist + */ + ext2fs_unlink(fs, darg.parent_ino, darg.dname, darg.ino, + EXT2FS_UNLINK_FORCE); + + ret = errcode_to_errno( + ext2fs_link(fs, darg.parent_ino, darg.dname, darg.ino, + filetype)); +out: + free(darg.dname); + return ret; + +} + +/* This function fixes the i_blocks field in the replayed indoe */ +static void ext4_fc_replay_fixup_iblocks(struct ext2_inode_large *ondisk_inode, + struct ext2_inode_large *fc_inode) +{ + if (ondisk_inode->i_flags & EXT4_EXTENTS_FL) { + struct ext3_extent_header *eh; + + eh = (struct ext3_extent_header *)(&ondisk_inode->i_block[0]); + if (le16_to_cpu(eh->eh_magic) != EXT3_EXT_MAGIC) { + memset(eh, 0, sizeof(*eh)); + eh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC); + eh->eh_max = cpu_to_le16( + (sizeof(ondisk_inode->i_block) - + sizeof(struct ext3_extent_header)) / + sizeof(struct ext3_extent)); + } + } else if (ondisk_inode->i_flags & EXT4_INLINE_DATA_FL) { + memcpy(ondisk_inode->i_block, fc_inode->i_block, + sizeof(fc_inode->i_block)); + } +} + +static int ext4_fc_handle_inode(e2fsck_t ctx, __u8 *val) +{ + int ino, inode_len = EXT2_GOOD_OLD_INODE_SIZE; + struct ext2_inode_large *inode = NULL, *fc_inode = NULL; + __le32 fc_ino; + __u8 *fc_raw_inode; + errcode_t err; + blk64_t blks; + + memcpy(&fc_ino, val, sizeof(fc_ino)); + fc_raw_inode = val + sizeof(fc_ino); + ino = le32_to_cpu(fc_ino); + + if (EXT2_INODE_SIZE(ctx->fs->super) > EXT2_GOOD_OLD_INODE_SIZE) + inode_len += ext2fs_le16_to_cpu( + ((struct ext2_inode_large *)fc_raw_inode)->i_extra_isize); + err = ext2fs_get_mem(inode_len, &inode); + if (err) + goto out; + err = ext2fs_get_mem(inode_len, &fc_inode); + if (err) + goto out; + ext4_fc_flush_extents(ctx, ino); + + err = ext2fs_read_inode_full(ctx->fs, ino, (struct ext2_inode *)inode, + inode_len); + if (err) + goto out; + memcpy(fc_inode, fc_raw_inode, inode_len); +#ifdef WORDS_BIGENDIAN + ext2fs_swap_inode_full(ctx->fs, fc_inode, fc_inode, 0, inode_len); +#endif + memcpy(inode, fc_inode, offsetof(struct ext2_inode_large, i_block)); + memcpy(&inode->i_generation, &fc_inode->i_generation, + inode_len - offsetof(struct ext2_inode_large, i_generation)); + ext4_fc_replay_fixup_iblocks(inode, fc_inode); + err = ext2fs_count_blocks(ctx->fs, ino, EXT2_INODE(inode), &blks); + if (err) + goto out; + ext2fs_iblk_set(ctx->fs, EXT2_INODE(inode), blks); + ext2fs_inode_csum_set(ctx->fs, ino, inode); + + err = ext2fs_write_inode_full(ctx->fs, ino, (struct ext2_inode *)inode, + inode_len); + if (err) + goto out; + if (inode->i_links_count) + ext2fs_mark_inode_bitmap2(ctx->fs->inode_map, ino); + else + ext2fs_unmark_inode_bitmap2(ctx->fs->inode_map, ino); + ext2fs_mark_ib_dirty(ctx->fs); + +out: + ext2fs_free_mem(&inode); + ext2fs_free_mem(&fc_inode); + return errcode_to_errno(err); +} + +/* + * Handle add extent replay tag. + */ +static int ext4_fc_handle_add_extent(e2fsck_t ctx, __u8 *val) +{ + struct ext2fs_extent extent; + struct ext4_fc_add_range add_range; + ino_t ino; + int ret = 0; + + memcpy(&add_range, val, sizeof(add_range)); + ino = le32_to_cpu(add_range.fc_ino); + ext4_fc_flush_extents(ctx, ino); + + ret = ext4_fc_read_extents(ctx, ino); + if (ret) + return ret; + memset(&extent, 0, sizeof(extent)); + ret = errcode_to_errno(ext2fs_decode_extent( + &extent, (void *)add_range.fc_ex, + sizeof(add_range.fc_ex))); + if (ret) + return ret; + return ext4_add_extent_to_list(ctx, + &ctx->fc_replay_state.fc_extent_list, &extent); +} + +/* + * Handle delete logical range replay tag. + */ +static int ext4_fc_handle_del_range(e2fsck_t ctx, __u8 *val) +{ + struct ext2fs_extent extent; + struct ext4_fc_del_range del_range; + int ret, ino; + + memcpy(&del_range, val, sizeof(del_range)); + ino = le32_to_cpu(del_range.fc_ino); + ext4_fc_flush_extents(ctx, ino); + + memset(&extent, 0, sizeof(extent)); + extent.e_lblk = le32_to_cpu(del_range.fc_lblk); + extent.e_len = le32_to_cpu(del_range.fc_len); + ret = ext4_fc_read_extents(ctx, ino); + if (ret) + return ret; + return ext4_del_extent_from_list(ctx, + &ctx->fc_replay_state.fc_extent_list, &extent); +} + +/* + * Main recovery path entry point. This function returns JBD2_FC_REPLAY_CONTINUE + * to indicate that it is expecting more fast commit blocks. It returns + * JBD2_FC_REPLAY_STOP to indicate that replay is done. + */ +static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh, + enum passtype pass, int off, tid_t expected_tid) +{ + e2fsck_t ctx = journal->j_fs_dev->k_ctx; + struct e2fsck_fc_replay_state *state = &ctx->fc_replay_state; + int ret = JBD2_FC_REPLAY_CONTINUE; + struct ext4_fc_tl tl; + __u8 *start, *end, *cur, *val; + + if (pass == PASS_SCAN) { + state->fc_current_pass = PASS_SCAN; + return ext4_fc_replay_scan(journal, bh, off, expected_tid); + } + + if (state->fc_replay_num_tags == 0) + goto replay_done; + + if (state->fc_current_pass != pass) { + /* Starting replay phase */ + state->fc_current_pass = pass; + /* We will reset checksums */ + ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; + ret = errcode_to_errno(ext2fs_read_bitmaps(ctx->fs)); + if (ret) { + jbd_debug(1, "Error %d while reading bitmaps\n", ret); + return ret; + } + state->fc_super_state = ctx->fs->super->s_state; + /* + * Mark the file system to indicate it contains errors. That's + * because the updates performed by fast commit replay code are + * not atomic and may result in incosistent file system if it + * crashes before the replay is complete. + */ + ctx->fs->super->s_state |= EXT2_ERROR_FS; + ctx->fs->super->s_state |= EXT4_FC_REPLAY; + ext2fs_mark_super_dirty(ctx->fs); + ext2fs_flush(ctx->fs); + } + + start = (__u8 *)bh->b_data; + end = (__u8 *)bh->b_data + journal->j_blocksize - 1; + + for (cur = start; cur < end; cur = cur + le16_to_cpu(tl.fc_len) + sizeof(tl)) { + memcpy(&tl, cur, sizeof(tl)); + val = cur + sizeof(tl); + + if (state->fc_replay_num_tags == 0) + goto replay_done; + jbd_debug(3, "Replay phase processing %s tag\n", + tag2str(le16_to_cpu(tl.fc_tag))); + state->fc_replay_num_tags--; + switch (le16_to_cpu(tl.fc_tag)) { + case EXT4_FC_TAG_CREAT: + case EXT4_FC_TAG_LINK: + ret = ext4_fc_handle_link_and_create(ctx, &tl, val); + break; + case EXT4_FC_TAG_UNLINK: + ret = ext4_fc_handle_unlink(ctx, &tl, val); + break; + case EXT4_FC_TAG_ADD_RANGE: + ret = ext4_fc_handle_add_extent(ctx, val); + break; + case EXT4_FC_TAG_DEL_RANGE: + ret = ext4_fc_handle_del_range(ctx, val); + break; + case EXT4_FC_TAG_INODE: + ret = ext4_fc_handle_inode(ctx, val); + break; + case EXT4_FC_TAG_TAIL: + ext4_fc_flush_extents(ctx, 0); + case EXT4_FC_TAG_PAD: + case EXT4_FC_TAG_HEAD: + break; + default: + ret = -ECANCELED; + break; + } + if (ret < 0) + break; + ret = JBD2_FC_REPLAY_CONTINUE; + } + return ret; +replay_done: + jbd_debug(1, "End of fast commit replay\n"); + if (state->fc_current_pass != pass) + return JBD2_FC_REPLAY_STOP; + + ext2fs_calculate_summary_stats(ctx->fs, 0 /* update bg also */); + ext2fs_write_block_bitmap(ctx->fs); + ext2fs_write_inode_bitmap(ctx->fs); + ext2fs_mark_super_dirty(ctx->fs); + ext2fs_set_gdt_csum(ctx->fs); + ctx->fs->super->s_state = state->fc_super_state; + ext2fs_flush(ctx->fs); + + return JBD2_FC_REPLAY_STOP; +} + static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) { struct process_block_struct pb; @@ -289,9 +958,9 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) errcode_t retval = 0; io_manager io_ptr = 0; unsigned long long start = 0; + int ret; int ext_journal = 0; int tried_backup_jnl = 0; - blk64_t maxlen; clear_problem_context(&pctx); @@ -356,7 +1025,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) goto try_backup_journal; } if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize < - JFS_MIN_JOURNAL_BLOCKS) { + JBD2_MIN_JOURNAL_BLOCKS) { retval = EXT2_ET_JOURNAL_TOO_SMALL; goto try_backup_journal; } @@ -376,7 +1045,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) goto errout; } - journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) / + journal->j_total_len = EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize; #ifdef USE_INODE_IO @@ -390,8 +1059,10 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) #else journal->j_inode = j_inode; ctx->journal_io = ctx->fs->io; - if ((retval = journal_bmap(journal, 0, &start)) != 0) + if ((ret = jbd2_journal_bmap(journal, 0, &start)) != 0) { + retval = (errcode_t) (-1 * ret); goto errout; + } #endif } else { ext_journal = 1; @@ -442,29 +1113,30 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize); if (ext_journal) { - if (ctx->fs->blocksize == 1024) - start = 1; + blk64_t maxlen; + + start = ext2fs_journal_sb_start(ctx->fs->blocksize) - 1; bh = getblk(dev_journal, start, ctx->fs->blocksize); if (!bh) { retval = EXT2_ET_NO_MEMORY; goto errout; } - ll_rw_block(READ, 1, &bh); + ll_rw_block(REQ_OP_READ, 0, 1, &bh); if ((retval = bh->b_err) != 0) { brelse(bh); goto errout; } - memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024, + memcpy(&jsuper, start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET, 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)) { + !ext2fs_has_feature_journal_dev(&jsuper)) { fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx); retval = EXT2_ET_LOAD_EXT_JOURNAL; + brelse(bh); goto errout; } /* Make sure the journal UUID is correct */ @@ -472,13 +1144,32 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) sizeof(jsuper.s_uuid))) { fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx); retval = EXT2_ET_LOAD_EXT_JOURNAL; + brelse(bh); goto errout; } + /* Check the superblock checksum */ + if (ext2fs_has_feature_metadata_csum(&jsuper)) { + struct struct_ext2_filsys fsx; + struct ext2_super_block superx; + void *p; + + p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET; + memcpy(&fsx, ctx->fs, sizeof(fsx)); + memcpy(&superx, ctx->fs->super, sizeof(superx)); + fsx.super = &superx; + ext2fs_set_feature_metadata_csum(fsx.super); + if (!ext2fs_superblock_csum_verify(&fsx, p) && + fix_problem(ctx, PR_0_EXT_JOURNAL_SUPER_CSUM_INVALID, + &pctx)) { + ext2fs_superblock_csum_set(&fsx, p); + mark_buffer_dirty(bh); + } + } + brelse(bh); + maxlen = ext2fs_blocks_count(&jsuper); - if (maxlen > 1ULL << 32) - maxlen = (1ULL << 32) - 1; - journal->j_maxlen = maxlen; + journal->j_total_len = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1; start++; } @@ -489,6 +1180,10 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) journal->j_sb_buffer = bh; journal->j_superblock = (journal_superblock_t *)bh->b_data; + if (ext2fs_has_feature_fast_commit(ctx->fs->super)) + journal->j_fc_replay_callback = ext4_fc_replay; + else + journal->j_fc_replay_callback = NULL; #ifdef USE_INODE_IO if (j_inode) @@ -514,26 +1209,24 @@ static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx, struct problem_context *pctx) { struct ext2_super_block *sb = ctx->fs->super; - int recover = ctx->fs->super->s_feature_incompat & - EXT3_FEATURE_INCOMPAT_RECOVER; - int has_journal = ctx->fs->super->s_feature_compat & - EXT3_FEATURE_COMPAT_HAS_JOURNAL; + int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super); + int has_journal = ext2fs_has_feature_journal(ctx->fs->super); if (has_journal || sb->s_journal_inum) { /* The journal inode is bogus, remove and force full fsck */ pctx->ino = sb->s_journal_inum; if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) { if (has_journal && sb->s_journal_inum) - printf("*** ext3 journal has been deleted - " - "filesystem is now ext2 only ***\n\n"); - sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; + printf("*** journal has been deleted ***\n\n"); + ext2fs_clear_feature_journal(sb); sb->s_journal_inum = 0; + memset(sb->s_jnl_blocks, 0, sizeof(sb->s_jnl_blocks)); ctx->flags |= E2F_FLAG_JOURNAL_INODE; ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; e2fsck_clear_recover(ctx, 1); return 0; } - return EXT2_ET_BAD_INODE_NUM; + return EXT2_ET_CORRUPT_JOURNAL_SB; } else if (recover) { if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) { e2fsck_clear_recover(ctx, 1); @@ -555,6 +1248,7 @@ static void clear_v2_journal_fields(journal_t *journal) if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx)) return; + ctx->flags |= E2F_FLAG_PROBLEMS_FIXED; memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0, ctx->fs->blocksize-V1_SB_SIZE); mark_buffer_dirty(journal->j_sb_buffer); @@ -570,20 +1264,20 @@ static errcode_t e2fsck_journal_load(journal_t *journal) clear_problem_context(&pctx); - ll_rw_block(READ, 1, &jbh); + ll_rw_block(REQ_OP_READ, 0, 1, &jbh); if (jbh->b_err) { - com_err(ctx->device_name, jbh->b_err, + com_err(ctx->device_name, jbh->b_err, "%s", _("reading journal superblock\n")); return jbh->b_err; } jsb = journal->j_superblock; - /* If we don't even have JFS_MAGIC, we probably have a wrong inode */ - if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER)) + /* If we don't even have JBD2_MAGIC, we probably have a wrong inode */ + if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER)) return e2fsck_journal_fix_bad_inode(ctx, &pctx); switch (ntohl(jsb->s_header.h_blocktype)) { - case JFS_SUPERBLOCK_V1: + case JBD2_SUPERBLOCK_V1: journal->j_format_version = 1; if (jsb->s_feature_compat || jsb->s_feature_incompat || @@ -592,7 +1286,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal) clear_v2_journal_fields(journal); break; - case JFS_SUPERBLOCK_V2: + case JBD2_SUPERBLOCK_V2: journal->j_format_version = 2; if (ntohl(jsb->s_nr_users) > 1 && uuid_is_null(ctx->fs->super->s_journal_uuid)) @@ -607,10 +1301,10 @@ static errcode_t e2fsck_journal_load(journal_t *journal) * These should never appear in a journal super block, so if * they do, the journal is badly corrupted. */ - case JFS_DESCRIPTOR_BLOCK: - case JFS_COMMIT_BLOCK: - case JFS_REVOKE_BLOCK: - return EXT2_ET_CORRUPT_SUPERBLOCK; + case JBD2_DESCRIPTOR_BLOCK: + case JBD2_COMMIT_BLOCK: + case JBD2_REVOKE_BLOCK: + return EXT2_ET_CORRUPT_JOURNAL_SB; /* 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 @@ -619,45 +1313,66 @@ static errcode_t e2fsck_journal_load(journal_t *journal) return EXT2_ET_JOURNAL_UNSUPP_VERSION; } - if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) + if (JBD2_HAS_INCOMPAT_FEATURE(journal, ~JBD2_KNOWN_INCOMPAT_FEATURES)) return EXT2_ET_UNSUPP_FEATURE; - if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) + if (JBD2_HAS_RO_COMPAT_FEATURE(journal, ~JBD2_KNOWN_ROCOMPAT_FEATURES)) return EXT2_ET_RO_UNSUPP_FEATURE; - /* Checksum v1 and v2 are mutually exclusive features. */ - if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V2) && - JFS_HAS_COMPAT_FEATURE(journal, JFS_FEATURE_COMPAT_CHECKSUM)) - return EXT2_ET_CORRUPT_SUPERBLOCK; + /* Checksum v1-3 are mutually exclusive features. */ + if (jbd2_has_feature_csum2(journal) && jbd2_has_feature_csum3(journal)) + return EXT2_ET_CORRUPT_JOURNAL_SB; + + if (jbd2_journal_has_csum_v2or3(journal) && + jbd2_has_feature_checksum(journal)) + return EXT2_ET_CORRUPT_JOURNAL_SB; if (!e2fsck_journal_verify_csum_type(journal, jsb) || !e2fsck_journal_sb_csum_verify(journal, jsb)) - return EXT2_ET_CORRUPT_SUPERBLOCK; + return EXT2_ET_CORRUPT_JOURNAL_SB; + + if (jbd2_journal_has_csum_v2or3(journal)) + journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid, + sizeof(jsb->s_uuid)); /* We have now checked whether we know enough about the journal * format to be able to proceed safely, so any other checks that * fail we should attempt to recover from. */ if (jsb->s_blocksize != htonl(journal->j_blocksize)) { - com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK, + com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB, _("%s: no valid journal superblock found\n"), ctx->device_name); - return EXT2_ET_CORRUPT_SUPERBLOCK; + return EXT2_ET_CORRUPT_JOURNAL_SB; } - if (ntohl(jsb->s_maxlen) < journal->j_maxlen) - journal->j_maxlen = ntohl(jsb->s_maxlen); - else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) { - com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK, + if (ntohl(jsb->s_maxlen) < journal->j_total_len) + journal->j_total_len = ntohl(jsb->s_maxlen); + else if (ntohl(jsb->s_maxlen) > journal->j_total_len) { + com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB, _("%s: journal too short\n"), ctx->device_name); - return EXT2_ET_CORRUPT_SUPERBLOCK; + return EXT2_ET_CORRUPT_JOURNAL_SB; } journal->j_tail_sequence = ntohl(jsb->s_sequence); journal->j_transaction_sequence = journal->j_tail_sequence; journal->j_tail = ntohl(jsb->s_start); journal->j_first = ntohl(jsb->s_first); - journal->j_last = ntohl(jsb->s_maxlen); + if (jbd2_has_feature_fast_commit(journal)) { + if (ntohl(jsb->s_maxlen) - jbd2_journal_get_num_fc_blks(jsb) + < JBD2_MIN_JOURNAL_BLOCKS) { + com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB, + _("%s: incorrect fast commit blocks\n"), + ctx->device_name); + return EXT2_ET_CORRUPT_JOURNAL_SB; + } + journal->j_fc_last = ntohl(jsb->s_maxlen); + journal->j_last = journal->j_fc_last - + jbd2_journal_get_num_fc_blks(jsb); + journal->j_fc_first = journal->j_last + 1; + } else { + journal->j_last = ntohl(jsb->s_maxlen); + } return 0; } @@ -677,10 +1392,10 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, * 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); - jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2); + if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER) || + jsb->s_header.h_blocktype != htonl(JBD2_SUPERBLOCK_V1)) { + jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER); + jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2); } /* Zero out everything else beyond the superblock header */ @@ -689,7 +1404,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t)); jsb->s_blocksize = htonl(ctx->fs->blocksize); - jsb->s_maxlen = htonl(journal->j_maxlen); + jsb->s_maxlen = htonl(journal->j_total_len); jsb->s_first = htonl(1); /* Initialize the journal sequence number so that there is "no" @@ -704,7 +1419,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, e2fsck_journal_sb_csum_set(journal, jsb); mark_buffer_dirty(journal->j_sb_buffer); - ll_rw_block(WRITE, 1, &journal->j_sb_buffer); + ll_rw_block(REQ_OP_WRITE, 0, 1, &journal->j_sb_buffer); } static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, @@ -712,10 +1427,9 @@ static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, struct problem_context *pctx) { struct ext2_super_block *sb = ctx->fs->super; - int recover = ctx->fs->super->s_feature_incompat & - EXT3_FEATURE_INCOMPAT_RECOVER; + int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super); - if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) { + if (ext2fs_has_feature_journal(sb)) { if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) { e2fsck_journal_reset_super(ctx, journal->j_superblock, journal); @@ -723,9 +1437,9 @@ static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, e2fsck_clear_recover(ctx, recover); return 0; } - return EXT2_ET_CORRUPT_SUPERBLOCK; + return EXT2_ET_CORRUPT_JOURNAL_SB; } else if (e2fsck_journal_fix_bad_inode(ctx, pctx)) - return EXT2_ET_CORRUPT_SUPERBLOCK; + return EXT2_ET_CORRUPT_JOURNAL_SB; return 0; } @@ -739,7 +1453,7 @@ static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, mark_buffer_clean(journal->j_sb_buffer); else if (!(ctx->options & E2F_OPT_READONLY)) { jsb = journal->j_superblock; - jsb->s_sequence = htonl(journal->j_transaction_sequence); + jsb->s_sequence = htonl(journal->j_tail_sequence); if (reset) jsb->s_start = 0; /* this marks the journal as empty */ e2fsck_journal_sb_csum_set(journal, jsb); @@ -766,19 +1480,18 @@ static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, * This function makes sure that the superblock fields regarding the * journal are consistent. */ -int e2fsck_check_ext3_journal(e2fsck_t ctx) +errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx) { struct ext2_super_block *sb = ctx->fs->super; journal_t *journal; - int recover = ctx->fs->super->s_feature_incompat & - EXT3_FEATURE_INCOMPAT_RECOVER; + int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super); struct problem_context pctx; problem_t problem; int reset = 0, force_fsck = 0; - int retval; + errcode_t retval; /* If we don't have any journal features, don't do anything more */ - if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && + if (!ext2fs_has_feature_journal(sb) && !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 && uuid_is_null(sb->s_journal_uuid)) return 0; @@ -798,7 +1511,7 @@ int e2fsck_check_ext3_journal(e2fsck_t ctx) retval = e2fsck_journal_load(journal); if (retval) { - if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) || + if ((retval == EXT2_ET_CORRUPT_JOURNAL_SB) || ((retval == EXT2_ET_UNSUPP_FEATURE) && (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT, &pctx))) || @@ -819,9 +1532,8 @@ int e2fsck_check_ext3_journal(e2fsck_t ctx) * with -y, -n, or -p, only if a user isn't making up their mind. */ no_has_journal: - if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { - recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER; - pctx.str = "inode"; + if (!ext2fs_has_feature_journal(sb)) { + recover = ext2fs_has_feature_journal_needs_recovery(sb); if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) { if (recover && !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx)) @@ -839,14 +1551,14 @@ no_has_journal: sizeof(sb->s_journal_uuid)); e2fsck_clear_recover(ctx, force_fsck); } else if (!(ctx->options & E2F_OPT_READONLY)) { - sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; + ext2fs_set_feature_journal(sb); ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; ext2fs_mark_super_dirty(ctx->fs); } } - if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL && - !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) && + if (ext2fs_has_feature_journal(sb) && + !ext2fs_has_feature_journal_needs_recovery(sb) && journal->j_superblock->s_start != 0) { /* Print status information */ fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx); @@ -856,8 +1568,7 @@ no_has_journal: 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_set_feature_journal_needs_recovery(sb); ext2fs_mark_super_dirty(ctx->fs); } else if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) { @@ -883,7 +1594,7 @@ no_has_journal: * the journal's errno is set; if so, we need to mark the file * system as being corrupt and clear the journal's s_errno. */ - if (!(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) && + if (!ext2fs_has_feature_journal_needs_recovery(sb) && journal->j_superblock->s_errno) { ctx->fs->super->s_state |= EXT2_ERROR_FS; ext2fs_mark_super_dirty(ctx->fs); @@ -900,11 +1611,18 @@ static errcode_t recover_ext3_journal(e2fsck_t ctx) { struct problem_context pctx; journal_t *journal; - int retval; + errcode_t retval; clear_problem_context(&pctx); - journal_init_revoke_caches(); + retval = jbd2_journal_init_revoke_record_cache(); + if (retval) + return retval; + + retval = jbd2_journal_init_revoke_table_cache(); + if (retval) + return retval; + retval = e2fsck_get_journal(ctx, &journal); if (retval) return retval; @@ -913,11 +1631,11 @@ static errcode_t recover_ext3_journal(e2fsck_t ctx) if (retval) goto errout; - retval = journal_init_revoke(journal, 1024); + retval = jbd2_journal_init_revoke(journal, 1024); if (retval) goto errout; - retval = -journal_recover(journal); + retval = -jbd2_journal_recover(journal); if (retval) goto errout; @@ -928,14 +1646,17 @@ static errcode_t recover_ext3_journal(e2fsck_t ctx) mark_buffer_dirty(journal->j_sb_buffer); } + journal->j_tail_sequence = journal->j_transaction_sequence; + errout: - journal_destroy_revoke(journal); - journal_destroy_revoke_caches(); + jbd2_journal_destroy_revoke(journal); + jbd2_journal_destroy_revoke_record_cache(); + jbd2_journal_destroy_revoke_table_cache(); e2fsck_journal_release(ctx, journal, 1, 0); return retval; } -int e2fsck_run_ext3_journal(e2fsck_t ctx) +errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx) { io_manager io_ptr = ctx->fs->io->manager; int blocksize = ctx->fs->blocksize; @@ -967,7 +1688,7 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx) ext2fs_mmp_stop(ctx->fs); ext2fs_free(ctx->fs); - retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW, + retval = ext2fs_open(ctx->filesystem_name, ctx->openfs_flags, ctx->superblock, blocksize, io_ptr, &ctx->fs); if (retval) { @@ -982,7 +1703,7 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx) ctx->fs->super->s_kbytes_written += kbytes_written; /* Set the superblock flags */ - e2fsck_clear_recover(ctx, recover_retval); + e2fsck_clear_recover(ctx, recover_retval != 0); /* * Do one last sanity check, and propagate journal->s_errno to @@ -1008,7 +1729,8 @@ void e2fsck_move_ext3_journal(e2fsck_t ctx) ext2_ino_t ino; errcode_t retval; const char * const * cpp; - int group, mount_flags; + dgrp_t group; + int mount_flags; clear_problem_context(&pctx); @@ -1018,7 +1740,7 @@ void e2fsck_move_ext3_journal(e2fsck_t ctx) */ if ((ctx->options & E2F_OPT_READONLY) || (sb->s_journal_inum == 0) || - !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) + !ext2fs_has_feature_journal(sb)) return; /* @@ -1130,7 +1852,7 @@ int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx) char uuid[37], *journal_name; struct stat st; - if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) || + if (!ext2fs_has_feature_journal(sb) || uuid_is_null(sb->s_journal_uuid)) return 0; @@ -1139,8 +1861,10 @@ int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx) if (!journal_name) return 0; - if (stat(journal_name, &st) < 0) + if (stat(journal_name, &st) < 0) { + free(journal_name); return 0; + } if (st.st_rdev != sb->s_journal_dev) { clear_problem_context(&pctx);