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