btrfs: test incremental send after a succession of rename and link operations
[xfstests-dev.git] / tests / btrfs / 221
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2020 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/221
6 #
7 # Test that an incremental send operation emits the correct path for link and
8 # rename operation after swapping the names and locations of several inodes in
9 # a way that creates a nasty dependency of rename and link operations. Notably
10 # one file has its name and location swapped with a directory for which it used
11 # to have a directory entry in it.
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 touch $SCRATCH_MNT/a
48 touch $SCRATCH_MNT/b
49 mkdir $SCRATCH_MNT/testdir
50 # We want "a" to have a lower inode number than its parent directory, so it
51 # was created before the directory and then moved into it.
52 mv $SCRATCH_MNT/a $SCRATCH_MNT/testdir/a
53
54 # Filesystem looks like:
55 #
56 # .                                                      (ino 256)
57 # |----- testdir/                                        (ino 259)
58 # |          |----- a                                    (ino 257)
59 # |
60 # |----- b                                               (ino 258)
61 #
62 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
63         $SCRATCH_MNT/mysnap1 > /dev/null
64
65 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
66         $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
67
68 # Now rename 259 to "testdir_2", then change the name of 257 to "testdir" and
69 # make it a direct descendant of the root inode (256). Also create a new link
70 # for inode 257 with the old name of inode 258. By swapping the names and
71 # location of several inodes and create a nasty dependency chain of rename and
72 # link operations.
73 mv $SCRATCH_MNT/testdir/a $SCRATCH_MNT/a2
74 touch $SCRATCH_MNT/testdir/a
75 mv $SCRATCH_MNT/b $SCRATCH_MNT/b2
76 ln $SCRATCH_MNT/a2 $SCRATCH_MNT/b
77 mv $SCRATCH_MNT/testdir $SCRATCH_MNT/testdir_2
78 mv $SCRATCH_MNT/a2 $SCRATCH_MNT/testdir
79
80 # Filesystem now looks like:
81 #
82 # .                                                      (ino 256)
83 # |----- testdir_2/                                      (ino 259)
84 # |          |----- a                                    (ino 260)
85 # |
86 # |----- testdir                                         (ino 257)
87 # |----- b                                               (ino 257)
88 # |----- b2                                              (ino 258)
89 #
90
91 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
92                  $SCRATCH_MNT/mysnap2 > /dev/null
93 $BTRFS_UTIL_PROG send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
94                  $SCRATCH_MNT/mysnap2 2>&1 1>/dev/null | _filter_scratch
95
96 $FSSUM_PROG -A -f -w $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
97 $FSSUM_PROG -A -f -w $send_files_dir/2.fssum \
98         -x $SCRATCH_MNT/mysnap2/mysnap1 $SCRATCH_MNT/mysnap2
99
100 # Now recreate the filesystem by receiving both send streams and verify we get
101 # the same content that the original filesystem had.
102 _scratch_unmount
103 _scratch_mkfs >>$seqres.full 2>&1
104 _scratch_mount
105
106 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
107
108 # The receive operation below used to fail because when attemping to create the
109 # hard link named "b" for inode 257, the link operation contained a target path
110 # of "o259-6-0/a", which caused the receiver process to fail because inode 259
111 # was not yet orphanized (renamed to "o259-6-0"), it still had the name "testdir"
112 # when the link operation was issued.
113 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
114
115 $FSSUM_PROG -r $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
116 $FSSUM_PROG -r $send_files_dir/2.fssum $SCRATCH_MNT/mysnap2
117
118 status=0
119 exit