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 one file with data and fsync it.
50 # This made the btrfs fsync log persist the data and the inode metadata with
51 # a correct inode->i_size (4096 bytes).
52 $XFS_IO_PROG -f -c "pwrite -S 0xaa -b 4K 0 4K" -c "fsync" \
53 $SCRATCH_MNT/foo | _filter_xfs_io
55 # Now add one hard link to our file. This made the btrfs code update the fsync
56 # log, in memory only, with an inode metadata having a size of 0.
57 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link
59 # Now force persistence of the fsync log to disk, for example, by fsyncing some
61 touch $SCRATCH_MNT/bar
62 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar
64 # Before a power loss or crash, we could read the 4Kb of data from our file as
66 echo "File content before:"
67 od -t x1 $SCRATCH_MNT/foo
69 _flakey_drop_and_remount
71 # After the fsync log replay, because the fsync log had a value of 0 for our
72 # inode's i_size, we couldn't read anymore the 4Kb of data that we previously
73 # wrote and fsync'ed. The size of the file became 0 after the fsync log replay.
74 echo "File content after:"
75 od -t x1 $SCRATCH_MNT/foo