xfs/007: fix regressions on V4 filesystems
[xfstests-dev.git] / tests / xfs / 507
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2019 Oracle, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 507
6 #
7 # Try to overflow i_delayed_blks by setting the largest cowextsize hint
8 # possible, creating a sparse file with a single byte every cowextsize bytes,
9 # reflinking it, and retouching every written byte to see if we can create
10 # enough speculative COW reservations to overflow i_delayed_blks.
11 #
12 . ./common/preamble
13 _begin_fstest clone
14
15 _register_cleanup "_cleanup" BUS
16
17 # Override the default cleanup function.
18 _cleanup()
19 {
20         cd /
21         test -n "$loop_mount" && $UMOUNT_PROG $loop_mount > /dev/null 2>&1
22         test -n "$loop_dev" && _destroy_loop_device $loop_dev
23         rm -rf $tmp.*
24 }
25
26 # Import common functions.
27 . ./common/reflink
28 . ./common/filter
29
30 # real QA test starts here
31 _supported_fs xfs
32 _require_scratch_reflink
33 _require_cp_reflink
34 _require_loop
35 _require_xfs_debug      # needed for xfs_bmap -c
36
37 MAXEXTLEN=2097151       # cowextsize can't be more than MAXEXTLEN
38
39 echo "Format and mount"
40 _scratch_mkfs > "$seqres.full" 2>&1
41 _scratch_mount
42
43 # Create a huge sparse filesystem on the scratch device because that's what
44 # we're going to need to guarantee that we have enough blocks to overflow in
45 # the first place.  We need to have at least enough free space on that huge fs
46 # to handle one written block every MAXEXTLEN blocks and to reserve 2^32 blocks
47 # in the COW fork.  There needs to be sufficient space in the scratch
48 # filesystem to handle a 256M log, all the per-AG metadata, and all the data
49 # written to the test file.
50 #
51 # Worst case, a 64k-block fs needs to be about 300TB.  Best case, a 1k block
52 # filesystem needs ~5TB.  For the most common 4k case we only need a ~20TB fs.
53 #
54 # In practice, the author observed that the space required on the scratch fs
55 # never exceeded ~800M even for a 300T 6k-block filesystem, so we'll just ask
56 # for about 1.2GB.
57 blksz=$(_get_file_block_size "$SCRATCH_MNT")
58 nr_cows="$(( ((2 ** 32) / MAXEXTLEN) + 100 ))"
59 blks_needed="$(( nr_cows * (1 + MAXEXTLEN) ))"
60 loop_file_sz="$(( ((blksz * blks_needed) * 12 / 10) / 512 * 512 ))"
61 _require_fs_space $SCRATCH_MNT 1234567
62
63 loop_file=$SCRATCH_MNT/a.img
64 loop_mount=$SCRATCH_MNT/a
65 $XFS_IO_PROG -f -c "truncate $loop_file_sz" $loop_file
66 test -s $loop_file || _notrun "Could not create large sparse file"
67 loop_dev=$(_create_loop_device $loop_file)
68
69 # Now we have to create the source file.  The goal is to overflow a 32-bit
70 # i_delayed_blks, which means that we have to create at least that many delayed
71 # allocation block reservations.  Take advantage of the fact that a cowextsize
72 # hint causes creation of large speculative delalloc reservations in the cow
73 # fork to reduce the amount of work we have to do.
74 #
75 # The maximum cowextsize can only be set to MAXEXTLEN fs blocks on a filesystem
76 # whose AGs each have more than MAXEXTLEN * 2 blocks.  This we can do easily
77 # with a multi-terabyte filesystem, so start by setting up the hint.  Note that
78 # the current fsxattr interface specifies its u32 cowextsize hint in units of
79 # bytes and therefore can't handle MAXEXTLEN * blksz on most filesystems, so we
80 # set it via mkfs because mkfs takes units of fs blocks, not bytes.
81
82 _mkfs_dev -d cowextsize=$MAXEXTLEN -l size=256m $loop_dev >> $seqres.full
83 mkdir $loop_mount
84 mount $loop_dev $loop_mount
85
86 echo "Create crazy huge file"
87 huge_file="$loop_mount/a"
88 touch "$huge_file"
89 blksz=$(_get_file_block_size "$loop_mount")
90 extsize_bytes="$(( MAXEXTLEN * blksz ))"
91
92 # Make sure it actually set a hint.
93 curr_cowextsize_str="$($XFS_IO_PROG -c 'cowextsize' "$huge_file")"
94 echo "$curr_cowextsize_str" >> $seqres.full
95 cowextsize_bytes="$(echo "$curr_cowextsize_str" | sed -e 's/^.\([0-9]*\).*$/\1/g')"
96 test "$cowextsize_bytes" -eq 0 && echo "could not set cowextsize?"
97
98 # Now we have to seed the file with sparse contents.  Remember, the goal is to
99 # create a little more than 2^32 delayed allocation blocks in the COW fork with
100 # as little effort as possible.  We know that speculative COW preallocation
101 # will create MAXEXTLEN-length reservations for us, so that means we should
102 # be able to get away with touching a single byte every extsize_bytes.  We
103 # do this backwards to avoid having to move EOF.
104 seq $nr_cows -1 0 | while read n; do
105         off="$((n * extsize_bytes))"
106         $XFS_IO_PROG -c "pwrite $off 1" "$huge_file" > /dev/null
107 done
108
109 echo "Reflink crazy huge file"
110 _cp_reflink "$huge_file" "$huge_file.b"
111
112 # Now that we've shared all the blocks in the file, we touch them all again
113 # to create speculative COW preallocations.
114 echo "COW crazy huge file"
115 seq $nr_cows -1 0 | while read n; do
116         off="$((n * extsize_bytes))"
117         $XFS_IO_PROG -c "pwrite $off 1" "$huge_file" > /dev/null
118 done
119
120 # Compare the number of blocks allocated to this file (as reported by stat)
121 # against the number of blocks that are in the COW fork.  If either one is
122 # less than 2^32 then we have evidence of an overflow problem.
123 echo "Check crazy huge file"
124 allocated_stat_blocks="$(stat -c %b "$huge_file")"
125 stat_blksz="$(stat -c %B "$huge_file")"
126 allocated_fsblocks=$(( allocated_stat_blocks * stat_blksz / blksz ))
127
128 # Make sure we got enough COW reservations to overflow a 32-bit counter.
129
130 # Return the number of delalloc & real blocks given bmap output for a fork of a
131 # file.  Output is in units of 512-byte blocks.
132 count_fork_blocks() {
133         $AWK_PROG "
134 {
135         if (\$3 == \"delalloc\") {
136                 x += \$4;
137         } else if (\$3 == \"hole\") {
138                 ;
139         } else {
140                 x += \$6;
141         }
142 }
143 END {
144         print(x);
145 }
146 "
147 }
148
149 # Count the number of blocks allocated to a file based on the xfs_bmap output.
150 # Output is in units of filesystem blocks.
151 count_file_fork_blocks() {
152         local tag="$1"
153         local file="$2"
154         local args="$3"
155
156         $XFS_IO_PROG -c "bmap $args -l -p -v" "$huge_file" > $tmp.extents
157         echo "$tag fork map" >> $seqres.full
158         cat $tmp.extents >> $seqres.full
159         local sectors="$(count_fork_blocks < $tmp.extents)"
160         echo "$(( sectors / (blksz / 512) ))"
161 }
162
163 cowblocks=$(count_file_fork_blocks cow "$huge_file" "-c")
164 attrblocks=$(count_file_fork_blocks attr "$huge_file" "-a")
165 datablocks=$(count_file_fork_blocks data "$huge_file" "")
166
167 # Did we create more than 2^32 blocks in the cow fork?
168 # Make sure the test actually set us up for the overflow.
169 echo "datablocks is $datablocks" >> $seqres.full
170 echo "attrblocks is $attrblocks" >> $seqres.full
171 echo "cowblocks is $cowblocks" >> $seqres.full
172 test "$cowblocks" -lt $((2 ** 32)) && \
173         echo "cowblocks (${cowblocks}) should be more than 2^32!"
174
175 # Does stat's block allocation count exceed 2^32?
176 # This is how we detect the incore delalloc count overflow.
177 echo "stat blocks is $allocated_fsblocks" >> $seqres.full
178 test "$allocated_fsblocks" -lt $((2 ** 32)) && \
179         echo "stat blocks (${allocated_fsblocks}) should be more than 2^32!"
180
181 # Finally, does st_blocks match what we computed from the forks?
182 # Sanity check the values computed from the forks.
183 expected_allocated_fsblocks=$((datablocks + cowblocks + attrblocks))
184 echo "expected stat blocks is $expected_allocated_fsblocks" >> $seqres.full
185
186 _within_tolerance "st_blocks" $allocated_fsblocks $expected_allocated_fsblocks 2% -v
187
188 echo "Test done"
189 # Quick check the large sparse fs, but skip xfs_db because it doesn't scale
190 # well on a multi-terabyte filesystem.
191 LARGE_SCRATCH_DEV=yes _check_xfs_filesystem $loop_dev none none
192
193 # success, all done
194 status=0
195 exit