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 after fsyncing an inode that got its link count
9 # decremented, and the new link count is greater than zero, after the
10 # fsync log replay the inode's parent directory metadata became
11 # inconsistent - it had a wrong i_size and dangling index entries which
12 # prevented the directory from ever being removed (rmdir always failed
13 # with -ENOTEMPTY, even if the directory had no more child inodes).
15 # The btrfs issue was fixed by the following linux kernel patch:
17 # Btrfs: fix directory inconsistency after fsync log replay
20 _begin_fstest metadata auto quick log
22 # Override the default cleanup function.
28 # Import common functions.
32 # real QA test starts here
36 _require_dm_target flakey
38 _scratch_mkfs >> $seqres.full 2>&1
39 _require_metadata_journaling $SCRATCH_DEV
43 # Create a test file with 2 hard links in the same directory.
44 mkdir -p $SCRATCH_MNT/a/b
45 echo "hello world" > $SCRATCH_MNT/a/b/foo
46 ln $SCRATCH_MNT/a/b/foo $SCRATCH_MNT/a/b/bar
48 # Make sure all metadata and data are durably persisted.
51 # Now remove one of the hard links and fsync the inode.
52 rm -f $SCRATCH_MNT/a/b/bar
53 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/a/b/foo
55 _flakey_drop_and_remount
57 # Remove the last hard link of the file and attempt to remove its parent
58 # directory - this failed in btrfs because the fsync log and replay code
59 # didn't decrement the parent directory's i_size and left dangling directory
60 # index entries - this made the btrfs rmdir implementation always fail with
61 # the error -ENOTEMPTY.
63 # The dangling directory index entries were visible to user space, but it was
64 # impossible to do anything on them (unlink, open, read, write, stat, etc)
65 # because the inode they pointed to did not exist anymore.
67 # The parent directory's metadata inconsistency (stale index entries) was
68 # also detected by btrfs' fsck tool, which is run automatically by the fstests
69 # framework when the test finishes. The error message reported by fsck was:
71 # root 5 inode 259 errors 2001, no inode item, link count wrong
72 # unresolved ref dir 258 index 3 namelen 3 name bar filetype 1 errors 4, no inode ref
74 rm -f $SCRATCH_MNT/a/b/*
75 rmdir $SCRATCH_MNT/a/b
78 echo "Silence is golden"