Skip to content

Commit 5ff9f62

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: tests: add hardlink related tests for mkfs --subvol
This introduces two new cases: - 3 hardlinks without any subvolume This should results 3 hard links inside the btrfs. - 3 hardlinks, but a subvolume will split 2 of them Then the 2 inside the same subvolume should still report 2 nlinks, but the lone one inside the new subvolume can only report 1 nlink. Signed-off-by: Qu Wenruo <[email protected]>
1 parent ef11574 commit 5ff9f62

File tree

2 files changed

+68
-18
lines changed

2 files changed

+68
-18
lines changed

mkfs/rootdir.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ static int ftw_add_inode(const char *full_path, const struct stat *st,
725725
parent = rootdir_path_last(&current_path);
726726
root = parent->root;
727727

728-
/* For non-directory inode, check if there is already any hard link. */
728+
/* Check if there is already a hard link record for this. */
729729
if (have_hard_links) {
730730
struct hardlink_entry *found;
731731

@@ -771,6 +771,7 @@ static int ftw_add_inode(const char *full_path, const struct stat *st,
771771
error("failed to insert inode item %llu for '%s': %m", ino, full_path);
772772
return ret;
773773
}
774+
774775
ret = btrfs_add_link(g_trans, root, ino, parent->ino,
775776
full_path + ftwbuf->base,
776777
strlen(full_path) - ftwbuf->base,
@@ -782,10 +783,7 @@ static int ftw_add_inode(const char *full_path, const struct stat *st,
782783
return ret;
783784
}
784785

785-
/*
786-
* Found a possible hard link, add it into the hard link rb tree for
787-
* future detection.
788-
*/
786+
/* Record this new hard link. */
789787
if (have_hard_links) {
790788
ret = add_hard_link(root, ino, st);
791789
if (ret < 0) {

tests/mkfs-tests/036-rootdir-subvol/test.sh

+65-13
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,75 @@ prepare_test_dev
1111

1212
tmp=$(_mktemp_dir mkfs-rootdir)
1313

14-
run_check touch "$tmp/foo"
15-
run_check mkdir "$tmp/dir"
16-
run_check mkdir "$tmp/dir/subvol"
17-
run_check touch "$tmp/dir/subvol/bar"
14+
basic()
15+
{
16+
run_check touch "$tmp/foo"
17+
run_check mkdir "$tmp/dir"
18+
run_check mkdir "$tmp/dir/subvol"
19+
run_check touch "$tmp/dir/subvol/bar"
1820

19-
run_check_mkfs_test_dev --rootdir "$tmp" --subvol dir/subvol
20-
run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV"
21+
run_check_mkfs_test_dev --rootdir "$tmp" --subvol dir/subvol
22+
run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV"
2123

22-
run_check_mount_test_dev
23-
run_check_stdout $SUDO_HELPER "$TOP/btrfs" subvolume list "$TEST_MNT" | \
24+
run_check_mount_test_dev
25+
run_check_stdout $SUDO_HELPER "$TOP/btrfs" subvolume list "$TEST_MNT" | \
2426
cut -d\ -f9 > "$tmp/output"
25-
run_check_umount_test_dev
27+
run_check_umount_test_dev
2628

27-
result=$(cat "$tmp/output")
29+
result=$(cat "$tmp/output")
2830

29-
if [ "$result" != "dir/subvol" ]; then
30-
_fail "dir/subvol not in subvolume list"
31-
fi
31+
if [ "$result" != "dir/subvol" ]; then
32+
_fail "dir/subvol not in subvolume list"
33+
fi
34+
rm -rf -- "$tmp/foo" "$tmp/dir"
35+
}
3236

37+
basic_hardlinks()
38+
{
39+
run_check touch "$tmp/hl1"
40+
run_check ln "$tmp/hl1" "$tmp/hl2"
41+
run_check mkdir "$tmp/dir"
42+
run_check ln "$tmp/hl1" "$tmp/dir/hl3"
43+
44+
run_check_mkfs_test_dev --rootdir "$tmp"
45+
run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV"
46+
47+
run_check_mount_test_dev
48+
nr_hardlink=$(run_check_stdout $SUDO_HELPER stat -c "%h" "$TEST_MNT/hl1")
49+
50+
if [ "$nr_hardlink" -ne 3 ]; then
51+
_fail "hard link number incorrect, has ${nr_hardlink} expect 3"
52+
fi
53+
run_check_umount_test_dev
54+
rm -rf -- "$tmp/hl1" "$tmp/hl2" "$tmp/dir"
55+
}
56+
57+
split_by_subvolume_hardlinks()
58+
{
59+
run_check touch "$tmp/hl1"
60+
run_check ln "$tmp/hl1" "$tmp/hl2"
61+
run_check mkdir "$tmp/subv"
62+
run_check ln "$tmp/hl1" "$tmp/subv/hl3"
63+
64+
run_check_mkfs_test_dev --rootdir "$tmp" --subvol subv
65+
run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV"
66+
67+
run_check_mount_test_dev
68+
nr_hardlink=$(run_check_stdout $SUDO_HELPER stat -c "%h" "$TEST_MNT/hl1")
69+
70+
if [ "$nr_hardlink" -ne 2 ]; then
71+
_fail "hard link number incorrect for hl1, has ${nr_hardlink} expect 2"
72+
fi
73+
74+
nr_hardlink=$(run_check_stdout $SUDO_HELPER stat -c "%h" "$TEST_MNT/subv/hl3")
75+
if [ "$nr_hardlink" -ne 1 ]; then
76+
_fail "hard link number incorrect for subv/hl3, has ${nr_hardlink} expect 1"
77+
fi
78+
run_check_umount_test_dev
79+
rm -rf -- "$tmp/hl1" "$tmp/hl2" "$tmp/dir"
80+
}
81+
82+
basic
83+
basic_hardlinks
84+
split_by_subvolume_hardlinks
3385
rm -rf -- "$tmp"

0 commit comments

Comments
 (0)