Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / misc / tune2fs.c
index 17c1565..f52beab 100644 (file)
@@ -448,7 +448,7 @@ struct rewrite_dir_context {
 
 static int rewrite_dir_block(ext2_filsys fs,
                             blk64_t    *blocknr,
-                            e2_blkcnt_t blockcnt,
+                            e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
                             blk64_t    ref_block EXT2FS_ATTR((unused)),
                             int        ref_offset EXT2FS_ATTR((unused)),
                             void       *priv_data)
@@ -510,12 +510,12 @@ static int rewrite_dir_block(ext2_filsys fs,
                                ctx->errcode = EXT2_ET_DIR_CORRUPTED;
                        if (ctx->errcode)
                                return BLOCK_ABORT;
-                       de = (struct ext2_dir_entry *)(((void *)de) + rec_len);
+                       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 = last_de->name_len & 0xFF;
+               name_size = ext2fs_dirent_name_len(last_de);
 
                if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
                                EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
@@ -540,7 +540,7 @@ static int rewrite_dir_block(ext2_filsys fs,
                                        penultimate_de);
                        changed = 1;
                } else {
-                       int csum_size = sizeof(struct ext2_dir_entry_tail);
+                       unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
                        struct ext2_dir_entry_tail *t;
 
                        /*
@@ -584,8 +584,8 @@ out:
        return 0;
 }
 
-errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
-                           struct ext2_inode *inode)
+static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
+                                  struct ext2_inode *inode)
 {
        errcode_t       retval;
        struct rewrite_dir_context ctx;
@@ -614,12 +614,13 @@ errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
 static void rewrite_inodes(ext2_filsys fs)
 {
        int length = EXT2_INODE_SIZE(fs->super);
-       struct ext2_inode *inode;
+       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;
@@ -636,6 +637,12 @@ static void rewrite_inodes(ext2_filsys fs)
                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");
@@ -650,11 +657,25 @@ static void rewrite_inodes(ext2_filsys fs)
                }
                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;
+                       }
+               }
 
-               retval = ext2fs_write_inode_full(fs, ino, inode, length);
-               if (retval) {
-                       com_err("set_csum", retval, "while writing inode");
-                       exit(1);
+               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);
@@ -691,6 +712,7 @@ static void rewrite_inodes(ext2_filsys fs)
                }
        } while (ino);
 
+       ext2fs_free_mem(&zero);
        ext2fs_free_mem(&inode);
        ext2fs_free_mem(&ea_buf);
        ext2fs_close_inode_scan(scan);
@@ -698,14 +720,14 @@ static void rewrite_inodes(ext2_filsys fs)
 
 static void rewrite_metadata_checksums(ext2_filsys fs)
 {
-       int i;
+       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);
-       rewrite_inodes(fs);
        ext2fs_read_bitmaps(fs);
+       rewrite_inodes(fs);
        ext2fs_mark_ib_dirty(fs);
        ext2fs_mark_bb_dirty(fs);
        ext2fs_mmp_update2(fs, 1);
@@ -722,7 +744,7 @@ static void rewrite_metadata_checksums(ext2_filsys fs)
 static void enable_uninit_bg(ext2_filsys fs)
 {
        struct ext2_group_desc *gd;
-       int i;
+       dgrp_t i;
 
        for (i = 0; i < fs->group_desc_count; i++) {
                gd = ext2fs_group_desc(fs, fs->group_desc, i);
@@ -733,10 +755,50 @@ static void enable_uninit_bg(ext2_filsys fs)
        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;
-       int i;
+       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;
@@ -745,21 +807,46 @@ static void disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
        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++) {
-               gd = ext2fs_group_desc(fs, fs->group_desc, i);
-               if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
-                       /*
-                        * XXX what we really should do is zap
-                        * uninitialized inode tables instead.
-                        */
+               b = ext2fs_block_bitmap_loc(fs, i);
+               ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, b));
+               b = ext2fs_inode_bitmap_loc(fs, i);
+               ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, b));
+
+               retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
+               if (retval == 0 && b)
+                       ext2fs_mark_block_bitmap2(fs->block_map,
+                                                 EXT2FS_B2C(fs, b));
+               if (retval == 0 && c)
+                       ext2fs_mark_block_bitmap2(fs->block_map,
+                                                 EXT2FS_B2C(fs, c));
+               if (retval == 0 && d)
+                       ext2fs_mark_block_bitmap2(fs->block_map,
+                                                 EXT2FS_B2C(fs, d));
+               if (retval) {
+                       com_err("disable_uninit_bg", retval,
+                               "while initializing block bitmaps");
                        request_fsck_afterwards(fs);
-                       break;
                }
+
+               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);
 }
 
 /*
@@ -768,9 +855,8 @@ static void disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
 static int update_feature_set(ext2_filsys fs, char *features)
 {
        struct ext2_super_block *sb = fs->super;
-       struct ext2_group_desc *gd;
        __u32           old_features[3];
-       int             i, type_err;
+       int             type_err;
        unsigned int    mask_err;
 
 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
@@ -889,7 +975,7 @@ static int update_feature_set(ext2_filsys fs, char *features)
 
                /* We need to force out the group descriptors as well */
                fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
