btrfs: exercise transaction abort on device with discard support
[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 _need_to_be_root
55
56 SCRATCH_BDEV=`_short_dev $SCRATCH_DEV`
57
58 enable_io_failure()
59 {
60         echo 100 > $DEBUGFS_MNT/fail_make_request/probability
61         echo 1000 > $DEBUGFS_MNT/fail_make_request/times
62         echo 0 > $DEBUGFS_MNT/fail_make_request/verbose
63         echo 1 > /sys/block/$SCRATCH_BDEV/make-it-fail
64 }
65
66 disable_io_failure()
67 {
68         echo 0 > /sys/block/$SCRATCH_BDEV/make-it-fail
69         echo 0 > $DEBUGFS_MNT/fail_make_request/probability
70         echo 0 > $DEBUGFS_MNT/fail_make_request/times
71 }
72
73 rm -f $seqres.full
74
75 # We will abort a btrfs transaction later, which always produces a warning in
76 # dmesg. We do not want the test to fail because of this.
77 _disable_dmesg_check
78
79 _scratch_mkfs >>$seqres.full 2>&1
80 _scratch_mount "-o discard"
81 _require_batched_discard $SCRATCH_MNT
82
83 # Create a file and call sync to commit our first transaction.
84 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 1M" $SCRATCH_MNT/foo | _filter_xfs_io
85 sync
86
87 # Create some other file, which forces a COW operation of the fs root, adding
88 # the old root location to the pinned extents list, and opens a new btrfs
89 # transaction.
90 touch $SCRATCH_MNT/bar
91
92 # Write to the first file to verify later that the original data extent was not
93 # a victim of a discard operation.
94 $XFS_IO_PROG -c "pwrite -S 0xbb 512K 1M" $SCRATCH_MNT/foo | _filter_xfs_io
95
96 # Now make sure the next transaction commit will abort and turn the fs readonly,
97 # unmount the fs, mount it again and verify we can open file foo and read its
98 # content, which should be what it had when the first transaction was committed
99 # (first call to sync), since btrfs is a COW filesystem and foo was not fsynced.
100 # Btrfs used to issue a discard operation on the extents in the pinned extents
101 # list, resulting in corruption of metadata and data, and used too to return the
102 # pinned extents to the free space caches, allowing future fstrim operations to
103 # perform a discard operation against the pinned exents. This made the fs
104 # unmountable because the btree roots that the superblock points at were written
105 # in place (by the discard operations).
106 enable_io_failure
107
108 # This sync will trigger a commit of the current transaction, which will be
109 # aborted because IO will fail for metadata extents (btree nodes/leafs).
110 sync
111 disable_io_failure
112
113 touch $SCRATCH_MNT/abc >>$seqres.full 2>&1 && \
114         echo "Transaction was not aborted, filesystem is not in readonly mode"
115
116 # This fstrim operation should not cause discard operations to be performed
117 # against extents that were COWed, otherwise the next mount will fail since
118 # the btree roots that the superblock points at have their physical areas
119 # on disk full of zeroes.
120 $FSTRIM_PROG $SCRATCH_MNT
121
122 # We expect to be able to mount the fs again and have available all metadata and
123 # data that got persisted in the first transaction.
124 _scratch_remount
125
126 # We now expect file's foo content to match what it had when the first
127 # transaction was committed because the second transaction was aborted and we
128 # did not fsync foo.
129 echo "File foo content after transaction abort + remount:"
130 od -t x1 $SCRATCH_MNT/foo
131
132 status=0
133 exit