]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: inspect logical-resolve: fail on an unresolvable reference
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 30 Oct 2026 03:34:39 +0000 (23:34 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 30 Oct 2026 05:02:59 +0000 (01:02 -0400)
When the subvolume of a reference cannot be opened, logical-resolve
prints "inode N subvol S could not be accessed: not mounted" to stdout
and exits with status 0, so a script that reads the paths sees fewer
paths, or none at all, and no error.  The same goes for a path of an
inode that no mount reaches ("inode N subvol S path P could not be
accessed: not mounted"), in logical-resolve and inode-resolve.

Report them as errors on stderr and exit with status 1 once all the
other paths have been printed.

This changes the output: scripts that parsed the message from stdout
will no longer see it there.

Update misc-tests/042, which read the message from stdout and relied on
the exit status 0: accept exit status 1 only together with at least one
such error, and require the error whenever the status is 1.  Also add
cases where bind mounts of directories are the only way in: a directory
of a subvolume with a file in it and a subvolume nested below it, and a
directory of that nested subvolume, each resolved with the bind mount
point, a directory in it, "." and an absolute path as the argument, and
checked to print the expected path relative to the argument, and a
file reached through the bind mount of its directory when the argument
is elsewhere, and a directory and mount points whose names have a space,
a tab, a newline and a backslash (escaped as octal in mountinfo) through
the argument, a bind mount and a subvolume mount.  Update misc-tests/074
for the stderr message and exit status.

Link: https://github.com/kdave/btrfs-progs/issues/400
Assisted-by: Claude:claude-opus-5-5
cmds/inspect.c
tests/misc-tests/042-inspect-internal-logical-resolve/test.sh
tests/misc-tests/074-inspect-internal-inode-resolve/test.sh

index 3d9a7ef8682af6148f8449bb82b557392beee9c5..e1ad1579c8c339d997dd5b6293f7c7c9abfb41bd 100644 (file)
@@ -461,7 +461,8 @@ static const struct subvol_bind *subvol_bind_for(const struct subvol_lookup *sl,
  * Route 1 and 3 open the subvolume root and reach every path; route 2 and
  * 4 reach only the paths below their directory.  Paths that no route
  * reaches, or the whole inode if no route reaches the subvolume, are
- * reported as not mounted.  Return 0, or 1 if a lookup failed.
+ * reported as not mounted, on stderr.  Return 0, or 1 if a lookup failed or
+ * a path was not reached.
  */
 static int resolve_ino_paths(int fd, const char *arg, const char *arg_fs_path,
                             const u8 *fsid, u64 root, u64 inum)
@@ -484,9 +485,9 @@ static int resolve_ino_paths(int fd, const char *arg, const char *arg_fs_path,
        memcpy(sl.fsid, fsid, BTRFS_FSID_SIZE);
 
        if (btrfs_subvolid_resolve(fd, subvol, sizeof(subvol), root) < 0) {
-               printf("inode %llu subvol <id %llu> could not be accessed: not mounted\n",
-                      inum, root);
-               return 0;
+               error("inode %llu subvol <id %llu> could not be accessed: not mounted",
+                     inum, root);
+               return 1;
        }
        sl.subvol = subvol;
 
@@ -527,8 +528,9 @@ static int resolve_ino_paths(int fd, const char *arg, const char *arg_fs_path,
        if (!strip) {
                names_fd = sl.fd >= 0 ? sl.fd : sl.bind_fd;
                if (names_fd < 0) {
-                       printf("inode %llu subvol %s could not be accessed: not mounted\n",
-                              inum, subvol[0] ? subvol : "<FS_TREE>");
+                       error("inode %llu subvol %s could not be accessed: not mounted",
+                             inum, subvol[0] ? subvol : "<FS_TREE>");
+                       ret = 1;
                        goto out;
                }
                ret = ino_paths(inum, names_fd, pathbuf);
@@ -556,8 +558,9 @@ static int resolve_ino_paths(int fd, const char *arg, const char *arg_fs_path,
                        print_ino_path(b->path, rest);
                        continue;
                }
-               printf("inode %llu subvol %s path %s could not be accessed: not mounted\n",
-                      inum, subvol[0] ? subvol : "<FS_TREE>", name);
+               error("inode %llu subvol %s path %s could not be accessed: not mounted",
+                     inum, subvol[0] ? subvol : "<FS_TREE>", name);
+               ret = 1;
        }
 out:
        if (sl.fd >= 0)
index 0ef90e93fafb0afd737be857299f9781325cc377..36987b4be95e4ea8032ef53ad469f394652a1bdb 100755 (executable)
@@ -11,22 +11,54 @@ prepare_test_dev
 check_prereq btrfs
 check_prereq mkfs.btrfs
 
+# A reference whose subvolume is not reachable from any mount is reported on
+# stderr, and makes logical-resolve exit with status 1 after printing the
+# paths of all the other references
+unreachable_re='^ERROR: inode [0-9]+ subvol .* could not be accessed: not mounted$'
+
+# Resolve $1 with $2 (default $TEST_MNT) as the path argument: every printed
+# path must be a file, and if $3 is given it must be one of them
 check_logical_offset_filename()
 {
        local offset
+       local path
+       local want
+       local output
+       local ret
+       local unreachable=0
+       local found=false
 
        offset="$1"
+       path="${2:-$TEST_MNT}"
+       want="$3"
 
+       output=$(run_mayfail_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
+                                       logical-resolve "$offset" "$path")
+       ret=$?
        while read file; do
-               if [[ "$file" = *"inode "* ]]; then
+               if [ -z "$file" ]; then
+                       continue
+               elif [[ "$file" =~ $unreachable_re ]]; then
                        _log "$file"
+                       unreachable=$((unreachable + 1))
                elif [ ! -f "$file" ]; then
                        _fail "path '$file' file cannot be accessed"
                else
                        _log "$file"
+                       [ "$file" = "$want" ] && found=true
                fi
-       done < <(run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
-                                       logical-resolve "$offset" "$TEST_MNT")
+       done <<< "$output"
+
+       if [ -n "$want" ] && ! $found; then
+               _fail "logical-resolve $offset $path did not print $want"
+       fi
+
+       if [ "$ret" -ne 0 ] && [ "$unreachable" -eq 0 ]; then
+               _fail "logical-resolve $offset failed with no unreachable references"
+       fi
+       if [ "$ret" -eq 0 ] && [ "$unreachable" -ne 0 ]; then
+               _fail "logical-resolve $offset succeeded with unreachable references"
+       fi
 }
 
 run_check_mkfs_test_dev
@@ -49,6 +81,29 @@ run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=150 of="$TEST_MNT/@/vol1/subv
 run_check $SUDO_HELPER "$TOP/btrfs" subvolume snapshot "$TEST_MNT/@/vol1/subvol1" \
                                                        "$TEST_MNT/@/vol1/snapshot1"
 
+# For the bind mount cases below: file3 in a directory of vol1, subvol2
+# nested below that directory with file4, and file5 in a directory of
+# subvol2.  They are created after the snapshots, so only one subvolume
+# refers to each of them.
+run_check $SUDO_HELPER mkdir -p "$TEST_MNT/@/vol1/bdir/sub"
+run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=4 of="$TEST_MNT/@/vol1/bdir/file3"
+run_check $SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/@/vol1/bdir/sub/subvol2"
+subvol2id=$(run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal rootid \
+                       "$TEST_MNT/@/vol1/bdir/sub/subvol2")
+run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=4 of="$TEST_MNT/@/vol1/bdir/sub/subvol2/file4"
+run_check $SUDO_HELPER mkdir "$TEST_MNT/@/vol1/bdir/sub/subvol2/d2"
+run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=4 of="$TEST_MNT/@/vol1/bdir/sub/subvol2/d2/file5"
+# For the escaping cases: a directory of subvol2 whose name has a space, a
+# tab, a newline and a backslash, which mountinfo shows as octal escapes
+odd=$'odd \t\n\\dir'
+run_check $SUDO_HELPER mkdir "$TEST_MNT/@/vol1/bdir/sub/subvol2/$odd"
+run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=4 \
+       of="$TEST_MNT/@/vol1/bdir/sub/subvol2/$odd/file6"
+file3ino=$(stat -c %i "$TEST_MNT/@/vol1/bdir/file3")
+file4ino=$(stat -c %i "$TEST_MNT/@/vol1/bdir/sub/subvol2/file4")
+file5ino=$(stat -c %i "$TEST_MNT/@/vol1/bdir/sub/subvol2/d2/file5")
+file6ino=$(stat -c %i "$TEST_MNT/@/vol1/bdir/sub/subvol2/$odd/file6")
+
 run_check "$TOP/btrfs" filesystem sync "$TEST_MNT"
 
 run_check_umount_test_dev
@@ -82,3 +137,88 @@ for offset in $("$TOP/btrfs" inspect-internal dump-tree -t "$subvol1id" "$TEST_D
 done
 
 run_check_umount_test_dev
+
+# Print the logical addresses of the extents of inode $2 in tree $1
+inode_offsets()
+{
+       "$TOP/btrfs" inspect-internal dump-tree -t "$1" "$TEST_DEV" |
+               awk -v key="key ($2 EXTENT_DATA " \
+                       'index($0, key) { f = 1; next }
+                        /key \(/ { f = 0 }
+                        f && /disk byte/ && $5 != 0 { print $5 }'
+}
+file3offsets=$(inode_offsets "$vol1id" "$file3ino")
+file4offsets=$(inode_offsets "$subvol2id" "$file4ino")
+file5offsets=$(inode_offsets "$subvol2id" "$file5ino")
+file6offsets=$(inode_offsets "$subvol2id" "$file6ino")
+[ -n "$file3offsets" ] && [ -n "$file4offsets" ] && [ -n "$file5offsets" ] &&
+       [ -n "$file6offsets" ] || _fail "no extents found for file3 to file6"
+
+# Bind mounts of directories are the only way in: mnt4 is vol1's directory
+# bdir, mnt5 the directory d2 of the nested subvol2.  Paths are printed
+# relative to the path argument, which can be the bind mount point, a
+# directory in it, or a relative path.  With mnt5 as the argument, file3 is
+# reached through the bind mount of its directory, as $PWD/mnt4 (the mount
+# point as listed in mountinfo).
+run_check $SUDO_HELPER mount -o subvol=/@/vol1 "$TEST_DEV" "$TEST_MNT"
+run_check mkdir -p mnt4 mnt5
+run_check $SUDO_HELPER mount --bind "$TEST_MNT/bdir" mnt4
+run_check $SUDO_HELPER mount --bind "$TEST_MNT/bdir/sub/subvol2/d2" mnt5
+run_check_umount_test_dev "$TEST_MNT"
+
+for offset in $file3offsets; do
+       check_logical_offset_filename "$offset" mnt4 mnt4/file3
+       check_logical_offset_filename "$offset" "$PWD/mnt4" "$PWD/mnt4/file3"
+       ( cd mnt4 && check_logical_offset_filename "$offset" . ./file3 ) ||
+               _fail "logical-resolve $offset in mnt4"
+       check_logical_offset_filename "$offset" mnt5 "$PWD/mnt4/file3"
+done
+for offset in $file4offsets; do
+       check_logical_offset_filename "$offset" mnt4 mnt4/sub/subvol2/file4
+       check_logical_offset_filename "$offset" mnt4/sub mnt4/sub/subvol2/file4
+       ( cd mnt4/sub && check_logical_offset_filename "$offset" . ./subvol2/file4 ) ||
+               _fail "logical-resolve $offset in mnt4/sub"
+done
+for offset in $file5offsets; do
+       check_logical_offset_filename "$offset" mnt5 mnt5/file5
+       ( cd mnt5 && check_logical_offset_filename "$offset" . ./file5 ) ||
+               _fail "logical-resolve $offset in mnt5"
+done
+
+run_check_umount_test_dev mnt5
+run_check_umount_test_dev mnt4
+run_check rmdir -- mnt4 mnt5
+
+# Resolve $1 with $2 as the path argument: the output must be exactly $3,
+# which may contain any character but a trailing newline
+check_logical_offset_exact()
+{
+       local output
+
+       output=$(run_check_stdout $SUDO_HELPER "$TOP/btrfs" inspect-internal \
+                                       logical-resolve "$1" "$2")
+       if [ "$output" != "$3" ]; then
+               _fail "logical-resolve $1 $2 printed '$output', not '$3'"
+       fi
+}
+
+# Mount points and mounted directories with special characters: mnt6 is a
+# bind mount of the odd directory of subvol2 at an odd mount point, mnt7
+# the unrelated subvol1, then mnt8 subvol2 itself at an odd mount point
+run_check $SUDO_HELPER mount -o subvol=/@/vol1/bdir/sub/subvol2 "$TEST_DEV" "$TEST_MNT"
+run_check mkdir -p "mnt6 $odd" mnt7 "mnt8 $odd"
+run_check $SUDO_HELPER mount --bind "$TEST_MNT/$odd" "mnt6 $odd"
+run_check $SUDO_HELPER mount -o subvol=/@/vol1/subvol1 "$TEST_DEV" mnt7
+run_check_umount_test_dev "$TEST_MNT"
+for offset in $file6offsets; do
+       check_logical_offset_exact "$offset" "mnt6 $odd" "mnt6 $odd/file6"
+       check_logical_offset_exact "$offset" mnt7 "$PWD/mnt6 $odd/file6"
+done
+run_check $SUDO_HELPER mount -o subvol=/@/vol1/bdir/sub/subvol2 "$TEST_DEV" "mnt8 $odd"
+for offset in $file6offsets; do
+       check_logical_offset_exact "$offset" mnt7 "$PWD/mnt8 $odd/$odd/file6"
+done
+run_check_umount_test_dev "mnt8 $odd"
+run_check_umount_test_dev mnt7
+run_check_umount_test_dev "mnt6 $odd"
+run_check rmdir -- "mnt6 $odd" mnt7 "mnt8 $odd"
index 22ace0cafcb0baa7f9fb61fbcf7d4c669f85a222..0e46e4b832d3658531acc25da7efefd85661e324 100755 (executable)
@@ -12,9 +12,10 @@ check_prereq mkfs.btrfs
 setup_root_helper
 prepare_test_dev
 
-# A path of the inode that no mount reaches
-unreachable_re='^inode [0-9]+ subvol .* path .* could not be accessed: not mounted$'
-unreachable_ret=0
+# A path of the inode that no mount reaches is reported on stderr, and makes
+# inode-resolve exit with status 1 after printing the other paths
+unreachable_re='^ERROR: inode [0-9]+ subvol .* path .* could not be accessed: not mounted$'
+unreachable_ret=1
 
 # Resolve inode $1 with $2 as the path argument: $3 paths must be reported
 # as not mounted, and the printed paths must be exactly $4...