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