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