2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
7 # Exercises the getfattr/setfattr tools
8 # Derived from tests originally written by Andreas Gruenbacher for ext2
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
16 status=1 # failure is the default!
18 # get standard environment, filters and checks
26 echo; echo "*** unmount"
27 _scratch_unmount 2>/dev/null
30 trap "_cleanup; exit \$status" 0 1 2 3 15
34 _getfattr --absolute-names -dh $@ 2>&1 | _filter_scratch
39 $SETFATTR_PROG $@ 2>&1 | _filter_scratch
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
53 find $SCRATCH_MNT | LC_COLLATE=POSIX sort | _filter_scratch | grep -v "lost+found"
56 # real QA test starts here
65 rm -f $tmp.backup1 $tmp.backup2 $seqres.full
67 # real QA test starts here
68 _scratch_mkfs > /dev/null 2>&1 || _fail "mkfs failed"
72 # In kernels before 3.0, getxattr() fails with EPERM for an attribute which
73 # cannot exist. Later kernels fail with ENODATA. Accept both results.
74 invalid_attribute_filter() {
75 sed -e "s:\(No such attribute\|Operation not permitted\):No such attribute or operation not permitted:"
78 if [ "$USE_ATTR_SECURE" = yes ]; then
79 ATTR_MODES="user security trusted"
80 ATTR_FILTER="^(user|security|trusted)"
82 ATTR_MODES="user trusted"
83 ATTR_FILTER="^(user|trusted)"
86 _require_attrs $ATTR_MODES
89 for nsp in $ATTR_MODES; do
90 for inode in reg dir lnk dev/b dev/c dev/p; do
92 echo; echo "=== TYPE $inode; NAMESPACE $nsp"; echo
93 echo "*** set/get one initially empty attribute"
95 setfattr -h -n $nsp.name $SCRATCH_MNT/$inode
96 getfattr -m $nsp $SCRATCH_MNT/$inode
98 echo "*** overwrite empty, set several new attributes"
99 setfattr -h -n $nsp.name -v 0xbabe $SCRATCH_MNT/$inode
100 setfattr -h -n $nsp.name2 -v 0xdeadbeef $SCRATCH_MNT/$inode
101 setfattr -h -n $nsp.name3 -v 0xdeface $SCRATCH_MNT/$inode
103 echo "*** fetch several attribute names and values (hex)"
104 getfattr -m $nsp -e hex $SCRATCH_MNT/$inode
106 echo "*** fetch several attribute names and values (base64)"
107 getfattr -m $nsp -e base64 $SCRATCH_MNT/$inode
109 echo "*** shrink value of an existing attribute"
110 setfattr -h -n $nsp.name2 -v 0xdeaf $SCRATCH_MNT/$inode
111 getfattr -m $nsp -e hex $SCRATCH_MNT/$inode
113 echo "*** grow value of existing attribute"
114 setfattr -h -n $nsp.name2 -v 0xdecade $SCRATCH_MNT/$inode
115 getfattr -m $nsp -e hex $SCRATCH_MNT/$inode
117 echo "*** set an empty value for second attribute"
118 setfattr -h -n $nsp.name2 $SCRATCH_MNT/$inode
119 getfattr -m $nsp -n $nsp.name2 $SCRATCH_MNT/$inode 2>&1 | invalid_attribute_filter
121 echo "*** overwrite empty value"
122 setfattr -h -n $nsp.name2 -v 0xcafe $SCRATCH_MNT/$inode
123 getfattr -m $nsp -e hex -n $nsp.name2 $SCRATCH_MNT/$inode 2>&1 | invalid_attribute_filter
125 echo "*** remove attribute"
126 setfattr -h -x $nsp.name2 $SCRATCH_MNT/$inode
127 getfattr -m $nsp -e hex -n $nsp.name2 $SCRATCH_MNT/$inode 2>&1 | invalid_attribute_filter
129 echo "*** final list (strings, type=$inode, nsp=$nsp)"
130 getfattr -m $ATTR_FILTER -e hex $SCRATCH_MNT/$inode
136 # Test the directory descent code
142 echo "*** extend test bed"
143 # must set some descents' attributes to be useful
144 mkdir -p $SCRATCH_MNT/here/up/ascend
145 mkdir -p $SCRATCH_MNT/descend/down/here
146 find $SCRATCH_MNT/descend | xargs setfattr -n user.x -v yz
147 find $SCRATCH_MNT/descend | xargs setfattr -n user.1 -v 23
148 find $SCRATCH_MNT/here | xargs setfattr -n trusted.a -v bc
149 find $SCRATCH_MNT/here | xargs setfattr -n trusted.9 -v 87
150 # whack a symlink in the middle, just to be difficult
151 ln -s $SCRATCH_MNT/here/up $SCRATCH_MNT/descend/and
152 # dump out our new starting point
153 find $SCRATCH_MNT | LC_COLLATE=POSIX sort | _filter_scratch | grep -v "lost+found"
159 echo "*** directory descent with us following symlinks"
160 getfattr -h -L -R -m "$ATTR_FILTER" -e hex $SCRATCH_MNT | _sort_getfattr_output
163 echo "*** directory descent without following symlinks"
164 getfattr -h -P -R -m "$ATTR_FILTER" -e hex $SCRATCH_MNT | _sort_getfattr_output
167 # Test the backup/restore code
173 # Note: we don't filter scratch here since we need to restore too. But
174 # we *do* sort the output by path, since it otherwise would depend on
175 # readdir order, which on some filesystems may change after re-creating
177 _getfattr --absolute-names -dh -R -m $ATTR_FILTER $SCRATCH_MNT | _sort_getfattr_output >$1
178 echo BACKUP $1 >>$seqres.full
179 cat $1 >> $seqres.full
180 [ ! -s $1 ] && echo "warning: $1 (backup file) is empty"
183 echo "*** backup everything"
186 echo "*** clear out the scratch device"
187 rm -rf $(find $SCRATCH_MNT/* | grep -v "lost+found")
188 echo "AFTER REMOVE" >>$seqres.full
189 getfattr -L -R -m '.' $SCRATCH_MNT >>$seqres.full
191 echo "*** reset test bed with no extended attributes"
195 echo "*** restore everything"
196 setfattr -h --restore=$tmp.backup1
199 echo "AFTER RESTORE" >>$seqres.full
200 getfattr -L -R -m '.' $SCRATCH_MNT >>$seqres.full
202 echo "*** compare before and after backups"
203 diff $tmp.backup1 $tmp.backup2
204 if [ $? -ne 0 ]; then
205 echo "urk, failed - creating $seq.backup1 and $seq.backup2"
206 cp $tmp.backup1 $seq.backup1 && cp $tmp.backup2 $seq.backup2