common/rc: Add _require_{chown,chmod}()
[xfstests-dev.git] / tests / generic / 065
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. 065
6 #
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.
12 #
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.
16 #
17 seq=`basename $0`
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
20
21 here=`pwd`
22 tmp=/tmp/$$
23 status=1        # failure is the default!
24
25 _cleanup()
26 {
27         _cleanup_flakey
28         rm -f $tmp.*
29 }
30 trap "_cleanup; exit \$status" 0 1 2 3 15
31
32 # get standard environment, filters and checks
33 . ./common/rc
34 . ./common/filter
35 . ./common/dmflakey
36
37 # real QA test starts here
38 _supported_fs generic
39 _require_scratch
40 _require_hardlinks
41 _require_dm_target flakey
42
43 rm -f $seqres.full
44
45 _scratch_mkfs >> $seqres.full 2>&1
46 _require_metadata_journaling $SCRATCH_DEV
47 _init_flakey
48 _mount_flakey
49
50 # Create our main test file and directory.
51 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 8K" $SCRATCH_MNT/foo | _filter_xfs_io
52 mkdir $SCRATCH_MNT/mydir
53
54 # Make sure all metadata and data are durably persisted.
55 sync
56
57 # Add a hard link to 'foo' inside our test directory and fsync only the
58 # directory. The btrfs fsync implementation had a bug that caused the new
59 # directory entry to be visible after the fsync log replay but, the inode
60 # of our file remained with a link count of 1.
61 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/foo_2
62
63 # Add a few more links and new files.
64 # This is just to verify nothing breaks or gives incorrect results after the
65 # fsync log is replayed.
66 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/foo_3
67 $XFS_IO_PROG -f -c "pwrite -S 0xff 0 64K" $SCRATCH_MNT/hello | _filter_xfs_io
68 ln $SCRATCH_MNT/hello $SCRATCH_MNT/mydir/hello_2
69
70 # Add some subdirectories and new files and links to them. This is to verify
71 # that after fsyncing our top level directory 'mydir', all the subdirectories
72 # and their files/links are registered in the fsync log and exist after the
73 # fsync log is replayed.
74 mkdir -p $SCRATCH_MNT/mydir/x/y/z
75 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/x/y/foo_y_link
76 ln $SCRATCH_MNT/foo $SCRATCH_MNT/mydir/x/y/z/foo_z_link
77 touch $SCRATCH_MNT/mydir/x/y/z/qwerty
78
79 # Now fsync only our top directory.
80 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/mydir
81
82 # And fsync now our new file named 'hello', just to verify later that it has
83 # the expected content and that the previous fsync on the directory 'mydir' had
84 # no bad influence on this fsync.
85 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/hello
86
87 _flakey_drop_and_remount
88
89 # Verify the content of our file 'foo' remains the same as before, 8192 bytes,
90 # all with the value 0xaa.
91 echo "File 'foo' content after log replay:"
92 od -t x1 $SCRATCH_MNT/foo
93
94 # Remove the first name of our inode. Because of the directory fsync bug, the
95 # inode's link count was 1 instead of 5, so removing the 'foo' name ended up
96 # deleting the inode and the other names became stale directory entries (still
97 # visible to applications). Attempting to remove or access the remaining
98 # dentries pointing to that inode resulted in stale file handle errors and
99 # made it impossible to remove the parent directories since it was impossible
100 # for them to become empty.
101 echo "file 'foo' link count after log replay: $(stat -c %h $SCRATCH_MNT/foo)"
102 rm -f $SCRATCH_MNT/foo
103
104 # Now verify that all files, links and directories created before fsyncing our
105 # directory exist after the fsync log was replayed.
106 [ -f $SCRATCH_MNT/mydir/foo_2 ] || echo "Link mydir/foo_2 is missing"
107 [ -f $SCRATCH_MNT/mydir/foo_3 ] || echo "Link mydir/foo_3 is missing"
108 [ -f $SCRATCH_MNT/hello ] || echo "File hello is missing"
109 [ -f $SCRATCH_MNT/mydir/hello_2 ] || echo "Link mydir/hello_2 is missing"
110 [ -f $SCRATCH_MNT/mydir/x/y/foo_y_link ] || \
111         echo "Link mydir/x/y/foo_y_link is missing"
112 [ -f $SCRATCH_MNT/mydir/x/y/z/foo_z_link ] || \
113         echo "Link mydir/x/y/z/foo_z_link is missing"
114 [ -f $SCRATCH_MNT/mydir/x/y/z/qwerty ] || \
115         echo "File mydir/x/y/z/qwerty is missing"
116
117 # We expect our file here to have a size of 64Kb and all the bytes having the
118 # value 0xff.
119 echo "file 'hello' content after log replay:"
120 od -t x1 $SCRATCH_MNT/hello
121
122 # Now remove all files/links, under our test directory 'mydir', and verify we
123 # can remove all the directories.
124 rm -f $SCRATCH_MNT/mydir/x/y/z/*
125 rmdir $SCRATCH_MNT/mydir/x/y/z
126 rm -f $SCRATCH_MNT/mydir/x/y/*
127 rmdir $SCRATCH_MNT/mydir/x/y
128 rmdir $SCRATCH_MNT/mydir/x
129 rm -f $SCRATCH_MNT/mydir/*
130 rmdir $SCRATCH_MNT/mydir
131
132 # An fsck, run by the fstests framework everytime a test finishes, also detected
133 # the inconsistency and printed the following error message:
134 #
135 # root 5 inode 257 errors 2001, no inode item, link count wrong
136 #    unresolved ref dir 258 index 2 namelen 5 name foo_2 filetype 1 errors 4, no inode ref
137 #    unresolved ref dir 258 index 3 namelen 5 name foo_3 filetype 1 errors 4, no inode ref
138
139 status=0
140 exit