xfstests: don't remove the two first devices from SCRATCH_DEV_POOL
[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 | grep noxfer > /dev/null 2>&1
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
78 _ls_l()
79 {
80         ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /"
81 }
82
83 _mount_opts()
84 {
85     # SELinux adds extra xattrs which can mess up our expected output.
86     # So, mount with a context, and they won't be created
87     # nfs_t is a "liberal" context so we can use it.
88     if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
89         SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:nfs_t:s0"
90         export SELINUX_MOUNT_OPTIONS
91     fi
92
93     case $FSTYP in
94     xfs)
95         export MOUNT_OPTIONS=$XFS_MOUNT_OPTIONS
96         ;;
97     udf)
98         export MOUNT_OPTIONS=$UDF_MOUNT_OPTIONS
99         ;;
100     nfs)
101         export MOUNT_OPTIONS=$NFS_MOUNT_OPTIONS
102         ;;
103     ext2|ext3|ext4|ext4dev)
104         # acls & xattrs aren't turned on by default on ext$FOO
105         export MOUNT_OPTIONS="-o acl,user_xattr $EXT_MOUNT_OPTIONS"
106         ;;
107     reiserfs)
108         # acls & xattrs aren't turned on by default on reiserfs
109         export MOUNT_OPTIONS="-o acl,user_xattr $REISERFS_MOUNT_OPTIONS"
110         ;;
111     gfs2)
112         # acls aren't turned on by default on gfs2
113         export MOUNT_OPTIONS="-o acl $GFS2_MOUNT_OPTIONS"
114         ;;
115     *)
116         ;;
117     esac
118 }
119
120 _mkfs_opts()
121 {
122     case $FSTYP in
123     xfs)
124         export MKFS_OPTIONS=$XFS_MKFS_OPTIONS
125         ;;
126     udf)
127         [ ! -z "$udf_fsize" ] && \
128             UDF_MKFS_OPTIONS="$UDF_MKFS_OPTIONS -s $udf_fsize"
129         export MKFS_OPTIONS=$UDF_MKFS_OPTIONS
130         ;;
131     nfs)
132         export MKFS_OPTIONS=$NFS_MKFS_OPTIONS
133         ;;
134     reiserfs)
135         export MKFS_OPTIONS="$REISERFS_MKFS_OPTIONS -q"
136         ;;
137     gfs2)
138         export MKFS_OPTIONS="$GFS2_MKFS_OPTIONS -O -p lock_nolock"
139         ;;
140     jfs)
141         export MKFS_OPTIONS="$JFS_MKFS_OPTIONS -q"
142         ;;
143     *)
144         ;;
145     esac
146 }
147
148 _fsck_opts()
149 {
150     case $FSTYP in
151     ext2|ext3|ext4|ext4dev)
152         export FSCK_OPTIONS="-nf"
153         ;;
154     reiserfs)
155         export FSCK_OPTIONS="--yes"
156         ;;
157     *)
158         export FSCK_OPTIONS="-n"
159         ;;
160     esac
161 }
162
163 [ -z "$FSTYP" ] && FSTYP=xfs
164 [ -z "$MOUNT_OPTIONS" ] && _mount_opts
165 [ -z "$MKFS_OPTIONS" ] && _mkfs_opts
166 [ -z "$FSCK_OPTIONS" ] && _fsck_opts
167
168
169 # we need common/config
170 if [ "$iam" != "check" ]
171 then
172     if ! . ./common/config
173         then
174         echo "$iam: failed to source common/config"
175         exit 1
176     fi
177 fi
178
179 # check for correct setup
180 case "$FSTYP" in
181     xfs)
182          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
183          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
184          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
185          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
186          ;;
187     udf)
188          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
189          ;;
190     btrfs)
191          [ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
192          ;;
193     nfs)
194          ;;
195 esac
196
197 # make sure we have a standard umask
198 umask 022
199
200 _mount()
201 {
202     $MOUNT_PROG `_mount_ops_filter $*`
203 }
204
205 _scratch_options()
206 {
207     type=$1
208     SCRATCH_OPTIONS=""
209
210     if [ "$FSTYP" != "xfs" ]; then
211         return
212     fi
213
214     case $type in
215     mkfs)
216         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
217         rt_opt="-r"
218         log_opt="-l"
219         ;;
220     mount)
221         rt_opt="-o"
222         log_opt="-o"
223         ;;
224     esac
225     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
226         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
227     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
228         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
229 }
230
231 _test_options()
232 {
233     type=$1
234     TEST_OPTIONS=""
235
236     if [ "$FSTYP" != "xfs" ]; then
237         return
238     fi
239
240     case $type in
241     mkfs)
242         rt_opt="-r"
243         log_opt="-l"
244         ;;
245     mount)
246         rt_opt="-o"
247         log_opt="-o"
248         ;;
249     esac
250     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
251         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
252     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
253         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
254 }
255
256 _mount_ops_filter()
257 {
258     params="$*"
259     
260     #get mount point to handle dmapi mtpt option correctly
261     let last_index=$#-1
262     [ $last_index -gt 0 ] && shift $last_index
263     FS_ESCAPED=$1
264     
265     # irix is fussy about how it is fed its mount options
266     # - multiple -o's are not allowed
267     # - no spaces between comma delimitered options
268     # the sed script replaces all -o's (except the first) with a comma
269     # not required for linux, but won't hurt
270     
271     echo $params | sed -e 's/[[:space:]]\+-o[[:space:]]*/UnIqUe/1; s/[[:space:]]\+-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
272         | sed -e 's/dmapi/dmi/' \
273         | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"
274
275 }
276
277 _scratch_mount_options()
278 {
279     _scratch_options mount
280
281     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
282 }
283
284 _scratch_mount()
285 {
286     _mount -t $FSTYP `_scratch_mount_options $*`
287 }
288
289 _scratch_unmount()
290 {
291     $UMOUNT_PROG $SCRATCH_DEV
292 }
293
294 _scratch_remount()
295 {
296     _scratch_unmount
297     _scratch_mount
298 }
299
300 _test_mount()
301 {
302     _test_options mount
303     _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
304 }
305
306 _scratch_mkfs_options()
307 {
308     _scratch_options mkfs
309     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
310 }
311
312
313 _setup_large_xfs_fs()
314 {
315         fs_size=$1
316         local tmp_dir=/tmp/
317
318         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
319         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
320         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
321
322         # calculate the size of the file we need to allocate.
323         # Default free space in the FS is 50GB, but you can specify more via
324         # SCRATCH_DEV_EMPTY_SPACE
325         file_size=$(($fs_size - 50*1024*1024*1024))
326         file_size=$(($file_size - $SCRATCH_DEV_EMPTY_SPACE))
327
328         # mount the filesystem, create the file, unmount it
329         _scratch_mount 2>&1 >$tmp_dir/mnt.err
330         local status=$?
331         if [ $status -ne 0 ]; then
332                 echo "mount failed"
333                 cat $tmp_dir/mnt.err >&2
334                 rm -f $tmp_dir/mnt.err
335                 return $status
336         fi
337         rm -f $tmp_dir/mnt.err
338
339         xfs_io -F -f \
340                 -c "truncate $file_size" \
341                 -c "falloc -k 0 $file_size" \
342                 -c "chattr +d" \
343                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
344         export NUM_SPACE_FILES=1
345         status=$?
346         umount $SCRATCH_MNT
347         if [ $status -ne 0 ]; then
348                 echo "large file prealloc failed"
349                 cat $tmp_dir/mnt.err >&2
350                 return $status
351         fi
352         return 0
353 }
354
355 _scratch_mkfs_xfs()
356 {
357     # extra mkfs options can be added by tests
358     local extra_mkfs_options=$*
359
360     local tmp_dir=/tmp/
361
362     _scratch_options mkfs
363
364     # save mkfs output in case conflict means we need to run again.
365     # only the output for the mkfs that applies should be shown
366     $MKFS_XFS_PROG $SCRATCH_OPTIONS $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
367         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
368     local mkfs_status=$?
369
370     # a mkfs failure may be caused by conflicts between
371     # $MKFS_OPTIONS and $extra_mkfs_options
372
373     if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
374         echo "** mkfs failed with extra mkfs options added to \"$MKFS_OPTIONS\" by test $seq **" \
375             >>$seqres.full
376         echo "** attempting to mkfs using only test $seq options: $extra_mkfs_options **" \
377             >>$seqres.full
378         # running mkfs again. overwrite previous mkfs output files
379         $MKFS_XFS_PROG $SCRATCH_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
380             2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
381         mkfs_status=$?
382     fi
383
384     if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
385         # manually parse the mkfs output to get the fs size in bytes
386         local fs_size
387         fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
388             if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
389                 my $size = $1 * $2;
390                 print STDOUT "$size\n";
391             }'`
392         _setup_large_xfs_fs $fs_size
393         mkfs_status=$?
394     fi
395
396     # output stored mkfs output
397     cat $tmp_dir.mkfserr >&2
398     cat $tmp_dir.mkfsstd
399     rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
400
401     return $mkfs_status
402 }
403
404 # xfs_check script is planned to be deprecated. But, we want to
405 # be able to invoke "xfs_check" behavior in xfstests in order to
406 # maintain the current verification levels.
407 _xfs_check()
408 {
409     OPTS=" "
410     DBOPTS=" "
411     USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
412
413     while getopts "b:fi:l:stvV" c
414     do
415         case $c in
416             s) OPTS=$OPTS"-s ";;
417             t) OPTS=$OPTS"-t ";;
418             v) OPTS=$OPTS"-v ";;
419             i) OPTS=$OPTS"-i "$OPTARG" ";;
420             b) OPTS=$OPTS"-b "$OPTARG" ";;
421             f) DBOPTS=$DBOPTS" -f";;
422             l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
423             V) $XFS_DB_PROG -p xfs_check -V
424                 return $?
425                 ;;
426         esac
427     done
428     set -- extra $@
429     shift $OPTIND
430     case $# in
431         1)    ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
432                status=$?
433                ;;
434         2)    echo $USAGE 1>&1
435               status=2
436               ;;
437     esac
438     return $status
439 }
440
441 _setup_large_ext4_fs()
442 {
443         fs_size=$1
444         local tmp_dir=/tmp/
445
446         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
447         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
448         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
449
450         # Default free space in the FS is 50GB, but you can specify more via
451         # SCRATCH_DEV_EMPTY_SPACE
452         space_to_consume=$(($fs_size - 50*1024*1024*1024 - $SCRATCH_DEV_EMPTY_SPACE))
453
454         # mount the filesystem and create 16TB - 4KB files until we consume
455         # all the necessary space.
456         _scratch_mount 2>&1 >$tmp_dir/mnt.err
457         local status=$?
458         if [ $status -ne 0 ]; then
459                 echo "mount failed"
460                 cat $tmp_dir/mnt.err >&2
461                 rm -f $tmp_dir/mnt.err
462                 return $status
463         fi
464         rm -f $tmp_dir/mnt.err
465
466         file_size=$((16*1024*1024*1024*1024 - 4096))
467         nfiles=0
468         while [ $space_to_consume -gt $file_size ]; do
469
470                 xfs_io -F -f \
471                         -c "truncate $file_size" \
472                         -c "falloc -k 0 $file_size" \
473                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
474                 status=$?
475                 if [ $status -ne 0 ]; then
476                         break;
477                 fi
478
479                 space_to_consume=$(( $space_to_consume - $file_size ))
480                 nfiles=$(($nfiles + 1))
481         done
482
483         # consume the remaining space.
484         if [ $space_to_consume -gt 0 ]; then
485                 xfs_io -F -f \
486                         -c "truncate $space_to_consume" \
487                         -c "falloc -k 0 $space_to_consume" \
488                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
489                 status=$?
490         fi
491         export NUM_SPACE_FILES=$nfiles
492
493         umount $SCRATCH_MNT
494         if [ $status -ne 0 ]; then
495                 echo "large file prealloc failed"
496                 cat $tmp_dir/mnt.err >&2
497                 return $status
498         fi
499         return 0
500 }
501 _scratch_mkfs_ext4()
502 {
503         local tmp_dir=/tmp/
504
505         /sbin/mkfs -t $FSTYP -- -F $MKFS_OPTIONS $* $SCRATCH_DEV \
506                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
507         local mkfs_status=$?
508
509         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
510                 # manually parse the mkfs output to get the fs size in bytes
511                 fs_size=`cat $tmp_dir.mkfsstd | awk ' \
512                         /^Block size/ { split($2, a, "="); bs = a[2] ; } \
513                         / inodes, / { blks = $3 } \
514                         /reserved for the super user/ { resv = $1 } \
515                         END { fssize = bs * blks - resv; print fssize }'`
516
517                 _setup_large_ext4_fs $fs_size
518                 mkfs_status=$?
519         fi
520
521         # output stored mkfs output
522         cat $tmp_dir.mkfserr >&2
523         cat $tmp_dir.mkfsstd
524         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
525
526         return $mkfs_status
527 }
528
529 _scratch_mkfs()
530 {
531     case $FSTYP in
532     xfs)
533         _scratch_mkfs_xfs $*
534         ;;
535     nfs*)
536         # do nothing for nfs
537         ;;
538     udf)
539         $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
540         ;;
541     btrfs)
542         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
543         ;;
544     ext4)
545         _scratch_mkfs_ext4 $*
546         ;;
547     *)
548         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV
549         ;;
550     esac
551 }
552
553 _scratch_pool_mkfs()
554 {
555     case $FSTYP in
556     btrfs)
557         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV_POOL > /dev/null
558         ;;
559     *)
560         echo "_scratch_pool_mkfs is not implemented for $FSTYP" 1>&2
561         ;;
562     esac
563 }
564
565 # Create fs of certain size on scratch device
566 # _scratch_mkfs_sized <size in bytes> [optional blocksize]
567 _scratch_mkfs_sized()
568 {
569     fssize=$1
570     blocksize=$2
571     [ -z "$blocksize" ] && blocksize=4096
572     blocks=`expr $fssize / $blocksize`
573
574     if [ "$HOSTOS" == "Linux" ]; then
575         devsize=`blockdev --getsize64 $SCRATCH_DEV`
576         [ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
577     fi
578
579     case $FSTYP in
580     xfs)
581         _scratch_mkfs_xfs -d size=$fssize -b size=$blocksize
582         ;;
583     ext2|ext3|ext4|ext4dev)
584         yes | ${MKFS_PROG}.$FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
585         ;;
586     btrfs)
587         $MKFS_BTRFS_PROG $MKFS_OPTIONS -b $fssize $SCRATCH_DEV
588         ;;
589     *)
590         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
591         ;;
592     esac
593 }
594
595 # Emulate an N-data-disk stripe w/ various stripe units
596 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
597 _scratch_mkfs_geom()
598 {
599     sunit_bytes=$1
600     swidth_mult=$2
601     blocksize=$3
602     [ -z "$blocksize" ] && blocksize=4096
603
604     let sunit_blocks=$sunit_bytes/$blocksize
605     let swidth_blocks=$sunit_blocks*$swidth_mult
606
607     case $FSTYP in
608     xfs)
609         MKFS_OPTIONS+=" -b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
610         ;;
611     ext4|ext4dev)
612         MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
613         ;;
614     *)
615         _notrun "can't mkfs $FSTYP with geometry"
616         ;;
617     esac
618     _scratch_mkfs
619 }
620
621 _scratch_resvblks()
622 {
623         case $FSTYP in
624         xfs)
625                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
626                 ;;
627         *)
628                 ;;
629         esac
630 }
631
632 _scratch_xfs_db_options()
633 {
634     SCRATCH_OPTIONS=""
635     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
636         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
637     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
638 }
639
640 _scratch_xfs_logprint()
641 {
642     SCRATCH_OPTIONS=""
643     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
644         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
645     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
646 }
647
648 _scratch_xfs_check()
649 {
650     SCRATCH_OPTIONS=""
651     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
652         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
653     [ "$LARGE_SCRATCH_DEV" = yes ] && \
654         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
655     _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
656 }
657
658 _scratch_xfs_repair()
659 {
660     SCRATCH_OPTIONS=""
661     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
662         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
663     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
664         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
665     [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
666     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
667 }
668
669 _get_pids_by_name()
670 {
671     if [ $# -ne 1 ]
672     then
673         echo "Usage: _get_pids_by_name process-name" 1>&2
674         exit 1
675     fi
676
677     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
678     # HH:MM:SS before the psargs field, use this as the search anchor.
679     #
680     # Matches with $1 (process-name) occur if the first psarg is $1
681     # or ends in /$1 ... the matching uses sed's regular expressions,
682     # so passing a regex into $1 will work.
683
684     ps $PS_ALL_FLAGS \
685     | sed -n \
686         -e 's/$/ /' \
687         -e 's/[         ][      ]*/ /g' \
688         -e 's/^ //' \
689         -e 's/^[^ ]* //' \
690         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
691         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
692 }
693
694 # fix malloc libs output
695 #
696 _fix_malloc()
697 {
698     # filter out the Electric Fence notice
699     $PERL_PROG -e '
700         while (<>) {
701             if (defined $o && /^\s+Electric Fence/) {
702                 chomp($o);
703                 print "$o";
704                 undef $o;
705                 next;
706             }
707             print $o if (defined $o);
708
709             $o=$_;
710         }
711         print $o if (defined $o);
712     '
713 }
714
715 # check if run as root
716 #
717 _need_to_be_root()
718 {
719     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
720     if [ "$id" -ne 0 ]
721     then
722         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
723         exit 1
724     fi
725 }
726
727
728 #
729 # _df_device : get an IRIX style df line for a given device
730 #
731 #       - returns "" if not mounted
732 #       - returns fs type in field two (ala IRIX)
733 #       - joins line together if split by fancy df formatting
734 #       - strips header etc
735 #
736
737 _df_device()
738 {
739     if [ $# -ne 1 ]
740     then
741         echo "Usage: _df_device device" 1>&2
742         exit 1
743     fi
744
745     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
746         match($1,what) && NF==1 {
747             v=$1
748             getline
749             print v, $0
750             exit
751         }
752         match($1,what) {
753             print
754             exit
755         }
756     '
757 }
758
759 #
760 # _df_dir : get an IRIX style df line for device where a directory resides
761 #
762 #       - returns fs type in field two (ala IRIX)
763 #       - joins line together if split by fancy df formatting
764 #       - strips header etc
765 #
766
767 _df_dir()
768 {
769     if [ $# -ne 1 ]
770     then
771         echo "Usage: _df_dir device" 1>&2
772         exit 1
773     fi
774
775     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
776         NR == 2 && NF==1 {
777             v=$1
778             getline
779             print v, $0;
780             exit 0
781         }
782         NR == 2 {
783             print;
784             exit 0
785         }
786         {}
787     '
788     # otherwise, nada
789 }
790
791 # return percentage used disk space for mounted device
792
793 _used()
794 {
795     if [ $# -ne 1 ]
796     then
797         echo "Usage: _used device" 1>&2
798         exit 1
799     fi
800
801     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
802 }
803
804 # return the FS type of a mounted device
805 #
806 _fs_type()
807 {
808     if [ $# -ne 1 ]
809     then
810         echo "Usage: _fs_type device" 1>&2
811         exit 1
812     fi
813
814     #
815     # The Linux kernel shows NFSv4 filesystems in df output as
816     # filesystem type nfs4, although we mounted it as nfs earlier.
817     # Fix the filesystem type up here so that the callers don't
818     # have to bother with this quirk.
819     #
820     _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
821 }
822
823 # return the FS mount options of a mounted device
824 #
825 # should write a version which just parses the output of mount for IRIX
826 # compatibility, but since this isn't used at all, at the moment I'll leave
827 # this for now
828 #
829 _fs_options()
830 {
831     if [ $# -ne 1 ]
832     then
833         echo "Usage: _fs_options device" 1>&2
834         exit 1
835     fi
836
837     $AWK_PROG -v dev=$1 '
838         match($1,dev) { print $4 }
839     ' </proc/mounts
840 }
841
842 # returns device number if a file is a block device
843 #
844 _is_block_dev()
845 {
846     if [ $# -ne 1 ]
847     then
848         echo "Usage: _is_block_dev dev" 1>&2
849         exit 1
850     fi
851
852     _dev=$1
853     if [ -L "${_dev}" ]; then
854         _dev=`readlink -f ${_dev}`
855     fi
856
857     if [ -b "${_dev}" ]; then
858         src/lstat64 ${_dev} | $AWK_PROG '/Device type:/ { print $9 }'
859     fi
860 }
861
862 # Do a command, log it to $seqres.full, optionally test return status
863 # and die if command fails. If called with one argument _do executes the
864 # command, logs it, and returns its exit status. With two arguments _do
865 # first prints the message passed in the first argument, and then "done"
866 # or "fail" depending on the return status of the command passed in the
867 # second argument. If the command fails and the variable _do_die_on_error
868 # is set to "always" or the two argument form is used and _do_die_on_error
869 # is set to "message_only" _do will print an error message to
870 # $seqres.out and exit.
871
872 _do()
873 {
874     if [ $# -eq 1 ]; then
875         _cmd=$1
876     elif [ $# -eq 2 ]; then
877         _note=$1
878         _cmd=$2
879         echo -n "$_note... "
880     else
881         echo "Usage: _do [note] cmd" 1>&2
882         status=1; exit
883     fi
884
885     (eval "echo '---' \"$_cmd\"") >>$seqres.full
886     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
887     cat $tmp._out | _fix_malloc >>$seqres.full
888     if [ $# -eq 2 ]; then
889         if [ $ret -eq 0 ]; then
890             echo "done"
891         else
892             echo "fail"
893         fi
894     fi
895     if [ $ret -ne 0  ] \
896         && [ "$_do_die_on_error" = "always" \
897             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
898     then
899         [ $# -ne 2 ] && echo
900         eval "echo \"$_cmd\" failed \(returned $ret\): see $seqres.full"
901         status=1; exit
902     fi
903
904     return $ret
905 }
906
907 # bail out, setting up .notrun file
908 #
909 _notrun()
910 {
911     echo "$*" > $seqres.notrun
912     echo "$seq not run: $*"
913     status=0
914     exit
915 }
916
917 # just plain bail out
918 #
919 _fail()
920 {
921     echo "$*" | tee -a $seqres.full
922     echo "(see $seqres.full for details)"
923     status=1
924     exit 1
925 }
926
927 # tests whether $FSTYP is one of the supported filesystems for a test
928 #
929 _supported_fs()
930 {
931     for f
932     do
933         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
934         then
935             return
936         fi
937     done
938
939     _notrun "not suitable for this filesystem type: $FSTYP"
940 }
941
942 # tests whether $FSTYP is one of the supported OSes for a test
943 #
944 _supported_os()
945 {
946     for h
947     do
948         if [ "$h" = "$HOSTOS" ]
949         then
950             return
951         fi
952     done
953
954     _notrun "not suitable for this OS: $HOSTOS"
955 }
956
957 # this test needs a scratch partition - check we're ok & unmount it
958 #
959 _require_scratch()
960 {
961     case "$FSTYP" in
962         nfs*)
963                  echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
964                  if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]
965                  then
966                      _notrun "this test requires a valid \$SCRATCH_DEV"
967                  fi
968                  ;;
969         *)
970                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
971                  then
972                      _notrun "this test requires a valid \$SCRATCH_DEV"
973                  fi
974                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
975                  then
976                      _notrun "this test requires a valid \$SCRATCH_DEV"
977                  fi
978                 if [ ! -d "$SCRATCH_MNT" ]
979                 then
980                      _notrun "this test requires a valid \$SCRATCH_MNT"
981                 fi
982                  ;;
983     esac
984
985     # mounted?
986     if _mount | grep -q $SCRATCH_DEV
987     then
988         # if it's mounted, make sure its on $SCRATCH_MNT
989         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
990         then
991             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
992             exit 1
993         fi
994         # and then unmount it
995         if ! $UMOUNT_PROG $SCRATCH_DEV
996         then
997             echo "failed to unmount $SCRATCH_DEV"
998             exit 1
999         fi
1000     fi
1001 }
1002
1003 # this test needs a logdev
1004 #
1005 _require_logdev()
1006 {
1007     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
1008         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
1009     [ "$USE_EXTERNAL" != yes ] && \
1010         _notrun "This test requires USE_EXTERNAL to be enabled"
1011
1012     # ensure its not mounted
1013     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
1014 }
1015
1016 # this test requires loopback device support
1017 #
1018 _require_loop()
1019 {
1020     if [ "$HOSTOS" != "Linux" ]
1021     then
1022         _notrun "This test requires linux for loopback device support"
1023     fi
1024
1025     modprobe loop >/dev/null 2>&1
1026     if grep loop /proc/devices >/dev/null 2>&1
1027     then
1028         :
1029     else
1030         _notrun "This test requires loopback device support"
1031     fi
1032 }
1033
1034 # this test requires ext2 filesystem support
1035 #
1036 _require_ext2()
1037 {
1038     if [ "$HOSTOS" != "Linux" ]
1039     then
1040         _notrun "This test requires linux for ext2 filesystem support"
1041     fi
1042
1043     modprobe ext2 >/dev/null 2>&1
1044     if grep ext2 /proc/filesystems >/dev/null 2>&1
1045     then
1046         :
1047     else
1048         _notrun "This test requires ext2 filesystem support"
1049     fi
1050 }
1051
1052 # this test requires that (large) loopback device files are not in use
1053 #
1054 _require_no_large_scratch_dev()
1055 {
1056     [ "$LARGE_SCRATCH_DEV" = yes ] && \
1057         _notrun "Large filesystem testing in progress, skipped this test"
1058 }
1059
1060 # this test requires that a realtime subvolume is in use, and
1061 # that the kernel supports realtime as well.
1062 #
1063 _require_realtime()
1064 {
1065     [ "$USE_EXTERNAL" = yes ] || \
1066         _notrun "External volumes not in use, skipped this test"
1067     [ "$SCRATCH_RTDEV" = "" ] && \
1068         _notrun "Realtime device required, skipped this test"
1069 }
1070
1071 # this test requires that a specified command (executable) exists
1072 # $1 - command, $2 - name for error message
1073 #
1074 _require_command()
1075 {
1076     [ -n "$1" ] && _cmd="$1" || _cmd="$2"
1077     [ -n "$1" -a -x "$1" ] || _notrun "$_cmd utility required, skipped this test"
1078 }
1079
1080 # this test requires the device mapper flakey target
1081 #
1082 _require_dm_flakey()
1083 {
1084     _require_command $DMSETUP_PROG
1085
1086     modprobe dm-flakey >/dev/null 2>&1
1087     $DMSETUP_PROG targets | grep flakey >/dev/null 2>&1
1088     if [ $? -eq 0 ]
1089     then
1090         :
1091     else
1092         _notrun "This test requires dm flakey support"
1093     fi
1094 }
1095
1096 # this test requires the projid32bit feature to be available in
1097 # mkfs.xfs
1098 #
1099 _require_projid32bit()
1100 {
1101         _scratch_mkfs_xfs -f -i projid32bit=0 2>&1 >/dev/null \
1102            || _notrun "mkfs.xfs doesn't have projid32bit feature"
1103 }
1104
1105 # this test requires that external log/realtime devices are not in use
1106 #
1107 _require_nonexternal()
1108 {
1109     [ "$USE_EXTERNAL" = yes ] && \
1110         _notrun "External device testing in progress, skipped this test"
1111 }
1112
1113 # this test requires that a (specified) aio-dio executable exists
1114 # $1 - command (optional)
1115 #
1116 _require_aiodio()
1117 {
1118     if [ -z "$1" ]
1119     then
1120         AIO_TEST=src/aio-dio-regress/aiodio_sparse2
1121         [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
1122     else
1123         AIO_TEST=src/aio-dio-regress/$1
1124         [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
1125     fi
1126 }
1127
1128 # run an aio-dio program
1129 # $1 - command
1130 _run_aiodio()
1131 {
1132     if [ -z "$1" ]
1133     then
1134         echo "usage: _run_aiodio command_name" 2>&1
1135         status=1; exit 1
1136     fi
1137
1138     _require_aiodio $1
1139
1140     local testtemp=$TEST_DIR/aio-testfile
1141     rm -f $testtemp
1142     $AIO_TEST $testtemp 2>&1
1143     status=$?
1144     rm -f $testtemp
1145
1146     return $status
1147 }
1148
1149 # indicate whether YP/NIS is active or not
1150 #
1151 _yp_active()
1152 {
1153         local dn
1154         dn=$(domainname 2>/dev/null)
1155         test -n "${dn}" -a "${dn}" != "(none)"
1156         echo $?
1157 }
1158
1159 # cat the password file
1160 #
1161 _cat_passwd()
1162 {
1163         [ $(_yp_active) -eq 0 ] && ypcat passwd
1164         cat /etc/passwd
1165 }
1166
1167 # cat the group file
1168 #
1169 _cat_group()
1170 {
1171         [ $(_yp_active) -eq 0 ] && ypcat group
1172         cat /etc/group
1173 }
1174
1175 # check for the fsgqa user on the machine
1176 #
1177 _require_user()
1178 {
1179     qa_user=fsgqa
1180     _cat_passwd | grep -q $qa_user
1181     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
1182     echo /bin/true | su $qa_user
1183     [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
1184 }
1185
1186 # check for the fsgqa group on the machine
1187 #
1188 _require_group()
1189 {
1190     qa_group=fsgqa
1191     _cat_group | grep -q $qa_group
1192     [ "$?" == "0" ] || _notrun "$qa_group user not defined."
1193 }
1194
1195 _filter_user_do()
1196 {
1197         perl -ne "
1198 s,.*Permission\sdenied.*,Permission denied,;
1199 s,.*no\saccess\sto\stty.*,,;
1200 s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
1201 s,^\s*$,,;
1202         print;"
1203 }
1204
1205 _user_do()
1206 {
1207     if [ "$HOSTOS" == "IRIX" ]
1208         then
1209         echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
1210     else
1211         echo $1 | su $qa_user 2>&1 | _filter_user_do
1212     fi
1213 }
1214
1215 # check that xfs_io, kernel, and filesystem all support zero
1216 _require_xfs_io_zero()
1217 {
1218         testio=`$XFS_IO_PROG -c "zero help" 2>&1`
1219         echo $testio | grep -q 'command "zero" not found' && \
1220                 _notrun "zero command not supported"
1221 }
1222
1223 # check that xfs_io, glibc, kernel, and filesystem all (!) support
1224 # fallocate
1225 #
1226 _require_xfs_io_falloc()
1227 {
1228         testfile=$TEST_DIR/$$.falloc
1229         testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $testfile 2>&1`
1230         rm -f $testfile 2>&1 > /dev/null
1231         echo $testio | grep -q "not found" && \
1232                 _notrun "xfs_io fallocate support is missing"
1233         echo $testio | grep -q "Operation not supported" && \
1234                 _notrun "xfs_io fallocate command failed (old kernel/wrong fs?)"
1235 }
1236
1237 # check that xfs_io, kernel and filesystem all support fallocate with hole
1238 # punching
1239 _require_xfs_io_falloc_punch()
1240 {
1241         testfile=$TEST_DIR/$$.falloc
1242         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1243                 -c "fpunch 4k 8k" $testfile 2>&1`
1244         rm -f $testfile 2>&1 > /dev/null
1245         echo $testio | grep -q "not found" && \
1246                 _notrun "xfs_io fallocate punch support is missing"
1247         echo $testio | grep -q "Operation not supported" && \
1248                 _notrun "xfs_io fallocate punch command failed (no fs support?)"
1249 }
1250
1251 # check that xfs_io, kernel and filesystem support fiemap
1252 _require_xfs_io_fiemap()
1253 {
1254         testfile=$TEST_DIR/$$.fiemap
1255         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1256                 -c "fiemap -v" $testfile 2>&1`
1257         rm -f $testfile 2>&1 > /dev/null
1258         echo $testio | grep -q "not found" && \
1259                 _notrun "xfs_io fiemap support is missing"
1260         echo $testio | grep -q "Operation not supported" && \
1261                 _notrun "xfs_io fiemap command failed (no fs support?)"
1262 }
1263
1264 # Check that a fs has enough free space (in 1024b blocks)
1265 #
1266 _require_fs_space()
1267 {
1268         MNT=$1
1269         BLOCKS=$2       # in units of 1024
1270         let GB=$BLOCKS/1024/1024
1271
1272         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
1273         [ $FREE_BLOCKS -lt $BLOCKS ] && \
1274                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
1275 }
1276
1277 #
1278 # Check if the filesystem supports sparse files.
1279 #
1280 # Unfortunately there is no better way to do this than a manual black list.
1281 #
1282 _require_sparse_files()
1283 {
1284     case $FSTYP in
1285     hfsplus)
1286         _notrun "Sparse files not supported by this filesystem type: $FSTYP"
1287         ;;
1288     *)
1289         ;;
1290     esac
1291 }
1292
1293 _require_debugfs()
1294 {
1295     #boot_params always present in debugfs
1296     [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
1297 }
1298
1299 _require_fail_make_request()
1300 {
1301     [ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
1302         || _notrun "$DEBUGFS_MNT/fail_make_request \
1303  not found. Seems that CONFIG_FAIL_MAKE_REQUEST kernel config option not enabled"
1304 }
1305
1306 #
1307 # Check if the file system supports seek_data/hole
1308 #
1309 _require_seek_data_hole()
1310 {
1311     testfile=$TEST_DIR/$$.seek
1312     testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`
1313     rm -f $testfile &>/dev/null
1314     echo $testseek | grep -q "Kernel does not support" && \
1315         _notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
1316 }
1317
1318 # check that a FS on a device is mounted
1319 # if so, return mount point
1320 #
1321 _is_mounted()
1322 {
1323     if [ $# -ne 1 ]
1324     then
1325         echo "Usage: _is_mounted device" 1>&2
1326         exit 1
1327     fi
1328
1329     device=$1
1330
1331     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
1332         pattern        { print $3 ; exit 0 }
1333         END            { exit 1 }
1334     '
1335     then
1336         echo "_is_mounted: $device is not a mounted $FSTYP FS"
1337         exit 1
1338     fi
1339 }
1340
1341 # remount a FS to a new mode (ro or rw)
1342 #
1343 _remount()
1344 {
1345     if [ $# -ne 2 ]
1346     then
1347         echo "Usage: _remount device ro/rw" 1>&2
1348         exit 1
1349     fi
1350     device=$1
1351     mode=$2
1352
1353     if ! mount -o remount,$mode $device
1354     then
1355         echo "_remount: failed to remount filesystem on $device as $mode"
1356         exit 1
1357     fi
1358 }
1359
1360 # Run the appropriate repair/check on a filesystem
1361 #
1362 # if the filesystem is mounted, it's either remounted ro before being
1363 # checked or it's unmounted and then remounted
1364 #
1365
1366 # If set, we remount ro instead of unmounting for fsck
1367 USE_REMOUNT=0
1368
1369 _umount_or_remount_ro()
1370 {
1371     if [ $# -ne 1 ]
1372     then
1373         echo "Usage: _umount_or_remount_ro <device>" 1>&2
1374         exit 1
1375     fi
1376
1377     device=$1
1378     mountpoint=`_is_mounted $device`
1379
1380     if [ $USE_REMOUNT -eq 0 ]; then
1381         $UMOUNT_PROG $device
1382     else
1383         _remount $device ro
1384     fi
1385     echo "$mountpoint"
1386 }
1387
1388 _mount_or_remount_rw()
1389 {
1390     if [ $# -ne 3 ]
1391     then
1392         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
1393         exit 1
1394     fi
1395     mount_opts=$1
1396     device=$2
1397     mountpoint=$3
1398
1399     if [ $USE_REMOUNT -eq 0 ]
1400     then
1401         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
1402         then
1403             echo "!!! failed to remount $device on $mountpoint"
1404             return 0 # ok=0
1405         fi
1406     else
1407         _remount $device rw
1408     fi
1409
1410     return 1 # ok=1
1411 }
1412
1413 # Check a generic filesystem in no-op mode; this assumes that the
1414 # underlying fsck program accepts "-n" for a no-op (check-only) run,
1415 # and that it will still return an errno for corruption in this mode.
1416 #
1417 # Filesystems which don't support this will need to define their
1418 # own check routine.
1419 #
1420 _check_generic_filesystem()
1421 {
1422     device=$1
1423
1424     # If type is set, we're mounted
1425     type=`_fs_type $device`
1426     ok=1
1427
1428     if [ "$type" = "$FSTYP" ]
1429     then
1430         # mounted ...
1431         mountpoint=`_umount_or_remount_ro $device`
1432     fi
1433
1434     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
1435     if [ $? -ne 0 ]
1436     then
1437         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1438
1439         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
1440         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1441         cat $tmp.fsck                           >>$seqres.full
1442         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1443
1444         ok=0
1445     fi
1446     rm -f $tmp.fsck
1447
1448     if [ $ok -eq 0 ]
1449     then
1450         echo "*** mount output ***"             >>$seqres.full
1451         _mount                                  >>$seqres.full
1452         echo "*** end mount output"             >>$seqres.full
1453     elif [ "$type" = "$FSTYP" ]
1454     then
1455         # was mounted ...
1456         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1457         ok=$?
1458     fi
1459
1460     if [ $ok -eq 0 ]; then
1461         status=1
1462         exit 1
1463     fi
1464
1465     return 0
1466 }
1467
1468 # run xfs_check and friends on a FS.
1469
1470 _check_xfs_filesystem()
1471 {
1472     if [ $# -ne 3 ]
1473     then
1474         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
1475         exit 1
1476     fi
1477
1478     extra_mount_options=""
1479     device=$1
1480     if [ "$2" != "none" ]; then
1481         extra_log_options="-l$2"
1482         extra_mount_options="-ologdev=$2"
1483     fi
1484
1485     if [ "$3" != "none" ]; then
1486         extra_rt_options="-r$3"
1487         extra_mount_options=$extra_mount_options" -ortdev=$3"
1488     fi
1489     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
1490
1491     [ "$FSTYP" != xfs ] && return 0
1492
1493     type=`_fs_type $device`
1494     ok=1
1495
1496     if [ "$type" = "xfs" ]
1497     then
1498         # mounted ...
1499         mountpoint=`_umount_or_remount_ro $device`
1500     fi
1501
1502     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1503                 | tee $tmp.logprint | grep -q "<CLEAN>"
1504     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1505     then
1506         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
1507
1508         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
1509         echo "*** xfs_logprint -t output ***"   >>$seqres.full
1510         cat $tmp.logprint                       >>$seqres.full
1511         echo "*** end xfs_logprint output"      >>$seqres.full
1512
1513         ok=0
1514     fi
1515
1516     # xfs_check runs out of memory on large files, so even providing the test
1517     # option (-t) to avoid indexing the free space trees doesn't make it pass on
1518     # large filesystems. Avoid it.
1519     if [ "$LARGE_SCRATCH_DEV" != yes ]; then
1520             _xfs_check $extra_log_options $device 2>&1 |\
1521                  _fix_malloc >$tmp.fs_check
1522     fi
1523     if [ -s $tmp.fs_check ]
1524     then
1525         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
1526
1527         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1528         echo "*** xfs_check output ***"         >>$seqres.full
1529         cat $tmp.fs_check                       >>$seqres.full
1530         echo "*** end xfs_check output"         >>$seqres.full
1531
1532         ok=0
1533     fi
1534
1535     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1536     if [ $? -ne 0 ]
1537     then
1538         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
1539
1540         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1541         echo "*** xfs_repair -n output ***"     >>$seqres.full
1542         cat $tmp.repair | _fix_malloc           >>$seqres.full
1543         echo "*** end xfs_repair output"        >>$seqres.full
1544
1545         ok=0
1546     fi
1547     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1548
1549     if [ $ok -eq 0 ]
1550     then
1551         echo "*** mount output ***"             >>$seqres.full
1552         _mount                                  >>$seqres.full
1553         echo "*** end mount output"             >>$seqres.full
1554     elif [ "$type" = "xfs" ]
1555     then
1556         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1557     fi
1558
1559     if [ $ok -eq 0 ]; then
1560         status=1
1561         exit 1
1562     fi
1563
1564     return 0
1565 }
1566
1567 # Filter the knowen errors the UDF Verifier reports.
1568 _udf_test_known_error_filter()
1569 {
1570         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."
1571
1572 }
1573
1574 _check_udf_filesystem()
1575 {
1576     [ "$DISABLE_UDF_TEST" == "1" ] && return
1577
1578     if [ $# -ne 1 -a $# -ne 2 ]
1579     then
1580         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1581         exit 1
1582     fi
1583
1584     if [ ! -x $here/src/udf_test ]
1585     then
1586         echo "udf_test not installed, please download and build the Philips"
1587         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1588         echo "Then copy the udf_test binary to $here/src/."
1589         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1590         echo "to 1."
1591         return
1592     fi
1593
1594     device=$1
1595     if [ $# -eq 2 ];
1596     then
1597         LAST_BLOCK=`expr \( $2 - 1 \)`
1598         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1599     fi
1600
1601     rm -f $seqres.checkfs
1602     sleep 1 # Due to a problem with time stamps in udf_test
1603     $here/src/udf_test $OPT_ARG $device | tee $seqres.checkfs | egrep "Error|Warning" | \
1604         _udf_test_known_error_filter | \
1605         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" | \
1606         sed "s/^.*$/Warning UDF Verifier reported errors see $seqres.checkfs./g"
1607
1608 }
1609
1610 _check_xfs_test_fs()
1611 {
1612     TEST_LOG="none"
1613     TEST_RT="none"
1614     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1615         TEST_LOG="$TEST_LOGDEV"
1616
1617     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1618         TEST_RT="$TEST_RTDEV"
1619
1620     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1621
1622     # check for ipath consistency
1623     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1624         # errors go to stderr
1625         xfs_check_ipaths $TEST_DIR >/dev/null
1626         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1627     fi
1628 }
1629
1630 _check_btrfs_filesystem()
1631 {
1632     device=$1
1633
1634     # If type is set, we're mounted
1635     type=`_fs_type $device`
1636     ok=1
1637
1638     if [ "$type" = "$FSTYP" ]
1639     then
1640         # mounted ...
1641         mountpoint=`_umount_or_remount_ro $device`
1642     fi
1643
1644     btrfsck $device >$tmp.fsck 2>&1
1645     if [ $? -ne 0 ]
1646     then
1647         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1648
1649         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1650         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1651         cat $tmp.fsck                           >>$seqres.full
1652         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1653
1654         ok=0
1655     fi
1656     rm -f $tmp.fsck
1657
1658     if [ $ok -eq 0 ]
1659     then
1660         echo "*** mount output ***"             >>$seqres.full
1661         _mount                                  >>$seqres.full
1662         echo "*** end mount output"             >>$seqres.full
1663     elif [ "$type" = "$FSTYP" ]
1664     then
1665         # was mounted ...
1666         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1667         ok=$?
1668     fi
1669
1670     if [ $ok -eq 0 ]; then
1671         status=1
1672         exit 1
1673     fi
1674
1675     return 0
1676 }
1677
1678 _check_test_fs()
1679 {
1680     case $FSTYP in
1681     xfs)
1682         _check_xfs_test_fs
1683         ;;
1684     nfs)
1685         # no way to check consistency for nfs
1686         ;;
1687     udf)
1688         # do nothing for now
1689         ;;
1690     btrfs)
1691         _check_btrfs_filesystem $TEST_DEV
1692         ;;
1693     *)
1694         _check_generic_filesystem $TEST_DEV
1695         ;;
1696     esac
1697 }
1698
1699 _check_scratch_fs()
1700 {
1701     device=$SCRATCH_DEV
1702     [ $# -eq 1 ] && device=$1
1703
1704     case $FSTYP in
1705     xfs)
1706         SCRATCH_LOG="none"
1707         SCRATCH_RT="none"
1708         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1709             SCRATCH_LOG="$SCRATCH_LOGDEV"
1710
1711         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1712             SCRATCH_RT="$SCRATCH_RTDEV"
1713
1714         _check_xfs_filesystem $device $SCRATCH_LOG $SCRATCH_RT
1715         ;;
1716     udf)
1717         _check_udf_filesystem $device $udf_fsize
1718         ;;
1719     nfs*)
1720         # Don't know how to check an NFS filesystem, yet.
1721         ;;
1722     btrfs)
1723         _check_btrfs_filesystem $device
1724         ;;
1725     *)
1726         _check_generic_filesystem $device
1727         ;;
1728     esac
1729 }
1730
1731 _full_fstyp_details()
1732 {
1733      [ -z "$FSTYP" ] && FSTYP=xfs
1734      if [ $FSTYP = xfs ]; then
1735         if [ -d /proc/fs/xfs ]; then
1736             if grep -q 'debug 0' /proc/fs/xfs/stat; then
1737                 FSTYP="$FSTYP (non-debug)"
1738             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
1739                 FSTYP="$FSTYP (debug)"
1740             fi
1741         else
1742             if uname -a | grep -qi 'debug'; then
1743                 FSTYP="$FSTYP (debug)"
1744             else
1745                 FSTYP="$FSTYP (non-debug)"
1746             fi
1747         fi
1748      fi
1749      echo $FSTYP
1750 }
1751
1752 _full_platform_details()
1753 {
1754      os=`uname -s`
1755      host=`hostname -s`
1756      kernel=`uname -r`
1757      platform=`uname -m`
1758      echo "$os/$platform $host $kernel"
1759 }
1760
1761 _setup_udf_scratchdir()
1762 {
1763     [ "$FSTYP" != "udf" ] \
1764         && _fail "setup_udf_testdir: \$FSTYP is not udf"
1765     [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
1766         && _notrun "this test requires a valid \$SCRATCH_DEV"
1767     [ -z "$SCRATCH_MNT" ] \
1768         && _notrun "this test requires a valid \$SCRATCH_MNT"
1769
1770     # mounted?
1771     if _mount | grep -q $SCRATCH_DEV
1772     then
1773         # if it's mounted, make sure its on $TEST_RW_DIR
1774         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1775         then
1776             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1777         fi
1778         $UMOUNT_PROG $SCRATCH_DEV
1779     fi
1780
1781     _scratch_mkfs
1782     _scratch_mount
1783
1784     testdir=$SCRATCH_MNT
1785 }
1786
1787 _setup_nfs_scratchdir()
1788 {
1789     [ "$FSTYP" != "nfs" ] \
1790         && _fail "setup_nfs_testdir: \$FSTYP is not nfs"
1791     [ -z "$SCRATCH_DEV" ] \
1792         && _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
1793     [ -z "$SCRATCH_MNT" ] \
1794         && _notrun "this test requires a valid \$SCRATCH_MNT"
1795
1796     # mounted?
1797     if _mount | grep -q $SCRATCH_DEV
1798     then
1799         # if it's mounted, make sure its on $SCRATCH_MNT
1800         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1801         then
1802             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1803         fi
1804         $UMOUNT_PROG $SCRATCH_DEV
1805     fi
1806
1807     _scratch_mkfs
1808     _scratch_mount
1809
1810     testdir=$SCRATCH_MNT
1811 }
1812
1813 #
1814 # Warning for UDF and NFS:
1815 # this function calls _setup_udf_scratchdir and _setup_udf_scratchdir
1816 # which actually uses the scratch dir for the test dir.
1817 #
1818 # This was done because testdir was intended to be a persistent
1819 # XFS only partition.  This should eventually change, and treat
1820 # at least local filesystems all the same.
1821 #
1822 _setup_testdir()
1823 {
1824     case $FSTYP in
1825     udf)
1826         _setup_udf_scratchdir
1827         ;;
1828     nfs*)
1829         _setup_nfs_scratchdir
1830         ;;
1831     *)
1832         testdir=$TEST_DIR
1833         ;;
1834     esac
1835 }
1836
1837 _cleanup_testdir()
1838 {
1839     case $FSTYP in
1840     udf)
1841         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1842         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1843         ;;
1844     nfs*)
1845         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1846         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1847         ;;
1848     *)
1849         # do nothing, testdir is $TEST_DIR
1850         :
1851         ;;
1852     esac
1853 }
1854
1855 _link_out_file()
1856 {
1857         if [ -z "$1" -o -z "$2" ]; then
1858                 echo Error must pass src and dst.
1859                 exit
1860         fi
1861         rm -f $2
1862         if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1863                 ln -s $1.irix $2
1864         elif [ "`uname`" == "Linux" ]; then
1865                 ln -s $1.linux $2
1866         else
1867                 echo Error test $seq does not run on the operating system: `uname`
1868                 exit
1869         fi
1870 }
1871
1872 _die()
1873 {
1874         echo $@
1875         exit 1
1876 }
1877
1878 #takes files, randomdata
1879 _nfiles()
1880 {
1881         f=0
1882         while [ $f -lt $1 ]
1883         do
1884                 file=f$f
1885                 echo > $file
1886                 if [ $size -gt 0 ]; then
1887                     if [ "$2" == "false" ]; then
1888                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
1889                     else
1890                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
1891                     fi
1892                 fi
1893                 let f=$f+1
1894         done
1895 }
1896
1897 # takes dirname, depth, randomdata
1898 _descend()
1899 {
1900         dirname=$1; depth=$2; randomdata=$3
1901         mkdir $dirname  || die "mkdir $dirname failed"
1902         cd $dirname
1903
1904         _nfiles $files $randomdata          # files for this dir and data type
1905
1906         [ $depth -eq 0 ] && return
1907         let deep=$depth-1 # go 1 down
1908
1909         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
1910
1911         d=0
1912         while [ $d -lt $dirs ]
1913         do
1914                 _descend d$d $deep &
1915                 let d=$d+1
1916                 wait
1917         done
1918 }
1919
1920 # Populate a filesystem with inodes for performance experiments
1921 #
1922 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
1923 #
1924 _populate_fs()
1925 {
1926     here=`pwd`
1927     dirs=5          # ndirs in each subdir till leaves
1928     size=0          # sizeof files in K
1929     files=100       # num files in _each_ subdir
1930     depth=2         # depth of tree from root to leaves
1931     verbose=false
1932     root=root       # path of initial root of directory tree
1933     randomdata=false # -x data type urandom or zero
1934
1935     OPTIND=1
1936     while getopts "d:f:n:r:s:v:x" c
1937     do
1938         case $c in
1939         d)      depth=$OPTARG;;
1940         n)      dirs=$OPTARG;;
1941         f)      files=$OPTARG;;
1942         s)      size=$OPTARG;;
1943         v)      verbose=true;;
1944         r)      root=$OPTARG;;
1945         x)      randomdata=true;;
1946         esac
1947     done
1948
1949     _descend $root $depth $randomdata
1950     wait
1951
1952     cd $here
1953
1954     [ $verbose = true ] && echo done
1955 }
1956
1957 # query whether the given file has the given inode flag set
1958 #
1959 _test_inode_flag()
1960 {
1961     flag=$1
1962     file=$2
1963
1964     if which $XFS_IO_PROG >/dev/null; then
1965         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
1966             return 0
1967         fi
1968     fi
1969     return 1
1970 }
1971
1972 # query the given files extsize allocator hint in bytes (if any)
1973 #
1974 _test_inode_extsz()
1975 {
1976     file=$1
1977     blocks=""
1978
1979     if which $XFS_IO_PROG >/dev/null; then
1980         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
1981                 awk '/^xattr.extsize =/ { print $3 }'`
1982     fi
1983     [ -z "$blocks" ] && blocks="0"
1984     echo $blocks
1985 }
1986
1987 # scratch_dev_pool should contain the disks pool for the btrfs raid
1988 _require_scratch_dev_pool()
1989 {
1990         local i
1991         if [ -z "$SCRATCH_DEV_POOL" ]; then
1992                 _notrun "this test requires a valid \$SCRATCH_DEV_POOL"
1993         fi
1994
1995         # btrfs test case needs 2 or more scratch_dev_pool; other FS not sure
1996         # so fail it
1997         case $FSTYP in
1998         btrfs)
1999                 if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt 2 ]; then
2000                         _notrun "btrfs and this test needs 2 or more disks in SCRATCH_DEV_POOL"
2001                 fi
2002         ;;
2003         *)
2004                 _notrun "dev_pool is not supported by fstype \"$FSTYP\""
2005         ;;
2006         esac
2007
2008         for i in $SCRATCH_DEV_POOL; do
2009                 if [ "`_is_block_dev $i`" = "" ]; then
2010                         _notrun "this test requires valid block disk $i"
2011                 fi
2012                 if [ "`_is_block_dev $i`" = "`_is_block_dev $TEST_DEV`" ]; then
2013                         _notrun "$i is part of TEST_DEV, this test requires unique disks"
2014                 fi
2015                 if _mount | grep -q $i; then
2016                         if ! $UMOUNT_PROG $i; then
2017                             echo "failed to unmount $i - aborting"
2018                             exit 1
2019                         fi
2020                 fi
2021                 # to help better debug when something fails, we remove
2022                 # traces of previous btrfs FS on the dev.
2023                 dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
2024         done
2025 }
2026
2027 # We will check if the device is virtual (eg: loop device) since it does not
2028 # have the delete entry-point. Otherwise SCSI and USB devices are fine. 
2029 _require_deletable_scratch_dev_pool()
2030 {
2031         local i
2032         local x
2033         for i in $SCRATCH_DEV_POOL; do
2034                 x=`echo $i | cut -d"/" -f 3`
2035                 ls -l /sys/class/block/${x} | grep -q "virtual" 
2036                 if [ $? == "0" ]; then
2037                         _notrun "$i is a virtual device which is not deletable"
2038                 fi
2039         done
2040 }
2041
2042 # We check for btrfs and (optionally) features of the btrfs command
2043 _require_btrfs()
2044 {
2045         cmd=$1
2046         _require_command $BTRFS_UTIL_PROG btrfs
2047         if [ -z "$1" ]; then
2048                 return 1;
2049         fi
2050         $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
2051         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
2052 }
2053
2054 # Check that fio is present, and it is able to execute given jobfile
2055 _require_fio()
2056 {
2057         job=$1
2058
2059         _require_command $FIO_PROG
2060         if [ -z "$1" ]; then
2061                 return 1;
2062         fi
2063
2064         $FIO_PROG --warnings-fatal --showcmd $job >> $seqres.full 2>&1
2065         [ $? -eq 0 ] || _notrun "$FIO_PROG too old, see $seqres.full"
2066 }
2067
2068 # Does freeze work on this fs?
2069 _require_freeze()
2070 {
2071         xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
2072         result=$? 
2073         xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
2074         [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
2075 }
2076
2077 # arg 1 is dev to remove and is output of the below eg.
2078 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2079 _devmgt_remove()
2080 {
2081         echo 1 > /sys/class/scsi_device/${1}/device/delete || _fail "Remove disk failed"
2082 }
2083
2084 # arg 1 is dev to add and is output of the below eg.
2085 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2086 _devmgt_add()
2087 {
2088         local h
2089         local tdl
2090         # arg 1 will be in h:t:d:l format now in the h and "t d l" format
2091         h=`echo ${1} | cut -d":" -f 1`
2092         tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`
2093
2094         echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"
2095 }
2096
2097 _require_fstrim()
2098 {
2099         if [ -z "$FSTRIM_PROG" ]; then
2100                 _notrun "This test requires fstrim utility."
2101         fi
2102 }
2103
2104 _test_batched_discard()
2105 {
2106         if [ $# -ne 1 ]; then
2107                 echo "Usage: _test_batched_discard mnt_point" 1>&2
2108                 exit 1
2109         fi
2110         _require_fstrim
2111         $FSTRIM_PROG ${1} &>/dev/null
2112 }
2113
2114 _require_dumpe2fs()
2115 {
2116         if [ -z "$DUMPE2FS_PROG" ]; then
2117                 _notrun "This test requires dumpe2fs utility."
2118         fi
2119 }
2120
2121 _create_loop_device()
2122 {
2123         file=$1
2124         dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"
2125         echo $dev
2126 }
2127
2128 _destroy_loop_device()
2129 {
2130         dev=$1
2131         losetup -d $dev || _fail "Cannot destroy loop device $dev"
2132 }
2133
2134 _scale_fsstress_args()
2135 {
2136     args=""
2137     while [ $# -gt 0 ]; do
2138         case "$1" in
2139             -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
2140             -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
2141             *) args="$args $1" ;;
2142         esac
2143         shift
2144     done
2145     echo $args
2146 }
2147
2148 run_check()
2149 {
2150         echo "# $@" >> $seqres.full 2>&1
2151         "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
2152 }
2153
2154 init_rc()
2155 {
2156         if [ "$iam" == new ]
2157         then
2158                 return
2159         fi
2160         # make some further configuration checks here
2161         if [ "$TEST_DEV" = ""  ]
2162         then
2163                 echo "common/rc: Error: \$TEST_DEV is not set"
2164                 exit 1
2165         fi
2166
2167         # if $TEST_DEV is not mounted, mount it now as XFS
2168         if [ -z "`_fs_type $TEST_DEV`" ]
2169         then
2170                 # $TEST_DEV is not mounted
2171                 if ! _test_mount
2172                 then
2173                         echo "common/rc: retrying test device mount with external set"
2174                         [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
2175                         if ! _test_mount
2176                         then
2177                                 echo "common/rc: could not mount $TEST_DEV on $TEST_DIR"
2178                                 exit 1
2179                         fi
2180                 fi
2181         fi
2182
2183         if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
2184         then
2185                 echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
2186                 $DF_PROG $TEST_DEV
2187                 exit 1
2188         fi
2189         # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
2190         xfs_io -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
2191         export XFS_IO_PROG="$XFS_IO_PROG -F"
2192 }
2193
2194 init_rc
2195
2196 ################################################################################
2197 # make sure this script returns success
2198 /bin/true