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
40 _require_dm_target flakey
44 _scratch_mkfs >> $seqres.full 2>&1
45 _require_metadata_journaling $SCRATCH_DEV
49 # Create our test file with some data.
50 $XFS_IO_PROG -f -c "pwrite -S 0xaa -b 8K 0 8K" \
51 $SCRATCH_MNT/foo | _filter_xfs_io
53 # Make sure the file is durably persisted.
56 # Append some data to our file, to increase its size.
57 $XFS_IO_PROG -f -c "pwrite -S 0xcc -b 4K 8K 4K" \
58 $SCRATCH_MNT/foo | _filter_xfs_io
60 # Fsync the file, so from this point on if a crash/power failure happens, our
61 # new data is guaranteed to be there next time the fs is mounted.
62 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
64 # Add one hard link to our file. This made btrfs write into the in memory fsync
65 # log a special inode with generation 0 and an i_size of 0 too. Note that this
66 # didn't update the inode in the fsync log on disk.
67 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link
69 # Now make sure the in memory fsync log is durably persisted.
70 # Creating and fsync'ing another file will do it.
71 touch $SCRATCH_MNT/bar
72 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar
74 # As expected, before the crash/power failure, we should be able to read the
76 echo "File content before:"
77 od -t x1 $SCRATCH_MNT/foo
79 _flakey_drop_and_remount
81 # After mounting the fs again, the fsync log was replayed.
82 # The btrfs fsync log replay code didn't update the i_size of the persisted
83 # inode because the inode item in the log had a special generation with a
84 # value of 0 (and it couldn't know the correct i_size, since that inode item
85 # had a 0 i_size too). This made the last 4Kb of file data inaccessible and
87 echo "File content after:"
88 od -t x1 $SCRATCH_MNT/foo