From patchwork Fri Jul 14 15:13:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sargun Dhillon X-Patchwork-Id: 9841301 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 54797602BD for ; Fri, 14 Jul 2017 15:13:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 40AE828542 for ; Fri, 14 Jul 2017 15:13:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 326F428750; Fri, 14 Jul 2017 15:13:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D3F928542 for ; Fri, 14 Jul 2017 15:13:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754566AbdGNPNH (ORCPT ); Fri, 14 Jul 2017 11:13:07 -0400 Received: from mail-io0-f178.google.com ([209.85.223.178]:34917 "EHLO mail-io0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754373AbdGNPNG (ORCPT ); Fri, 14 Jul 2017 11:13:06 -0400 Received: by mail-io0-f178.google.com with SMTP id h134so15998242iof.2 for ; Fri, 14 Jul 2017 08:13:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Gzm6upoLzMacG4C/b7DY68c+PsykFN460R6TajdnB/8=; b=whoz5J2CcdWBLuUNZqrFbP6gKgB9Qw8t1dxKSDJb045MI9oIGOmfp/2sqmUuEm1TmS DMyBhdrMLXGY5JZAJcOGKRnN2r6p8Hd3u18GVi/yGFyeJJE2B89OZkSUurtjHl6YbZ86 /jobWnRNq+CuxN5br5g11lWZCKw96vF1lh5mQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Gzm6upoLzMacG4C/b7DY68c+PsykFN460R6TajdnB/8=; b=YAye1hV+kcqMACP9AXfQYTZ30+lhuCVB8gusKyrmRXN993QBR6TZW+hLaIlpw67Jeu ALTgMzImgWrO1AANumuFutOG3bsjzCfsPIY1SKxnAv4A9dAomLeYagDLPKvZvWKOL154 MffH7J7rVOuLgW9zeHmjYGlDkj/vBMDjv4tSm+ii8wU7V2UPYKz+MYrWXOFZKVFje/Sc gu8ATgdmmpzibFBVutt6jFRHep+iLbLNKBtJLdQ1upwuJmDOXoIczkVpkyDDfAUBwVav YyPCq00Y3UVccrbkjbStKW6BB4GTaLgi0pkKr1BJ5PsvPyOfYh+ffi626r3f2SZblJ73 XXrQ== X-Gm-Message-State: AIVw111i0bmmNXFPTTpt6u0+5PpHTuR3vSc8LEaZ7PSt9gqSOok00oBY 4GdEnt+Zz6lcMVUuiIOqJg== X-Received: by 10.107.182.132 with SMTP id g126mr9749414iof.216.1500045185186; Fri, 14 Jul 2017 08:13:05 -0700 (PDT) Received: from ircssh-2.c.rugged-nimbus-611.internal (80.60.198.104.bc.googleusercontent.com. [104.198.60.80]) by smtp.gmail.com with ESMTPSA id 190sm1498325itk.30.2017.07.14.08.13.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Jul 2017 08:13:04 -0700 (PDT) Date: Fri, 14 Jul 2017 15:13:03 +0000 From: Sargun Dhillon To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, quwenruo@cn.fujitsu.com Subject: [PATCH v3 2/2] btrfs: Add new ioctl uapis for qgroup creation / removal Message-ID: <7d071875077a30dd1694bf6fab43bbc9c12c2487.1499941625.git.sargun@sargun.me> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch introduces two new ioctls to create, and remove qgroups. These offer a somewhat more intuitive set of operations with the opportunity to add flags that gate to unintentional manipulation of qgroups. The create qgroup ioctl has a a new semantic which level-0 qgroups cannot be created unless a matching subvolume ID is created. The delete qgroup ioctl has the new semantic that it will not let you delete a currently utilized (referenced by a subvolume) level-0 qgroup unless you pass the BTRFS_QGROUP_NO_SUBVOL_CHECK flag. Signed-off-by: Sargun Dhillon --- fs/btrfs/ioctl.c | 100 ++++++++++++++++++++++++++++++++++++++++++++- fs/btrfs/qgroup.c | 76 +++++++++++++++++++++++++++++++--- fs/btrfs/qgroup.h | 6 ++- include/uapi/linux/btrfs.h | 16 ++++++++ 4 files changed, 189 insertions(+), 9 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index fa1b78c..2eca8e5 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -4924,6 +4924,98 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) return ret; } +static long btrfs_ioctl_qgroup_create_v2(struct file *file, void __user *uargs) +{ + struct btrfs_ioctl_qgroup_args_v2 args; + struct btrfs_trans_handle *trans; + struct btrfs_fs_info *fs_info; + struct btrfs_root *root; + struct inode *inode; + int ret; + int err; + + inode = file_inode(file); + fs_info = btrfs_sb(inode->i_sb); + root = BTRFS_I(inode)->root; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + ret = copy_from_user(&args, uargs, sizeof(args)); + if (ret) + return ret; + + if (!args.qgroupid) + return -EINVAL; + + ret = mnt_want_write_file(file); + if (ret) + return ret; + + trans = btrfs_join_transaction(root); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + goto out; + } + + ret = btrfs_create_qgroup(trans, fs_info, args.qgroupid, 1); + err = btrfs_end_transaction(trans); + + if (err && !ret) + ret = err; + +out: + mnt_drop_write_file(file); + return ret; +} + +static long btrfs_ioctl_qgroup_remove_v2(struct file *file, void __user *uargs) +{ + struct btrfs_ioctl_qgroup_args_v2 args; + struct btrfs_trans_handle *trans; + struct btrfs_fs_info *fs_info; + struct btrfs_root *root; + struct inode *inode; + int check; + int ret; + int err; + + inode = file_inode(file); + fs_info = btrfs_sb(inode->i_sb); + root = BTRFS_I(inode)->root; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + ret = copy_from_user(&args, uargs, sizeof(args)); + if (ret) + return ret; + + if (!args.qgroupid) + return -EINVAL; + check = !(args.flags & BTRFS_QGROUP_NO_SUBVOL_CHECK); + + ret = mnt_want_write_file(file); + if (ret) + return ret; + + trans = btrfs_join_transaction(root); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + goto out; + } + + ret = btrfs_remove_qgroup(trans, fs_info, args.qgroupid, check); + err = btrfs_end_transaction(trans); + + if (err && !ret) + ret = err; + +out: + mnt_drop_write_file(file); + return ret; +} + static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) { struct inode *inode = file_inode(file); @@ -4959,9 +5051,9 @@ static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) } if (sa->create) { - ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid); + ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid, 0); } else { - ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid); + ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid, 0); } err = btrfs_end_transaction(trans); @@ -5632,6 +5724,10 @@ long btrfs_ioctl(struct file *file, unsigned int return btrfs_ioctl_get_features(file, argp); case BTRFS_IOC_SET_FEATURES: return btrfs_ioctl_set_features(file, argp); + case BTRFS_IOC_QGROUP_CREATE_V2: + return btrfs_ioctl_qgroup_create_v2(file, argp); + case BTRFS_IOC_QGROUP_REMOVE_V2: + return btrfs_ioctl_qgroup_remove_v2(file, argp); } return -ENOTTY; diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c index 3abc517..05bce7b 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -1247,8 +1247,51 @@ int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, return ret; } +/* + * Meant to only operate on level-0 qroupid. + * + * It returns 1 if a matching subvolume is found; 0 if none is found. + * < 0 if there is an error. + */ +static int btrfs_subvolume_exists(struct btrfs_fs_info *fs_info, u64 qgroupid) +{ + struct btrfs_path *path; + struct btrfs_key key; + int err, ret = 0; + + if (qgroupid == BTRFS_FS_TREE_OBJECTID) + return 1; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + key.objectid = qgroupid; + key.type = BTRFS_ROOT_BACKREF_KEY; + key.offset = 0; + + err = btrfs_search_slot_for_read(fs_info->tree_root, &key, path, 1, 0); + if (err == 1) + goto out; + + if (err) { + ret = err; + goto out; + } + + btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); + if (key.objectid != qgroupid || key.type != BTRFS_ROOT_BACKREF_KEY) + goto out; + + ret = 1; +out: + btrfs_free_path(path); + return ret; +} + int btrfs_create_qgroup(struct btrfs_trans_handle *trans, - struct btrfs_fs_info *fs_info, u64 qgroupid) + struct btrfs_fs_info *fs_info, u64 qgroupid, + int check_subvol_exists) { struct btrfs_root *quota_root; struct btrfs_qgroup *qgroup; @@ -1260,12 +1303,23 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, ret = -EINVAL; goto out; } + qgroup = find_qgroup_rb(fs_info, qgroupid); if (qgroup) { ret = -EEXIST; goto out; } + if (check_subvol_exists && btrfs_qgroup_level(qgroupid) == 0) { + ret = btrfs_subvolume_exists(fs_info, qgroupid); + if (ret < 0) + goto out; + if (!ret) { + ret = -ENOENT; + goto out; + } + } + ret = add_qgroup_item(trans, quota_root, qgroupid); if (ret) goto out; @@ -1282,7 +1336,8 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, } int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, - struct btrfs_fs_info *fs_info, u64 qgroupid) + struct btrfs_fs_info *fs_info, u64 qgroupid, + int check_subvol_exists) { struct btrfs_root *quota_root; struct btrfs_qgroup *qgroup; @@ -1300,13 +1355,24 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, if (!qgroup) { ret = -ENOENT; goto out; - } else { - /* check if there are no children of this qgroup */ - if (!list_empty(&qgroup->members)) { + } + + if (check_subvol_exists && btrfs_qgroup_level(qgroupid) == 0) { + ret = btrfs_subvolume_exists(fs_info, qgroupid); + if (ret < 0) + goto out; + if (ret) { ret = -EBUSY; goto out; } } + + /* check if there are no children of this qgroup */ + if (!list_empty(&qgroup->members)) { + ret = -EBUSY; + goto out; + } + ret = del_qgroup_item(trans, quota_root, qgroupid); if (ret && ret != -ENOENT) diff --git a/fs/btrfs/qgroup.h b/fs/btrfs/qgroup.h index d9984e8..3f63d4d 100644 --- a/fs/btrfs/qgroup.h +++ b/fs/btrfs/qgroup.h @@ -125,9 +125,11 @@ int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, u64 src, u64 dst); int btrfs_create_qgroup(struct btrfs_trans_handle *trans, - struct btrfs_fs_info *fs_info, u64 qgroupid); + struct btrfs_fs_info *fs_info, u64 qgroupid, + int check_subvol_exists); int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, - struct btrfs_fs_info *fs_info, u64 qgroupid); + struct btrfs_fs_info *fs_info, u64 qgroupid, + int check_subvol_exists); int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, u64 qgroupid, struct btrfs_qgroup_limit *limit); diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h index 9aa74f3..aea8504 100644 --- a/include/uapi/linux/btrfs.h +++ b/include/uapi/linux/btrfs.h @@ -676,6 +676,18 @@ struct btrfs_ioctl_qgroup_create_args { __u64 create; __u64 qgroupid; }; + +/* + * Allows for the deletion of qgroups even if they are associated with active + * volumes. + */ +#define BTRFS_QGROUP_NO_SUBVOL_CHECK (1ULL << 0) +struct btrfs_ioctl_qgroup_args_v2 { + __u64 qgroupid; + __u64 flags; + __u64 reserved[16]; +}; + struct btrfs_ioctl_timespec { __u64 sec; __u32 nsec; @@ -841,5 +853,9 @@ enum btrfs_err_code { struct btrfs_ioctl_feature_flags[3]) #define BTRFS_IOC_RM_DEV_V2 _IOW(BTRFS_IOCTL_MAGIC, 58, \ struct btrfs_ioctl_vol_args_v2) +#define BTRFS_IOC_QGROUP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 59, \ + struct btrfs_ioctl_qgroup_args_v2) +#define BTRFS_IOC_QGROUP_REMOVE_V2 _IOW(BTRFS_IOCTL_MAGIC, 60, \ + struct btrfs_ioctl_qgroup_args_v2) #endif /* _UAPI_LINUX_BTRFS_H */