* 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)
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;
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);
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)
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
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
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"