generic: test MADV_POPULATE_READ with IO errors
[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 . ./common/preamble
14 _begin_fstest auto quick send
15
16 # Override the default cleanup function.
17 _cleanup()
18 {
19         cd /
20         rm -fr $send_files_dir
21         rm -f $tmp.*
22 }
23
24 # Import common functions.
25 . ./common/filter
26
27 # real QA test starts here
28 _supported_fs btrfs
29 _require_test
30 _require_scratch
31 _require_fssum
32
33 send_files_dir=$TEST_DIR/btrfs-test-$seq
34
35 rm -fr $send_files_dir
36 mkdir $send_files_dir
37
38 _scratch_mkfs >>$seqres.full 2>&1
39 _scratch_mount
40
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
45
46 mkdir $SCRATCH_MNT/A
47 ln $SCRATCH_MNT/file3 $SCRATCH_MNT/A/hard_link
48
49 # Filesystem looks like:
50 #
51 # .                                     (ino 256)
52 # |----- file1                          (ino 257)
53 # |----- file2                          (ino 258)
54 # |----- file3                          (ino 259)
55 # |----- A/                             (ino 260)
56 #        |---- hard_link                (ino 259)
57 #
58
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
63
64 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
65         $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
66
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
70
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
75
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
80
81 # Filesystem now looks like:
82 #
83 # .                                     (ino 256)
84 # |----- tmp                            (ino 259)
85 # |----- file3                          (ino 259)
86 # |----- B/                             (ino 260)
87 # |      |---- file1                    (ino 257)
88 # |      |---- hard_link                (ino 258)
89 # |
90 # |----- A                              (ino 258)
91
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
97
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
101
102 # Now recreate the filesystem by receiving both send streams and verify we get
103 # the same content that the original filesystem had.
104 _scratch_unmount
105 _scratch_mkfs >>$seqres.full 2>&1
106 _scratch_mount
107
108 # First add the first snapshot to the new filesystem by applying the first send
109 # stream.
110 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
111
112 # The incremental receive operation below used to fail with the following error:
113 #
114 #    ERROR: unlink A/hard_link failed: No such file or directory
115 #
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
118 # caches that path.
119 #
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
123 # "/o260-6-0".
124 #
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
134 # above error.
135 #
136 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
137
138 $FSSUM_PROG -r $send_files_dir/1.fssum $SCRATCH_MNT/mysnap1
139 $FSSUM_PROG -r $send_files_dir/2.fssum $SCRATCH_MNT/mysnap2
140
141 status=0
142 exit