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