88b28260b995f3c96f8a52c377af0ed56d9bcb21
[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 _require_scratch
61 _require_dm_target flakey
62 _require_attrs
63 _require_metadata_journaling $SCRATCH_DEV
64
65 rm -f $seqres.full
66
67 _scratch_mkfs >> $seqres.full 2>&1
68 _init_flakey
69 _mount_flakey
70
71 # Create the test file with some initial data and make sure everything is
72 # durably persisted.
73 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 32k" $SCRATCH_MNT/foo | _filter_xfs_io
74 sync
75
76 # Add many small xattrs to our file.
77 # We create such a large amount because it's needed to trigger the issue found
78 # in btrfs - we need to have an amount that causes the fs to have at least 3
79 # btree leafs with xattrs stored in them, and it must work on any leaf size
80 # (maximum leaf/node size is 64Kb).
81 num_xattrs=2000
82 for ((i = 1; i <= $num_xattrs; i++)); do
83         name="user.attr_$(printf "%04d" $i)"
84         $SETFATTR_PROG -n $name -v "val_$(printf "%04d" $i)" $SCRATCH_MNT/foo
85 done
86
87 # Sync the filesystem to force a commit of the current btrfs transaction, this
88 # is a necessary condition to trigger the bug on btrfs.
89 sync
90
91 # Now update our file's data and fsync the file.
92 # After a successful fsync, if the fsync log/journal is replayed we expect to
93 # see all the xattrs we added before with the same values (and the updated file
94 # data of course). Btrfs used to delete some of these xattrs when it replayed
95 # its fsync log/journal.
96 $XFS_IO_PROG -c "pwrite -S 0xbb 8K 16K" \
97                 -c "fsync" \
98                 $SCRATCH_MNT/foo | _filter_xfs_io
99
100 _flakey_drop_and_remount
101
102 echo "File content after crash and log replay:"
103 od -t x1 $SCRATCH_MNT/foo
104
105 echo "File xattrs after crash and log replay:"
106 for ((i = 1; i <= $num_xattrs; i++)); do
107         name="user.attr_$(printf "%04d" $i)"
108         echo -n "$name="
109         $GETFATTR_PROG --absolute-names -n $name --only-values $SCRATCH_MNT/foo
110         echo
111 done
112
113 status=0
114 exit