This patch adds readmirror property to be applied at the filesystem object.
And uses ioctl BTRFS_IOC_GET_READMIRROR and BTRFS_IOC_SET_READMIRROR to get
and set the property respectively.

Signed-off-by: Anand Jain <anand.j...@oracle.com>
---
v1->RFC v2:
  . Changed format specifier from devid1,2,3.. to devid:1,2,3..

 ioctl.h | 14 +++++++++
 props.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 112 insertions(+)

diff --git a/ioctl.h b/ioctl.h
index 66ee599f7a82..ccdf600bae77 100644
--- a/ioctl.h
+++ b/ioctl.h
@@ -765,6 +765,16 @@ struct btrfs_ioctl_get_subvol_rootref_args {
 };
 BUILD_ASSERT(sizeof(struct btrfs_ioctl_get_subvol_rootref_args) == 4096);
 
+enum btrfs_readmirror_policy {
+       BTRFS_READMIRROR_DEFAULT = 0,
+       BTRFS_READMIRROR_DEVID,
+};
+
+struct btrfs_ioctl_readmirror_args {
+       __u64 type; /* RW */
+       __u64 device_bitmap; /* RW */
+};
+
 /* Error codes as returned by the kernel */
 enum btrfs_err_code {
        notused,
@@ -929,6 +939,10 @@ static inline char *btrfs_err_str(enum btrfs_err_code 
err_code)
                                struct btrfs_ioctl_get_subvol_rootref_args)
 #define BTRFS_IOC_INO_LOOKUP_USER _IOWR(BTRFS_IOCTL_MAGIC, 62, \
                                struct btrfs_ioctl_ino_lookup_user_args)
+#define BTRFS_IOC_GET_READMIRROR _IOWR(BTRFS_IOCTL_MAGIC, 63, \
+                               struct btrfs_ioctl_readmirror_args)
+#define BTRFS_IOC_SET_READMIRROR _IOWR(BTRFS_IOCTL_MAGIC, 64, \
+                               struct btrfs_ioctl_readmirror_args)
 #ifdef __cplusplus
 }
 #endif
diff --git a/props.c b/props.c
index 004022600d51..f077e55e2274 100644
--- a/props.c
+++ b/props.c
@@ -166,6 +166,98 @@ out:
        return ret;
 }
 
+static int prop_readmirror(enum prop_object_type type, const char *object,
+                          const char *name, const char *value)
+{
+       int fd;
+       int ret;
+       DIR *dirstream = NULL;
+
+       fd = open_file_or_dir3(object, &dirstream, value ? O_RDWR : O_RDONLY);
+       if (fd < 0) {
+               ret = -errno;
+               error("failed to open %s: %m", object);
+               return ret;
+       }
+
+       if (value) {
+               int ret;
+               int final_ret;
+               size_t len = strlen(value);
+               char *value_dup;
+               char *devid_str;
+               struct btrfs_ioctl_readmirror_args readmirror;
+
+               if (len > 0 && (len <= 6 || strncmp("devid:", value, 6)))
+                       return -EINVAL;
+
+               /* value format - an example: readmirror devid:1,2,3,.. */
+               value_dup = strndup(value + 6, len - 6);
+               if (!value_dup)
+                       return -ENOMEM;
+
+               final_ret = 0;
+               if (len == 0) {
+                       readmirror.type = BTRFS_READMIRROR_DEFAULT;
+                       final_ret = ioctl(fd, BTRFS_IOC_SET_READMIRROR, 
&readmirror);
+                       if (final_ret < 0)
+                               error("failed to reset readmirror: (%m)");
+               } else {
+                       __u64 device_bitmap = 0;
+
+                       while ((devid_str = strsep(&value_dup, ",")) != NULL)
+                               device_bitmap = device_bitmap |
+                                               (1ULL << 
arg_strtou64(devid_str));
+
+                       readmirror.type = BTRFS_READMIRROR_DEVID;
+                       readmirror.device_bitmap = device_bitmap;
+                       ret = ioctl(fd, BTRFS_IOC_SET_READMIRROR, &readmirror);
+                       if (ret < 0) {
+                               error("failed to set readmirror: (%m)");
+                               final_ret = ret;
+                       }
+               }
+               return final_ret;
+       } else {
+               struct btrfs_ioctl_readmirror_args readmirror;
+               struct btrfs_ioctl_dev_info_args *device;
+               struct btrfs_ioctl_fs_info_args fs_devices;
+
+               ret = get_fs_info(object, &fs_devices, &device);
+               if (ret)
+                       return ret;
+
+               free(device);
+               if (fs_devices.num_devices > 63) {
+                       error("can't set readmirror for device id more than 
63");
+                       return -EOPNOTSUPP;
+               }
+
+               ret = ioctl(fd, BTRFS_IOC_GET_READMIRROR, &readmirror);
+               if (ret) {
+                       error("readmirror ioctl failed: %m");
+                       return ret;
+               }
+               if (readmirror.type == BTRFS_READMIRROR_DEFAULT) {
+                       printf("readmirror default\n");
+               } else if (readmirror.type == BTRFS_READMIRROR_DEVID) {
+                       u64 cnt;
+
+                       printf("readmirror devid:");
+                       for (cnt = 0; cnt < 64; cnt++) {
+                               if ((1ULL << cnt) & readmirror.device_bitmap)
+                                       printf("%llu ", cnt);
+                       }
+                       printf("\n");
+               }
+               return 0;
+       }
+
+       close_file_or_dir(fd, dirstream);
+
+       return ret;
+}
+
 const struct prop_handler prop_handlers[] = {
        {
                .name ="ro",
@@ -187,5 +279,11 @@ const struct prop_handler prop_handlers[] = {
                .read_only = 0,
                .types = prop_object_inode, prop_compression
        },
+       {
+               .name = "readmirror",
+               .desc = "specify the device to be used for read",
+               .read_only = 0,
+               .types = prop_object_root, prop_readmirror
+       },
        {NULL, NULL, 0, 0, NULL}
 };
-- 
2.21.0 (Apple Git-120)

Reply via email to