fix common.config to allow SCRATCH_DEV and SCRATCH_MNT to be optional
[xfstests-dev.git] / common.rc
1 ##/bin/sh
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 dd()
24 {
25    if [ "$HOSTOS" == "Linux" ]
26    then 
27         command dd --help | grep noxfer > /dev/null 2>&1
28         
29         if [ "$?" -eq 0 ]
30             then
31                 command dd status=noxfer $@
32             else
33                 command dd $@
34         fi
35    else
36         command dd $@
37    fi
38 }
39
40 _mount_opts()
41 {
42     case $FSTYP in
43     xfs)
44         export MOUNT_OPTIONS=$XFS_MOUNT_OPTIONS
45         ;;
46     udf)
47         export MOUNT_OPTIONS=$UDF_MOUNT_OPTIONS
48         ;;
49     nfs)
50         export MOUNT_OPTIONS=$NFS_MOUNT_OPTIONS
51         ;;
52     ext2|ext3|ext4)
53         # acls & xattrs aren't turned on by default on ext$FOO
54         export MOUNT_OPTIONS="-o acl,user_xattr $EXT_MOUNT_OPTIONS"
55         ;;
56     reiserfs)
57         # acls & xattrs aren't turned on by default on reiserfs
58         export MOUNT_OPTIONS="-o acl,user_xattr $REISERFS_MOUNT_OPTIONS"
59         ;;
60     gfs2)
61         # acls aren't turned on by default on gfs2
62         export MOUNT_OPTIONS="-o acl $GFS2_MOUNT_OPTIONS"
63         ;;
64     *)
65         ;;
66     esac
67 }
68
69 _mkfs_opts()
70 {
71     case $FSTYP in
72     xfs)
73         export MKFS_OPTIONS=$XFS_MKFS_OPTIONS
74         ;;
75     udf)
76         [ ! -z "$udf_fsize" ] && \
77             UDF_MKFS_OPTIONS="$UDF_MKFS_OPTIONS -s $udf_fsize"
78         export MKFS_OPTIONS=$UDF_MKFS_OPTIONS
79         ;;
80     nfs)
81         export MKFS_OPTIONS=$NFS_MKFS_OPTIONS
82         ;;
83     reiserfs)
84         export MKFS_OPTIONS="$REISERFS_MKFS_OPTIONS -q"
85         ;;
86     gfs2)
87         export MKFS_OPTIONS="$GFS2_MKFS_OPTIONS -O -p lock_nolock"
88         ;;
89     *)
90         ;;
91     esac
92 }
93
94 [ -z "$FSTYP" ] && FSTYP=xfs
95 [ -z "$MOUNT_OPTIONS" ] && _mount_opts
96 [ -z "$MKFS_OPTIONS" ] && _mkfs_opts
97
98
99 # we need common.config
100 if [ "$iam" != "check" ]
101 then
102     if ! . ./common.config
103         then
104         echo "$iam: failed to source common.config"
105         exit 1
106     fi
107 fi
108
109 # make sure we have a standard umask
110 umask 022
111
112 _mount()
113 {
114     $MOUNT_PROG `_mount_ops_filter $*`
115 }
116
117 _scratch_options()
118 {
119     type=$1
120     SCRATCH_OPTIONS=""
121
122     if [ "$FSTYP" != "xfs" ]; then
123         return
124     fi
125
126     case $type in
127     mkfs)
128         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
129         rt_opt="-r"
130         log_opt="-l"
131         ;;
132     mount)
133         rt_opt="-o"
134         log_opt="-o"
135         ;;
136     esac
137     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
138         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
139     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
140         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
141 }
142
143 _test_options()
144 {
145     type=$1
146     TEST_OPTIONS=""
147
148     if [ "$FSTYP" != "xfs" ]; then
149         return
150     fi
151
152     case $type in
153     mkfs)
154         rt_opt="-r"
155         log_opt="-l"
156         ;;
157     mount)
158         rt_opt="-o"
159         log_opt="-o"
160         ;;
161     esac
162     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
163         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
164     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
165         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
166 }
167
168 _mount_ops_filter()
169 {
170     params="$*"
171     
172     #get mount point to handle dmapi mtpt option correctly
173     let last_index=$#-1
174     [ $last_index -gt 0 ] && shift $last_index
175     FS_ESCAPED=$1
176     
177     # irix is fussy about how it is fed its mount options
178     # - multiple -o's are not allowed
179     # - no spaces between comma delimitered options
180     # the sed script replaces all -o's (except the first) with a comma
181     # not required for linux, but won't hurt
182     
183     echo $params | sed -e 's/[[:space:]]*-o[[:space:]]*/UnIqUe/1; s/[[:space:]]*-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
184         | sed -e 's/dmapi/dmi/' \
185         | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"
186
187 }
188
189 _scratch_mount_options()
190 {
191     _scratch_options mount
192
193     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
194 }
195
196 _scratch_mount()
197 {
198     _mount -t $FSTYP `_scratch_mount_options $*`
199 }
200
201 _test_mount()
202 {
203     _test_options mount
204     _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $* $TEST_DEV $TEST_DIR
205 }
206
207 _scratch_mkfs_options()
208 {
209     _scratch_options mkfs
210     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
211 }
212
213 _scratch_mkfs_xfs()
214 {
215     # extra mkfs options can be added by tests
216     local extra_mkfs_options=$*
217
218     local tmp_dir=/tmp/
219
220     _scratch_options mkfs
221
222     # save mkfs output in case conflict means we need to run again.
223     # only the output for the mkfs that applies should be shown
224     $MKFS_XFS_PROG $SCRATCH_OPTIONS $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
225         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
226     local mkfs_status=$?
227
228     # a mkfs failure may be caused by conflicts between
229     # $MKFS_OPTIONS and $extra_mkfs_options
230
231     if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
232         echo "** mkfs failed with extra mkfs options added to \"$MKFS_OPTIONS\" by test $seq **" \
233             >>$here/$seq.full
234         echo "** attempting to mkfs using only test $seq options: $extra_mkfs_options **" \
235             >>$here/$seq.full
236         # running mkfs again. overwrite previous mkfs output files
237         $MKFS_XFS_PROG $SCRATCH_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
238             2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
239         mkfs_status=$?
240     fi
241
242     # output stored mkfs output
243     cat $tmp_dir.mkfserr >&2
244     cat $tmp_dir.mkfsstd
245     rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
246
247     if [ "$USE_BIG_LOOPFS" = yes ]; then
248         [ -z "$RETAIN_AG_BYTES" ] && RETAIN_AG_BYTES=0
249         ./tools/ag-wipe -q -r $RETAIN_AG_BYTES $SCRATCH_DEV
250     fi
251
252     return $mkfs_status
253 }
254
255 _scratch_mkfs()
256 {
257     case $FSTYP in
258     xfs)
259         _scratch_mkfs_xfs $*
260         ;;
261     nfs*)
262         # do nothing for nfs
263         ;;
264     udf)
265         $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
266         ;;
267     *)
268         /sbin/mkfs -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
269         ;;
270     esac
271 }
272
273 _scratch_xfs_db_options()
274 {
275     SCRATCH_OPTIONS=""
276     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
277         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
278     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
279 }
280
281 _scratch_xfs_logprint()
282 {
283     SCRATCH_OPTIONS=""
284     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
285         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
286     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
287 }
288
289 _scratch_xfs_check()
290 {
291     SCRATCH_OPTIONS=""
292     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
293         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
294     $XFS_CHECK_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
295 }
296
297 _scratch_xfs_repair()
298 {
299     SCRATCH_OPTIONS=""
300     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
301         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
302     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
303         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
304     [ "$USE_BIG_LOOPFS" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
305     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
306 }
307
308 _get_pids_by_name()
309 {
310     if [ $# -ne 1 ]
311     then
312         echo "Usage: _get_pids_by_name process-name" 1>&2
313         exit 1
314     fi
315
316     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
317     # HH:MM:SS before the psargs field, use this as the search anchor.
318     #
319     # Matches with $1 (process-name) occur if the first psarg is $1
320     # or ends in /$1 ... the matching uses sed's regular expressions,
321     # so passing a regex into $1 will work.
322
323     ps $PS_ALL_FLAGS \
324     | sed -n \
325         -e 's/$/ /' \
326         -e 's/[         ][      ]*/ /g' \
327         -e 's/^ //' \
328         -e 's/^[^ ]* //' \
329         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
330         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
331 }
332
333 # fqdn for localhost
334 #
335 _get_fqdn()
336 {
337     host=`hostname`
338     $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
339 }
340
341 # fix malloc libs output
342 #
343 _fix_malloc()
344 {
345     # filter out the Electric Fence notice
346     $PERL_PROG -e '
347         while (<>) {
348             if (defined $o && /^\s+Electric Fence/) {
349                 chomp($o);
350                 print "$o";
351                 undef $o;
352                 next;
353             }
354             print $o if (defined $o);
355
356             $o=$_;
357         }
358         print $o if (defined $o);
359     '
360 }
361
362 # check if run as root
363 #
364 _need_to_be_root()
365 {
366     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
367     if [ "$id" -ne 0 ]
368     then
369         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
370         exit 1
371     fi
372 }
373
374
375 #
376 # _df_device : get an IRIX style df line for a given device
377 #
378 #       - returns "" if not mounted
379 #       - returns fs type in field two (ala IRIX)
380 #       - joins line together if split by fancy df formatting
381 #       - strips header etc
382 #
383
384 _df_device()
385 {
386     if [ $# -ne 1 ]
387     then
388         echo "Usage: _df_device device" 1>&2
389         exit 1
390     fi
391
392     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
393         match($1,what) && NF==1 {
394             v=$1
395             getline
396             print v, $0
397             exit
398         }
399         match($1,what) {
400             print
401             exit
402         }
403     '
404 }
405
406 #
407 # _df_dir : get an IRIX style df line for device where a directory resides
408 #
409 #       - returns fs type in field two (ala IRIX)
410 #       - joins line together if split by fancy df formatting
411 #       - strips header etc
412 #
413
414 _df_dir()
415 {
416     if [ $# -ne 1 ]
417     then
418         echo "Usage: _df_dir device" 1>&2
419         exit 1
420     fi
421
422     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
423         NR == 2 && NF==1 {
424             v=$1
425             getline
426             print v, $0;
427             exit 0
428         }
429         NR == 2 {
430             print;
431             exit 0
432         }
433         {}
434     '
435     # otherwise, nada
436 }
437
438 # return percentage used disk space for mounted device
439
440 _used()
441 {
442     if [ $# -ne 1 ]
443     then
444         echo "Usage: _used device" 1>&2
445         exit 1
446     fi
447
448     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
449 }
450
451 # return the FS type of a mounted device
452 #
453 _fs_type()
454 {
455     if [ $# -ne 1 ]
456     then
457         echo "Usage: _fs_type device" 1>&2
458         exit 1
459     fi
460
461     _df_device $1 | $AWK_PROG '{ print $2 }'
462 }
463
464 # return the FS mount options of a mounted device
465 #
466 # should write a version which just parses the output of mount for IRIX
467 # compatibility, but since this isn't used at all, at the moment I'll leave
468 # this for now
469 #
470 _fs_options()
471 {
472     if [ $# -ne 1 ]
473     then
474         echo "Usage: _fs_options device" 1>&2
475         exit 1
476     fi
477
478     $AWK_PROG -v dev=$1 '
479         match($1,dev) { print $4 }
480     ' </proc/mounts
481 }
482
483 # returns device number if a file is a block device
484 #
485 _is_block_dev()
486 {
487     if [ $# -ne 1 ]
488     then
489         echo "Usage: _is_block_dev dev" 1>&2
490         exit 1
491     fi
492
493     [ -b $1 ] && src/lstat64 $1 | $AWK_PROG '/Device type:/ { print $9 }'
494 }
495
496 # Do a command, log it to $seq.full, optionally test return status
497 # and die if command fails. If called with one argument _do executes the
498 # command, logs it, and returns its exit status. With two arguments _do
499 # first prints the message passed in the first argument, and then "done"
500 # or "fail" depending on the return status of the command passed in the
501 # second argument. If the command fails and the variable _do_die_on_error
502 # is set to "always" or the two argument form is used and _do_die_on_error
503 # is set to "message_only" _do will print an error message to
504 # $seq.out and exit.
505
506 _do()
507 {
508     if [ $# -eq 1 ]; then
509         _cmd=$1
510     elif [ $# -eq 2 ]; then
511         _note=$1
512         _cmd=$2
513         echo -n "$_note... "
514     else
515         echo "Usage: _do [note] cmd" 1>&2
516         status=1; exit
517     fi
518
519     (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
520     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
521     cat $tmp._out | _fix_malloc >>$here/$seq.full
522     if [ $# -eq 2 ]; then
523         if [ $ret -eq 0 ]; then
524             echo "done"
525         else
526             echo "fail"
527         fi
528     fi
529     if [ $ret -ne 0  ] \
530         && [ "$_do_die_on_error" = "always" \
531             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
532     then
533         [ $# -ne 2 ] && echo
534         eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
535         status=1; exit
536     fi
537
538     return $ret
539 }
540
541 # bail out, setting up .notrun file
542 #
543 _notrun()
544 {
545     echo "$*" >$seq.notrun
546     echo "$seq not run: $*"
547     status=0
548     exit
549 }
550
551 # just plain bail out
552 #
553 _fail()
554 {
555     echo "$*" | tee -a $here/$seq.full
556     echo "(see $seq.full for details)"
557     status=1
558     exit 1
559 }
560
561 # tests whether $FSTYP is one of the supported filesystems for a test
562 #
563 _supported_fs()
564 {
565     for f
566     do
567         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
568         then
569             return
570         fi
571     done
572
573     _notrun "not suitable for this filesystem type: $FSTYP"
574 }
575
576 # tests whether $FSTYP is one of the supported OSes for a test
577 #
578 _supported_os()
579 {
580     for h
581     do
582         if [ "$h" = "$HOSTOS" ]
583         then
584             return
585         fi
586     done
587
588     _notrun "not suitable for this OS: $HOSTOS"
589 }
590
591 # this test needs a scratch partition - check we're ok & unmount it
592 #
593 _require_scratch()
594 {
595     case "$FSTYP" in
596         nfs*)
597                  echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
598                  if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]
599                  then
600                      _notrun "this test requires a valid \$SCRATCH_DEV"
601                  fi
602                  ;;
603         *)
604                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
605                  then
606                      _notrun "this test requires a valid \$SCRATCH_DEV"
607                  fi
608                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
609                  then
610                      _notrun "this test requires a valid \$SCRATCH_DEV"
611                  fi
612                  ;;
613     esac
614
615     # mounted?
616     if _mount | grep -q $SCRATCH_DEV
617     then
618         # if it's mounted, make sure its on $SCRATCH_MNT
619         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
620         then
621             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
622             exit 1
623         fi
624         # and then unmount it
625         if ! $UMOUNT_PROG $SCRATCH_DEV
626         then
627             echo "failed to unmount $SCRATCH_DEV"
628             exit 1
629         fi
630     fi
631 }
632
633 # this test needs a logdev
634 #
635 _require_logdev()
636 {
637     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
638         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
639     [ "$USE_EXTERNAL" != yes ] && \
640         _notrun "This test requires USE_EXTERNAL to be enabled"
641
642     # ensure its not mounted
643     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
644 }
645
646 # this test requires loopback device support
647 #
648 _require_loop()
649 {
650     if [ "$HOSTOS" != "Linux" ]
651     then
652         _notrun "This test requires linux for loopback device support"
653     fi
654
655     modprobe loop >/dev/null 2>&1
656     if grep loop /proc/devices >/dev/null 2>&1
657     then
658         :
659     else
660         _notrun "This test requires loopback device support"
661     fi
662 }
663
664 # this test requires that (large) loopback device files are not in use
665 #
666 _require_nobigloopfs()
667 {
668     [ "$USE_BIG_LOOPFS" = yes ] && \
669         _notrun "Large filesystem testing in progress, skipped this test"
670 }
671
672 # this test requires that a realtime subvolume is in use, and
673 # that the kernel supports realtime as well.
674 #
675 _require_realtime()
676 {
677     [ "$USE_EXTERNAL" = yes ] || \
678         _notrun "External volumes not in use, skipped this test"
679     [ "$SCRATCH_RTDEV" = "" ] && \
680         _notrun "Realtime device required, skipped this test"
681 }
682
683 # this test requires that a specified command (executable) exists
684 #
685 _require_command()
686 {
687     [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
688 }
689
690 # this test requires that external log/realtime devices are not in use
691 #
692 _require_nonexternal()
693 {
694     [ "$USE_EXTERNAL" = yes ] && \
695         _notrun "External device testing in progress, skipped this test"
696 }
697
698 # check for the fsgqa user on the machine
699 #
700 _require_user()
701 {
702     qa_user=fsgqa
703     cat /etc/passwd | grep -q $qa_user
704     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
705 }
706
707 # check that a FS on a device is mounted
708 # if so, return mount point
709 #
710 _is_mounted()
711 {
712     if [ $# -ne 1 ]
713     then
714         echo "Usage: _is_mounted device" 1>&2
715         exit 1
716     fi
717
718     device=$1
719
720     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
721         pattern        { print $3 ; exit 0 }
722         END            { exit 1 }
723     '
724     then
725         echo "_is_mounted: $device is not a mounted $FSTYP FS"
726         exit 1
727     fi
728 }
729
730 # remount a FS to a new mode (ro or rw)
731 #
732 _remount()
733 {
734     if [ $# -ne 2 ]
735     then
736         echo "Usage: _remount device ro/rw" 1>&2
737         exit 1
738     fi
739     device=$1
740     mode=$2
741
742     if ! mount -o remount,$mode $device
743     then
744         echo "_remount: failed to remount filesystem on $device as $mode"
745         exit 1
746     fi
747 }
748
749 # Run the appropriate repair/check on a filesystem
750 #
751 # if the filesystem is mounted, it's either remounted ro before being
752 # checked or it's unmounted and then remounted
753 #
754
755 # If set, we remount ro instead of unmounting for fsck
756 USE_REMOUNT=0
757
758 _umount_or_remount_ro()
759 {
760     if [ $# -ne 1 ]
761     then
762         echo "Usage: _umount_or_remount_ro <device>" 1>&2
763         exit 1
764     fi
765
766     device=$1
767     mountpoint=`_is_mounted $device`
768
769     if [ $USE_REMOUNT -eq 0 ]; then
770         $UMOUNT_PROG $device
771     else
772         _remount $device ro
773     fi
774     echo "$mountpoint"
775 }
776
777 _mount_or_remount_rw()
778 {
779     if [ $# -ne 3 ]
780     then
781         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
782         exit 1
783     fi
784     mount_opts=$1
785     device=$2
786     mountpoint=$3
787
788     if [ $USE_REMOUNT -eq 0 ]
789     then
790         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
791         then
792             echo "!!! failed to remount $device on $mountpoint"
793             return 0 # ok=0
794         fi
795     else
796         _remount $device rw
797     fi
798
799     return 1 # ok=1
800 }
801
802 # Check a generic filesystem in no-op mode; this assumes that the
803 # underlying fsck program accepts "-n" for a no-op (check-only) run,
804 # and that it will still return an errno for corruption in this mode.
805 #
806 # Filesystems which don't support this will need to define their
807 # own check routine.
808 #
809 _check_generic_filesystem()
810 {
811     device=$1
812
813     # If type is set, we're mounted
814     type=`_fs_type $device`
815     ok=1
816
817     if [ "$type" = "$FSTYP" ]
818     then
819         # mounted ...
820         mountpoint=`_umount_or_remount_ro $device`
821     fi
822
823     fsck -t $FSTYP -n $device >$tmp.fsck 2>&1
824     if [ $? -ne 0 ]
825     then
826         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seq.full)"
827
828         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
829         echo "*** fsck.$FSTYP output ***"                     >>$here/$seq.full
830         cat $tmp.fsck                                         >>$here/$seq.full
831         echo "*** end fsck.$FSTYP output"                     >>$here/$seq.full
832
833         ok=0
834     fi
835     rm -f $tmp.fsck
836
837     if [ $ok -eq 0 ]
838     then
839         echo "*** mount output ***"                             >>$here/$seq.full
840         _mount                                                  >>$here/$seq.full
841         echo "*** end mount output"                             >>$here/$seq.full
842     elif [ "$type" = "$FSTYP" ]
843     then
844         # was mounted ...
845         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
846         ok=$?
847     fi
848
849     [ $ok -eq 0 ] && exit 1
850     return 0
851 }
852
853 # run xfs_check and friends on a FS.
854
855 _check_xfs_filesystem()
856 {
857     if [ $# -ne 3 ]
858     then
859         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
860         exit 1
861     fi
862
863     extra_mount_options=""
864     device=$1
865     if [ "$2" != "none" ]; then
866         extra_log_options="-l$2"
867         extra_mount_options="-ologdev=$2"
868     fi
869
870     if [ "$3" != "none" ]; then
871         extra_rt_options="-r$3"
872         extra_mount_options=$extra_mount_options" -ortdev=$3"
873     fi
874     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
875
876     [ "$FSTYP" != xfs ] && return 0
877     testoption=""
878     [ "$USE_BIG_LOOPFS" = yes ] && testoption=-t
879
880     type=`_fs_type $device`
881     ok=1
882
883     if [ "$type" = "xfs" ]
884     then
885         # mounted ...
886         mountpoint=`_umount_or_remount_ro $device`
887     fi
888
889     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
890                 | tee $tmp.logprint | grep -q "<CLEAN>"
891     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
892     then
893         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seq.full)"
894
895         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$here/$seq.full
896         echo "*** xfs_logprint -t output ***"                   >>$here/$seq.full
897         cat $tmp.logprint                                       >>$here/$seq.full
898         echo "*** end xfs_logprint output"                      >>$here/$seq.full
899
900         ok=0
901     fi
902
903     $XFS_CHECK_PROG $testoption $extra_log_options $device 2>&1 |\
904          _fix_malloc >$tmp.fs_check
905     if [ -s $tmp.fs_check ]
906     then
907         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seq.full)"
908
909         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
910         echo "*** xfs_check output ***"                         >>$here/$seq.full
911         cat $tmp.fs_check                                       >>$here/$seq.full
912         echo "*** end xfs_check output"                         >>$here/$seq.full
913
914         ok=0
915     fi
916     # repair doesn't scale massively at this stage, optionally skip it for now
917     [ "$USE_BIG_LOOPFS" = yes ] || \
918     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
919     if [ $? -ne 0 ]
920     then
921         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seq.full)"
922
923         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
924         echo "*** xfs_repair -n output ***"                     >>$here/$seq.full
925         cat $tmp.repair | _fix_malloc                           >>$here/$seq.full
926         echo "*** end xfs_repair output"                        >>$here/$seq.full
927
928         ok=0
929     fi
930     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
931
932     if [ $ok -eq 0 ]
933     then
934         echo "*** mount output ***"                             >>$here/$seq.full
935         _mount                                                  >>$here/$seq.full
936         echo "*** end mount output"                             >>$here/$seq.full
937     elif [ "$type" = "xfs" ]
938     then
939         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
940     fi
941
942     [ $ok -eq 0 ] && exit 1
943     return 0
944 }
945
946 # Filter the knowen errors the UDF Verifier reports.
947 _udf_test_known_error_filter()
948 {
949         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."
950
951 }
952
953 _check_udf_filesystem()
954 {
955     [ "$DISABLE_UDF_TEST" == "1" ] && return
956
957     if [ $# -ne 1 -a $# -ne 2 ]
958     then
959         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
960         exit 1
961     fi
962
963     if [ ! -x $here/src/udf_test ]
964     then
965         echo "udf_test not installed, please download and build the Philips"
966         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
967         echo "Then copy the udf_test binary to $here/src/."
968         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
969         echo "to 1."
970         return
971     fi
972
973     device=$1
974     if [ $# -eq 2 ];
975     then
976         LAST_BLOCK=`expr \( $2 - 1 \)`
977         OPT_ARG="-lastvalidblock $LAST_BLOCK"
978     fi
979
980     rm -f $seq.checkfs
981     sleep 1 # Due to a problem with time stamps in udf_test
982     $here/src/udf_test $OPT_ARG $device | tee $here/$seq.checkfs | egrep "Error|Warning" | \
983         _udf_test_known_error_filter | \
984         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" | \
985         sed "s/^.*$/Warning UDF Verifier reported errors see $seq.checkfs./g"
986
987 }
988
989 _check_xfs_test_fs()
990 {
991     TEST_LOG="none"
992     TEST_RT="none"
993     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
994         TEST_LOG="$TEST_LOGDEV"
995
996     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
997         TEST_RT="$TEST_RTDEV"
998
999     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1000
1001     # check for ipath consistency
1002     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1003         # errors go to stderr
1004         xfs_check_ipaths $TEST_DIR >/dev/null
1005         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1006     fi
1007 }
1008
1009 _check_test_fs()
1010 {
1011     case $FSTYP in
1012     xfs)
1013         _check_xfs_test_fs
1014         ;;
1015     nfs)
1016         # no way to check consistency for nfs
1017         ;;
1018     udf)
1019         # do nothing for now
1020         ;;
1021     *)
1022         _check_generic_filesystem $TEST_DEV
1023         ;;
1024     esac
1025 }
1026
1027 _check_scratch_fs()
1028 {
1029     case $FSTYP in
1030     xfs)
1031         SCRATCH_LOG="none"
1032         SCRATCH_RT="none"
1033         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1034             SCRATCH_LOG="$SCRATCH_LOGDEV"
1035
1036         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1037             SCRATCH_RT="$SCRATCH_RTDEV"
1038
1039         _check_xfs_filesystem $SCRATCH_DEV $SCRATCH_LOG $SCRATCH_RT
1040         ;;
1041     udf)
1042         _check_udf_filesystem $SCRATCH_DEV $udf_fsize
1043         ;;
1044     nfs*)
1045         # Don't know how to check an NFS filesystem, yet.
1046         ;;
1047     *)
1048         _check_generic_filesystem $SCRATCH_DEV
1049         ;;
1050     esac
1051 }
1052
1053 _full_fstyp_details()
1054 {
1055      [ -z "$FSTYP" ] && FSTYP=xfs
1056      if [ $FSTYP = xfs ]; then
1057         if [ -d /proc/fs/xfs ]; then
1058             if grep -q 'debug 0' /proc/fs/xfs/stat; then
1059                 FSTYP="$FSTYP (non-debug)"
1060             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
1061                 FSTYP="$FSTYP (debug)"
1062             fi
1063         else
1064             if uname -a | grep -qi 'debug'; then
1065                 FSTYP="$FSTYP (debug)"
1066             else
1067                 FSTYP="$FSTYP (non-debug)"
1068             fi
1069         fi
1070      fi
1071      echo $FSTYP
1072 }
1073
1074 _full_platform_details()
1075 {
1076      os=`uname -s`
1077      host=`hostname -s`
1078      kernel=`uname -r`
1079      platform=`uname -m`
1080      echo "$os/$platform $host $kernel"
1081 }
1082
1083 _setup_udf_scratchdir()
1084 {
1085     [ "$FSTYP" != "udf" ] \
1086         && _fail "setup_udf_testdir: \$FSTYP is not udf"
1087     [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
1088         && _notrun "this test requires a valid \$SCRATCH_DEV"
1089     [ -z "$SCRATCH_MNT" ] \
1090         && _notrun "this test requires a valid \$SCRATCH_MNT"
1091
1092     # mounted?
1093     if _mount | grep -q $SCRATCH_DEV
1094     then
1095         # if it's mounted, make sure its on $TEST_RW_DIR
1096         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1097         then
1098             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1099         fi
1100         $UMOUNT_PROG $SCRATCH_DEV
1101     fi
1102
1103     _scratch_mkfs
1104     _scratch_mount
1105
1106     testdir=$SCRATCH_MNT
1107 }
1108
1109 _setup_nfs_scratchdir()
1110 {
1111     [ "$FSTYP" != "nfs" ] \
1112         && _fail "setup_nfs_testdir: \$FSTYP is not nfs"
1113     [ -z "$SCRATCH_DEV" ] \
1114         && _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
1115     [ -z "$SCRATCH_MNT" ] \
1116         && _notrun "this test requires a valid \$SCRATCH_MNT"
1117
1118     # mounted?
1119     if _mount | grep -q $SCRATCH_DEV
1120     then
1121         # if it's mounted, make sure its on $SCRATCH_MNT
1122         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1123         then
1124             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1125         fi
1126         $UMOUNT_PROG $SCRATCH_DEV
1127     fi
1128
1129     _scratch_mkfs
1130     _scratch_mount
1131
1132     testdir=$SCRATCH_MNT
1133 }
1134
1135 #
1136 # Warning for UDF and NFS:
1137 # this function calls _setup_udf_scratchdir and _setup_udf_scratchdir
1138 # which actually uses the scratch dir for the test dir.
1139 #
1140 # This was done because testdir was intended to be a persistent
1141 # XFS only partition.  This should eventually change, and treat
1142 # at least local filesystems all the same.
1143 #
1144 _setup_testdir()
1145 {
1146     case $FSTYP in
1147     udf)
1148         _setup_udf_scratchdir
1149         ;;
1150     nfs*)
1151         _setup_nfs_scratchdir
1152         ;;
1153     *)
1154         testdir=$TEST_DIR
1155         ;;
1156     esac
1157 }
1158
1159 _cleanup_testdir()
1160 {
1161     case $FSTYP in
1162     udf)
1163         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1164         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1165         ;;
1166     nfs*)
1167         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1168         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1169         ;;
1170     *)
1171         # do nothing, testdir is $TEST_DIR
1172         :
1173         ;;
1174     esac
1175 }
1176
1177 _link_out_file()
1178 {
1179    if [ -z "$1" ]; then
1180       echo Error must pass \$seq.
1181       exit
1182    fi
1183    rm -f $1
1184    if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1185       ln -s $1.irix $1
1186    elif [ "`uname`" == "Linux" ]; then
1187       ln -s $1.linux $1
1188    else
1189       echo Error test $seq does not run on the operating system: `uname`
1190       exit
1191    fi
1192 }
1193
1194 _die()
1195 {
1196         echo $@
1197         exit 1
1198 }
1199
1200 _nfiles()
1201 {
1202         f=0
1203         while [ $f -lt $1 ]
1204         do
1205                 file=f$f
1206                 echo > $file
1207                 if [ $size -gt 0 ]; then
1208                     dd if=/dev/zero of=$file bs=1024 count=$size
1209                 fi
1210                 let f=$f+1
1211         done
1212 }
1213
1214 # takes dirname, depth
1215 _descend()
1216 {
1217         dirname=$1; depth=$2
1218         mkdir $dirname  || die "mkdir $dirname failed"
1219         cd $dirname
1220
1221         _nfiles $files           # files for this dir
1222
1223         [ $depth -eq 0 ] && return
1224         let deep=$depth-1 # go 1 down
1225
1226         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
1227
1228         d=0
1229         while [ $d -lt $dirs ]
1230         do
1231                 _descend d$d $deep &
1232                 let d=$d+1
1233                 wait
1234         done
1235 }
1236
1237 # Populate a filesystem with inodes for performance experiments
1238 #
1239 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size]
1240 #
1241 _populate_fs()
1242 {
1243     here=`pwd`
1244     dirs=5          # ndirs in each subdir till leaves
1245     size=0          # sizeof files in K
1246     files=100       # num files in _each_ subdir
1247     depth=2         # depth of tree from root to leaves
1248     verbose=false
1249     root=root       # path of initial root of directory tree
1250
1251     while getopts "d:f:n:r:s:v" c
1252     do
1253         case $c in
1254         d)      depth=$OPTARG;;
1255         n)      dirs=$OPTARG;;
1256         f)      files=$OPTARG;;
1257         s)      size=$OPTARG;;
1258         v)      verbose=true;;
1259         r)      root=$OPTARG;;
1260         esac
1261     done
1262
1263     _descend $root $depth
1264     wait
1265
1266     cd $here
1267
1268     [ $verbose = true ] && echo done
1269 }
1270
1271 # query whether the given file has the given inode flag set
1272 #
1273 _test_inode_flag()
1274 {
1275     flag=$1
1276     file=$2
1277
1278     if which $XFS_IO_PROG >/dev/null; then
1279         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
1280             return 0
1281         fi
1282     fi
1283     return 1
1284 }
1285
1286 # query the given files extsize allocator hint in bytes (if any)
1287 #
1288 _test_inode_extsz()
1289 {
1290     file=$1
1291     blocks=""
1292
1293     if which $XFS_IO_PROG >/dev/null; then
1294         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
1295                 awk '/^xattr.extsize =/ { print $3 }'`
1296     fi
1297     [ -z "$blocks" ] && blocks="0"
1298     echo $blocks
1299 }
1300
1301
1302 ################################################################################
1303
1304 if [ "$iam" != new -a "$iam" != bench ]
1305 then
1306     # make some further configuration checks here
1307
1308     if [ "$TEST_DEV" = ""  ]
1309     then
1310         echo "common.rc: Error: \$TEST_DEV is not set"
1311         exit 1
1312     fi
1313
1314     # if $TEST_DEV is not mounted, mount it now as XFS
1315     if [ -z "`_fs_type $TEST_DEV`" ]
1316     then
1317         # $TEST_DEV is not mounted
1318         if ! _test_mount
1319         then
1320             echo "common.rc: retrying test device mount with external set"
1321             [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
1322             if ! _test_mount
1323             then
1324                 echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
1325                 exit 1
1326             fi
1327         fi
1328     fi
1329
1330     if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
1331     then
1332         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
1333         $DF_PROG $TEST_DEV
1334         exit 1
1335     fi
1336 fi
1337
1338 # make sure this script returns success
1339 /bin/true