generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / btrfs / 201
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 201
6 #
7 # Test that when we have the no-holes feature enabled and a specific metadata
8 # layout, if we punch a hole that starts at file offset 0 and fsync the file,
9 # after replaying the log the hole exists.
10 #
11 . ./common/preamble
12 _begin_fstest auto quick punch log
13
14 # Override the default cleanup function.
15 _cleanup()
16 {
17         _cleanup_flakey
18         cd /
19         rm -f $tmp.*
20 }
21
22 # Import common functions.
23 . ./common/attr
24 . ./common/filter
25 . ./common/dmflakey
26
27 # real QA test starts here
28 _supported_fs btrfs
29 _require_scratch
30 _require_dm_target flakey
31 _require_attrs
32 _require_xfs_io_command "fpunch"
33 _require_btrfs_fs_feature "no_holes"
34 _require_btrfs_mkfs_feature "no-holes"
35 _require_odirect
36
37 run_test_leading_hole()
38 {
39     # We create the filesystem with a node size of 64Kb because we need to
40     # create a specific metadata layout in order to trigger the bug we are
41     # testing. At the moment the node size can not be smaller then the system's
42     # page size, so given that the largest possible page size is 64Kb and by
43     # default the node size is set to the system's page size value, we explicitly
44     # create a filesystem with a 64Kb node size, so that the test can run
45     # reliably independently of the system's page size.
46     _scratch_mkfs -O no-holes -n $((64 * 1024)) >>$seqres.full 2>&1
47     _require_metadata_journaling $SCRATCH_DEV
48     _init_flakey
49     _mount_flakey
50
51     # Create our first file, which is used just to fill space in a leaf. Its
52     # items ocuppy most of the first leaf. We use a large xattr since it's an
53     # easy and fast way to fill a lot of leaf space.
54     touch $SCRATCH_MNT/foo
55     $SETFATTR_PROG -n 'user.x1' -v $(printf '%0.sX' $(seq 1 63617)) \
56         $SCRATCH_MNT/foo
57
58    # Create our second file, which we will use to test if fsync persists a hole
59    # punch operation against it. Create several extent items for the file, all with
60    # a size of 64Kb. The first extent item of this file is located within the first
61    # leaf of the fs tree, as its last item, and all the remaining extent items in
62    # another leaf.
63    local offset=0
64    for ((i = 0; i <= 10; i++)); do
65        $XFS_IO_PROG -f -d -c "pwrite -S 0xab -b 64K $offset 64K" \
66            $SCRATCH_MNT/bar >/dev/null
67        offset=$(($offset + 64 * 1024))
68    done
69
70    # Make sure everything done so far is durably persisted. We also want to start
71    # a new transaction and bump the filesystem generation. We don't fsync because
72    # we want to keep the 'full sync' flag in the inode of file 'bar', so that the
73    # fsync after the hole punch operation uses the slow path, which is necessary
74    # to trigger the bug we are testing.
75    sync
76
77    # Now punch a hole that covers only the first extent item of file bar. That
78    # is the only extent item in the first leaf of the first tree, so the hole
79    # punch operation will drop it and will not touch the second leaf which
80    # contains the remaining extent items. These conditions are necessary to
81    # trigger the bug we are testing.
82    $XFS_IO_PROG -c "fpunch 0 64K" -c "fsync" $SCRATCH_MNT/bar
83
84    echo "File digest before power failure:"
85    md5sum $SCRATCH_MNT/bar | _filter_scratch
86    # Simulate a power failure and mount the filesystem to check that replaying the
87    # log tree succeeds and our file bar has the expected content.
88    _flakey_drop_and_remount
89    echo "File digest after power failure and log replay:"
90    md5sum $SCRATCH_MNT/bar | _filter_scratch
91
92    _unmount_flakey
93    _cleanup_flakey
94 }
95
96 run_test_middle_hole()
97 {
98     local hole_offset=$1
99     local hole_len=$2
100
101     # We create the filesystem with a node size of 64Kb because we need to
102     # create a specific metadata layout in order to trigger the bug we are
103     # testing. At the moment the node size can not be smaller then the system's
104     # page size, so given that the largest possible page size is 64Kb and by
105     # default the node size is set to the system's page size value, we explicitly
106     # create a filesystem with a 64Kb node size, so that the test can run
107     # reliably independently of the system's page size.
108     _scratch_mkfs -O no-holes -n $((64 * 1024)) >>$seqres.full 2>&1
109     _require_metadata_journaling $SCRATCH_DEV
110     _init_flakey
111     _mount_flakey
112
113     # Create our first file, which is used just to fill space in a leaf. Its
114     # items ocuppy most of the first leaf. We use a large xattr since it's an
115     # easy and fast way to fill a lot of leaf space.
116     touch $SCRATCH_MNT/foo
117     $SETFATTR_PROG -n 'user.x1' -v $(printf '%0.sX' $(seq 1 63600)) \
118         $SCRATCH_MNT/foo
119
120     # Create our second file, which we will use to test if fsync persists a hole
121     # punch operation against it. Create several extent items for the file, all
122     # with a size of 64Kb. The goal is to have the items of this file span 5
123     # btree leafs.
124     offset=0
125     for ((i = 0; i <= 2000; i++)); do
126         $XFS_IO_PROG -f -d -c "pwrite -S 0xab -b 64K $offset 64K" \
127             $SCRATCH_MNT/bar >/dev/null
128         offset=$(($offset + 64 * 1024))
129     done
130
131     # Make sure everything done so far is durably persisted. We also want to
132     # start a new transaction and bump the filesystem generation. We don't fsync
133     # because we want to keep the 'full sync' flag in the inode of file 'bar',
134     # so that the fsync after the hole punch operation uses the slow path, which
135     # is necessary to trigger the bug we are testing.
136     sync
137
138     # Now punch a hole that covers the entire 3rd leaf. This results in deleting
139     # the entire leaf, without touching the 2nd and 4th leafs. The first leaf is
140     # touched because the inode item needs to be updated (bytes used, ctime,
141     # mtime, etc). After that we modify the last extent, located at the 5th leaf,
142     # and then fsync the file. We want to verify that both the hole and the new
143     # data are correctly persisted by the fsync.
144     $XFS_IO_PROG -c "fpunch $hole_offset $hole_len" \
145                  -c "pwrite -S 0xf1 131072000 64K" \
146                  -c "fsync" $SCRATCH_MNT/bar >/dev/null
147
148     echo "File digest before power failure:"
149     md5sum $SCRATCH_MNT/bar | _filter_scratch
150     # Simulate a power failure and mount the filesystem to check that replaying
151     # the log tree succeeds and our file bar has the expected content.
152     _flakey_drop_and_remount
153     echo "File digest after power failure and log replay:"
154     md5sum $SCRATCH_MNT/bar | _filter_scratch
155
156     _unmount_flakey
157     _cleanup_flakey
158 }
159
160 echo "Testing with hole offset 0 hole length 65536"
161 run_test_leading_hole
162
163 # Now test cases where file extent items span many leafs and we punch a large
164 # hole in the middle of the file, with the goal of getting an entire leaf
165 # deleted during the punch hole operation. We test 3 different ranges because
166 # depending on whether SELinux is enabled or not the layout of the items in
167 # the leafs varies slightly.
168
169 echo
170 echo "Testing with hole offset 786432 hole length 54919168"
171 run_test_middle_hole 786432 54919168
172
173 echo
174 echo "Testing with hole offset 720896 hole length 54919168"
175 run_test_middle_hole 720896 54919168
176
177 echo
178 echo "Testing with hole offset 655360 hole length 54919168"
179 run_test_middle_hole 655360 54919168
180
181 status=0
182 exit