2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
7 # This test is motivated by an fsync issue discovered in btrfs.
8 # The issue was that we could lose file data, that was previously fsync'ed
9 # successfully, if we end up adding a hard link to our inode and then persist
10 # the fsync log later via an fsync of other inode for example.
12 # The btrfs issue was fixed by the following linux kernel patch:
14 # Btrfs: fix fsync data loss after adding hard link to inode
17 seqres=$RESULT_DIR/$seq
18 echo "QA output created by $seq"
22 status=1 # failure is the default!
29 trap "_cleanup; exit \$status" 0 1 2 3 15
31 # get standard environment, filters and checks
36 # real QA test starts here
41 _require_dm_target flakey
45 _scratch_mkfs >> $seqres.full 2>&1
46 _require_metadata_journaling $SCRATCH_DEV
50 # Create our test file with some data.
51 $XFS_IO_PROG -f -c "pwrite -S 0xaa -b 8K 0 8K" \
52 $SCRATCH_MNT/foo | _filter_xfs_io
54 # Make sure the file is durably persisted.
57 # Append some data to our file, to increase its size.
58 $XFS_IO_PROG -f -c "pwrite -S 0xcc -b 4K 8K 4K" \
59 $SCRATCH_MNT/foo | _filter_xfs_io
61 # Fsync the file, so from this point on if a crash/power failure happens, our
62 # new data is guaranteed to be there next time the fs is mounted.
63 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
65 # Add one hard link to our file. This made btrfs write into the in memory fsync
66 # log a special inode with generation 0 and an i_size of 0 too. Note that this
67 # didn't update the inode in the fsync log on disk.
68 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link
70 # Now make sure the in memory fsync log is durably persisted.
71 # Creating and fsync'ing another file will do it.
72 touch $SCRATCH_MNT/bar
73 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar
75 # As expected, before the crash/power failure, we should be able to read the
77 echo "File content before:"
78 od -t x1 $SCRATCH_MNT/foo
80 _flakey_drop_and_remount
82 # After mounting the fs again, the fsync log was replayed.
83 # The btrfs fsync log replay code didn't update the i_size of the persisted
84 # inode because the inode item in the log had a special generation with a
85 # value of 0 (and it couldn't know the correct i_size, since that inode item
86 # had a 0 i_size too). This made the last 4Kb of file data inaccessible and
88 echo "File content after:"
89 od -t x1 $SCRATCH_MNT/foo