-               ext2fs_block_alloc_stats(fs, sb->s_mmp_block, -1);
+               ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
 mmp_error:
                sb->s_mmp_block = 0;
                sb->s_mmp_update_interval = 0;
@@ -1128,7 +1214,7 @@ err:
        return 1;
 }
 
-void handle_quota_options(ext2_filsys fs)
+static void handle_quota_options(ext2_filsys fs)
 {
        quota_ctx_t qctx;
        ext2_ino_t qf_ino;
@@ -1163,6 +1249,10 @@ void handle_quota_options(ext2_filsys fs)
        quota_release_context(&qctx);
 
        if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
+               fprintf(stderr, _("\nWarning: the quota feature is still "
+                                 "under development\n"
+                                 "See https://ext4.wiki.kernel.org/"
+                                 "index.php/Quota for more information\n\n"));
                fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
                ext2fs_mark_super_dirty(fs);
        } else if (!fs->super->s_usr_quota_inum &&
@@ -1174,7 +1264,7 @@ void handle_quota_options(ext2_filsys fs)
        return;
 }
 
-void parse_quota_opts(const char *opts)
+static void parse_quota_opts(const char *opts)
 {
        char    *buf, *token, *next, *p;
        int     len;
@@ -1519,7 +1609,7 @@ static void parse_tune2fs_options(int argc, char **argv)
                *io_options++ = 0;
        device_name = blkid_get_devname(NULL, argv[optind], NULL);
        if (!device_name) {
-               com_err("tune2fs", 0, _("Unable to resolve '%s'"),
+               com_err(program_name, 0, _("Unable to resolve '%s'"),
                        argv[optind]);
                exit(1);
        }
@@ -1576,12 +1666,12 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
                    strcmp(token, "clear_mmp") == 0) {
                        clear_mmp = 1;
                } else if (strcmp(token, "mmp_update_interval") == 0) {
-                       unsigned long interval;
+                       unsigned long intv;
                        if (!arg) {
                                r_usage++;
                                continue;
                        }
-                       interval = strtoul(arg, &p, 0);
+                       intv = strtoul(arg, &p, 0);
                        if (*p) {
                                fprintf(stderr,
                                        _("Invalid mmp_update_interval: %s\n"),
@@ -1589,21 +1679,21 @@ static int parse_extended_opts(ext2_filsys fs, const char *opts)
                                r_usage++;
                                continue;
                        }
-                       if (interval == 0) {
-                               interval = EXT4_MMP_UPDATE_INTERVAL;
-                       } else if (interval > EXT4_MMP_MAX_UPDATE_INTERVAL) {
+                       if (intv == 0) {
+                               intv = EXT4_MMP_UPDATE_INTERVAL;
+                       } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
                                fprintf(stderr,
                                        _("mmp_update_interval too big: %lu\n"),
-                                       interval);
+                                       intv);
                                r_usage++;
                                continue;
                        }
                        printf(P_("Setting multiple mount protection update "
                                  "interval to %lu second\n",
                                  "Setting multiple mount protection update "
-                                 "interval to %lu seconds\n", interval),
-                              interval);
-                       fs->super->s_mmp_update_interval = interval;
+                                 "interval to %lu seconds\n", intv),
+                              intv);
+                       fs->super->s_mmp_update_interval = intv;
                        ext2fs_mark_super_dirty(fs);
                } else if (!strcmp(token, "test_fs")) {
                        fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
@@ -1749,10 +1839,10 @@ static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
        return 0;
 }
 
-static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
+static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
 {
        dgrp_t group;
-       group = ext2fs_group_of_blk(fs, blk);
+       group = ext2fs_group_of_blk2(fs, blk);
        if (ext2fs_block_bitmap_loc(fs, group) == blk)
                return 1;
        if (ext2fs_inode_bitmap_loc(fs, group) == blk)
@@ -1760,9 +1850,9 @@ static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
        return 0;
 }
 
-static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
+static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
 {
-       blk_t start_blk, end_blk;
+       blk64_t start_blk, end_blk;
        start_blk = fs->super->s_first_data_block +
                        EXT2_BLOCKS_PER_GROUP(fs->super) * group;
        /*
@@ -1802,7 +1892,7 @@ static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
                         * the respective fs metadata pointers. Otherwise
                         * fail
                         */
-                       group = ext2fs_group_of_blk(fs, blk);
+                       group = ext2fs_group_of_blk2(fs, blk);
                        goal = ext2fs_group_first_block2(fs, group);
                        meta_data = 1;
 
@@ -1963,7 +2053,7 @@ err_out:
 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
 {
        dgrp_t i;
-       blk_t blk, new_blk;
+       blk64_t blk, new_blk;
 
        for (i = 0; i < fs->group_desc_count; i++) {
                blk = ext2fs_block_bitmap_loc(fs, i);
@@ -2367,6 +2457,12 @@ retry_open:
                        rc = 1;
                        goto closefs;
                }
+               if (new_inode_size > fs->blocksize) {
+                       fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
+                               new_inode_size, fs->blocksize);
+                       rc = 1;
+                       goto closefs;
+               }
 
                /*
                 * If inode resize is requested use the