diff mbox

[v3,2/2] btrfs: Add new ioctl uapis for qgroup creation / removal

Message ID 7d071875077a30dd1694bf6fab43bbc9c12c2487.1499941625.git.sargun@sargun.me (mailing list archive)
State New, archived
Headers show

Commit Message

Sargun Dhillon July 14, 2017, 3:13 p.m. UTC
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 <sargun@sargun.me>
---
 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(-)

Comments

kernel test robot July 15, 2017, 12:38 p.m. UTC | #1
Hi Sargun,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.12 next-20170714]
[cannot apply to btrfs/next]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Sargun-Dhillon/New-qgroup-creation-removal-ioctls/20170715-195435
config: xtensa-allyesconfig (attached as .config)
compiler: xtensa-linux-gcc (GCC) 4.9.0
reproduce:
        wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=xtensa 

All errors (new ones prefixed by >>):

   fs/btrfs/tests/qgroup-tests.c: In function 'test_no_shared_qgroup':
>> fs/btrfs/tests/qgroup-tests.c:232:8: error: too few arguments to function 'btrfs_create_qgroup'
     ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
           ^
   In file included from fs/btrfs/tests/qgroup-tests.c:24:0:
   fs/btrfs/tests/../qgroup.h:127:5: note: declared here
    int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
        ^
   fs/btrfs/tests/qgroup-tests.c: In function 'test_multiple_refs':
   fs/btrfs/tests/qgroup-tests.c:334:8: error: too few arguments to function 'btrfs_create_qgroup'
     ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID);
           ^
   In file included from fs/btrfs/tests/qgroup-tests.c:24:0:
   fs/btrfs/tests/../qgroup.h:127:5: note: declared here
    int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
        ^

vim +/btrfs_create_qgroup +232 fs/btrfs/tests/qgroup-tests.c

