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