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