mkfs.f2fs: add root_owner to give uid/gid
authorJaegeuk Kim <jaegeuk@kernel.org>
Fri, 27 Jul 2018 08:12:32 +0000 (17:12 +0900)
committerJaegeuk Kim <jaegeuk@kernel.org>
Tue, 28 Aug 2018 06:49:26 +0000 (23:49 -0700)
This patch adds an option to mkfs.f2fs in order for user to assign uid/gid
to the target partition.
This requires when vold in android formats a sdcard partition.

Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
include/f2fs_fs.h
lib/libf2fs.c
man/mkfs.f2fs.8
mkfs/f2fs_format.c
mkfs/f2fs_format_main.c

index 36f3c62..ef13bf0 100644 (file)
@@ -384,6 +384,8 @@ struct f2fs_configuration {
        u_int32_t lpf_inum;
        u_int32_t lpf_dnum;
        u_int32_t lpf_ino;
+       u_int32_t root_uid;
+       u_int32_t root_gid;
 
        /* defragmentation parameters */
        int defrag_shrink;
@@ -1379,4 +1381,24 @@ static inline int parse_feature(struct feature *table, const char *features)
        free(buf);
        return 0;
 }
+
+static inline int parse_root_owner(char *ids,
+                       u_int32_t *root_uid, u_int32_t *root_gid)
+{
+       char *uid = ids;
+       char *gid = NULL;
+       int i;
+
+       /* uid:gid */
+       for (i = 0; i < strlen(ids) - 1; i++)
+               if (*(ids + i) == ':')
+                       gid = ids + i + 1;
+       if (!gid)
+               return -1;
+
+       *root_uid = atoi(uid);
+       *root_gid = atoi(gid);
+       return 0;
+}
+
 #endif /*__F2FS_FS_H */
index 5625cff..a1f8beb 100644 (file)
@@ -615,6 +615,10 @@ void f2fs_init_configuration(void)
        c.trim = 1;
        c.kd = -1;
        c.fixed_time = -1;
+
+       /* default root owner */
+       c.root_uid = getuid();
+       c.root_gid = getgid();
 }
 
 #ifdef HAVE_SETMNTENT
index 29dd68f..f9484eb 100644 (file)
@@ -45,6 +45,10 @@ mkfs.f2fs \- create an F2FS file system
 .B \-q
 ]
 [
+.B \-R
+.I root_owner
+]
+[
 .B \-s
 .I #-of-segments-per-section
 ]
@@ -120,6 +124,10 @@ e.g "encrypt" and so on.
 Quiet mode.
 With it, mkfs.f2fs does not show any messages include the basic messages.
 .TP
+.BI \-R
+Give root_owner option for initial uid/gid assignment.
+Default is set by getuid()/getgid(), and assigned by "-R $uid:$gid".
+.TP
 .BI \-s " #-of-segments-per-section"
 Specify the number of segments per section. A section consists of
 multiple consecutive segments, and is the unit of garbage collection.
index d039434..f2880eb 100644 (file)
@@ -1078,8 +1078,8 @@ static int f2fs_write_root_inode(void)
                raw_node->i.i_links = cpu_to_le32(3);
        else
                raw_node->i.i_links = cpu_to_le32(2);
-       raw_node->i.i_uid = cpu_to_le32(getuid());
-       raw_node->i.i_gid = cpu_to_le32(getgid());
+       raw_node->i.i_uid = cpu_to_le32(c.root_uid);
+       raw_node->i.i_gid = cpu_to_le32(c.root_gid);
 
        blk_size_bytes = 1 << get_sb(log_blocksize);
        raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes); /* dentry */
@@ -1236,8 +1236,8 @@ static int f2fs_write_qf_inode(int qtype)
 
        raw_node->i.i_mode = cpu_to_le16(0x8180);
        raw_node->i.i_links = cpu_to_le32(1);
-       raw_node->i.i_uid = cpu_to_le32(getuid());
-       raw_node->i.i_gid = cpu_to_le32(getgid());
+       raw_node->i.i_uid = cpu_to_le32(c.root_uid);
+       raw_node->i.i_gid = cpu_to_le32(c.root_gid);
 
        raw_node->i.i_size = cpu_to_le64(1024 * 6); /* Hard coded */
        raw_node->i.i_blocks = cpu_to_le64(1 + QUOTA_DATA(qtype));
@@ -1435,8 +1435,8 @@ static int f2fs_write_lpf_inode(void)
 
        raw_node->i.i_mode = cpu_to_le16(0x41c0); /* 0700 */
        raw_node->i.i_links = cpu_to_le32(2);
-       raw_node->i.i_uid = cpu_to_le32(getuid());
-       raw_node->i.i_gid = cpu_to_le32(getgid());
+       raw_node->i.i_uid = cpu_to_le32(c.root_uid);
+       raw_node->i.i_gid = cpu_to_le32(c.root_gid);
 
        blk_size_bytes = 1 << get_sb(log_blocksize);
        raw_node->i.i_size = cpu_to_le64(1 * blk_size_bytes);
index 8af70a2..6bcfbb0 100644 (file)
@@ -56,6 +56,7 @@ static void mkfs_usage()
        MSG(0, "  -o overprovision ratio [default:5]\n");
        MSG(0, "  -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
        MSG(0, "  -q quiet mode\n");
+       MSG(0, "  -R root_owner [default: 0:0]\n");
        MSG(0, "  -s # of segments per section [default:1]\n");
        MSG(0, "  -S sparse mode\n");
        MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
@@ -104,7 +105,7 @@ static void add_default_options(void)
 
 static void f2fs_parse_options(int argc, char *argv[])
 {
-       static const char *option_string = "qa:c:d:e:E:g:il:mo:O:s:S:z:t:fw:V";
+       static const char *option_string = "qa:c:d:e:E:g:il:mo:O:R:s:S:z:t:fw:V";
        int32_t option=0;
 
        while ((option = getopt(argc,argv,option_string)) != EOF) {
@@ -162,6 +163,10 @@ static void f2fs_parse_options(int argc, char *argv[])
                        if (parse_feature(feature_table, optarg))
                                mkfs_usage();
                        break;
+               case 'R':
+                       if (parse_root_owner(optarg, &c.root_uid, &c.root_gid))
+                               mkfs_usage();
+                       break;
                case 's':
                        c.segs_per_sec = atoi(optarg);
                        break;