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