2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 SUSE Linux Products GmbH. All Rights Reserved.
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.
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
15 status=1 # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
25 # get standard environment, filters and checks
31 # real QA test starts here
34 _require_dm_target flakey
36 _require_xfs_io_command "fpunch"
37 _require_btrfs_fs_feature "no_holes"
38 _require_btrfs_mkfs_feature "no-holes"
43 run_test_leading_hole()
45 # We create the filesystem with a node size of 64Kb because we need to
46 # create a specific metadata layout in order to trigger the bug we are
47 # testing. At the moment the node size can not be smaller then the system's
48 # page size, so given that the largest possible page size is 64Kb and by
49 # default the node size is set to the system's page size value, we explicitly
50 # create a filesystem with a 64Kb node size, so that the test can run
51 # reliably independently of the system's page size.
52 _scratch_mkfs -O no-holes -n $((64 * 1024)) >>$seqres.full 2>&1
53 _require_metadata_journaling $SCRATCH_DEV
57 # Create our first file, which is used just to fill space in a leaf. Its
58 # items ocuppy most of the first leaf. We use a large xattr since it's an
59 # easy and fast way to fill a lot of leaf space.
60 touch $SCRATCH_MNT/foo
61 $SETFATTR_PROG -n 'user.x1' -v $(printf '%0.sX' $(seq 1 63617)) \
64 # Create our second file, which we will use to test if fsync persists a hole
65 # punch operation against it. Create several extent items for the file, all with
66 # a size of 64Kb. The first extent item of this file is located within the first
67 # leaf of the fs tree, as its last item, and all the remaining extent items in
70 for ((i = 0; i <= 10; i++)); do
71 $XFS_IO_PROG -f -d -c "pwrite -S 0xab -b 64K $offset 64K" \
72 $SCRATCH_MNT/bar >/dev/null
73 offset=$(($offset + 64 * 1024))
76 # Make sure everything done so far is durably persisted. We also want to start
77 # a new transaction and bump the filesystem generation. We don't fsync because
78 # we want to keep the 'full sync' flag in the inode of file 'bar', so that the
79 # fsync after the hole punch operation uses the slow path, which is necessary
80 # to trigger the bug we are testing.
83 # Now punch a hole that covers only the first extent item of file bar. That
84 # is the only extent item in the first leaf of the first tree, so the hole
85 # punch operation will drop it and will not touch the second leaf which
86 # contains the remaining extent items. These conditions are necessary to
87 # trigger the bug we are testing.
88 $XFS_IO_PROG -c "fpunch 0 64K" -c "fsync" $SCRATCH_MNT/bar
90 echo "File digest before power failure:"
91 md5sum $SCRATCH_MNT/bar | _filter_scratch
92 # Simulate a power failure and mount the filesystem to check that replaying the
93 # log tree succeeds and our file bar has the expected content.
94 _flakey_drop_and_remount
95 echo "File digest after power failure and log replay:"
96 md5sum $SCRATCH_MNT/bar | _filter_scratch
102 run_test_middle_hole()
107 # We create the filesystem with a node size of 64Kb because we need to
108 # create a specific metadata layout in order to trigger the bug we are
109 # testing. At the moment the node size can not be smaller then the system's
110 # page size, so given that the largest possible page size is 64Kb and by
111 # default the node size is set to the system's page size value, we explicitly
112 # create a filesystem with a 64Kb node size, so that the test can run
113 # reliably independently of the system's page size.
114 _scratch_mkfs -O no-holes -n $((64 * 1024)) >>$seqres.full 2>&1
115 _require_metadata_journaling $SCRATCH_DEV
119 # Create our first file, which is used just to fill space in a leaf. Its
120 # items ocuppy most of the first leaf. We use a large xattr since it's an
121 # easy and fast way to fill a lot of leaf space.
122 touch $SCRATCH_MNT/foo
123 $SETFATTR_PROG -n 'user.x1' -v $(printf '%0.sX' $(seq 1 63600)) \
126 # Create our second file, which we will use to test if fsync persists a hole
127 # punch operation against it. Create several extent items for the file, all
128 # with a size of 64Kb. The goal is to have the items of this file span 5
131 for ((i = 0; i <= 2000; i++)); do
132 $XFS_IO_PROG -f -d -c "pwrite -S 0xab -b 64K $offset 64K" \
133 $SCRATCH_MNT/bar >/dev/null
134 offset=$(($offset + 64 * 1024))
137 # Make sure everything done so far is durably persisted. We also want to
138 # start a new transaction and bump the filesystem generation. We don't fsync
139 # because we want to keep the 'full sync' flag in the inode of file 'bar',
140 # so that the fsync after the hole punch operation uses the slow path, which
141 # is necessary to trigger the bug we are testing.
144 # Now punch a hole that covers the entire 3rd leaf. This results in deleting
145 # the entire leaf, without touching the 2nd and 4th leafs. The first leaf is
146 # touched because the inode item needs to be updated (bytes used, ctime,
147 # mtime, etc). After that we modify the last extent, located at the 5th leaf,
148 # and then fsync the file. We want to verify that both the hole and the new
149 # data are correctly persisted by the fsync.
150 $XFS_IO_PROG -c "fpunch $hole_offset $hole_len" \
151 -c "pwrite -S 0xf1 131072000 64K" \
152 -c "fsync" $SCRATCH_MNT/bar >/dev/null
154 echo "File digest before power failure:"
155 md5sum $SCRATCH_MNT/bar | _filter_scratch
156 # Simulate a power failure and mount the filesystem to check that replaying
157 # the log tree succeeds and our file bar has the expected content.
158 _flakey_drop_and_remount
159 echo "File digest after power failure and log replay:"
160 md5sum $SCRATCH_MNT/bar | _filter_scratch
167 echo "Testing with hole offset 0 hole length 65536"
168 run_test_leading_hole
170 # Now test cases where file extent items span many leafs and we punch a large
171 # hole in the middle of the file, with the goal of getting an entire leaf
172 # deleted during the punch hole operation. We test 3 different ranges because
173 # depending on whether SELinux is enabled or not the layout of the items in
174 # the leafs varies slightly.
177 echo "Testing with hole offset 786432 hole length 54919168"
178 run_test_middle_hole 786432 54919168
181 echo "Testing with hole offset 720896 hole length 54919168"
182 run_test_middle_hole 720896 54919168
185 echo "Testing with hole offset 655360 hole length 54919168"
186 run_test_middle_hole 655360 54919168