faa2dbf0 Josef Bacik        2014-05-07  219  
b9ef22de Feifei Xu          2016-06-01  220  static int test_no_shared_qgroup(struct btrfs_root *root,
b9ef22de Feifei Xu          2016-06-01  221  		u32 sectorsize, u32 nodesize)
faa2dbf0 Josef Bacik        2014-05-07  222  {
faa2dbf0 Josef Bacik        2014-05-07  223  	struct btrfs_trans_handle trans;
faa2dbf0 Josef Bacik        2014-05-07  224  	struct btrfs_fs_info *fs_info = root->fs_info;
442244c9 Qu Wenruo          2015-04-16  225  	struct ulist *old_roots = NULL;
442244c9 Qu Wenruo          2015-04-16  226  	struct ulist *new_roots = NULL;
faa2dbf0 Josef Bacik        2014-05-07  227  	int ret;
faa2dbf0 Josef Bacik        2014-05-07  228  
7c55ee0c Omar Sandoval      2015-09-29  229  	btrfs_init_dummy_trans(&trans);
faa2dbf0 Josef Bacik        2014-05-07  230  
faa2dbf0 Josef Bacik        2014-05-07  231  	test_msg("Qgroup basic add\n");
ef9f2db3 Feifei Xu          2016-06-01 @232  	ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
faa2dbf0 Josef Bacik        2014-05-07  233  	if (ret) {
faa2dbf0 Josef Bacik        2014-05-07  234  		test_msg("Couldn't create a qgroup %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  235  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  236  	}
faa2dbf0 Josef Bacik        2014-05-07  237  
442244c9 Qu Wenruo          2015-04-16  238  	/*
01327610 Nicholas D Steeves 2016-05-19  239  	 * Since the test trans doesn't have the complicated delayed refs,
442244c9 Qu Wenruo          2015-04-16  240  	 * we can only call btrfs_qgroup_account_extent() directly to test
442244c9 Qu Wenruo          2015-04-16  241  	 * quota.
442244c9 Qu Wenruo          2015-04-16  242  	 */
b9ef22de Feifei Xu          2016-06-01  243  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
faa2dbf0 Josef Bacik        2014-05-07  244  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  245  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  246  		test_msg("Couldn't find old roots: %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  247  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  248  	}
faa2dbf0 Josef Bacik        2014-05-07  249  
ef9f2db3 Feifei Xu          2016-06-01  250  	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
ef9f2db3 Feifei Xu          2016-06-01  251  				BTRFS_FS_TREE_OBJECTID);
faa2dbf0 Josef Bacik        2014-05-07  252  	if (ret)
faa2dbf0 Josef Bacik        2014-05-07  253  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  254  
b9ef22de Feifei Xu          2016-06-01  255  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
442244c9 Qu Wenruo          2015-04-16  256  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  257  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  258  		ulist_free(new_roots);
442244c9 Qu Wenruo          2015-04-16  259  		test_msg("Couldn't find old roots: %d\n", ret);
442244c9 Qu Wenruo          2015-04-16  260  		return ret;
442244c9 Qu Wenruo          2015-04-16  261  	}
442244c9 Qu Wenruo          2015-04-16  262  
b9ef22de Feifei Xu          2016-06-01  263  	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
b9ef22de Feifei Xu          2016-06-01  264  					  nodesize, old_roots, new_roots);
faa2dbf0 Josef Bacik        2014-05-07  265  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  266  		test_msg("Couldn't account space for a qgroup %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  267  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  268  	}
faa2dbf0 Josef Bacik        2014-05-07  269  
ef9f2db3 Feifei Xu          2016-06-01  270  	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
ef9f2db3 Feifei Xu          2016-06-01  271  				nodesize, nodesize)) {
faa2dbf0 Josef Bacik        2014-05-07  272  		test_msg("Qgroup counts didn't match expected values\n");
faa2dbf0 Josef Bacik        2014-05-07  273  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  274  	}
442244c9 Qu Wenruo          2015-04-16  275  	old_roots = NULL;
442244c9 Qu Wenruo          2015-04-16  276  	new_roots = NULL;
442244c9 Qu Wenruo          2015-04-16  277  
b9ef22de Feifei Xu          2016-06-01  278  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
442244c9 Qu Wenruo          2015-04-16  279  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  280  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  281  		test_msg("Couldn't find old roots: %d\n", ret);
442244c9 Qu Wenruo          2015-04-16  282  		return ret;
442244c9 Qu Wenruo          2015-04-16  283  	}
faa2dbf0 Josef Bacik        2014-05-07  284  
b9ef22de Feifei Xu          2016-06-01  285  	ret = remove_extent_item(root, nodesize, nodesize);
faa2dbf0 Josef Bacik        2014-05-07  286  	if (ret)
faa2dbf0 Josef Bacik        2014-05-07  287  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  288  
b9ef22de Feifei Xu          2016-06-01  289  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
faa2dbf0 Josef Bacik        2014-05-07  290  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  291  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  292  		ulist_free(new_roots);
442244c9 Qu Wenruo          2015-04-16  293  		test_msg("Couldn't find old roots: %d\n", ret);
442244c9 Qu Wenruo          2015-04-16  294  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  295  	}
faa2dbf0 Josef Bacik        2014-05-07  296  
b9ef22de Feifei Xu          2016-06-01  297  	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
b9ef22de Feifei Xu          2016-06-01  298  					  nodesize, old_roots, new_roots);
faa2dbf0 Josef Bacik        2014-05-07  299  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  300  		test_msg("Couldn't account space for a qgroup %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  301  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  302  	}
faa2dbf0 Josef Bacik        2014-05-07  303  
ef9f2db3 Feifei Xu          2016-06-01  304  	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
faa2dbf0 Josef Bacik        2014-05-07  305  		test_msg("Qgroup counts didn't match expected values\n");
faa2dbf0 Josef Bacik        2014-05-07  306  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  307  	}
faa2dbf0 Josef Bacik        2014-05-07  308  
faa2dbf0 Josef Bacik        2014-05-07  309  	return 0;
faa2dbf0 Josef Bacik        2014-05-07  310  }
faa2dbf0 Josef Bacik        2014-05-07  311  

:::::: The code at line 232 was first introduced by commit
:::::: ef9f2db365c31433e52b0c5863793273bb632666 Btrfs: self-tests: Use macros instead of constants and add missing newline

:::::: TO: Feifei Xu <xufeifei@linux.vnet.ibm.com>
:::::: CC: David Sterba <dsterba@suse.com>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kernel test robot July 17, 2017, 7:03 a.m. UTC | #2
Hi Sargun,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.13-rc1 next-20170717]
[cannot apply to btrfs/next]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Sargun-Dhillon/New-qgroup-creation-removal-ioctls/20170715-195435
reproduce:
        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__


