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