btrfs: simplify the return value handling in search_ioctl()
authorSun YangKai <sunk67188@gmail.com>
Tue, 11 Mar 2025 08:13:12 +0000 (16:13 +0800)
committerDavid Sterba <dsterba@suse.com>
Tue, 18 Mar 2025 19:35:51 +0000 (20:35 +0100)
Move the assignment of -EFAULT to within the error condition check
in fault_in_subpage_writeable(). The previous placement outside the
condition could lead to the error value being overwritten by subsequent
assignments, cause unnecessary assignments.

Simplify loop exit logic by removing redundant goto.
The original code used 'goto err' to bypass post-loop processing after
handling errors from btrfs_search_forward(). However, the loop's
termination naturally falls through to the post-loop section, which
already handles 'ret' values. Replacing 'goto err' with 'break'
eliminates redundant control flow, consolidates error handling, and
makes the loop's exit conditions explicit.

Signed-off-by: Sun YangKai <sunk67188@gmail.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c

index c68e505710afb5055f9fd5c87ebb745d602e800f..a13d81bb56a089a5eb458d3339f486a58c15678c 100644 (file)
@@ -1641,21 +1641,19 @@ static noinline int search_ioctl(struct btrfs_root *root,
        key.offset = sk->min_offset;
 
        while (1) {
-               ret = -EFAULT;
                /*
                 * Ensure that the whole user buffer is faulted in at sub-page
                 * granularity, otherwise the loop may live-lock.
                 */
-               if (fault_in_subpage_writeable(ubuf + sk_offset,
-                                              *buf_size - sk_offset))
+               if (fault_in_subpage_writeable(ubuf + sk_offset, *buf_size - sk_offset)) {
+                       ret = -EFAULT;
                        break;
+               }
 
                ret = btrfs_search_forward(root, &key, path, sk->min_transid);
-               if (ret != 0) {
-                       if (ret > 0)
-                               ret = 0;
-                       goto err;
-               }
+               if (ret)
+                       break;
+
                ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
                                 &sk_offset, &num_found);
                btrfs_release_path(path);
@@ -1663,9 +1661,10 @@ static noinline int search_ioctl(struct btrfs_root *root,
                        break;
 
        }
+       /* Normalize return values from btrfs_search_forward() and copy_to_sk(). */
        if (ret > 0)
                ret = 0;
-err:
+
        sk->nr_items = num_found;
        btrfs_put_root(root);
        btrfs_free_path(path);