1c92c66fc78d368cf2a0a5ee3813106589461c8a
[xfstests-dev.git] / tests / generic / 073
1 #! /bin/bash
2 # FS QA Test No. 073
3 #
4 # Test file A fsync after moving one other unrelated file B between directories
5 # and fsyncing B's old parent directory before fsyncing the file A. Check that
6 # after a crash all the file A data we fsynced is available.
7 #
8 # This test is motivated by an issue discovered in btrfs which caused the file
9 # data to be lost (despite fsync returning success to user space). That btrfs
10 # bug was fixed by the following linux kernel patch:
11 #
12 #   Btrfs: fix data loss in the fast fsync path
13 #
14 #-----------------------------------------------------------------------
15 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
16 # Author: Filipe Manana <fdmanana@suse.com>
17 #
18 # This program is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU General Public License as
20 # published by the Free Software Foundation.
21 #
22 # This program is distributed in the hope that it would be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write the Free Software Foundation,
29 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30 #-----------------------------------------------------------------------
31 #
32
33 seq=`basename $0`
34 seqres=$RESULT_DIR/$seq
35 echo "QA output created by $seq"
36
37 here=`pwd`
38 tmp=/tmp/$$
39 status=1        # failure is the default!
40
41 _cleanup()
42 {
43         _cleanup_flakey
44         rm -f $tmp.*
45 }
46 trap "_cleanup; exit \$status" 0 1 2 3 15
47
48 # get standard environment, filters and checks
49 . ./common/rc
50 . ./common/filter
51 . ./common/dmflakey
52
53 # real QA test starts here
54 _supported_fs generic
55 _supported_os Linux
56 _need_to_be_root
57 _require_scratch
58 _require_dm_target flakey
59 _require_metadata_journaling $SCRATCH_DEV
60
61 rm -f $seqres.full
62
63 _scratch_mkfs >> $seqres.full 2>&1
64 _init_flakey
65 _mount_flakey
66
67 # Create our main test file 'foo', the one we check for data loss.
68 # By doing an fsync against our file, it makes btrfs clear the 'needs_full_sync'
69 # bit from its flags (btrfs inode specific flags).
70 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 8K" \
71                 -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io
72
73 # Now create one other file and 2 directories. We will move this second file
74 # from one directory to the other later because it forces btrfs to commit its
75 # currently open transaction if we fsync the old parent directory. This is
76 # necessary to trigger the data loss bug that affected btrfs.
77 mkdir $SCRATCH_MNT/testdir_1
78 touch $SCRATCH_MNT/testdir_1/bar
79 mkdir $SCRATCH_MNT/testdir_2
80
81 # Make sure everything is durably persisted.
82 sync
83
84 # Write more 8Kb of data to our file.
85 $XFS_IO_PROG -c "pwrite -S 0xbb 8K 8K" $SCRATCH_MNT/foo | _filter_xfs_io
86
87 # Move our 'bar' file into a new directory.
88 mv $SCRATCH_MNT/testdir_1/bar $SCRATCH_MNT/testdir_2/bar
89
90 # Fsync our first directory. Because it had a file moved into some other
91 # directory, this made btrfs commit the currently open transaction. This is
92 # a condition necessary to trigger the data loss bug.
93 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/testdir_1
94
95 # Now fsync our main test file. If the fsync succeeds, we expect the 8Kb of
96 # data we wrote previously to be persisted and available if a crash happens.
97 # This did not happen with btrfs, because of the transaction commit that
98 # happened when we fsynced the parent directory.
99 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
100
101 _flakey_drop_and_remount
102
103 # Now check that all data we wrote before are available.
104 echo "File content after log replay:"
105 od -t x1 $SCRATCH_MNT/foo
106
107 status=0
108 exit