Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp56114ybg; Mon, 8 Jun 2020 16:18:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzmDksOpDu5NwQmbLe6gOck2h95aGF1xeWMro66MKLKeG1w5eGmPs1wu+4+nOd79f/KA1N X-Received: by 2002:aa7:c908:: with SMTP id b8mr24327014edt.76.1591658280817; Mon, 08 Jun 2020 16:18:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591658280; cv=none; d=google.com; s=arc-20160816; b=M0jtrxikHdET+YTTChOJYPxNZLWx99q3Pn0smvIJPKXs2yWlo3Xfw1gMFRo+ZGr9Et O6ClRKxECFStdwBpZDhBb7sfF1Euc1RmZtGqROfUp/0ODxNlwc4BIgMVt+ZhtXv4/1Py nL9TIozDBExwo5FccPNPaaypjHh7CDx/q/yPB1G27pqnjSLPnN8x5uwCa4nCAtGxs1/G Q+6G5BFvJevrK7atfx106Uye0IfkAxab6glY+8SncJ5uImmRx2ZUliMXUipVEmUiCXLj f8UgvERErDfLlpfRNsvehswt5vCKg/RVB9EO7QlGy8FZQMxxvFgwlD3iTyGR+clq8hYy w/uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+NjhBG6Ex7yYtzakUfW7zAU6pFIRJ/FeRQaDHEy1ucA=; b=SrVtOzIDyEKR1WnF5M8XPIHnN+Adnwm95HSQCrMb3l+dZ68dnBk1VXlNMggyUF8BlF KWkuPKmaTGEL+uiKU5Zo+KOfKcH4Q4L8fRm6MzR8v8zkqfg7iviF0BlTOPn6oiOOYEkN diHAVsc0ALnfSeCRshM8A/yCILyHH43i+25v6xP65u/F4VFn88XHt94HP8k73dQRdsqC 7uiZXa9IiAXM/3uKgfC7fwHx5Mw0zjPrk7w3W0WzSegHmCuBcbvm5sxw4MVObOfLJvK1 6SoXv1eyzfozbrXUUz9jN0zqpKPsbt3q9tW2vNZJ7nKWKdxOcMzkjI89K0ftxetnQ2aV XMDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qGZ0luR+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ch25si9482832ejb.62.2020.06.08.16.17.38; Mon, 08 Jun 2020 16:18:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qGZ0luR+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728194AbgFHXNY (ORCPT + 99 others); Mon, 8 Jun 2020 19:13:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:57974 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728872AbgFHXLQ (ORCPT ); Mon, 8 Jun 2020 19:11:16 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5009E20C09; Mon, 8 Jun 2020 23:11:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591657876; bh=Tt/7pY4PnIPEIc2iLv2wehgd86XaeBM8jaynhxE6sOg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qGZ0luR+0R2bICqI7joi6cV5XPYQ92mfb9q2LJaCuId3GW+1rJpF+kShVuY65tEqW rNM9XJsAxQQwlvAMz3iGrWJDmeB8i/Le8KPX065sDb8CA3gBVK/jVSm0j9VxP1lsEh QRMijfySlv9679H9dNvBp6MZ7lXSuIBOjw/fVuIQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qu Wenruo , Josef Bacik , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 5.7 233/274] btrfs: qgroup: mark qgroup inconsistent if we're inherting snapshot to a new qgroup Date: Mon, 8 Jun 2020 19:05:26 -0400 Message-Id: <20200608230607.3361041-233-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200608230607.3361041-1-sashal@kernel.org> References: <20200608230607.3361041-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo [ Upstream commit cbab8ade585a18c4334b085564d9d046e01a3f70 ] [BUG] For the following operation, qgroup is guaranteed to be screwed up due to snapshot adding to a new qgroup: # mkfs.btrfs -f $dev # mount $dev $mnt # btrfs qgroup en $mnt # btrfs subv create $mnt/src # xfs_io -f -c "pwrite 0 1m" $mnt/src/file # sync # btrfs qgroup create 1/0 $mnt/src # btrfs subv snapshot -i 1/0 $mnt/src $mnt/snapshot # btrfs qgroup show -prce $mnt/src qgroupid rfer excl max_rfer max_excl parent child -------- ---- ---- -------- -------- ------ ----- 0/5 16.00KiB 16.00KiB none none --- --- 0/257 1.02MiB 16.00KiB none none --- --- 0/258 1.02MiB 16.00KiB none none 1/0 --- 1/0 0.00B 0.00B none none --- 0/258 ^^^^^^^^^^^^^^^^^^^^ [CAUSE] The problem is in btrfs_qgroup_inherit(), we don't have good enough check to determine if the new relation would break the existing accounting. Unlike btrfs_add_qgroup_relation(), which has proper check to determine if we can do quick update without a rescan, in btrfs_qgroup_inherit() we can even assign a snapshot to multiple qgroups. [FIX] Fix it by manually marking qgroup inconsistent for snapshot inheritance. For subvolume creation, since all its extents are exclusively owned, we don't need to rescan. In theory, we should call relation check like quick_update_accounting() when doing qgroup inheritance and inform user about qgroup accounting inconsistency. But we don't have good mechanism to relay that back to the user in the snapshot creation context, thus we can only silently mark the qgroup inconsistent. Anyway, user shouldn't use qgroup inheritance during snapshot creation, and should add qgroup relationship after snapshot creation by 'btrfs qgroup assign', which has a much better UI to inform user about qgroup inconsistent and kick in rescan automatically. Reviewed-by: Josef Bacik Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/qgroup.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c index c3888fb367e7..5bd4089ad0e1 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -2622,6 +2622,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, struct btrfs_root *quota_root; struct btrfs_qgroup *srcgroup; struct btrfs_qgroup *dstgroup; + bool need_rescan = false; u32 level_size = 0; u64 nums; @@ -2765,6 +2766,13 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, goto unlock; } ++i_qgroups; + + /* + * If we're doing a snapshot, and adding the snapshot to a new + * qgroup, the numbers are guaranteed to be incorrect. + */ + if (srcid) + need_rescan = true; } for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) { @@ -2784,6 +2792,9 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dst->rfer = src->rfer - level_size; dst->rfer_cmpr = src->rfer_cmpr - level_size; + + /* Manually tweaking numbers certainly needs a rescan */ + need_rescan = true; } for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) { struct btrfs_qgroup *src; @@ -2802,6 +2813,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dst->excl = src->excl + level_size; dst->excl_cmpr = src->excl_cmpr + level_size; + need_rescan = true; } unlock: @@ -2809,6 +2821,8 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, out: if (!committing) mutex_unlock(&fs_info->qgroup_ioctl_lock); + if (need_rescan) + fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; return ret; } -- 2.25.1