Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp4230187pxk; Tue, 8 Sep 2020 14:18:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw49OfDywV5ZgnVJwpGgV8OCLiG5sojrlrA/QUPbhvGP4CNXL2TLKIAfUa2uFzzBIAw0hS+ X-Received: by 2002:a17:906:aacb:: with SMTP id kt11mr422316ejb.294.1599599887831; Tue, 08 Sep 2020 14:18:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599599887; cv=none; d=google.com; s=arc-20160816; b=ZjchBhUFFCXBrDuneroJoA58XE6FusBJ1HU77lo61u/UbThw5zSGoJDpXDjfdAFXnW P9hyzodj2+RMkNDTXr6j+IGmi7I0r+ikSTxrEk7EJLPTlcT88wuP/BdXBSMVMLipV+qt u+ovMD+NJrx2gy/aJ1IPvS35OZ100PCCFiZBdSsS4gXJF1FN/c0gFemNYgir8v8ddwiP 7IlHhlCvDvulLp/RyMnqpvI9XDuRCoW34qnZqbTuePe3Q5bslfW89oPfJ1DNmpsPA59a GiXx4wNORp0Q0kShcWbPGswTO0+atB4txkVHnzbluIsGujLKH3jNhLquFYjg3w97sS9e jlZg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=r9AXvnpGRVdIHHrypowmSWVOZvBbhlfxcSiSD/vJ/Vc=; b=qKZuiNKNj1rQgkgywl2iyIHCfnNaCmRI+2Q9SgEOoWHMdO1FUA9jRKV1C3sOCeHpFg UJLI5PkUENxluge8klfYdexO16sfeXfCsQFa+AmUVA5tZh05MPfCBsXiMbUH8aBSDw4F yDNlN4Wk3DNt5uHzrQHt3N42nodtLiW4Lw/V4OPlNaM7qbQYrQqfyD0cD17ift+WBUlK y8arJKDEH1eDcDx8minbqZh5ida92sENwCu2i4uqwc9YdVdxlYk/nfnTPEBLedu2zP9p zav1bAfXxX9V4q5ugI5jCq6gQPbUYzAqk9SEzpn1CYB0OnKhehM8RkgRu4FuPqSBEcsd jIXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gWko5L8R; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cb15si152647ejb.366.2020.09.08.14.17.45; Tue, 08 Sep 2020 14:18:07 -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=gWko5L8R; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731821AbgIHTLF (ORCPT + 99 others); Tue, 8 Sep 2020 15:11:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:53450 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731212AbgIHQFX (ORCPT ); Tue, 8 Sep 2020 12:05:23 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E7E9624672; Tue, 8 Sep 2020 15:39:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599579565; bh=KL/RnWFdKMBOtqrl2VD04BYDPvj9MiMytGRWijMGInc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gWko5L8RGYr50jiWd8B6lwYl4P3imRCKY8QB3FkTYjZuocuNG1WAAvcH67se7V61a 172a3GUX3buZL7N4TEaSwOOE+AH7rPBmJd9b0TEmAA2jJN4hS0ZIizljuylt0oQHVX 3CVodfbqtVrffEBg1iG1VhltDSZMC6KKKn+4ZR6k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qu Wenruo , Filipe Manana , Marcos Paulo de Souza , David Sterba Subject: [PATCH 5.8 130/186] btrfs: block-group: fix free-space bitmap threshold Date: Tue, 8 Sep 2020 17:24:32 +0200 Message-Id: <20200908152247.941811240@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200908152241.646390211@linuxfoundation.org> References: <20200908152241.646390211@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marcos Paulo de Souza commit e3e39c72b99f93bbd0420d38c858e7c4a061bb63 upstream. [BUG] After commit 9afc66498a0b ("btrfs: block-group: refactor how we read one block group item"), cache->length is being assigned after calling btrfs_create_block_group_cache. This causes a problem since set_free_space_tree_thresholds calculates the free-space threshold to decide if the free-space tree should convert from extents to bitmaps. The current code calls set_free_space_tree_thresholds with cache->length being 0, which then makes cache->bitmap_high_thresh zero. This implies the system will always use bitmap instead of extents, which is not desired if the block group is not fragmented. This behavior can be seen by a test that expects to repair systems with FREE_SPACE_EXTENT and FREE_SPACE_BITMAP, but the current code only created FREE_SPACE_BITMAP. [FIX] Call set_free_space_tree_thresholds after setting cache->length. There is now a WARN_ON in set_free_space_tree_thresholds to help preventing the same mistake to happen again in the future. Link: https://github.com/kdave/btrfs-progs/issues/251 Fixes: 9afc66498a0b ("btrfs: block-group: refactor how we read one block group item") CC: stable@vger.kernel.org # 5.8+ Reviewed-by: Qu Wenruo Reviewed-by: Filipe Manana Signed-off-by: Marcos Paulo de Souza Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/block-group.c | 4 +++- fs/btrfs/free-space-tree.c | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -1814,7 +1814,6 @@ static struct btrfs_block_group *btrfs_c cache->fs_info = fs_info; cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); - set_free_space_tree_thresholds(cache); cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; @@ -1928,6 +1927,8 @@ static int read_one_block_group(struct b if (ret < 0) goto error; + set_free_space_tree_thresholds(cache); + if (need_clear) { /* * When we mount with old space cache, we need to @@ -2148,6 +2149,7 @@ int btrfs_make_block_group(struct btrfs_ return -ENOMEM; cache->length = size; + set_free_space_tree_thresholds(cache); cache->used = bytes_used; cache->flags = type; cache->last_byte_to_unpin = (u64)-1; --- a/fs/btrfs/free-space-tree.c +++ b/fs/btrfs/free-space-tree.c @@ -22,6 +22,10 @@ void set_free_space_tree_thresholds(stru size_t bitmap_size; u64 num_bitmaps, total_bitmap_size; + if (WARN_ON(cache->length == 0)) + btrfs_warn(cache->fs_info, "block group %llu length is zero", + cache->start); + /* * We convert to bitmaps when the disk space required for using extents * exceeds that required for using bitmaps.