btrfs: Add quota_override knob into sysfs
authorSargun Dhillon <sargun@sargun.me>
Thu, 11 May 2017 21:18:03 +0000 (21:18 +0000)
committerDavid Sterba <dsterba@suse.com>
Mon, 19 Jun 2017 16:25:58 +0000 (18:25 +0200)
This patch adds the read-write attribute quota_override into sysfs.
Any process which has CAP_SYS_RESOURCE can set this flag to on, and
once it is set to true, processes with CAP_SYS_RESOURCE can exceed
the quota.

Signed-off-by: Sargun Dhillon <sargun@sargun.me>
Reviewed-by: David Sterba <dsterba@suse.com>
[ minor changelog edits ]
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/sysfs.c

index 1f157fba89404a6994b603345d959f6d0ea02827..c2d5f3580b4cff9fe00352f2daabe07f14e6dfd8 100644 (file)
@@ -447,11 +447,52 @@ static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
 
 BTRFS_ATTR(clone_alignment, btrfs_clone_alignment_show);
 
+static ssize_t quota_override_show(struct kobject *kobj,
+                                  struct kobj_attribute *a, char *buf)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+       int quota_override;
+
+       quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
+       return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
+}
+
+static ssize_t quota_override_store(struct kobject *kobj,
+                                   struct kobj_attribute *a,
+                                   const char *buf, size_t len)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+       unsigned long knob;
+       int err;
+
+       if (!fs_info)
+               return -EPERM;
+
+       if (!capable(CAP_SYS_RESOURCE))
+               return -EPERM;
+
+       err = kstrtoul(buf, 10, &knob);
+       if (err)
+               return err;
+       if (knob > 1)
+               return -EINVAL;
+
+       if (knob)
+               set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
+       else
+               clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
+
+       return len;
+}
+
+BTRFS_ATTR_RW(quota_override, quota_override_show, quota_override_store);
+
 static const struct attribute *btrfs_attrs[] = {
        BTRFS_ATTR_PTR(label),
        BTRFS_ATTR_PTR(nodesize),
        BTRFS_ATTR_PTR(sectorsize),
        BTRFS_ATTR_PTR(clone_alignment),
+       BTRFS_ATTR_PTR(quota_override),
        NULL,
 };