4 # Test fsync on directories that got new hardlinks added to them and that point
5 # to existing inodes. The goal is to verify that after the fsync log is replayed
6 # the new hardlinks exist and the inodes have a correct link count.
7 # Also test that new hardlinks pointing to new inodes are logged and exist as
8 # well after the fsync log is replayed.
10 # This test is motivated by an issue discovered in btrfs, where the inode link
11 # counts were incorrect after the fsync log was replayed and the hardlinks for
12 # new inodes were not logged.
14 #-----------------------------------------------------------------------
15 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
16 # Author: Filipe Manana <fdmanana@suse.com>
18 # This program is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU General Public License as
20 # published by the Free Software Foundation.
22 # This program is distributed in the hope that it would be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write the Free Software Foundation,
29 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #-----------------------------------------------------------------------
34 seqres=$RESULT_DIR/$seq
35 echo "QA output created by $seq"
39 status=1 # failure is the default!
46 trap "_cleanup; exit \$status" 0 1 2 3 15
48 # get standard environment, filters and checks
53 # real QA test starts here
58 _require_dm_target flakey
59 _require_metadata_journaling $SCRATCH_DEV
63 _scratch_mkfs >> $seqres.full 2>&1
67 # Create our main test file and directory.
68 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 8K" $SCRATCH_MNT/foo | _filter_xfs_io
69 mkdir $SCRATCH_MNT/mydir
71 # Make sure all metadata and data are durably persisted.
74 # Add a hard link to 'foo' inside our test directory and fsync only the
75 # directory. The btrfs fsync implementation had a bug that caused the new
76 # directory entry to be visible after the fsync log replay but, the inode
77 # of our file remained with a link count of 1.
78 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/foo_2
80 # Add a few more links and new files.
81 # This is just to verify nothing breaks or gives incorrect results after the
82 # fsync log is replayed.
83 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/foo_3
84 $XFS_IO_PROG -f -c "pwrite -S 0xff 0 64K" $SCRATCH_MNT/hello | _filter_xfs_io
85 ln $SCRATCH_MNT/hello $SCRATCH_MNT/mydir/hello_2
87 # Add some subdirectories and new files and links to them. This is to verify
88 # that after fsyncing our top level directory 'mydir', all the subdirectories
89 # and their files/links are registered in the fsync log and exist after the
90 # fsync log is replayed.
91 mkdir -p $SCRATCH_MNT/mydir/x/y/z
92 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/x/y/foo_y_link
93 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/x/y/z/foo_z_link
94 touch $SCRATCH_MNT/mydir/x/y/z/qwerty
96 # Now fsync only our top directory.
97 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/mydir
99 # And fsync now our new file named 'hello', just to verify later that it has
100 # the expected content and that the previous fsync on the directory 'mydir' had
101 # no bad influence on this fsync.
102 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/hello
104 _flakey_drop_and_remount
106 # Verify the content of our file 'foo' remains the same as before, 8192 bytes,
107 # all with the value 0xaa.
108 echo "File 'foo' content after log replay:"
109 od -t x1 $SCRATCH_MNT/foo
111 # Remove the first name of our inode. Because of the directory fsync bug, the
112 # inode's link count was 1 instead of 5, so removing the 'foo' name ended up
113 # deleting the inode and the other names became stale directory entries (still
114 # visible to applications). Attempting to remove or access the remaining
115 # dentries pointing to that inode resulted in stale file handle errors and
116 # made it impossible to remove the parent directories since it was impossible
117 # for them to become empty.
118 echo "file 'foo' link count after log replay: $(stat -c %h $SCRATCH_MNT/foo)"
119 rm -f $SCRATCH_MNT/foo
121 # Now verify that all files, links and directories created before fsyncing our
122 # directory exist after the fsync log was replayed.
123 [ -f $SCRATCH_MNT/mydir/foo_2 ] || echo "Link mydir/foo_2 is missing"
124 [ -f $SCRATCH_MNT/mydir/foo_3 ] || echo "Link mydir/foo_3 is missing"
125 [ -f $SCRATCH_MNT/hello ] || echo "File hello is missing"
126 [ -f $SCRATCH_MNT/mydir/hello_2 ] || echo "Link mydir/hello_2 is missing"
127 [ -f $SCRATCH_MNT/mydir/x/y/foo_y_link ] || \
128 echo "Link mydir/x/y/foo_y_link is missing"
129 [ -f $SCRATCH_MNT/mydir/x/y/z/foo_z_link ] || \
130 echo "Link mydir/x/y/z/foo_z_link is missing"
131 [ -f $SCRATCH_MNT/mydir/x/y/z/qwerty ] || \
132 echo "File mydir/x/y/z/qwerty is missing"
134 # We expect our file here to have a size of 64Kb and all the bytes having the
136 echo "file 'hello' content after log replay:"
137 od -t x1 $SCRATCH_MNT/hello
139 # Now remove all files/links, under our test directory 'mydir', and verify we
140 # can remove all the directories.
141 rm -f $SCRATCH_MNT/mydir/x/y/z/*
142 rmdir $SCRATCH_MNT/mydir/x/y/z
143 rm -f $SCRATCH_MNT/mydir/x/y/*
144 rmdir $SCRATCH_MNT/mydir/x/y
145 rmdir $SCRATCH_MNT/mydir/x
146 rm -f $SCRATCH_MNT/mydir/*
147 rmdir $SCRATCH_MNT/mydir
149 # An fsck, run by the fstests framework everytime a test finishes, also detected
150 # the inconsistency and printed the following error message:
152 # root 5 inode 257 errors 2001, no inode item, link count wrong
153 # unresolved ref dir 258 index 2 namelen 5 name foo_2 filetype 1 errors 4, no inode ref
154 # unresolved ref dir 258 index 3 namelen 5 name foo_3 filetype 1 errors 4, no inode ref