generic: test MADV_POPULATE_READ with IO errors
[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 . ./common/preamble
18 _begin_fstest auto clone punch
19
20 # Override the default cleanup function.
21 _cleanup()
22 {
23     cd /
24 #    rm -rf $tmp.* $testdir
25 }
26
27 # Import common functions.
28 . ./common/filter
29 . ./common/reflink
30
31 # real QA test starts here
32 _require_scratch_reflink
33 _require_cp_reflink
34 _require_xfs_io_command "falloc"
35 _require_xfs_io_command "fpunch"
36 test $FSTYP = "btrfs" && _notrun "Can't fragment free space on btrfs."
37
38 _fragment_freesp()
39 {
40         file=$1
41
42         # consume nearly all available space (leave ~1MB)
43         avail=`_get_available_space $SCRATCH_MNT`
44         echo "$avail bytes left"
45         filesize=$((avail - 1048576))
46         $XFS_IO_PROG -fc "truncate $filesize" $file
47
48         chunks=20
49         chunksizemb=$((filesize / chunks / 1048576))
50         seq 1 $chunks | while read f; do
51                 echo "$((f * chunksizemb)) file size $f / 20"
52                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
53         done
54
55         chunks=100
56         chunksizemb=$((filesize / chunks / 1048576))
57         seq 80 $chunks | while read f; do
58                 echo "$((f * chunksizemb)) file size $f / $chunks"
59                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
60         done
61
62         filesizemb=$((filesize / 1048576))
63         $XFS_IO_PROG -fc "falloc -k 0 ${filesizemb}m" $file
64
65         # Try again anyway
66         avail=`_get_available_space $SCRATCH_MNT`
67         $XFS_IO_PROG -fc "pwrite -S 0x65 0 $avail" ${file}
68
69         # Punch out whatever we need
70         seq 1 $((nr * 4)) | while read f; do
71                 $XFS_IO_PROG -f -c "fpunch $((f * 2 * blksz)) $blksz" $file
72         done
73 }
74
75 echo "Format and mount"
76 _scratch_mkfs > $seqres.full 2>&1
77 _scratch_mount >> $seqres.full 2>&1
78
79 testdir=$SCRATCH_MNT/test-$seq
80 mkdir $testdir
81
82 echo "Create the original files"
83 blksz=65536
84 nr=1024
85 filesize=$((blksz * nr))
86 _pwrite_byte 0x61 0 $filesize $testdir/file1 >> $seqres.full
87 _pwrite_byte 0x62 0 $filesize $testdir/file2 >> $seqres.full
88 seq 0 2 $((nr-1)) | while read f; do
89         _reflink_range $testdir/file1 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
90         _pwrite_byte 0x61 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
91 done
92 seq 1 2 $((nr-1)) | while read f; do
93         _reflink_range $testdir/file2 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
94         _pwrite_byte 0x62 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
95 done
96 _scratch_cycle_mount
97 _fragment_freesp $testdir/bigfile >> $seqres.full 2>&1
98 filesize=$((blksz * nr))
99 _scratch_cycle_mount
100
101 echo "Compare files"
102 md5sum $testdir/file1 | _filter_scratch
103 md5sum $testdir/file2 | _filter_scratch
104 md5sum $testdir/file3 | _filter_scratch
105 md5sum $testdir/file3.chk | _filter_scratch
106
107 echo "CoW with multiple extents?"
108 cowoff=$((filesize / 4))
109 cowsz=$((filesize / 2))
110 $XFS_IO_PROG -f -c "pwrite -S 0x63 -b $cowsz $cowoff $cowsz" $testdir/file3 >> $seqres.full
111 _pwrite_byte 0x63 $cowoff $cowsz $testdir/file3.chk >> $seqres.full
112 _scratch_cycle_mount
113
114 echo "Compare files"
115 md5sum $testdir/file1 | _filter_scratch
116 md5sum $testdir/file2 | _filter_scratch
117 md5sum $testdir/file3 | _filter_scratch
118 md5sum $testdir/file3.chk | _filter_scratch
119
120 # success, all done
121 status=0
122 exit