sparse warnings: (new ones prefixed by >>)

   include/linux/compiler.h:260:8: sparse: attribute 'no_sanitize_address': unknown attribute
>> fs/btrfs/tests/qgroup-tests.c:232:34: sparse: not enough arguments for function btrfs_create_qgroup
   fs/btrfs/tests/qgroup-tests.c:334:34: sparse: not enough arguments for function btrfs_create_qgroup
   fs/btrfs/tests/qgroup-tests.c: In function 'test_no_shared_qgroup':
   fs/btrfs/tests/qgroup-tests.c:232:8: error: too few arguments to function 'btrfs_create_qgroup'
     ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
           ^~~~~~~~~~~~~~~~~~~
   In file included from fs/btrfs/tests/qgroup-tests.c:24:0:
   fs/btrfs/tests/../qgroup.h:127:5: note: declared here
    int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
        ^~~~~~~~~~~~~~~~~~~
   fs/btrfs/tests/qgroup-tests.c: In function 'test_multiple_refs':
   fs/btrfs/tests/qgroup-tests.c:334:8: error: too few arguments to function 'btrfs_create_qgroup'
     ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID);
           ^~~~~~~~~~~~~~~~~~~
   In file included from fs/btrfs/tests/qgroup-tests.c:24:0:
   fs/btrfs/tests/../qgroup.h:127:5: note: declared here
    int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
        ^~~~~~~~~~~~~~~~~~~

vim +232 fs/btrfs/tests/qgroup-tests.c

