@@ -92,6 +92,9 @@ struct btrfs_free_space_ctl;
/* device stats in the device tree */
#define BTRFS_DEV_STATS_OBJECTID 0ULL
+/* store readmirror policy inforamtion in the device tree */
+#define BTRFS_READMIRROR_OBJECTID -3ULL
+
/* for storing balance parameters in the root tree */
#define BTRFS_BALANCE_OBJECTID -4ULL
@@ -879,6 +882,14 @@ struct btrfs_balance_item {
__le64 unused[4];
} __attribute__ ((__packed__));
+/*
+ * readmirror's persistent storage format
+ */
+struct btrfs_readmirror_item {
+ __le64 type;
+ __le64 unused[3];
+} __attribute__ ((__packed__));
+
#define BTRFS_FILE_EXTENT_INLINE 0
#define BTRFS_FILE_EXTENT_REG 1
#define BTRFS_FILE_EXTENT_PREALLOC 2
@@ -2389,6 +2400,9 @@ BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_exclusive,
/* btrfs_balance_item */
BTRFS_SETGET_FUNCS(balance_item_flags, struct btrfs_balance_item, flags, 64);
+/* btrfs_readmirror_item */
+BTRFS_SETGET_FUNCS(readmirror_type, struct btrfs_readmirror_item, type, 64);
+
static inline struct btrfs_disk_balance_args* btrfs_balance_item_data(
struct extent_buffer *eb, struct btrfs_balance_item *bi)
{
@@ -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
@@ -51,6 +51,9 @@
/* device stats in the device tree */
#define BTRFS_DEV_STATS_OBJECTID 0ULL
+/* store readmirror policy inforamtion in the device tree */
+#define BTRFS_READMIRROR_OBJECTID -3ULL
+
/* for storing balance parameters in the root tree */
#define BTRFS_BALANCE_OBJECTID -4ULL
@@ -962,4 +965,12 @@ struct btrfs_qgroup_limit_item {
__le64 rsv_excl;
} __attribute__ ((__packed__));
+/*
+ * readmirror's persistent storage format
+ */
+struct btrfs_readmirror_item {
+ __le64 type;
+ __le64 unused[3];
+} __attribute__ ((__packed__));
+
#endif /* _BTRFS_CTREE_H_ */
@@ -692,6 +692,8 @@ void print_objectid(FILE *stream, u64 objectid, u8 type)
case BTRFS_PERSISTENT_ITEM_KEY:
if (objectid == BTRFS_DEV_STATS_OBJECTID)
fprintf(stream, "DEV_STATS");
+ else if (objectid == BTRFS_READMIRROR_OBJECTID)
+ fprintf(stream, "READMIRROR");
else
fprintf(stream, "%llu", (unsigned long long)objectid);
return;
@@ -960,6 +962,16 @@ static void print_balance_item(struct extent_buffer *eb,
print_disk_balance_args(btrfs_balance_item_sys(eb, bi));
}
+static void print_readmirror(struct extent_buffer *eb,
+ struct btrfs_readmirror_item *rm, u32 size)
+{
+ if (btrfs_readmirror_type(eb, rm) == 1)
+ printf("\t\treadmirror.type devid\n");
+ else
+ printf("\t\treadmirror.type 0x%llx\n",
+ btrfs_readmirror_type(eb, rm));
+}
+
static void print_dev_stats(struct extent_buffer *eb,
struct btrfs_dev_stats_item *stats, u32 size)
{
@@ -1109,6 +1121,9 @@ static void print_persistent_item(struct extent_buffer *eb, void *ptr,
print_objectid(stdout, objectid, BTRFS_PERSISTENT_ITEM_KEY);
printf(" offset %llu\n", (unsigned long long)offset);
switch (objectid) {
+ case BTRFS_READMIRROR_OBJECTID:
+ print_readmirror(eb, ptr, item_size);
+ break;
case BTRFS_DEV_STATS_OBJECTID:
print_dev_stats(eb, ptr, item_size);
break;
@@ -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}
};
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.jain@oracle.com> --- RFC v2->RFC v2.1: add dump-tree support, depends on the patch [PATCH] btrfs-progs: print-tree add missing DEV_STATS v1->RFC v2: Changed format specifier from devid1,2,3.. to devid:1,2,3.. ctree.h | 14 ++++++ ioctl.h | 14 ++++++ libbtrfsutil/btrfs_tree.h | 11 +++++ print-tree.c | 15 ++++++ props.c | 98 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+)