xfs/530: skip test if user MKFS_OPTIONS screw up formatting
[xfstests-dev.git] / common / attr
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # common extended attribute and ACL support
6
7 # filesystems that want to test maximum supported acl counts need to
8 # add support in here
9 _acl_get_max()
10 {
11         case $FSTYP in
12         xfs)
13                 # CRC format filesystems have much larger ACL counts. The actual
14                 # number is into the thousands, but testing that meany takes too
15                 # long, so just test well past the old limit of 25.
16                 $XFS_INFO_PROG $TEST_DIR | _filter_mkfs > /dev/null 2> $tmp.info
17                 . $tmp.info
18                 rm $tmp.info
19                 if [ $_fs_has_crcs -eq 0 ]; then
20                         echo 25
21                 else
22                         echo 5461
23                 fi
24                 ;;
25         jfs)
26                 echo 8191
27                 ;;
28         f2fs)
29                 _fs_options $TEST_DEV | grep "inline_xattr" >/dev/null 2>&1
30                 if [ $? -eq 0 ]; then
31                         echo 531
32                 else
33                         echo 506
34                 fi
35                 ;;
36         bcachefs)
37                 echo 251
38                 ;;
39         *)
40                 echo 0
41                 ;;
42         esac
43 }
44
45 _require_acl_get_max()
46 {
47         if [ $(_acl_get_max) -eq 0 ]; then
48                 _notrun "$FSTYP does not define maximum ACL count"
49         fi
50 }
51
52 # pick three unused user/group ids, store them as $acl[1-3]
53 #
54 _acl_setup_ids()
55 {
56     eval `(_cat_passwd; _cat_group) | awk -F: '
57       { ids[$3]=1 }
58       END {
59         j=1
60         for(i=1; i<1000000 && j<=3;i++){
61           if (! (i in ids)) {
62              printf "acl%d=%d;", j, i;           
63              j++
64           }
65         }       
66       }'`
67 }
68
69 # filter for the acl ids selected above
70 #
71 _acl_filter_id()
72 {
73     sed \
74        -e "s/u:$acl1/u:id1/" \
75        -e "s/u:$acl2/u:id2/" \
76        -e "s/u:$acl3/u:id3/" \
77        -e "s/g:$acl1/g:id1/" \
78        -e "s/g:$acl2/g:id2/" \
79        -e "s/g:$acl3/g:id3/" \
80        -e "s/ $acl1 / id1 /" \
81        -e "s/ $acl2 / id2 /" \
82        -e "s/ $acl3 / id3 /"
83 }
84
85 _getfacl_filter_id()
86 {
87     sed \
88        -e "s/user:$acl1/user:id1/" \
89        -e "s/user:$acl2/user:id2/" \
90        -e "s/user:$acl3/user:id3/" \
91        -e "s/group:$acl1/group:id1/" \
92        -e "s/group:$acl2/group:id2/" \
93        -e "s/group:$acl3/group:id3/" \
94        -e "s/: $acl1/: id1/" \
95        -e "s/: $acl2/: id2/" \
96        -e "s/: $acl3/: id3/"
97 }
98
99 # filtered ls
100 #
101 _acl_ls()
102 {
103     _ls_l -n $* | awk '{ print $1, $3, $4, $NF }' | _acl_filter_id
104
105
106 # create an ACL with n ACEs in it
107 #
108 _create_n_aces()
109 {
110     let n=$1-4
111     acl='u::rwx,g::rwx,o::rwx,m::rwx' # 4 ace acl start
112     while [ $n -ne 0 ]; do
113         acl="$acl,u:$n:rwx"
114         let n=$n-1
115     done
116     echo $acl
117 }
118
119 # filter user ace names to user ids
120 #
121 _filter_aces()
122 {
123     tmp_file=`mktemp /tmp/ace.XXXXXX`
124
125     (_cat_passwd; _cat_group) > $tmp_file
126
127     $AWK_PROG -v tmpfile=$tmp_file '
128         BEGIN {
129             FS=":"
130             while ( getline <tmpfile > 0 ) {
131                 idlist[$1] = $3 
132             }
133         }
134         /^user/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
135         /^u/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
136         /^default:user/ { if ($3 in idlist) sub($3, idlist[$3]); print; next}
137         {print}
138     '
139     rm -f $tmp_file
140 }
141
142 _filter_aces_notypes()
143 {
144     tr '\[' '\012' | tr ']' '\012' | tr ',' '\012' | _filter_aces|\
145     sed -e 's/u:/user:/' -e 's/g:/group:/' -e 's/o:/other:/' -e 's/m:/mask:/'
146 }
147
148 _require_acls()
149 {
150     [ -n "$CHACL_PROG" ] || _notrun "chacl command not found"
151
152     #
153     # Test if chacl is able to list ACLs on the target filesystems.  On really
154     # old kernels the system calls might not be implemented at all, but the
155     # more common case is that the tested filesystem simply doesn't support
156     # ACLs.
157     #
158     touch $TEST_DIR/syscalltest
159     chacl -l $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
160     cat $TEST_DIR/syscalltest.out >> $seqres.full
161
162     if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
163       _notrun "kernel does not support ACLs"
164     fi
165     if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
166       _notrun "ACLs not supported by this filesystem type: $FSTYP"
167     fi
168
169     rm -f $TEST_DIR/syscalltest.out
170 }
171
172 _list_acl()
173 {
174     file=$1
175
176     ls -dD $file | _acl_filter_id
177 }
178
179 _require_attrs()
180 {
181         local args
182         local nsp
183         
184         if [ $# -eq 0 ]; then
185                 args="user"
186         else
187                 args="$*"
188         fi
189         
190         [ -n "$ATTR_PROG" ] || _notrun "attr command not found"
191         [ -n "$GETFATTR_PROG" ] || _notrun "getfattr command not found"
192         [ -n "$SETFATTR_PROG" ] || _notrun "setfattr command not found"
193         
194         for nsp in $args; do
195                 #
196                 # Test if chacl is able to write an attribute on the target
197                 # filesystems.  On really old kernels the system calls might
198                 # not be implemented at all, but the more common case is that
199                 # the tested filesystem simply doesn't support attributes.
200                 # Note that we can't simply list attributes as various security
201                 # modules generate synthetic attributes not actually stored on
202                 # disk.
203                 #
204                 touch $TEST_DIR/syscalltest
205                 $SETFATTR_PROG -n "$nsp.xfstests" -v "attr" $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
206                 cat $TEST_DIR/syscalltest.out >> $seqres.full
207                 
208                 if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
209                         _notrun "kernel does not support attrs"
210                 fi
211                 if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
212                         _notrun "attr namespace $nsp not supported by this filesystem type: $FSTYP"
213                 fi
214                 
215                 rm -f $TEST_DIR/syscalltest.out
216         done
217 }
218
219 _require_attr_v1()
220 {
221         _scratch_mkfs_xfs_supported -i attr=1 >/dev/null 2>&1 \
222                 || _notrun "attr v1 not supported on $SCRATCH_DEV"
223 }
224
225 # check if we support the noattr2 mount option
226 _require_noattr2()
227 {
228         _scratch_mkfs_xfs > /dev/null 2>&1 \
229                 || _fail "_scratch_mkfs_xfs failed on $SCRATCH_DEV"
230         _try_scratch_mount -o noattr2 > /dev/null 2>&1 \
231                 || _notrun "noattr2 mount option not supported on $SCRATCH_DEV"
232         _scratch_unmount
233 }
234
235 # getfattr -R returns info in readdir order which varies from fs to fs.
236 # This sorts the output by filename
237 _sort_getfattr_output()
238 {
239     awk '{a[FNR]=$0}END{n = asort(a); for(i=1; i <= n; i++) print a[i]"\n"}' RS=''
240 }
241
242 # Previously, when getfattr dumps values of all extended attributes, it prints
243 # empty attr as 'user.name', but new getfattr (since attr-2.4.48) prints it as
244 # 'user.name=""'. Filter out the ending '=""' so that both old and new getfattr
245 # pints the same output.
246 #
247 # Note: This function returns the getfattr command result.
248 _getfattr()
249 {
250         $GETFATTR_PROG "$@" | sed -e 's/=\"\"//'
251         return ${PIPESTATUS[0]}
252 }
253
254 # set maximum total attr space based on fs type
255 case "$FSTYP" in
256 xfs|udf|pvfs2|9p|ceph|nfs)
257         MAX_ATTRS=1000
258         ;;
259 *)
260         # Assume max ~1 block of attrs
261         BLOCK_SIZE=`_get_block_size $TEST_DIR`
262         # user.attribute_XXX="value.XXX" is about 32 bytes; leave some overhead
263         let MAX_ATTRS=$BLOCK_SIZE/40
264 esac
265
266 export MAX_ATTRS
267
268 # Set max attr value size based on fs type
269 case "$FSTYP" in
270 xfs|udf|btrfs)
271         MAX_ATTRVAL_SIZE=64
272         ;;
273 pvfs2)
274         MAX_ATTRVAL_SIZE=8192
275         ;;
276 9p|ceph|nfs)
277         MAX_ATTRVAL_SIZE=65536
278         ;;
279 bcachefs)
280         MAX_ATTRVAL_SIZE=1024
281         ;;
282 *)
283         # Assume max ~1 block of attrs
284         BLOCK_SIZE=`_get_block_size $TEST_DIR`
285         # leave a little overhead
286         let MAX_ATTRVAL_SIZE=$BLOCK_SIZE-256
287 esac
288
289 export MAX_ATTRVAL_SIZE
290 # make sure this script returns success
291 /bin/true