cc59cdb1d0f69ed6367423f25704692047430b67
[xfstests-dev.git] / tests / shared / 002
1 #! /bin/bash
2 # FS QA Test No. 002
3 #
4 # Test that after syncing the filesystem, adding many xattrs to a file, syncing
5 # the filesystem again, writing to the file and then doing a fsync against that
6 # file, all the xattrs still exists after a power failure. That is, after the
7 # fsync log/journal is replayed, the xattrs still exist and with the correct
8 # values.
9 #
10 # This test is motivated by a bug found in btrfs.
11 #
12 #-----------------------------------------------------------------------
13 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
14 # Author: Filipe Manana <fdmanana@suse.com>
15 #
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License as
18 # published by the Free Software Foundation.
19 #
20 # This program is distributed in the hope that it would be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write the Free Software Foundation,
27 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28 #-----------------------------------------------------------------------
29 #
30
31 seq=`basename $0`
32 seqres=$RESULT_DIR/$seq
33 echo "QA output created by $seq"
34
35 here=`pwd`
36 tmp=/tmp/$$
37 status=1        # failure is the default!
38
39 _cleanup()
40 {
41         _cleanup_flakey
42         rm -f $tmp.*
43 }
44 trap "_cleanup; exit \$status" 0 1 2 3 15
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter
49 . ./common/dmflakey
50 . ./common/attr
51
52 # real QA test starts here
53
54 # We create a lot of xattrs for a single file. Only btrfs and xfs are currently
55 # able to store such a large mount of xattrs per file, other filesystems such
56 # as ext3/4 and f2fs for example, fail with ENOSPC even if we attempt to add
57 # less than 1000 xattrs with very small values.
58 _supported_fs btrfs xfs
59 _supported_os Linux
60 _need_to_be_root
61 _require_scratch
62 _require_dm_flakey
63 _require_attrs
64 _require_metadata_journaling $SCRATCH_DEV
65
66 rm -f $seqres.full
67
68 _scratch_mkfs >> $seqres.full 2>&1
69 _init_flakey
70 _mount_flakey
71
72 # Create the test file with some initial data and make sure everything is
73 # durably persisted.
74 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 32k" $SCRATCH_MNT/foo | _filter_xfs_io
75 sync
76
77 # Add many small xattrs to our file.
78 # We create such a large amount because it's needed to trigger the issue found
79 # in btrfs - we need to have an amount that causes the fs to have at least 3
80 # btree leafs with xattrs stored in them, and it must work on any leaf size
81 # (maximum leaf/node size is 64Kb).
82 num_xattrs=2000
83 for ((i = 1; i <= $num_xattrs; i++)); do
84         name="user.attr_$(printf "%04d" $i)"
85         $SETFATTR_PROG -n $name -v "val_$(printf "%04d" $i)" $SCRATCH_MNT/foo
86 done
87
88 # Sync the filesystem to force a commit of the current btrfs transaction, this
89 # is a necessary condition to trigger the bug on btrfs.
90 sync
91
92 # Now update our file's data and fsync the file.
93 # After a successful fsync, if the fsync log/journal is replayed we expect to
94 # see all the xattrs we added before with the same values (and the updated file
95 # data of course). Btrfs used to delete some of these xattrs when it replayed
96 # its fsync log/journal.
97 $XFS_IO_PROG -c "pwrite -S 0xbb 8K 16K" \
98                 -c "fsync" \
99                 $SCRATCH_MNT/foo | _filter_xfs_io
100
101 # Simulate a crash/power loss.
102 _load_flakey_table $FLAKEY_DROP_WRITES
103 _unmount_flakey
104
105 # Allow writes again and mount. This makes the fs replay its fsync log.
106 _load_flakey_table $FLAKEY_ALLOW_WRITES
107 _mount_flakey
108
109 echo "File content after crash and log replay:"
110 od -t x1 $SCRATCH_MNT/foo
111
112 echo "File xattrs after crash and log replay:"
113 for ((i = 1; i <= $num_xattrs; i++)); do
114         name="user.attr_$(printf "%04d" $i)"
115         echo -n "$name="
116         $GETFATTR_PROG --absolute-names -n $name --only-values $SCRATCH_MNT/foo
117         echo
118 done
119
120 status=0
121 exit