btrfs-progs: provide fail safe for BTRFS_IOC_GET_FSLABEL ioctl
authorAnand Jain <anand.jain@oracle.com>
Tue, 15 Sep 2015 08:46:22 +0000 (16:46 +0800)
committerDavid Sterba <dsterba@suse.com>
Fri, 2 Oct 2015 15:52:10 +0000 (17:52 +0200)
Old kernels before 3.9 do not provide ioctl BTRFS_IOC_GET_FSLABEL.
So we need to provide a fail safe logic for btrfs-progs running
on those kernel.

In this patch when get_label_mounted() fails on the old kernel
it will fail back to the old method and uses get_label_unmounted(),
where it will read from the disk directly.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
cmds-filesystem.c
utils.c
utils.h

index 8822695..09cb37d 100644 (file)
@@ -468,7 +468,13 @@ static int btrfs_scan_kernel(void *search, unsigned unit_mode)
                        goto out;
                }
 
-               if (get_label_mounted(mnt->mnt_dir, label)) {
+               ret = get_label_mounted(mnt->mnt_dir, label);
+               /* provide backward kernel compatibility */
+               if (ret == -ENOTTY)
+                       ret = get_label_unmounted(
+                               (const char *)dev_info_arg->path, label);
+
+               if (ret) {
                        kfree(dev_info_arg);
                        goto out;
                }
diff --git a/utils.c b/utils.c
index 679be3c..bb4091e 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -1740,7 +1740,7 @@ static int set_label_mounted(const char *mount_path, const char *label)
        return 0;
 }
 
-static int get_label_unmounted(const char *dev, char *label)
+int get_label_unmounted(const char *dev, char *label)
 {
        struct btrfs_root *root;
        int ret;
@@ -1750,11 +1750,6 @@ static int get_label_unmounted(const char *dev, char *label)
               fprintf(stderr, "FATAL: error checking %s mount status\n", dev);
               return -1;
        }
-       if (ret > 0) {
-               fprintf(stderr, "ERROR: dev %s is mounted, use mount point\n",
-                       dev);
-               return -1;
-       }
 
        /* Open the super_block at the default location
         * and as read-only.
@@ -1779,6 +1774,7 @@ int get_label_mounted(const char *mount_path, char *labelp)
 {
        char label[BTRFS_LABEL_SIZE];
        int fd;
+       int ret;
 
        fd = open(mount_path, O_RDONLY | O_NOATIME);
        if (fd < 0) {
@@ -1787,10 +1783,14 @@ int get_label_mounted(const char *mount_path, char *labelp)
        }
 
        memset(label, '\0', sizeof(label));
-       if (ioctl(fd, BTRFS_IOC_GET_FSLABEL, label) < 0) {
-               fprintf(stderr, "ERROR: unable get label %s\n", strerror(errno));
+       ret = ioctl(fd, BTRFS_IOC_GET_FSLABEL, label);
+       if (ret < 0) {
+               if (errno != ENOTTY)
+                       fprintf(stderr, "ERROR: unable to get label %s\n",
+                                       strerror(errno));
+               ret = -errno;
                close(fd);
-               return -1;
+               return ret;
        }
 
        strncpy(labelp, label, sizeof(label));
diff --git a/utils.h b/utils.h
index dce0a47..93e1de6 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -164,6 +164,7 @@ u64 btrfs_device_size(int fd, struct stat *st);
 #define strncpy_null(dest, src) __strncpy__null(dest, src, sizeof(dest))
 int test_dev_for_mkfs(char *file, int force_overwrite);
 int get_label_mounted(const char *mount_path, char *labelp);
+int get_label_unmounted(const char *dev, char *label);
 int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile,
        u64 dev_cnt, int mixed);
 int group_profile_max_safe_loss(u64 flags);