2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2021 SUSE Linux Products GmbH. All Rights Reserved.
5 # FS QA Test No. btrfs/241
7 # Test that an incremental send operation succeeds, and produces the correct
8 # results, after renaming and moving around directories and files with multiple
9 # hardlinks, in such a way that one of the files gets the old name and location
10 # of a directory and another name (hardlink) with the old name and location of
11 # another file that was located in that same directory.
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
18 status=1 # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
24 rm -fr $send_files_dir
28 # get standard environment, filters and checks
32 # real QA test starts here
38 send_files_dir=$TEST_DIR/btrfs-test-$seq
41 rm -fr $send_files_dir
44 _scratch_mkfs >>$seqres.full 2>&1
47 # Create our test files and directory. Inode 259 (file3) has two hard links.
48 touch $SCRATCH_MNT/file1
49 touch $SCRATCH_MNT/file2
50 touch $SCRATCH_MNT/file3
53 ln $SCRATCH_MNT/file3 $SCRATCH_MNT/A/hard_link
55 # Filesystem looks like:
58 # |----- file1 (ino 257)
59 # |----- file2 (ino 258)
60 # |----- file3 (ino 259)
62 # |---- hard_link (ino 259)
65 # Now create the base snapshot, which is going to be the parent snapshot for
66 # a later incremental send and receive.
67 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
68 $SCRATCH_MNT/mysnap1 > /dev/null
70 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
71 $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
73 # Move inode 257 into directory inode 260. This results in computing the path
74 # for inode 260 as "/A" and caching it.
75 mv $SCRATCH_MNT/file1 $SCRATCH_MNT/A/file1
77 # Move inode 258 (file2) into directory inode 260, with a name of "hard_link",
78 # moving first inode 259 away since it currently has that location and name.
79 mv $SCRATCH_MNT/A/hard_link $SCRATCH_MNT/tmp
80 mv $SCRATCH_MNT/file2 $SCRATCH_MNT/A/hard_link
82 # Now rename inode 260 to something else (B for example) and then create a hard
83 # link for inode 258 that has the old name and location of inode 260 ("/A").
84 mv $SCRATCH_MNT/A $SCRATCH_MNT/B
85 ln $SCRATCH_MNT/B/hard_link $SCRATCH_MNT/A
87 # Filesystem now looks like:
90 # |----- tmp (ino 259)
91 # |----- file3 (ino 259)
93 # | |---- file1 (ino 257)
94 # | |---- hard_link (ino 258)
98 # Create another snapshot of our subvolume and use it for an incremental send.
99 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
100 $SCRATCH_MNT/mysnap2 > /dev/null
101 $BTRFS_UTIL_PROG send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
102 $SCRATCH_MNT/mysnap2 2>&1 1>/dev/null | _filter_scratch
104 $FSSUM_PROG -A -f -w $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
105 $FSSUM_PROG -A -f -w $send_files_dir/2.fssum \
106 -x $SCRATCH_MNT/mysnap2/mysnap1 $SCRATCH_MNT/mysnap2
108 # Now recreate the filesystem by receiving both send streams and verify we get
109 # the same content that the original filesystem had.
111 _scratch_mkfs >>$seqres.full 2>&1
114 # First add the first snapshot to the new filesystem by applying the first send
116 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
118 # The incremental receive operation below used to fail with the following error:
120 # ERROR: unlink A/hard_link failed: No such file or directory
122 # This is because when send is processing inode 257, it generates the path for
123 # inode 260 as "/A", since that inode is its parent in the send snapshot, and
126 # Later when processing inode 258, it first processes its new reference that has
127 # the path of "/A", which results in orphanizing inode 260 because there is a
128 # a path collision. This results in issuing a rename operation from "/A" to
131 # Finally when processing the new reference "B/hard_link" for inode 258, it
132 # notices that it collides with inode 259 (not yet processed, because it has a
133 # higher inode number), since that inode has the name "hard_link" under the
134 # directory inode 260. It also checks that inode 259 has two hardlinks, so it
135 # decides to issue a unlink operation for the name "hard_link" for inode 259.
136 # However the path passed to the unlink operation is "/A/hard_link", which is
137 # incorrect since currently "/A" does not exists, due to the orphanization of
138 # inode 260 mentioned before. The path is incorrect because it was computed and
139 # cached before the orphanization. This results in the receiver to fail with the
142 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
144 $FSSUM_PROG -r $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
145 $FSSUM_PROG -r $send_files_dir/2.fssum $SCRATCH_MNT/mysnap2