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.
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:
12 # Btrfs: fix data loss in the fast fsync path
14 #-----------------------------------------------------------------------
15 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
16 # Author: Filipe Manana <fdmanana@suse.com>
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.
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.
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 #-----------------------------------------------------------------------
34 seqres=$RESULT_DIR/$seq
35 echo "QA output created by $seq"
39 status=1 # failure is the default!
46 trap "_cleanup; exit \$status" 0 1 2 3 15
48 # get standard environment, filters and checks
53 # real QA test starts here
59 _require_metadata_journaling $SCRATCH_DEV
63 _scratch_mkfs >> $seqres.full 2>&1
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
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
81 # Make sure everything is durably persisted.
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
87 # Move our 'bar' file into a new directory.
88 mv $SCRATCH_MNT/testdir_1/bar $SCRATCH_MNT/testdir_2/bar
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
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
101 # Simulate a crash/power loss.
102 _load_flakey_table $FLAKEY_DROP_WRITES
105 _load_flakey_table $FLAKEY_ALLOW_WRITES
108 # Now check that all data we wrote before are available.
109 echo "File content after log replay:"
110 od -t x1 $SCRATCH_MNT/foo