common/rc: add _scratch_{u}mount_idmapped() helpers
[xfstests-dev.git] / tests / generic / 040
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. 040
6 #
7 # This test is motivated by an fsync issue discovered in btrfs.
8 # The issue in btrfs was that adding a new hard link to an inode that already
9 # had a large number of hardlinks and fsync the inode, would make the fsync
10 # log replay code update the inode with a wrong link count (smaller than the
11 # correct value). This resulted later in dangling directory index entries,
12 # after removing most of the hard links (correct_value - wrong_value), that
13 # were visible to user space but it was impossible to delete them or do
14 # any other operation on them (since they pointed to an inode that didn't
15 # exist anymore, resulting in -ESTALE errors).
16 #
17 # The btrfs issue was fixed by the following linux kernel patch:
18 #
19 #    Btrfs: fix fsync when extend references are added to an inode
20 #
21 # This issue was present in btrfs since the extrefs (extend references)
22 # feature was added (2012).
23 #
24 seq=`basename $0`
25 seqres=$RESULT_DIR/$seq
26 echo "QA output created by $seq"
27
28 here=`pwd`
29 tmp=/tmp/$$
30 status=1        # failure is the default!
31
32 _cleanup()
33 {
34         _cleanup_flakey
35 }
36 trap "_cleanup; exit \$status" 0 1 2 3 15
37
38 # get standard environment, filters and checks
39 . ./common/rc
40 . ./common/filter
41 . ./common/dmflakey
42
43 # real QA test starts here
44 _supported_fs generic
45 _require_scratch
46 _require_hardlinks
47 _require_dm_target flakey
48
49 rm -f $seqres.full
50
51 # If the test filesystem is btrfs, make sure we create a filesystem with
52 # the extend references (extrefs) feature enabled (it's enabled by default
53 # in recent versions of btrfs-progs).
54 if [ "$FSTYP" = "btrfs" ]; then
55         _scratch_mkfs "-O extref" >> $seqres.full 2>&1
56 else
57         _scratch_mkfs >> $seqres.full 2>&1
58 fi
59
60 _require_metadata_journaling $SCRATCH_DEV
61 _init_flakey
62 _mount_flakey
63
64 # Create a test file with 3001 hard links. This number is large enough to
65 # make btrfs start using extrefs at some point even if the fs has the maximum
66 # possible leaf/node size (64Kb).
67 echo "hello world" > $SCRATCH_MNT/foo
68 for i in `seq 1 3000`; do
69         ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link_`printf "%04d" $i`
70 done
71
72 # Make sure all metadata and data are durably persisted.
73 sync
74
75 # Add one more link to the inode that ends up being a btrfs extref and fsync
76 # the inode.
77 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link_3001
78 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
79
80 _flakey_drop_and_remount
81
82 # Now after the fsync log replay btrfs left our inode with a wrong link count N,
83 # which was smaller than the correct link count M (N < M).
84 # So after removing N hard links, the remaining M - N directory entries were
85 # still visible to user space but it was impossible to do anything with them
86 # because they pointed to an inode that didn't exist anymore. This resulted in
87 # stale file handle errors (-ESTALE) when accessing those dentries for example.
88 #
89 # So remove all hard links except the first one and then attempt to read the
90 # file, to verify we don't get an -ESTALE error when accessing the inode.
91 #
92 # The btrfs fsck tool also detected the incorrect inode link count and it
93 # reported an error message like the following:
94 #
95 # root 5 inode 257 errors 2001, no inode item, link count wrong
96 #   unresolved ref dir 256 index 2978 namelen 13 name foo_link_2976 filetype 1 errors 4, no inode ref
97 #
98 # The fstests framework automatically calls fsck after a test is run, so we
99 # don't need to call fsck explicitly here.
100
101 echo "Link count before rm foo_link_*: $(stat -c %h $SCRATCH_MNT/foo)"
102 rm -f $SCRATCH_MNT/foo_link_*
103 echo "Link count after rm foo_link_*: $(stat -c %h $SCRATCH_MNT/foo)"
104 cat $SCRATCH_MNT/foo
105
106 status=0
107 exit