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