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 _begin_fstest auto quick send
16 # Override the default cleanup function.
20 rm -fr $send_files_dir
24 # Import common functions.
27 # real QA test starts here
33 send_files_dir=$TEST_DIR/btrfs-test-$seq
35 rm -fr $send_files_dir
38 _scratch_mkfs >>$seqres.full 2>&1
41 # Create our test files and directory. Inode 259 (file3) has two hard links.
42 touch $SCRATCH_MNT/file1
43 touch $SCRATCH_MNT/file2
44 touch $SCRATCH_MNT/file3
47 ln $SCRATCH_MNT/file3 $SCRATCH_MNT/A/hard_link
49 # Filesystem looks like:
52 # |----- file1 (ino 257)
53 # |----- file2 (ino 258)
54 # |----- file3 (ino 259)
56 # |---- hard_link (ino 259)
59 # Now create the base snapshot, which is going to be the parent snapshot for
60 # a later incremental send and receive.
61 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
62 $SCRATCH_MNT/mysnap1 > /dev/null
64 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
65 $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
67 # Move inode 257 into directory inode 260. This results in computing the path
68 # for inode 260 as "/A" and caching it.
69 mv $SCRATCH_MNT/file1 $SCRATCH_MNT/A/file1
71 # Move inode 258 (file2) into directory inode 260, with a name of "hard_link",
72 # moving first inode 259 away since it currently has that location and name.
73 mv $SCRATCH_MNT/A/hard_link $SCRATCH_MNT/tmp
74 mv $SCRATCH_MNT/file2 $SCRATCH_MNT/A/hard_link
76 # Now rename inode 260 to something else (B for example) and then create a hard
77 # link for inode 258 that has the old name and location of inode 260 ("/A").
78 mv $SCRATCH_MNT/A $SCRATCH_MNT/B
79 ln $SCRATCH_MNT/B/hard_link $SCRATCH_MNT/A
81 # Filesystem now looks like:
84 # |----- tmp (ino 259)
85 # |----- file3 (ino 259)
87 # | |---- file1 (ino 257)
88 # | |---- hard_link (ino 258)
92 # Create another snapshot of our subvolume and use it for an incremental send.
93 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
94 $SCRATCH_MNT/mysnap2 > /dev/null
95 $BTRFS_UTIL_PROG send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
96 $SCRATCH_MNT/mysnap2 2>&1 1>/dev/null | _filter_scratch
98 $FSSUM_PROG -A -f -w $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
99 $FSSUM_PROG -A -f -w $send_files_dir/2.fssum \
100 -x $SCRATCH_MNT/mysnap2/mysnap1 $SCRATCH_MNT/mysnap2
102 # Now recreate the filesystem by receiving both send streams and verify we get
103 # the same content that the original filesystem had.
105 _scratch_mkfs >>$seqres.full 2>&1
108 # First add the first snapshot to the new filesystem by applying the first send
110 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
112 # The incremental receive operation below used to fail with the following error:
114 # ERROR: unlink A/hard_link failed: No such file or directory
116 # This is because when send is processing inode 257, it generates the path for
117 # inode 260 as "/A", since that inode is its parent in the send snapshot, and
120 # Later when processing inode 258, it first processes its new reference that has
121 # the path of "/A", which results in orphanizing inode 260 because there is a
122 # a path collision. This results in issuing a rename operation from "/A" to
125 # Finally when processing the new reference "B/hard_link" for inode 258, it
126 # notices that it collides with inode 259 (not yet processed, because it has a
127 # higher inode number), since that inode has the name "hard_link" under the
128 # directory inode 260. It also checks that inode 259 has two hardlinks, so it
129 # decides to issue a unlink operation for the name "hard_link" for inode 259.
130 # However the path passed to the unlink operation is "/A/hard_link", which is
131 # incorrect since currently "/A" does not exists, due to the orphanization of
132 # inode 260 mentioned before. The path is incorrect because it was computed and
133 # cached before the orphanization. This results in the receiver to fail with the
136 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
138 $FSSUM_PROG -r $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
139 $FSSUM_PROG -r $send_files_dir/2.fssum $SCRATCH_MNT/mysnap2