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