xfs: Check for extent overflow when trivally adding a new extent
[xfstests-dev.git] / tests / xfs / 231
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 231
6 #
7 # Test recovery of unused CoW reservations:
8 # - Create two reflinked files.  Set extsz hint on second file.
9 # - Dirty a single byte on a number of CoW reservations in the second file.
10 # - Fsync to flush out the dirty pages.
11 # - Wait for the reclaim to run.
12 # - Write more and see how bad fragmentation is.
13 #
14 seq=`basename $0`
15 seqres=$RESULT_DIR/$seq
16 echo "QA output created by $seq"
17
18 here=`pwd`
19 tmp=/tmp/$$
20 status=1    # failure is the default!
21 trap "_cleanup; exit \$status" 0 1 2 3 15
22
23 _cleanup()
24 {
25     cd /
26     echo $old_cow_lifetime > /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime
27     rm -rf $tmp.*
28 }
29
30 # get standard environment, filters and checks
31 . ./common/rc
32 . ./common/filter
33 . ./common/reflink
34
35 # real QA test starts here
36 _supported_fs xfs
37 _require_scratch_reflink
38 _require_cp_reflink
39 _require_xfs_io_command "cowextsize"
40 _require_xfs_io_command "falloc"
41 _require_xfs_io_command "fiemap"
42
43 old_cow_lifetime=$(cat /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime)
44
45 rm -f $seqres.full
46
47 echo "Format and mount"
48 _scratch_mkfs > $seqres.full 2>&1
49 _scratch_mount >> $seqres.full 2>&1
50
51 testdir=$SCRATCH_MNT/test-$seq
52 mkdir $testdir
53
54 blksz=65536
55 nr=64
56 filesize=$((blksz * nr))
57 bufnr=2
58 bufsize=$((blksz * bufnr))
59
60 _require_fs_space $SCRATCH_MNT $((filesize / 1024 * 3 * 5 / 4))
61 real_blksz=$(_get_block_size $testdir)
62 internal_blks=$((filesize / real_blksz))
63
64 echo "Create the original files"
65 $XFS_IO_PROG -c "cowextsize $bufsize" $testdir
66 $XFS_IO_PROG -f -c "pwrite -S 0x61 -b $bufsize 0 $filesize" $testdir/file1 >> $seqres.full
67 $XFS_IO_PROG -f -c "pwrite -S 0x61 -b $bufsize 0 $filesize" $testdir/file2.chk >> $seqres.full
68 _cp_reflink $testdir/file1 $testdir/file2 >> $seqres.full
69 _scratch_cycle_mount
70
71 echo "Compare files"
72 md5sum $testdir/file1 | _filter_scratch
73 md5sum $testdir/file2 | _filter_scratch
74 md5sum $testdir/file2.chk | _filter_scratch
75
76 echo "CoW and leave leftovers"
77 echo 2 > /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime
78 seq 2 2 $((nr - 1)) | while read f; do
79         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f - 1)) 1" $testdir/file2 >> $seqres.full
80         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f - 1)) 1" $testdir/file2.chk >> $seqres.full
81 done
82 sync
83
84 echo "Wait for CoW expiration"
85 sleep 3
86
87 echo "Allocate free space"
88 for i in $(seq 1 32); do
89         $XFS_IO_PROG -f -c "falloc 0 1" $testdir/junk.$i >> $seqres.full
90 done
91 $XFS_IO_PROG -f -c "falloc 0 $filesize" $testdir/junk >> $seqres.full
92
93 echo "CoW and leave leftovers"
94 echo $old_cow_lifetime > /proc/sys/fs/xfs/speculative_cow_prealloc_lifetime
95 seq 2 2 $((nr - 1)) | while read f; do
96         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f)) 1" $testdir/file2 >> $seqres.full
97         $XFS_IO_PROG -f -c "pwrite -S 0x63 $((blksz * f)) 1" $testdir/file2.chk >> $seqres.full
98 done
99 sync
100
101 echo "Compare files"
102 md5sum $testdir/file1 | _filter_scratch
103 md5sum $testdir/file2 | _filter_scratch
104 md5sum $testdir/file2.chk | _filter_scratch
105
106 echo "Check extent counts"
107 old_extents=$(_count_extents $testdir/file1)
108 new_extents=$(_count_extents $testdir/file2)
109
110 echo "old extents: $old_extents" >> $seqres.full
111 echo "new extents: $new_extents" >> $seqres.full
112 echo "maximum extents: $internal_blks" >> $seqres.full
113 test $new_extents -le $((3 * nr / bufnr)) || echo "file2 more fragmented than expected"
114
115 # success, all done
116 status=0
117 exit