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