Cleanup mkfs on scratch device for arbitrary filesystems.
[xfstests-dev.git] / common.rc
1 ##/bin/sh
2 #
3 # Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
4
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of version 2 of the GNU General Public License as
7 # published by the Free Software Foundation.
8
9 # This program is distributed in the hope that it would be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 # Further, this software is distributed without any warranty that it is
14 # free of the rightful claim of any third person regarding infringement
15 # or the like.  Any license provided herein, whether implied or
16 # otherwise, applies only to this software file.  Patent licenses, if
17 # any, provided herein do not apply to combinations of this program with
18 # other software, or any other product whatsoever.
19
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write the Free Software Foundation, Inc., 59
22 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
23
24 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 # Mountain View, CA  94043, or:
26
27 # http://www.sgi.com 
28
29 # For further information regarding this notice, see: 
30
31 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32 #
33
34
35 _mount_opts()
36 {
37     case $FSTYP in
38     xfs)
39         export MOUNT_OPTIONS=$XFS_MOUNT_OPTIONS
40         ;;
41     udf)
42         export MOUNT_OPTIONS=$UDF_MOUNT_OPTIONS
43         ;;
44     nfs)
45         export MOUNT_OPTIONS=$NFS_MOUNT_OPTIONS
46         ;;
47     *)
48         ;;
49     esac
50 }
51
52 _mkfs_opts()
53 {
54     case $FSTYP in
55     xfs)
56         export MKFS_OPTIONS=$XFS_MKFS_OPTIONS
57         ;;
58     udf)
59         [ ! -z "$udf_fsize" ] && \
60             UDF_MKFS_OPTIONS="$UDF_MKFS_OPTIONS -s $udf_fsize"
61         export MKFS_OPTIONS=$UDF_MKFS_OPTIONS
62         ;;
63     nfs)
64         export MKFS_OPTIONS=$NFS_MKFS_OPTIONS
65         ;;
66     *)
67         ;;
68     esac
69 }
70
71 [ -z "$FSTYP" ] && FSTYP=xfs
72 [ -z "$MOUNT_OPTIONS" ] && _mount_opts
73 [ -z "$MKFS_OPTIONS" ] && _mkfs_opts
74
75
76 # we need common.config
77 if ! . ./common.config
78 then
79     echo "$iam: failed to source common.config"
80     exit 1
81 fi
82
83 # make sure we have a standard umask
84 umask 022
85
86 _mount()
87 {
88     $MOUNT_PROG $*
89 }
90
91 _scratch_options()
92 {
93     type=$1
94     SCRATCH_OPTIONS=""
95
96     if [ $FSTYP != "xfs" ]; then
97         return
98     fi
99
100     case $type in
101     mkfs)
102         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
103         rt_opt="-r"
104         log_opt="-l"
105         ;;
106     mount)
107         rt_opt="-o"
108         log_opt="-o"
109         ;;
110     esac
111     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
112         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
113     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
114         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
115 }
116
117 _test_options()
118 {
119     type=$1
120     TEST_OPTIONS=""
121
122     if [ $FSTYP != "xfs" ]; then
123         return
124     fi
125
126     case $type in
127     mkfs)
128         rt_opt="-r"
129         log_opt="-l"
130         ;;
131     mount)
132         rt_opt="-o"
133         log_opt="-o"
134         ;;
135     esac
136     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
137         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
138     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
139         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
140 }
141
142 _scratch_mount_options()
143 {
144     _scratch_options mount
145     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
146 }
147
148 _scratch_mount()
149 {
150     _mount -t $FSTYP `_scratch_mount_options $*`
151 }
152
153 _test_mount()
154 {
155     _test_options mount 
156     _mount -t $FSTYP $TEST_OPTIONS $MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
157 }
158
159 _scratch_mkfs_options()
160 {
161     _scratch_options mkfs 
162     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
163 }
164
165 _scratch_mkfs_xfs()
166 {
167     _scratch_options mkfs 
168
169     $MKFS_XFS_PROG $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
170     mkfs_status=$?
171
172     if [ "$USE_BIG_LOOPFS" = yes ]; then
173         [ -z "$RETAIN_AG_BYTES" ] && RETAIN_AG_BYTES=0
174         ./tools/ag-wipe -q -r $RETAIN_AG_BYTES $SCRATCH_DEV
175     fi
176
177     return $mkfs_status
178 }
179
180 _scratch_mkfs()
181 {
182     case $FSTYP in
183     xfs)
184         _scratch_mkfs_xfs $*
185         ;;
186     nfs*)
187         # do nothing for nfs
188         ;;
189     udf*)
190         $MKFS_PROG -t $FSTYP $MKFS_OPTIONS $* > /dev/null
191         ;;
192     *)
193         /sbin/mkfs -t $FSTYP $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
194         ;;
195     esac
196 }
197
198 _scratch_xfs_db_options()
199 {
200     SCRATCH_OPTIONS=""
201     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
202         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
203     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
204 }
205
206 _scratch_xfs_logprint()
207 {
208     SCRATCH_OPTIONS=""
209     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
210         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
211     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
212 }
213
214 _scratch_xfs_repair()
215 {
216     SCRATCH_OPTIONS=""
217     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
218         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
219     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
220         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
221     [ "$USE_BIG_LOOPFS" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
222     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
223 }
224
225 _get_pids_by_name()
226 {
227     if [ $# -ne 1 ]
228     then
229         echo "Usage: _get_pids_by_name process-name" 1>&2
230         exit 1
231     fi
232
233     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
234     # HH:MM:SS before the psargs field, use this as the search anchor.
235     #
236     # Matches with $1 (process-name) occur if the first psarg is $1
237     # or ends in /$1 ... the matching uses sed's regular expressions,
238     # so passing a regex into $1 will work.
239
240     ps $PS_ALL_FLAGS \
241     | sed -n \
242         -e 's/$/ /' \
243         -e 's/[         ][      ]*/ /g' \
244         -e 's/^ //' \
245         -e 's/^[^ ]* //' \
246         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
247         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
248 }
249
250 # fqdn for localhost
251 #
252 _get_fqdn()
253 {
254     host=`hostname`
255     $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
256 }
257
258 # fix malloc libs output
259 #
260 _fix_malloc()
261 {
262     # filter out the Electric Fence notice
263     $PERL_PROG -e '
264         while (<>) {
265             if (defined $o && /^\s+Electric Fence/) {
266                 chomp($o);
267                 print "$o";
268                 undef $o;
269                 next;
270             }
271             print $o if (defined $o);
272
273             $o=$_;
274         }
275         print $o if (defined $o);
276     '
277 }
278
279 # check if run as root
280 #
281 _need_to_be_root()
282 {
283     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
284     if [ "$id" -ne 0 ]
285     then
286         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
287         exit 1
288     fi
289 }
290
291
292 #
293 # _df_device : get an IRIX style df line for a given device 
294 #
295 #       - returns "" if not mounted
296 #       - returns fs type in field two (ala IRIX)
297 #       - joins line together if split by fancy df formatting
298 #       - strips header etc
299 #
300
301 _df_device()
302 {
303     if [ $# -ne 1 ]
304     then
305         echo "Usage: _df_device device" 1>&2
306         exit 1
307     fi
308     
309     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
310         match($1,what) && NF==1 { 
311             v=$1
312             getline
313             print v, $0
314             exit
315         }
316         match($1,what) {
317             print
318             exit
319         }
320     '
321 }
322
323 #
324 # _df_dir : get an IRIX style df line for device where a directory resides
325 #
326 #       - returns fs type in field two (ala IRIX)
327 #       - joins line together if split by fancy df formatting
328 #       - strips header etc
329 #
330
331 _df_dir()
332 {
333     if [ $# -ne 1 ]
334     then
335         echo "Usage: _df_dir device" 1>&2
336         exit 1
337     fi
338     
339     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
340         NR == 2 && NF==1 { 
341             v=$1
342             getline 
343             print v, $0;
344             exit 0
345         }
346         NR == 2 {
347             print;
348             exit 0
349         }
350         {}
351     '
352     # otherwise, nada
353 }
354
355 # return percentage used disk space for mounted device
356
357 _used()
358 {
359     if [ $# -ne 1 ]
360     then
361         echo "Usage: _used device" 1>&2
362         exit 1
363     fi
364     
365     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
366 }
367
368 # return the FS type of a mounted device
369 #
370 _fs_type()
371 {
372     if [ $# -ne 1 ]
373     then
374         echo "Usage: _fs_type device" 1>&2
375         exit 1
376     fi
377     
378     _df_device $1 | $AWK_PROG '{ print $2 }'
379 }
380
381 # return the FS mount options of a mounted device
382 #
383 # should write a version which just parses the output of mount for IRIX
384 # compatibility, but since this isn't used at all, at the moment I'll leave
385 # this for now
386 #
387 _fs_options()
388 {
389     if [ $# -ne 1 ]
390     then
391         echo "Usage: _fs_options device" 1>&2
392         exit 1
393     fi
394     
395     $AWK_PROG -v dev=$1 '
396         match($1,dev) { print $4 }
397     ' </proc/mounts
398     
399 }
400
401 # returns device number if a file is a block device
402 #
403 _is_block_dev()
404 {
405     if [ $# -ne 1 ]
406     then
407         echo "Usage: _is_block_dev dev" 1>&2
408         exit 1
409     fi
410     
411     [ -b $1 ] && src/lstat64 $1 | $AWK_PROG '/Device type:/ { print $9 }'
412 }
413
414 # Do a command, log it to $seq.full, optionally test return status
415 # and die if command fails. If called with one argument _do executes the
416 # command, logs it, and returns its exit status. With two arguments _do
417 # first prints the message passed in the first argument, and then "done"
418 # or "fail" depending on the return status of the command passed in the
419 # second argument. If the command fails and the variable _do_die_on_error
420 # is set to "always" or the two argument form is used and _do_die_on_error
421 # is set to "message_only" _do will print an error message to
422 # $seq.out and exit.
423
424 _do()
425 {
426     if [ $# -eq 1 ]; then
427         _cmd=$1
428     elif [ $# -eq 2 ]; then
429         _note=$1
430         _cmd=$2
431         echo -n "$_note... "
432     else
433         echo "Usage: _do [note] cmd" 1>&2
434         status=1; exit
435     fi
436
437     (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
438     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
439     cat $tmp._out | _fix_malloc >>$here/$seq.full
440     if [ $# -eq 2 ]; then
441         if [ $ret -eq 0 ]; then
442             echo "done"
443         else
444             echo "fail"
445         fi
446     fi
447     if [ $ret -ne 0  ] \
448         && [ "$_do_die_on_error" = "always" \
449             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
450     then
451         [ $# -ne 2 ] && echo
452         eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
453         status=1; exit
454     fi
455
456     return $ret
457 }
458
459 # bail out, setting up .notrun file
460
461 _notrun()
462 {
463     echo "$*" >$seq.notrun
464     echo "$seq not run: $*"
465     status=0
466     exit
467 }
468
469 # just plain bail out
470
471 _fail()
472 {
473     echo "$*" | tee -a $here/$seq.full
474     echo "(see $seq.full for details)"
475     status=1
476     exit 1
477 }
478
479 # tests whether $FSTYP is one of the supported filesystems for a test
480 #
481 _supported_fs()
482 {
483     for f
484     do
485         if [ "$f" = "$FSTYP" ]
486         then
487             return
488         fi
489     done
490
491     _notrun "not suitable for this filesystem type: $FSTYP"
492 }
493
494 # tests whether $FSTYP is one of the supported OSes for a test
495 #
496 _supported_os()
497 {
498     for h
499     do
500         if [ "$h" = "$HOSTOS" ]
501         then
502             return
503         fi
504     done
505
506     _notrun "not suitable for this OS: $HOSTOS"
507 }
508
509 # this test needs a scratch partition - check we're ok & unmount it
510
511 _require_scratch()
512 {
513     case "$FSTYP" in
514         xfs|udf)
515                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
516                  then
517                      _notrun "this test requires a valid \$SCRATCH_DEV"
518                  fi
519                  
520                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
521                  then
522                      _notrun "this test requires a valid \$SCRATCH_DEV"
523                  fi
524                  ;;
525         nfs*|ext2|ext3|reiserfs)
526                  echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
527                  if [ ! -z "$SCRATCH_DEV" -a ! -b "$SCRATCH_DEV" -a "$?" != "0" ]
528                  then
529                      _notrun "this test requires a valid \$SCRATCH_DEV"
530                  fi
531                  ;;
532         *)
533                  _notrun "\$FSTYP ($FSTYP) unknown or not specified"
534                  ;;
535     esac
536
537     
538     # mounted?
539     if _mount | grep -q $SCRATCH_DEV
540     then 
541         # if it's mounted, make sure its on $SCRATCH_MNT
542         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
543         then 
544             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
545             exit 1
546         fi
547         # and then unmount it
548         if ! $UMOUNT_PROG $SCRATCH_DEV
549         then
550             echo "failed to unmount $SCRATCH_DEV"
551             exit 1
552         fi
553     fi
554 }
555
556 # this test needs a logdev 
557
558 _require_logdev()
559 {
560     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
561         _notrun "This test requires a valid \$SCRATCH_LOGDEV" 
562     [ "$USE_EXTERNAL" != yes ] && \
563         _notrun "This test requires USE_EXTERNAL to be enabled"
564
565     # ensure its not mounted
566     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
567 }
568
569 # this test requires loopback device support
570
571 _require_loop()
572 {
573     if [ "$HOSTOS" != "Linux" ]
574     then
575         _notrun "This test requires linux for loopback device support"
576     fi
577
578     modprobe loop >/dev/null 2>&1
579     if grep loop /proc/devices >/dev/null 2>&1
580     then
581         :
582     else
583         _notrun "This test requires loopback device support"
584     fi
585 }
586
587 # this test requires that (large) loopback device files are not in use
588
589 _require_nobigloopfs()
590 {
591     [ "$USE_BIG_LOOPFS" = yes ] && \
592         _notrun "Large filesystem testing in progress, skipped this test"
593 }
594
595 # this test requires that external log/realtime devices are not in use
596
597 _require_nonexternal()
598 {
599     [ "$USE_EXTERNAL" = yes ] && \
600         _notrun "External device testing in progress, skipped this test"
601 }
602
603 # check that a FS is mounted as XFS. if so, return mount point
604 #
605 _xfs_mounted()
606 {
607     if [ $# -ne 1 ]
608     then
609         echo "Usage: _xfs_mounted device" 1>&2
610         exit 1
611     fi
612     
613     device=$1
614     
615     if _mount | grep $device | $AWK_PROG '
616         /type xfs/  { print $3 ; exit 0 }
617         END         { exit 1 }
618     '
619     then
620         echo "_xfs_mounted: $device is not a mounted XFS FS"
621         exit 1
622     fi
623 }
624
625
626 # remount a FS to a new mode (ro or rw)
627 #
628 _remount()
629 {
630     if [ $# -ne 2 ]
631     then
632         echo "Usage: _remount device ro/rw" 1>&2
633         exit 1
634     fi
635     device=$1
636     mode=$2
637     
638     if ! mount -o remount,$mode $device
639     then
640         echo "_remount: failed to remount filesystem on $device as $mode"
641         exit 1
642     fi
643 }
644
645 # run xfs_check and friends on a FS. 
646 #
647 # if the filesystem is mounted, it's either remounted ro before being
648 # checked or it's unmounted and then remounted
649 #
650
651 USE_REMOUNT=0
652
653 _check_xfs_filesystem()
654 {
655     if [ $# -ne 3 ]
656     then
657         echo "Usage: _check_fs device <logdev>|none <rtdev>|none" 1>&2
658         exit 1
659     fi
660
661     device=$1
662     if [ "$2" != "none" ]; then
663         extra_log_options="-l$2"
664         extra_mount_options="-ologdev=$2"
665     fi
666
667     if [ "$3" != "none" ]; then
668         extra_rt_options="-r$3"
669         extra_mount_options=$extra_mount_options" -ortdev=$3"
670     fi
671
672     [ "$FSTYP" != xfs ] && return 0
673     testoption=""
674     [ "$USE_BIG_LOOPFS" = yes ] && testoption=-t
675
676     type=`_fs_type $device`
677     ok=1
678     
679     if [ "$type" = "xfs" ]
680     then
681         # mounted... 
682         
683         if [ $USE_REMOUNT -eq 0 ]
684         then
685             mountpoint=`_xfs_mounted $device`
686             $UMOUNT_PROG $device
687         else
688             _remount $device ro
689         fi
690     fi
691
692     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
693                 | tee $tmp.logprint | grep -q "<CLEAN>"
694     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
695     then
696         echo "_check_fs: filesystem on $device has dirty log (see $seq.full)"
697         
698         echo "_check_fs: filesystem on $device has dirty log"   >>$here/$seq.full
699         echo "*** xfs_logprint -t output ***"                   >>$here/$seq.full
700         cat $tmp.logprint                                       >>$here/$seq.full
701         echo "*** end xfs_logprint output"                      >>$here/$seq.full
702         
703         ok=0
704     fi
705
706     $XFS_CHECK_PROG $testoption $extra_log_options $device 2>&1 |\
707          _fix_malloc >$tmp.fs_check 
708     if [ -s $tmp.fs_check ]
709     then
710         echo "_check_fs: filesystem on $device is inconsistent (c) (see $seq.full)"
711         
712         echo "_check_fs: filesystem on $device is inconsistent" >>$here/$seq.full
713         echo "*** xfs_check output ***"                         >>$here/$seq.full
714         cat $tmp.fs_check                                       >>$here/$seq.full
715         echo "*** end xfs_check output"                         >>$here/$seq.full
716         
717         ok=0
718     fi
719     # repair doesn't scale massively at this stage, optionally skip it for now
720     [ "$USE_BIG_LOOPFS" = yes ] || \
721     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
722     if [ $? -ne 0 ]
723     then
724         echo "_check_fs: filesystem on $device is inconsistent (r) (see $seq.full)"
725         
726         echo "_check_fs: filesystem on $device is inconsistent" >>$here/$seq.full
727         echo "*** xfs_repair -n output ***"                     >>$here/$seq.full
728         cat $tmp.repair | _fix_malloc                           >>$here/$seq.full
729         echo "*** end xfs_repair output"                        >>$here/$seq.full
730         
731         ok=0
732     fi
733     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
734  
735     if [ $ok -eq 0 ]
736     then
737         echo "*** mount output ***"                             >>$here/$seq.full
738         _mount                                                  >>$here/$seq.full
739         echo "*** end mount output"                             >>$here/$seq.full
740     elif [ "$type" = "xfs" ]
741     then
742         # mounted... 
743         if [ $USE_REMOUNT -eq 0 ]
744         then
745             if ! _mount -t xfs $extra_mount_options $device $mountpoint
746             then
747                 echo "!!! failed to remount $device on $mountpoint"
748                 ok=0
749             fi
750         else
751             _remount $device rw
752         fi
753     fi
754     
755     [ $ok -eq 0 ] && exit 1
756     return 0    
757 }
758
759 _check_udf_filesystem()
760 {
761     if [ $# -ne 1 -a $# -ne 2 ]
762     then
763         echo "Usage: _check_fs device [last_block]" 1>&2
764         exit 1
765     fi
766
767     device=$1
768     if [ $# -eq 2 ];
769     then
770         LAST_BLOCK=`expr \( $2 - 1 \)`
771         OPT_ARG="-lastvalidblock $LAST_BLOCK"
772     fi
773
774     # Output messages format:
775     #       Error messages contain :   "Error:" or   "error:"
776     #     Warning messages contain : "Warning:" or "warning:"
777     #   Attention messages contain :    "Note:" or    "note:"
778     # Message continuation lines start with a "-" character.
779
780     rm -f $seq.checkfs
781     sleep 1 # Due to a problem with time stamps in udf_test
782     $here/src/udf_test $OPT_ARG $device | tee $here/$seq.checkfs | \
783         egrep -i "error:|warning:|Error count:|Warning count:" | \
784         egrep -v "Error count:   0|Warning count:   0"
785 }
786
787 _check_test_fs()
788 {
789     TEST_LOG="none"
790     TEST_RT="none"
791     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
792         TEST_LOG="$TEST_LOGDEV"
793
794     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
795         TEST_RT="$TEST_RTDEV"
796
797     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
798 }
799
800 _check_scratch_fs()
801 {
802     case $FSTYP in
803     xfs)
804         SCRATCH_LOG="none"
805         SCRATCH_RT="none"
806         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
807             SCRATCH_LOG="$SCRATCH_LOGDEV"
808
809         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
810             SCRATCH_RT="$SCRATCH_RTDEV"
811
812         _check_xfs_filesystem $SCRATCH_DEV $SCRATCH_LOG $SCRATCH_RT
813         ;;
814     udf)
815         _check_udf_filesystem $SCRATCH_DEV $udf_fsize
816         ;;
817     nfs*)
818         # Don't know how to check an NFS filesystem, yet.
819         ;;
820     *)
821         ;;
822     esac
823 }
824
825 _full_fstyp_details()
826 {
827      [ -z "$FSTYP" ] && FSTYP=xfs
828      if [ $FSTYP = xfs ]; then
829         if grep 'debug 0' /proc/fs/xfs/stat >/dev/null; then
830             FSTYP="$FSTYP (non-debug)"
831         elif grep 'debug 1' /proc/fs/xfs/stat >/dev/null; then
832             FSTYP="$FSTYP (debug)"
833         fi
834      fi
835      echo $FSTYP
836 }
837
838 _full_platform_details()
839 {
840      os=`uname -s`
841      host=`hostname -s`
842      kernel=`uname -r`
843      platform=`uname -m`
844      echo "$os/$platform $host $kernel"
845 }
846
847 _check_testdir()
848 {
849     case $FSTYP in
850     xfs)
851         _check_test_fs
852         ;;
853     udf)
854         _cleanup_testdir
855         _check_scratch_fs
856         _scratch_mount
857         ;;
858     nfs*)
859         # Don't know how to check an NFS filesystem, yet.
860         ;;
861     *)
862         ;;
863     esac
864 }
865
866
867 _setup_xfs_testdir()
868 {
869     [ "$FSTYP" != "xfs" ] \
870         && _fail "setup_xfs_testdir: \$FSTYP ($FSTYP) is not xfs"
871     
872     testdir=$TEST_DIR
873 }
874
875 _setup_udf_testdir()
876 {
877     [ "$FSTYP" != "udf" ] \
878         && _fail "setup_udf_testdir: \$FSTYP is not udf"
879     [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
880         && _notrun "this test requires a valid \$SCRATCH_DEV"
881     [ -z "$SCRATCH_MNT" ] \
882         && _notrun "this test requires a valid \$SCRATCH_MNT"
883     
884     # mounted?
885     if _mount | grep -q $SCRATCH_DEV
886     then 
887         # if it's mounted, make sure its on $TEST_RW_DIR
888         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
889         then 
890             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
891         fi
892         $UMOUNT_PROG $SCRATCH_DEV
893     fi 
894
895     _scratch_mkfs
896     _scratch_mount
897
898     testdir=$SCRATCH_MNT
899 }
900
901 _setup_nfs_testdir()
902 {
903     [ "$FSTYP" != "nfs" ] \
904         && _fail "setup_udf_testdir: \$FSTYP is not nfs"
905     [ -z "$SCRATCH_DEV" ] \
906         && _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
907     [ -z "$SCRATCH_MNT" ] \
908         && _notrun "this test requires a valid \$SCRATCH_MNT"
909     
910     # mounted?
911     if _mount | grep -q $SCRATCH_DEV
912     then 
913         # if it's mounted, make sure its on $TEST_RW_DIR
914         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
915         then 
916             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
917         fi
918         $UMOUNT_PROG $SCRATCH_DEV
919     fi 
920
921     _scratch_mkfs
922     _scratch_mount
923
924     testdir=$SCRATCH_MNT
925 }
926
927 _setup_testdir()
928 {
929     case $FSTYP in
930     xfs)
931         _setup_xfs_testdir
932         ;;
933     udf)
934         _setup_udf_testdir
935         ;;
936     nfs*)
937         _setup_nfs_testdir
938         ;;
939     *)
940         _fail "\$FSTYP is not xfs, udf or nfs"
941         ;;
942     esac    
943 }
944
945 _cleanup_testdir()
946 {
947     case $FSTYP in
948     xfs)
949         # do nothing, testdir is $TEST_DIR
950         ;;
951     udf)
952         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
953         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
954         ;;
955     nfs*)
956         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
957         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
958         ;;
959     *)
960         _fail "\$FSTYP is not xfs, udf or nfs"
961         ;;
962     esac    
963 }
964
965 ################################################################################
966
967 if [ "$iam" != new -a "$iam" != bench ]
968 then
969     # make some further configuration checks here
970
971     if [ "$TEST_DEV" = ""  ]
972     then
973         echo "common.rc: Error: \$TEST_DEV is not set"
974         exit 1
975     fi
976     
977     # if $TEST_DEV is not mounted, mount it now as XFS
978     if [ -z "`_fs_type $TEST_DEV`" ]
979     then
980         # $TEST_DEV is not mounted
981         if ! _test_mount
982         then
983             echo "common.rc: retrying test device mount with external set"
984             [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
985             if ! _test_mount
986             then
987                 echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
988                 exit 1
989             fi
990         fi
991     fi
992     
993     if [ "`_fs_type $TEST_DEV`" != "xfs" ]
994     then
995         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED XFS filesystem"
996         $DF_PROG $TEST_DEV
997         exit 1
998     fi
999
1000 fi
1001
1002 _link_out_file()
1003 {
1004    if [ -z "$1" ]; then
1005       echo Error must pass \$seq.
1006       exit      
1007    fi
1008    rm -f $1
1009    if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1010       ln -s $1.irix $1
1011    elif [ "`uname`" == "Linux" ]; then
1012       ln -s $1.linux $1
1013    else
1014       echo Error test $seq does not run on the operating system: `uname`
1015       exit
1016    fi
1017 }
1018
1019 # make sure this script returns success
1020 /bin/true