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