fsx/fsstress: round blocksize properly
[xfstests-dev.git] / tests / generic / 186
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 186
6 #
7 # Ensuring that copy on write in buffered mode works when free space
8 # is heavily fragmented.
9 #   - Create two files
10 #   - Reflink the odd blocks of the first file into a third file.
11 #   - Reflink the even blocks of the second file into the third file.
12 #   - Try to fragment the free space by allocating a huge file and
13 #     punching out every other block.
14 #   - CoW across the halfway mark.
15 #   - Check that the files are now different where we say they're different.
16 #
17 seq=`basename $0`
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
20
21 here=`pwd`
22 tmp=/tmp/$$
23 status=1    # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
25
26 _cleanup()
27 {
28     cd /
29 #    rm -rf $tmp.* $testdir
30 }
31
32 # get standard environment, filters and checks
33 . ./common/rc
34 . ./common/filter
35 . ./common/reflink
36
37 # real QA test starts here
38 _require_scratch_reflink
39 _require_cp_reflink
40 _require_xfs_io_command "falloc"
41 _require_xfs_io_command "fpunch"
42 test $FSTYP = "btrfs" && _notrun "Can't fragment free space on btrfs."
43
44 rm -f $seqres.full
45
46 _fragment_freesp()
47 {
48         file=$1
49
50         # consume nearly all available space (leave ~1MB)
51         avail=`_get_available_space $SCRATCH_MNT`
52         echo "$avail bytes left"
53         filesize=$((avail - 1048576))
54         $XFS_IO_PROG -fc "truncate $filesize" $file
55
56         chunks=20
57         chunksizemb=$((filesize / chunks / 1048576))
58         seq 1 $chunks | while read f; do
59                 echo "$((f * chunksizemb)) file size $f / 20"
60                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
61         done
62
63         chunks=100
64         chunksizemb=$((filesize / chunks / 1048576))
65         seq 80 $chunks | while read f; do
66                 echo "$((f * chunksizemb)) file size $f / $chunks"
67                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
68         done
69
70         filesizemb=$((filesize / 1048576))
71         $XFS_IO_PROG -fc "falloc -k 0 ${filesizemb}m" $file
72
73         # Try again anyway
74         avail=`_get_available_space $SCRATCH_MNT`
75         $XFS_IO_PROG -fc "pwrite -S 0x65 0 $avail" ${file}
76
77         # Punch out whatever we need
78         seq 1 $((nr * 4)) | while read f; do
79                 $XFS_IO_PROG -f -c "fpunch $((f * 2 * blksz)) $blksz" $file
80         done
81 }
82
83 echo "Format and mount"
84 _scratch_mkfs > $seqres.full 2>&1
85 _scratch_mount >> $seqres.full 2>&1
86
87 testdir=$SCRATCH_MNT/test-$seq
88 mkdir $testdir
89
90 echo "Create the original files"
91 blksz=65536
92 nr=1024
93 filesize=$((blksz * nr))
94 _pwrite_byte 0x61 0 $filesize $testdir/file1 >> $seqres.full
95 _pwrite_byte 0x62 0 $filesize $testdir/file2 >> $seqres.full
96 seq 0 2 $((nr-1)) | while read f; do
97         _reflink_range $testdir/file1 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
98         _pwrite_byte 0x61 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
99 done
100 seq 1 2 $((nr-1)) | while read f; do
101         _reflink_range $testdir/file2 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
102         _pwrite_byte 0x62 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
103 done
104 _scratch_cycle_mount
105 _fragment_freesp $testdir/bigfile >> $seqres.full 2>&1
106 filesize=$((blksz * nr))
107 _scratch_cycle_mount
108
109 echo "Compare files"
110 md5sum $testdir/file1 | _filter_scratch
111 md5sum $testdir/file2 | _filter_scratch
112 md5sum $testdir/file3 | _filter_scratch
113 md5sum $testdir/file3.chk | _filter_scratch
114
115 echo "CoW with multiple extents?"
116 cowoff=$((filesize / 4))
117 cowsz=$((filesize / 2))
118 $XFS_IO_PROG -f -c "pwrite -S 0x63 -b $cowsz $cowoff $cowsz" $testdir/file3 >> $seqres.full
119 _pwrite_byte 0x63 $cowoff $cowsz $testdir/file3.chk >> $seqres.full
120 _scratch_cycle_mount
121
122 echo "Compare files"
123 md5sum $testdir/file1 | _filter_scratch
124 md5sum $testdir/file2 | _filter_scratch
125 md5sum $testdir/file3 | _filter_scratch
126 md5sum $testdir/file3.chk | _filter_scratch
127
128 # success, all done
129 status=0
130 exit