btrfs/012: check free size of scratch device before copying files
[xfstests-dev.git] / tests / btrfs / 127
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2016 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/127
6 #
7 # Test that an incremental send operation works after doing radical changes
8 # in the directory hierarchy that involve switching the inode that directory
9 # entries point to.
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14
15 tmp=/tmp/$$
16 status=1        # failure is the default!
17 trap "_cleanup; exit \$status" 0 1 2 3 15
18
19 _cleanup()
20 {
21         cd /
22         rm -fr $send_files_dir
23         rm -f $tmp.*
24 }
25
26 # get standard environment, filters and checks
27 . ./common/rc
28 . ./common/filter
29
30 # real QA test starts here
31 _supported_fs btrfs
32 _require_test
33 _require_scratch
34 _require_fssum
35
36 send_files_dir=$TEST_DIR/btrfs-test-$seq
37
38 rm -f $seqres.full
39 rm -fr $send_files_dir
40 mkdir $send_files_dir
41
42 _scratch_mkfs >>$seqres.full 2>&1
43 _scratch_mount
44
45 # case 1
46 mkdir -p $SCRATCH_MNT/case_1/d/p1
47 mkdir $SCRATCH_MNT/case_1/p1
48
49 # case 2
50 mkdir -p $SCRATCH_MNT/case_2/a
51 mkdir $SCRATCH_MNT/case_2/d
52 mkdir $SCRATCH_MNT/case_2/e
53 mkdir $SCRATCH_MNT/case_2/f
54 mkdir $SCRATCH_MNT/case_2/ance
55 mkdir $SCRATCH_MNT/case_2/d/ance
56 mkdir $SCRATCH_MNT/case_2/a/c
57 mv $SCRATCH_MNT/case_2/e $SCRATCH_MNT/case_2/d/ance
58 mv $SCRATCH_MNT/case_2/f $SCRATCH_MNT/case_2/d/ance
59 mv $SCRATCH_MNT/case_2/ance $SCRATCH_MNT/case_2/d/ance
60
61 # case 3
62 mkdir -p $SCRATCH_MNT/case_3/d
63 mkdir $SCRATCH_MNT/case_3/a
64 mkdir $SCRATCH_MNT/case_3/waiting_dir
65 mkdir -p $SCRATCH_MNT/case_3/pre/ance
66 mkdir $SCRATCH_MNT/case_3/d/ance
67 mkdir $SCRATCH_MNT/case_3/a/c
68 mv $SCRATCH_MNT/case_3/waiting_dir $SCRATCH_MNT/case_3/d/ance
69
70 # case 4
71 mkdir -p $SCRATCH_MNT/case_4/tmp
72 mkdir $SCRATCH_MNT/case_4/below_ance
73 mkdir -p $SCRATCH_MNT/case_4/pre/wait_dir
74 mkdir $SCRATCH_MNT/case_4/desc
75 mkdir $SCRATCH_MNT/case_4/ance
76 mv $SCRATCH_MNT/case_4/below_ance $SCRATCH_MNT/case_4/ance
77 mkdir $SCRATCH_MNT/case_4/other_dir
78
79 # Filesystem looks like:
80 #
81 # .                                                                  (ino 256)
82 # |--- case_1/                                                       (ino 257)
83 # |       |---- d/                                                   (ino 258)
84 # |       |     |--- p1/                                             (ino 259)
85 # |       |
86 # |       |---- p1/                                                  (ino 260)
87 # |
88 # |--- case_2/                                                       (ino 261)
89 # |       |---- a/                                                   (ino 262)
90 # |       |     |---- c/                                             (ino 268)
91 # |       |
92 # |       |---- d/                                                   (ino 263)
93 # |             |---- ance/                                          (ino 267)
94 # |                     |---- e/                                     (ino 264)
95 # |                     |---- f/                                     (ino 265)
96 # |                     |---- ance/                                  (ino 266)
97 # |
98 # |--- case_3/                                                       (ino 269)
99 # |       |---- a/                                                   (ino 271)
100 # |       |     |---- c/                                             (ino 276)
101 # |       |
102 # |       |---- d/                                                   (ino 270)
103 # |       |     |---- ance/                                          (ino 275)
104 # |       |             |---- waiting_dir/                           (ino 272)
105 # |       |
106 # |       |---- pre/                                                 (ino 273)
107 # |              |---- ance/                                         (ino 274)
108 # |
109 # |--- case_4/                                                       (ino 277)
110 #         |---- tmp/                                                 (ino 278)
111 #         |---- pre/                                                 (ino 280)
112 #         |      |---- wait_dir/                                     (ino 281)
113 #         |
114 #         |---- desc/                                                (ino 282)
115 #         |---- ance/                                                (ino 283)
116 #         |       |---- below_ance/                                  (ino 279)
117 #         |
118 #         |---- other_dir/                                           (ino 284)
119 #
120 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap1
121
122 # case 1
123 #
124 # The directory named "d" (inode 257) has in both snapshots an entry with the
125 # name "p1" but it refers to different inodes in both snapshots (inode 258 in
126 # the parent snapshot and inode 259 in the send snapshot). When attempting to
127 # move inode 258, the operation is delayed because its new parent, inode 259,
128 # was not yet moved/renamed (as the stream is currently processing inode 258).
129 # Later on when processing inode 259, btrfs' send also ended up delaying the
130 # move/rename operation for this inode, so that it would happen after inode 258
131 # was processed, creating a circular dependency that resulted in the send stream
132 # terminating without issuing a rename operations for the inodes 258 and 259.
133 #
134 mv $SCRATCH_MNT/case_1/d/p1 $SCRATCH_MNT/case_1/p1
135 mv $SCRATCH_MNT/case_1/p1 $SCRATCH_MNT/case_1/d
136
137 # case 2
138 #
139 # When the inode 265 is processed, the path for inode 267 is computed, which at
140 # that time corresponds to "case_2/d/ance", and it was stored in the name cache
141 # (to avoid recomputing it again later when needed).
142 # Later on when processing inode 266, btrfs' send end up orphanizing (renaming
143 # to a name matching the pattern o<ino>-<gen>-<seq>) inode 267 because it has
144 # the same name as inode 266 and at that time it's a child of the new parent
145 # directory (inode 263) for inode 266. After the orphanization and while still
146 # processing inode 266, a rename operation for inode 266 was generated. However
147 # the source path for that rename operation was incorrect because it ended up
148 # using the old, pre-orphanization, name of inode 267.
149 #
150 mv $SCRATCH_MNT/case_2/a/c $SCRATCH_MNT/case_2
151 mv $SCRATCH_MNT/case_2/d/ance $SCRATCH_MNT/case_2/c
152 mv $SCRATCH_MNT/case_2/c/ance/ance $SCRATCH_MNT/case_2/d
153 mv $SCRATCH_MNT/case_2/c/ance/f $SCRATCH_MNT/case_2
154 mv $SCRATCH_MNT/case_2/c/ance/e $SCRATCH_MNT/case_2/f
155
156 # case 3
157 #
158 # This is similar to cases 1 and 2, but adding more complexity just to exercise
159 # btrfs' incremental send correctness.
160 #
161 mv $SCRATCH_MNT/case_3/d/ance $SCRATCH_MNT/case_3/a
162 mv $SCRATCH_MNT/case_3/a/c $SCRATCH_MNT/case_3
163 mv $SCRATCH_MNT/case_3/a/ance/waiting_dir $SCRATCH_MNT/case_3/c
164 mv $SCRATCH_MNT/case_3/pre/ance $SCRATCH_MNT/case_3/d
165 mv $SCRATCH_MNT/case_3/pre $SCRATCH_MNT/case_3/c/waiting_dir
166
167 # case 4
168 #
169 # When attempting to rename inode 283, the incremental send stream included an
170 # invalid destination path for the rename command. This was due to a missing
171 # path loop detection in the send code that made the rename of inode 283 happen
172 # without waiting for the rename of inode 284 to happen first (since it's an
173 # ancestor in the send snapshot that is beyond the current progress and it was
174 # also renamed/moved).
175 #
176 mv $SCRATCH_MNT/case_4/other_dir $SCRATCH_MNT/case_4/tmp
177 mv $SCRATCH_MNT/case_4/ance/below_ance $SCRATCH_MNT/case_4/tmp/other_dir
178 mv $SCRATCH_MNT/case_4/pre/wait_dir $SCRATCH_MNT/case_4/tmp/other_dir
179 mv $SCRATCH_MNT/case_4/pre $SCRATCH_MNT/case_4/tmp/other_dir/below_ance
180 mv $SCRATCH_MNT/case_4/desc $SCRATCH_MNT/case_4/tmp/other_dir/wait_dir
181 mv $SCRATCH_MNT/case_4/ance $SCRATCH_MNT/case_4/tmp/other_dir/wait_dir/desc
182
183 # Filesystem now looks like:
184 #
185 # .                                                                  (ino 256)
186 # |--- case_1/                                                       (ino 257)
187 # |        |--- d/                                                   (ino 258)
188 # |             |--- p1/                                             (ino 260)
189 # |                   |--- p1/                                       (ino 259)
190 # |
191 # |--- case_2/                                                       (ino 261)
192 # |       |---- a/                                                   (ino 262)
193 # |       |---- c/                                                   (ino 268)
194 # |       |     |---- ance/                                          (ino 267)
195 # |       |
196 # |       |---- d/                                                   (ino 263)
197 # |       |     |---- ance/                                          (ino 266)
198 # |       |
199 # |       |---- f/                                                   (ino 265)
200 # |             |---- e/                                             (ino 264)
201 # |
202 # |--- case_3/                                                       (ino 269)
203 # |       |---- a/                                                   (ino 271)
204 # |       |     |---- ance/                                          (ino 275)
205 # |       |
206 # |       |---- c/                                                   (ino 276)
207 # |       |     |---- waiting_dir/                                   (ino 272)
208 # |       |                   |---- pre/                             (ino 273)
209 # |       |
210 # |       |---- d/                                                   (ino 270)
211 # |             |---- ance/                                          (ino 274)
212 # |
213 # |--- case_4/                                                       (ino 277)
214 #         |---- tmp/                                                 (ino 278)
215 #                |---- other_dir/                                    (ino 284)
216 #                            |---- below_ance/                       (ino 279)
217 #                            |            |---- pre/                 (ino 280)
218 #                            |
219 #                            |---- wait_dir/                         (ino 281)
220 #                                       |---- desc/                  (ino 282)
221 #                                               |---- ance/          (ino 283)
222 #
223 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap2
224
225 run_check $FSSUM_PROG -A -f -w $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
226 run_check $FSSUM_PROG -A -f -w $send_files_dir/2.fssum \
227         -x $SCRATCH_MNT/mysnap2/mysnap1 $SCRATCH_MNT/mysnap2
228
229 _run_btrfs_util_prog send -f $send_files_dir/1.snap $SCRATCH_MNT/mysnap1
230 _run_btrfs_util_prog send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
231         $SCRATCH_MNT/mysnap2
232
233 # Now recreate the filesystem by receiving both send streams and verify we get
234 # the same content that the original filesystem had.
235 _scratch_unmount
236 _scratch_mkfs >>$seqres.full 2>&1
237 _scratch_mount
238
239 _run_btrfs_util_prog receive -f $send_files_dir/1.snap $SCRATCH_MNT
240 run_check $FSSUM_PROG -r $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
241 _run_btrfs_util_prog receive -f $send_files_dir/2.snap $SCRATCH_MNT
242 run_check $FSSUM_PROG -r $send_files_dir/2.fssum $SCRATCH_MNT/mysnap2
243
244 echo "Silence is golden"
245 status=0
246 exit