b5eb58264441bda6bcbaf57c08ec2d5895b9870a
[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 BC=$(which bc 2> /dev/null) || BC=
24
25 _require_math()
26 {
27         if [ -z "$BC" ]; then
28                 _notrun "this test requires 'bc' tool for doing math operations"
29         fi
30 }
31
32 _math()
33 {
34         [ $# -le 0 ] && return
35         if [ "$BC" ]; then
36                 result=$(LANG=C echo "scale=0; $@" | "$BC" -q 2> /dev/null)
37         else
38                 _notrun "this test requires 'bc' tool for doing math operations"
39         fi
40         echo "$result"
41 }
42
43 dd()
44 {
45    if [ "$HOSTOS" == "Linux" ]
46    then 
47         command dd --help | grep noxfer > /dev/null 2>&1
48         
49         if [ "$?" -eq 0 ]
50             then
51                 command dd status=noxfer $@
52             else
53                 command dd $@
54         fi
55    else
56         command dd $@
57    fi
58 }
59
60 _btrfs_get_subvolid()
61 {
62         mnt=$1
63         name=$2
64
65         $BTRFS_UTIL_PROG sub list $mnt | grep $name | awk '{ print $2 }'
66 }
67
68 # Prints the md5 checksum of a given file
69 _md5_checksum()
70 {
71         md5sum $1 | cut -d ' ' -f1
72 }
73
74
75 # ls -l w/ selinux sometimes puts a dot at the end:
76 # -rwxrw-r--. id1 id2 file1
77
78 _ls_l()
79 {
80         ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /"
81 }
82
83 _mount_opts()
84 {
85     # SELinux adds extra xattrs which can mess up our expected output.
86     # So, mount with a context, and they won't be created
87     # nfs_t is a "liberal" context so we can use it.
88     if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
89         SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:nfs_t:s0"
90         export SELINUX_MOUNT_OPTIONS
91     fi
92
93     case $FSTYP in
94     xfs)
95         export MOUNT_OPTIONS=$XFS_MOUNT_OPTIONS
96         ;;
97     udf)
98         export MOUNT_OPTIONS=$UDF_MOUNT_OPTIONS
99         ;;
100     nfs)
101         export MOUNT_OPTIONS=$NFS_MOUNT_OPTIONS
102         ;;
103     ext2|ext3|ext4|ext4dev)
104         # acls & xattrs aren't turned on by default on ext$FOO
105         export MOUNT_OPTIONS="-o acl,user_xattr $EXT_MOUNT_OPTIONS"
106         ;;
107     reiserfs)
108         # acls & xattrs aren't turned on by default on reiserfs
109         export MOUNT_OPTIONS="-o acl,user_xattr $REISERFS_MOUNT_OPTIONS"
110         ;;
111     gfs2)
112         # acls aren't turned on by default on gfs2
113         export MOUNT_OPTIONS="-o acl $GFS2_MOUNT_OPTIONS"
114         ;;
115     *)
116         ;;
117     esac
118 }
119
120 _mkfs_opts()
121 {
122     case $FSTYP in
123     xfs)
124         export MKFS_OPTIONS=$XFS_MKFS_OPTIONS
125         ;;
126     udf)
127         [ ! -z "$udf_fsize" ] && \
128             UDF_MKFS_OPTIONS="$UDF_MKFS_OPTIONS -s $udf_fsize"
129         export MKFS_OPTIONS=$UDF_MKFS_OPTIONS
130         ;;
131     nfs)
132         export MKFS_OPTIONS=$NFS_MKFS_OPTIONS
133         ;;
134     reiserfs)
135         export MKFS_OPTIONS="$REISERFS_MKFS_OPTIONS -q"
136         ;;
137     gfs2)
138         export MKFS_OPTIONS="$GFS2_MKFS_OPTIONS -O -p lock_nolock"
139         ;;
140     jfs)
141         export MKFS_OPTIONS="$JFS_MKFS_OPTIONS -q"
142         ;;
143     *)
144         ;;
145     esac
146 }
147
148 _fsck_opts()
149 {
150     case $FSTYP in
151     ext2|ext3|ext4|ext4dev)
152         export FSCK_OPTIONS="-nf"
153         ;;
154     reiserfs)
155         export FSCK_OPTIONS="--yes"
156         ;;
157     *)
158         export FSCK_OPTIONS="-n"
159         ;;
160     esac
161 }
162
163 [ -z "$FSTYP" ] && FSTYP=xfs
164 [ -z "$MOUNT_OPTIONS" ] && _mount_opts
165 [ -z "$MKFS_OPTIONS" ] && _mkfs_opts
166 [ -z "$FSCK_OPTIONS" ] && _fsck_opts
167
168
169 # we need common/config
170 if [ "$iam" != "check" ]
171 then
172     if ! . ./common/config
173         then
174         echo "$iam: failed to source common/config"
175         exit 1
176     fi
177 fi
178
179 # check for correct setup
180 case "$FSTYP" in
181     xfs)
182          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
183          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
184          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
185          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
186          ;;
187     udf)
188          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
189          ;;
190     btrfs)
191          [ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
192          ;;
193     nfs)
194          ;;
195 esac
196
197 # make sure we have a standard umask
198 umask 022
199
200 _mount()
201 {
202     $MOUNT_PROG `_mount_ops_filter $*`
203 }
204
205 _scratch_options()
206 {
207     type=$1
208     SCRATCH_OPTIONS=""
209
210     if [ "$FSTYP" != "xfs" ]; then
211         return
212     fi
213
214     case $type in
215     mkfs)
216         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
217         rt_opt="-r"
218         log_opt="-l"
219         ;;
220     mount)
221         rt_opt="-o"
222         log_opt="-o"
223         ;;
224     esac
225     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
226         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
227     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
228         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
229 }
230
231 _test_options()
232 {
233     type=$1
234     TEST_OPTIONS=""
235
236     if [ "$FSTYP" != "xfs" ]; then
237         return
238     fi
239
240     case $type in
241     mkfs)
242         rt_opt="-r"
243         log_opt="-l"
244         ;;
245     mount)
246         rt_opt="-o"
247         log_opt="-o"
248         ;;
249     esac
250     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
251         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
252     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
253         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
254 }
255
256 _mount_ops_filter()
257 {
258     params="$*"
259     
260     #get mount point to handle dmapi mtpt option correctly
261     let last_index=$#-1
262     [ $last_index -gt 0 ] && shift $last_index
263     FS_ESCAPED=$1
264     
265     # irix is fussy about how it is fed its mount options
266     # - multiple -o's are not allowed
267     # - no spaces between comma delimitered options
268     # the sed script replaces all -o's (except the first) with a comma
269     # not required for linux, but won't hurt
270     
271     echo $params | sed -e 's/[[:space:]]\+-o[[:space:]]*/UnIqUe/1; s/[[:space:]]\+-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
272         | sed -e 's/dmapi/dmi/' \
273         | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"
274
275 }
276
277 _scratch_mount_options()
278 {
279     _scratch_options mount
280
281     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
282 }
283
284 _scratch_mount()
285 {
286     _mount -t $FSTYP `_scratch_mount_options $*`
287 }
288
289 _scratch_unmount()
290 {
291     $UMOUNT_PROG $SCRATCH_DEV
292 }
293
294 _scratch_remount()
295 {
296     _scratch_unmount
297     _scratch_mount
298 }
299
300 _test_mount()
301 {
302     _test_options mount
303     _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
304 }
305
306 _scratch_mkfs_options()
307 {
308     _scratch_options mkfs
309     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
310 }
311
312
313 _setup_large_xfs_fs()
314 {
315         fs_size=$1
316         local tmp_dir=/tmp/
317
318         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
319         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
320         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
321
322         # calculate the size of the file we need to allocate.
323         # Default free space in the FS is 50GB, but you can specify more via
324         # SCRATCH_DEV_EMPTY_SPACE
325         file_size=$(($fs_size - 50*1024*1024*1024))
326         file_size=$(($file_size - $SCRATCH_DEV_EMPTY_SPACE))
327
328         # mount the filesystem, create the file, unmount it
329         _scratch_mount 2>&1 >$tmp_dir/mnt.err
330         local status=$?
331         if [ $status -ne 0 ]; then
332                 echo "mount failed"
333                 cat $tmp_dir/mnt.err >&2
334                 rm -f $tmp_dir/mnt.err
335                 return $status
336         fi
337         rm -f $tmp_dir/mnt.err
338
339         xfs_io -F -f \
340                 -c "truncate $file_size" \
341                 -c "falloc -k 0 $file_size" \
342                 -c "chattr +d" \
343                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
344         export NUM_SPACE_FILES=1
345         status=$?
346         umount $SCRATCH_MNT
347         if [ $status -ne 0 ]; then
348                 echo "large file prealloc failed"
349                 cat $tmp_dir/mnt.err >&2
350                 return $status
351         fi
352         return 0
353 }
354
355 _scratch_mkfs_xfs()
356 {
357     # extra mkfs options can be added by tests
358     local extra_mkfs_options=$*
359
360     local tmp_dir=/tmp/
361
362     _scratch_options mkfs
363
364     # save mkfs output in case conflict means we need to run again.
365     # only the output for the mkfs that applies should be shown
366     $MKFS_XFS_PROG $SCRATCH_OPTIONS $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
367         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
368     local mkfs_status=$?
369
370     # a mkfs failure may be caused by conflicts between
371     # $MKFS_OPTIONS and $extra_mkfs_options
372
373     if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
374         echo "** mkfs failed with extra mkfs options added to \"$MKFS_OPTIONS\" by test $seq **" \
375             >>$seqres.full
376         echo "** attempting to mkfs using only test $seq options: $extra_mkfs_options **" \
377             >>$seqres.full
378         # running mkfs again. overwrite previous mkfs output files
379         $MKFS_XFS_PROG $SCRATCH_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
380             2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
381         mkfs_status=$?
382     fi
383
384     if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
385         # manually parse the mkfs output to get the fs size in bytes
386         local fs_size
387         fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
388             if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
389                 my $size = $1 * $2;
390                 print STDOUT "$size\n";
391             }'`
392         _setup_large_xfs_fs $fs_size
393         mkfs_status=$?
394     fi
395
396     # output stored mkfs output
397     cat $tmp_dir.mkfserr >&2
398     cat $tmp_dir.mkfsstd
399     rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
400
401     return $mkfs_status
402 }
403
404 # xfs_check script is planned to be deprecated. But, we want to
405 # be able to invoke "xfs_check" behavior in xfstests in order to
406 # maintain the current verification levels.
407 _xfs_check()
408 {
409     OPTS=" "
410     DBOPTS=" "
411     USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
412
413     while getopts "b:fi:l:stvV" c
414     do
415         case $c in
416             s) OPTS=$OPTS"-s ";;
417             t) OPTS=$OPTS"-t ";;
418             v) OPTS=$OPTS"-v ";;
419             i) OPTS=$OPTS"-i "$OPTARG" ";;
420             b) OPTS=$OPTS"-b "$OPTARG" ";;
421             f) DBOPTS=$DBOPTS" -f";;
422             l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
423             V) $XFS_DB_PROG -p xfs_check -V
424                 return $?
425                 ;;
426         esac
427     done
428     set -- extra $@
429     shift $OPTIND
430     case $# in
431         1)    ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
432                status=$?
433                ;;
434         2)    echo $USAGE 1>&1
435               status=2
436               ;;
437     esac
438     return $status
439 }
440
441 _setup_large_ext4_fs()
442 {
443         fs_size=$1
444         local tmp_dir=/tmp/
445
446         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
447         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
448         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
449
450         # Default free space in the FS is 50GB, but you can specify more via
451         # SCRATCH_DEV_EMPTY_SPACE
452         space_to_consume=$(($fs_size - 50*1024*1024*1024 - $SCRATCH_DEV_EMPTY_SPACE))
453
454         # mount the filesystem and create 16TB - 4KB files until we consume
455         # all the necessary space.
456         _scratch_mount 2>&1 >$tmp_dir/mnt.err
457         local status=$?
458         if [ $status -ne 0 ]; then
459                 echo "mount failed"
460                 cat $tmp_dir/mnt.err >&2
461                 rm -f $tmp_dir/mnt.err
462                 return $status
463         fi
464         rm -f $tmp_dir/mnt.err
465
466         file_size=$((16*1024*1024*1024*1024 - 4096))
467         nfiles=0
468         while [ $space_to_consume -gt $file_size ]; do
469
470                 xfs_io -F -f \
471                         -c "truncate $file_size" \
472                         -c "falloc -k 0 $file_size" \
473                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
474                 status=$?
475                 if [ $status -ne 0 ]; then
476                         break;
477                 fi
478
479                 space_to_consume=$(( $space_to_consume - $file_size ))
480                 nfiles=$(($nfiles + 1))
481         done
482
483         # consume the remaining space.
484         if [ $space_to_consume -gt 0 ]; then
485                 xfs_io -F -f \
486                         -c "truncate $space_to_consume" \
487                         -c "falloc -k 0 $space_to_consume" \
488                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
489                 status=$?
490         fi
491         export NUM_SPACE_FILES=$nfiles
492
493         umount $SCRATCH_MNT
494         if [ $status -ne 0 ]; then
495                 echo "large file prealloc failed"
496                 cat $tmp_dir/mnt.err >&2
497                 return $status
498         fi
499         return 0
500 }
501 _scratch_mkfs_ext4()
502 {
503         local tmp_dir=/tmp/
504
505         /sbin/mkfs -t $FSTYP -- -F $MKFS_OPTIONS $* $SCRATCH_DEV \
506                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
507         local mkfs_status=$?
508
509         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
510                 # manually parse the mkfs output to get the fs size in bytes
511                 fs_size=`cat $tmp_dir.mkfsstd | awk ' \
512                         /^Block size/ { split($2, a, "="); bs = a[2] ; } \
513                         / inodes, / { blks = $3 } \
514                         /reserved for the super user/ { resv = $1 } \
515                         END { fssize = bs * blks - resv; print fssize }'`
516
517                 _setup_large_ext4_fs $fs_size
518                 mkfs_status=$?
519         fi
520
521         # output stored mkfs output
522         cat $tmp_dir.mkfserr >&2
523         cat $tmp_dir.mkfsstd
524         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
525
526         return $mkfs_status
527 }
528
529 _scratch_mkfs()
530 {
531     case $FSTYP in
532     xfs)
533         _scratch_mkfs_xfs $*
534         ;;
535     nfs*)
536         # do nothing for nfs
537         ;;
538     udf)
539         $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
540         ;;
541     btrfs)
542         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
543         ;;
544     ext4)
545         _scratch_mkfs_ext4 $*
546         ;;
547     *)
548         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV
549         ;;
550     esac
551 }
552
553 _scratch_pool_mkfs()
554 {
555     case $FSTYP in
556     btrfs)
557         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV_POOL > /dev/null
558         ;;
559     *)
560         echo "_scratch_pool_mkfs is not implemented for $FSTYP" 1>&2
561         ;;
562     esac
563 }
564
565 # Create fs of certain size on scratch device
566 # _scratch_mkfs_sized <size in bytes> [optional blocksize]
567 _scratch_mkfs_sized()
568 {
569     fssize=$1
570     blocksize=$2
571     [ -z "$blocksize" ] && blocksize=4096
572
573     re='^[0-9]+$'
574     if ! [[ $fssize =~ $re ]] ; then
575         _notrun "error: _scratch_mkfs_sized: fs size \"$fssize\" not an integer."
576     fi
577     if ! [[ $blocksize =~ $re ]] ; then
578         _notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
579     fi
580
581     blocks=`expr $fssize / $blocksize`
582
583     if [ "$HOSTOS" == "Linux" ]; then
584         devsize=`blockdev --getsize64 $SCRATCH_DEV`
585         [ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
586     fi
587
588     case $FSTYP in
589     xfs)
590         # don't override MKFS_OPTIONS that set a block size.
591         echo $MKFS_OPTIONS |egrep -q "b?size="
592         if [ $? -eq 0 ]; then
593                 _scratch_mkfs_xfs -d size=$fssize
594         else
595                 _scratch_mkfs_xfs -d size=$fssize -b size=$blocksize
596         fi
597         ;;
598     ext2|ext3|ext4|ext4dev)
599         yes | ${MKFS_PROG}.$FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
600         ;;
601     btrfs)
602         $MKFS_BTRFS_PROG $MKFS_OPTIONS -b $fssize $SCRATCH_DEV
603         ;;
604     *)
605         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
606         ;;
607     esac
608 }
609
610 # Emulate an N-data-disk stripe w/ various stripe units
611 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
612 _scratch_mkfs_geom()
613 {
614     sunit_bytes=$1
615     swidth_mult=$2
616     blocksize=$3
617     [ -z "$blocksize" ] && blocksize=4096
618
619     let sunit_blocks=$sunit_bytes/$blocksize
620     let swidth_blocks=$sunit_blocks*$swidth_mult
621
622     case $FSTYP in
623     xfs)
624         MKFS_OPTIONS+=" -b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
625         ;;
626     ext4|ext4dev)
627         MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
628         ;;
629     *)
630         _notrun "can't mkfs $FSTYP with geometry"
631         ;;
632     esac
633     _scratch_mkfs
634 }
635
636 _scratch_resvblks()
637 {
638         case $FSTYP in
639         xfs)
640                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
641                 ;;
642         *)
643                 ;;
644         esac
645 }
646
647 _scratch_xfs_db_options()
648 {
649     SCRATCH_OPTIONS=""
650     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
651         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
652     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
653 }
654
655 _scratch_xfs_logprint()
656 {
657     SCRATCH_OPTIONS=""
658     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
659         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
660     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
661 }
662
663 _scratch_xfs_check()
664 {
665     SCRATCH_OPTIONS=""
666     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
667         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
668     [ "$LARGE_SCRATCH_DEV" = yes ] && \
669         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
670     _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
671 }
672
673 _scratch_xfs_repair()
674 {
675     SCRATCH_OPTIONS=""
676     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
677         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
678     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
679         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
680     [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
681     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
682 }
683
684 _get_pids_by_name()
685 {
686     if [ $# -ne 1 ]
687     then
688         echo "Usage: _get_pids_by_name process-name" 1>&2
689         exit 1
690     fi
691
692     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
693     # HH:MM:SS before the psargs field, use this as the search anchor.
694     #
695     # Matches with $1 (process-name) occur if the first psarg is $1
696     # or ends in /$1 ... the matching uses sed's regular expressions,
697     # so passing a regex into $1 will work.
698
699     ps $PS_ALL_FLAGS \
700     | sed -n \
701         -e 's/$/ /' \
702         -e 's/[         ][      ]*/ /g' \
703         -e 's/^ //' \
704         -e 's/^[^ ]* //' \
705         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
706         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
707 }
708
709 # fix malloc libs output
710 #
711 _fix_malloc()
712 {
713     # filter out the Electric Fence notice
714     $PERL_PROG -e '
715         while (<>) {
716             if (defined $o && /^\s+Electric Fence/) {
717                 chomp($o);
718                 print "$o";
719                 undef $o;
720                 next;
721             }
722             print $o if (defined $o);
723
724             $o=$_;
725         }
726         print $o if (defined $o);
727     '
728 }
729
730 # check if run as root
731 #
732 _need_to_be_root()
733 {
734     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
735     if [ "$id" -ne 0 ]
736     then
737         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
738         exit 1
739     fi
740 }
741
742
743 #
744 # _df_device : get an IRIX style df line for a given device
745 #
746 #       - returns "" if not mounted
747 #       - returns fs type in field two (ala IRIX)
748 #       - joins line together if split by fancy df formatting
749 #       - strips header etc
750 #
751
752 _df_device()
753 {
754     if [ $# -ne 1 ]
755     then
756         echo "Usage: _df_device device" 1>&2
757         exit 1
758     fi
759
760     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
761         match($1,what) && NF==1 {
762             v=$1
763             getline
764             print v, $0
765             exit
766         }
767         match($1,what) {
768             print
769             exit
770         }
771     '
772 }
773
774 #
775 # _df_dir : get an IRIX style df line for device where a directory resides
776 #
777 #       - returns fs type in field two (ala IRIX)
778 #       - joins line together if split by fancy df formatting
779 #       - strips header etc
780 #
781
782 _df_dir()
783 {
784     if [ $# -ne 1 ]
785     then
786         echo "Usage: _df_dir device" 1>&2
787         exit 1
788     fi
789
790     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
791         NR == 2 && NF==1 {
792             v=$1
793             getline
794             print v, $0;
795             exit 0
796         }
797         NR == 2 {
798             print;
799             exit 0
800         }
801         {}
802     '
803     # otherwise, nada
804 }
805
806 # return percentage used disk space for mounted device
807
808 _used()
809 {
810     if [ $# -ne 1 ]
811     then
812         echo "Usage: _used device" 1>&2
813         exit 1
814     fi
815
816     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
817 }
818
819 # return the FS type of a mounted device
820 #
821 _fs_type()
822 {
823     if [ $# -ne 1 ]
824     then
825         echo "Usage: _fs_type device" 1>&2
826         exit 1
827     fi
828
829     #
830     # The Linux kernel shows NFSv4 filesystems in df output as
831     # filesystem type nfs4, although we mounted it as nfs earlier.
832     # Fix the filesystem type up here so that the callers don't
833     # have to bother with this quirk.
834     #
835     _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
836 }
837
838 # return the FS mount options of a mounted device
839 #
840 # should write a version which just parses the output of mount for IRIX
841 # compatibility, but since this isn't used at all, at the moment I'll leave
842 # this for now
843 #
844 _fs_options()
845 {
846     if [ $# -ne 1 ]
847     then
848         echo "Usage: _fs_options device" 1>&2
849         exit 1
850     fi
851
852     $AWK_PROG -v dev=$1 '
853         match($1,dev) { print $4 }
854     ' </proc/mounts
855 }
856
857 # returns device number if a file is a block device
858 #
859 _is_block_dev()
860 {
861     if [ $# -ne 1 ]
862     then
863         echo "Usage: _is_block_dev dev" 1>&2
864         exit 1
865     fi
866
867     _dev=$1
868     if [ -L "${_dev}" ]; then
869         _dev=`readlink -f ${_dev}`
870     fi
871
872     if [ -b "${_dev}" ]; then
873         src/lstat64 ${_dev} | $AWK_PROG '/Device type:/ { print $9 }'
874     fi
875 }
876
877 # Do a command, log it to $seqres.full, optionally test return status
878 # and die if command fails. If called with one argument _do executes the
879 # command, logs it, and returns its exit status. With two arguments _do
880 # first prints the message passed in the first argument, and then "done"
881 # or "fail" depending on the return status of the command passed in the
882 # second argument. If the command fails and the variable _do_die_on_error
883 # is set to "always" or the two argument form is used and _do_die_on_error
884 # is set to "message_only" _do will print an error message to
885 # $seqres.out and exit.
886
887 _do()
888 {
889     if [ $# -eq 1 ]; then
890         _cmd=$1
891     elif [ $# -eq 2 ]; then
892         _note=$1
893         _cmd=$2
894         echo -n "$_note... "
895     else
896         echo "Usage: _do [note] cmd" 1>&2
897         status=1; exit
898     fi
899
900     (eval "echo '---' \"$_cmd\"") >>$seqres.full
901     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
902     cat $tmp._out | _fix_malloc >>$seqres.full
903     if [ $# -eq 2 ]; then
904         if [ $ret -eq 0 ]; then
905             echo "done"
906         else
907             echo "fail"
908         fi
909     fi
910     if [ $ret -ne 0  ] \
911         && [ "$_do_die_on_error" = "always" \
912             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
913     then
914         [ $# -ne 2 ] && echo
915         eval "echo \"$_cmd\" failed \(returned $ret\): see $seqres.full"
916         status=1; exit
917     fi
918
919     return $ret
920 }
921
922 # bail out, setting up .notrun file
923 #
924 _notrun()
925 {
926     echo "$*" > $seqres.notrun
927     echo "$seq not run: $*"
928     status=0
929     exit
930 }
931
932 # just plain bail out
933 #
934 _fail()
935 {
936     echo "$*" | tee -a $seqres.full
937     echo "(see $seqres.full for details)"
938     status=1
939     exit 1
940 }
941
942 # tests whether $FSTYP is one of the supported filesystems for a test
943 #
944 _supported_fs()
945 {
946     for f
947     do
948         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
949         then
950             return
951         fi
952     done
953
954     _notrun "not suitable for this filesystem type: $FSTYP"
955 }
956
957 # tests whether $FSTYP is one of the supported OSes for a test
958 #
959 _supported_os()
960 {
961     for h
962     do
963         if [ "$h" = "$HOSTOS" ]
964         then
965             return
966         fi
967     done
968
969     _notrun "not suitable for this OS: $HOSTOS"
970 }
971
972 # this test needs a scratch partition - check we're ok & unmount it
973 #
974 _require_scratch()
975 {
976     case "$FSTYP" in
977         nfs*)
978                  echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
979                  if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]
980                  then
981                      _notrun "this test requires a valid \$SCRATCH_DEV"
982                  fi
983                  ;;
984         *)
985                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
986                  then
987                      _notrun "this test requires a valid \$SCRATCH_DEV"
988                  fi
989                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
990                  then
991                      _notrun "this test requires a valid \$SCRATCH_DEV"
992                  fi
993                 if [ ! -d "$SCRATCH_MNT" ]
994                 then
995                      _notrun "this test requires a valid \$SCRATCH_MNT"
996                 fi
997                  ;;
998     esac
999
1000     # mounted?
1001     if _mount | grep -q $SCRATCH_DEV
1002     then
1003         # if it's mounted, make sure its on $SCRATCH_MNT
1004         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1005         then
1006             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1007             exit 1
1008         fi
1009         # and then unmount it
1010         if ! $UMOUNT_PROG $SCRATCH_DEV
1011         then
1012             echo "failed to unmount $SCRATCH_DEV"
1013             exit 1
1014         fi
1015     fi
1016 }
1017
1018 # this test needs a logdev
1019 #
1020 _require_logdev()
1021 {
1022     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
1023         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
1024     [ "$USE_EXTERNAL" != yes ] && \
1025         _notrun "This test requires USE_EXTERNAL to be enabled"
1026
1027     # ensure its not mounted
1028     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
1029 }
1030
1031 # this test requires loopback device support
1032 #
1033 _require_loop()
1034 {
1035     if [ "$HOSTOS" != "Linux" ]
1036     then
1037         _notrun "This test requires linux for loopback device support"
1038     fi
1039
1040     modprobe loop >/dev/null 2>&1
1041     if grep loop /proc/devices >/dev/null 2>&1
1042     then
1043         :
1044     else
1045         _notrun "This test requires loopback device support"
1046     fi
1047 }
1048
1049 # this test requires ext2 filesystem support
1050 #
1051 _require_ext2()
1052 {
1053     if [ "$HOSTOS" != "Linux" ]
1054     then
1055         _notrun "This test requires linux for ext2 filesystem support"
1056     fi
1057
1058     modprobe ext2 >/dev/null 2>&1
1059     if grep ext2 /proc/filesystems >/dev/null 2>&1
1060     then
1061         :
1062     else
1063         _notrun "This test requires ext2 filesystem support"
1064     fi
1065 }
1066
1067 # this test requires that (large) loopback device files are not in use
1068 #
1069 _require_no_large_scratch_dev()
1070 {
1071     [ "$LARGE_SCRATCH_DEV" = yes ] && \
1072         _notrun "Large filesystem testing in progress, skipped this test"
1073 }
1074
1075 # this test requires that a realtime subvolume is in use, and
1076 # that the kernel supports realtime as well.
1077 #
1078 _require_realtime()
1079 {
1080     [ "$USE_EXTERNAL" = yes ] || \
1081         _notrun "External volumes not in use, skipped this test"
1082     [ "$SCRATCH_RTDEV" = "" ] && \
1083         _notrun "Realtime device required, skipped this test"
1084 }
1085
1086 # this test requires that a specified command (executable) exists
1087 # $1 - command, $2 - name for error message
1088 #
1089 _require_command()
1090 {
1091     [ -n "$1" ] && _cmd="$1" || _cmd="$2"
1092     [ -n "$1" -a -x "$1" ] || _notrun "$_cmd utility required, skipped this test"
1093 }
1094
1095 # this test requires the device mapper flakey target
1096 #
1097 _require_dm_flakey()
1098 {
1099     _require_command $DMSETUP_PROG
1100
1101     modprobe dm-flakey >/dev/null 2>&1
1102     $DMSETUP_PROG targets | grep flakey >/dev/null 2>&1
1103     if [ $? -eq 0 ]
1104     then
1105         :
1106     else
1107         _notrun "This test requires dm flakey support"
1108     fi
1109 }
1110
1111 # this test requires the projid32bit feature to be available in
1112 # mkfs.xfs
1113 #
1114 _require_projid32bit()
1115 {
1116         _scratch_mkfs_xfs -f -i projid32bit=0 2>&1 >/dev/null \
1117            || _notrun "mkfs.xfs doesn't have projid32bit feature"
1118 }
1119
1120 # this test requires that external log/realtime devices are not in use
1121 #
1122 _require_nonexternal()
1123 {
1124     [ "$USE_EXTERNAL" = yes ] && \
1125         _notrun "External device testing in progress, skipped this test"
1126 }
1127
1128 # this test requires that a (specified) aio-dio executable exists
1129 # $1 - command (optional)
1130 #
1131 _require_aiodio()
1132 {
1133     if [ -z "$1" ]
1134     then
1135         AIO_TEST=src/aio-dio-regress/aiodio_sparse2
1136         [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
1137     else
1138         AIO_TEST=src/aio-dio-regress/$1
1139         [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
1140     fi
1141 }
1142
1143 # run an aio-dio program
1144 # $1 - command
1145 _run_aiodio()
1146 {
1147     if [ -z "$1" ]
1148     then
1149         echo "usage: _run_aiodio command_name" 2>&1
1150         status=1; exit 1
1151     fi
1152
1153     _require_aiodio $1
1154
1155     local testtemp=$TEST_DIR/aio-testfile
1156     rm -f $testtemp
1157     $AIO_TEST $testtemp 2>&1
1158     status=$?
1159     rm -f $testtemp
1160
1161     return $status
1162 }
1163
1164 # indicate whether YP/NIS is active or not
1165 #
1166 _yp_active()
1167 {
1168         local dn
1169         dn=$(domainname 2>/dev/null)
1170         test -n "${dn}" -a "${dn}" != "(none)"
1171         echo $?
1172 }
1173
1174 # cat the password file
1175 #
1176 _cat_passwd()
1177 {
1178         [ $(_yp_active) -eq 0 ] && ypcat passwd
1179         cat /etc/passwd
1180 }
1181
1182 # cat the group file
1183 #
1184 _cat_group()
1185 {
1186         [ $(_yp_active) -eq 0 ] && ypcat group
1187         cat /etc/group
1188 }
1189
1190 # check for the fsgqa user on the machine
1191 #
1192 _require_user()
1193 {
1194     qa_user=fsgqa
1195     _cat_passwd | grep -q $qa_user
1196     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
1197     echo /bin/true | su $qa_user
1198     [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
1199 }
1200
1201 # check for the fsgqa group on the machine
1202 #
1203 _require_group()
1204 {
1205     qa_group=fsgqa
1206     _cat_group | grep -q $qa_group
1207     [ "$?" == "0" ] || _notrun "$qa_group user not defined."
1208 }
1209
1210 _filter_user_do()
1211 {
1212         perl -ne "
1213 s,.*Permission\sdenied.*,Permission denied,;
1214 s,.*no\saccess\sto\stty.*,,;
1215 s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
1216 s,^\s*$,,;
1217         print;"
1218 }
1219
1220 _user_do()
1221 {
1222     if [ "$HOSTOS" == "IRIX" ]
1223         then
1224         echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
1225     else
1226         echo $1 | su $qa_user 2>&1 | _filter_user_do
1227     fi
1228 }
1229
1230 # check that xfs_io, kernel, and filesystem all support zero
1231 _require_xfs_io_zero()
1232 {
1233         testio=`$XFS_IO_PROG -c "zero help" 2>&1`
1234         echo $testio | grep -q 'command "zero" not found' && \
1235                 _notrun "zero command not supported"
1236 }
1237
1238 # check that xfs_io, glibc, kernel, and filesystem all (!) support
1239 # fallocate
1240 #
1241 _require_xfs_io_falloc()
1242 {
1243         testfile=$TEST_DIR/$$.falloc
1244         testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $testfile 2>&1`
1245         rm -f $testfile 2>&1 > /dev/null
1246         echo $testio | grep -q "not found" && \
1247                 _notrun "xfs_io fallocate support is missing"
1248         echo $testio | grep -q "Operation not supported" && \
1249                 _notrun "xfs_io fallocate command failed (old kernel/wrong fs?)"
1250 }
1251
1252 # check that xfs_io, kernel and filesystem all support fallocate with hole
1253 # punching
1254 _require_xfs_io_falloc_punch()
1255 {
1256         testfile=$TEST_DIR/$$.falloc
1257         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1258                 -c "fpunch 4k 8k" $testfile 2>&1`
1259         rm -f $testfile 2>&1 > /dev/null
1260         echo $testio | grep -q "not found" && \
1261                 _notrun "xfs_io fallocate punch support is missing"
1262         echo $testio | grep -q "Operation not supported" && \
1263                 _notrun "xfs_io fallocate punch command failed (no fs support?)"
1264 }
1265
1266 # check that xfs_io, kernel and filesystem support fiemap
1267 _require_xfs_io_fiemap()
1268 {
1269         testfile=$TEST_DIR/$$.fiemap
1270         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1271                 -c "fiemap -v" $testfile 2>&1`
1272         rm -f $testfile 2>&1 > /dev/null
1273         echo $testio | grep -q "not found" && \
1274                 _notrun "xfs_io fiemap support is missing"
1275         echo $testio | grep -q "Operation not supported" && \
1276                 _notrun "xfs_io fiemap command failed (no fs support?)"
1277 }
1278
1279 # Check that a fs has enough free space (in 1024b blocks)
1280 #
1281 _require_fs_space()
1282 {
1283         MNT=$1
1284         BLOCKS=$2       # in units of 1024
1285         let GB=$BLOCKS/1024/1024
1286
1287         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
1288         [ $FREE_BLOCKS -lt $BLOCKS ] && \
1289                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
1290 }
1291
1292 #
1293 # Check if the filesystem supports sparse files.
1294 #
1295 # Unfortunately there is no better way to do this than a manual black list.
1296 #
1297 _require_sparse_files()
1298 {
1299     case $FSTYP in
1300     hfsplus)
1301         _notrun "Sparse files not supported by this filesystem type: $FSTYP"
1302         ;;
1303     *)
1304         ;;
1305     esac
1306 }
1307
1308 _require_debugfs()
1309 {
1310     #boot_params always present in debugfs
1311     [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
1312 }
1313
1314 _require_fail_make_request()
1315 {
1316     [ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
1317         || _notrun "$DEBUGFS_MNT/fail_make_request \
1318  not found. Seems that CONFIG_FAIL_MAKE_REQUEST kernel config option not enabled"
1319 }
1320
1321 #
1322 # Check if the file system supports seek_data/hole
1323 #
1324 _require_seek_data_hole()
1325 {
1326     testfile=$TEST_DIR/$$.seek
1327     testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`
1328     rm -f $testfile &>/dev/null
1329     echo $testseek | grep -q "Kernel does not support" && \
1330         _notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
1331 }
1332
1333 # check that a FS on a device is mounted
1334 # if so, return mount point
1335 #
1336 _is_mounted()
1337 {
1338     if [ $# -ne 1 ]
1339     then
1340         echo "Usage: _is_mounted device" 1>&2
1341         exit 1
1342     fi
1343
1344     device=$1
1345
1346     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
1347         pattern        { print $3 ; exit 0 }
1348         END            { exit 1 }
1349     '
1350     then
1351         echo "_is_mounted: $device is not a mounted $FSTYP FS"
1352         exit 1
1353     fi
1354 }
1355
1356 # remount a FS to a new mode (ro or rw)
1357 #
1358 _remount()
1359 {
1360     if [ $# -ne 2 ]
1361     then
1362         echo "Usage: _remount device ro/rw" 1>&2
1363         exit 1
1364     fi
1365     device=$1
1366     mode=$2
1367
1368     if ! mount -o remount,$mode $device
1369     then
1370         echo "_remount: failed to remount filesystem on $device as $mode"
1371         exit 1
1372     fi
1373 }
1374
1375 # Run the appropriate repair/check on a filesystem
1376 #
1377 # if the filesystem is mounted, it's either remounted ro before being
1378 # checked or it's unmounted and then remounted
1379 #
1380
1381 # If set, we remount ro instead of unmounting for fsck
1382 USE_REMOUNT=0
1383
1384 _umount_or_remount_ro()
1385 {
1386     if [ $# -ne 1 ]
1387     then
1388         echo "Usage: _umount_or_remount_ro <device>" 1>&2
1389         exit 1
1390     fi
1391
1392     device=$1
1393     mountpoint=`_is_mounted $device`
1394
1395     if [ $USE_REMOUNT -eq 0 ]; then
1396         $UMOUNT_PROG $device
1397     else
1398         _remount $device ro
1399     fi
1400     echo "$mountpoint"
1401 }
1402
1403 _mount_or_remount_rw()
1404 {
1405     if [ $# -ne 3 ]
1406     then
1407         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
1408         exit 1
1409     fi
1410     mount_opts=$1
1411     device=$2
1412     mountpoint=$3
1413
1414     if [ $USE_REMOUNT -eq 0 ]
1415     then
1416         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
1417         then
1418             echo "!!! failed to remount $device on $mountpoint"
1419             return 0 # ok=0
1420         fi
1421     else
1422         _remount $device rw
1423     fi
1424
1425     return 1 # ok=1
1426 }
1427
1428 # Check a generic filesystem in no-op mode; this assumes that the
1429 # underlying fsck program accepts "-n" for a no-op (check-only) run,
1430 # and that it will still return an errno for corruption in this mode.
1431 #
1432 # Filesystems which don't support this will need to define their
1433 # own check routine.
1434 #
1435 _check_generic_filesystem()
1436 {
1437     device=$1
1438
1439     # If type is set, we're mounted
1440     type=`_fs_type $device`
1441     ok=1
1442
1443     if [ "$type" = "$FSTYP" ]
1444     then
1445         # mounted ...
1446         mountpoint=`_umount_or_remount_ro $device`
1447     fi
1448
1449     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
1450     if [ $? -ne 0 ]
1451     then
1452         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1453
1454         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
1455         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1456         cat $tmp.fsck                           >>$seqres.full
1457         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1458
1459         ok=0
1460     fi
1461     rm -f $tmp.fsck
1462
1463     if [ $ok -eq 0 ]
1464     then
1465         echo "*** mount output ***"             >>$seqres.full
1466         _mount                                  >>$seqres.full
1467         echo "*** end mount output"             >>$seqres.full
1468     elif [ "$type" = "$FSTYP" ]
1469     then
1470         # was mounted ...
1471         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1472         ok=$?
1473     fi
1474
1475     if [ $ok -eq 0 ]; then
1476         status=1
1477         exit 1
1478     fi
1479
1480     return 0
1481 }
1482
1483 # run xfs_check and friends on a FS.
1484
1485 _check_xfs_filesystem()
1486 {
1487     if [ $# -ne 3 ]
1488     then
1489         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
1490         exit 1
1491     fi
1492
1493     extra_mount_options=""
1494     device=$1
1495     if [ "$2" != "none" ]; then
1496         extra_log_options="-l$2"
1497         extra_mount_options="-ologdev=$2"
1498     fi
1499
1500     if [ "$3" != "none" ]; then
1501         extra_rt_options="-r$3"
1502         extra_mount_options=$extra_mount_options" -ortdev=$3"
1503     fi
1504     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
1505
1506     [ "$FSTYP" != xfs ] && return 0
1507
1508     type=`_fs_type $device`
1509     ok=1
1510
1511     if [ "$type" = "xfs" ]
1512     then
1513         # mounted ...
1514         mountpoint=`_umount_or_remount_ro $device`
1515     fi
1516
1517     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1518                 | tee $tmp.logprint | grep -q "<CLEAN>"
1519     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1520     then
1521         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
1522
1523         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
1524         echo "*** xfs_logprint -t output ***"   >>$seqres.full
1525         cat $tmp.logprint                       >>$seqres.full
1526         echo "*** end xfs_logprint output"      >>$seqres.full
1527
1528         ok=0
1529     fi
1530
1531     # xfs_check runs out of memory on large files, so even providing the test
1532     # option (-t) to avoid indexing the free space trees doesn't make it pass on
1533     # large filesystems. Avoid it.
1534     if [ "$LARGE_SCRATCH_DEV" != yes ]; then
1535             _xfs_check $extra_log_options $device 2>&1 |\
1536                  _fix_malloc >$tmp.fs_check
1537     fi
1538     if [ -s $tmp.fs_check ]
1539     then
1540         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
1541
1542         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1543         echo "*** xfs_check output ***"         >>$seqres.full
1544         cat $tmp.fs_check                       >>$seqres.full
1545         echo "*** end xfs_check output"         >>$seqres.full
1546
1547         ok=0
1548     fi
1549
1550     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1551     if [ $? -ne 0 ]
1552     then
1553         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
1554
1555         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1556         echo "*** xfs_repair -n output ***"     >>$seqres.full
1557         cat $tmp.repair | _fix_malloc           >>$seqres.full
1558         echo "*** end xfs_repair output"        >>$seqres.full
1559
1560         ok=0
1561     fi
1562     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1563
1564     if [ $ok -eq 0 ]
1565     then
1566         echo "*** mount output ***"             >>$seqres.full
1567         _mount                                  >>$seqres.full
1568         echo "*** end mount output"             >>$seqres.full
1569     elif [ "$type" = "xfs" ]
1570     then
1571         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1572     fi
1573
1574     if [ $ok -eq 0 ]; then
1575         status=1
1576         exit 1
1577     fi
1578
1579     return 0
1580 }
1581
1582 # Filter the knowen errors the UDF Verifier reports.
1583 _udf_test_known_error_filter()
1584 {
1585         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."
1586
1587 }
1588
1589 _check_udf_filesystem()
1590 {
1591     [ "$DISABLE_UDF_TEST" == "1" ] && return
1592
1593     if [ $# -ne 1 -a $# -ne 2 ]
1594     then
1595         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1596         exit 1
1597     fi
1598
1599     if [ ! -x $here/src/udf_test ]
1600     then
1601         echo "udf_test not installed, please download and build the Philips"
1602         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1603         echo "Then copy the udf_test binary to $here/src/."
1604         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1605         echo "to 1."
1606         return
1607     fi
1608
1609     device=$1
1610     if [ $# -eq 2 ];
1611     then
1612         LAST_BLOCK=`expr \( $2 - 1 \)`
1613         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1614     fi
1615
1616     rm -f $seqres.checkfs
1617     sleep 1 # Due to a problem with time stamps in udf_test
1618     $here/src/udf_test $OPT_ARG $device | tee $seqres.checkfs | egrep "Error|Warning" | \
1619         _udf_test_known_error_filter | \
1620         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" | \
1621         sed "s/^.*$/Warning UDF Verifier reported errors see $seqres.checkfs./g"
1622
1623 }
1624
1625 _check_xfs_test_fs()
1626 {
1627     TEST_LOG="none"
1628     TEST_RT="none"
1629     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1630         TEST_LOG="$TEST_LOGDEV"
1631
1632     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1633         TEST_RT="$TEST_RTDEV"
1634
1635     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1636
1637     # check for ipath consistency
1638     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1639         # errors go to stderr
1640         xfs_check_ipaths $TEST_DIR >/dev/null
1641         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1642     fi
1643 }
1644
1645 _check_btrfs_filesystem()
1646 {
1647     device=$1
1648
1649     # If type is set, we're mounted
1650     type=`_fs_type $device`
1651     ok=1
1652
1653     if [ "$type" = "$FSTYP" ]
1654     then
1655         # mounted ...
1656         mountpoint=`_umount_or_remount_ro $device`
1657     fi
1658
1659     btrfsck $device >$tmp.fsck 2>&1
1660     if [ $? -ne 0 ]
1661     then
1662         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1663
1664         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1665         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1666         cat $tmp.fsck                           >>$seqres.full
1667         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1668
1669         ok=0
1670     fi
1671     rm -f $tmp.fsck
1672
1673     if [ $ok -eq 0 ]
1674     then
1675         echo "*** mount output ***"             >>$seqres.full
1676         _mount                                  >>$seqres.full
1677         echo "*** end mount output"             >>$seqres.full
1678     elif [ "$type" = "$FSTYP" ]
1679     then
1680         # was mounted ...
1681         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1682         ok=$?
1683     fi
1684
1685     if [ $ok -eq 0 ]; then
1686         status=1
1687         exit 1
1688     fi
1689
1690     return 0
1691 }
1692
1693 _check_test_fs()
1694 {
1695     case $FSTYP in
1696     xfs)
1697         _check_xfs_test_fs
1698         ;;
1699     nfs)
1700         # no way to check consistency for nfs
1701         ;;
1702     udf)
1703         # do nothing for now
1704         ;;
1705     btrfs)
1706         _check_btrfs_filesystem $TEST_DEV
1707         ;;
1708     *)
1709         _check_generic_filesystem $TEST_DEV
1710         ;;
1711     esac
1712 }
1713
1714 _check_scratch_fs()
1715 {
1716     device=$SCRATCH_DEV
1717     [ $# -eq 1 ] && device=$1
1718
1719     case $FSTYP in
1720     xfs)
1721         SCRATCH_LOG="none"
1722         SCRATCH_RT="none"
1723         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1724             SCRATCH_LOG="$SCRATCH_LOGDEV"
1725
1726         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1727             SCRATCH_RT="$SCRATCH_RTDEV"
1728
1729         _check_xfs_filesystem $device $SCRATCH_LOG $SCRATCH_RT
1730         ;;
1731     udf)
1732         _check_udf_filesystem $device $udf_fsize
1733         ;;
1734     nfs*)
1735         # Don't know how to check an NFS filesystem, yet.
1736         ;;
1737     btrfs)
1738         _check_btrfs_filesystem $device
1739         ;;
1740     *)
1741         _check_generic_filesystem $device
1742         ;;
1743     esac
1744 }
1745
1746 _full_fstyp_details()
1747 {
1748      [ -z "$FSTYP" ] && FSTYP=xfs
1749      if [ $FSTYP = xfs ]; then
1750         if [ -d /proc/fs/xfs ]; then
1751             if grep -q 'debug 0' /proc/fs/xfs/stat; then
1752                 FSTYP="$FSTYP (non-debug)"
1753             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
1754                 FSTYP="$FSTYP (debug)"
1755             fi
1756         else
1757             if uname -a | grep -qi 'debug'; then
1758                 FSTYP="$FSTYP (debug)"
1759             else
1760                 FSTYP="$FSTYP (non-debug)"
1761             fi
1762         fi
1763      fi
1764      echo $FSTYP
1765 }
1766
1767 _full_platform_details()
1768 {
1769      os=`uname -s`
1770      host=`hostname -s`
1771      kernel=`uname -r`
1772      platform=`uname -m`
1773      echo "$os/$platform $host $kernel"
1774 }
1775
1776 _setup_udf_scratchdir()
1777 {
1778     [ "$FSTYP" != "udf" ] \
1779         && _fail "setup_udf_testdir: \$FSTYP is not udf"
1780     [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
1781         && _notrun "this test requires a valid \$SCRATCH_DEV"
1782     [ -z "$SCRATCH_MNT" ] \
1783         && _notrun "this test requires a valid \$SCRATCH_MNT"
1784
1785     # mounted?
1786     if _mount | grep -q $SCRATCH_DEV
1787     then
1788         # if it's mounted, make sure its on $TEST_RW_DIR
1789         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1790         then
1791             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1792         fi
1793         $UMOUNT_PROG $SCRATCH_DEV
1794     fi
1795
1796     _scratch_mkfs
1797     _scratch_mount
1798
1799     testdir=$SCRATCH_MNT
1800 }
1801
1802 _setup_nfs_scratchdir()
1803 {
1804     [ "$FSTYP" != "nfs" ] \
1805         && _fail "setup_nfs_testdir: \$FSTYP is not nfs"
1806     [ -z "$SCRATCH_DEV" ] \
1807         && _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
1808     [ -z "$SCRATCH_MNT" ] \
1809         && _notrun "this test requires a valid \$SCRATCH_MNT"
1810
1811     # mounted?
1812     if _mount | grep -q $SCRATCH_DEV
1813     then
1814         # if it's mounted, make sure its on $SCRATCH_MNT
1815         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1816         then
1817             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1818         fi
1819         $UMOUNT_PROG $SCRATCH_DEV
1820     fi
1821
1822     _scratch_mkfs
1823     _scratch_mount
1824
1825     testdir=$SCRATCH_MNT
1826 }
1827
1828 #
1829 # Warning for UDF and NFS:
1830 # this function calls _setup_udf_scratchdir and _setup_udf_scratchdir
1831 # which actually uses the scratch dir for the test dir.
1832 #
1833 # This was done because testdir was intended to be a persistent
1834 # XFS only partition.  This should eventually change, and treat
1835 # at least local filesystems all the same.
1836 #
1837 _setup_testdir()
1838 {
1839     case $FSTYP in
1840     udf)
1841         _setup_udf_scratchdir
1842         ;;
1843     nfs*)
1844         _setup_nfs_scratchdir
1845         ;;
1846     *)
1847         testdir=$TEST_DIR
1848         ;;
1849     esac
1850 }
1851
1852 _cleanup_testdir()
1853 {
1854     case $FSTYP in
1855     udf)
1856         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1857         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1858         ;;
1859     nfs*)
1860         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1861         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1862         ;;
1863     *)
1864         # do nothing, testdir is $TEST_DIR
1865         :
1866         ;;
1867     esac
1868 }
1869
1870 _link_out_file()
1871 {
1872         if [ -z "$1" -o -z "$2" ]; then
1873                 echo Error must pass src and dst.
1874                 exit
1875         fi
1876         rm -f $2
1877         if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1878                 ln -s $1.irix $2
1879         elif [ "`uname`" == "Linux" ]; then
1880                 ln -s $1.linux $2
1881         else
1882                 echo Error test $seq does not run on the operating system: `uname`
1883                 exit
1884         fi
1885 }
1886
1887 _die()
1888 {
1889         echo $@
1890         exit 1
1891 }
1892
1893 #takes files, randomdata
1894 _nfiles()
1895 {
1896         f=0
1897         while [ $f -lt $1 ]
1898         do
1899                 file=f$f
1900                 echo > $file
1901                 if [ $size -gt 0 ]; then
1902                     if [ "$2" == "false" ]; then
1903                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
1904                     else
1905                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
1906                     fi
1907                 fi
1908                 let f=$f+1
1909         done
1910 }
1911
1912 # takes dirname, depth, randomdata
1913 _descend()
1914 {
1915         dirname=$1; depth=$2; randomdata=$3
1916         mkdir $dirname  || die "mkdir $dirname failed"
1917         cd $dirname
1918
1919         _nfiles $files $randomdata          # files for this dir and data type
1920
1921         [ $depth -eq 0 ] && return
1922         let deep=$depth-1 # go 1 down
1923
1924         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
1925
1926         d=0
1927         while [ $d -lt $dirs ]
1928         do
1929                 _descend d$d $deep &
1930                 let d=$d+1
1931                 wait
1932         done
1933 }
1934
1935 # Populate a filesystem with inodes for performance experiments
1936 #
1937 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
1938 #
1939 _populate_fs()
1940 {
1941     here=`pwd`
1942     dirs=5          # ndirs in each subdir till leaves
1943     size=0          # sizeof files in K
1944     files=100       # num files in _each_ subdir
1945     depth=2         # depth of tree from root to leaves
1946     verbose=false
1947     root=root       # path of initial root of directory tree
1948     randomdata=false # -x data type urandom or zero
1949
1950     OPTIND=1
1951     while getopts "d:f:n:r:s:v:x" c
1952     do
1953         case $c in
1954         d)      depth=$OPTARG;;
1955         n)      dirs=$OPTARG;;
1956         f)      files=$OPTARG;;
1957         s)      size=$OPTARG;;
1958         v)      verbose=true;;
1959         r)      root=$OPTARG;;
1960         x)      randomdata=true;;
1961         esac
1962     done
1963
1964     _descend $root $depth $randomdata
1965     wait
1966
1967     cd $here
1968
1969     [ $verbose = true ] && echo done
1970 }
1971
1972 # query whether the given file has the given inode flag set
1973 #
1974 _test_inode_flag()
1975 {
1976     flag=$1
1977     file=$2
1978
1979     if which $XFS_IO_PROG >/dev/null; then
1980         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
1981             return 0
1982         fi
1983     fi
1984     return 1
1985 }
1986
1987 # query the given files extsize allocator hint in bytes (if any)
1988 #
1989 _test_inode_extsz()
1990 {
1991     file=$1
1992     blocks=""
1993
1994     if which $XFS_IO_PROG >/dev/null; then
1995         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
1996                 awk '/^xattr.extsize =/ { print $3 }'`
1997     fi
1998     [ -z "$blocks" ] && blocks="0"
1999     echo $blocks
2000 }
2001
2002 # scratch_dev_pool should contain the disks pool for the btrfs raid
2003 _require_scratch_dev_pool()
2004 {
2005         local i
2006         if [ -z "$SCRATCH_DEV_POOL" ]; then
2007                 _notrun "this test requires a valid \$SCRATCH_DEV_POOL"
2008         fi
2009
2010         # btrfs test case needs 2 or more scratch_dev_pool; other FS not sure
2011         # so fail it
2012         case $FSTYP in
2013         btrfs)
2014                 if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt 2 ]; then
2015                         _notrun "btrfs and this test needs 2 or more disks in SCRATCH_DEV_POOL"
2016                 fi
2017         ;;
2018         *)
2019                 _notrun "dev_pool is not supported by fstype \"$FSTYP\""
2020         ;;
2021         esac
2022
2023         for i in $SCRATCH_DEV_POOL; do
2024                 if [ "`_is_block_dev $i`" = "" ]; then
2025                         _notrun "this test requires valid block disk $i"
2026                 fi
2027                 if [ "`_is_block_dev $i`" = "`_is_block_dev $TEST_DEV`" ]; then
2028                         _notrun "$i is part of TEST_DEV, this test requires unique disks"
2029                 fi
2030                 if _mount | grep -q $i; then
2031                         if ! $UMOUNT_PROG $i; then
2032                             echo "failed to unmount $i - aborting"
2033                             exit 1
2034                         fi
2035                 fi
2036                 # to help better debug when something fails, we remove
2037                 # traces of previous btrfs FS on the dev.
2038                 dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
2039         done
2040 }
2041
2042 # We will check if the device is virtual (eg: loop device) since it does not
2043 # have the delete entry-point. Otherwise SCSI and USB devices are fine. 
2044 _require_deletable_scratch_dev_pool()
2045 {
2046         local i
2047         local x
2048         for i in $SCRATCH_DEV_POOL; do
2049                 x=`echo $i | cut -d"/" -f 3`
2050                 ls -l /sys/class/block/${x} | grep -q "virtual" 
2051                 if [ $? == "0" ]; then
2052                         _notrun "$i is a virtual device which is not deletable"
2053                 fi
2054         done
2055 }
2056
2057 # We check for btrfs and (optionally) features of the btrfs command
2058 _require_btrfs()
2059 {
2060         cmd=$1
2061         _require_command $BTRFS_UTIL_PROG btrfs
2062         if [ -z "$1" ]; then
2063                 return 1;
2064         fi
2065         $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
2066         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
2067 }
2068
2069 # Check that fio is present, and it is able to execute given jobfile
2070 _require_fio()
2071 {
2072         job=$1
2073
2074         _require_command $FIO_PROG
2075         if [ -z "$1" ]; then
2076                 return 1;
2077         fi
2078
2079         $FIO_PROG --warnings-fatal --showcmd $job >> $seqres.full 2>&1
2080         [ $? -eq 0 ] || _notrun "$FIO_PROG too old, see $seqres.full"
2081 }
2082
2083 # Does freeze work on this fs?
2084 _require_freeze()
2085 {
2086         xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
2087         result=$? 
2088         xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
2089         [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
2090 }
2091
2092 # arg 1 is dev to remove and is output of the below eg.
2093 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2094 _devmgt_remove()
2095 {
2096         local lun=$1
2097         local disk=$2
2098
2099         echo 1 > /sys/class/scsi_device/${lun}/device/delete || _fail "Remove disk failed"
2100
2101         stat $disk > /dev/null 2>&1
2102         while [ $? -eq 0 ]; do
2103                 sleep 1
2104                 stat $disk > /dev/null 2>&1
2105         done
2106 }
2107
2108 # arg 1 is dev to add and is output of the below eg.
2109 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2110 _devmgt_add()
2111 {
2112         local h
2113         local tdl
2114         # arg 1 will be in h:t:d:l format now in the h and "t d l" format
2115         h=`echo ${1} | cut -d":" -f 1`
2116         tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`
2117
2118         echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"
2119 }
2120
2121 _require_fstrim()
2122 {
2123         if [ -z "$FSTRIM_PROG" ]; then
2124                 _notrun "This test requires fstrim utility."
2125         fi
2126 }
2127
2128 _test_batched_discard()
2129 {
2130         if [ $# -ne 1 ]; then
2131                 echo "Usage: _test_batched_discard mnt_point" 1>&2
2132                 exit 1
2133         fi
2134         _require_fstrim
2135         $FSTRIM_PROG ${1} &>/dev/null
2136 }
2137
2138 _require_dumpe2fs()
2139 {
2140         if [ -z "$DUMPE2FS_PROG" ]; then
2141                 _notrun "This test requires dumpe2fs utility."
2142         fi
2143 }
2144
2145 _create_loop_device()
2146 {
2147         file=$1
2148         dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"
2149         echo $dev
2150 }
2151
2152 _destroy_loop_device()
2153 {
2154         dev=$1
2155         losetup -d $dev || _fail "Cannot destroy loop device $dev"
2156 }
2157
2158 _scale_fsstress_args()
2159 {
2160     args=""
2161     while [ $# -gt 0 ]; do
2162         case "$1" in
2163             -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
2164             -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
2165             *) args="$args $1" ;;
2166         esac
2167         shift
2168     done
2169     echo $args
2170 }
2171
2172 run_check()
2173 {
2174         echo "# $@" >> $seqres.full 2>&1
2175         "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
2176 }
2177
2178 init_rc()
2179 {
2180         if [ "$iam" == new ]
2181         then
2182                 return
2183         fi
2184         # make some further configuration checks here
2185         if [ "$TEST_DEV" = ""  ]
2186         then
2187                 echo "common/rc: Error: \$TEST_DEV is not set"
2188                 exit 1
2189         fi
2190
2191         # if $TEST_DEV is not mounted, mount it now as XFS
2192         if [ -z "`_fs_type $TEST_DEV`" ]
2193         then
2194                 # $TEST_DEV is not mounted
2195                 if ! _test_mount
2196                 then
2197                         echo "common/rc: retrying test device mount with external set"
2198                         [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
2199                         if ! _test_mount
2200                         then
2201                                 echo "common/rc: could not mount $TEST_DEV on $TEST_DIR"
2202                                 exit 1
2203                         fi
2204                 fi
2205         fi
2206
2207         if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
2208         then
2209                 echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
2210                 $DF_PROG $TEST_DEV
2211                 exit 1
2212         fi
2213         # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
2214         xfs_io -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
2215         export XFS_IO_PROG="$XFS_IO_PROG -F"
2216 }
2217
2218 init_rc
2219
2220 ################################################################################
2221 # make sure this script returns success
2222 /bin/true