generic: test for file loss after mix of rename, fsync and inode eviction
[xfstests-dev.git] / tests / generic / 187
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. 187
6 #
7 # Ensuring that copy on write in directio 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 _require_odirect
38
39 _fragment_freesp()
40 {
41         file=$1
42
43         # consume nearly all available space (leave ~1MB)
44         avail=`_get_available_space $SCRATCH_MNT`
45         echo "$avail bytes left"
46         filesize=$((avail - 1048576))
47         $XFS_IO_PROG -fc "truncate $filesize" $file
48
49         chunks=20
50         chunksizemb=$((filesize / chunks / 1048576))
51         seq 1 $chunks | while read f; do
52                 echo "$((f * chunksizemb)) file size $f / 20"
53                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
54         done
55
56         chunks=100
57         chunksizemb=$((filesize / chunks / 1048576))
58         seq 80 $chunks | while read f; do
59                 echo "$((f * chunksizemb)) file size $f / $chunks"
60                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
61         done
62
63         filesizemb=$((filesize / 1048576))
64         $XFS_IO_PROG -fc "falloc -k 0 ${filesizemb}m" $file
65
66         # Try again anyway
67         avail=`_get_available_space $SCRATCH_MNT`
68         $XFS_IO_PROG -fc "pwrite -S 0x65 0 $avail" ${file}
69
70         # Punch out whatever we need
71         seq 1 $((nr * 4)) | while read f; do
72                 $XFS_IO_PROG -f -c "fpunch $((f * 2 * blksz)) $blksz" $file
73         done
74 }
75
76 echo "Format and mount"
77 _scratch_mkfs > $seqres.full 2>&1
78 _scratch_mount >> $seqres.full 2>&1
79
80 testdir=$SCRATCH_MNT/test-$seq
81 mkdir $testdir
82
83 echo "Create the original files"
84 blksz=65536
85 nr=1024
86 filesize=$((blksz * nr))
87 _pwrite_byte 0x61 0 $filesize $testdir/file1 >> $seqres.full
88 _pwrite_byte 0x62 0 $filesize $testdir/file2 >> $seqres.full
89 seq 0 2 $((nr-1)) | while read f; do
90         _reflink_range $testdir/file1 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
91         _pwrite_byte 0x61 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
92 done
93 seq 1 2 $((nr-1)) | while read f; do
94         _reflink_range $testdir/file2 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
95         _pwrite_byte 0x62 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
96 done
97 _scratch_cycle_mount
98 _fragment_freesp $testdir/bigfile >> $seqres.full 2>&1
99 filesize=$((blksz * nr))
100 _scratch_cycle_mount
101
102 echo "Compare files"
103 md5sum $testdir/file1 | _filter_scratch
104 md5sum $testdir/file2 | _filter_scratch
105 md5sum $testdir/file3 | _filter_scratch
106 md5sum $testdir/file3.chk | _filter_scratch
107
108 echo "CoW with multiple extents?"
109 cowoff=$((filesize / 4))
110 cowsz=$((filesize / 2))
111 $XFS_IO_PROG -d -f -c "pwrite -S 0x63 -b $cowsz $cowoff $cowsz" $testdir/file3 >> $seqres.full
112 _pwrite_byte 0x63 $cowoff $cowsz $testdir/file3.chk >> $seqres.full
113 _scratch_cycle_mount
114
115 echo "Compare files"
116 md5sum $testdir/file1 | _filter_scratch
117 md5sum $testdir/file2 | _filter_scratch
118 md5sum $testdir/file3 | _filter_scratch
119 md5sum $testdir/file3.chk | _filter_scratch
120
121 # success, all done
122 status=0
123 exit