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