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