7816389109a8f62c3d40e558a327f483bbeb3d63
[xfstests-dev.git] / tests / generic / 041
1 #! /bin/bash
2 # FS QA Test No. 041
3 #
4 # This test is motivated by an fsync issue discovered in btrfs.
5 # The steps to trigger the issue were:
6 #
7 # 1) remove an hard link from an inode with a large number of hard links;
8 # 2) add a new hard link;
9 # 3) add another hard link with the same name as the one removed in step 1;
10 # 4) fsync the inode.
11 #
12 # These steps made the btrfs fsync log replay fail (with the -EOVERFLOW error),
13 # making the filesystem unmountable, requiring the use of btrfs-zero-log (it
14 # wipes the fsync log) in order to make the filesystem mountable again (but
15 # losing some data/metadata).
16 #
17 # The btrfs issue was fixed by the following linux kernel patches:
18 #
19 #  Btrfs: fix fsync when extend references are added to an inode
20 #  Btrfs: fix fsync log replay for inodes with a mix of regular refs and extrefs
21 #
22 # This issue was present in btrfs since the extrefs (extend references)
23 # feature was added (2012).
24 #
25 #-----------------------------------------------------------------------
26 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
27 # Author: Filipe Manana <fdmanana@suse.com>
28 #
29 # This program is free software; you can redistribute it and/or
30 # modify it under the terms of the GNU General Public License as
31 # published by the Free Software Foundation.
32 #
33 # This program is distributed in the hope that it would be useful,
34 # but WITHOUT ANY WARRANTY; without even the implied warranty of
35 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36 # GNU General Public License for more details.
37 #
38 # You should have received a copy of the GNU General Public License
39 # along with this program; if not, write the Free Software Foundation,
40 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
41 #-----------------------------------------------------------------------
42 #
43
44 seq=`basename $0`
45 seqres=$RESULT_DIR/$seq
46 echo "QA output created by $seq"
47
48 here=`pwd`
49 tmp=/tmp/$$
50 status=1        # failure is the default!
51
52 _cleanup()
53 {
54         _cleanup_flakey
55 }
56 trap "_cleanup; exit \$status" 0 1 2 3 15
57
58 # get standard environment, filters and checks
59 . ./common/rc
60 . ./common/filter
61 . ./common/dmflakey
62
63 # real QA test starts here
64 _supported_fs generic
65 _supported_os Linux
66 _need_to_be_root
67 _require_scratch
68 _require_dm_target flakey
69 _require_metadata_journaling $SCRATCH_DEV
70
71 rm -f $seqres.full
72
73 # If the test filesystem is btrfs, make sure we create a filesystem with
74 # the extend references (extrefs) feature enabled (it's enabled by default
75 # in recent versions of btrfs-progs).
76 if [ "$FSTYP" = "btrfs" ]; then
77         _scratch_mkfs "-O extref" >> $seqres.full 2>&1
78 else
79         _scratch_mkfs >> $seqres.full 2>&1
80 fi
81
82 _init_flakey
83 _mount_flakey
84
85 # Create a test file with 3001 hard links. This number is large enough to
86 # make btrfs start using extrefs at some point even if the fs has the maximum
87 # possible leaf/node size (64Kb).
88 echo "hello world" > $SCRATCH_MNT/foo
89 for i in `seq 1 3000`; do
90         ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link_`printf "%04d" $i`
91 done
92
93 # Make sure all metadata and data are durably persisted.
94 sync
95
96 # Now remove one link, add a new one with a new name, add another new one with
97 # the same name as the one we just removed and fsync the inode.
98 rm -f $SCRATCH_MNT/foo_link_0001
99 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link_3001
100 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link_0001
101 rm -f $SCRATCH_MNT/foo_link_0002
102 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link_3002
103 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link_3003
104 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
105
106 _flakey_drop_and_remount
107
108 # Check that the number of hard links is correct, we are able to remove all
109 # the hard links and read the file's data. This is just to verify we don't
110 # get stale file handle errors (due to dangling directory index entries that
111 # point to inodes that no longer exist).
112 echo "Link count: $(stat -c %h $SCRATCH_MNT/foo)"
113 [ -f $SCRATCH_MNT/foo ] || echo "Link foo is missing"
114 for ((i = 1; i <= 3003; i++)); do
115         name=foo_link_`printf "%04d" $i`
116         if [ $i -eq 2 ]; then
117                 [ -f $SCRATCH_MNT/$name ] && echo "Link $name found"
118         else
119                 [ -f $SCRATCH_MNT/$name ] || echo "Link $name is missing"
120         fi
121 done
122 rm -f $SCRATCH_MNT/foo_link_*
123 cat $SCRATCH_MNT/foo
124 rm -f $SCRATCH_MNT/foo
125
126 status=0
127 exit