fsck.f2fs: add -O features to tune the bits
authorJaegeuk Kim <jaegeuk@kernel.org>
Thu, 19 Apr 2018 20:53:31 +0000 (13:53 -0700)
committerJaegeuk Kim <jaegeuk@kernel.org>
Sat, 14 Jul 2018 05:46:00 +0000 (22:46 -0700)
This patch add -O features for fsck.f2fs in order to tune the feature bits.
Currently, it supports -O encrypt only.

Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fsck/fsck.h
fsck/main.c
fsck/mount.c
fsck/resize.c

index 8e133fa..3e13fc6 100644 (file)
@@ -176,6 +176,7 @@ extern void move_curseg_info(struct f2fs_sb_info *, u64);
 extern void write_curseg_info(struct f2fs_sb_info *);
 extern int find_next_free_block(struct f2fs_sb_info *, u64 *, int, int);
 extern void write_checkpoint(struct f2fs_sb_info *);
+extern void write_superblock(struct f2fs_super_block *);
 extern void update_data_blkaddr(struct f2fs_sb_info *, nid_t, u16, block_t);
 extern void update_nat_blkaddr(struct f2fs_sb_info *, nid_t, nid_t, block_t);
 
index 9256d21..c4dd8b1 100644 (file)
@@ -28,6 +28,8 @@ struct f2fs_fsck gfsck;
 extern struct sparse_file *f2fs_sparse_file;
 #endif
 
+INIT_FEATURE_TABLE;
+
 static char *absolute_path(const char *file)
 {
        char *ret;
@@ -54,6 +56,7 @@ void fsck_usage()
        MSG(0, "  -d debug level [default:0]\n");
        MSG(0, "  -f check/fix entire partition\n");
        MSG(0, "  -g add default options\n");
+       MSG(0, "  -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
        MSG(0, "  -p preen mode [default:0 the same as -a [0|1]]\n");
        MSG(0, "  -S sparse_mode\n");
        MSG(0, "  -t show directory tree\n");
@@ -180,7 +183,7 @@ void f2fs_parse_options(int argc, char *argv[])
        }
 
        if (!strcmp("fsck.f2fs", prog)) {
-               const char *option_string = ":ad:fg:p:q:StyV";
+               const char *option_string = ":ad:fg:O:p:q:StyV";
                int opt = 0;
                struct option long_opt[] = {
                        {"dry-run", no_argument, 0, 1},
@@ -203,6 +206,10 @@ void f2fs_parse_options(int argc, char *argv[])
                                if (!strcmp(optarg, "android"))
                                        c.defset = CONF_ANDROID;
                                break;
+                       case 'O':
+                               if (parse_feature(feature_table, optarg))
+                                       fsck_usage();
+                               break;
                        case 'p':
                                /* preen mode has different levels:
                                 *  0: default level, the same as -a
index 4a14320..2ad78d1 100644 (file)
@@ -2228,6 +2228,22 @@ void write_checkpoint(struct f2fs_sb_info *sbi)
        ASSERT(ret >= 0);
 }
 
+void write_superblock(struct f2fs_super_block *new_sb)
+{
+       int index, ret;
+       u_int8_t *buf;
+
+       buf = calloc(BLOCK_SZ, 1);
+
+       memcpy(buf + F2FS_SUPER_OFFSET, new_sb, sizeof(*new_sb));
+       for (index = 0; index < 2; index++) {
+               ret = dev_write_block(buf, index);
+               ASSERT(ret >= 0);
+       }
+       free(buf);
+       DBG(0, "Info: Done to rebuild superblock\n");
+}
+
 void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
 {
        struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
@@ -2398,6 +2414,26 @@ static int check_sector_size(struct f2fs_super_block *sb)
        return 0;
 }
 
+static void tune_sb_features(struct f2fs_sb_info *sbi)
+{
+       int sb_changed = 0;
+       struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
+
+       if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) &&
+                       c.feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) {
+               sb->feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
+               MSG(0, "Info: Set Encryption feature\n");
+               sb_changed = 1;
+       }
+       /* TODO: quota needs to allocate inode numbers */
+
+       c.feature = sb->feature;
+       if (!sb_changed)
+               return;
+
+       write_superblock(sb);
+}
+
 int f2fs_do_mount(struct f2fs_sb_info *sbi)
 {
        struct f2fs_checkpoint *cp = NULL;
@@ -2449,7 +2485,8 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi)
        }
 
        c.bug_on = 0;
-       c.feature = sb->feature;
+
+       tune_sb_features(sbi);
 
        /* precompute checksum seed for metadata */
        if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
index 9d79e6b..99a0bd1 100644 (file)
@@ -572,22 +572,6 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi,
        DBG(0, "Info: Done to rebuild checkpoint blocks\n");
 }
 
-static void rebuild_superblock(struct f2fs_super_block *new_sb)
-{
-       int index, ret;
-       u_int8_t *buf;
-
-       buf = calloc(BLOCK_SZ, 1);
-
-       memcpy(buf + F2FS_SUPER_OFFSET, new_sb, sizeof(*new_sb));
-       for (index = 0; index < 2; index++) {
-               ret = dev_write_block(buf, index);
-               ASSERT(ret >= 0);
-       }
-       free(buf);
-       DBG(0, "Info: Done to rebuild superblock\n");
-}
-
 int f2fs_resize(struct f2fs_sb_info *sbi)
 {
        struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
@@ -639,6 +623,6 @@ int f2fs_resize(struct f2fs_sb_info *sbi)
        migrate_nat(sbi, new_sb);
        migrate_sit(sbi, new_sb, offset_seg);
        rebuild_checkpoint(sbi, new_sb, offset_seg);
-       rebuild_superblock(new_sb);
+       write_superblock(new_sb);
        return 0;
 }