generic: test for file loss after mix of rename, fsync and inode eviction
[xfstests-dev.git] / tests / generic / 062
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 062
6 #
7 # Exercises the getfattr/setfattr tools
8 # Derived from tests originally written by Andreas Gruenbacher for ext2
9 #
10 . ./common/preamble
11 _begin_fstest attr udf auto quick
12
13 # Import common functions.
14 . ./common/filter
15 . ./common/attr
16
17 # Override the default cleanup function.
18 _cleanup()
19 {
20         cd /
21         echo; echo "*** unmount"
22         _scratch_unmount 2>/dev/null
23         rm -f $tmp.*
24 }
25
26 getfattr()
27 {
28     _getfattr --absolute-names -dh $@ 2>&1 | _filter_scratch
29 }
30
31 setfattr()
32 {
33     $SETFATTR_PROG $@ 2>&1 | _filter_scratch
34 }
35
36 _create_test_bed()
37 {
38         echo "*** create test bed"
39         touch $SCRATCH_MNT/reg
40         mkdir -p $SCRATCH_MNT/dir
41         ln -s $SCRATCH_MNT/dir $SCRATCH_MNT/lnk
42         mkdir $SCRATCH_MNT/dev
43         mknod $SCRATCH_MNT/dev/b b 0 0
44         mknod $SCRATCH_MNT/dev/c c 1 3
45         mknod $SCRATCH_MNT/dev/p p
46         # sanity check
47         find $SCRATCH_MNT | LC_COLLATE=POSIX sort | _filter_scratch | grep -v "lost+found"
48 }
49
50 # real QA test starts here
51 _supported_fs generic
52
53 _require_scratch
54 _require_attrs
55 _require_symlinks
56 _require_mknod
57
58 rm -f $tmp.backup1 $tmp.backup2 $seqres.full
59
60 # real QA test starts here
61 _scratch_mkfs > /dev/null 2>&1 || _fail "mkfs failed"
62 _scratch_mount
63 _create_test_bed
64
65 # In kernels before 3.0, getxattr() fails with EPERM for an attribute which
66 # cannot exist.  Later kernels fail with ENODATA.  Accept both results.
67 invalid_attribute_filter() {
68         sed -e "s:\(No such attribute\|Operation not permitted\):No such attribute or operation not permitted:"
69 }
70
71 if [ "$USE_ATTR_SECURE" = yes ]; then
72     ATTR_MODES="user security trusted"
73     ATTR_FILTER="^(user|security|trusted)"
74 else
75     ATTR_MODES="user trusted"
76     ATTR_FILTER="^(user|trusted)"
77 fi
78
79 _require_attrs $ATTR_MODES
80
81 for nsp in $ATTR_MODES; do
82         for inode in reg dir lnk dev/b dev/c dev/p; do
83
84                 echo; echo "=== TYPE $inode; NAMESPACE $nsp"; echo
85                 echo "*** set/get one initially empty attribute"
86     
87                 setfattr -h -n $nsp.name $SCRATCH_MNT/$inode
88                 getfattr -m $nsp $SCRATCH_MNT/$inode
89
90                 echo "*** overwrite empty, set several new attributes"
91                 setfattr -h -n $nsp.name -v 0xbabe $SCRATCH_MNT/$inode
92                 setfattr -h -n $nsp.name2 -v 0xdeadbeef $SCRATCH_MNT/$inode
93                 setfattr -h -n $nsp.name3 -v 0xdeface $SCRATCH_MNT/$inode
94
95                 echo "*** fetch several attribute names and values (hex)"
96                 getfattr -m $nsp -e hex $SCRATCH_MNT/$inode
97
98                 echo "*** fetch several attribute names and values (base64)"
99                 getfattr -m $nsp -e base64 $SCRATCH_MNT/$inode
100                 
101                 echo "*** shrink value of an existing attribute"
102                 setfattr -h -n $nsp.name2 -v 0xdeaf $SCRATCH_MNT/$inode
103                 getfattr -m $nsp -e hex $SCRATCH_MNT/$inode
104
105                 echo "*** grow value of existing attribute"
106                 setfattr -h -n $nsp.name2 -v 0xdecade $SCRATCH_MNT/$inode
107                 getfattr -m $nsp -e hex $SCRATCH_MNT/$inode
108                 
109                 echo "*** set an empty value for second attribute"
110                 setfattr -h -n $nsp.name2 $SCRATCH_MNT/$inode
111                 getfattr -m $nsp -n $nsp.name2 $SCRATCH_MNT/$inode 2>&1 | invalid_attribute_filter
112
113                 echo "*** overwrite empty value"
114                 setfattr -h -n $nsp.name2 -v 0xcafe $SCRATCH_MNT/$inode
115                 getfattr -m $nsp -e hex -n $nsp.name2 $SCRATCH_MNT/$inode 2>&1 | invalid_attribute_filter
116
117                 echo "*** remove attribute"
118                 setfattr -h -x $nsp.name2 $SCRATCH_MNT/$inode
119                 getfattr -m $nsp -e hex -n $nsp.name2 $SCRATCH_MNT/$inode 2>&1 | invalid_attribute_filter
120
121                 echo "*** final list (strings, type=$inode, nsp=$nsp)"
122                 getfattr -m $ATTR_FILTER -e hex $SCRATCH_MNT/$inode
123         
124         done
125 done
126
127
128 # Test the directory descent code
129
130 echo; echo
131
132 _extend_test_bed()
133 {
134         echo "*** extend test bed"
135         # must set some descents' attributes to be useful
136         mkdir -p $SCRATCH_MNT/here/up/ascend
137         mkdir -p $SCRATCH_MNT/descend/down/here
138         find $SCRATCH_MNT/descend | xargs setfattr -n user.x -v yz
139         find $SCRATCH_MNT/descend | xargs setfattr -n user.1 -v 23
140         find $SCRATCH_MNT/here | xargs setfattr -n trusted.a -v bc
141         find $SCRATCH_MNT/here | xargs setfattr -n trusted.9 -v 87
142         # whack a symlink in the middle, just to be difficult
143         ln -s $SCRATCH_MNT/here/up $SCRATCH_MNT/descend/and
144         # dump out our new starting point
145         find $SCRATCH_MNT | LC_COLLATE=POSIX sort | _filter_scratch | grep -v "lost+found"
146 }
147
148 _extend_test_bed
149
150 echo
151 echo "*** directory descent with us following symlinks"
152 getfattr -h -L -R -m "$ATTR_FILTER" -e hex $SCRATCH_MNT | _sort_getfattr_output
153
154 echo
155 echo "*** directory descent without following symlinks"
156 getfattr -h -P -R -m "$ATTR_FILTER" -e hex $SCRATCH_MNT | _sort_getfattr_output
157
158
159 # Test the backup/restore code
160
161 echo; echo
162
163 _backup()
164 {
165         # Note: we don't filter scratch here since we need to restore too.  But
166         # we *do* sort the output by path, since it otherwise would depend on
167         # readdir order, which on some filesystems may change after re-creating
168         # the files.
169         _getfattr --absolute-names -dh -R -m $ATTR_FILTER $SCRATCH_MNT | _sort_getfattr_output >$1
170         echo BACKUP $1 >>$seqres.full
171         cat $1 >> $seqres.full
172         [ ! -s $1 ] && echo "warning: $1 (backup file) is empty"
173 }
174
175 echo "*** backup everything"
176 _backup $tmp.backup1
177
178 echo "*** clear out the scratch device"
179 rm -rf $(find $SCRATCH_MNT/* | grep -v "lost+found")
180 echo "AFTER REMOVE" >>$seqres.full
181 getfattr -L -R -m '.' $SCRATCH_MNT >>$seqres.full
182
183 echo "*** reset test bed with no extended attributes"
184 _create_test_bed
185 _extend_test_bed
186
187 echo "*** restore everything"
188 setfattr -h --restore=$tmp.backup1
189 _backup $tmp.backup2
190
191 echo "AFTER RESTORE" >>$seqres.full
192 getfattr -L -R -m '.' $SCRATCH_MNT >>$seqres.full
193
194 echo "*** compare before and after backups"
195 diff $tmp.backup1 $tmp.backup2
196 if [ $? -ne 0 ]; then
197         echo "urk, failed - creating $seq.backup1 and $seq.backup2"
198         cp $tmp.backup1 $seq.backup1 && cp $tmp.backup2 $seq.backup2
199         status=1
200         exit
201 fi
202
203 # success, all done
204 status=0
205 exit