]> git.hungrycats.org Git - linux/commitdiff
Add an ioctl to set/retrive the device properties
authorGoffredo Baroncelli <kreijack@inwind.it>
Thu, 28 May 2020 18:34:48 +0000 (20:34 +0200)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 29 Dec 2020 02:58:32 +0000 (21:58 -0500)
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
(cherry picked from commit 6ea419396b107061c491c53385bd3931fdff7669)
(cherry picked from commit e47c1e792870d932a3b92d83e494a0a38f1175bc)
(cherry picked from commit e1ad99696bbb6345fe4dcb6ce9ee2543add73cde)

fs/btrfs/ioctl.c
fs/btrfs/volumes.c
fs/btrfs/volumes.h
include/uapi/linux/btrfs.h

index eb36ff0778cb502030f43665281bbcf4d98119f8..c54d95a72aaf86d26d780037931d30bc73f7eb34 100644 (file)
@@ -4796,6 +4796,71 @@ out_drop_write:
        return ret;
 }
 
+static long btrfs_ioctl_dev_properties(struct file *file,
+                                               void __user *argp)
+{
+       struct inode *inode = file_inode(file);
+       struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
+       struct btrfs_ioctl_dev_properties dev_props;
+       struct btrfs_device     *device;
+        struct btrfs_root *root = fs_info->chunk_root;
+        struct btrfs_trans_handle *trans;
+       int ret;
+       u64 prev_type;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       if (copy_from_user(&dev_props, argp, sizeof(dev_props)))
+               return -EFAULT;
+
+       device = btrfs_find_device(fs_info->fs_devices, dev_props.devid,
+                               NULL, NULL, false);
+       if (!device) {
+               btrfs_info(fs_info, "change_dev_properties: unable to find device %llu",
+                          dev_props.devid);
+               return -ENODEV;
+       }
+
+       if (dev_props.properties & BTRFS_DEV_PROPERTY_READ) {
+               u64 props = dev_props.properties;
+               memset(&dev_props, 0, sizeof(dev_props));
+               if (props & BTRFS_DEV_PROPERTY_TYPE) {
+                       dev_props.properties = BTRFS_DEV_PROPERTY_TYPE;
+                       dev_props.type = device->type;
+               }
+               if(copy_to_user(argp, &dev_props, sizeof(dev_props)))
+                       return -EFAULT;
+               return 0;
+       }
+
+       /* it is possible to set only BTRFS_DEV_PROPERTY_TYPE for now */
+       if (dev_props.properties & ~(BTRFS_DEV_PROPERTY_TYPE))
+               return -EPERM;
+
+       trans = btrfs_start_transaction(root, 0);
+        if (IS_ERR(trans))
+                return PTR_ERR(trans);
+
+       prev_type = device->type;
+       device->type = dev_props.type;
+       ret = btrfs_update_device(trans, device);
+
+        if (ret < 0) {
+                btrfs_abort_transaction(trans, ret);
+                btrfs_end_transaction(trans);
+               device->type = prev_type;
+               return  ret;
+        }
+
+        ret = btrfs_commit_transaction(trans);
+       if (ret < 0)
+               device->type = prev_type;
+
+       return ret;
+
+}
+
 static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
 {
        struct btrfs_ioctl_send_args *arg;
@@ -4979,6 +5044,8 @@ long btrfs_ioctl(struct file *file, unsigned int
                return btrfs_ioctl_get_subvol_rootref(file, argp);
        case BTRFS_IOC_INO_LOOKUP_USER:
                return btrfs_ioctl_ino_lookup_user(file, argp);
+       case BTRFS_IOC_DEV_PROPERTIES:
+               return btrfs_ioctl_dev_properties(file, argp);
        }
 
        return -ENOTTY;
index d0482a4fc6a444ca5e20da7d0ff9c5e8371161e6..8855db6a013d091c183ab5c8f597653568e0def8 100644 (file)
@@ -2732,7 +2732,7 @@ error:
        return ret;
 }
 
-static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
+int btrfs_update_device(struct btrfs_trans_handle *trans,
                                        struct btrfs_device *device)
 {
        int ret;
index 2a33a6af289b9738fc9372e1c1fab30769871f69..06ca13b19abdd6b5f09d58677da48be328ee8216 100644 (file)
@@ -581,5 +581,7 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
 int btrfs_bg_type_to_factor(u64 flags);
 const char *btrfs_bg_type_to_raid_name(u64 flags);
 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
+int btrfs_update_device(struct btrfs_trans_handle *trans,
+                                        struct btrfs_device *device);
 
 #endif
index 1b544f95f27bd47a8c9ce05f86b400c79efe8215..0840cfc12f704578056294bc3c99861902b57159 100644 (file)
@@ -862,6 +862,44 @@ struct btrfs_ioctl_get_subvol_rootref_args {
                __u8 align[7];
 };
 
+#define BTRFS_DEV_PROPERTY_TYPE                (1ULL << 0)
+#define BTRFS_DEV_PROPERTY_DEV_GROUP   (1ULL << 1)
+#define BTRFS_DEV_PROPERTY_SEEK_SPEED  (1ULL << 2)
+#define BTRFS_DEV_PROPERTY_BANDWIDTH   (1ULL << 3)
+#define BTRFS_DEV_PROPERTY_READ                (1ULL << 60)
+
+/*
+ * The ioctl BTRFS_IOC_DEV_PROPERTIES can read and write the device properties.
+ *
+ * The properties that the user want to write have to be set
+ * in the 'properties' field using the BTRFS_DEV_PROPERTY_xxxx constants.
+ *
+ * If the ioctl is used to read the device properties, the bit
+ * BTRFS_DEV_PROPERTY_READ has to be set in the 'properties' field.
+ * In this case the properties that the user want have to be set in the
+ * 'properties' field. The kernel doesn't return a property that was not
+ * required, however it may return a subset of the requested properties.
+ * The returned properties have the corrispondent BTRFS_DEV_PROPERTY_xxxx
+ * flag set in the 'properties' field.
+ *
+ * Up to 2020/05/11 the only properties that can be read/write is the 'type'
+ * one.
+ */
+struct btrfs_ioctl_dev_properties {
+       __u64   devid;
+       __u64   properties;
+       __u64   type;
+       __u32   dev_group;
+       __u8    seek_speed;
+       __u8    bandwidth;
+
+       /*
+        * for future expansion
+        */
+       __u8    unused1[2];
+       __u64   unused2[4];
+};
+
 /* Error codes as returned by the kernel */
 enum btrfs_err_code {
        BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET = 1,
@@ -990,5 +1028,7 @@ enum btrfs_err_code {
                                struct btrfs_ioctl_ino_lookup_user_args)
 #define BTRFS_IOC_SNAP_DESTROY_V2 _IOW(BTRFS_IOCTL_MAGIC, 63, \
                                struct btrfs_ioctl_vol_args_v2)
+#define BTRFS_IOC_DEV_PROPERTIES _IOW(BTRFS_IOCTL_MAGIC, 64, \
+                               struct btrfs_ioctl_dev_properties)
 
 #endif /* _UAPI_LINUX_BTRFS_H */