From nobody Thu Dec 18 23:03:36 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8935C00140 for ; Mon, 15 Aug 2022 23:43:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354117AbiHOXnL (ORCPT ); Mon, 15 Aug 2022 19:43:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354082AbiHOXlJ (ORCPT ); Mon, 15 Aug 2022 19:41:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2A95AF48D; Mon, 15 Aug 2022 13:10:29 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 802746069F; Mon, 15 Aug 2022 20:10:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 878E2C433D6; Mon, 15 Aug 2022 20:10:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660594228; bh=iBlodVhxIf0LxzgFBWgv4W30nDtMAnVEY5Fng0C0a7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vy+moCRcXiNaCVSxeDnGb2CrWad2aEmHltzReBCsdwydGsTgri2amuzmfU1qjo5Qa 4YyK3CBPwT3yUf7oWL2n5aVepFks+aQrR+zei3nA6ZKI9cyCLChim8YotuboFJKNLZ uA/SQFZJoOzyxM4OCokPq1Kg4Po7Onu5r/QccaU0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Naohiro Aota , David Sterba Subject: [PATCH 5.18 1075/1095] btrfs: convert count_max_extents() to use fs_info->max_extent_size Date: Mon, 15 Aug 2022 20:07:54 +0200 Message-Id: <20220815180513.510007223@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Naohiro Aota commit 7d7672bc5d1038c745716c397d892d21e29de71c upstream. If count_max_extents() uses BTRFS_MAX_EXTENT_SIZE to calculate the number of extents needed, btrfs release the metadata reservation too much on its way to write out the data. Now that BTRFS_MAX_EXTENT_SIZE is replaced with fs_info->max_extent_size, convert count_max_extents() to use it instead, and fix the calculation of the metadata reservation. CC: stable@vger.kernel.org # 5.12+ Fixes: d8e3fb106f39 ("btrfs: zoned: use ZONE_APPEND write for zoned mode") Signed-off-by: Naohiro Aota Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/ctree.h | 21 +++++++++++++-------- fs/btrfs/delalloc-space.c | 6 +++--- fs/btrfs/inode.c | 16 ++++++++-------- 3 files changed, 24 insertions(+), 19 deletions(-) --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -107,14 +107,6 @@ struct btrfs_ioctl_encoded_io_args; #define BTRFS_STAT_CURR 0 #define BTRFS_STAT_PREV 1 =20 -/* - * Count how many BTRFS_MAX_EXTENT_SIZE cover the @size - */ -static inline u32 count_max_extents(u64 size) -{ - return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE); -} - static inline unsigned long btrfs_chunk_item_size(int num_stripes) { BUG_ON(num_stripes =3D=3D 0); @@ -3945,6 +3937,19 @@ static inline bool btrfs_is_zoned(const return fs_info->zoned !=3D 0; } =20 +/* + * Count how many fs_info->max_extent_size cover the @size + */ +static inline u32 count_max_extents(struct btrfs_fs_info *fs_info, u64 siz= e) +{ +#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS + if (!fs_info) + return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE); +#endif + + return div_u64(size + fs_info->max_extent_size - 1, fs_info->max_extent_s= ize); +} + static inline bool btrfs_is_data_reloc_root(const struct btrfs_root *root) { return root->root_key.objectid =3D=3D BTRFS_DATA_RELOC_TREE_OBJECTID; --- a/fs/btrfs/delalloc-space.c +++ b/fs/btrfs/delalloc-space.c @@ -273,7 +273,7 @@ static void calc_inode_reservations(stru u64 num_bytes, u64 disk_num_bytes, u64 *meta_reserve, u64 *qgroup_reserve) { - u64 nr_extents =3D count_max_extents(num_bytes); + u64 nr_extents =3D count_max_extents(fs_info, num_bytes); u64 csum_leaves =3D btrfs_csum_bytes_to_leaves(fs_info, disk_num_bytes); u64 inode_update =3D btrfs_calc_metadata_size(fs_info, 1); =20 @@ -349,7 +349,7 @@ int btrfs_delalloc_reserve_metadata(stru * needs to free the reservation we just made. */ spin_lock(&inode->lock); - nr_extents =3D count_max_extents(num_bytes); + nr_extents =3D count_max_extents(fs_info, num_bytes); btrfs_mod_outstanding_extents(inode, nr_extents); inode->csum_bytes +=3D disk_num_bytes; btrfs_calculate_inode_block_rsv_size(fs_info, inode); @@ -412,7 +412,7 @@ void btrfs_delalloc_release_extents(stru unsigned num_extents; =20 spin_lock(&inode->lock); - num_extents =3D count_max_extents(num_bytes); + num_extents =3D count_max_extents(fs_info, num_bytes); btrfs_mod_outstanding_extents(inode, -num_extents); btrfs_calculate_inode_block_rsv_size(fs_info, inode); spin_unlock(&inode->lock); --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -2159,10 +2159,10 @@ void btrfs_split_delalloc_extent(struct * applies here, just in reverse. */ new_size =3D orig->end - split + 1; - num_extents =3D count_max_extents(new_size); + num_extents =3D count_max_extents(fs_info, new_size); new_size =3D split - orig->start; - num_extents +=3D count_max_extents(new_size); - if (count_max_extents(size) >=3D num_extents) + num_extents +=3D count_max_extents(fs_info, new_size); + if (count_max_extents(fs_info, size) >=3D num_extents) return; } =20 @@ -2219,10 +2219,10 @@ void btrfs_merge_delalloc_extent(struct * this case. */ old_size =3D other->end - other->start + 1; - num_extents =3D count_max_extents(old_size); + num_extents =3D count_max_extents(fs_info, old_size); old_size =3D new->end - new->start + 1; - num_extents +=3D count_max_extents(old_size); - if (count_max_extents(new_size) >=3D num_extents) + num_extents +=3D count_max_extents(fs_info, old_size); + if (count_max_extents(fs_info, new_size) >=3D num_extents) return; =20 spin_lock(&BTRFS_I(inode)->lock); @@ -2301,7 +2301,7 @@ void btrfs_set_delalloc_extent(struct in if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { struct btrfs_root *root =3D BTRFS_I(inode)->root; u64 len =3D state->end + 1 - state->start; - u32 num_extents =3D count_max_extents(len); + u32 num_extents =3D count_max_extents(fs_info, len); bool do_list =3D !btrfs_is_free_space_inode(BTRFS_I(inode)); =20 spin_lock(&BTRFS_I(inode)->lock); @@ -2343,7 +2343,7 @@ void btrfs_clear_delalloc_extent(struct struct btrfs_inode *inode =3D BTRFS_I(vfs_inode); struct btrfs_fs_info *fs_info =3D btrfs_sb(vfs_inode->i_sb); u64 len =3D state->end + 1 - state->start; - u32 num_extents =3D count_max_extents(len); + u32 num_extents =3D count_max_extents(fs_info, len); =20 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) { spin_lock(&inode->lock);