faa2dbf0 Josef Bacik        2014-05-07  219  
b9ef22de Feifei Xu          2016-06-01  220  static int test_no_shared_qgroup(struct btrfs_root *root,
b9ef22de Feifei Xu          2016-06-01  221  		u32 sectorsize, u32 nodesize)
faa2dbf0 Josef Bacik        2014-05-07  222  {
faa2dbf0 Josef Bacik        2014-05-07  223  	struct btrfs_trans_handle trans;
faa2dbf0 Josef Bacik        2014-05-07  224  	struct btrfs_fs_info *fs_info = root->fs_info;
442244c9 Qu Wenruo          2015-04-16  225  	struct ulist *old_roots = NULL;
442244c9 Qu Wenruo          2015-04-16  226  	struct ulist *new_roots = NULL;
faa2dbf0 Josef Bacik        2014-05-07  227  	int ret;
faa2dbf0 Josef Bacik        2014-05-07  228  
7c55ee0c Omar Sandoval      2015-09-29  229  	btrfs_init_dummy_trans(&trans);
faa2dbf0 Josef Bacik        2014-05-07  230  
faa2dbf0 Josef Bacik        2014-05-07  231  	test_msg("Qgroup basic add\n");
ef9f2db3 Feifei Xu          2016-06-01 @232  	ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
faa2dbf0 Josef Bacik        2014-05-07  233  	if (ret) {
faa2dbf0 Josef Bacik        2014-05-07  234  		test_msg("Couldn't create a qgroup %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  235  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  236  	}
faa2dbf0 Josef Bacik        2014-05-07  237  
442244c9 Qu Wenruo          2015-04-16  238  	/*
01327610 Nicholas D Steeves 2016-05-19  239  	 * Since the test trans doesn't have the complicated delayed refs,
442244c9 Qu Wenruo          2015-04-16  240  	 * we can only call btrfs_qgroup_account_extent() directly to test
442244c9 Qu Wenruo          2015-04-16  241  	 * quota.
442244c9 Qu Wenruo          2015-04-16  242  	 */
b9ef22de Feifei Xu          2016-06-01  243  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
faa2dbf0 Josef Bacik        2014-05-07  244  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  245  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  246  		test_msg("Couldn't find old roots: %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  247  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  248  	}
faa2dbf0 Josef Bacik        2014-05-07  249  
ef9f2db3 Feifei Xu          2016-06-01  250  	ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
ef9f2db3 Feifei Xu          2016-06-01  251  				BTRFS_FS_TREE_OBJECTID);
faa2dbf0 Josef Bacik        2014-05-07  252  	if (ret)
faa2dbf0 Josef Bacik        2014-05-07  253  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  254  
b9ef22de Feifei Xu          2016-06-01  255  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
442244c9 Qu Wenruo          2015-04-16  256  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  257  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  258  		ulist_free(new_roots);
442244c9 Qu Wenruo          2015-04-16  259  		test_msg("Couldn't find old roots: %d\n", ret);
442244c9 Qu Wenruo          2015-04-16  260  		return ret;
442244c9 Qu Wenruo          2015-04-16  261  	}
442244c9 Qu Wenruo          2015-04-16  262  
b9ef22de Feifei Xu          2016-06-01  263  	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
b9ef22de Feifei Xu          2016-06-01  264  					  nodesize, old_roots, new_roots);
faa2dbf0 Josef Bacik        2014-05-07  265  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  266  		test_msg("Couldn't account space for a qgroup %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  267  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  268  	}
faa2dbf0 Josef Bacik        2014-05-07  269  
ef9f2db3 Feifei Xu          2016-06-01  270  	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
ef9f2db3 Feifei Xu          2016-06-01  271  				nodesize, nodesize)) {
faa2dbf0 Josef Bacik        2014-05-07  272  		test_msg("Qgroup counts didn't match expected values\n");
faa2dbf0 Josef Bacik        2014-05-07  273  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  274  	}
442244c9 Qu Wenruo          2015-04-16  275  	old_roots = NULL;
442244c9 Qu Wenruo          2015-04-16  276  	new_roots = NULL;
442244c9 Qu Wenruo          2015-04-16  277  
b9ef22de Feifei Xu          2016-06-01  278  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
442244c9 Qu Wenruo          2015-04-16  279  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  280  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  281  		test_msg("Couldn't find old roots: %d\n", ret);
442244c9 Qu Wenruo          2015-04-16  282  		return ret;
442244c9 Qu Wenruo          2015-04-16  283  	}
faa2dbf0 Josef Bacik        2014-05-07  284  
b9ef22de Feifei Xu          2016-06-01  285  	ret = remove_extent_item(root, nodesize, nodesize);
faa2dbf0 Josef Bacik        2014-05-07  286  	if (ret)
faa2dbf0 Josef Bacik        2014-05-07  287  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  288  
b9ef22de Feifei Xu          2016-06-01  289  	ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
faa2dbf0 Josef Bacik        2014-05-07  290  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  291  		ulist_free(old_roots);
442244c9 Qu Wenruo          2015-04-16  292  		ulist_free(new_roots);
442244c9 Qu Wenruo          2015-04-16  293  		test_msg("Couldn't find old roots: %d\n", ret);
442244c9 Qu Wenruo          2015-04-16  294  		return ret;
faa2dbf0 Josef Bacik        2014-05-07  295  	}
faa2dbf0 Josef Bacik        2014-05-07  296  
b9ef22de Feifei Xu          2016-06-01  297  	ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
b9ef22de Feifei Xu          2016-06-01  298  					  nodesize, old_roots, new_roots);
faa2dbf0 Josef Bacik        2014-05-07  299  	if (ret) {
442244c9 Qu Wenruo          2015-04-16  300  		test_msg("Couldn't account space for a qgroup %d\n", ret);
faa2dbf0 Josef Bacik        2014-05-07  301  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  302  	}
faa2dbf0 Josef Bacik        2014-05-07  303  
ef9f2db3 Feifei Xu          2016-06-01  304  	if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
faa2dbf0 Josef Bacik        2014-05-07  305  		test_msg("Qgroup counts didn't match expected values\n");
faa2dbf0 Josef Bacik        2014-05-07  306  		return -EINVAL;
faa2dbf0 Josef Bacik        2014-05-07  307  	}
faa2dbf0 Josef Bacik        2014-05-07  308  
faa2dbf0 Josef Bacik        2014-05-07  309  	return 0;
faa2dbf0 Josef Bacik        2014-05-07  310  }
faa2dbf0 Josef Bacik        2014-05-07  311  

:::::: The code at line 232 was first introduced by commit
:::::: ef9f2db365c31433e52b0c5863793273bb632666 Btrfs: self-tests: Use macros instead of constants and add missing newline

:::::: TO: Feifei Xu <xufeifei@linux.vnet.ibm.com>
:::::: CC: David Sterba <dsterba@suse.com>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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 */