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