From patchwork Tue Oct 8 00:27:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13825480 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAE363214 for ; Tue, 8 Oct 2024 00:28:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728347324; cv=none; b=gWHm8CbQtJsPqF0jgJf0KIgdfjT53uYwl93K1W4H5YmIEE4CzTvGOgQavDLGrLO5Rv3sNJBMSN4g6VYhVOCD//18QSdus/YlU4bpaWiHUUFU09eot3hULTqfcc5kbvSiySWf5Goqu6MQxAqzguTEtV5P04u4mz5aPINbEQ48BnQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728347324; c=relaxed/simple; bh=G0D0Eix0H1+sZqZytDho2dcXOhVvAWqqjT4t9n5N3xY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iZIcHk2ataSV41kUGXxNUSs7RDdMHTfhnO7hn+7O8X6oWZMTNZGHjju+vY1tHtb4F2gPXcztMQmDe7nyZz3BoEK2Yu7RpjwsYjSWpqzmZl/galw/x9Uztaxx1D9CRai8tDmvpuY0CfMz3TtaUtiUNbrKXRFfHZGoQqzljyIYymc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KxLIFZ/Q; arc=none smtp.client-ip=209.85.210.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KxLIFZ/Q" Received: by mail-pf1-f193.google.com with SMTP id d2e1a72fcca58-71e06ba441cso1047353b3a.1 for ; Mon, 07 Oct 2024 17:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728347322; x=1728952122; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=QI08yDpMes1iXwZwkZOt6HU4/wrX2VQfUF+zUPPZD5M=; b=KxLIFZ/Qf189+MDD5IvcCf6aA6ZW4kr7POFPVQ/xjkvGrI3cTLpKJEPqNOtahHuqZt wIaxqEwUYZLj3U3WaLDwpqj7Cw003KAfuX7HT8KrBkMxpTwsRpfkpbRNjqttIlRNmbpt UNtjzGg5LAu6xKyCQ4gPt9dFVmhPwQ0lwZBj2yKBU4k2JoWzb9dU973evr6jNGHr3nZr /FH2OBzfYnYB5jU4aiQby0AWznO6TOxDveZO27l9yBPqKZFIhEhgCzjcOMmg+m+Q+YiV 21M8wlSxjVgLA1SuBavuYw//IUmCqZh1LKQ+MU5up6KofCf/yvj5p5Sl0jnuCRz8T8Fm gSBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728347322; x=1728952122; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QI08yDpMes1iXwZwkZOt6HU4/wrX2VQfUF+zUPPZD5M=; b=Qb6Bmsd+vKonK8wK4dX8yY8dsduMZKHoW3BLgO9KbbDaRyEyqHT57NO0RWUFbXOkuc K4QiQPQ/e7WRPW2GCVJT7TAxw4mazSOs56bNFj6xrjS6/RKTfKa09hHy+jyQKk1L2i0/ jbWfqBB310stevBm6jvqs9wmOSwG5BDsOySp4DaTU0CwtTto9yQBsBCbkAFMhbnYuxao NpCOLBgegolUz0mA+NY2zrSt7hpfv9K7LD4cjdDanaF6lCmiezhxKL5cWj55SIUiOLT+ +4oruCGuXfUySXO1UaPMSR7IEidyCKeAQINwMB3rBYLea0pgWRWWpwtAl8DE2YaT3f8B +7xg== X-Gm-Message-State: AOJu0Yx2pzjk01aVuzhMn593Kpq01gh2bIPa07rcWoWfEYR72zNBs0g/ qyfIq7OAtg1tLMvY8ZimXJ0JlyOlXXhjgcp9Lspj8wb7n/1jdpPA5OLKQUGA X-Google-Smtp-Source: AGHT+IFpZPdz3X1xMbt0l/QDj+0YI6GQeSUcwyv5aA/U4pDQSUL/im1TM8Oa7QA9Idz/uId3Gd8eFw== X-Received: by 2002:a05:6a20:c909:b0:1d2:e78a:cab with SMTP id adf61e73a8af0-1d6dfadd42dmr18944640637.35.1728347321906; Mon, 07 Oct 2024 17:28:41 -0700 (PDT) Received: from localhost (fwdproxy-eag-001.fbsv.net. [2a03:2880:3ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d67c22sm5014891b3a.163.2024.10.07.17.28.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 17:28:41 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v3 1/3] btrfs-progs: remove block group free space Date: Mon, 7 Oct 2024 17:27:46 -0700 Message-ID: <96adc196ea0a449be6299ef7d77c4b814e76af3a.1728346056.git.loemra.dev@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In the upstream equivalent of btrfs_remove_block_group(), the function remove_block_group_free_space() is called to delete free spaces associated with the block group being freed. However, this function is defined in btrfs-progs but not called anywhere. To address this issue, I added a call to remove_block_group_free_space() in btrfs_remove_block_group(). This ensures that the free spaces are properly deleted when a block group is removed. I also added a check to remove_block_group_free_space to make sure that free-space-tree is enabled. Signed-off-by: Leo Martins --- CHANGELOG: v3: - No change v2: - Added the check to make sure that free-space-tree is enabled --- kernel-shared/extent-tree.c | 10 ++++++++++ kernel-shared/free-space-tree.c | 3 +++ 2 files changed, 13 insertions(+) diff --git a/kernel-shared/extent-tree.c b/kernel-shared/extent-tree.c index af04b9ea..f44126e2 100644 --- a/kernel-shared/extent-tree.c +++ b/kernel-shared/extent-tree.c @@ -3536,6 +3536,16 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, return ret; } + /* delete free space items associated with this block group */ + ret = remove_block_group_free_space(trans, block_group); + if (ret < 0) { + fprintf(stderr, + "failed to remove free space associated with block group for [%llu,%llu)\n", + bytenr, bytenr + len); + btrfs_unpin_extent(fs_info, bytenr, len); + return ret; + } + /* Now release the block_group_cache */ ret = free_block_group_cache(trans, fs_info, bytenr, len); btrfs_unpin_extent(fs_info, bytenr, len); diff --git a/kernel-shared/free-space-tree.c b/kernel-shared/free-space-tree.c index 81fd57b8..61263f94 100644 --- a/kernel-shared/free-space-tree.c +++ b/kernel-shared/free-space-tree.c @@ -1171,6 +1171,9 @@ int remove_block_group_free_space(struct btrfs_trans_handle *trans, int done = 0, nr; int ret; + if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) + return 0; + path = btrfs_alloc_path(); if (!path) { ret = -ENOMEM; From patchwork Tue Oct 8 00:27:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13825481 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2237AAD24 for ; Tue, 8 Oct 2024 00:28:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728347325; cv=none; b=CS5yGJdqceCjzd9aP7HSbXkePhhi1gYOPu6GouJBnfqKxj3B2E0aN7fDKmkzSDP6MzRZjF6dQSycpgT9MdD+6pcRiSufH4oNhi5QZg5bqHcSdoBNTINqo5wl3eBqTIeoGKJZxGv9czt1IWmIFfa616pIVMAF22EWnVFwa7RSnEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728347325; c=relaxed/simple; bh=yGVx+6IsFC4X5rLjroo099GW2HO/SYUKsVe3Icz7Sac=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s4xBmrNAh6hjHRaQFvp6U+tYFSTXe4raA/pHfstR6C8dODfufW+dQSrO9n1SW2xLHmKsytBwySmhqScmWiOdAfiwtsEnyFVOyYvB+n6X5+RzLp38ySKaxh8XSGmu89zjSjRzunI1H8Ckj8Il7zy9E9+QQN+WGrfSe7xUFjNfCZk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Vd8HCBOV; arc=none smtp.client-ip=209.85.214.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Vd8HCBOV" Received: by mail-pl1-f196.google.com with SMTP id d9443c01a7336-20ba733b904so49503155ad.1 for ; Mon, 07 Oct 2024 17:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728347323; x=1728952123; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=NFAdmzEnV3i1fxUaclUOs9yaWxIhH+cg4GHI/fQaUq0=; b=Vd8HCBOVG5lg74IpozUIYjmHz2GuLCJkri1c26CrtB1LRR7cFSsZ0v9GWS7eVmyklO 1fqpdnyiFwUmEpe8wlueCiUkbMzB1+/kn2ozCifqV95GjIGPZ2YcoFILrKYHPl54AW7o bl+xADRCpzT80aUjsmewZLWN1W9ryLhuN40tBcm+JWWYItC0lq5pZmMoGRKrPBBZvoHg R+Cjyp+NswIjIFqzIODQpUQ8faTeO+txHH5b3zi17gvn+y0eu0GJtJsJSYORgbEzQ40R +I/rkBSakuTkesxSdxs+JCKd3n8wimPjPDEomWb4g/Zam0C8//+KWi2UfO8PeJRn2fju XVEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728347323; x=1728952123; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NFAdmzEnV3i1fxUaclUOs9yaWxIhH+cg4GHI/fQaUq0=; b=XFxblVlYQisZL0YlG1APNFDmwv9/q6ytTA8nn2Kw0fK/cCn3QJgtvF105myG62Qy0y xwjN1BvvmRHBlzqXef79isrjm0jA2OCyb15McMi1bS7NEjg+OEFnumKG3W4rJyglAigU ACEtixuFJDoQkT3/Mq1TPW9W3kM2iEe39PL3AOEyodiPr0p515Wf2K1AXLynxwgzHR7T SrUvGS1f5nri4TesZs7BtigxNcY4dXijy127wUHFc+zoqpr2hGbQiFT4eK4Nz3YBd0+F OoSoCOn4/jGJKHBoMKNsEAiiegVtSYy7ghelIWsbHBCbDMdsYK4dK6EgCO8Wu4QkJV4Y LZqw== X-Gm-Message-State: AOJu0Yz5gbwWehTsNbRgQ19+OMjLPnnh06YvMq/EYkoNq0ur35lJvlt7 IlpcbKHBO5ZKrEqfq6TNM1rHAteGJqOn//3ZQpqRU9ldePham4SDKQ+Zaqgq X-Google-Smtp-Source: AGHT+IESsDO1alKgNd7gRd39MgzVS2wXsJ0ahvgcptnOak186V2xLE2Xhj78NbuEiT0Up89zjBVY+w== X-Received: by 2002:a17:903:1c1:b0:206:c12d:abad with SMTP id d9443c01a7336-20bfe04d596mr192489925ad.34.1728347323114; Mon, 07 Oct 2024 17:28:43 -0700 (PDT) Received: from localhost (fwdproxy-eag-115.fbsv.net. [2a03:2880:3ff:73::face:b00c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c13934971sm45416155ad.150.2024.10.07.17.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 17:28:42 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v3 2/3] btrfs-progs: check free space maps to block group Date: Mon, 7 Oct 2024 17:27:47 -0700 Message-ID: X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Check that the block-group that is found matches the objectid and offset of the free-space-info. Without this the check only verifies that there is some block-group that exists with objectid >= free-space-info's objectid. I have softened the language of the warning and included instructions on how to fix the problem. This can be done in a couple of ways: - btrfs check --repair - btrfs rescue clear-space-cache v2 I chose to include btrfs rescue as it is more targeted. Signed-off-by: Leo Martins --- CHANGELOG: v3: - softened the warning and added instructions --- common/clear-cache.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/common/clear-cache.c b/common/clear-cache.c index 6493866d..6b362f64 100644 --- a/common/clear-cache.c +++ b/common/clear-cache.c @@ -165,9 +165,16 @@ static int check_free_space_tree(struct btrfs_root *root) } bg = btrfs_lookup_first_block_group(fs_info, key.objectid); - if (!bg) { + if (!bg || key.objectid != bg->start || + key.offset != bg->length) { fprintf(stderr, - "We have a space info key for a block group that doesn't exist\n"); + "We have a space info key [%llu %u %llu] for a block group that " + "doesn't exist.\n", + key.objectid, key.type, key.offset); + fprintf(stderr, + "This is likely due to a minor bug in mkfs.btrfs that doesn't properly\n" + "cleanup free spaces and can be fixed using btrfs rescue " + "clear-space-cache v2\n"); ret = -EINVAL; goto out; } From patchwork Tue Oct 8 00:27:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13825482 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6266C148 for ; Tue, 8 Oct 2024 00:28:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728347327; cv=none; b=ueoFPcXrqOuQz5HC1crXROr+mWkX0ZqeXtNq5JPYDgdRBDsEe6urKS4zxSKzgioQQL9WyJzesmsUrwwvgL+/my5njZkgfHhaqWF0gIR9O8eRbNeS41UzN5NrgbHzqIhLFEbAdKzybGEA0cvO252EaTnFH2Z6mnEgS7NbJ45/qt0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728347327; c=relaxed/simple; bh=tMKwIfEqDC2CcRuFj7V/i4ZDx2ncXTo0kTM8V2a3MHs=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oBJYDgaAsCzi94ETl4qac1TpeXqC2HRZ5bPgtCb4fqs6AAo7yrEgRCyJ6bYx2RlBhUnzNror/1AhZuMg/j+nfRvPwMCbgdXYiHx+ApHMHcxJ16sPq2FwTHUVIEX01YyD+kkg3Sad2bYFSFRDiM5IK11veTWJmuINne5FA9/UAg4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LNelfpbb; arc=none smtp.client-ip=209.85.215.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LNelfpbb" Received: by mail-pg1-f193.google.com with SMTP id 41be03b00d2f7-7c3e1081804so2274389a12.3 for ; Mon, 07 Oct 2024 17:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728347324; x=1728952124; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=qRlDen1x04WLqn4bdS5L9+9UdbeAOT6cyt5e05ZYNdY=; b=LNelfpbboz2DkzxBEkh4WwviOPxXecYtQ4v689gPoHgvkglpA2lZCwx/EoLVtnrOyo Jjd5KNX7b+2JmlnAZTCP5z1+fKSnFFu6vlFm4OiC8R2uxDUMK2qXzUW5/V4VOrdsXSiu +ANXgDXa/aJiQ4AFdN5n9bBJoJJh27lsrdDzL8COpmShy+AEw+WUDZraXr2HJP2ZsGQ8 OIKrrLRimfG6x19XS1UCcVY0n8RAWQAgf5lCnMWW8eY+MYRMYfc5uACIDVXCckzQh3pW 5ER2V8QoTHnFKPNJLUp9AI/UtKedBb3UKOmFQAvZioUmsQXP8/UbA91DY+uDljE9nfgH VMDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728347324; x=1728952124; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qRlDen1x04WLqn4bdS5L9+9UdbeAOT6cyt5e05ZYNdY=; b=LsMEF8Z7HIWfM3trvzGzAFkI1kBZ6st1Zby4JI+Wl1GWs4Oxw0KapJLrAUOVB1lKl8 OC4ifdnUqj/3EqrLPkoE6OYpyieGHN9mCZHVuhSwmzXV9cEYFu7oSicxFxxb8c7+la7D ZWFxQgNEdKhZVPDZENSje8Vc+AH8SlO5MhFMbCdxKvDeSoGPVs31mxUwU+MzcFo0nKqp elP4LuNT2ZrwiSiTdhQYBxzkgvrLX0whP807XCxc8l6wfarZk5YwWuUZc9SEX3FK6APK A4H8PoQACzRT2RS+e6qDOZU0+nzm+DfLVMiTggnnMFvp7EcLZcdaD80TSu0NwuFMhXES kndw== X-Gm-Message-State: AOJu0YyX+jE+j5Uu3e0nb5qe6sLa6k+sHsftdrB11kxiwZ6D9h6bf0Vv BZUWmnuPwmSqS+TTzwARu12esvyZi8fT1CqrdRrR5ng8PHemvhc9AUUouc2/ X-Google-Smtp-Source: AGHT+IGdSVt+p/jQWpe69olpioup7f7VFQDZAtV6D7PCtg8okvRWv7q0g5F0V8PUqW4Qhf9zf2m1tg== X-Received: by 2002:a05:6a21:3414:b0:1d4:fb0c:613 with SMTP id adf61e73a8af0-1d6dfa27e78mr17016711637.1.1728347324299; Mon, 07 Oct 2024 17:28:44 -0700 (PDT) Received: from localhost (fwdproxy-eag-007.fbsv.net. [2a03:2880:3ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0cd16a3sm5005355b3a.59.2024.10.07.17.28.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 17:28:44 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v3 3/3] btrfs-progs: free-space-info tree-checker Date: Mon, 7 Oct 2024 17:27:48 -0700 Message-ID: <41fc4441860cc2769fd11c236067ff979547faf7.1728346056.git.loemra.dev@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a new check in check_leaf_item for btrfs_free_space_info. This check performs exactly the same check that is performed in btrfs check. That is it searches for the block group that the current free-space-info belogns to and warns if there is none. When I was testing I found that sometimes this would be called before the block group cache was initialized leading to incorrect warnings so I added a check to make sure that the cache was initialized. I also chose to not return an error since this bug does not really affect the ability of the system to function properly. I'm still not convinced that this tree-checker is helpful or necessary so if anyone has any opinions I would love to hear them! If this is deemed helpful I will send out another patch to add this check to the kernel tree-checker. Signed-off-by: Leo Martins --- kernel-shared/tree-checker.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/kernel-shared/tree-checker.c b/kernel-shared/tree-checker.c index 0be8f022..cf2e389e 100644 --- a/kernel-shared/tree-checker.c +++ b/kernel-shared/tree-checker.c @@ -1737,6 +1737,32 @@ static int check_raid_stripe_extent(const struct extent_buffer *leaf, return 0; } +static int check_free_space_info_item(const struct extent_buffer *leaf, + const struct btrfs_key *key, int slot) +{ + struct btrfs_fs_info *fs_info = leaf->fs_info; + struct btrfs_block_group *bg; + + /* block_group_cache is uninitialized at this point */ + if (!fs_info->block_group_cache_tree.rb_node) + return 0; + + bg = btrfs_lookup_first_block_group(fs_info, key->objectid); + if (unlikely(!bg || key->objectid != bg->start || + key->offset != bg->length)) { + generic_err( + leaf, slot, + "We have a space info key [%llu %u %llu] for a block group that " + "doesn't exist.\n" + "This is likely due to a minor bug in mkfs.btrfs that doesn't properly\n" + "cleanup free spaces and can be fixed using btrfs rescue " + "clear-space-cache v2\n", + key->objectid, key->type, key->offset); + } + + return 0; +} + /* * Common point to switch the item-specific validation. */ @@ -1798,6 +1824,9 @@ static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf, case BTRFS_RAID_STRIPE_KEY: ret = check_raid_stripe_extent(leaf, key, slot); break; + case BTRFS_FREE_SPACE_INFO_KEY: + ret = check_free_space_info_item(leaf, key, slot); + break; } if (ret)