common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / tests / xfs / 231
1 #! /bin/bash
2 # FS QA Test No. 231
3 #
4 # Test recovery of unused CoW reservations:
5 # - Create two reflinked files.  Set extsz hint on second file.
6 # - Dirty a single byte on a number of CoW reservations in the second file.
7 # - Fsync to flush out the dirty pages.
8 # - Wait for the reclaim to run.
9 # - Write more and see how bad fragmentation is.
10 #
11 #-----------------------------------------------------------------------
12 # Copyright (c) 2016, Oracle and/or its affiliates.  All Rights Reserved.
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License as
16 # published by the Free Software Foundation.
17 #
18 # This program is distributed in the hope that it would be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write the Free Software Foundation,
25 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26 #-----------------------------------------------------------------------
27
28 seq=`basename $0`
29 seqres=$RESULT_DIR/$seq
30 echo "QA output created by $seq"
31
32 here=`pwd`
33 tmp=/tmp/$$
34 status=1    # failure is the default!
35 trap "_cleanup; exit \$status" 0 1 2 3 15
36
37 _cleanup()
38 {
39     cd /
40     echo $old_cow_lifetime > /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime
41     rm -rf $tmp.*
42 }
43
44 # get standard environment, filters and checks
45 . ./common/rc
46 . ./common/filter
47 . ./common/reflink
48
49 # real QA test starts here
50 _supported_os Linux
51 _supported_fs xfs
52 _require_scratch_reflink
53 _require_cp_reflink
54 _require_xfs_io_command "cowextsize"
55 _require_xfs_io_command "fiemap"
56
57 old_cow_lifetime=$(cat /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime)
58
59 rm -f $seqres.full
60
61 echo "Format and mount"
62 _scratch_mkfs > $seqres.full 2>&1
63 _scratch_mount >> $seqres.full 2>&1
64
65 testdir=$SCRATCH_MNT/test-$seq
66 mkdir $testdir
67
68 blksz=65536
69 nr=64
70 filesize=$((blksz * nr))
71 bufnr=2
72 bufsize=$((blksz * bufnr))
73
74 _require_fs_space $SCRATCH_MNT $((filesize / 1024 * 3 * 5 / 4))
75 real_blksz=$(_get_block_size $testdir)
76 internal_blks=$((filesize / real_blksz))
77
78 echo "Create the original files"
79 $XFS_IO_PROG -c "cowextsize $bufsize" $testdir
80 $XFS_IO_PROG -f -c "pwrite -S 0x61 -b $bufsize 0 $filesize" $testdir/file1 >> $seqres.full
81 $XFS_IO_PROG -f -c "pwrite -S 0x61 -b $bufsize 0 $filesize" $testdir/file2.chk >> $seqres.full
82 _cp_reflink $testdir/file1 $testdir/file2 >> $seqres.full
83 _scratch_cycle_mount
84
85 echo "Compare files"
86 md5sum $testdir/file1 | _filter_scratch
87 md5sum $testdir/file2 | _filter_scratch
88 md5sum $testdir/file2.chk | _filter_scratch
89
90 echo "CoW and leave leftovers"
91 echo 2 > /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime
92 seq 2 2 $((nr - 1)) | while read f; do
93         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f - 1)) 1" $testdir/file2 >> $seqres.full
94         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f - 1)) 1" $testdir/file2.chk >> $seqres.full
95 done
96 sync
97
98 echo "Wait for CoW expiration"
99 sleep 3
100
101 echo "Allocate free space"
102 for i in $(seq 1 32); do
103         $XFS_IO_PROG -f -c "falloc 0 1" $testdir/junk.$i >> $seqres.full
104 done
105 $XFS_IO_PROG -f -c "falloc 0 $filesize" $testdir/junk >> $seqres.full
106
107 echo "CoW and leave leftovers"
108 echo $old_cow_lifetime > /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime
109 seq 2 2 $((nr - 1)) | while read f; do
110         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f)) 1" $testdir/file2 >> $seqres.full
111         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f)) 1" $testdir/file2.chk >> $seqres.full
112 done
113 sync
114
115 echo "Compare files"
116 md5sum $testdir/file1 | _filter_scratch
117 md5sum $testdir/file2 | _filter_scratch
118 md5sum $testdir/file2.chk | _filter_scratch
119
120 echo "Check extent counts"
121 old_extents=$(_count_extents $testdir/file1)
122 new_extents=$(_count_extents $testdir/file2)
123
124 echo "old extents: $old_extents" >> $seqres.full
125 echo "new extents: $new_extents" >> $seqres.full
126 echo "maximum extents: $internal_blks" >> $seqres.full
127 test $new_extents -le $((3 * nr / bufnr)) || echo "file2 more fragmented than expected"
128
129 # success, all done
130 status=0
131 exit