btrfs-progs: mkfs: move image creation of rootdir to its own files
[platform/upstream/btrfs-progs.git] / cmds-fi-usage.c
index a72fb4e..0b0e47f 100644 (file)
@@ -508,7 +508,7 @@ static int cmp_device_info(const void *a, const void *b)
                        ((struct device_info *)b)->path);
 }
 
-int dev_to_fsid(const char *dev, __u8 *fsid)
+int dev_to_fsid(const char *dev, u8 *fsid)
 {
        struct btrfs_super_block *disk_super;
        char buf[BTRFS_SUPER_INFO_SIZE];
@@ -545,6 +545,7 @@ static int load_device_info(int fd, struct device_info **device_info_ptr,
        struct btrfs_ioctl_fs_info_args fi_args;
        struct btrfs_ioctl_dev_info_args dev_info;
        struct device_info *info;
+       u8 fsid[BTRFS_UUID_SIZE];
 
        *device_info_count = 0;
        *device_info_ptr = NULL;
@@ -568,6 +569,8 @@ static int load_device_info(int fd, struct device_info **device_info_ptr,
                if (ndevs >= fi_args.num_devices) {
                        error("unexpected number of devices: %d >= %llu", ndevs,
                                (unsigned long long)fi_args.num_devices);
+                       error(
+               "if seed device is used, try running this command as root");
                        goto out;
                }
                memset(&dev_info, 0, sizeof(dev_info));
@@ -580,6 +583,17 @@ static int load_device_info(int fd, struct device_info **device_info_ptr,
                        goto out;
                }
 
+               /*
+                * Skip seed device by checking device's fsid (requires root).
+                * And we will skip only if dev_to_fsid is successful and dev
+                * is a seed device.
+                * Ignore any other error including -EACCES, which is seen when
+                * a non-root process calls dev_to_fsid(path)->open(path).
+                */
+               ret = dev_to_fsid((const char *)dev_info.path, fsid);
+               if (!ret && memcmp(fi_args.fsid, fsid, BTRFS_FSID_SIZE) != 0)
+                       continue;
+
                info[ndevs].devid = dev_info.devid;
                if (!dev_info.path[0]) {
                        strcpy(info[ndevs].path, "missing");