2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
7 # Test fsync on directories that got new hardlinks added to them and that point
8 # to existing inodes. The goal is to verify that after the fsync log is replayed
9 # the new hardlinks exist and the inodes have a correct link count.
10 # Also test that new hardlinks pointing to new inodes are logged and exist as
11 # well after the fsync log is replayed.
13 # This test is motivated by an issue discovered in btrfs, where the inode link
14 # counts were incorrect after the fsync log was replayed and the hardlinks for
15 # new inodes were not logged.
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
23 status=1 # failure is the default!
30 trap "_cleanup; exit \$status" 0 1 2 3 15
32 # get standard environment, filters and checks
37 # real QA test starts here
42 _require_dm_target flakey
46 _scratch_mkfs >> $seqres.full 2>&1
47 _require_metadata_journaling $SCRATCH_DEV
51 # Create our main test file and directory.
52 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 8K" $SCRATCH_MNT/foo | _filter_xfs_io
53 mkdir $SCRATCH_MNT/mydir
55 # Make sure all metadata and data are durably persisted.
58 # Add a hard link to 'foo' inside our test directory and fsync only the
59 # directory. The btrfs fsync implementation had a bug that caused the new
60 # directory entry to be visible after the fsync log replay but, the inode
61 # of our file remained with a link count of 1.
62 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/foo_2
64 # Add a few more links and new files.
65 # This is just to verify nothing breaks or gives incorrect results after the
66 # fsync log is replayed.
67 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/foo_3
68 $XFS_IO_PROG -f -c "pwrite -S 0xff 0 64K" $SCRATCH_MNT/hello | _filter_xfs_io
69 ln $SCRATCH_MNT/hello $SCRATCH_MNT/mydir/hello_2
71 # Add some subdirectories and new files and links to them. This is to verify
72 # that after fsyncing our top level directory 'mydir', all the subdirectories
73 # and their files/links are registered in the fsync log and exist after the
74 # fsync log is replayed.
75 mkdir -p $SCRATCH_MNT/mydir/x/y/z
76 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/x/y/foo_y_link
77 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/x/y/z/foo_z_link
78 touch $SCRATCH_MNT/mydir/x/y/z/qwerty
80 # Now fsync only our top directory.
81 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/mydir
83 # And fsync now our new file named 'hello', just to verify later that it has
84 # the expected content and that the previous fsync on the directory 'mydir' had
85 # no bad influence on this fsync.
86 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/hello
88 _flakey_drop_and_remount
90 # Verify the content of our file 'foo' remains the same as before, 8192 bytes,
91 # all with the value 0xaa.
92 echo "File 'foo' content after log replay:"
93 od -t x1 $SCRATCH_MNT/foo
95 # Remove the first name of our inode. Because of the directory fsync bug, the
96 # inode's link count was 1 instead of 5, so removing the 'foo' name ended up
97 # deleting the inode and the other names became stale directory entries (still
98 # visible to applications). Attempting to remove or access the remaining
99 # dentries pointing to that inode resulted in stale file handle errors and
100 # made it impossible to remove the parent directories since it was impossible
101 # for them to become empty.
102 echo "file 'foo' link count after log replay: $(stat -c %h $SCRATCH_MNT/foo)"
103 rm -f $SCRATCH_MNT/foo
105 # Now verify that all files, links and directories created before fsyncing our
106 # directory exist after the fsync log was replayed.
107 [ -f $SCRATCH_MNT/mydir/foo_2 ] || echo "Link mydir/foo_2 is missing"
108 [ -f $SCRATCH_MNT/mydir/foo_3 ] || echo "Link mydir/foo_3 is missing"
109 [ -f $SCRATCH_MNT/hello ] || echo "File hello is missing"
110 [ -f $SCRATCH_MNT/mydir/hello_2 ] || echo "Link mydir/hello_2 is missing"
111 [ -f $SCRATCH_MNT/mydir/x/y/foo_y_link ] || \
112 echo "Link mydir/x/y/foo_y_link is missing"
113 [ -f $SCRATCH_MNT/mydir/x/y/z/foo_z_link ] || \
114 echo "Link mydir/x/y/z/foo_z_link is missing"
115 [ -f $SCRATCH_MNT/mydir/x/y/z/qwerty ] || \
116 echo "File mydir/x/y/z/qwerty is missing"
118 # We expect our file here to have a size of 64Kb and all the bytes having the
120 echo "file 'hello' content after log replay:"
121 od -t x1 $SCRATCH_MNT/hello
123 # Now remove all files/links, under our test directory 'mydir', and verify we
124 # can remove all the directories.
125 rm -f $SCRATCH_MNT/mydir/x/y/z/*
126 rmdir $SCRATCH_MNT/mydir/x/y/z
127 rm -f $SCRATCH_MNT/mydir/x/y/*
128 rmdir $SCRATCH_MNT/mydir/x/y
129 rmdir $SCRATCH_MNT/mydir/x
130 rm -f $SCRATCH_MNT/mydir/*
131 rmdir $SCRATCH_MNT/mydir
133 # An fsck, run by the fstests framework everytime a test finishes, also detected
134 # the inconsistency and printed the following error message:
136 # root 5 inode 257 errors 2001, no inode item, link count wrong
137 # unresolved ref dir 258 index 2 namelen 5 name foo_2 filetype 1 errors 4, no inode ref
138 # unresolved ref dir 258 index 3 namelen 5 name foo_3 filetype 1 errors 4, no inode ref