btrfs/012: check free size of scratch device before copying files
[xfstests-dev.git] / tests / generic / 460
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Red Hat Inc.  All Rights Reserved.
4 #
5 # FS QA Test 460
6 #
7 # Test that XFS reserves reasonable indirect blocks for delalloc and
8 # speculative allocation, and doesn't cause any fdblocks corruption.
9 #
10 # This was inspired by an XFS but that too large 'indlen' was returned by
11 # xfs_bmap_worst_indlen() which can't fit in a 17 bits value (STARTBLOCKVALBITS
12 # is defined as 17), then leaked 1 << 17 blocks in sb_fdblocks.
13 #
14 # This was only seen on XFS with rmapbt feature enabled, but nothing prevents
15 # the test from being a generic test.
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 testfile=$SCRATCH_MNT/1G_file.$seq
25 file_size=$((1024 * 1024 * 1024))
26 trap "_cleanup; exit \$status" 0 1 2 3 15
27
28 saved_dirty_background_ratio=0
29 saved_dirty_ratio=0
30
31 save_dirty_ratio()
32 {
33         saved_dirty_background_ratio=`cat /proc/sys/vm/dirty_background_ratio`
34         saved_dirty_ratio=`cat /proc/sys/vm/dirty_ratio`
35 }
36
37 set_dirty_ratio()
38 {
39         echo 100 > /proc/sys/vm/dirty_background_ratio
40         echo 100 > /proc/sys/vm/dirty_ratio
41 }
42
43 restore_dirty_ratio()
44 {
45         if [ $saved_dirty_background_ratio -ne 0 ]; then
46                 echo $saved_dirty_background_ratio > /proc/sys/vm/dirty_background_ratio
47         fi
48         if [ $saved_dirty_ratio -ne 0 ]; then
49                 echo $saved_dirty_ratio > /proc/sys/vm/dirty_ratio
50         fi
51 }
52
53 _cleanup()
54 {
55         cd /
56         restore_dirty_ratio
57         rm -f $tmp.*
58 }
59
60 # get standard environment, filters and checks
61 . ./common/rc
62 . ./common/filter
63
64 # remove previous $seqres.full before test
65 rm -f $seqres.full
66
67 # real QA test starts here
68 _supported_fs generic
69 # test with scratch device, because test is known to corrupt fs, we don't want
70 # the corruption affect subsequent tests
71 _require_scratch
72
73 _scratch_mkfs >>$seqres.full 2>&1
74 _scratch_mount
75
76 # need at least 1G free space
77 _require_fs_space $SCRATCH_MNT $((1024 * 1024))
78
79 # To reproduce the bug, we need to keep enough dirty data in memory (1G at
80 # least), so that a large enough delay allocated extent is kept in memory, then
81 # speculative preallocation could allocate large number of blocks based on the
82 # existing extent size.
83 # So we set dirty_background_ratio and dirty_ratio to 100% uncontitionally,
84 # even if the total memory is less than 1G, there's no harm to run a test on a
85 # such host.
86 save_dirty_ratio
87 set_dirty_ratio
88
89 # buffer write a 1G file, which is enough to trigger the bug,
90 # _check_filesystems will complain about fs corruption after test
91 $XFS_IO_PROG -fc "pwrite -b 1m 0 $file_size" $testfile >/dev/null 2>&1
92
93 echo "Silence is golden"
94
95 # success, all done
96 status=0
97 exit