btrfs: Verify falloc on multiple holes won't leak qgroup reserved data space
[xfstests-dev.git] / tests / btrfs / 160
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018, Jeff Layton <jlayton@redhat.com>
4 #
5 # FS QA Test No. 160
6 #
7 # Open a file and write to it and fsync. Then flip the data device to throw
8 # errors, write to it again and call sync. Close the file, reopen it and
9 # then call fsync on it. Is the error reported?
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14
15 here=`pwd`
16 tmp=/tmp/$$
17 status=1    # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 _cleanup()
21 {
22         cd /
23         rm -f $tmp.*
24         _dmerror_cleanup
25 }
26
27 # get standard environment, filters and checks
28 . ./common/rc
29 . ./common/filter
30 . ./common/dmerror
31
32 # real QA test starts here
33 _supported_os Linux
34 _supported_fs btrfs
35 _require_scratch_dev_pool
36
37 _require_dm_target error
38
39 rm -f $seqres.full
40
41 # bring up dmerror device
42 _dmerror_init
43
44 # Replace first device with error-test device
45 old_SCRATCH_DEV=$SCRATCH_DEV
46 SCRATCH_DEV_POOL=`echo $SCRATCH_DEV_POOL | perl -pe "s#$SCRATCH_DEV#$DMERROR_DEV#"`
47 SCRATCH_DEV=$DMERROR_DEV
48
49 echo "Format and mount"
50 _scratch_pool_mkfs "-d raid0 -m raid1" > $seqres.full 2>&1
51 _scratch_mount
52
53 # How much do we need to write? We need to hit all of the stripes. btrfs uses a
54 # fixed 64k stripesize, so write enough to hit each one. In the case of
55 # compression, each 128K input data chunk will be compressed to 4K (because of
56 # the characters written are duplicate). Therefore we have to write
57 # (128K * 16) = 2048K to make sure every stripe can be hit.
58 number_of_devices=`echo $SCRATCH_DEV_POOL | wc -w`
59 write_kb=$(($number_of_devices * 2048))
60 _require_fs_space $SCRATCH_MNT $write_kb
61 datalen=$((($write_kb * 1024)-1))
62
63 # use fd 5 to hold file open
64 testfile=$SCRATCH_MNT/fsync-open-after-err
65 exec 5>$testfile
66
67 # write some data to file and fsync it out
68 $XFS_IO_PROG -c "pwrite -q 0 $datalen" -c fsync $testfile
69
70 # flip device to non-working mode
71 _dmerror_load_error_table
72
73 # rewrite the data, call sync to ensure it's written back w/o scraping error
74 $XFS_IO_PROG -c "pwrite -q 0 $datalen" -c sync $testfile
75
76 # heal the device error
77 _dmerror_load_working_table
78
79 # open again and call fsync
80 echo "The following fsync should fail with EIO:"
81 $XFS_IO_PROG -c fsync $testfile
82 echo "done"
83
84 # close file
85 exec 5>&-
86
87 # success, all done
88 _dmerror_cleanup
89
90 status=0
91 exit