xfstests: 226: have xfs_io use bigger buffers
[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)
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)
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)
330         /sbin/mkfs.$FSTYP -b $blocksize $SCRATCH_DEV $blocks
331         ;;
332     *)
333         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
334         ;;
335     esac
336 }
337
338 # Emulate an N-data-disk stripe w/ various stripe units
339 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
340 _scratch_mkfs_geom()
341 {
342     sunit_bytes=$1
343     swidth_mult=$2
344     blocksize=$3
345     [ -z "$blocksize" ] && blocksize=4096
346
347     let sunit_blocks=$sunit_bytes/$blocksize
348     let swidth_blocks=$sunit_blocks*$swidth_mult
349
350     case $FSTYP in
351     xfs)
352         MKFS_OPTIONS="-b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
353         ;;
354     ext4)
355         MKFS_OPTIONS="-b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
356         ;;
357     *)
358         _notrun "can't mkfs $FSTYP with geometry"
359         ;;
360     esac
361     _scratch_mkfs
362 }
363
364 _scratch_resvblks()
365 {
366         case $FSTYP in
367         xfs)
368                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
369                 ;;
370         *)
371                 ;;
372         esac
373 }
374
375 _scratch_xfs_db_options()
376 {
377     SCRATCH_OPTIONS=""
378     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
379         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
380     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
381 }
382
383 _scratch_xfs_logprint()
384 {
385     SCRATCH_OPTIONS=""
386     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
387         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
388     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
389 }
390
391 _scratch_xfs_check()
392 {
393     SCRATCH_OPTIONS=""
394     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
395         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
396     $XFS_CHECK_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
397 }
398
399 _scratch_xfs_repair()
400 {
401     SCRATCH_OPTIONS=""
402     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
403         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
404     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
405         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
406     [ "$USE_BIG_LOOPFS" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
407     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
408 }
409
410 _get_pids_by_name()
411 {
412     if [ $# -ne 1 ]
413     then
414         echo "Usage: _get_pids_by_name process-name" 1>&2
415         exit 1
416     fi
417
418     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
419     # HH:MM:SS before the psargs field, use this as the search anchor.
420     #
421     # Matches with $1 (process-name) occur if the first psarg is $1
422     # or ends in /$1 ... the matching uses sed's regular expressions,
423     # so passing a regex into $1 will work.
424
425     ps $PS_ALL_FLAGS \
426     | sed -n \
427         -e 's/$/ /' \
428         -e 's/[         ][      ]*/ /g' \
429         -e 's/^ //' \
430         -e 's/^[^ ]* //' \
431         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
432         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
433 }
434
435 # fix malloc libs output
436 #
437 _fix_malloc()
438 {
439     # filter out the Electric Fence notice
440     $PERL_PROG -e '
441         while (<>) {
442             if (defined $o && /^\s+Electric Fence/) {
443                 chomp($o);
444                 print "$o";
445                 undef $o;
446                 next;
447             }
448             print $o if (defined $o);
449
450             $o=$_;
451         }
452         print $o if (defined $o);
453     '
454 }
455
456 # check if run as root
457 #
458 _need_to_be_root()
459 {
460     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
461     if [ "$id" -ne 0 ]
462     then
463         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
464         exit 1
465     fi
466 }
467
468
469 #
470 # _df_device : get an IRIX style df line for a given device
471 #
472 #       - returns "" if not mounted
473 #       - returns fs type in field two (ala IRIX)
474 #       - joins line together if split by fancy df formatting
475 #       - strips header etc
476 #
477
478 _df_device()
479 {
480     if [ $# -ne 1 ]
481     then
482         echo "Usage: _df_device device" 1>&2
483         exit 1
484     fi
485
486     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
487         match($1,what) && NF==1 {
488             v=$1
489             getline
490             print v, $0
491             exit
492         }
493         match($1,what) {
494             print
495             exit
496         }
497     '
498 }
499
500 #
501 # _df_dir : get an IRIX style df line for device where a directory resides
502 #
503 #       - returns fs type in field two (ala IRIX)
504 #       - joins line together if split by fancy df formatting
505 #       - strips header etc
506 #
507
508 _df_dir()
509 {
510     if [ $# -ne 1 ]
511     then
512         echo "Usage: _df_dir device" 1>&2
513         exit 1
514     fi
515
516     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
517         NR == 2 && NF==1 {
518             v=$1
519             getline
520             print v, $0;
521             exit 0
522         }
523         NR == 2 {
524             print;
525             exit 0
526         }
527         {}
528     '
529     # otherwise, nada
530 }
531
532 # return percentage used disk space for mounted device
533
534 _used()
535 {
536     if [ $# -ne 1 ]
537     then
538         echo "Usage: _used device" 1>&2
539         exit 1
540     fi
541
542     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
543 }
544
545 # return the FS type of a mounted device
546 #
547 _fs_type()
548 {
549     if [ $# -ne 1 ]
550     then
551         echo "Usage: _fs_type device" 1>&2
552         exit 1
553     fi
554
555     _df_device $1 | $AWK_PROG '{ print $2 }'
556 }
557
558 # return the FS mount options of a mounted device
559 #
560 # should write a version which just parses the output of mount for IRIX
561 # compatibility, but since this isn't used at all, at the moment I'll leave
562 # this for now
563 #
564 _fs_options()
565 {
566     if [ $# -ne 1 ]
567     then
568         echo "Usage: _fs_options device" 1>&2
569         exit 1
570     fi
571
572     $AWK_PROG -v dev=$1 '
573         match($1,dev) { print $4 }
574     ' </proc/mounts
575 }
576
577 # returns device number if a file is a block device
578 #
579 _is_block_dev()
580 {
581     if [ $# -ne 1 ]
582     then
583         echo "Usage: _is_block_dev dev" 1>&2
584         exit 1
585     fi
586
587     [ -b $1 ] && src/lstat64 $1 | $AWK_PROG '/Device type:/ { print $9 }'
588 }
589
590 # Do a command, log it to $seq.full, optionally test return status
591 # and die if command fails. If called with one argument _do executes the
592 # command, logs it, and returns its exit status. With two arguments _do
593 # first prints the message passed in the first argument, and then "done"
594 # or "fail" depending on the return status of the command passed in the
595 # second argument. If the command fails and the variable _do_die_on_error
596 # is set to "always" or the two argument form is used and _do_die_on_error
597 # is set to "message_only" _do will print an error message to
598 # $seq.out and exit.
599
600 _do()
601 {
602     if [ $# -eq 1 ]; then
603         _cmd=$1
604     elif [ $# -eq 2 ]; then
605         _note=$1
606         _cmd=$2
607         echo -n "$_note... "
608     else
609         echo "Usage: _do [note] cmd" 1>&2
610         status=1; exit
611     fi
612
613     (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
614     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
615     cat $tmp._out | _fix_malloc >>$here/$seq.full
616     if [ $# -eq 2 ]; then
617         if [ $ret -eq 0 ]; then
618             echo "done"
619         else
620             echo "fail"
621         fi
622     fi
623     if [ $ret -ne 0  ] \
624         && [ "$_do_die_on_error" = "always" \
625             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
626     then
627         [ $# -ne 2 ] && echo
628         eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
629         status=1; exit
630     fi
631
632     return $ret
633 }
634
635 # bail out, setting up .notrun file
636 #
637 _notrun()
638 {
639     echo "$*" >$seq.notrun
640     echo "$seq not run: $*"
641     status=0
642     exit
643 }
644
645 # just plain bail out
646 #
647 _fail()
648 {
649     echo "$*" | tee -a $here/$seq.full
650     echo "(see $seq.full for details)"
651     status=1
652     exit 1
653 }
654
655 # tests whether $FSTYP is one of the supported filesystems for a test
656 #
657 _supported_fs()
658 {
659     for f
660     do
661         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
662         then
663             return
664         fi
665     done
666
667     _notrun "not suitable for this filesystem type: $FSTYP"
668 }
669
670 # tests whether $FSTYP is one of the supported OSes for a test
671 #
672 _supported_os()
673 {
674     for h
675     do
676         if [ "$h" = "$HOSTOS" ]
677         then
678             return
679         fi
680     done
681
682     _notrun "not suitable for this OS: $HOSTOS"
683 }
684
685 # this test needs a scratch partition - check we're ok & unmount it
686 #
687 _require_scratch()
688 {
689     case "$FSTYP" in
690         nfs*)
691                  echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
692                  if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]
693                  then
694                      _notrun "this test requires a valid \$SCRATCH_DEV"
695                  fi
696                  ;;
697         *)
698                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
699                  then
700                      _notrun "this test requires a valid \$SCRATCH_DEV"
701                  fi
702                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
703                  then
704                      _notrun "this test requires a valid \$SCRATCH_DEV"
705                  fi
706                  ;;
707     esac
708
709     # mounted?
710     if _mount | grep -q $SCRATCH_DEV
711     then
712         # if it's mounted, make sure its on $SCRATCH_MNT
713         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
714         then
715             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
716             exit 1
717         fi
718         # and then unmount it
719         if ! $UMOUNT_PROG $SCRATCH_DEV
720         then
721             echo "failed to unmount $SCRATCH_DEV"
722             exit 1
723         fi
724     fi
725 }
726
727 # this test needs a logdev
728 #
729 _require_logdev()
730 {
731     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
732         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
733     [ "$USE_EXTERNAL" != yes ] && \
734         _notrun "This test requires USE_EXTERNAL to be enabled"
735
736     # ensure its not mounted
737     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
738 }
739
740 # this test requires loopback device support
741 #
742 _require_loop()
743 {
744     if [ "$HOSTOS" != "Linux" ]
745     then
746         _notrun "This test requires linux for loopback device support"
747     fi
748
749     modprobe loop >/dev/null 2>&1
750     if grep loop /proc/devices >/dev/null 2>&1
751     then
752         :
753     else
754         _notrun "This test requires loopback device support"
755     fi
756 }
757
758 # this test requires that (large) loopback device files are not in use
759 #
760 _require_nobigloopfs()
761 {
762     [ "$USE_BIG_LOOPFS" = yes ] && \
763         _notrun "Large filesystem testing in progress, skipped this test"
764 }
765
766 # this test requires that a realtime subvolume is in use, and
767 # that the kernel supports realtime as well.
768 #
769 _require_realtime()
770 {
771     [ "$USE_EXTERNAL" = yes ] || \
772         _notrun "External volumes not in use, skipped this test"
773     [ "$SCRATCH_RTDEV" = "" ] && \
774         _notrun "Realtime device required, skipped this test"
775 }
776
777 # this test requires that a specified command (executable) exists
778 #
779 _require_command()
780 {
781     [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
782 }
783
784 # this test requires that external log/realtime devices are not in use
785 #
786 _require_nonexternal()
787 {
788     [ "$USE_EXTERNAL" = yes ] && \
789         _notrun "External device testing in progress, skipped this test"
790 }
791
792 # check for the fsgqa user on the machine
793 #
794 _require_user()
795 {
796     qa_user=fsgqa
797     cat /etc/passwd | grep -q $qa_user
798     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
799 }
800
801 # check that xfs_io, glibc, kernel, and filesystem all (!) support
802 # fallocate
803 #
804 _require_xfs_io_falloc()
805 {
806         testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $TEST_DIR/$tmp.io 2>&1`
807         rm -f $TEST_DIR/$tmp.io 2>&1 > /dev/null
808         echo $testio | grep -q "not found" && \
809                 _notrun "xfs_io fallocate support is missing"
810         echo $testio | grep -q "Operation not supported" && \
811                 _notrun "xfs_io fallocate command failed (old kernel/wrong fs?)"
812 }
813
814 # Check that a fs has enough free space (in 1024b blocks)
815 #
816 _require_fs_space()
817 {
818         MNT=$1
819         BLOCKS=$2       # in units of 1024
820         let GB=$BLOCKS/1024/1024
821
822         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
823         [ $FREE_BLOCKS -lt $BLOCKS ] && \
824                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
825 }
826
827 # check that a FS on a device is mounted
828 # if so, return mount point
829 #
830 _is_mounted()
831 {
832     if [ $# -ne 1 ]
833     then
834         echo "Usage: _is_mounted device" 1>&2
835         exit 1
836     fi
837
838     device=$1
839
840     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
841         pattern        { print $3 ; exit 0 }
842         END            { exit 1 }
843     '
844     then
845         echo "_is_mounted: $device is not a mounted $FSTYP FS"
846         exit 1
847     fi
848 }
849
850 # remount a FS to a new mode (ro or rw)
851 #
852 _remount()
853 {
854     if [ $# -ne 2 ]
855     then
856         echo "Usage: _remount device ro/rw" 1>&2
857         exit 1
858     fi
859     device=$1
860     mode=$2
861
862     if ! mount -o remount,$mode $device
863     then
864         echo "_remount: failed to remount filesystem on $device as $mode"
865         exit 1
866     fi
867 }
868
869 # Run the appropriate repair/check on a filesystem
870 #
871 # if the filesystem is mounted, it's either remounted ro before being
872 # checked or it's unmounted and then remounted
873 #
874
875 # If set, we remount ro instead of unmounting for fsck
876 USE_REMOUNT=0
877
878 _umount_or_remount_ro()
879 {
880     if [ $# -ne 1 ]
881     then
882         echo "Usage: _umount_or_remount_ro <device>" 1>&2
883         exit 1
884     fi
885
886     device=$1
887     mountpoint=`_is_mounted $device`
888
889     if [ $USE_REMOUNT -eq 0 ]; then
890         $UMOUNT_PROG $device
891     else
892         _remount $device ro
893     fi
894     echo "$mountpoint"
895 }
896
897 _mount_or_remount_rw()
898 {
899     if [ $# -ne 3 ]
900     then
901         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
902         exit 1
903     fi
904     mount_opts=$1
905     device=$2
906     mountpoint=$3
907
908     if [ $USE_REMOUNT -eq 0 ]
909     then
910         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
911         then
912             echo "!!! failed to remount $device on $mountpoint"
913             return 0 # ok=0
914         fi
915     else
916         _remount $device rw
917     fi
918
919     return 1 # ok=1
920 }
921
922 # Check a generic filesystem in no-op mode; this assumes that the
923 # underlying fsck program accepts "-n" for a no-op (check-only) run,
924 # and that it will still return an errno for corruption in this mode.
925 #
926 # Filesystems which don't support this will need to define their
927 # own check routine.
928 #
929 _check_generic_filesystem()
930 {
931     device=$1
932
933     # If type is set, we're mounted
934     type=`_fs_type $device`
935     ok=1
936
937     if [ "$type" = "$FSTYP" ]
938     then
939         # mounted ...
940         mountpoint=`_umount_or_remount_ro $device`
941     fi
942
943     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
944     if [ $? -ne 0 ]
945     then
946         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seq.full)"
947
948         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
949         echo "*** fsck.$FSTYP output ***"                     >>$here/$seq.full
950         cat $tmp.fsck                                         >>$here/$seq.full
951         echo "*** end fsck.$FSTYP output"                     >>$here/$seq.full
952
953         ok=0
954     fi
955     rm -f $tmp.fsck
956
957     if [ $ok -eq 0 ]
958     then
959         echo "*** mount output ***"                             >>$here/$seq.full
960         _mount                                                  >>$here/$seq.full
961         echo "*** end mount output"                             >>$here/$seq.full
962     elif [ "$type" = "$FSTYP" ]
963     then
964         # was mounted ...
965         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
966         ok=$?
967     fi
968
969     if [ $ok -eq 0 ]; then
970         status=1
971         exit 1
972     fi
973
974     return 0
975 }
976
977 # run xfs_check and friends on a FS.
978
979 _check_xfs_filesystem()
980 {
981     if [ $# -ne 3 ]
982     then
983         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
984         exit 1
985     fi
986
987     extra_mount_options=""
988     device=$1
989     if [ "$2" != "none" ]; then
990         extra_log_options="-l$2"
991         extra_mount_options="-ologdev=$2"
992     fi
993
994     if [ "$3" != "none" ]; then
995         extra_rt_options="-r$3"
996         extra_mount_options=$extra_mount_options" -ortdev=$3"
997     fi
998     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
999
1000     [ "$FSTYP" != xfs ] && return 0
1001     testoption=""
1002     [ "$USE_BIG_LOOPFS" = yes ] && testoption=-t
1003
1004     type=`_fs_type $device`
1005     ok=1
1006
1007     if [ "$type" = "xfs" ]
1008     then
1009         # mounted ...
1010         mountpoint=`_umount_or_remount_ro $device`
1011     fi
1012
1013     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1014                 | tee $tmp.logprint | grep -q "<CLEAN>"
1015     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1016     then
1017         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seq.full)"
1018
1019         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$here/$seq.full
1020         echo "*** xfs_logprint -t output ***"                   >>$here/$seq.full
1021         cat $tmp.logprint                                       >>$here/$seq.full
1022         echo "*** end xfs_logprint output"                      >>$here/$seq.full
1023
1024         ok=0
1025     fi
1026
1027     $XFS_CHECK_PROG $testoption $extra_log_options $device 2>&1 |\
1028          _fix_malloc >$tmp.fs_check
1029     if [ -s $tmp.fs_check ]
1030     then
1031         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seq.full)"
1032
1033         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
1034         echo "*** xfs_check output ***"                         >>$here/$seq.full
1035         cat $tmp.fs_check                                       >>$here/$seq.full
1036         echo "*** end xfs_check output"                         >>$here/$seq.full
1037
1038         ok=0
1039     fi
1040     # repair doesn't scale massively at this stage, optionally skip it for now
1041     [ "$USE_BIG_LOOPFS" = yes ] || \
1042     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1043     if [ $? -ne 0 ]
1044     then
1045         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seq.full)"
1046
1047         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
1048         echo "*** xfs_repair -n output ***"                     >>$here/$seq.full
1049         cat $tmp.repair | _fix_malloc                           >>$here/$seq.full
1050         echo "*** end xfs_repair output"                        >>$here/$seq.full
1051
1052         ok=0
1053     fi
1054     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1055
1056     if [ $ok -eq 0 ]
1057     then
1058         echo "*** mount output ***"                             >>$here/$seq.full
1059         _mount                                                  >>$here/$seq.full
1060         echo "*** end mount output"                             >>$here/$seq.full
1061     elif [ "$type" = "xfs" ]
1062     then
1063         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1064     fi
1065
1066     if [ $ok -eq 0 ]; then
1067         status=1
1068         exit 1
1069     fi
1070
1071     return 0
1072 }
1073
1074 # Filter the knowen errors the UDF Verifier reports.
1075 _udf_test_known_error_filter()
1076 {
1077         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."
1078
1079 }
1080
1081 _check_udf_filesystem()
1082 {
1083     [ "$DISABLE_UDF_TEST" == "1" ] && return
1084
1085     if [ $# -ne 1 -a $# -ne 2 ]
1086     then
1087         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1088         exit 1
1089     fi
1090
1091     if [ ! -x $here/src/udf_test ]
1092     then
1093         echo "udf_test not installed, please download and build the Philips"
1094         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1095         echo "Then copy the udf_test binary to $here/src/."
1096         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1097         echo "to 1."
1098         return
1099     fi
1100
1101     device=$1
1102     if [ $# -eq 2 ];
1103     then
1104         LAST_BLOCK=`expr \( $2 - 1 \)`
1105         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1106     fi
1107
1108     rm -f $seq.checkfs
1109     sleep 1 # Due to a problem with time stamps in udf_test
1110     $here/src/udf_test $OPT_ARG $device | tee $here/$seq.checkfs | egrep "Error|Warning" | \
1111         _udf_test_known_error_filter | \
1112         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" | \
1113         sed "s/^.*$/Warning UDF Verifier reported errors see $seq.checkfs./g"
1114
1115 }
1116
1117 _check_xfs_test_fs()
1118 {
1119     TEST_LOG="none"
1120     TEST_RT="none"
1121     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1122         TEST_LOG="$TEST_LOGDEV"
1123
1124     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1125         TEST_RT="$TEST_RTDEV"
1126
1127     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1128
1129     # check for ipath consistency
1130     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1131         # errors go to stderr
1132         xfs_check_ipaths $TEST_DIR >/dev/null
1133         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1134     fi
1135 }
1136
1137 _check_test_fs()
1138 {
1139     case $FSTYP in
1140     xfs)
1141         _check_xfs_test_fs
1142         ;;
1143     nfs)
1144         # no way to check consistency for nfs
1145         ;;
1146     udf)
1147         # do nothing for now
1148         ;;
1149     *)
1150         _check_generic_filesystem $TEST_DEV
1151         ;;
1152     esac
1153 }
1154
1155 _check_scratch_fs()
1156 {
1157     case $FSTYP in
1158     xfs)
1159         SCRATCH_LOG="none"
1160         SCRATCH_RT="none"
1161         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1162             SCRATCH_LOG="$SCRATCH_LOGDEV"
1163
1164         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1165             SCRATCH_RT="$SCRATCH_RTDEV"
1166
1167         _check_xfs_filesystem $SCRATCH_DEV $SCRATCH_LOG $SCRATCH_RT
1168         ;;
1169     udf)
1170         _check_udf_filesystem $SCRATCH_DEV $udf_fsize
1171         ;;
1172     nfs*)
1173         # Don't know how to check an NFS filesystem, yet.
1174         ;;
1175     *)
1176         _check_generic_filesystem $SCRATCH_DEV
1177         ;;
1178     esac
1179 }
1180
1181 _full_fstyp_details()
1182 {
1183      [ -z "$FSTYP" ] && FSTYP=xfs
1184      if [ $FSTYP = xfs ]; then
1185         if [ -d /proc/fs/xfs ]; then
1186             if grep -q 'debug 0' /proc/fs/xfs/stat; then
1187                 FSTYP="$FSTYP (non-debug)"
1188             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
1189                 FSTYP="$FSTYP (debug)"
1190             fi
1191         else
1192             if uname -a | grep -qi 'debug'; then
1193                 FSTYP="$FSTYP (debug)"
1194             else
1195                 FSTYP="$FSTYP (non-debug)"
1196             fi
1197         fi
1198      fi
1199      echo $FSTYP
1200 }
1201
1202 _full_platform_details()
1203 {
1204      os=`uname -s`
1205      host=`hostname -s`
1206      kernel=`uname -r`
1207      platform=`uname -m`
1208      echo "$os/$platform $host $kernel"
1209 }
1210
1211 _setup_udf_scratchdir()
1212 {
1213     [ "$FSTYP" != "udf" ] \
1214         && _fail "setup_udf_testdir: \$FSTYP is not udf"
1215     [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
1216         && _notrun "this test requires a valid \$SCRATCH_DEV"
1217     [ -z "$SCRATCH_MNT" ] \
1218         && _notrun "this test requires a valid \$SCRATCH_MNT"
1219
1220     # mounted?
1221     if _mount | grep -q $SCRATCH_DEV
1222     then
1223         # if it's mounted, make sure its on $TEST_RW_DIR
1224         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1225         then
1226             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1227         fi
1228         $UMOUNT_PROG $SCRATCH_DEV
1229     fi
1230
1231     _scratch_mkfs
1232     _scratch_mount
1233
1234     testdir=$SCRATCH_MNT
1235 }
1236
1237 _setup_nfs_scratchdir()
1238 {
1239     [ "$FSTYP" != "nfs" ] \
1240         && _fail "setup_nfs_testdir: \$FSTYP is not nfs"
1241     [ -z "$SCRATCH_DEV" ] \
1242         && _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
1243     [ -z "$SCRATCH_MNT" ] \
1244         && _notrun "this test requires a valid \$SCRATCH_MNT"
1245
1246     # mounted?
1247     if _mount | grep -q $SCRATCH_DEV
1248     then
1249         # if it's mounted, make sure its on $SCRATCH_MNT
1250         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1251         then
1252             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1253         fi
1254         $UMOUNT_PROG $SCRATCH_DEV
1255     fi
1256
1257     _scratch_mkfs
1258     _scratch_mount
1259
1260     testdir=$SCRATCH_MNT
1261 }
1262
1263 #
1264 # Warning for UDF and NFS:
1265 # this function calls _setup_udf_scratchdir and _setup_udf_scratchdir
1266 # which actually uses the scratch dir for the test dir.
1267 #
1268 # This was done because testdir was intended to be a persistent
1269 # XFS only partition.  This should eventually change, and treat
1270 # at least local filesystems all the same.
1271 #
1272 _setup_testdir()
1273 {
1274     case $FSTYP in
1275     udf)
1276         _setup_udf_scratchdir
1277         ;;
1278     nfs*)
1279         _setup_nfs_scratchdir
1280         ;;
1281     *)
1282         testdir=$TEST_DIR
1283         ;;
1284     esac
1285 }
1286
1287 _cleanup_testdir()
1288 {
1289     case $FSTYP in
1290     udf)
1291         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1292         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1293         ;;
1294     nfs*)
1295         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1296         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1297         ;;
1298     *)
1299         # do nothing, testdir is $TEST_DIR
1300         :
1301         ;;
1302     esac
1303 }
1304
1305 _link_out_file()
1306 {
1307    if [ -z "$1" ]; then
1308       echo Error must pass \$seq.
1309       exit
1310    fi
1311    rm -f $1
1312    if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1313       ln -s $1.irix $1
1314    elif [ "`uname`" == "Linux" ]; then
1315       ln -s $1.linux $1
1316    else
1317       echo Error test $seq does not run on the operating system: `uname`
1318       exit
1319    fi
1320 }
1321
1322 _die()
1323 {
1324         echo $@
1325         exit 1
1326 }
1327
1328 _nfiles()
1329 {
1330         f=0
1331         while [ $f -lt $1 ]
1332         do
1333                 file=f$f
1334                 echo > $file
1335                 if [ $size -gt 0 ]; then
1336                     dd if=/dev/zero of=$file bs=1024 count=$size
1337                 fi
1338                 let f=$f+1
1339         done
1340 }
1341
1342 # takes dirname, depth
1343 _descend()
1344 {
1345         dirname=$1; depth=$2
1346         mkdir $dirname  || die "mkdir $dirname failed"
1347         cd $dirname
1348
1349         _nfiles $files           # files for this dir
1350
1351         [ $depth -eq 0 ] && return
1352         let deep=$depth-1 # go 1 down
1353
1354         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
1355
1356         d=0
1357         while [ $d -lt $dirs ]
1358         do
1359                 _descend d$d $deep &
1360                 let d=$d+1
1361                 wait
1362         done
1363 }
1364
1365 # Populate a filesystem with inodes for performance experiments
1366 #
1367 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size]
1368 #
1369 _populate_fs()
1370 {
1371     here=`pwd`
1372     dirs=5          # ndirs in each subdir till leaves
1373     size=0          # sizeof files in K
1374     files=100       # num files in _each_ subdir
1375     depth=2         # depth of tree from root to leaves
1376     verbose=false
1377     root=root       # path of initial root of directory tree
1378
1379     while getopts "d:f:n:r:s:v" c
1380     do
1381         case $c in
1382         d)      depth=$OPTARG;;
1383         n)      dirs=$OPTARG;;
1384         f)      files=$OPTARG;;
1385         s)      size=$OPTARG;;
1386         v)      verbose=true;;
1387         r)      root=$OPTARG;;
1388         esac
1389     done
1390
1391     _descend $root $depth
1392     wait
1393
1394     cd $here
1395
1396     [ $verbose = true ] && echo done
1397 }
1398
1399 # query whether the given file has the given inode flag set
1400 #
1401 _test_inode_flag()
1402 {
1403     flag=$1
1404     file=$2
1405
1406     if which $XFS_IO_PROG >/dev/null; then
1407         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
1408             return 0
1409         fi
1410     fi
1411     return 1
1412 }
1413
1414 # query the given files extsize allocator hint in bytes (if any)
1415 #
1416 _test_inode_extsz()
1417 {
1418     file=$1
1419     blocks=""
1420
1421     if which $XFS_IO_PROG >/dev/null; then
1422         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
1423                 awk '/^xattr.extsize =/ { print $3 }'`
1424     fi
1425     [ -z "$blocks" ] && blocks="0"
1426     echo $blocks
1427 }
1428
1429
1430 ################################################################################
1431
1432 if [ "$iam" != new -a "$iam" != bench ]
1433 then
1434     # make some further configuration checks here
1435
1436     if [ "$TEST_DEV" = ""  ]
1437     then
1438         echo "common.rc: Error: \$TEST_DEV is not set"
1439         exit 1
1440     fi
1441
1442     # if $TEST_DEV is not mounted, mount it now as XFS
1443     if [ -z "`_fs_type $TEST_DEV`" ]
1444     then
1445         # $TEST_DEV is not mounted
1446         if ! _test_mount
1447         then
1448             echo "common.rc: retrying test device mount with external set"
1449             [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
1450             if ! _test_mount
1451             then
1452                 echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
1453                 exit 1
1454             fi
1455         fi
1456     fi
1457
1458     if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
1459     then
1460         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
1461         $DF_PROG $TEST_DEV
1462         exit 1
1463     fi
1464 fi
1465
1466 # make sure this script returns success
1467 /bin/true