From: Theodore Ts'o Date: Mon, 16 Dec 2013 06:35:56 +0000 (-0500) Subject: Merge branch 'maint' into next X-Git-Tag: v1.43-WIP-2015-05-18~365 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=e4681bca170094430ef173b67345d2d8f0cfcea8;p=tools%2Fe2fsprogs.git Merge branch 'maint' into next Conflicts: debugfs/debugfs.8.in --- e4681bca170094430ef173b67345d2d8f0cfcea8 diff --cc debugfs/debugfs.8.in index 65a30d5,0153fa8..7ff4155 --- a/debugfs/debugfs.8.in +++ b/debugfs/debugfs.8.in @@@ -409,19 -416,16 +421,19 @@@ an .I \-b options. .TP - .I ls [-l] [-c] [-d] [-p] filespec -.BI ls " [-d] [-l] [-p] filespec" ++.BI ls " [-l] [-c] [-d] [-p] filespec" Print a listing of the files in the directory .IR filespec . - The - .I \-l - flag will list files using a more verbose format. The +.I \-c +flag causes directory block checksums (if present) to be displayed. +The .I \-d flag will list deleted entries in the directory. - The + The + .I \-l + flag will list files using a more verbose format. + The .I \-p flag will list the files in a format which is more easily parsable by scripts, as well as making it more clear when there are spaces or other diff --cc e2fsck/rehash.c index 20704a9,3aafbb1..9b90353 --- a/e2fsck/rehash.c +++ b/e2fsck/rehash.c @@@ -704,32 -645,28 +704,40 @@@ static int write_dir_block(ext2_filsys if (*block_nr == 0) return 0; - if (blockcnt >= wd->outdir->num) { + if (blockcnt < 0) + return 0; + if (blockcnt < wd->outdir->num) + dir = wd->outdir->buf + (blockcnt * fs->blocksize); + else if (wd->ctx->lost_and_found == wd->dir) { + /* Don't release any extra directory blocks for lost+found */ + wd->err = ext2fs_new_dir_block(fs, 0, 0, &buf); + if (wd->err) + return BLOCK_ABORT; + dir = buf; + wd->outdir->num++; + } else { + /* We don't need this block, so release it */ e2fsck_read_bitmaps(wd->ctx); blk = *block_nr; - ext2fs_unmark_block_bitmap2(wd->ctx->block_found_map, blk); - ext2fs_block_alloc_stats2(fs, blk, -1); + /* + * In theory, we only release blocks from the end of the + * directory file, so it's fine to clobber a whole cluster at + * once. + */ + if (blk % EXT2FS_CLUSTER_RATIO(fs) == 0) { + ext2fs_unmark_block_bitmap2(wd->ctx->block_found_map, + blk); + ext2fs_block_alloc_stats2(fs, blk, -1); + wd->cleared++; + } *block_nr = 0; - wd->cleared++; return BLOCK_CHANGED; } - if (blockcnt < 0) - return 0; - dir = wd->outdir->buf + (blockcnt * fs->blocksize); - wd->err = ext2fs_write_dir_block3(fs, *block_nr, dir, 0); + wd->err = ext2fs_write_dir_block4(fs, *block_nr, dir, 0, wd->dir); + if (buf) + ext2fs_free_mem(&buf); + if (wd->err) return BLOCK_ABORT; return 0; diff --cc misc/mke2fs.c index c1cbcaa,b7b93b0..4075099 --- a/misc/mke2fs.c +++ b/misc/mke2fs.c @@@ -2458,29 -2383,10 +2461,30 @@@ int main (int argc, char *argv[] flags |= EXT2_FLAG_PRINT_PROGRESS; retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs); if (retval) { - com_err(device_name, retval, _("while setting up superblock")); + com_err(device_name, retval, "%s", + _("while setting up superblock")); exit(1); } + fs->progress_ops = &ext2fs_numeric_progress_ops; + + /* Check the user's mkfs options for metadata checksumming */ + if (!quiet && + EXT2_HAS_RO_COMPAT_FEATURE(fs->super, + EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { + if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super, + EXT3_FEATURE_INCOMPAT_EXTENTS)) + printf(_("Extents are not enabled. The file extent " + "tree can be checksummed, whereas block maps " + "cannot. Not enabling extents reduces the " + "coverage of metadata checksumming. " + "Pass -O extents to rectify.\n")); + if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super, + EXT4_FEATURE_INCOMPAT_64BIT)) + printf(_("64-bit filesystem support is not " + "enabled. The larger fields afforded by " + "this feature enable full-strength " + "checksumming. Pass -O 64bit to rectify.\n")); + } /* Calculate journal blocks */ if (!journal_device && ((journal_size) || diff --cc misc/tune2fs.c index a84e6d6,568fb30..0eddf6d --- a/misc/tune2fs.c +++ b/misc/tune2fs.c @@@ -394,461 -381,9 +398,461 @@@ static void request_fsck_afterwards(ext fs->super->s_state &= ~EXT2_VALID_FS; printf("\n%s\n", _(please_fsck)); if (mount_flags & EXT2_MF_READONLY) - printf(_("(and reboot afterwards!)\n")); + printf("%s", _("(and reboot afterwards!)\n")); } +/* Rewrite extents */ +static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino, + struct ext2_inode *inode) +{ + ext2_extent_handle_t handle; + struct ext2fs_extent extent; + int op = EXT2_EXTENT_ROOT; + errcode_t errcode; + + if (!(inode->i_flags & EXT4_EXTENTS_FL)) + return 0; + + errcode = ext2fs_extent_open(fs, ino, &handle); + if (errcode) + return errcode; + + while (1) { + errcode = ext2fs_extent_get(handle, op, &extent); + if (errcode) + break; + + /* Root node is in the separately checksummed inode */ + if (op == EXT2_EXTENT_ROOT) { + op = EXT2_EXTENT_NEXT; + continue; + } + op = EXT2_EXTENT_NEXT; + + /* Only visit the first extent in each extent block */ + if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) + continue; + errcode = ext2fs_extent_replace(handle, 0, &extent); + if (errcode) + break; + } + + /* Ok if we run off the end */ + if (errcode == EXT2_ET_EXTENT_NO_NEXT) + errcode = 0; + return errcode; +} + +/* + * Rewrite directory blocks with checksums + */ +struct rewrite_dir_context { + char *buf; + errcode_t errcode; + ext2_ino_t dir; + int is_htree; +}; + +static int rewrite_dir_block(ext2_filsys fs, + blk64_t *blocknr, + e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), + blk64_t ref_block EXT2FS_ATTR((unused)), + int ref_offset EXT2FS_ATTR((unused)), + void *priv_data) +{ + struct ext2_dx_countlimit *dcl = NULL; + struct rewrite_dir_context *ctx = priv_data; + int dcl_offset, changed = 0; + + ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0, + ctx->dir); + if (ctx->errcode) + return BLOCK_ABORT; + + /* if htree node... */ + if (ctx->is_htree) + ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf, + &dcl, &dcl_offset); + if (dcl) { + if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, + EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { + /* Ensure limit is the max size */ + int max_entries = (fs->blocksize - dcl_offset) / + sizeof(struct ext2_dx_entry); + if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) { + changed = 1; + dcl->limit = ext2fs_cpu_to_le16(max_entries); + } + } else { + /* If htree block is full then rebuild the dir */ + if (ext2fs_le16_to_cpu(dcl->count) == + ext2fs_le16_to_cpu(dcl->limit)) { + request_dir_fsck_afterwards(fs); + return 0; + } + /* + * Ensure dcl->limit is small enough to leave room for + * the checksum tail. + */ + int max_entries = (fs->blocksize - (dcl_offset + + sizeof(struct ext2_dx_tail))) / + sizeof(struct ext2_dx_entry); + if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) + dcl->limit = ext2fs_cpu_to_le16(max_entries); + /* Always rewrite checksum */ + changed = 1; + } + } else { + unsigned int rec_len, name_size; + char *top = ctx->buf + fs->blocksize; + struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf; + struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL; + + /* Find last and penultimate dirent */ + while ((char *)de < top) { + penultimate_de = last_de; + last_de = de; + ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len); + if (!ctx->errcode && !rec_len) + ctx->errcode = EXT2_ET_DIR_CORRUPTED; + if (ctx->errcode) + return BLOCK_ABORT; + de = (struct ext2_dir_entry *)(((char *)de) + rec_len); + } + ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len); + if (ctx->errcode) + return BLOCK_ABORT; + name_size = ext2fs_dirent_name_len(last_de); + + if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, + EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { + if (!penultimate_de) + return 0; + if (last_de->inode || + name_size || + rec_len != sizeof(struct ext2_dir_entry_tail)) + return 0; + /* + * The last dirent is unused and the right length to + * have stored a checksum. Erase it. + */ + ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de, + &rec_len); + if (!rec_len) + ctx->errcode = EXT2_ET_DIR_CORRUPTED; + if (ctx->errcode) + return BLOCK_ABORT; + ext2fs_set_rec_len(fs, rec_len + + sizeof(struct ext2_dir_entry_tail), + penultimate_de); + changed = 1; + } else { + unsigned csum_size = sizeof(struct ext2_dir_entry_tail); + struct ext2_dir_entry_tail *t; + + /* + * If the last dirent looks like the tail, just update + * the checksum. + */ + if (!last_de->inode && + rec_len == csum_size) { + t = (struct ext2_dir_entry_tail *)last_de; + t->det_reserved_name_len = + EXT2_DIR_NAME_LEN_CSUM; + changed = 1; + goto out; + } + if (name_size & 3) + name_size = (name_size & ~3) + 4; + /* If there's not enough space for the tail, e2fsck */ + if (rec_len <= (8 + name_size + csum_size)) { + request_dir_fsck_afterwards(fs); + return 0; + } + /* Shorten that last de and insert the tail */ + ext2fs_set_rec_len(fs, rec_len - csum_size, last_de); + t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize); + ext2fs_initialize_dirent_tail(fs, t); + + /* Always update checksum */ + changed = 1; + } + } + +out: + if (!changed) + return 0; + + ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf, + 0, ctx->dir); + if (ctx->errcode) + return BLOCK_ABORT; + + return 0; +} + +static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir, + struct ext2_inode *inode) +{ + errcode_t retval; + struct rewrite_dir_context ctx; + + retval = ext2fs_get_mem(fs->blocksize, &ctx.buf); + if (retval) + return retval; + + ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL); + ctx.dir = dir; + ctx.errcode = 0; + retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY | + BLOCK_FLAG_DATA_ONLY, + 0, rewrite_dir_block, &ctx); + + ext2fs_free_mem(&ctx.buf); + if (retval) + return retval; + + return ctx.errcode; +} + +/* + * Forcibly set checksums in all inodes. + */ +static void rewrite_inodes(ext2_filsys fs) +{ + int length = EXT2_INODE_SIZE(fs->super); + struct ext2_inode *inode, *zero; + char *ea_buf; + ext2_inode_scan scan; + errcode_t retval; + ext2_ino_t ino; + blk64_t file_acl_block; + int inode_dirty; + + if (fs->super->s_creator_os != EXT2_OS_LINUX) + return; + + retval = ext2fs_open_inode_scan(fs, 0, &scan); + if (retval) { + com_err("set_csum", retval, "while opening inode scan"); + exit(1); + } + + retval = ext2fs_get_mem(length, &inode); + if (retval) { + com_err("set_csum", retval, "while allocating memory"); + exit(1); + } + + retval = ext2fs_get_memzero(length, &zero); + if (retval) { + com_err("set_csum", retval, "while allocating memory"); + exit(1); + } + + retval = ext2fs_get_mem(fs->blocksize, &ea_buf); + if (retval) { + com_err("set_csum", retval, "while allocating memory"); + exit(1); + } + + do { + retval = ext2fs_get_next_inode_full(scan, &ino, inode, length); + if (retval) { + com_err("set_csum", retval, "while getting next inode"); + exit(1); + } + if (!ino) + break; + if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) { + inode_dirty = 1; + } else { + if (memcmp(inode, zero, length) != 0) { + memset(inode, 0, length); + inode_dirty = 1; + } else { + inode_dirty = 0; + } + } + + if (inode_dirty) { + retval = ext2fs_write_inode_full(fs, ino, inode, + length); + if (retval) { + com_err("set_csum", retval, "while writing " + "inode"); + exit(1); + } + } + + retval = rewrite_extents(fs, ino, inode); + if (retval) { + com_err("rewrite_extents", retval, + "while rewriting extents"); + exit(1); + } + + if (LINUX_S_ISDIR(inode->i_mode)) { + retval = rewrite_directory(fs, ino, inode); + if (retval) { + com_err("rewrite_directory", retval, + "while rewriting directories"); + exit(1); + } + } + + file_acl_block = ext2fs_file_acl_block(fs, inode); + if (!file_acl_block) + continue; + retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino); + if (retval) { + com_err("rewrite_eablock", retval, + "while rewriting extended attribute"); + exit(1); + } + retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf, + ino); + if (retval) { + com_err("rewrite_eablock", retval, + "while rewriting extended attribute"); + exit(1); + } + } while (ino); + + ext2fs_free_mem(&zero); + ext2fs_free_mem(&inode); + ext2fs_free_mem(&ea_buf); + ext2fs_close_inode_scan(scan); +} + +static void rewrite_metadata_checksums(ext2_filsys fs) +{ + dgrp_t i; + + fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; + ext2fs_init_csum_seed(fs); + for (i = 0; i < fs->group_desc_count; i++) + ext2fs_group_desc_csum_set(fs, i); + ext2fs_read_bitmaps(fs); + rewrite_inodes(fs); + ext2fs_mark_ib_dirty(fs); + ext2fs_mark_bb_dirty(fs); + ext2fs_mmp_update2(fs, 1); + fs->flags &= ~EXT2_FLAG_SUPER_ONLY; + fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS; + if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, + EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) + fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM; + else + fs->super->s_checksum_type = 0; + ext2fs_mark_super_dirty(fs); +} + +static void enable_uninit_bg(ext2_filsys fs) +{ + struct ext2_group_desc *gd; + dgrp_t i; + + for (i = 0; i < fs->group_desc_count; i++) { + gd = ext2fs_group_desc(fs, fs->group_desc, i); + gd->bg_itable_unused = 0; + gd->bg_flags = EXT2_BG_INODE_ZEROED; + ext2fs_group_desc_csum_set(fs, i); + } + fs->flags &= ~EXT2_FLAG_SUPER_ONLY; +} + +static errcode_t zero_empty_inodes(ext2_filsys fs) +{ + int length = EXT2_INODE_SIZE(fs->super); + struct ext2_inode *inode; + ext2_inode_scan scan; + errcode_t retval; + ext2_ino_t ino; + + retval = ext2fs_open_inode_scan(fs, 0, &scan); + if (retval) + goto out; + + retval = ext2fs_get_mem(length, &inode); + if (retval) + goto out; + + do { + retval = ext2fs_get_next_inode_full(scan, &ino, inode, length); + if (retval) + goto out; + if (!ino) + break; + if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) { + memset(inode, 0, length); + retval = ext2fs_write_inode_full(fs, ino, inode, + length); + if (retval) + goto out; + } + } while (1); + +out: + ext2fs_free_mem(&inode); + ext2fs_close_inode_scan(scan); + return retval; +} + +static void disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag) +{ + struct ext2_group_desc *gd; + dgrp_t i; + errcode_t retval; + blk64_t b, c, d; + int has_super; + + /* Load bitmaps to ensure that the uninit ones get written out */ + fs->super->s_feature_ro_compat |= csum_feature_flag; + ext2fs_read_bitmaps(fs); + ext2fs_mark_ib_dirty(fs); + ext2fs_mark_bb_dirty(fs); + fs->super->s_feature_ro_compat &= ~csum_feature_flag; + + /* If we're only turning off uninit_bg, zero the inodes */ + if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { + retval = zero_empty_inodes(fs); + if (retval) { + com_err("disable_uninit_bg", retval, + "while zeroing unused inodes"); + request_fsck_afterwards(fs); + } + } + + /* The bbitmap is zeroed; we must mark group metadata blocks in use */ + for (i = 0; i < fs->group_desc_count; i++) { + b = ext2fs_block_bitmap_loc(fs, i); + ext2fs_mark_block_bitmap2(fs->block_map, b); + b = ext2fs_inode_bitmap_loc(fs, i); + ext2fs_mark_block_bitmap2(fs->block_map, b); + + retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL); + if (retval == 0 && b) + ext2fs_mark_block_bitmap2(fs->block_map, b); + if (retval == 0 && c) + ext2fs_mark_block_bitmap2(fs->block_map, c); + if (retval == 0 && d) + ext2fs_mark_block_bitmap2(fs->block_map, d); + if (retval) { + com_err("disable_uninit_bg", retval, + "while initializing block bitmaps"); + request_fsck_afterwards(fs); + } + + gd = ext2fs_group_desc(fs, fs->group_desc, i); + gd->bg_itable_unused = 0; + gd->bg_flags = 0; + ext2fs_group_desc_csum_set(fs, i); + } + fs->flags &= ~EXT2_FLAG_SUPER_ONLY; + ext2fs_mark_super_dirty(fs); +} + /* * Update the feature set as provided by the user. */