7b8e12805ea6ba6655de2d9e8f43f3d34d3807b2
[xfstests-dev.git] / tests / btrfs / 088
1 #! /bin/bash
2 # FS QA Test No. btrfs/088
3 #
4 # Test that btrfs' transaction abortion does not corrupt a filesystem mounted
5 # with -o discard nor allows a subsequent fstrim to corrupt the filesystem
6 # (regardless of being mounted with or without -o discard).
7 #
8 # This issue was fixed by the following linux kernel patch:
9 #
10 #    Btrfs: fix fs corruption on transaction abort if device supports discard
11 #    (commit 678886bdc6378c1cbd5072da2c5a3035000214e3)
12 #
13 #-----------------------------------------------------------------------
14 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
15 # Author: Filipe Manana <fdmanana@suse.com>
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License as
19 # published by the Free Software Foundation.
20 #
21 # This program is distributed in the hope that it would be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write the Free Software Foundation,
28 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 #-----------------------------------------------------------------------
30 #
31
32 seq=`basename $0`
33 seqres=$RESULT_DIR/$seq
34 echo "QA output created by $seq"
35
36 tmp=/tmp/$$
37 status=1        # failure is the default!
38 trap "_cleanup; exit \$status" 0 1 2 3 15
39
40 _cleanup()
41 {
42         rm -f $tmp.*
43 }
44
45 # get standard environment, filters and checks
46 . ./common/rc
47 . ./common/filter
48
49 # real QA test starts here
50 _supported_fs btrfs
51 _supported_os Linux
52 _require_scratch
53 _require_fail_make_request
54
55 SYSFS_BDEV=`_sysfs_dev $SCRATCH_DEV`
56
57 enable_io_failure()
58 {
59         echo 100 > $DEBUGFS_MNT/fail_make_request/probability
60         echo 1000 > $DEBUGFS_MNT/fail_make_request/times
61         echo 0 > $DEBUGFS_MNT/fail_make_request/verbose
62         echo 1 > $SYSFS_BDEV/make-it-fail
63 }
64
65 disable_io_failure()
66 {
67         echo 0 > $SYSFS_BDEV/make-it-fail
68         echo 0 > $DEBUGFS_MNT/fail_make_request/probability
69         echo 0 > $DEBUGFS_MNT/fail_make_request/times
70 }
71
72 rm -f $seqres.full
73
74 # We will abort a btrfs transaction later, which always produces a warning in
75 # dmesg. We do not want the test to fail because of this.
76 _disable_dmesg_check
77
78 _scratch_mkfs >>$seqres.full 2>&1
79 _scratch_mount "-o discard"
80 _require_batched_discard $SCRATCH_MNT
81
82 # Create a file and call sync to commit our first transaction.
83 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 1M" $SCRATCH_MNT/foo | _filter_xfs_io
84 sync
85
86 # Create some other file, which forces a COW operation of the fs root, adding
87 # the old root location to the pinned extents list, and opens a new btrfs
88 # transaction.
89 touch $SCRATCH_MNT/bar
90
91 # Write to the first file to verify later that the original data extent was not
92 # a victim of a discard operation.
93 $XFS_IO_PROG -c "pwrite -S 0xbb 512K 1M" $SCRATCH_MNT/foo | _filter_xfs_io
94
95 # Now make sure the next transaction commit will abort and turn the fs readonly,
96 # unmount the fs, mount it again and verify we can open file foo and read its
97 # content, which should be what it had when the first transaction was committed
98 # (first call to sync), since btrfs is a COW filesystem and foo was not fsynced.
99 # Btrfs used to issue a discard operation on the extents in the pinned extents
100 # list, resulting in corruption of metadata and data, and used too to return the
101 # pinned extents to the free space caches, allowing future fstrim operations to
102 # perform a discard operation against the pinned exents. This made the fs
103 # unmountable because the btree roots that the superblock points at were written
104 # in place (by the discard operations).
105 enable_io_failure
106
107 # This sync will trigger a commit of the current transaction, which will be
108 # aborted because IO will fail for metadata extents (btree nodes/leafs).
109 sync
110 disable_io_failure
111
112 touch $SCRATCH_MNT/abc >>$seqres.full 2>&1 && \
113         echo "Transaction was not aborted, filesystem is not in readonly mode"
114
115 # This fstrim operation should not cause discard operations to be performed
116 # against extents that were COWed, otherwise the next mount will fail since
117 # the btree roots that the superblock points at have their physical areas
118 # on disk full of zeroes.
119 $FSTRIM_PROG $SCRATCH_MNT
120
121 # We expect to be able to mount the fs again and have available all metadata and
122 # data that got persisted in the first transaction.
123 _scratch_cycle_mount
124
125 # We now expect file's foo content to match what it had when the first
126 # transaction was committed because the second transaction was aborted and we
127 # did not fsync foo.
128 echo "File foo content after transaction abort + remount:"
129 od -t x1 $SCRATCH_MNT/foo
130
131 status=0
132 exit