ext4: the _require checks for bigalloc need a larger fs size
[xfstests-dev.git] / common / rc
1 ##/bin/bash
2 #-----------------------------------------------------------------------
3 #  Copyright (c) 2000-2006 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
23 BC=$(which bc 2> /dev/null) || BC=
24
25 _require_math()
26 {
27         if [ -z "$BC" ]; then
28                 _notrun "this test requires 'bc' tool for doing math operations"
29         fi
30 }
31
32 _math()
33 {
34         [ $# -le 0 ] && return
35         if [ "$BC" ]; then
36                 result=$(LANG=C echo "scale=0; $@" | "$BC" -q 2> /dev/null)
37         else
38                 _notrun "this test requires 'bc' tool for doing math operations"
39         fi
40         echo "$result"
41 }
42
43 dd()
44 {
45    if [ "$HOSTOS" == "Linux" ]
46    then 
47         command dd --help 2>&1 | grep noxfer >/dev/null
48         
49         if [ "$?" -eq 0 ]
50             then
51                 command dd status=noxfer $@
52             else
53                 command dd $@
54         fi
55    else
56         command dd $@
57    fi
58 }
59
60 _btrfs_get_subvolid()
61 {
62         mnt=$1
63         name=$2
64
65         $BTRFS_UTIL_PROG sub list $mnt | grep $name | awk '{ print $2 }'
66 }
67
68 # Prints the md5 checksum of a given file
69 _md5_checksum()
70 {
71         md5sum $1 | cut -d ' ' -f1
72 }
73
74
75 # ls -l w/ selinux sometimes puts a dot at the end:
76 # -rwxrw-r--. id1 id2 file1
77 # Also filter out lost+found directory on extN file system if present
78
79 _ls_l()
80 {
81         ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /" | grep -v 'lost+found'
82 }
83
84 # we need common/config
85 if [ "$iam" != "check" ]
86 then
87     if ! . ./common/config
88         then
89         echo "$iam: failed to source common/config"
90         exit 1
91     fi
92 fi
93
94 # check for correct setup
95 case "$FSTYP" in
96     xfs)
97          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
98          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
99          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
100          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
101          ;;
102     udf)
103          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
104          ;;
105     btrfs)
106          [ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
107          ;;
108     ext4)
109          [ "$MKFS_EXT4_PROG" = "" ] && _fatal "mkfs.ext4 not found"
110          ;;
111     nfs)
112          ;;
113     cifs)
114          ;;
115 esac
116
117 # make sure we have a standard umask
118 umask 022
119
120 _mount()
121 {
122     $MOUNT_PROG `_mount_ops_filter $*`
123 }
124
125 _scratch_options()
126 {
127     type=$1
128     SCRATCH_OPTIONS=""
129
130     if [ "$FSTYP" != "xfs" ]; then
131         return
132     fi
133
134     case $type in
135     mkfs)
136         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
137         rt_opt="-r"
138         log_opt="-l"
139         ;;
140     mount)
141         rt_opt="-o"
142         log_opt="-o"
143         ;;
144     esac
145     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
146         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
147     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
148         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
149 }
150
151 _test_options()
152 {
153     type=$1
154     TEST_OPTIONS=""
155
156     if [ "$FSTYP" != "xfs" ]; then
157         return
158     fi
159
160     case $type in
161     mkfs)
162         rt_opt="-r"
163         log_opt="-l"
164         ;;
165     mount)
166         rt_opt="-o"
167         log_opt="-o"
168         ;;
169     esac
170     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
171         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
172     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
173         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
174 }
175
176 _mount_ops_filter()
177 {
178     params="$*"
179     
180     #get mount point to handle dmapi mtpt option correctly
181     let last_index=$#-1
182     [ $last_index -gt 0 ] && shift $last_index
183     FS_ESCAPED=$1
184     
185     # irix is fussy about how it is fed its mount options
186     # - multiple -o's are not allowed
187     # - no spaces between comma delimitered options
188     # the sed script replaces all -o's (except the first) with a comma
189     # not required for linux, but won't hurt
190     
191     echo $params | sed -e 's/[[:space:]]\+-o[[:space:]]*/UnIqUe/1; s/[[:space:]]\+-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
192         | sed -e 's/dmapi/dmi/' \
193         | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"
194
195 }
196
197 _scratch_mount_options()
198 {
199     _scratch_options mount
200
201     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
202 }
203
204 _scratch_mount()
205 {
206     _mount -t $FSTYP `_scratch_mount_options $*`
207 }
208
209 _scratch_unmount()
210 {
211     $UMOUNT_PROG $SCRATCH_DEV
212 }
213
214 _scratch_remount()
215 {
216     _scratch_unmount
217     _scratch_mount
218 }
219
220 _test_mount()
221 {
222     _test_options mount
223     _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
224 }
225
226 _test_remount()
227 {
228     $UMOUNT_PROG $TEST_DEV
229     _test_mount
230 }
231
232 _scratch_mkfs_options()
233 {
234     _scratch_options mkfs
235     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
236 }
237
238 _scratch_metadump()
239 {
240         dumpfile=$1
241         options=
242
243         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
244                 options="-l $SCRATCH_LOGDEV"
245
246         xfs_metadump $options $SCRATCH_DEV $dumpfile
247 }
248
249 _setup_large_xfs_fs()
250 {
251         fs_size=$1
252         local tmp_dir=/tmp/
253
254         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
255         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
256         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
257
258         # calculate the size of the file we need to allocate.
259         # Default free space in the FS is 50GB, but you can specify more via
260         # SCRATCH_DEV_EMPTY_SPACE
261         file_size=$(($fs_size - 50*1024*1024*1024))
262         file_size=$(($file_size - $SCRATCH_DEV_EMPTY_SPACE))
263
264         # mount the filesystem, create the file, unmount it
265         _scratch_mount 2>&1 >$tmp_dir/mnt.err
266         local status=$?
267         if [ $status -ne 0 ]; then
268                 echo "mount failed"
269                 cat $tmp_dir/mnt.err >&2
270                 rm -f $tmp_dir/mnt.err
271                 return $status
272         fi
273         rm -f $tmp_dir/mnt.err
274
275         xfs_io -F -f \
276                 -c "truncate $file_size" \
277                 -c "falloc -k 0 $file_size" \
278                 -c "chattr +d" \
279                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
280         export NUM_SPACE_FILES=1
281         status=$?
282         umount $SCRATCH_MNT
283         if [ $status -ne 0 ]; then
284                 echo "large file prealloc failed"
285                 cat $tmp_dir/mnt.err >&2
286                 return $status
287         fi
288         return 0
289 }
290
291 _scratch_mkfs_xfs_opts()
292 {
293         mkfs_opts=$*
294
295         _scratch_options mkfs
296
297         $MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
298 }
299
300
301 _scratch_mkfs_xfs_supported()
302 {
303         mkfs_opts=$*
304
305         _scratch_options mkfs
306
307         $MKFS_XFS_PROG -N $MKFS_OPTIONS $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
308 }
309
310 _scratch_mkfs_xfs()
311 {
312         # extra mkfs options can be added by tests
313         local extra_mkfs_options=$*
314
315         local tmp_dir=/tmp/
316
317         # save mkfs output in case conflict means we need to run again.
318         # only the output for the mkfs that applies should be shown
319         _scratch_mkfs_xfs_opts $MKFS_OPTIONS $extra_mkfs_options \
320                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
321         local mkfs_status=$?
322
323
324         # a mkfs failure may be caused by conflicts between
325         # $MKFS_OPTIONS and $extra_mkfs_options
326         if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
327                 (
328                 echo -n "** mkfs failed with extra mkfs options "
329                 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
330                 echo -n "** attempting to mkfs using only test $seq "
331                 echo "options: $extra_mkfs_options **"
332                 ) >> $seqres.full
333
334                 # running mkfs again. overwrite previous mkfs output files
335                 _scratch_mkfs_xfs_opts $extra_mkfs_options \
336                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
337                 local mkfs_status=$?
338         fi
339
340         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
341                 # manually parse the mkfs output to get the fs size in bytes
342                 local fs_size
343                 fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
344                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
345                                 my $size = $1 * $2;
346                                 print STDOUT "$size\n";
347                         }'`
348                 _setup_large_xfs_fs $fs_size
349                 mkfs_status=$?
350         fi
351
352         # output stored mkfs output
353         cat $tmp_dir.mkfserr >&2
354         cat $tmp_dir.mkfsstd
355         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
356
357         return $mkfs_status
358 }
359
360 # xfs_check script is planned to be deprecated. But, we want to
361 # be able to invoke "xfs_check" behavior in xfstests in order to
362 # maintain the current verification levels.
363 _xfs_check()
364 {
365     OPTS=" "
366     DBOPTS=" "
367     USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
368
369     while getopts "b:fi:l:stvV" c
370     do
371         case $c in
372             s) OPTS=$OPTS"-s ";;
373             t) OPTS=$OPTS"-t ";;
374             v) OPTS=$OPTS"-v ";;
375             i) OPTS=$OPTS"-i "$OPTARG" ";;
376             b) OPTS=$OPTS"-b "$OPTARG" ";;
377             f) DBOPTS=$DBOPTS" -f";;
378             l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
379             V) $XFS_DB_PROG -p xfs_check -V
380                 return $?
381                 ;;
382         esac
383     done
384     set -- extra $@
385     shift $OPTIND
386     case $# in
387         1)    ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
388                status=$?
389                ;;
390         2)    echo $USAGE 1>&1
391               status=2
392               ;;
393     esac
394     return $status
395 }
396
397 _setup_large_ext4_fs()
398 {
399         fs_size=$1
400         local tmp_dir=/tmp/
401
402         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
403         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
404         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
405
406         # Default free space in the FS is 50GB, but you can specify more via
407         # SCRATCH_DEV_EMPTY_SPACE
408         space_to_consume=$(($fs_size - 50*1024*1024*1024 - $SCRATCH_DEV_EMPTY_SPACE))
409
410         # mount the filesystem and create 16TB - 4KB files until we consume
411         # all the necessary space.
412         _scratch_mount 2>&1 >$tmp_dir/mnt.err
413         local status=$?
414         if [ $status -ne 0 ]; then
415                 echo "mount failed"
416                 cat $tmp_dir/mnt.err >&2
417                 rm -f $tmp_dir/mnt.err
418                 return $status
419         fi
420         rm -f $tmp_dir/mnt.err
421
422         file_size=$((16*1024*1024*1024*1024 - 4096))
423         nfiles=0
424         while [ $space_to_consume -gt $file_size ]; do
425
426                 xfs_io -F -f \
427                         -c "truncate $file_size" \
428                         -c "falloc -k 0 $file_size" \
429                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
430                 status=$?
431                 if [ $status -ne 0 ]; then
432                         break;
433                 fi
434
435                 space_to_consume=$(( $space_to_consume - $file_size ))
436                 nfiles=$(($nfiles + 1))
437         done
438
439         # consume the remaining space.
440         if [ $space_to_consume -gt 0 ]; then
441                 xfs_io -F -f \
442                         -c "truncate $space_to_consume" \
443                         -c "falloc -k 0 $space_to_consume" \
444                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
445                 status=$?
446         fi
447         export NUM_SPACE_FILES=$nfiles
448
449         umount $SCRATCH_MNT
450         if [ $status -ne 0 ]; then
451                 echo "large file prealloc failed"
452                 cat $tmp_dir/mnt.err >&2
453                 return $status
454         fi
455         return 0
456 }
457
458 _scratch_mkfs_ext4()
459 {
460         # extra mkfs options can be added by tests
461         local extra_mkfs_options=$*
462
463         local tmp_dir=/tmp/
464
465         $MKFS_EXT4_PROG -F $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
466                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
467         local mkfs_status=$?
468
469         # a mkfs failure may be caused by conflicts between
470         # $MKFS_OPTIONS and $extra_mkfs_options
471         if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
472                 (
473                 echo -n "** mkfs failed with extra mkfs options "
474                 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
475                 echo -n "** attempting to mkfs using only test $seq "
476                 echo "options: $extra_mkfs_options **"
477                 ) >> $seqres.full
478
479                 # running mkfs again. overwrite previous mkfs output files
480                 $MKFS_EXT4_PROG -F $extra_mkfs_options $SCRATCH_DEV \
481                                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
482                 local mkfs_status=$?
483         fi
484
485         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
486                 # manually parse the mkfs output to get the fs size in bytes
487                 fs_size=`cat $tmp_dir.mkfsstd | awk ' \
488                         /^Block size/ { split($2, a, "="); bs = a[2] ; } \
489                         / inodes, / { blks = $3 } \
490                         /reserved for the super user/ { resv = $1 } \
491                         END { fssize = bs * blks - resv; print fssize }'`
492
493                 _setup_large_ext4_fs $fs_size
494                 mkfs_status=$?
495         fi
496
497         # output stored mkfs output
498         cat $tmp_dir.mkfserr >&2
499         cat $tmp_dir.mkfsstd
500         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
501
502         return $mkfs_status
503 }
504
505 _test_mkfs()
506 {
507     case $FSTYP in
508     nfs*)
509         # do nothing for nfs
510         ;;
511     cifs)
512         # do nothing for cifs
513         ;;
514     udf)
515         $MKFS_UDF_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
516         ;;
517     btrfs)
518         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
519         ;;
520     *)
521         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $TEST_DEV
522         ;;
523     esac
524 }
525
526 _mkfs_dev()
527 {
528     case $FSTYP in
529     nfs*)
530         # do nothing for nfs
531         ;;
532     udf)
533         $MKFS_UDF_PROG $MKFS_OPTIONS $* 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
534         ;;
535     btrfs)
536         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
537         ;;
538     *)
539         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* \
540                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
541         ;;
542     esac
543
544     if [ $? -ne 0 ]; then
545         # output stored mkfs output
546         cat $tmp_dir.mkfserr >&2
547         cat $tmp_dir.mkfsstd
548         status=1
549         exit 1
550     fi
551     rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
552 }
553
554 # remove all files in $SCRATCH_MNT, useful when testing on NFS/CIFS
555 _scratch_cleanup_files()
556 {
557         _scratch_mount
558         rm -rf $SCRATCH_MNT/*
559         _scratch_unmount
560 }
561
562 _scratch_mkfs()
563 {
564     case $FSTYP in
565     xfs)
566         _scratch_mkfs_xfs $*
567         ;;
568     nfs*)
569         # unable to re-create NFS, just remove all files in $SCRATCH_MNT to
570         # avoid EEXIST caused by the leftover files created in previous runs
571         _scratch_cleanup_files
572         ;;
573     cifs)
574         # unable to re-create CIFS, just remove all files in $SCRATCH_MNT to
575         # avoid EEXIST caused by the leftover files created in previous runs
576         _scratch_cleanup_files
577         ;;
578     udf)
579         $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
580         ;;
581     btrfs)
582         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
583         ;;
584     ext4)
585         _scratch_mkfs_ext4 $*
586         ;;
587     tmpfs)
588         # do nothing for tmpfs
589         ;;
590     *)
591         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV
592         ;;
593     esac
594 }
595
596 _scratch_pool_mkfs()
597 {
598     case $FSTYP in
599     btrfs)
600         # if dup profile is in mkfs options call _scratch_mkfs instead
601         # because dup profile only works with single device
602         if [[ "$*" =~ dup ]]; then
603             _scratch_mkfs $*
604         else
605             $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV_POOL > /dev/null
606         fi
607         ;;
608     *)
609         echo "_scratch_pool_mkfs is not implemented for $FSTYP" 1>&2
610         ;;
611     esac
612 }
613
614 # Create fs of certain size on scratch device
615 # _scratch_mkfs_sized <size in bytes> [optional blocksize]
616 _scratch_mkfs_sized()
617 {
618     fssize=$1
619     blocksize=$2
620
621     case $FSTYP in
622     xfs)
623         def_blksz=`echo $MKFS_OPTIONS|sed -rn 's/.*-b ?size= ?+([0-9]+).*/\1/p'`
624         ;;
625     ext2|ext3|ext4|ext4dev|udf|btrfs)
626         def_blksz=`echo $MKFS_OPTIONS| sed -rn 's/.*-b ?+([0-9]+).*/\1/p'`
627         ;;
628     esac
629
630     [ -n "$def_blksz" ] && blocksize=$def_blksz
631     [ -z "$blocksize" ] && blocksize=4096
632
633
634     re='^[0-9]+$'
635     if ! [[ $fssize =~ $re ]] ; then
636         _notrun "error: _scratch_mkfs_sized: fs size \"$fssize\" not an integer."
637     fi
638     if ! [[ $blocksize =~ $re ]] ; then
639         _notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
640     fi
641
642     blocks=`expr $fssize / $blocksize`
643
644     if [ "$HOSTOS" == "Linux" ]; then
645         devsize=`blockdev --getsize64 $SCRATCH_DEV`
646         [ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
647     fi
648
649     case $FSTYP in
650     xfs)
651         # don't override MKFS_OPTIONS that set a block size.
652         echo $MKFS_OPTIONS |egrep -q "b?size="
653         if [ $? -eq 0 ]; then
654                 _scratch_mkfs_xfs -d size=$fssize
655         else
656                 _scratch_mkfs_xfs -d size=$fssize -b size=$blocksize
657         fi
658         ;;
659     ext2|ext3|ext4|ext4dev)
660         yes | ${MKFS_PROG}.$FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
661         ;;
662     udf)
663         $MKFS_UDF_PROG $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
664         ;;
665     btrfs)
666         $MKFS_BTRFS_PROG $MKFS_OPTIONS -b $fssize $SCRATCH_DEV
667         ;;
668     *)
669         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
670         ;;
671     esac
672 }
673
674 # Emulate an N-data-disk stripe w/ various stripe units
675 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
676 _scratch_mkfs_geom()
677 {
678     sunit_bytes=$1
679     swidth_mult=$2
680     blocksize=$3
681     [ -z "$blocksize" ] && blocksize=4096
682
683     let sunit_blocks=$sunit_bytes/$blocksize
684     let swidth_blocks=$sunit_blocks*$swidth_mult
685
686     case $FSTYP in
687     xfs)
688         MKFS_OPTIONS+=" -b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
689         ;;
690     ext4|ext4dev)
691         MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
692         ;;
693     *)
694         _notrun "can't mkfs $FSTYP with geometry"
695         ;;
696     esac
697     _scratch_mkfs
698 }
699
700 _scratch_resvblks()
701 {
702         case $FSTYP in
703         xfs)
704                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
705                 ;;
706         *)
707                 ;;
708         esac
709 }
710
711 _scratch_xfs_db_options()
712 {
713     SCRATCH_OPTIONS=""
714     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
715         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
716     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
717 }
718
719 _scratch_xfs_logprint()
720 {
721     SCRATCH_OPTIONS=""
722     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
723         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
724     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
725 }
726
727 _scratch_xfs_check()
728 {
729     SCRATCH_OPTIONS=""
730     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
731         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
732     [ "$LARGE_SCRATCH_DEV" = yes ] && \
733         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
734     _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
735 }
736
737 _scratch_xfs_repair()
738 {
739     SCRATCH_OPTIONS=""
740     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
741         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
742     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
743         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
744     [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
745     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
746 }
747
748 _get_pids_by_name()
749 {
750     if [ $# -ne 1 ]
751     then
752         echo "Usage: _get_pids_by_name process-name" 1>&2
753         exit 1
754     fi
755
756     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
757     # HH:MM:SS before the psargs field, use this as the search anchor.
758     #
759     # Matches with $1 (process-name) occur if the first psarg is $1
760     # or ends in /$1 ... the matching uses sed's regular expressions,
761     # so passing a regex into $1 will work.
762
763     ps $PS_ALL_FLAGS \
764     | sed -n \
765         -e 's/$/ /' \
766         -e 's/[         ][      ]*/ /g' \
767         -e 's/^ //' \
768         -e 's/^[^ ]* //' \
769         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
770         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
771 }
772
773 # fix malloc libs output
774 #
775 _fix_malloc()
776 {
777     # filter out the Electric Fence notice
778     $PERL_PROG -e '
779         while (<>) {
780             if (defined $o && /^\s+Electric Fence/) {
781                 chomp($o);
782                 print "$o";
783                 undef $o;
784                 next;
785             }
786             print $o if (defined $o);
787
788             $o=$_;
789         }
790         print $o if (defined $o);
791     '
792 }
793
794 # check if run as root
795 #
796 _need_to_be_root()
797 {
798     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
799     if [ "$id" -ne 0 ]
800     then
801         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
802         exit 1
803     fi
804 }
805
806
807 #
808 # _df_device : get an IRIX style df line for a given device
809 #
810 #       - returns "" if not mounted
811 #       - returns fs type in field two (ala IRIX)
812 #       - joins line together if split by fancy df formatting
813 #       - strips header etc
814 #
815
816 _df_device()
817 {
818     if [ $# -ne 1 ]
819     then
820         echo "Usage: _df_device device" 1>&2
821         exit 1
822     fi
823
824     # Note that we use "==" here so awk doesn't try to interpret an NFS over
825     # IPv6 server as a regular expression.
826     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
827         ($1==what) && (NF==1) {
828             v=$1
829             getline
830             print v, $0
831             exit
832         }
833         ($1==what) {
834             print
835             exit
836         }
837     '
838 }
839
840 #
841 # _df_dir : get an IRIX style df line for device where a directory resides
842 #
843 #       - returns fs type in field two (ala IRIX)
844 #       - joins line together if split by fancy df formatting
845 #       - strips header etc
846 #
847
848 _df_dir()
849 {
850     if [ $# -ne 1 ]
851     then
852         echo "Usage: _df_dir device" 1>&2
853         exit 1
854     fi
855
856     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
857         NR == 2 && NF==1 {
858             v=$1
859             getline
860             print v, $0;
861             exit 0
862         }
863         NR == 2 {
864             print;
865             exit 0
866         }
867         {}
868     '
869     # otherwise, nada
870 }
871
872 # return percentage used disk space for mounted device
873
874 _used()
875 {
876     if [ $# -ne 1 ]
877     then
878         echo "Usage: _used device" 1>&2
879         exit 1
880     fi
881
882     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
883 }
884
885 # return the FS type of a mounted device
886 #
887 _fs_type()
888 {
889     if [ $# -ne 1 ]
890     then
891         echo "Usage: _fs_type device" 1>&2
892         exit 1
893     fi
894
895     #
896     # The Linux kernel shows NFSv4 filesystems in df output as
897     # filesystem type nfs4, although we mounted it as nfs earlier.
898     # Fix the filesystem type up here so that the callers don't
899     # have to bother with this quirk.
900     #
901     _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
902 }
903
904 # return the FS mount options of a mounted device
905 #
906 # should write a version which just parses the output of mount for IRIX
907 # compatibility, but since this isn't used at all, at the moment I'll leave
908 # this for now
909 #
910 _fs_options()
911 {
912     if [ $# -ne 1 ]
913     then
914         echo "Usage: _fs_options device" 1>&2
915         exit 1
916     fi
917
918     $AWK_PROG -v dev=$1 '
919         match($1,dev) { print $4 }
920     ' </proc/mounts
921 }
922
923 # returns device number if a file is a block device
924 #
925 _is_block_dev()
926 {
927     if [ $# -ne 1 ]
928     then
929         echo "Usage: _is_block_dev dev" 1>&2
930         exit 1
931     fi
932
933     _dev=$1
934     if [ -L "${_dev}" ]; then
935         _dev=`readlink -f ${_dev}`
936     fi
937
938     if [ -b "${_dev}" ]; then
939         src/lstat64 ${_dev} | $AWK_PROG '/Device type:/ { print $9 }'
940     fi
941 }
942
943 # Do a command, log it to $seqres.full, optionally test return status
944 # and die if command fails. If called with one argument _do executes the
945 # command, logs it, and returns its exit status. With two arguments _do
946 # first prints the message passed in the first argument, and then "done"
947 # or "fail" depending on the return status of the command passed in the
948 # second argument. If the command fails and the variable _do_die_on_error
949 # is set to "always" or the two argument form is used and _do_die_on_error
950 # is set to "message_only" _do will print an error message to
951 # $seqres.out and exit.
952
953 _do()
954 {
955     if [ $# -eq 1 ]; then
956         _cmd=$1
957     elif [ $# -eq 2 ]; then
958         _note=$1
959         _cmd=$2
960         echo -n "$_note... "
961     else
962         echo "Usage: _do [note] cmd" 1>&2
963         status=1; exit
964     fi
965
966     (eval "echo '---' \"$_cmd\"") >>$seqres.full
967     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
968     cat $tmp._out | _fix_malloc >>$seqres.full
969     if [ $# -eq 2 ]; then
970         if [ $ret -eq 0 ]; then
971             echo "done"
972         else
973             echo "fail"
974         fi
975     fi
976     if [ $ret -ne 0  ] \
977         && [ "$_do_die_on_error" = "always" \
978             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
979     then
980         [ $# -ne 2 ] && echo
981         eval "echo \"$_cmd\" failed \(returned $ret\): see $seqres.full"
982         status=1; exit
983     fi
984
985     return $ret
986 }
987
988 # bail out, setting up .notrun file. Need to kill the filesystem check files
989 # here, otherwise they are set incorrectly for the next test.
990 #
991 _notrun()
992 {
993     echo "$*" > $seqres.notrun
994     echo "$seq not run: $*"
995     rm -f ${RESULT_DIR}/require_test
996     rm -f ${RESULT_DIR}/require_scratch
997     status=0
998     exit
999 }
1000
1001 # just plain bail out
1002 #
1003 _fail()
1004 {
1005     echo "$*" | tee -a $seqres.full
1006     echo "(see $seqres.full for details)"
1007     status=1
1008     exit 1
1009 }
1010
1011 # tests whether $FSTYP is one of the supported filesystems for a test
1012 #
1013 _supported_fs()
1014 {
1015     for f
1016     do
1017         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
1018         then
1019             return
1020         fi
1021     done
1022
1023     _notrun "not suitable for this filesystem type: $FSTYP"
1024 }
1025
1026
1027 # tests whether $FSTYP is one of the supported OSes for a test
1028 #
1029 _supported_os()
1030 {
1031     for h
1032     do
1033         if [ "$h" = "$HOSTOS" ]
1034         then
1035             return
1036         fi
1037     done
1038
1039     _notrun "not suitable for this OS: $HOSTOS"
1040 }
1041
1042 # this test needs a scratch partition - check we're ok & unmount it
1043 # No post-test check of the device is required. e.g. the test intentionally
1044 # finishes the test with the filesystem in a corrupt state
1045 _require_scratch_nocheck()
1046 {
1047     case "$FSTYP" in
1048         nfs*)
1049                 echo $SCRATCH_DEV | grep -q ":/" > /dev/null 2>&1
1050                 if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
1051                         _notrun "this test requires a valid \$SCRATCH_DEV"
1052                 fi
1053                 if [ ! -d "$SCRATCH_MNT" ]; then
1054                         _notrun "this test requires a valid \$SCRATCH_MNT"
1055                 fi
1056                 ;;
1057         cifs)
1058                 echo $SCRATCH_DEV | grep -q "//" > /dev/null 2>&1
1059                 if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
1060                         _notrun "this test requires a valid \$SCRATCH_DEV"
1061                 fi
1062                 if [ ! -d "$SCRATCH_MNT" ]; then
1063                      _notrun "this test requires a valid \$SCRATCH_MNT"
1064                 fi
1065                 ;;
1066         tmpfs)
1067                 if [ -z "$SCRATCH_DEV" -o ! -d "$SCRATCH_MNT" ];
1068                 then
1069                     _notrun "this test requires a valid \$SCRATCH_MNT and unique $SCRATCH_DEV"
1070                 fi
1071                 ;;
1072         *)
1073                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
1074                  then
1075                      _notrun "this test requires a valid \$SCRATCH_DEV"
1076                  fi
1077                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
1078                  then
1079                      _notrun "this test requires a valid \$SCRATCH_DEV"
1080                  fi
1081                 if [ ! -d "$SCRATCH_MNT" ]
1082                 then
1083                      _notrun "this test requires a valid \$SCRATCH_MNT"
1084                 fi
1085                  ;;
1086     esac
1087
1088     # mounted?
1089     if _mount | grep -q $SCRATCH_DEV
1090     then
1091         # if it's mounted, make sure its on $SCRATCH_MNT
1092         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1093         then
1094             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1095             exit 1
1096         fi
1097         # and then unmount it
1098         if ! $UMOUNT_PROG $SCRATCH_DEV
1099         then
1100             echo "failed to unmount $SCRATCH_DEV"
1101             exit 1
1102         fi
1103     fi
1104     rm -f ${RESULT_DIR}/require_scratch
1105 }
1106
1107 # we need the scratch device and it should be checked post test.
1108 _require_scratch()
1109 {
1110         _require_scratch_nocheck
1111         touch ${RESULT_DIR}/require_scratch
1112 }
1113
1114
1115 # this test needs a test partition - check we're ok & mount it
1116 #
1117 _require_test()
1118 {
1119     case "$FSTYP" in
1120         nfs*)
1121                 echo $TEST_DEV | grep -q ":/" > /dev/null 2>&1
1122                 if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
1123                         _notrun "this test requires a valid \$TEST_DIR"
1124                 fi
1125                 if [ ! -d "$TEST_DIR" ]; then
1126                         _notrun "this test requires a valid \$TEST_DIR"
1127                 fi
1128                 ;;
1129         cifs)
1130                 echo $TEST_DEV | grep -q "//" > /dev/null 2>&1
1131                 if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
1132                         _notrun "this test requires a valid \$TEST_DEV"
1133                 fi
1134                 if [ ! -d "$TEST_DIR" ]; then
1135                      _notrun "this test requires a valid \$TEST_DIR"
1136                 fi
1137                 ;;
1138         tmpfs)
1139                 if [ -z "$TEST_DEV" -o ! -d "$TEST_DIR" ];
1140                 then
1141                     _notrun "this test requires a valid \$TEST_DIR and unique $TEST_DEV"
1142                 fi
1143                 ;;
1144         *)
1145                  if [ -z "$TEST_DEV" -o "`_is_block_dev $TEST_DEV`" = "" ]
1146                  then
1147                      _notrun "this test requires a valid \$TEST_DEV"
1148                  fi
1149                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
1150                  then
1151                      _notrun "this test requires a valid \$TEST_DEV"
1152                  fi
1153                 if [ ! -d "$TEST_DIR" ]
1154                 then
1155                      _notrun "this test requires a valid \$TEST_DIR"
1156                 fi
1157                  ;;
1158     esac
1159
1160     # mounted?
1161     # Note that we use -F here so grep doesn't try to interpret an NFS over
1162     # IPv6 server as a regular expression.
1163     if _mount | grep -F -q $TEST_DEV
1164     then
1165         # if it's mounted, make sure its on $TEST_DIR
1166         if ! _mount | grep -F $TEST_DEV | grep -q $TEST_DIR
1167         then
1168             echo "\$TEST_DEV is mounted but not on \$TEST_DIR - aborting"
1169             exit 1
1170         fi
1171     else
1172         out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
1173         if [ $? -ne 1 ]; then
1174                 echo $out
1175                 exit 1
1176         fi
1177     fi
1178     touch ${RESULT_DIR}/require_test
1179 }
1180
1181 # this test needs a logdev
1182 #
1183 _require_logdev()
1184 {
1185     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
1186         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
1187     [ "$USE_EXTERNAL" != yes ] && \
1188         _notrun "This test requires USE_EXTERNAL to be enabled"
1189
1190     # ensure its not mounted
1191     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
1192 }
1193
1194 # this test requires loopback device support
1195 #
1196 _require_loop()
1197 {
1198     if [ "$HOSTOS" != "Linux" ]
1199     then
1200         _notrun "This test requires linux for loopback device support"
1201     fi
1202
1203     modprobe loop >/dev/null 2>&1
1204     if grep loop /proc/devices >/dev/null 2>&1
1205     then
1206         :
1207     else
1208         _notrun "This test requires loopback device support"
1209     fi
1210 }
1211
1212 # this test requires ext2 filesystem support
1213 #
1214 _require_ext2()
1215 {
1216     if [ "$HOSTOS" != "Linux" ]
1217     then
1218         _notrun "This test requires linux for ext2 filesystem support"
1219     fi
1220
1221     modprobe ext2 >/dev/null 2>&1
1222     if grep ext2 /proc/filesystems >/dev/null 2>&1
1223     then
1224         :
1225     else
1226         _notrun "This test requires ext2 filesystem support"
1227     fi
1228 }
1229
1230 # this test requires that (large) loopback device files are not in use
1231 #
1232 _require_no_large_scratch_dev()
1233 {
1234     [ "$LARGE_SCRATCH_DEV" = yes ] && \
1235         _notrun "Large filesystem testing in progress, skipped this test"
1236 }
1237
1238 # this test requires that a realtime subvolume is in use, and
1239 # that the kernel supports realtime as well.
1240 #
1241 _require_realtime()
1242 {
1243     [ "$USE_EXTERNAL" = yes ] || \
1244         _notrun "External volumes not in use, skipped this test"
1245     [ "$SCRATCH_RTDEV" = "" ] && \
1246         _notrun "Realtime device required, skipped this test"
1247 }
1248
1249 # this test requires that a specified command (executable) exists
1250 # $1 - command, $2 - name for error message
1251 #
1252 _require_command()
1253 {
1254     [ -n "$1" ] && _cmd="$1" || _cmd="$2"
1255     [ -n "$1" -a -x "$1" ] || _notrun "$_cmd utility required, skipped this test"
1256 }
1257
1258 # this test requires the device to be valid block device
1259 # $1 - device
1260 _require_block_device()
1261 {
1262         if [ -z "$1" ]; then
1263                 echo "Usage: _require_block_device <dev>" 1>&2
1264                 exit 1
1265         fi
1266         if [ "`_is_block_dev $SCRATCH_DEV`" == "" ]; then
1267                 _notrun "require $1 to be valid block disk"
1268         fi
1269 }
1270
1271 # this test requires the device mapper flakey target
1272 #
1273 _require_dm_flakey()
1274 {
1275     # require SCRATCH_DEV to be a valid block device
1276     _require_block_device $SCRATCH_DEV
1277     _require_command $DMSETUP_PROG
1278
1279     modprobe dm-flakey >/dev/null 2>&1
1280     $DMSETUP_PROG targets | grep flakey >/dev/null 2>&1
1281     if [ $? -eq 0 ]
1282     then
1283         :
1284     else
1285         _notrun "This test requires dm flakey support"
1286     fi
1287 }
1288
1289 # this test requires the projid32bit feature to be available in mkfs.xfs.
1290 #
1291 _require_projid32bit()
1292 {
1293        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
1294            || _notrun "mkfs.xfs doesn't have projid32bit feature"
1295 }
1296
1297 _require_projid16bit()
1298 {
1299         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
1300            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
1301 }
1302
1303 # this test requires the crc feature to be available in mkfs.xfs
1304 #
1305 _require_xfs_mkfs_crc()
1306 {
1307         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
1308            || _notrun "mkfs.xfs doesn't have crc feature"
1309 }
1310
1311 # this test requires the xfs kernel support crc feature
1312 #
1313 _require_xfs_crc()
1314 {
1315         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
1316         _scratch_mount >/dev/null 2>&1 \
1317            || _notrun "Kernel doesn't support crc feature"
1318         umount $SCRATCH_MNT
1319 }
1320
1321 # this test requires the bigalloc feature to be available in mkfs.ext4
1322 #
1323 _require_ext4_mkfs_bigalloc()
1324 {
1325         $MKFS_EXT4_PROG -F -O bigalloc -n $SCRATCH_DEV 512m >/dev/null 2>&1 \
1326            || _notrun "mkfs.ext4 doesn't have bigalloc feature"
1327 }
1328
1329 # this test requires the ext4 kernel support bigalloc feature
1330 #
1331 _require_ext4_bigalloc()
1332 {
1333         $MKFS_EXT4_PROG -F -O bigalloc $SCRATCH_DEV 512m >/dev/null 2>&1
1334         _scratch_mount >/dev/null 2>&1 \
1335            || _notrun "Ext4 kernel doesn't support bigalloc feature"
1336         umount $SCRATCH_MNT
1337 }
1338
1339 # this test requires the finobt feature to be available in mkfs.xfs
1340 #
1341 _require_xfs_mkfs_finobt()
1342 {
1343         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
1344            || _notrun "mkfs.xfs doesn't have finobt feature"
1345 }
1346
1347 # this test requires the xfs kernel support finobt feature
1348 #
1349 _require_xfs_finobt()
1350 {
1351         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
1352         _scratch_mount >/dev/null 2>&1 \
1353            || _notrun "Kernel doesn't support finobt feature"
1354         umount $SCRATCH_MNT
1355 }
1356
1357 # this test requires xfs sysfs attribute support
1358 #
1359 _require_xfs_sysfs()
1360 {
1361         attr=$1
1362         sysfsdir=/sys/fs/xfs
1363
1364         if [ ! -e $sysfsdir ]; then
1365                 _notrun "no kernel support for XFS sysfs attributes"
1366         fi
1367
1368         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
1369                 _notrun "sysfs attribute '$attr' is not supported"
1370         fi
1371 }
1372
1373 # this test requires that external log/realtime devices are not in use
1374 #
1375 _require_nonexternal()
1376 {
1377     [ "$USE_EXTERNAL" = yes ] && \
1378         _notrun "External device testing in progress, skipped this test"
1379 }
1380
1381 # this test requires that a (specified) aio-dio executable exists
1382 # $1 - command (optional)
1383 #
1384 _require_aiodio()
1385 {
1386     if [ -z "$1" ]
1387     then
1388         AIO_TEST=src/aio-dio-regress/aiodio_sparse2
1389         [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
1390     else
1391         AIO_TEST=src/aio-dio-regress/$1
1392         [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
1393     fi
1394 }
1395
1396 # run an aio-dio program
1397 # $1 - command
1398 _run_aiodio()
1399 {
1400     if [ -z "$1" ]
1401     then
1402         echo "usage: _run_aiodio command_name" 2>&1
1403         status=1; exit 1
1404     fi
1405
1406     _require_aiodio $1
1407
1408     local testtemp=$TEST_DIR/aio-testfile
1409     rm -f $testtemp
1410     $AIO_TEST $testtemp 2>&1
1411     status=$?
1412     rm -f $testtemp
1413
1414     return $status
1415 }
1416
1417 # indicate whether YP/NIS is active or not
1418 #
1419 _yp_active()
1420 {
1421         local dn
1422         dn=$(domainname 2>/dev/null)
1423         test -n "${dn}" -a "${dn}" != "(none)"
1424         echo $?
1425 }
1426
1427 # cat the password file
1428 #
1429 _cat_passwd()
1430 {
1431         [ $(_yp_active) -eq 0 ] && ypcat passwd
1432         cat /etc/passwd
1433 }
1434
1435 # cat the group file
1436 #
1437 _cat_group()
1438 {
1439         [ $(_yp_active) -eq 0 ] && ypcat group
1440         cat /etc/group
1441 }
1442
1443 # check for the fsgqa user on the machine
1444 #
1445 _require_user()
1446 {
1447     qa_user=fsgqa
1448     _cat_passwd | grep -q $qa_user
1449     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
1450     echo /bin/true | su $qa_user
1451     [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
1452 }
1453
1454 # check for the fsgqa group on the machine
1455 #
1456 _require_group()
1457 {
1458     qa_group=fsgqa
1459     _cat_group | grep -q $qa_group
1460     [ "$?" == "0" ] || _notrun "$qa_group user not defined."
1461 }
1462
1463 _filter_user_do()
1464 {
1465         perl -ne "
1466 s,.*Permission\sdenied.*,Permission denied,;
1467 s,.*no\saccess\sto\stty.*,,;
1468 s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
1469 s,^\s*$,,;
1470         print;"
1471 }
1472
1473 _user_do()
1474 {
1475     if [ "$HOSTOS" == "IRIX" ]
1476         then
1477         echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
1478     else
1479         echo $1 | su $qa_user 2>&1 | _filter_user_do
1480     fi
1481 }
1482
1483 _require_xfs_io_command()
1484 {
1485         if [ $# -ne 1 ]
1486         then
1487                 echo "Usage: _require_xfs_io_command command" 1>&2
1488                 exit 1
1489         fi
1490         command=$1
1491
1492         testfile=$TEST_DIR/$$.xfs_io
1493         case $command in
1494         "falloc" )
1495                 testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $testfile 2>&1`
1496                 ;;
1497         "fpunch" | "fcollapse" | "zero" | "fzero" )
1498                 testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1499                         -c "$command 4k 8k" $testfile 2>&1`
1500                 ;;
1501         "fiemap")
1502                 testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1503                         -c "fiemap -v" $testfile 2>&1`
1504                 ;;
1505         "flink" )
1506                 testio=`$XFS_IO_PROG -T -F -c "flink $testfile" \
1507                         $TEST_DIR 2>&1`
1508                 echo $testio | egrep -q "invalid option|Is a directory" && \
1509                         _notrun "xfs_io $command support is missing"
1510                 ;;
1511         *)
1512                 testio=`$XFS_IO_PROG -c "$command help" 2>&1`
1513         esac
1514
1515         rm -f $testfile 2>&1 > /dev/null
1516         echo $testio | grep -q "not found" && \
1517                 _notrun "xfs_io $command support is missing"
1518         echo $testio | grep -q "Operation not supported" && \
1519                 _notrun "xfs_io $command failed (old kernel/wrong fs?)"
1520 }
1521
1522 # Check that a fs has enough free space (in 1024b blocks)
1523 #
1524 _require_fs_space()
1525 {
1526         MNT=$1
1527         BLOCKS=$2       # in units of 1024
1528         let GB=$BLOCKS/1024/1024
1529
1530         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
1531         [ $FREE_BLOCKS -lt $BLOCKS ] && \
1532                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
1533 }
1534
1535 #
1536 # Check if the filesystem supports sparse files.
1537 #
1538 # Unfortunately there is no better way to do this than a manual black list.
1539 #
1540 _require_sparse_files()
1541 {
1542     case $FSTYP in
1543     hfsplus)
1544         _notrun "Sparse files not supported by this filesystem type: $FSTYP"
1545         ;;
1546     *)
1547         ;;
1548     esac
1549 }
1550
1551 _require_debugfs()
1552 {
1553     #boot_params always present in debugfs
1554     [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
1555 }
1556
1557 _require_fail_make_request()
1558 {
1559     [ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
1560         || _notrun "$DEBUGFS_MNT/fail_make_request \
1561  not found. Seems that CONFIG_FAIL_MAKE_REQUEST kernel config option not enabled"
1562 }
1563
1564 #
1565 # Check if the file system supports seek_data/hole
1566 #
1567 _require_seek_data_hole()
1568 {
1569     testfile=$TEST_DIR/$$.seek
1570     testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`
1571     rm -f $testfile &>/dev/null
1572     echo $testseek | grep -q "Kernel does not support" && \
1573         _notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
1574 }
1575
1576 # check that a FS on a device is mounted
1577 # if so, return mount point
1578 #
1579 _is_mounted()
1580 {
1581     if [ $# -ne 1 ]
1582     then
1583         echo "Usage: _is_mounted device" 1>&2
1584         exit 1
1585     fi
1586
1587     device=$1
1588
1589     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
1590         pattern        { print $3 ; exit 0 }
1591         END            { exit 1 }
1592     '
1593     then
1594         echo "_is_mounted: $device is not a mounted $FSTYP FS"
1595         exit 1
1596     fi
1597 }
1598
1599 # remount a FS to a new mode (ro or rw)
1600 #
1601 _remount()
1602 {
1603     if [ $# -ne 2 ]
1604     then
1605         echo "Usage: _remount device ro/rw" 1>&2
1606         exit 1
1607     fi
1608     device=$1
1609     mode=$2
1610
1611     if ! mount -o remount,$mode $device
1612     then
1613         echo "_remount: failed to remount filesystem on $device as $mode"
1614         exit 1
1615     fi
1616 }
1617
1618 # Run the appropriate repair/check on a filesystem
1619 #
1620 # if the filesystem is mounted, it's either remounted ro before being
1621 # checked or it's unmounted and then remounted
1622 #
1623
1624 # If set, we remount ro instead of unmounting for fsck
1625 USE_REMOUNT=0
1626
1627 _umount_or_remount_ro()
1628 {
1629     if [ $# -ne 1 ]
1630     then
1631         echo "Usage: _umount_or_remount_ro <device>" 1>&2
1632         exit 1
1633     fi
1634
1635     device=$1
1636     mountpoint=`_is_mounted $device`
1637
1638     if [ $USE_REMOUNT -eq 0 ]; then
1639         $UMOUNT_PROG $device
1640     else
1641         _remount $device ro
1642     fi
1643     echo "$mountpoint"
1644 }
1645
1646 _mount_or_remount_rw()
1647 {
1648     if [ $# -ne 3 ]
1649     then
1650         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
1651         exit 1
1652     fi
1653     mount_opts=$1
1654     device=$2
1655     mountpoint=$3
1656
1657     if [ $USE_REMOUNT -eq 0 ]
1658     then
1659         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
1660         then
1661             echo "!!! failed to remount $device on $mountpoint"
1662             return 0 # ok=0
1663         fi
1664     else
1665         _remount $device rw
1666     fi
1667
1668     return 1 # ok=1
1669 }
1670
1671 # Check a generic filesystem in no-op mode; this assumes that the
1672 # underlying fsck program accepts "-n" for a no-op (check-only) run,
1673 # and that it will still return an errno for corruption in this mode.
1674 #
1675 # Filesystems which don't support this will need to define their
1676 # own check routine.
1677 #
1678 _check_generic_filesystem()
1679 {
1680     device=$1
1681
1682     # If type is set, we're mounted
1683     type=`_fs_type $device`
1684     ok=1
1685
1686     if [ "$type" = "$FSTYP" ]
1687     then
1688         # mounted ...
1689         mountpoint=`_umount_or_remount_ro $device`
1690     fi
1691
1692     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
1693     if [ $? -ne 0 ]
1694     then
1695         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1696
1697         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
1698         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1699         cat $tmp.fsck                           >>$seqres.full
1700         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1701
1702         ok=0
1703     fi
1704     rm -f $tmp.fsck
1705
1706     if [ $ok -eq 0 ]
1707     then
1708         echo "*** mount output ***"             >>$seqres.full
1709         _mount                                  >>$seqres.full
1710         echo "*** end mount output"             >>$seqres.full
1711     elif [ "$type" = "$FSTYP" ]
1712     then
1713         # was mounted ...
1714         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1715         ok=$?
1716     fi
1717
1718     if [ $ok -eq 0 ]; then
1719         status=1
1720         exit 1
1721     fi
1722
1723     return 0
1724 }
1725
1726 # run xfs_check and friends on a FS.
1727
1728 _check_xfs_filesystem()
1729 {
1730     if [ $# -ne 3 ]
1731     then
1732         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
1733         exit 1
1734     fi
1735
1736     extra_mount_options=""
1737     device=$1
1738     if [ "$2" != "none" ]; then
1739         extra_log_options="-l$2"
1740         extra_mount_options="-ologdev=$2"
1741     fi
1742
1743     if [ "$3" != "none" ]; then
1744         extra_rt_options="-r$3"
1745         extra_mount_options=$extra_mount_options" -ortdev=$3"
1746     fi
1747     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
1748
1749     [ "$FSTYP" != xfs ] && return 0
1750
1751     type=`_fs_type $device`
1752     ok=1
1753
1754     if [ "$type" = "xfs" ]
1755     then
1756         # mounted ...
1757         mountpoint=`_umount_or_remount_ro $device`
1758     fi
1759
1760     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1761                 | tee $tmp.logprint | grep -q "<CLEAN>"
1762     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1763     then
1764         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
1765
1766         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
1767         echo "*** xfs_logprint -t output ***"   >>$seqres.full
1768         cat $tmp.logprint                       >>$seqres.full
1769         echo "*** end xfs_logprint output"      >>$seqres.full
1770
1771         ok=0
1772     fi
1773
1774     # xfs_check runs out of memory on large files, so even providing the test
1775     # option (-t) to avoid indexing the free space trees doesn't make it pass on
1776     # large filesystems. Avoid it.
1777     if [ "$LARGE_SCRATCH_DEV" != yes ]; then
1778             _xfs_check $extra_log_options $device 2>&1 |\
1779                  _fix_malloc >$tmp.fs_check
1780     fi
1781     if [ -s $tmp.fs_check ]
1782     then
1783         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
1784
1785         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1786         echo "*** xfs_check output ***"         >>$seqres.full
1787         cat $tmp.fs_check                       >>$seqres.full
1788         echo "*** end xfs_check output"         >>$seqres.full
1789
1790         ok=0
1791     fi
1792
1793     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1794     if [ $? -ne 0 ]
1795     then
1796         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
1797
1798         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1799         echo "*** xfs_repair -n output ***"     >>$seqres.full
1800         cat $tmp.repair | _fix_malloc           >>$seqres.full
1801         echo "*** end xfs_repair output"        >>$seqres.full
1802
1803         ok=0
1804     fi
1805     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1806
1807     if [ $ok -eq 0 ]
1808     then
1809         echo "*** mount output ***"             >>$seqres.full
1810         _mount                                  >>$seqres.full
1811         echo "*** end mount output"             >>$seqres.full
1812     elif [ "$type" = "xfs" ]
1813     then
1814         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1815     fi
1816
1817     if [ $ok -eq 0 ]; then
1818         status=1
1819         if [ "$iam" != "check" ]; then
1820                 exit 1
1821         fi
1822     fi
1823
1824     return 0
1825 }
1826
1827 # Filter the knowen errors the UDF Verifier reports.
1828 _udf_test_known_error_filter()
1829 {
1830         egrep -v "PVD  60  Error: Interchange Level: 1, Maximum Interchange Level: 0|FSD  28  Error: Interchange Level: 1, Maximum Interchange Level: 1,|PVD  72  Warning: Volume Set Identifier: \"\*IRIX UDF\",|Warning: [0-9]+ unused blocks NOT marked as unallocated."
1831
1832 }
1833
1834 _check_udf_filesystem()
1835 {
1836     [ "$DISABLE_UDF_TEST" == "1" ] && return
1837
1838     if [ $# -ne 1 -a $# -ne 2 ]
1839     then
1840         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1841         exit 1
1842     fi
1843
1844     if [ ! -x $here/src/udf_test ]
1845     then
1846         echo "udf_test not installed, please download and build the Philips"
1847         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1848         echo "Then copy the udf_test binary to $here/src/."
1849         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1850         echo "to 1."
1851         return
1852     fi
1853
1854     device=$1
1855     if [ $# -eq 2 ];
1856     then
1857         LAST_BLOCK=`expr \( $2 - 1 \)`
1858         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1859     fi
1860
1861     rm -f $seqres.checkfs
1862     sleep 1 # Due to a problem with time stamps in udf_test
1863     $here/src/udf_test $OPT_ARG $device | tee $seqres.checkfs | egrep "Error|Warning" | \
1864         _udf_test_known_error_filter | \
1865         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" && \
1866         echo "Warning UDF Verifier reported errors see $seqres.checkfs."
1867 }
1868
1869 _check_xfs_test_fs()
1870 {
1871     TEST_LOG="none"
1872     TEST_RT="none"
1873     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1874         TEST_LOG="$TEST_LOGDEV"
1875
1876     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1877         TEST_RT="$TEST_RTDEV"
1878
1879     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1880
1881     # check for ipath consistency
1882     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1883         # errors go to stderr
1884         xfs_check_ipaths $TEST_DIR >/dev/null
1885         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1886     fi
1887 }
1888
1889 _check_btrfs_filesystem()
1890 {
1891     device=$1
1892
1893     # If type is set, we're mounted
1894     type=`_fs_type $device`
1895     ok=1
1896
1897     if [ "$type" = "$FSTYP" ]
1898     then
1899         # mounted ...
1900         mountpoint=`_umount_or_remount_ro $device`
1901     fi
1902
1903     btrfsck $device >$tmp.fsck 2>&1
1904     if [ $? -ne 0 ]
1905     then
1906         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1907
1908         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1909         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1910         cat $tmp.fsck                           >>$seqres.full
1911         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1912
1913         ok=0
1914     fi
1915     rm -f $tmp.fsck
1916
1917     if [ $ok -eq 0 ]
1918     then
1919         echo "*** mount output ***"             >>$seqres.full
1920         _mount                                  >>$seqres.full
1921         echo "*** end mount output"             >>$seqres.full
1922     elif [ "$type" = "$FSTYP" ]
1923     then
1924         # was mounted ...
1925         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1926         ok=$?
1927     fi
1928
1929     if [ $ok -eq 0 ]; then
1930         status=1
1931         exit 1
1932     fi
1933
1934     return 0
1935 }
1936
1937 _check_test_fs()
1938 {
1939     case $FSTYP in
1940     xfs)
1941         _check_xfs_test_fs
1942         ;;
1943     nfs)
1944         # no way to check consistency for nfs
1945         ;;
1946     cifs)
1947         # no way to check consistency for cifs
1948         ;;
1949     udf)
1950         # do nothing for now
1951         ;;
1952     btrfs)
1953         _check_btrfs_filesystem $TEST_DEV
1954         ;;
1955     tmpfs)
1956         # no way to check consistency for tmpfs
1957         ;;
1958     *)
1959         _check_generic_filesystem $TEST_DEV
1960         ;;
1961     esac
1962 }
1963
1964 _check_scratch_fs()
1965 {
1966     device=$SCRATCH_DEV
1967     [ $# -eq 1 ] && device=$1
1968
1969     case $FSTYP in
1970     xfs)
1971         SCRATCH_LOG="none"
1972         SCRATCH_RT="none"
1973         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1974             SCRATCH_LOG="$SCRATCH_LOGDEV"
1975
1976         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1977             SCRATCH_RT="$SCRATCH_RTDEV"
1978
1979         _check_xfs_filesystem $device $SCRATCH_LOG $SCRATCH_RT
1980         ;;
1981     udf)
1982         _check_udf_filesystem $device $udf_fsize
1983         ;;
1984     nfs*)
1985         # Don't know how to check an NFS filesystem, yet.
1986         ;;
1987     cifs)
1988         # Don't know how to check a CIFS filesystem, yet.
1989         ;;
1990     btrfs)
1991         _check_btrfs_filesystem $device
1992         ;;
1993     tmpfs)
1994         # no way to check consistency for tmpfs
1995         ;;
1996     *)
1997         _check_generic_filesystem $device
1998         ;;
1999     esac
2000 }
2001
2002 _full_fstyp_details()
2003 {
2004      [ -z "$FSTYP" ] && FSTYP=xfs
2005      if [ $FSTYP = xfs ]; then
2006         if [ -d /proc/fs/xfs ]; then
2007             if grep -q 'debug 0' /proc/fs/xfs/stat; then
2008                 FSTYP="$FSTYP (non-debug)"
2009             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
2010                 FSTYP="$FSTYP (debug)"
2011             fi
2012         else
2013             if uname -a | grep -qi 'debug'; then
2014                 FSTYP="$FSTYP (debug)"
2015             else
2016                 FSTYP="$FSTYP (non-debug)"
2017             fi
2018         fi
2019      fi
2020      echo $FSTYP
2021 }
2022
2023 _full_platform_details()
2024 {
2025      os=`uname -s`
2026      host=`hostname -s`
2027      kernel=`uname -r`
2028      platform=`uname -m`
2029      echo "$os/$platform $host $kernel"
2030 }
2031
2032 _link_out_file()
2033 {
2034         if [ -z "$1" -o -z "$2" ]; then
2035                 echo Error must pass src and dst.
2036                 exit
2037         fi
2038         rm -f $2
2039         if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
2040                 ln -s $1.irix $2
2041         elif [ "`uname`" == "Linux" ]; then
2042                 ln -s $1.linux $2
2043         else
2044                 echo Error test $seq does not run on the operating system: `uname`
2045                 exit
2046         fi
2047 }
2048
2049 _die()
2050 {
2051         echo $@
2052         exit 1
2053 }
2054
2055 #takes files, randomdata
2056 _nfiles()
2057 {
2058         f=0
2059         while [ $f -lt $1 ]
2060         do
2061                 file=f$f
2062                 echo > $file
2063                 if [ $size -gt 0 ]; then
2064                     if [ "$2" == "false" ]; then
2065                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
2066                     else
2067                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
2068                     fi
2069                 fi
2070                 let f=$f+1
2071         done
2072 }
2073
2074 # takes dirname, depth, randomdata
2075 _descend()
2076 {
2077         dirname=$1; depth=$2; randomdata=$3
2078         mkdir $dirname  || die "mkdir $dirname failed"
2079         cd $dirname
2080
2081         _nfiles $files $randomdata          # files for this dir and data type
2082
2083         [ $depth -eq 0 ] && return
2084         let deep=$depth-1 # go 1 down
2085
2086         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
2087
2088         d=0
2089         while [ $d -lt $dirs ]
2090         do
2091                 _descend d$d $deep &
2092                 let d=$d+1
2093                 wait
2094         done
2095 }
2096
2097 # Populate a filesystem with inodes for performance experiments
2098 #
2099 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
2100 #
2101 _populate_fs()
2102 {
2103     here=`pwd`
2104     dirs=5          # ndirs in each subdir till leaves
2105     size=0          # sizeof files in K
2106     files=100       # num files in _each_ subdir
2107     depth=2         # depth of tree from root to leaves
2108     verbose=false
2109     root=root       # path of initial root of directory tree
2110     randomdata=false # -x data type urandom or zero
2111
2112     OPTIND=1
2113     while getopts "d:f:n:r:s:v:x" c
2114     do
2115         case $c in
2116         d)      depth=$OPTARG;;
2117         n)      dirs=$OPTARG;;
2118         f)      files=$OPTARG;;
2119         s)      size=$OPTARG;;
2120         v)      verbose=true;;
2121         r)      root=$OPTARG;;
2122         x)      randomdata=true;;
2123         esac
2124     done
2125
2126     _descend $root $depth $randomdata
2127     wait
2128
2129     cd $here
2130
2131     [ $verbose = true ] && echo done
2132 }
2133
2134 # query whether the given file has the given inode flag set
2135 #
2136 _test_inode_flag()
2137 {
2138     flag=$1
2139     file=$2
2140
2141     if which $XFS_IO_PROG >/dev/null; then
2142         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
2143             return 0
2144         fi
2145     fi
2146     return 1
2147 }
2148
2149 # query the given files extsize allocator hint in bytes (if any)
2150 #
2151 _test_inode_extsz()
2152 {
2153     file=$1
2154     blocks=""
2155
2156     if which $XFS_IO_PROG >/dev/null; then
2157         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
2158                 awk '/^xattr.extsize =/ { print $3 }'`
2159     fi
2160     [ -z "$blocks" ] && blocks="0"
2161     echo $blocks
2162 }
2163
2164 # scratch_dev_pool should contain the disks pool for the btrfs raid
2165 _require_scratch_dev_pool()
2166 {
2167         local i
2168         local ndevs
2169
2170         if [ -z "$SCRATCH_DEV_POOL" ]; then
2171                 _notrun "this test requires a valid \$SCRATCH_DEV_POOL"
2172         fi
2173
2174         if [ -z "$1" ]; then
2175                 ndevs=2
2176         else
2177                 ndevs=$1
2178         fi
2179
2180         # btrfs test case needs ndevs or more scratch_dev_pool; other FS not sure
2181         # so fail it
2182         case $FSTYP in
2183         btrfs)
2184                 if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt $ndevs ]; then
2185                         _notrun "btrfs and this test needs $ndevs or more disks in SCRATCH_DEV_POOL"
2186                 fi
2187         ;;
2188         *)
2189                 _notrun "dev_pool is not supported by fstype \"$FSTYP\""
2190         ;;
2191         esac
2192
2193         for i in $SCRATCH_DEV_POOL; do
2194                 if [ "`_is_block_dev $i`" = "" ]; then
2195                         _notrun "this test requires valid block disk $i"
2196                 fi
2197                 if [ "`_is_block_dev $i`" = "`_is_block_dev $TEST_DEV`" ]; then
2198                         _notrun "$i is part of TEST_DEV, this test requires unique disks"
2199                 fi
2200                 if _mount | grep -q $i; then
2201                         if ! $UMOUNT_PROG $i; then
2202                             echo "failed to unmount $i - aborting"
2203                             exit 1
2204                         fi
2205                 fi
2206                 # to help better debug when something fails, we remove
2207                 # traces of previous btrfs FS on the dev.
2208                 dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
2209         done
2210 }
2211
2212 # ensure devices in SCRATCH_DEV_POOL are of the same size
2213 # must be called after _require_scratch_dev_pool
2214 _require_scratch_dev_pool_equal_size()
2215 {
2216         local _size
2217         local _newsize
2218         local _dev
2219
2220         # SCRATCH_DEV has been set to the first device in SCRATCH_DEV_POOL
2221         _size=`_get_device_size $SCRATCH_DEV`
2222         for _dev in $SCRATCH_DEV_POOL; do
2223                 _newsize=`_get_device_size $_dev`
2224                 if [ $_size -ne $_newsize ]; then
2225                         _notrun "This test requires devices in SCRATCH_DEV_POOL have the same size"
2226                 fi
2227         done
2228 }
2229
2230 # We will check if the device is deletable
2231 _require_deletable_scratch_dev_pool()
2232 {
2233         local i
2234         local x
2235         for i in $SCRATCH_DEV_POOL; do
2236                 x=`echo $i | cut -d"/" -f 3`
2237                 if [ ! -f /sys/class/block/${x}/device/delete ]; then
2238                         _notrun "$i is a device which is not deletable"
2239                 fi
2240         done
2241 }
2242
2243 # We check for btrfs and (optionally) features of the btrfs command
2244 _require_btrfs()
2245 {
2246         cmd=$1
2247         _require_command $BTRFS_UTIL_PROG btrfs
2248         if [ -z "$1" ]; then
2249                 return 1;
2250         fi
2251         $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
2252         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
2253 }
2254
2255 # Check that fio is present, and it is able to execute given jobfile
2256 _require_fio()
2257 {
2258         job=$1
2259
2260         _require_command $FIO_PROG
2261         if [ -z "$1" ]; then
2262                 return 1;
2263         fi
2264
2265         $FIO_PROG --warnings-fatal --showcmd $job >> $seqres.full 2>&1
2266         [ $? -eq 0 ] || _notrun "$FIO_PROG too old, see $seqres.full"
2267 }
2268
2269 # Does freeze work on this fs?
2270 _require_freeze()
2271 {
2272         xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
2273         result=$? 
2274         xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
2275         [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
2276 }
2277
2278 # arg 1 is dev to remove and is output of the below eg.
2279 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2280 _devmgt_remove()
2281 {
2282         local lun=$1
2283         local disk=$2
2284
2285         echo 1 > /sys/class/scsi_device/${lun}/device/delete || _fail "Remove disk failed"
2286
2287         stat $disk > /dev/null 2>&1
2288         while [ $? -eq 0 ]; do
2289                 sleep 1
2290                 stat $disk > /dev/null 2>&1
2291         done
2292 }
2293
2294 # arg 1 is dev to add and is output of the below eg.
2295 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2296 _devmgt_add()
2297 {
2298         local h
2299         local tdl
2300         # arg 1 will be in h:t:d:l format now in the h and "t d l" format
2301         h=`echo ${1} | cut -d":" -f 1`
2302         tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`
2303
2304         echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"
2305
2306         # ensure the device comes online
2307         dev_back_oneline=0
2308         for i in `seq 1 10`; do
2309                 if [ -d /sys/class/scsi_device/${1}/device/block ]; then
2310                         dev=`ls /sys/class/scsi_device/${1}/device/block`
2311                         for j in `seq 1 10`;
2312                         do
2313                                 stat /dev/$dev > /dev/null 2>&1
2314                                 if [ $? -eq 0 ]; then
2315                                         dev_back_oneline=1
2316                                         break
2317                                 fi
2318                                 sleep 1
2319                         done
2320                         break
2321                 else
2322                         sleep 1
2323                 fi
2324         done
2325         if [ $dev_back_oneline -eq 0 ]; then
2326                 echo "/dev/$dev online failed" >> $seqres.full
2327         else
2328                 echo "/dev/$dev is back online" >> $seqres.full
2329         fi
2330 }
2331
2332 _require_fstrim()
2333 {
2334         if [ -z "$FSTRIM_PROG" ]; then
2335                 _notrun "This test requires fstrim utility."
2336         fi
2337 }
2338
2339 _test_batched_discard()
2340 {
2341         if [ $# -ne 1 ]; then
2342                 echo "Usage: _test_batched_discard mnt_point" 1>&2
2343                 exit 1
2344         fi
2345         _require_fstrim
2346         $FSTRIM_PROG ${1} &>/dev/null
2347 }
2348
2349 _require_dumpe2fs()
2350 {
2351         if [ -z "$DUMPE2FS_PROG" ]; then
2352                 _notrun "This test requires dumpe2fs utility."
2353         fi
2354 }
2355
2356 _require_ugid_map()
2357 {
2358         if [ ! -e /proc/self/uid_map ]; then
2359                 _notrun "This test requires procfs uid_map support."
2360         fi
2361         if [ ! -e /proc/self/gid_map ]; then
2362                 _notrun "This test requires procfs gid_map support."
2363         fi
2364 }
2365
2366 _require_cp_reflink()
2367 {
2368        cp --help | grep -q reflink || \
2369                _notrun "This test requires a cp with --reflink support."
2370 }
2371
2372 _require_fssum()
2373 {
2374         FSSUM_PROG=$here/src/fssum
2375         [ -x $FSSUM_PROG ] || _notrun "fssum not built"
2376 }
2377
2378 _require_cloner()
2379 {
2380         CLONER_PROG=$here/src/cloner
2381         [ -x $CLONER_PROG ] || \
2382                 _notrun "cloner binary not present at $CLONER_PROG"
2383 }
2384
2385 # Given 2 files, verify that they have the same mapping but different
2386 # inodes - i.e. an undisturbed reflink
2387 # Silent if so, make noise if not
2388 _verify_reflink()
2389 {
2390        # not a hard link or symlink?
2391        cmp -s  <(stat -c '%i' $1) <(stat -c '%i' $2) \
2392                && echo "$1 and $2 are not reflinks: same inode number"
2393
2394        # same mapping?
2395        diff -u <($XFS_IO_PROG -c "fiemap" $1 | grep -v $1) \
2396                <($XFS_IO_PROG -c "fiemap" $2 | grep -v $2) \
2397                || echo "$1 and $2 are not reflinks: different extents"
2398 }
2399
2400 _require_atime()
2401 {
2402         if [ "$FSTYP" == "nfs" ]; then
2403                 _notrun "atime related mount options have no effect on NFS"
2404         fi
2405 }
2406
2407 _require_relatime()
2408 {
2409         _scratch_mkfs > /dev/null 2>&1
2410         _scratch_mount -o relatime || \
2411                 _notrun "relatime not supported by the current kernel"
2412         _scratch_unmount
2413 }
2414
2415 _require_userns()
2416 {
2417         [ -x src/nsexec ] || _notrun "src/nsexec executable not found"
2418         src/nsexec -U true 2>/dev/null || _notrun "userns not supported by this kernel"
2419 }
2420
2421 _create_loop_device()
2422 {
2423         file=$1
2424         dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"
2425         echo $dev
2426 }
2427
2428 _destroy_loop_device()
2429 {
2430         dev=$1
2431         losetup -d $dev || _fail "Cannot destroy loop device $dev"
2432 }
2433
2434 _scale_fsstress_args()
2435 {
2436     args=""
2437     while [ $# -gt 0 ]; do
2438         case "$1" in
2439             -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
2440             -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
2441             *) args="$args $1" ;;
2442         esac
2443         shift
2444     done
2445     echo $args
2446 }
2447
2448 #
2449 # Return the logical block size if running on a block device,
2450 # else substitute the page size.
2451 #
2452 _min_dio_alignment()
2453 {
2454     dev=$1
2455
2456     if [ -b "$dev" ]; then
2457         blockdev --getss $dev
2458     else
2459         $here/src/feature -s
2460     fi
2461 }
2462
2463 run_check()
2464 {
2465         echo "# $@" >> $seqres.full 2>&1
2466         "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
2467 }
2468
2469 _run_btrfs_util_prog()
2470 {
2471         run_check $BTRFS_UTIL_PROG $*
2472 }
2473
2474 _require_btrfs_send_stream_version()
2475 {
2476         $BTRFS_UTIL_PROG send 2>&1 | \
2477                 grep '^[ \t]*\-\-stream\-version <version>' > /dev/null 2>&1
2478         if [ $? -ne 0 ]; then
2479                 _notrun "Missing btrfs-progs send --stream-version command line option, skipped this test"
2480         fi
2481
2482         # test if btrfs kernel supports send stream version 2
2483         if [ ! -f /sys/fs/btrfs/send/stream_version ]; then
2484                 _notrun "Missing btrfs kernel patch for send stream version 2, skipped this test"
2485         fi
2486 }
2487
2488 _require_btrfs_mkfs_feature()
2489 {
2490         if [ -z $1 ]; then
2491                 echo "Missing feature name argument for _require_btrfs_mkfs_feature"
2492                 exit 1
2493         fi
2494         feat=$1
2495         $MKFS_BTRFS_PROG -O list-all 2>&1 | \
2496                 grep '^[ \t]*'"$feat"'\b' > /dev/null 2>&1
2497         [ $? -eq 0 ] || \
2498                 _notrun "Feature $feat not supported in the available version of mkfs.btrfs"
2499 }
2500
2501 _require_btrfs_fs_feature()
2502 {
2503         if [ -z $1 ]; then
2504                 echo "Missing feature name argument for _require_btrfs_fs_feature"
2505                 exit 1
2506         fi
2507         feat=$1
2508         modprobe btrfs > /dev/null 2>&1
2509         [ -e /sys/fs/btrfs/features/$feat ] || \
2510                 _notrun "Feature $feat not supported by the available btrfs version"
2511 }
2512
2513 _require_test_symlinks()
2514 {
2515         # IRIX UDF does not support symlinks
2516         [ "$HOSTOS" = "IRIX" -a "$FSTYP" = 'udf' ] && \
2517                 _notrun "Require symlinks support"
2518         target=`mktemp -p $TEST_DIR`
2519         link=`mktemp -p $TEST_DIR -u`
2520         ln -s `basename $target` $link
2521         if [ "$?" -ne 0 ]; then
2522                 rm -f $target
2523                 _notrun "Require symlinks support"
2524         fi
2525         rm -f $target $link
2526 }
2527
2528 _require_test_fcntl_advisory_locks()
2529 {
2530         [ "$FSTYP" != "cifs" ] && return 0
2531         cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -q "nobrl" && return 0
2532         cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -qE "nounix|forcemand" && \
2533                 _notrun "Require fcntl advisory locks support"
2534 }
2535
2536 _get_total_inode()
2537 {
2538         if [ -z "$1" ]; then
2539                 echo "Usage: _get_total_inode <mnt>"
2540                 exit 1
2541         fi
2542         local nr_inode;
2543         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $3}'`
2544         echo $nr_inode
2545 }
2546
2547 _get_used_inode()
2548 {
2549         if [ -z "$1" ]; then
2550                 echo "Usage: _get_used_inode <mnt>"
2551                 exit 1
2552         fi
2553         local nr_inode;
2554         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $4}'`
2555         echo $nr_inode
2556 }
2557
2558 _get_free_inode()
2559 {
2560         if [ -z "$1" ]; then
2561                 echo "Usage: _get_free_inode <mnt>"
2562                 exit 1
2563         fi
2564         local nr_inode;
2565         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $5}'`
2566         echo $nr_inode
2567 }
2568
2569 # get btrfs profile configs being tested
2570 #
2571 # A set of pre-set profile configs are exported via _btrfs_profile_configs
2572 # array. Default configs can be overridden by setting BTRFS_PROFILE_CONFIGS
2573 # var in the format "metadata_profile:data_profile", multiple configs can be
2574 # seperated by space, e.g.
2575 # export BTRFS_PROFILE_CONFIGS="raid0:raid0 raid1:raid1 dup:single"
2576 _btrfs_get_profile_configs()
2577 {
2578         if [ "$FSTYP" != "btrfs" ]; then
2579                 return
2580         fi
2581
2582         # no user specified btrfs profile configs, export the default configs
2583         if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
2584                 # default configs
2585                 _btrfs_profile_configs=(
2586                         "-m single -d single"
2587                         "-m dup -d single"
2588                         "-m raid0 -d raid0"
2589                         "-m raid1 -d raid0"
2590                         "-m raid1 -d raid1"
2591                         "-m raid10 -d raid10"
2592                         "-m raid5 -d raid5"
2593                         "-m raid6 -d raid6"
2594                 )
2595
2596                 # remove dup/raid5/raid6 profiles if we're doing device replace
2597                 # dup profile indicates only one device being used (SCRATCH_DEV),
2598                 # but we don't want to replace SCRATCH_DEV, which will be used in
2599                 # _scratch_mount/_check_scratch_fs etc.
2600                 # and raid5/raid6 doesn't support replace yet
2601                 if [ "$1" == "replace" ]; then
2602                         _btrfs_profile_configs=(
2603                                 "-m single -d single"
2604                                 "-m raid0 -d raid0"
2605                                 "-m raid1 -d raid0"
2606                                 "-m raid1 -d raid1"
2607                                 "-m raid10 -d raid10"
2608                                 # add these back when raid5/6 is working with replace
2609                                 #"-m raid5 -d raid5"
2610                                 #"-m raid6 -d raid6"
2611                         )
2612                 fi
2613                 export _btrfs_profile_configs
2614                 return
2615         fi
2616
2617         # parse user specified btrfs profile configs
2618         local i=0
2619         local cfg=""
2620         for cfg in $BTRFS_PROFILE_CONFIGS; do
2621                 # turn "metadata:data" format to "-m metadata -d data"
2622                 # and assign it to _btrfs_profile_configs array
2623                 cfg=`echo "$cfg" | sed -e 's/^/-m /' -e 's/:/ -d /'`
2624                 _btrfs_profile_configs[$i]="$cfg"
2625                 let i=i+1
2626         done
2627
2628         if [ "$1" == "replace" ]; then
2629                 if echo ${_btrfs_profile_configs[*]} | grep -q raid[56]; then
2630                         _notrun "RAID5/6 doesn't support btrfs device replace yet"
2631                 fi
2632                 if echo ${_btrfs_profile_configs[*]} | grep -q dup; then
2633                         _notrun "Do not set dup profile in btrfs device replace test"
2634                 fi
2635         fi
2636         export _btrfs_profile_configs
2637 }
2638
2639 # stress btrfs by running balance operation in a loop
2640 _btrfs_stress_balance()
2641 {
2642         local btrfs_mnt=$1
2643         while true; do
2644                 $BTRFS_UTIL_PROG balance start $btrfs_mnt
2645         done
2646 }
2647
2648 # stress btrfs by creating/mounting/umounting/deleting subvolume in a loop
2649 _btrfs_stress_subvolume()
2650 {
2651         local btrfs_dev=$1
2652         local btrfs_mnt=$2
2653         local subvol_name=$3
2654         local subvol_mnt=$4
2655
2656         mkdir -p $subvol_mnt
2657         while true; do
2658                 $BTRFS_UTIL_PROG subvolume create $btrfs_mnt/$subvol_name
2659                 $MOUNT_PROG -o subvol=$subvol_name $btrfs_dev $subvol_mnt
2660                 $UMOUNT_PROG $subvol_mnt
2661                 $BTRFS_UTIL_PROG subvolume delete $btrfs_mnt/$subvol_name
2662         done
2663 }
2664
2665 # stress btrfs by running scrub in a loop
2666 _btrfs_stress_scrub()
2667 {
2668         local btrfs_mnt=$1
2669         while true; do
2670                 $BTRFS_UTIL_PROG scrub start -B $btrfs_mnt
2671         done
2672 }
2673
2674 # stress btrfs by defragmenting every file/dir in a loop and compress file
2675 # contents while defragmenting if second argument is not "nocompress"
2676 _btrfs_stress_defrag()
2677 {
2678         local btrfs_mnt=$1
2679         local compress=$2
2680
2681         while true; do
2682                 if [ "$compress" == "nocompress" ]; then
2683                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2684                         $BTRFS_UTIL_PROG filesystem defrag {} \;
2685                 else
2686                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2687                         $BTRFS_UTIL_PROG filesystem defrag -clzo {} \;
2688                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2689                         $BTRFS_UTIL_PROG filesystem defrag -czlib {} \;
2690                 fi
2691         done
2692 }
2693
2694 # stress btrfs by remounting it with different compression algorithms in a loop
2695 # run this with fsstress running at background could exercise the compression
2696 # code path and ensure no race when switching compression algorithm with constant
2697 # I/O activity.
2698 _btrfs_stress_remount_compress()
2699 {
2700         local btrfs_mnt=$1
2701         while true; do
2702                 for algo in no zlib lzo; do
2703                         $MOUNT_PROG -o remount,compress=$algo $btrfs_mnt
2704                 done
2705         done
2706 }
2707
2708 # stress btrfs by replacing devices in a loop
2709 # Note that at least 3 devices are needed in SCRATCH_DEV_POOL and the last
2710 # device should be free(not used by btrfs)
2711 _btrfs_stress_replace()
2712 {
2713         local btrfs_mnt=$1
2714
2715         # The device number in SCRATCH_DEV_POOL should be at least 3,
2716         # one is SCRATCH_DEV, one is to be replaced, one is free device
2717         # we won't replace SCRATCH_DEV, see below for reason
2718         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt 3 ]; then
2719                 echo "_btrfs_stress_replace requires at least 3 devices in SCRATCH_DEV_POOL"
2720                 return
2721         fi
2722
2723         # take the last device as the first free_dev
2724         local free_dev="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $NF}'`"
2725
2726         # free_dev should be really free
2727         if $BTRFS_UTIL_PROG filesystem show $btrfs_mnt | grep -q "$free_dev"; then
2728                 echo "_btrfs_stress_replace: $free_dev is used by btrfs"
2729                 return
2730         fi
2731
2732         # dev_pool is device list being currently used by btrfs (excluding SCRATCH_DEV)
2733         # and can be replaced. We don't replace SCRATCH_DEV because it will be used in
2734         # _scratch_mount and _check_scratch_fs etc.
2735         local dev_pool=`echo $SCRATCH_DEV_POOL | sed -e "s# *$SCRATCH_DEV *##" \
2736                         -e "s# *$free_dev *##"`
2737
2738         # set the first device in dev_pool as the first src_dev to be replaced
2739         local src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
2740
2741         echo "dev_pool=$dev_pool"
2742         echo "free_dev=$free_dev, src_dev=$src_dev"
2743         while true; do
2744                 echo "Replacing $src_dev with $free_dev"
2745                 $BTRFS_UTIL_PROG replace start -fB $src_dev $free_dev $btrfs_mnt
2746                 if [ $? -ne 0 ]; then
2747                         # don't update src_dev and free_dev if replace failed
2748                         continue
2749                 fi
2750                 dev_pool="$dev_pool $free_dev"
2751                 dev_pool=`echo $dev_pool | sed -e "s# *$src_dev *##"`
2752                 free_dev=$src_dev
2753                 src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
2754         done
2755 }
2756
2757 # return device size in kb
2758 _get_device_size()
2759 {
2760         grep `_short_dev $1` /proc/partitions | awk '{print $3}'
2761 }
2762
2763 init_rc()
2764 {
2765         if [ "$iam" == new ]
2766         then
2767                 return
2768         fi
2769         # make some further configuration checks here
2770         if [ "$TEST_DEV" = ""  ]
2771         then
2772                 echo "common/rc: Error: \$TEST_DEV is not set"
2773                 exit 1
2774         fi
2775
2776         # if $TEST_DEV is not mounted, mount it now as XFS
2777         if [ -z "`_fs_type $TEST_DEV`" ]
2778         then
2779                 # $TEST_DEV is not mounted
2780                 if ! _test_mount
2781                 then
2782                         echo "common/rc: retrying test device mount with external set"
2783                         [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
2784                         if ! _test_mount
2785                         then
2786                                 echo "common/rc: could not mount $TEST_DEV on $TEST_DIR"
2787                                 exit 1
2788                         fi
2789                 fi
2790         fi
2791
2792         if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
2793         then
2794                 echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
2795                 $DF_PROG $TEST_DEV
2796                 exit 1
2797         fi
2798         # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
2799         xfs_io -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
2800         export XFS_IO_PROG="$XFS_IO_PROG -F"
2801 }
2802
2803 # get real device path name by following link
2804 _real_dev()
2805 {
2806         local _dev=$1
2807         if [ -b "$_dev" ] && [ -L "$_dev" ]; then
2808                 _dev=`readlink -f "$_dev"`
2809         fi
2810         echo $_dev
2811 }
2812
2813 # basename of a device
2814 _short_dev()
2815 {
2816         echo `basename $(_real_dev $1)`
2817 }
2818
2819 get_block_size()
2820 {
2821         if [ -z $1 ] || [ ! -d $1 ]; then
2822                 echo "Missing mount point argument for get_block_size"
2823                 exit 1
2824         fi
2825         echo `stat -f -c %S $1`
2826 }
2827
2828 init_rc
2829
2830 ################################################################################
2831 # make sure this script returns success
2832 /bin/true