common: add helper function _flakey_drop_and_remount
[xfstests-dev.git] / tests / btrfs / 056
1 #! /bin/bash
2 # FS QA Test No. btrfs/056
3 #
4 # Regression test for btrfs ioctl clone operation + fsync + log recovery.
5 # The issue was that doing an fsync after cloning into a file didn't gave any
6 # persistence guarantees as it should. What happened was that the in memory
7 # metadata (extent maps) weren't updated, which made the fsync code not able
8 # to detect that file data has been changed.
9 #
10 # This issue is fixed by the following linux kernel btrfs patch:
11 #
12 #    Btrfs: make fsync work after cloning into a file
13 #
14 #-----------------------------------------------------------------------
15 # Copyright (c) 2014 Filipe Manana.  All Rights Reserved.
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         _cleanup_flakey
43         rm -fr $tmp
44 }
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter
49 . ./common/dmflakey
50
51 # real QA test starts here
52 _supported_fs btrfs
53 _supported_os Linux
54 _require_scratch
55 _require_cloner
56 _require_btrfs_fs_feature "no_holes"
57 _require_btrfs_mkfs_feature "no-holes"
58 _require_dm_target flakey
59 _need_to_be_root
60
61 rm -f $seqres.full
62
63 test_btrfs_clone_fsync_log_recover()
64 {
65         _scratch_mkfs "$1" >/dev/null 2>&1
66         _init_flakey
67         SAVE_MOUNT_OPTIONS="$MOUNT_OPTIONS"
68         MOUNT_OPTIONS="$MOUNT_OPTIONS $2"
69         _mount_flakey
70
71         # Create a file with 4 extents and 1 hole, all with a size of 8Kb each.
72         # The hole is in the range [16384, 24576[.
73         $XFS_IO_PROG -s -f -c "pwrite -S 0x01 -b 8192 0 8192" \
74                         -c "pwrite -S 0x02 -b 8192 8192 8192" \
75                         -c "pwrite -S 0x04 -b 8192 24576 8192" \
76                         -c "pwrite -S 0x05 -b 8192 32768 8192" \
77                 $SCRATCH_MNT/foo | _filter_xfs_io
78
79         # Clone destination file, 1 extent of 96kb.
80         $XFS_IO_PROG -f -c "pwrite -S 0xff -b 98304 0 98304" -c "fsync" \
81                 $SCRATCH_MNT/bar | _filter_xfs_io
82
83         # Clone second half of the 2nd extent, the 8kb hole, the 3rd extent
84         # and the first half of the 4th extent into file bar.
85         $CLONER_PROG -s 12288 -d 0 -l 24576 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
86         $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar
87
88         # Test small files too consisting of 1 inline extent
89         $XFS_IO_PROG -f -c "pwrite -S 0x00 -b 3500 0 3500" -c "fsync" \
90                 $SCRATCH_MNT/foo2 | _filter_xfs_io
91
92         $XFS_IO_PROG -f -c "pwrite -S 0xcc -b 1000 0 1000" -c "fsync" \
93                 $SCRATCH_MNT/bar2 | _filter_xfs_io
94
95         # Clone the entire foo2 file into bar2, overwriting all data in bar2
96         # and increasing its size.
97         $CLONER_PROG -s 0 -d 0 -l 3500 $SCRATCH_MNT/foo2 $SCRATCH_MNT/bar2
98         $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar2
99
100         _flakey_drop_and_remount yes
101
102         # Verify the cloned range was persisted by fsync and the log recovery
103         # code did its work well.
104         echo "Verifying file bar content"
105         od -t x1 $SCRATCH_MNT/bar
106
107         echo "Verifying file bar2 content"
108         od -t x1 $SCRATCH_MNT/bar2
109
110         _unmount_flakey
111
112         # Verify that there are no consistency errors.
113         _check_scratch_fs $FLAKEY_DEV
114
115         _cleanup_flakey
116         MOUNT_OPTIONS="$SAVE_MOUNT_OPTIONS"
117 }
118
119 # Regardless of the NO_HOLES feature being enabled or not, the test results
120 # should be exactly the same for both cases.
121
122 echo "Testing without the NO_HOLES feature"
123 # As of btrfs-progs 3.14.x, the no-holes feature isn't enabled by default.
124 # But explicitly disable it at mkfs time as it might be enabled by default
125 # in future versions.
126 test_btrfs_clone_fsync_log_recover "-O ^no-holes"
127
128 echo "Testing without the NO_HOLES feature and compression (lzo)"
129 test_btrfs_clone_fsync_log_recover "-O ^no-holes" "-o compress-force=lzo"
130
131 echo "Testing with the NO_HOLES feature enabled"
132 test_btrfs_clone_fsync_log_recover "-O no-holes"
133
134 echo "Testing with the NO_HOLES feature enabled and compression (lzo)"
135 test_btrfs_clone_fsync_log_recover "-O no-holes" "-o compress-force=lzo"
136
137 status=0
138 exit