[PATCH v2] btrfs: Fix passing 0 to ERR_PTR in btrfs_search_dir_index_item()

From: Yue Haibing
Date: Tue Oct 22 2024 - 05:35:26 EST


ret may be zero in btrfs_search_dir_index_item() and should not passed
to ERR_PTR(). Now btrfs_unlink_subvol() is the only caller to this,
reconstructed it to check ERR_PTR(-ENOENT) while ret >= 0, this fix
smatch warnings:

fs/btrfs/dir-item.c:353
btrfs_search_dir_index_item() warn: passing zero to 'ERR_PTR'

Fixes: 9dcbe16fccbb ("btrfs: use btrfs_for_each_slot in btrfs_search_dir_index_item")
Signed-off-by: Yue Haibing <yuehaibing@xxxxxxxxxx>
---
v2: return ERR_PTR(-ENOENT) while ret >= 0
---
fs/btrfs/dir-item.c | 4 ++--
fs/btrfs/inode.c | 7 ++-----
2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index d3093eba54a5..1ea5d8fcfbf7 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -345,8 +345,8 @@ btrfs_search_dir_index_item(struct btrfs_root *root, struct btrfs_path *path,
return di;
}
/* Adjust return code if the key was not found in the next leaf. */
- if (ret > 0)
- ret = 0;
+ if (ret >= 0)
+ ret = -ENOENT;

return ERR_PTR(ret);
}
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index acb206d5da3b..7569c8b27f9f 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -4373,11 +4373,8 @@ static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
*/
if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
di = btrfs_search_dir_index_item(root, path, dir_ino, &fname.disk_name);
- if (IS_ERR_OR_NULL(di)) {
- if (!di)
- ret = -ENOENT;
- else
- ret = PTR_ERR(di);
+ if (IS_ERR(di)) {
+ ret = PTR_ERR(di);
btrfs_abort_transaction(trans, ret);
goto out;
}
--
2.34.1