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