From patchwork Wed Dec 11 14:53:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903619 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 036AB1A9B2A for ; Wed, 11 Dec 2024 14:53:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928813; cv=none; b=NfRGkAu+xECcvPGvlx7yhtVz9vlBWeTDM5gIiE3aDSiiIAue/pC6mPLiqJKZsrU7FA2WuohdBjoxRxis0cgHR22hxpMkLGnPiJWSGk4SFKKfWHmR3EQNBHEBo9402AO8iJBqIY26CmqXkV1Aogm5cdl0k4pHYn7uqB4imPrnJmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928813; c=relaxed/simple; bh=e5FY9cFPOz7HTCMHUO8Q9Tyjy0uK6qB42bpCRh5M0uY=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lpaP0Hq7mAI3rF9sLWQPwzKuaD0ZYf4j2RokCNGOCOnge0U+4OMfIGSowckGmgSiRZHOIhoSNn/WSVlwoPpdOoyZUDsF8HNBQnCJiOK/ctX2j+CXaxrB5vHpUxKDS2Xe4FRWvVPSB/gDGyrT6iu/4L4EKn2oIHIHI37OY2Z/IaM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eSUdtn7F; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="eSUdtn7F" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05BE6C4CED2 for ; Wed, 11 Dec 2024 14:53:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928812; bh=e5FY9cFPOz7HTCMHUO8Q9Tyjy0uK6qB42bpCRh5M0uY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=eSUdtn7FN+w+ePTiDxg99l42DJnm25lc4q44c183HfdB558asRPfhix/HP27V8OHi vOFSI959oSHX/W+m/Qd1FQQg0uxblMf2pa4v64q0qV0ZXRFDU3wSfVAV+F4OhKrUwx OqGIvDun1zXSPQxxqNjss5JI4Xex1k2X/A1DXYPpF9rMFnO8cuxDBzSPKBw1ROrZXP nWg7oL5SsowouHK0Xs1mHiLSb38dXviYOTCkwvmbJHIoz4gTfsWLLnCw28PYD/ax6s u4t53v/LkYDA3kJWw9xErXPGB566a8xU+785tDW8+9IQUETT3s6zky1E3QQbNltU6v TrAt7dpY8o/aA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 01/11] btrfs: fix race with memory mapped writes when activating swap file Date: Wed, 11 Dec 2024 14:53:18 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana When activating the swap file we flush all delalloc and wait for ordered extent completion, so that we don't miss any delalloc and extents before we check that the file's extent layout is usable for a swap file and activate the swap file. We are called with the inode's VFS lock acquired, so we won't race with buffered and direct IO writes, however we can still race with memory mapped writes since they don't acquire the inode's VFS lock. The race window is between flushing all delalloc and locking the whole file's extent range, since memory mapped writes lock an extent range with the length of a page. Fix this by acquiring the inode's mmap lock before we flush delalloc. Signed-off-by: Filipe Manana --- fs/btrfs/inode.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index c4997200dbb2..926d82fbdbae 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -9809,6 +9809,15 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, u64 isize; u64 start; + /* + * Acquire the inode's mmap lock to prevent races with memory mapped + * writes, as they could happen after we flush delalloc below and before + * we lock the extent range further below. The inode was already locked + * up in the call chain. + */ + btrfs_assert_inode_locked(BTRFS_I(inode)); + down_write(&BTRFS_I(inode)->i_mmap_lock); + /* * If the swap file was just created, make sure delalloc is done. If the * file changes again after this, the user is doing something stupid and @@ -9816,22 +9825,25 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, */ ret = btrfs_wait_ordered_range(BTRFS_I(inode), 0, (u64)-1); if (ret) - return ret; + goto out_unlock_mmap; /* * The inode is locked, so these flags won't change after we check them. */ if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { btrfs_warn(fs_info, "swapfile must not be compressed"); - return -EINVAL; + ret = -EINVAL; + goto out_unlock_mmap; } if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { btrfs_warn(fs_info, "swapfile must not be copy-on-write"); - return -EINVAL; + ret = -EINVAL; + goto out_unlock_mmap; } if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { btrfs_warn(fs_info, "swapfile must not be checksummed"); - return -EINVAL; + ret = -EINVAL; + goto out_unlock_mmap; } /* @@ -9846,7 +9858,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) { btrfs_warn(fs_info, "cannot activate swapfile while exclusive operation is running"); - return -EBUSY; + ret = -EBUSY; + goto out_unlock_mmap; } /* @@ -9860,7 +9873,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, btrfs_exclop_finish(fs_info); btrfs_warn(fs_info, "cannot activate swapfile because snapshot creation is in progress"); - return -EINVAL; + ret = -EINVAL; + goto out_unlock_mmap; } /* * Snapshots can create extents which require COW even if NODATACOW is @@ -9881,7 +9895,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, btrfs_warn(fs_info, "cannot activate swapfile because subvolume %llu is being deleted", btrfs_root_id(root)); - return -EPERM; + ret = -EPERM; + goto out_unlock_mmap; } atomic_inc(&root->nr_swapfiles); spin_unlock(&root->root_item_lock); @@ -10036,6 +10051,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, btrfs_exclop_finish(fs_info); +out_unlock_mmap: + up_write(&BTRFS_I(inode)->i_mmap_lock); if (ret) return ret; From patchwork Wed Dec 11 14:53:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903620 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F02C51AAE01 for ; Wed, 11 Dec 2024 14:53:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928814; cv=none; b=QyvxmfHA1EUJKDutE0T5QVomAhWrdYzImUs9E2qZANpZAoouQ4bKskkNJpFaAesbFvDCqlxGbvfidi0CvrYcILBiyYen1V/zoIun8kswCeSSZFrSfYF5xczHkp5NBQq8pXOLr99Ml2s/xt7s7j/3VHBcqOYnX1j/FlRGmeOX6ng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928814; c=relaxed/simple; bh=FfqGGrIJ8xqJ4u62KUDF387J3v4JKcvwUsLGzjUoBuA=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sotOwvfzf23aBJbOzYpCDRF1FqtEVYh+iJZ17eMRCWPdmdzCMPtYIU4S8RYjN5TzMLldW/vqiEP7Zf2azJ99F+Eayi8+Dx8dcDDGpeFjlgNJIL/lhadVcDHBzOc2oV0hcwT6/kvCvnHgYj+ucNKjIcKcVeo3KpJqQgBebNaTxzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tg4BZm6Y; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="tg4BZm6Y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0CB22C4CED7 for ; Wed, 11 Dec 2024 14:53:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928813; bh=FfqGGrIJ8xqJ4u62KUDF387J3v4JKcvwUsLGzjUoBuA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=tg4BZm6YZjLiNVQyYx8envvRvwbweuJ3wRyNAGr8Qa9eKbr1G/Q/mIZtM5AtsgGb2 i0KZntsdNUMjPEnvJKlVPfqJUlR+MTebmlv4mmCVgHOHcB+3B5qfDuHYiUsfGMG0Jn NcXkZM80tVAsYoFke237E8iue4j17nepFWSf18syHKuUUkrQOO4CIy+VuxQTjIg/2s TOizJPpGzRqt8BscQat2RLF0whaw/jMmaBNWVVe6B2rWsfX2I3YI5iJeDcNw81zEu5 Sd1P4Cp3KfI57GBCUqTwAECbjQaVNE+8h1A20xD1iUtRaWiAk2Gjsr2AhbU0yJDoJK Ua7SAEvhXvDEQ== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 02/11] btrfs: fix swap file activation failure due to extents that used to be shared Date: Wed, 11 Dec 2024 14:53:19 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana When activating a swap file, to determine if an extent is shared we use can_nocow_extent(), which ends up at btrfs_cross_ref_exist(). That helper is meant to be quick because it's used in the NOCOW write path, when flushing delalloc and when doing a direct IO write, however it does return some false positives, meaning it may indicate that an extent is shared even if it's no longer the case. For the write path this is fine, we just do a unnecessary COW operation instead of doing a more rigorous check which would be too heavy (calling btrfs_is_data_extent_shared()). However when activating a swap file, the false positives simply result in a failure, which is confusing for users/applications. One particular case where this happens is when a data extent only has 1 reference but that reference is not inlined in the extent item located in the extent tree - this happens when we create more than 33 references for an extent and then delete those 33 references plus every other non-inline reference except one. The function check_committed_ref() assumes that if the size of an extent item doesn't match the size of struct btrfs_extent_item plus the size of an inline reference (plus an owner reference in case simple quotas are enabled), then the extent is shared - that is not the case however, we can have a single reference but it's not inlined - the reason we do this is to be fast and avoid inspecting non-inline references which may be located in another leaf of the extent tree, slowing down write paths. The following test script reproduces the bug: $ cat test.sh #!/bin/bash DEV=/dev/sdi MNT=/mnt/sdi NUM_CLONES=50 umount $DEV &> /dev/null run_test() { local sync_after_add_reflinks=$1 local sync_after_remove_reflinks=$2 mkfs.btrfs -f $DEV > /dev/null #mkfs.xfs -f $DEV > /dev/null mount $DEV $MNT touch $MNT/foo chmod 0600 $MNT/foo # On btrfs the file must be NOCOW. chattr +C $MNT/foo &> /dev/null xfs_io -s -c "pwrite -b 1M 0 1M" $MNT/foo mkswap $MNT/foo for ((i = 1; i <= $NUM_CLONES; i++)); do touch $MNT/foo_clone_$i chmod 0600 $MNT/foo_clone_$i # On btrfs the file must be NOCOW. chattr +C $MNT/foo_clone_$i &> /dev/null cp --reflink=always $MNT/foo $MNT/foo_clone_$i done if [ $sync_after_add_reflinks -ne 0 ]; then # Flush delayed refs and commit current transaction. sync -f $MNT fi # Remove the original file and all clones except the last. rm -f $MNT/foo for ((i = 1; i < $NUM_CLONES; i++)); do rm -f $MNT/foo_clone_$i done if [ $sync_after_remove_reflinks -ne 0 ]; then # Flush delayed refs and commit current transaction. sync -f $MNT fi # Now use the last clone as a swap file. It should work since # its extent are not shared anymore. swapon $MNT/foo_clone_${NUM_CLONES} swapoff $MNT/foo_clone_${NUM_CLONES} umount $MNT } echo -e "\nTest without sync after creating and removing clones" run_test 0 0 echo -e "\nTest with sync after creating clones" run_test 1 0 echo -e "\nTest with sync after removing clones" run_test 0 1 echo -e "\nTest with sync after creating and removing clones" run_test 1 1 Running the test: $ ./test.sh Test without sync after creating and removing clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0017 sec (556.793 MiB/sec and 556.7929 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=a6b9c29e-5ef4-4689-a8ac-bc199c750f02 swapon: /mnt/sdi/foo_clone_50: swapon failed: Invalid argument swapoff: /mnt/sdi/foo_clone_50: swapoff failed: Invalid argument Test with sync after creating clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0036 sec (271.739 MiB/sec and 271.7391 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=5e9008d6-1f7a-4948-a1b4-3f30aba20a33 swapon: /mnt/sdi/foo_clone_50: swapon failed: Invalid argument swapoff: /mnt/sdi/foo_clone_50: swapoff failed: Invalid argument Test with sync after removing clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0103 sec (96.665 MiB/sec and 96.6651 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=916c2740-fa9f-4385-9f06-29c3f89e4764 Test with sync after creating and removing clones wrote 1048576/1048576 bytes at offset 0 1 MiB, 1 ops; 0.0031 sec (314.268 MiB/sec and 314.2678 ops/sec) Setting up swapspace version 1, size = 1020 KiB (1044480 bytes) no label, UUID=06aab1dd-4d90-49c0-bd9f-3a8db4e2f912 swapon: /mnt/sdi/foo_clone_50: swapon failed: Invalid argument swapoff: /mnt/sdi/foo_clone_50: swapoff failed: Invalid argument Fix this by reworking btrfs_swap_activate() to instead of using extent maps and checking for shared extents with can_nocow_extent(), iterate over the inode's file extent items and use the accurate btrfs_is_data_extent_shared(). Signed-off-by: Filipe Manana --- fs/btrfs/inode.c | 96 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 27 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 926d82fbdbae..7ddb8a01b60f 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -9799,15 +9799,16 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, struct btrfs_fs_info *fs_info = root->fs_info; struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; struct extent_state *cached_state = NULL; - struct extent_map *em = NULL; struct btrfs_chunk_map *map = NULL; struct btrfs_device *device = NULL; struct btrfs_swap_info bsi = { .lowest_ppage = (sector_t)-1ULL, }; + struct btrfs_backref_share_check_ctx *backref_ctx = NULL; + struct btrfs_path *path = NULL; int ret = 0; u64 isize; - u64 start; + u64 prev_extent_end = 0; /* * Acquire the inode's mmap lock to prevent races with memory mapped @@ -9846,6 +9847,13 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, goto out_unlock_mmap; } + path = btrfs_alloc_path(); + backref_ctx = btrfs_alloc_backref_share_check_ctx(); + if (!path || !backref_ctx) { + ret = -ENOMEM; + goto out_unlock_mmap; + } + /* * Balance or device remove/replace/resize can move stuff around from * under us. The exclop protection makes sure they aren't running/won't @@ -9904,24 +9912,39 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); lock_extent(io_tree, 0, isize - 1, &cached_state); - start = 0; - while (start < isize) { - u64 logical_block_start, physical_block_start; + while (prev_extent_end < isize) { + struct btrfs_key key; + struct extent_buffer *leaf; + struct btrfs_file_extent_item *ei; struct btrfs_block_group *bg; - u64 len = isize - start; + u64 logical_block_start; + u64 physical_block_start; + u64 extent_gen; + u64 disk_bytenr; + u64 len; - em = btrfs_get_extent(BTRFS_I(inode), NULL, start, len); - if (IS_ERR(em)) { - ret = PTR_ERR(em); + key.objectid = btrfs_ino(BTRFS_I(inode)); + key.type = BTRFS_EXTENT_DATA_KEY; + key.offset = prev_extent_end; + + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + if (ret < 0) goto out; - } - if (em->disk_bytenr == EXTENT_MAP_HOLE) { + /* + * If key not found it means we have an implicit hole (NO_HOLES + * is enabled). + */ + if (ret > 0) { btrfs_warn(fs_info, "swapfile must not have holes"); ret = -EINVAL; goto out; } - if (em->disk_bytenr == EXTENT_MAP_INLINE) { + + leaf = path->nodes[0]; + ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); + + if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_INLINE) { /* * It's unlikely we'll ever actually find ourselves * here, as a file small enough to fit inline won't be @@ -9933,23 +9956,45 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, ret = -EINVAL; goto out; } - if (extent_map_is_compressed(em)) { + + if (btrfs_file_extent_compression(leaf, ei) != BTRFS_COMPRESS_NONE) { btrfs_warn(fs_info, "swapfile must not be compressed"); ret = -EINVAL; goto out; } - logical_block_start = extent_map_block_start(em) + (start - em->start); - len = min(len, em->len - (start - em->start)); - free_extent_map(em); - em = NULL; + disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); + if (disk_bytenr == 0) { + btrfs_warn(fs_info, "swapfile must not have holes"); + ret = -EINVAL; + goto out; + } + + logical_block_start = disk_bytenr + btrfs_file_extent_offset(leaf, ei); + extent_gen = btrfs_file_extent_generation(leaf, ei); + prev_extent_end = btrfs_file_extent_end(path); + + if (prev_extent_end > isize) + len = isize - key.offset; + else + len = btrfs_file_extent_num_bytes(leaf, ei); - ret = can_nocow_extent(inode, start, &len, NULL, false, true); + backref_ctx->curr_leaf_bytenr = leaf->start; + + /* + * Don't need the path anymore, release to avoid deadlocks when + * calling btrfs_is_data_extent_shared() because when joining a + * transaction it can block waiting for the current one's commit + * which in turn may be trying to lock the same leaf to flush + * delayed items for example. + */ + btrfs_release_path(path); + + ret = btrfs_is_data_extent_shared(BTRFS_I(inode), disk_bytenr, + extent_gen, backref_ctx); if (ret < 0) { goto out; - } else if (ret) { - ret = 0; - } else { + } else if (ret > 0) { btrfs_warn(fs_info, "swapfile must not be copy-on-write"); ret = -EINVAL; @@ -9984,7 +10029,6 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, physical_block_start = (map->stripes[0].physical + (logical_block_start - map->start)); - len = min(len, map->chunk_len - (logical_block_start - map->start)); btrfs_free_chunk_map(map); map = NULL; @@ -10025,20 +10069,16 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, if (ret) goto out; } - bsi.start = start; + bsi.start = key.offset; bsi.block_start = physical_block_start; bsi.block_len = len; } - - start += len; } if (bsi.block_len) ret = btrfs_add_swap_extent(sis, &bsi); out: - if (!IS_ERR_OR_NULL(em)) - free_extent_map(em); if (!IS_ERR_OR_NULL(map)) btrfs_free_chunk_map(map); @@ -10053,6 +10093,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, out_unlock_mmap: up_write(&BTRFS_I(inode)->i_mmap_lock); + btrfs_free_backref_share_ctx(backref_ctx); + btrfs_free_path(path); if (ret) return ret; From patchwork Wed Dec 11 14:53:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903621 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B83AD1ABEA1 for ; Wed, 11 Dec 2024 14:53:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928814; cv=none; b=O8aFCAUh5kIBLioA6hpwwlv4py+g+iy8Al6vuRf9z4qH+8J2lMiu3aHvjAqkdJff7kG7mi5Ncz3BUcd8ec5+NzEvrbmHMz727eoDcHN8fmSSxINUX4at1TQYzXUXI8QllvUg9aEN4miqENa0Wia9yUzItYzpENEnC+beZ2/p4fA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928814; c=relaxed/simple; bh=/Elnbw3J8+WEUwhw4pS0ScYsHxiN1Bhr3UJd26ScI18=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PjR45qr8w8faucxThb+zuBXNroUNkVF6TuwfzVNhXRuGVV4RHINU9UElev89pIs4OtsrU1rbneiW94cgLS8wzZD236aVinBgc64s82d02pZ82/WH3Fb2b4kMsqFrzY0SZRX365pyOu2x3ML9+zYVMysIwgRbKP87HACPEJR8PRk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=B9OCFdIA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="B9OCFdIA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13821C4CED4 for ; Wed, 11 Dec 2024 14:53:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928814; bh=/Elnbw3J8+WEUwhw4pS0ScYsHxiN1Bhr3UJd26ScI18=; h=From:To:Subject:Date:In-Reply-To:References:From; b=B9OCFdIA6Bg5w7JTtZOJyfV4YuhaVC4e6vwziXuYlamCJu1pGWibCGHPYA21Hsj7+ XWEGpt518XM2pLxGtJV4rFrpNDUO0mrtv+2u6PxBqfJ3c7agRUd9JxJA704BAnp1g/ nLREcSO2N8HdJTa3/+prYIF2EAlyQgzAmfh9jR+XMbiVvVfdwBNqom8EQ4qjKK15SG TXMJZyxFFI6D4QEbgYDNUN+QYp24waczxUr+Aq7nke6Gg+T+Rc9cxcZ3QInD7I3amS 9iFy81tkC3+tzdeEJG+WtvXo8deagGkeTD3+BD0owqBkytHVBNzzGKVLKiNv3pr0Mv pc6o80NyAKSWQ== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 03/11] btrfs: allow swap activation to be interruptible Date: Wed, 11 Dec 2024 14:53:20 +0000 Message-Id: <05685edc7747bda9a359a04cd66b07e11e889f91.1733832118.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana During swap activation we iterate over the extents of a file, then do several checks for each extent, some of which may take some significant time such as checking if an extent is shared. Since a file can have many thousands of extents, this can be a very slow operation and it's currently not interruptible. I had a bug during development of a previous patch that resulted in an infinite loop when iterating the extents, so a core was busy looping and I couldn't cancel the operation, which is very annoying and requires a reboot. So make the loop interruptible by checking for fatal signals at the end of each iteration and stopping immediately if there is one. Signed-off-by: Filipe Manana --- fs/btrfs/inode.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 7ddb8a01b60f..5edc151c640d 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -10073,6 +10073,11 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, bsi.block_start = physical_block_start; bsi.block_len = len; } + + if (fatal_signal_pending(current)) { + ret = -EINTR; + goto out; + } } if (bsi.block_len) From patchwork Wed Dec 11 14:53:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903622 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50B581B6D0D for ; Wed, 11 Dec 2024 14:53:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928816; cv=none; b=MdyW/MrG78w5BwVGkP6GJx/J+OiUU3SIW9QxFZ0PTIN0VzrVg9oPIhekBwpG55GqMAk5H55UKE8Wnh5o9xuKYMkMg+yeRxMJaZfwbcp2Kk96urgc6ae8alDnrGMPcc0AvoocbwIdi2SFjhbVcmH6fuTuF+Oh7sR05RGiWJh6RhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928816; c=relaxed/simple; bh=gcJlCl3eYEkScuFHlQMq13/7I4cWeKB5TpS20bMssO8=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QOPZV+GpCNasojYI5ntZrefk7DKtrQ2XgrE/y6KMft2vaAxMZ0AYKAeF8fia4khjdbZ4VyBADEOPuA9DclZfvyYjqBAh0TxZ02oLyVFHi6vdWnhrGWaKDT6Z8OrlPSujDN27c4XvhfmLKcU+p1Y/fr+ZRwJb5GNX7ezPXdPem7c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GLULYCcX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GLULYCcX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19D26C4CED2 for ; Wed, 11 Dec 2024 14:53:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928815; bh=gcJlCl3eYEkScuFHlQMq13/7I4cWeKB5TpS20bMssO8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=GLULYCcXd7+vcYriF9lmQtWPY+sbNFDWXeC3BxDGlfblmiBAQUbUJH9PKGBQ4M9wA 4p4Ywx3tyDgF7pVAddkPJ5N6nx0WQtuoAEkztXANHDI7PQ9rZgl1peixq83p+7sYiI vMkVssV1l3MXU6au37nhB3X5E7UnHgrxCQokG/FMHgoNNl9AglhiEDeovqXCjGRtxv 44QQMSpJj1MKAYzp4g70pPQvqacTbpQ+FyBo9NbM6bx90hDa+nSF+I6iLiOHLtD9kh q0MW+e37z+if7k1aPPWZ12iOhdsXECPMQekacHRA4frysYY6VLmaNp6cX8QU+QImdJ bvmCFC8kelTPQ== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 04/11] btrfs: avoid monopolizing a core when activating a swap file Date: Wed, 11 Dec 2024 14:53:21 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana During swap activation we iterate over the extents of a file and we can have many thounsands of them, so we can end up in a busy loop monopolizing a core. Avoid this by doing a voluntary reschedule after processing each extent. Signed-off-by: Filipe Manana --- fs/btrfs/inode.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 5edc151c640d..283199d11642 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -10078,6 +10078,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, ret = -EINTR; goto out; } + + cond_resched(); } if (bsi.block_len) From patchwork Wed Dec 11 14:53:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903623 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 290BC19F12A for ; Wed, 11 Dec 2024 14:53:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928817; cv=none; b=KXaaTZZ0Hvko9XjQ0GrD0vg+e2o+4Pu4DqswktHITbN/BnoVePBJUjbYF7joAl+QjHshXaCOjwQ2uh9Uea5vZqvWgAzLSxHL4mCp7Jla+zbu3fcP6ObWjDVYoqMoOP1W5e3+ZsbNNW83OMg+AI80wuWn4tpZLcxlP6NBoZpE8fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928817; c=relaxed/simple; bh=6VwCPiBsWimEYCZeJJjWMXSayHBnPW9jPdrAz7rOh6c=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rUX95gao9EkTxfyTmigCI0jxMn5E+syWw+W/uGp3lB3aj6aYQDzesgyhzBv/ApHAToMXCyY7r+wG6lvP92TbU9uc6JkpDG3KK7hZJST/U5+UsQ94o520UB4YHf442yakV9KHct4AFSakH7aVnkcbW9ceEsC1SPijHcxEjgk2eDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ns6+8EQ5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ns6+8EQ5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29FEFC4CED4 for ; Wed, 11 Dec 2024 14:53:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928816; bh=6VwCPiBsWimEYCZeJJjWMXSayHBnPW9jPdrAz7rOh6c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Ns6+8EQ5VZeaSXtMcYqN5qcrCmJ7u0orDa+dhimtJO2/X215c+rUs2HoAMq+UR6PT iPZd/8SYJgOjiwBS+5DZkaBpI/8u47Yz5fItC9J8RrpiwFEioC5EwI1XlqO9JSvrTP vSIXTgBx+8jGx9uyB8Jubo6wdOjdPuS0u5bYr8vmCMQdXhNvQOCFuhr2OHQ7JZddlb vmW+J1Wr2yicxe8d3mv0QekCy4+IFuli3QXD2+TXf0PRDAmippeyzsenTj+aO6LmW2 iMecbN2yYYKO2Otrmv5vZ2dGltevVcJSizgzy1cZG3IjIB+zXnd0xJdA7iXYtYXJ5z U6v+O9oFXSF0g== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 05/11] btrfs: remove no longer needed strict argument from can_nocow_extent() Date: Wed, 11 Dec 2024 14:53:22 +0000 Message-Id: <2aa371a0b76fa92f94b7ec408788381530a3bf8e.1733832118.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana All callers of can_nocow_extent() now pass a value of false for its 'strict' argument, making it redundant. So remove the argument from can_nocow_extent() as well as can_nocow_file_extent(), btrfs_cross_ref_exist() and check_committed_ref(), because this argument was used just to influence the behavior of check_committed_ref(). Also remove the 'strict' field from struct can_nocow_file_extent_args, which is now always false as well, as its value is taken from the argument to can_nocow_extent(). Signed-off-by: Filipe Manana --- fs/btrfs/btrfs_inode.h | 2 +- fs/btrfs/direct-io.c | 3 +-- fs/btrfs/extent-tree.c | 15 ++++++--------- fs/btrfs/extent-tree.h | 2 +- fs/btrfs/file.c | 2 +- fs/btrfs/inode.c | 11 +++-------- 6 files changed, 13 insertions(+), 22 deletions(-) diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h index aa1f55cd81b7..b2fa33911c28 100644 --- a/fs/btrfs/btrfs_inode.h +++ b/fs/btrfs/btrfs_inode.h @@ -526,7 +526,7 @@ bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev, u32 bio_offset, struct bio_vec *bv); noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, struct btrfs_file_extent *file_extent, - bool nowait, bool strict); + bool nowait); void btrfs_del_delalloc_inode(struct btrfs_inode *inode); struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry); diff --git a/fs/btrfs/direct-io.c b/fs/btrfs/direct-io.c index a7c3e221378d..8567af46e16f 100644 --- a/fs/btrfs/direct-io.c +++ b/fs/btrfs/direct-io.c @@ -248,8 +248,7 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map, len = min(len, em->len - (start - em->start)); block_start = extent_map_block_start(em) + (start - em->start); - if (can_nocow_extent(inode, start, &len, - &file_extent, false, false) == 1) { + if (can_nocow_extent(inode, start, &len, &file_extent, false) == 1) { bg = btrfs_inc_nocow_writers(fs_info, block_start); if (bg) can_nocow = true; diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 2f9126528a01..46a3a4a4536b 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2296,8 +2296,7 @@ static noinline int check_delayed_ref(struct btrfs_root *root, static noinline int check_committed_ref(struct btrfs_root *root, struct btrfs_path *path, - u64 objectid, u64 offset, u64 bytenr, - bool strict) + u64 objectid, u64 offset, u64 bytenr) { struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); @@ -2361,11 +2360,10 @@ static noinline int check_committed_ref(struct btrfs_root *root, /* * If extent created before last snapshot => it's shared unless the - * snapshot has been deleted. Use the heuristic if strict is false. + * snapshot has been deleted. */ - if (!strict && - (btrfs_extent_generation(leaf, ei) <= - btrfs_root_last_snapshot(&root->root_item))) + if (btrfs_extent_generation(leaf, ei) <= + btrfs_root_last_snapshot(&root->root_item)) goto out; /* If this extent has SHARED_DATA_REF then it's shared */ @@ -2387,13 +2385,12 @@ static noinline int check_committed_ref(struct btrfs_root *root, } int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, - u64 bytenr, bool strict, struct btrfs_path *path) + u64 bytenr, struct btrfs_path *path) { int ret; do { - ret = check_committed_ref(root, path, objectid, - offset, bytenr, strict); + ret = check_committed_ref(root, path, objectid, offset, bytenr); if (ret && ret != -ENOENT) goto out; diff --git a/fs/btrfs/extent-tree.h b/fs/btrfs/extent-tree.h index 2ad51130c037..ee62035c4a71 100644 --- a/fs/btrfs/extent-tree.h +++ b/fs/btrfs/extent-tree.h @@ -117,7 +117,7 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, const struct extent_buffer *eb); int btrfs_exclude_logged_extents(struct extent_buffer *eb); int btrfs_cross_ref_exist(struct btrfs_root *root, - u64 objectid, u64 offset, u64 bytenr, bool strict, + u64 objectid, u64 offset, u64 bytenr, struct btrfs_path *path); struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index c61f210259d8..a2fb100fd017 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1069,7 +1069,7 @@ int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos, &cached_state); } ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes, - NULL, nowait, false); + NULL, nowait); if (ret <= 0) btrfs_drew_write_unlock(&root->snapshot_lock); else diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 283199d11642..0965a29cf4f7 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1837,7 +1837,6 @@ struct can_nocow_file_extent_args { /* End file offset (inclusive) of the range we want to NOCOW. */ u64 end; bool writeback_path; - bool strict; /* * Free the path passed to can_nocow_file_extent() once it's not needed * anymore. @@ -1892,8 +1891,7 @@ static int can_nocow_file_extent(struct btrfs_path *path, * for its subvolume was created, then this implies the extent is shared, * hence we must COW. */ - if (!args->strict && - btrfs_file_extent_generation(leaf, fi) <= + if (btrfs_file_extent_generation(leaf, fi) <= btrfs_root_last_snapshot(&root->root_item)) goto out; @@ -1924,7 +1922,7 @@ static int can_nocow_file_extent(struct btrfs_path *path, ret = btrfs_cross_ref_exist(root, btrfs_ino(inode), key->offset - args->file_extent.offset, - args->file_extent.disk_bytenr, args->strict, path); + args->file_extent.disk_bytenr, path); WARN_ON_ONCE(ret > 0 && is_freespace_inode); if (ret != 0) goto out; @@ -7011,8 +7009,6 @@ static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) * @orig_start: (optional) Return the original file offset of the file extent * @orig_len: (optional) Return the original on-disk length of the file extent * @ram_bytes: (optional) Return the ram_bytes of the file extent - * @strict: if true, omit optimizations that might force us into unnecessary - * cow. e.g., don't trust generation number. * * Return: * >0 and update @len if we can do nocow write @@ -7024,7 +7020,7 @@ static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) */ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, struct btrfs_file_extent *file_extent, - bool nowait, bool strict) + bool nowait) { struct btrfs_fs_info *fs_info = inode_to_fs_info(inode); struct can_nocow_file_extent_args nocow_args = { 0 }; @@ -7077,7 +7073,6 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, nocow_args.start = offset; nocow_args.end = offset + *len - 1; - nocow_args.strict = strict; nocow_args.free_path = true; ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args); From patchwork Wed Dec 11 14:53:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903624 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3AAF91D3576 for ; Wed, 11 Dec 2024 14:53:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928818; cv=none; b=PN4LqFs5iIraCFhzGZFVVo13/c+eIdfE2n8wHwA/BbgnCs1kRNh3nR7BPwimDUVCAOUJEiGF7NEZ1tuNbKj3AZxaMrEf+E9BelgNWaoEzHWSVJOpRYijnoOX2r22lej0svziVBeLREN9QflyNK9U6/cf4YVXVgiQdH4ueulVqxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928818; c=relaxed/simple; bh=wlztWU2/5Sh2XLcOIFFf2g8YfNNaC4Gx+8q51GvVDo0=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c4v3CvS/wMyRgnEaGXCvFFZK53tZHCjgJiDP+SzLqdnltVOQ0SPFKBsG8569O9JZTzsAiVUuESTyO9mAJ1InjUq3atJVz1fx69l03l3LSosbIU1ws0Juud2dsTkJjn21ai8omvdBfKo9X5XzXJpbSJJwzn1cUi7h3FL5x2PtHSM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fyhThOgf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fyhThOgf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30F5EC4CEDD for ; Wed, 11 Dec 2024 14:53:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928817; bh=wlztWU2/5Sh2XLcOIFFf2g8YfNNaC4Gx+8q51GvVDo0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fyhThOgfWYSDTfslod/bMAGS0zjYgJI5k4tiqSruzR3Ub2qtU59MRgYeIo+56vBQx ptZ1on9Znh+yGAkSSJ4aft+isCgcAKR4pWblBLst75vjzN1QzWYVOgChhEKGMsi9Yt AizzSSpf5iFYifIw6XcGknfLdKVATcrrCNBvpC4vRjZVYO8L4/ZaNGDWLlW4gaM4yg YI7cf9f9UquRwMUNtj2Mn0+jMoE5p/UR8ypcJJn9qUkqTfKjJ/Mtju0oA+yzN5aMT8 FwwpTzhlX4RVWj8IEdG6Vn+b8mFVx9AzETPO1VffhJOaLF5M/GD/gNjgK2xr9o/vss fHsW3jZXuLPtg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 06/11] btrfs: remove the snapshot check from check_committed_ref() Date: Wed, 11 Dec 2024 14:53:23 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana At check_committed_ref() we have this check to see if the data extent was created in a generation lower than or equals to the generation where the last snapshot for the root was created, and if so we return immediately with 1, since it's very likely the extent is shared, referenced by other root. The only call chain for check_committed_ref() is the following: can_nocow_file_extent() btrfs_cross_ref_exist() check_committed_ref() And we already do that snapshot check at can_nocow_file_extent(), before we call btrfs_cross_ref_exist(). This makes the check done at check_committed_ref() redundant, so remove it. Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 46a3a4a4536b..e81f4615ccdf 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2358,14 +2358,6 @@ static noinline int check_committed_ref(struct btrfs_root *root, if (item_size != expected_size) goto out; - /* - * If extent created before last snapshot => it's shared unless the - * snapshot has been deleted. - */ - if (btrfs_extent_generation(leaf, ei) <= - btrfs_root_last_snapshot(&root->root_item)) - goto out; - /* If this extent has SHARED_DATA_REF then it's shared */ type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); if (type != BTRFS_EXTENT_DATA_REF_KEY) From patchwork Wed Dec 11 14:53:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903625 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4102D1D618A for ; Wed, 11 Dec 2024 14:53:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928819; cv=none; b=uEJQSSs945Gp9o2nWlcZcJE4QKTwmYQAprv1GGSa4AxgyjYfj9BQpo4Q3gQuj6F+z9bOODWixPDS5JkZ+Z2L7mwaKrbxOAhoH4esL8s419KWJsnDE5p0Lq1UoboeZvjWWCLgx6r3e9aJD8cCPOaA3ADiSYm8QEWTZUVsLSXpILc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928819; c=relaxed/simple; bh=CYBRkDsmtANHN4WgojWQVkSyw2QTe/htm/IKqRNm99I=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qjQMOc2LCYyi4cc22CP5L5QM0ybm+9QK1a1pp53/rqwKidol29ttXWUKJxCndwolcCFKmwoW0Ieb3cERDhvIpALboc27MY2mBLItCTFgQ7a27FSsjPKSd7wGALDwWkxassh4Df3l+MJzXaLUWOKcPZ4zr+y9OgunUiuDkHEMRFo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mb/g5Nrz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Mb/g5Nrz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 384D6C4CED2 for ; Wed, 11 Dec 2024 14:53:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928818; bh=CYBRkDsmtANHN4WgojWQVkSyw2QTe/htm/IKqRNm99I=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Mb/g5Nrzr89QiysN7xgYQpT6uIMXsNVbo6FpYUfjPklYEqwxYIMkOaoaXymfz1KRs MU4MLqHCS4Wvj22h8pzO+3nSa5CE10iqhGStO7UHYSC/PH/LVkK5X62h+h7X+2hYe8 K8t3sSUYxZD8nkJbDw81WG2aOAZ8X/EC8HnfYjrp0sTRXPhLEJWO363Fm0wQFDDsXs bzcuWCS6+xBZvhRvNge5/AebQJg5SREHbTC6CEgxg4OiN2Ze170h191VlewyFI6IU6 mAoMcL+VfdEpQM33JiIxBlPmpJxMhMa08VjKLdfjwkgetezNbEOZojA3e3vRdRCAut UBDKfsOEMYcZA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 07/11] btrfs: avoid redundant call to get inline ref type at check_committed_ref() Date: Wed, 11 Dec 2024 14:53:24 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana At check_committed_ref() we are calling btrfs_get_extent_inline_ref_type() twice, once before we check if have an inline extent owner ref (for simple qgroups) and then once again sometime after that check. This second call is redundant when we have simple quotas disabled or we found an inline ref that is not of the owner ref type. So avoid this second call unless we have simple quotas enabled and found an owner ref, saving a function call that does inline ref validation again. Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index e81f4615ccdf..00e137c48a9b 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2352,6 +2352,7 @@ static noinline int check_committed_ref(struct btrfs_root *root, if (btrfs_fs_incompat(fs_info, SIMPLE_QUOTA) && type == BTRFS_EXTENT_OWNER_REF_KEY) { expected_size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY); iref = (struct btrfs_extent_inline_ref *)(iref + 1); + type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); } /* If extent item has more than 1 inline ref then it's shared */ @@ -2359,7 +2360,6 @@ static noinline int check_committed_ref(struct btrfs_root *root, goto out; /* If this extent has SHARED_DATA_REF then it's shared */ - type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); if (type != BTRFS_EXTENT_DATA_REF_KEY) goto out; From patchwork Wed Dec 11 14:53:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903626 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35E7F1D63DE for ; Wed, 11 Dec 2024 14:53:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928820; cv=none; b=F1EYSL4OOy5AYLOxtVBvTUDVYJuufxNveS3JzgEX/7dlTAg3/HY3qx805lHFJI9xkQAu0p54KD0O26vIOyO8LpE5ldvhCuiVJeJBf5AjKGEqdVgyyZYAK4UfW33ahMMuPtXXAQ6KSDq9eUfDCUjdEyj9DOgl3zTQglXglptEVDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928820; c=relaxed/simple; bh=Mnhn1ycT3BR2wHhbiKSpCZU+4nq13CqR7isYDP4qCbg=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hJOyk8b95sOzna1x5PCHvdnQR9QzWamlJ8F4Yv95xowhEytCy4OC61NoWwD14BWJb8oI5XFvbb7WiuqYJ1raOxQEAWjwQB2UtYOcAyFCixA23O9txwguFFcg/EdQskQNWD8arwQMuUiS/e06UqEQ/t/AJORRgwKGf6nGx1yG3rs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nkSGIalh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nkSGIalh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F10DC4CEDD for ; Wed, 11 Dec 2024 14:53:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928819; bh=Mnhn1ycT3BR2wHhbiKSpCZU+4nq13CqR7isYDP4qCbg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=nkSGIalhSXYDSojdqq7RrwWD/oG9ewyQVkdVpDSZJVTGPZodQmWcIO2ciNU662VK5 1eCqChblqd0z455Bds/5FvpaNUM6Msq7cKAtK83oFGJ31TNgncE6Ph/6dwHJCDYpoZ ifpQfw0+Bzw+ZmFGh4xUrWb2d/u7UiPT9rw+xiiitkPghwadSzzC8nhMe8Q0bng6Vi 0JtbX+HPlaeTCvXH+NNvilZS6/0h2V0Vz80JL5mNU3DD0K/TfX9NO0sxflb/ajoc5l OZd2iWdubJyM9PeOBWlGVCZIX68lDQN8w6oJcsMeLYQDEtz8ypvd3TatNIHqguXiWW ytfS/r2LqTKeA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 08/11] btrfs: simplify return logic at check_committed_ref() Date: Wed, 11 Dec 2024 14:53:25 +0000 Message-Id: <81b8ad0b51003fe2d99e8ece25a7b5ba1dc1a644.1733832118.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana Instead of setting the value to return in a local variable 'ret' and then jumping into a label named 'out' that does nothing but return that value, simplify everything by getting rid of the label and directly returning a value. Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 00e137c48a9b..51c49b2f4991 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2316,35 +2316,32 @@ static noinline int check_committed_ref(struct btrfs_root *root, ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); if (ret < 0) - goto out; + return ret; if (ret == 0) { /* * Key with offset -1 found, there would have to exist an extent * item with such offset, but this is out of the valid range. */ - ret = -EUCLEAN; - goto out; + return -EUCLEAN; } - ret = -ENOENT; if (path->slots[0] == 0) - goto out; + return -ENOENT; path->slots[0]--; leaf = path->nodes[0]; btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) - goto out; + return -ENOENT; - ret = 1; item_size = btrfs_item_size(leaf, path->slots[0]); ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); expected_size = sizeof(*ei) + btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY); /* No inline refs; we need to bail before checking for owner ref. */ if (item_size == sizeof(*ei)) - goto out; + return 1; /* Check for an owner ref; skip over it to the real inline refs. */ iref = (struct btrfs_extent_inline_ref *)(ei + 1); @@ -2357,11 +2354,11 @@ static noinline int check_committed_ref(struct btrfs_root *root, /* If extent item has more than 1 inline ref then it's shared */ if (item_size != expected_size) - goto out; + return 1; /* If this extent has SHARED_DATA_REF then it's shared */ if (type != BTRFS_EXTENT_DATA_REF_KEY) - goto out; + return 1; ref = (struct btrfs_extent_data_ref *)(&iref->offset); if (btrfs_extent_refs(leaf, ei) != @@ -2369,11 +2366,9 @@ static noinline int check_committed_ref(struct btrfs_root *root, btrfs_extent_data_ref_root(leaf, ref) != btrfs_root_id(root) || btrfs_extent_data_ref_objectid(leaf, ref) != objectid || btrfs_extent_data_ref_offset(leaf, ref) != offset) - goto out; + return 1; - ret = 0; -out: - return ret; + return 0; } int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, From patchwork Wed Dec 11 14:53:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903627 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 020D51D6DBB for ; Wed, 11 Dec 2024 14:53:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928821; cv=none; b=rgEDmIcjioafBPvOjapicsWPP5K8YprgB3JOVoM3lFsBSOPFKLtXj8sFNRWa2IScGxqFNdRNh9v+GvLzNp7LPvt6mLoMZ6GjKW5ObRbvdmxZ80BQdmL7Gs/mPuXKlZyMBR+DLPpxnx8dHMeeqkEcZdtMx0Qi/i22RBjcKTtqUCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928821; c=relaxed/simple; bh=Vc2HYuXc5jCwXidfHBZdaXlbz2uKzzjm7GSQzu3eYA4=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S3S3i9ZHOd56gTyC6yNB33fu7S1J9p25PlijR/ON/efCaP4oCrZP3neDC/l+V9CHcLFiV3fXBCJCVQyjZCgsMtGByZw3s2SAgrlq+LU1z59NdkpuvZ/In73D7MjXP7vSPxf2cFn1Oj2XdWr2t6/v5zo3+9ZQ+HoYmfFWLwSnHCM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZdqnIQ0k; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZdqnIQ0k" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45B69C4CED4 for ; Wed, 11 Dec 2024 14:53:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928820; bh=Vc2HYuXc5jCwXidfHBZdaXlbz2uKzzjm7GSQzu3eYA4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ZdqnIQ0kh9Cb9R5k6FDMfmOEvnQxa3TgFLYVVInuN8HYCgVIVt49qcf/YBKFn8SFG f1O+kOOI5sbBnkGgeSSlU4y5joaEiegYWrzofohEm6J6pyWMxlyP+skSP/kJnVTlBs wHgOk05+YYiMg2p4z9/tKrFuwZDuMJAMJaRLR02Yogellwej+6p2y3kPmFO0esHd4x pNWApyhrnyAk1yEFyYbRky4w/BukfWwtRo13CClVBbVZ2n21RmgWpGnOQ/1qorENoh Vq6iofdh4mN6xgupt/M/8JAcUme5M2fzifnJMd3HMWVDeNdSYfuxwL78p3OWN+4rke 1dv9YAf/akNXA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 09/11] btrfs: simplify arguments for btrfs_cross_ref_exist() Date: Wed, 11 Dec 2024 14:53:26 +0000 Message-Id: <9e374da66e73917c33b27bff49ba1627c0fda3e0.1733832118.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana Instead of passing a root and an objectid which matches an inode number, pass the inode instead, since the root is always the root associated to the inode and the objectid is the number of that inode. Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 22 ++++++++++++---------- fs/btrfs/extent-tree.h | 3 +-- fs/btrfs/inode.c | 3 +-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 51c49b2f4991..af3893ad784b 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2206,10 +2206,11 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, return ret; } -static noinline int check_delayed_ref(struct btrfs_root *root, +static noinline int check_delayed_ref(struct btrfs_inode *inode, struct btrfs_path *path, - u64 objectid, u64 offset, u64 bytenr) + u64 offset, u64 bytenr) { + struct btrfs_root *root = inode->root; struct btrfs_delayed_ref_head *head; struct btrfs_delayed_ref_node *ref; struct btrfs_delayed_ref_root *delayed_refs; @@ -2283,7 +2284,7 @@ static noinline int check_delayed_ref(struct btrfs_root *root, * then we have a cross reference. */ if (ref->ref_root != btrfs_root_id(root) || - ref_owner != objectid || ref_offset != offset) { + ref_owner != btrfs_ino(inode) || ref_offset != offset) { ret = 1; break; } @@ -2294,10 +2295,11 @@ static noinline int check_delayed_ref(struct btrfs_root *root, return ret; } -static noinline int check_committed_ref(struct btrfs_root *root, +static noinline int check_committed_ref(struct btrfs_inode *inode, struct btrfs_path *path, - u64 objectid, u64 offset, u64 bytenr) + u64 offset, u64 bytenr) { + struct btrfs_root *root = inode->root; struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr); struct extent_buffer *leaf; @@ -2364,29 +2366,29 @@ static noinline int check_committed_ref(struct btrfs_root *root, if (btrfs_extent_refs(leaf, ei) != btrfs_extent_data_ref_count(leaf, ref) || btrfs_extent_data_ref_root(leaf, ref) != btrfs_root_id(root) || - btrfs_extent_data_ref_objectid(leaf, ref) != objectid || + btrfs_extent_data_ref_objectid(leaf, ref) != btrfs_ino(inode) || btrfs_extent_data_ref_offset(leaf, ref) != offset) return 1; return 0; } -int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, +int btrfs_cross_ref_exist(struct btrfs_inode *inode, u64 offset, u64 bytenr, struct btrfs_path *path) { int ret; do { - ret = check_committed_ref(root, path, objectid, offset, bytenr); + ret = check_committed_ref(inode, path, offset, bytenr); if (ret && ret != -ENOENT) goto out; - ret = check_delayed_ref(root, path, objectid, offset, bytenr); + ret = check_delayed_ref(inode, path, offset, bytenr); } while (ret == -EAGAIN && !path->nowait); out: btrfs_release_path(path); - if (btrfs_is_data_reloc_root(root)) + if (btrfs_is_data_reloc_root(inode->root)) WARN_ON(ret > 0); return ret; } diff --git a/fs/btrfs/extent-tree.h b/fs/btrfs/extent-tree.h index ee62035c4a71..46b8e19022df 100644 --- a/fs/btrfs/extent-tree.h +++ b/fs/btrfs/extent-tree.h @@ -116,8 +116,7 @@ int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num, int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, const struct extent_buffer *eb); int btrfs_exclude_logged_extents(struct extent_buffer *eb); -int btrfs_cross_ref_exist(struct btrfs_root *root, - u64 objectid, u64 offset, u64 bytenr, +int btrfs_cross_ref_exist(struct btrfs_inode *inode, u64 offset, u64 bytenr, struct btrfs_path *path); struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 0965a29cf4f7..8a173a24ac05 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1920,8 +1920,7 @@ static int can_nocow_file_extent(struct btrfs_path *path, */ btrfs_release_path(path); - ret = btrfs_cross_ref_exist(root, btrfs_ino(inode), - key->offset - args->file_extent.offset, + ret = btrfs_cross_ref_exist(inode, key->offset - args->file_extent.offset, args->file_extent.disk_bytenr, path); WARN_ON_ONCE(ret > 0 && is_freespace_inode); if (ret != 0) From patchwork Wed Dec 11 14:53:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903628 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52A221D90DF for ; Wed, 11 Dec 2024 14:53:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928822; cv=none; b=fqvZMNpZykK8fvigKj4z6P+sx87FzjG7KfNTQClWKYbloILZpFI9XGG2QaUsdeIAOwWRv0iqz9v3b5JKFlvoyPFi6ARjvwimDUei6BkLbsToF1M0W+yZ+nENCfxnGvMl4hs5dtSGmJBw4v1RTPMPqshOys5VRLi9oBa7ahrQAz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928822; c=relaxed/simple; bh=ycC0dKG9jNfYd/lP/T7AVF97cMRAFo92hhkzk1tdHa8=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CTM123vimpeEAu+LQmmgRxsbs2NNJF0IAE/EFLR2rHTU8ZcunspyAupl9fvQaNx3PVpfU6lkqbxDyhDd2n9Ue/kJ2SjhvYCcdFIHBqohlcFn3N7KEZqpEbns2SPJG/6J47rglZCEjVKAVQrCc9hNSgwvWBQ8hS7P+3r3ZbRMoQE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IApfP/Zg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IApfP/Zg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C301C4CED4 for ; Wed, 11 Dec 2024 14:53:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928821; bh=ycC0dKG9jNfYd/lP/T7AVF97cMRAFo92hhkzk1tdHa8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=IApfP/ZgSs6513fRvKaOL55KyYGKB4uRcSLzFpPK/VI8uzfhc02bjobeEVQ2SN8pc nPtomaGDPvNuOhFPVTVPswIdNGi6r6lB5GNXjR5VVMrJ5KHdSPtxbzSjAJDLfYYg6Z +ZsMiPBI1CApzpWTZzzxA8zwdyaw0gScr6Wv1/kY0d0264nNXtvTV5AP8hVNDPem99 tbZbjOBugpQr1NgQY+5ZPR5TDzOkOahN43hmklaphvK0BtoPmreqGbnEfoqzD+t0+7 ix2c1kZtgSGMOdKP7XoafdQ0+mxF7knqCuUMzl4JwnU0sVnll3p60beBURjlN+o9Cy aVtJbx0YyB9IA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 10/11] btrfs: add function comment for check_committed_ref() Date: Wed, 11 Dec 2024 14:53:27 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana There are some not immediately obvious details about the operation of check_committed_ref(), namely that when it returns 0 it must return with the path having a locked leaf from the extent tree that contains the extent's extent item, so that we can later check for delayed refs when calling check_delayed_ref() in a way that doesn't race with a task running delayed references. For similar reasons, it must also return with a locked leaf when the extent item is not found, and that leaf is where the extent item should be located, because we may have delayed references that are going to create the extent item. Also document that the function can return false positives in order to not be too slow, and that the most important is to not return false negatives. So add a function comment to check_committed_ref(). Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index af3893ad784b..3dea8ce87bf7 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2295,6 +2295,48 @@ static noinline int check_delayed_ref(struct btrfs_inode *inode, return ret; } +/* + * Check if there are references for a data extent other than the one belonging + * to the given inode and offset. + * + * @inode: The only inode we expect to find associated with the data extent. + * @path: A path to use for searching the extent tree. + * @offset: The only offset we expect to find associated with the data + * extent. + * @bytenr: The logical address of the data extent. + * + * When the extent does not have any other references other than the one we + * expect to find, we always return a value of 0 with the path having a locked + * leaf that contains the extent's extent item - this is necessary to ensure + * we don't race with a task running delayed references, and our caller must + * have such a path when calling check_delayed_ref() - it must lock a delayed + * ref head while holding the leaf locked. In case the extent item is not found + * in the extent tree, we return -ENOENT with the path having the leaf (locked) + * where the extent item should be, in order to prevent races with another task + * running delayed references, so that we don't miss any reference when calling + * check_delayed_ref(). + * + * Note: this may return false positives, and this is because we want to be + * quick here as we're called in write paths (when flushing delalloc and + * in the direct IO write path). For example we can have an extent with + * a single reference but that reference is not inlined, or we may have + * many references in the extent tree but we also have delayed references + * that cancel all the reference except the one for our inode and offset, + * but it would be expensive to do such checks and complex due to all + * locking to avoid races between the checks and flushing delayed refs, + * plus non-inline reference may be located on leaves other than the one + * that contains the extent item in the extent tree. The import thing here + * is to not return false negatives and that the false positives are not + * very common. + * + * Returns: 0 if there are no cross references and with the path having a locked + * leaf from the extent tree that contains the extent's extent item. + * + * 1 if there are cross references or if there may be because @strict + * is false and the root was snapshoted after the extent was created. + * + * < 0 in case of an error. + */ static noinline int check_committed_ref(struct btrfs_inode *inode, struct btrfs_path *path, u64 offset, u64 bytenr) From patchwork Wed Dec 11 14:53:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13903629 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62BF11A2872 for ; Wed, 11 Dec 2024 14:53:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928823; cv=none; b=q1CIQaFLtKn/hu+OtfQwwrOG/3H2J9BXq+sRUFBcvKIgBuM+OXyuB32b8oBpn1QYdjHMFUXIfa81h+hhsVMxIQPibv1owWtMCDcU1zqL4d3ap6mjJQ67gIVc7AKM8HLXXv182DlkiagZCSfd+QyEvNGJGYrJBVgvG57/vR2aQeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733928823; c=relaxed/simple; bh=BBqgylXidZCr8p9bv7e8+j3t3kEn9oqjUjJMKDpQkXE=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IU3zIlKFIZTlsGGPJDoxJdQb1BmdPnSzNWi38O8/b0L9xkuuVtlHnBcgiSLZl+TBwNg2mSREY49DuHy8gcQa7oqcWkD23HWTPM04YpFuzkho2Rf6RDJsdaWayXg9Mkw74/bJU/HE2xesG1+eTz3Jse9RnfVjaB5oMOKArtr5o0Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PiWWDuFB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PiWWDuFB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5271AC4CED7 for ; Wed, 11 Dec 2024 14:53:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733928822; bh=BBqgylXidZCr8p9bv7e8+j3t3kEn9oqjUjJMKDpQkXE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=PiWWDuFBYPapoFZKmM/i+mEPncgLA/Yf1M4Fg7pFmRNO5AOFNuIOKFIL3SS7XRvoR +7eEbbZeUuUvjZvlJWn73oF/XILMVxhG8Z3g3MgdHf/k8nGjUypJhEU/UKvVgPXW/f PieWN1LuOj9CTO/7sen2z7rw2n+YBjMeKdC5RY440QTGMk9VA1c7g8T7sqn93OqPJD DVeqTrabca6wRnCUvqTs7vYcqC82j2pVBcdXznv8zmjfrfWMj4SmYp1IrojrHfWib9 oozN8w41pz7EhrkVDeaOJZA2BdgT/bd5B9WlBoUhJpO0uPLrPotw77Yj5rZch6F2Qv /kWJFFIDy+ujA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 11/11] btrfs: add assertions and comment about path expectations to btrfs_cross_ref_exist() Date: Wed, 11 Dec 2024 14:53:28 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana We should always call check_delayed_ref() with a path having a locked leaf from the extent tree where either the extent item is located or where it should be located in case it doesn't exist yet (when there's a pending unflushed delayed ref to do it), as we need to lock any existing delayed ref head while holding such leaf locked in order to avoid races with flushing delayed references, which could make us think an extent is not shared when it really is. So add some assertions and a comment about such expectations to btrfs_cross_ref_exist(), which is the only caller of check_delayed_ref(). Signed-off-by: Filipe Manana --- fs/btrfs/extent-tree.c | 25 +++++++++++++++++++++++++ fs/btrfs/locking.h | 5 +++++ 2 files changed, 30 insertions(+) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 3dea8ce87bf7..7ef07ae0c895 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -2425,6 +2425,31 @@ int btrfs_cross_ref_exist(struct btrfs_inode *inode, u64 offset, if (ret && ret != -ENOENT) goto out; + /* + * The path must have a locked leaf from the extent tree where + * the extent item for our extent is located, in case it exists, + * or where it should be located in case it doesn't exist yet + * because it's new and its delayed ref was not yet flushed. + * We need to lock the delayed ref head at check_delayed_ref(), + * if one exists, while holding the leaf locked in order to not + * race with delayed ref flushing, missing references and + * incorrectly reporting that the extent is not shared. + */ + if (IS_ENABLED(CONFIG_BTRFS_ASSERT)) { + struct extent_buffer *leaf = path->nodes[0]; + + ASSERT(leaf != NULL); + btrfs_assert_tree_read_locked(leaf); + + if (ret != -ENOENT) { + struct btrfs_key key; + + btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); + ASSERT(key.objectid == bytenr); + ASSERT(key.type == BTRFS_EXTENT_ITEM_KEY); + } + } + ret = check_delayed_ref(inode, path, offset, bytenr); } while (ret == -EAGAIN && !path->nowait); diff --git a/fs/btrfs/locking.h b/fs/btrfs/locking.h index 35036b151bf5..c69e57ff804b 100644 --- a/fs/btrfs/locking.h +++ b/fs/btrfs/locking.h @@ -199,8 +199,13 @@ static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { lockdep_assert_held_write(&eb->lock); } +static inline void btrfs_assert_tree_read_locked(struct extent_buffer *eb) +{ + lockdep_assert_held_read(&eb->lock); +} #else static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { } +static inline void btrfs_assert_tree_read_locked(struct extent_buffer *eb) { } #endif void btrfs_unlock_up_safe(struct btrfs_path *path, int level);