misc: move exit status into trap handler
[xfstests-dev.git] / tests / btrfs / 241
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2021 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/241
6 #
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.
12 #
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 tmp=/tmp/$$
18 status=1        # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
20
21 _cleanup()
22 {
23         cd /
24         rm -fr $send_files_dir
25         rm -f $tmp.*
26 }
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/filter
31
32 # real QA test starts here
33 _supported_fs btrfs
34 _require_test
35 _require_scratch
36 _require_fssum
37
38 send_files_dir=$TEST_DIR/btrfs-test-$seq
39
40 rm -f $seqres.full
41 rm -fr $send_files_dir
42 mkdir $send_files_dir
43
44 _scratch_mkfs >>$seqres.full 2>&1
45 _scratch_mount
46
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
51
52 mkdir $SCRATCH_MNT/A
53 ln $SCRATCH_MNT/file3 $SCRATCH_MNT/A/hard_link
54
55 # Filesystem looks like:
56 #
57 # .                                     (ino 256)
58 # |----- file1                          (ino 257)
59 # |----- file2                          (ino 258)
60 # |----- file3                          (ino 259)
61 # |----- A/                             (ino 260)
62 #        |---- hard_link                (ino 259)
63 #
64
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
69
70 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
71         $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
72
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
76
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
81
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
86
87 # Filesystem now looks like:
88 #
89 # .                                     (ino 256)
90 # |----- tmp                            (ino 259)
91 # |----- file3                          (ino 259)
92 # |----- B/                             (ino 260)
93 # |      |---- file1                    (ino 257)
94 # |      |---- hard_link                (ino 258)
95 # |
96 # |----- A                              (ino 258)
97
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
103
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
107
108 # Now recreate the filesystem by receiving both send streams and verify we get
109 # the same content that the original filesystem had.
110 _scratch_unmount
111 _scratch_mkfs >>$seqres.full 2>&1
112 _scratch_mount
113
114 # First add the first snapshot to the new filesystem by applying the first send
115 # stream.
116 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
117
118 # The incremental receive operation below used to fail with the following error:
119 #
120 #    ERROR: unlink A/hard_link failed: No such file or directory
121 #
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
124 # caches that path.
125 #
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
129 # "/o260-6-0".
130 #
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
140 # above error.
141 #
142 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
143
144 $FSSUM_PROG -r $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
145 $FSSUM_PROG -r $send_files_dir/2.fssum $SCRATCH_MNT/mysnap2
146
147 status=0
148 exit