xfstests: Obey mkfs options for sized filesystems on XFS
[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     blocks=`expr $fssize / $blocksize`
573
574     if [ "$HOSTOS" == "Linux" ]; then
575         devsize=`blockdev --getsize64 $SCRATCH_DEV`
576         [ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
577     fi
578
579     case $FSTYP in
580     xfs)
581         # don't override MKFS_OPTIONS that set a block size.
582         echo $MKFS_OPTIONS |egrep -q "b?size="
583         if [ $? -eq 0 ]; then
584                 _scratch_mkfs_xfs -d size=$fssize
585         else
586                 _scratch_mkfs_xfs -d size=$fssize -b size=$blocksize
587         fi
588         ;;
589     ext2|ext3|ext4|ext4dev)
590         yes | ${MKFS_PROG}.$FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
591         ;;
592     btrfs)
593         $MKFS_BTRFS_PROG $MKFS_OPTIONS -b $fssize $SCRATCH_DEV
594         ;;
595     *)
596         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
597         ;;
598     esac
599 }
600
601 # Emulate an N-data-disk stripe w/ various stripe units
602 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
603 _scratch_mkfs_geom()
604 {
605     sunit_bytes=$1
606     swidth_mult=$2
607     blocksize=$3
608     [ -z "$blocksize" ] && blocksize=4096
609
610     let sunit_blocks=$sunit_bytes/$blocksize
611     let swidth_blocks=$sunit_blocks*$swidth_mult
612
613     case $FSTYP in
614     xfs)
615         MKFS_OPTIONS+=" -b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
616         ;;
617     ext4|ext4dev)
618         MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
619         ;;
620     *)
621         _notrun "can't mkfs $FSTYP with geometry"
622         ;;
623     esac
624     _scratch_mkfs
625 }
626
627 _scratch_resvblks()
628 {
629         case $FSTYP in
630         xfs)
631                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
632                 ;;
633         *)
634                 ;;
635         esac
636 }
637
638 _scratch_xfs_db_options()
639 {
640     SCRATCH_OPTIONS=""
641     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
642         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
643     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
644 }
645
646 _scratch_xfs_logprint()
647 {
648     SCRATCH_OPTIONS=""
649     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
650         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
651     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
652 }
653
654 _scratch_xfs_check()
655 {
656     SCRATCH_OPTIONS=""
657     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
658         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
659     [ "$LARGE_SCRATCH_DEV" = yes ] && \
660         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
661     _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
662 }
663
664 _scratch_xfs_repair()
665 {
666     SCRATCH_OPTIONS=""
667     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
668         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
669     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
670         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
671     [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
672     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
673 }
674
675 _get_pids_by_name()
676 {
677     if [ $# -ne 1 ]
678     then
679         echo "Usage: _get_pids_by_name process-name" 1>&2
680         exit 1
681     fi
682
683     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
684     # HH:MM:SS before the psargs field, use this as the search anchor.
685     #
686     # Matches with $1 (process-name) occur if the first psarg is $1
687     # or ends in /$1 ... the matching uses sed's regular expressions,
688     # so passing a regex into $1 will work.
689
690     ps $PS_ALL_FLAGS \
691     | sed -n \
692         -e 's/$/ /' \
693         -e 's/[         ][      ]*/ /g' \
694         -e 's/^ //' \
695         -e 's/^[^ ]* //' \
696         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
697         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
698 }
699
700 # fix malloc libs output
701 #
702 _fix_malloc()
703 {
704     # filter out the Electric Fence notice
705     $PERL_PROG -e '
706         while (<>) {
707             if (defined $o && /^\s+Electric Fence/) {
708                 chomp($o);
709                 print "$o";
710                 undef $o;
711                 next;
712             }
713             print $o if (defined $o);
714
715             $o=$_;
716         }
717         print $o if (defined $o);
718     '
719 }
720
721 # check if run as root
722 #
723 _need_to_be_root()
724 {
725     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
726     if [ "$id" -ne 0 ]
727     then
728         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
729         exit 1
730     fi
731 }
732
733
734 #
735 # _df_device : get an IRIX style df line for a given device
736 #
737 #       - returns "" if not mounted
738 #       - returns fs type in field two (ala IRIX)
739 #       - joins line together if split by fancy df formatting
740 #       - strips header etc
741 #
742
743 _df_device()
744 {
745     if [ $# -ne 1 ]
746     then
747         echo "Usage: _df_device device" 1>&2
748         exit 1
749     fi
750
751     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
752         match($1,what) && NF==1 {
753             v=$1
754             getline
755             print v, $0
756             exit
757         }
758         match($1,what) {
759             print
760             exit
761         }
762     '
763 }
764
765 #
766 # _df_dir : get an IRIX style df line for device where a directory resides
767 #
768 #       - returns fs type in field two (ala IRIX)
769 #       - joins line together if split by fancy df formatting
770 #       - strips header etc
771 #
772
773 _df_dir()
774 {
775     if [ $# -ne 1 ]
776     then
777         echo "Usage: _df_dir device" 1>&2
778         exit 1
779     fi
780
781     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
782         NR == 2 && NF==1 {
783             v=$1
784             getline
785             print v, $0;
786             exit 0
787         }
788         NR == 2 {
789             print;
790             exit 0
791         }
792         {}
793     '
794     # otherwise, nada
795 }
796
797 # return percentage used disk space for mounted device
798
799 _used()
800 {
801     if [ $# -ne 1 ]
802     then
803         echo "Usage: _used device" 1>&2
804         exit 1
805     fi
806
807     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
808 }
809
810 # return the FS type of a mounted device
811 #
812 _fs_type()
813 {
814     if [ $# -ne 1 ]
815     then
816         echo "Usage: _fs_type device" 1>&2
817         exit 1
818     fi
819
820     #
821     # The Linux kernel shows NFSv4 filesystems in df output as
822     # filesystem type nfs4, although we mounted it as nfs earlier.
823     # Fix the filesystem type up here so that the callers don't
824     # have to bother with this quirk.
825     #
826     _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
827 }
828
829 # return the FS mount options of a mounted device
830 #
831 # should write a version which just parses the output of mount for IRIX
832 # compatibility, but since this isn't used at all, at the moment I'll leave
833 # this for now
834 #
835 _fs_options()
836 {
837     if [ $# -ne 1 ]
838     then
839         echo "Usage: _fs_options device" 1>&2
840         exit 1
841     fi
842
843     $AWK_PROG -v dev=$1 '
844         match($1,dev) { print $4 }
845     ' </proc/mounts
846 }
847
848 # returns device number if a file is a block device
849 #
850 _is_block_dev()
851 {
852     if [ $# -ne 1 ]
853     then
854         echo "Usage: _is_block_dev dev" 1>&2
855         exit 1
856     fi
857
858     _dev=$1
859     if [ -L "${_dev}" ]; then
860         _dev=`readlink -f ${_dev}`
861     fi
862
863     if [ -b "${_dev}" ]; then
864         src/lstat64 ${_dev} | $AWK_PROG '/Device type:/ { print $9 }'
865     fi
866 }
867
868 # Do a command, log it to $seqres.full, optionally test return status
869 # and die if command fails. If called with one argument _do executes the
870 # command, logs it, and returns its exit status. With two arguments _do
871 # first prints the message passed in the first argument, and then "done"
872 # or "fail" depending on the return status of the command passed in the
873 # second argument. If the command fails and the variable _do_die_on_error
874 # is set to "always" or the two argument form is used and _do_die_on_error
875 # is set to "message_only" _do will print an error message to
876 # $seqres.out and exit.
877
878 _do()
879 {
880     if [ $# -eq 1 ]; then
881         _cmd=$1
882     elif [ $# -eq 2 ]; then
883         _note=$1
884         _cmd=$2
885         echo -n "$_note... "
886     else
887         echo "Usage: _do [note] cmd" 1>&2
888         status=1; exit
889     fi
890
891     (eval "echo '---' \"$_cmd\"") >>$seqres.full
892     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
893     cat $tmp._out | _fix_malloc >>$seqres.full
894     if [ $# -eq 2 ]; then
895         if [ $ret -eq 0 ]; then
896             echo "done"
897         else
898             echo "fail"
899         fi
900     fi
901     if [ $ret -ne 0  ] \
902         && [ "$_do_die_on_error" = "always" \
903             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
904     then
905         [ $# -ne 2 ] && echo
906         eval "echo \"$_cmd\" failed \(returned $ret\): see $seqres.full"
907         status=1; exit
908     fi
909
910     return $ret
911 }
912
913 # bail out, setting up .notrun file
914 #
915 _notrun()
916 {
917     echo "$*" > $seqres.notrun
918     echo "$seq not run: $*"
919     status=0
920     exit
921 }
922
923 # just plain bail out
924 #
925 _fail()
926 {
927     echo "$*" | tee -a $seqres.full
928     echo "(see $seqres.full for details)"
929     status=1
930     exit 1
931 }
932
933 # tests whether $FSTYP is one of the supported filesystems for a test
934 #
935 _supported_fs()
936 {
937     for f
938     do
939         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
940         then
941             return
942         fi
943     done
944
945     _notrun "not suitable for this filesystem type: $FSTYP"
946 }
947
948 # tests whether $FSTYP is one of the supported OSes for a test
949 #
950 _supported_os()
951 {
952     for h
953     do
954         if [ "$h" = "$HOSTOS" ]
955         then
956             return
957         fi
958     done
959
960     _notrun "not suitable for this OS: $HOSTOS"
961 }
962
963 # this test needs a scratch partition - check we're ok & unmount it
964 #
965 _require_scratch()
966 {
967     case "$FSTYP" in
968         nfs*)
969                  echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
970                  if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]
971                  then
972                      _notrun "this test requires a valid \$SCRATCH_DEV"
973                  fi
974                  ;;
975         *)
976                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
977                  then
978                      _notrun "this test requires a valid \$SCRATCH_DEV"
979                  fi
980                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
981                  then
982                      _notrun "this test requires a valid \$SCRATCH_DEV"
983                  fi
984                 if [ ! -d "$SCRATCH_MNT" ]
985                 then
986                      _notrun "this test requires a valid \$SCRATCH_MNT"
987                 fi
988                  ;;
989     esac
990
991     # mounted?
992     if _mount | grep -q $SCRATCH_DEV
993     then
994         # if it's mounted, make sure its on $SCRATCH_MNT
995         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
996         then
997             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
998             exit 1
999         fi
1000         # and then unmount it
1001         if ! $UMOUNT_PROG $SCRATCH_DEV
1002         then
1003             echo "failed to unmount $SCRATCH_DEV"
1004             exit 1
1005         fi
1006     fi
1007 }
1008
1009 # this test needs a logdev
1010 #
1011 _require_logdev()
1012 {
1013     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
1014         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
1015     [ "$USE_EXTERNAL" != yes ] && \
1016         _notrun "This test requires USE_EXTERNAL to be enabled"
1017
1018     # ensure its not mounted
1019     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
1020 }
1021
1022 # this test requires loopback device support
1023 #
1024 _require_loop()
1025 {
1026     if [ "$HOSTOS" != "Linux" ]
1027     then
1028         _notrun "This test requires linux for loopback device support"
1029     fi
1030
1031     modprobe loop >/dev/null 2>&1
1032     if grep loop /proc/devices >/dev/null 2>&1
1033     then
1034         :
1035     else
1036         _notrun "This test requires loopback device support"
1037     fi
1038 }
1039
1040 # this test requires ext2 filesystem support
1041 #
1042 _require_ext2()
1043 {
1044     if [ "$HOSTOS" != "Linux" ]
1045     then
1046         _notrun "This test requires linux for ext2 filesystem support"
1047     fi
1048
1049     modprobe ext2 >/dev/null 2>&1
1050     if grep ext2 /proc/filesystems >/dev/null 2>&1
1051     then
1052         :
1053     else
1054         _notrun "This test requires ext2 filesystem support"
1055     fi
1056 }
1057
1058 # this test requires that (large) loopback device files are not in use
1059 #
1060 _require_no_large_scratch_dev()
1061 {
1062     [ "$LARGE_SCRATCH_DEV" = yes ] && \
1063         _notrun "Large filesystem testing in progress, skipped this test"
1064 }
1065
1066 # this test requires that a realtime subvolume is in use, and
1067 # that the kernel supports realtime as well.
1068 #
1069 _require_realtime()
1070 {
1071     [ "$USE_EXTERNAL" = yes ] || \
1072         _notrun "External volumes not in use, skipped this test"
1073     [ "$SCRATCH_RTDEV" = "" ] && \
1074         _notrun "Realtime device required, skipped this test"
1075 }
1076
1077 # this test requires that a specified command (executable) exists
1078 # $1 - command, $2 - name for error message
1079 #
1080 _require_command()
1081 {
1082     [ -n "$1" ] && _cmd="$1" || _cmd="$2"
1083     [ -n "$1" -a -x "$1" ] || _notrun "$_cmd utility required, skipped this test"
1084 }
1085
1086 # this test requires the device mapper flakey target
1087 #
1088 _require_dm_flakey()
1089 {
1090     _require_command $DMSETUP_PROG
1091
1092     modprobe dm-flakey >/dev/null 2>&1
1093     $DMSETUP_PROG targets | grep flakey >/dev/null 2>&1
1094     if [ $? -eq 0 ]
1095     then
1096         :
1097     else
1098         _notrun "This test requires dm flakey support"
1099     fi
1100 }
1101
1102 # this test requires the projid32bit feature to be available in
1103 # mkfs.xfs
1104 #
1105 _require_projid32bit()
1106 {
1107         _scratch_mkfs_xfs -f -i projid32bit=0 2>&1 >/dev/null \
1108            || _notrun "mkfs.xfs doesn't have projid32bit feature"
1109 }
1110
1111 # this test requires that external log/realtime devices are not in use
1112 #
1113 _require_nonexternal()
1114 {
1115     [ "$USE_EXTERNAL" = yes ] && \
1116         _notrun "External device testing in progress, skipped this test"
1117 }
1118
1119 # this test requires that a (specified) aio-dio executable exists
1120 # $1 - command (optional)
1121 #
1122 _require_aiodio()
1123 {
1124     if [ -z "$1" ]
1125     then
1126         AIO_TEST=src/aio-dio-regress/aiodio_sparse2
1127         [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
1128     else
1129         AIO_TEST=src/aio-dio-regress/$1
1130         [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
1131     fi
1132 }
1133
1134 # run an aio-dio program
1135 # $1 - command
1136 _run_aiodio()
1137 {
1138     if [ -z "$1" ]
1139     then
1140         echo "usage: _run_aiodio command_name" 2>&1
1141         status=1; exit 1
1142     fi
1143
1144     _require_aiodio $1
1145
1146     local testtemp=$TEST_DIR/aio-testfile
1147     rm -f $testtemp
1148     $AIO_TEST $testtemp 2>&1
1149     status=$?
1150     rm -f $testtemp
1151
1152     return $status
1153 }
1154
1155 # indicate whether YP/NIS is active or not
1156 #
1157 _yp_active()
1158 {
1159         local dn
1160         dn=$(domainname 2>/dev/null)
1161         test -n "${dn}" -a "${dn}" != "(none)"
1162         echo $?
1163 }
1164
1165 # cat the password file
1166 #
1167 _cat_passwd()
1168 {
1169         [ $(_yp_active) -eq 0 ] && ypcat passwd
1170         cat /etc/passwd
1171 }
1172
1173 # cat the group file
1174 #
1175 _cat_group()
1176 {
1177         [ $(_yp_active) -eq 0 ] && ypcat group
1178         cat /etc/group
1179 }
1180
1181 # check for the fsgqa user on the machine
1182 #
1183 _require_user()
1184 {
1185     qa_user=fsgqa
1186     _cat_passwd | grep -q $qa_user
1187     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
1188     echo /bin/true | su $qa_user
1189     [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
1190 }
1191
1192 # check for the fsgqa group on the machine
1193 #
1194 _require_group()
1195 {
1196     qa_group=fsgqa
1197     _cat_group | grep -q $qa_group
1198     [ "$?" == "0" ] || _notrun "$qa_group user not defined."
1199 }
1200
1201 _filter_user_do()
1202 {
1203         perl -ne "
1204 s,.*Permission\sdenied.*,Permission denied,;
1205 s,.*no\saccess\sto\stty.*,,;
1206 s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
1207 s,^\s*$,,;
1208         print;"
1209 }
1210
1211 _user_do()
1212 {
1213     if [ "$HOSTOS" == "IRIX" ]
1214         then
1215         echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
1216     else
1217         echo $1 | su $qa_user 2>&1 | _filter_user_do
1218     fi
1219 }
1220
1221 # check that xfs_io, kernel, and filesystem all support zero
1222 _require_xfs_io_zero()
1223 {
1224         testio=`$XFS_IO_PROG -c "zero help" 2>&1`
1225         echo $testio | grep -q 'command "zero" not found' && \
1226                 _notrun "zero command not supported"
1227 }
1228
1229 # check that xfs_io, glibc, kernel, and filesystem all (!) support
1230 # fallocate
1231 #
1232 _require_xfs_io_falloc()
1233 {
1234         testfile=$TEST_DIR/$$.falloc
1235         testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $testfile 2>&1`
1236         rm -f $testfile 2>&1 > /dev/null
1237         echo $testio | grep -q "not found" && \
1238                 _notrun "xfs_io fallocate support is missing"
1239         echo $testio | grep -q "Operation not supported" && \
1240                 _notrun "xfs_io fallocate command failed (old kernel/wrong fs?)"
1241 }
1242
1243 # check that xfs_io, kernel and filesystem all support fallocate with hole
1244 # punching
1245 _require_xfs_io_falloc_punch()
1246 {
1247         testfile=$TEST_DIR/$$.falloc
1248         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1249                 -c "fpunch 4k 8k" $testfile 2>&1`
1250         rm -f $testfile 2>&1 > /dev/null
1251         echo $testio | grep -q "not found" && \
1252                 _notrun "xfs_io fallocate punch support is missing"
1253         echo $testio | grep -q "Operation not supported" && \
1254                 _notrun "xfs_io fallocate punch command failed (no fs support?)"
1255 }
1256
1257 # check that xfs_io, kernel and filesystem support fiemap
1258 _require_xfs_io_fiemap()
1259 {
1260         testfile=$TEST_DIR/$$.fiemap
1261         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1262                 -c "fiemap -v" $testfile 2>&1`
1263         rm -f $testfile 2>&1 > /dev/null
1264         echo $testio | grep -q "not found" && \
1265                 _notrun "xfs_io fiemap support is missing"
1266         echo $testio | grep -q "Operation not supported" && \
1267                 _notrun "xfs_io fiemap command failed (no fs support?)"
1268 }
1269
1270 # Check that a fs has enough free space (in 1024b blocks)
1271 #
1272 _require_fs_space()
1273 {
1274         MNT=$1
1275         BLOCKS=$2       # in units of 1024
1276         let GB=$BLOCKS/1024/1024
1277
1278         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
1279         [ $FREE_BLOCKS -lt $BLOCKS ] && \
1280                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
1281 }
1282
1283 #
1284 # Check if the filesystem supports sparse files.
1285 #
1286 # Unfortunately there is no better way to do this than a manual black list.
1287 #
1288 _require_sparse_files()
1289 {
1290     case $FSTYP in
1291     hfsplus)
1292         _notrun "Sparse files not supported by this filesystem type: $FSTYP"
1293         ;;
1294     *)
1295         ;;
1296     esac
1297 }
1298
1299 _require_debugfs()
1300 {
1301     #boot_params always present in debugfs
1302     [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
1303 }
1304
1305 _require_fail_make_request()
1306 {
1307     [ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
1308         || _notrun "$DEBUGFS_MNT/fail_make_request \
1309  not found. Seems that CONFIG_FAIL_MAKE_REQUEST kernel config option not enabled"
1310 }
1311
1312 #
1313 # Check if the file system supports seek_data/hole
1314 #
1315 _require_seek_data_hole()
1316 {
1317     testfile=$TEST_DIR/$$.seek
1318     testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`
1319     rm -f $testfile &>/dev/null
1320     echo $testseek | grep -q "Kernel does not support" && \
1321         _notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
1322 }
1323
1324 # check that a FS on a device is mounted
1325 # if so, return mount point
1326 #
1327 _is_mounted()
1328 {
1329     if [ $# -ne 1 ]
1330     then
1331         echo "Usage: _is_mounted device" 1>&2
1332         exit 1
1333     fi
1334
1335     device=$1
1336
1337     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
1338         pattern        { print $3 ; exit 0 }
1339         END            { exit 1 }
1340     '
1341     then
1342         echo "_is_mounted: $device is not a mounted $FSTYP FS"
1343         exit 1
1344     fi
1345 }
1346
1347 # remount a FS to a new mode (ro or rw)
1348 #
1349 _remount()
1350 {
1351     if [ $# -ne 2 ]
1352     then
1353         echo "Usage: _remount device ro/rw" 1>&2
1354         exit 1
1355     fi
1356     device=$1
1357     mode=$2
1358
1359     if ! mount -o remount,$mode $device
1360     then
1361         echo "_remount: failed to remount filesystem on $device as $mode"
1362         exit 1
1363     fi
1364 }
1365
1366 # Run the appropriate repair/check on a filesystem
1367 #
1368 # if the filesystem is mounted, it's either remounted ro before being
1369 # checked or it's unmounted and then remounted
1370 #
1371
1372 # If set, we remount ro instead of unmounting for fsck
1373 USE_REMOUNT=0
1374
1375 _umount_or_remount_ro()
1376 {
1377     if [ $# -ne 1 ]
1378     then
1379         echo "Usage: _umount_or_remount_ro <device>" 1>&2
1380         exit 1
1381     fi
1382
1383     device=$1
1384     mountpoint=`_is_mounted $device`
1385
1386     if [ $USE_REMOUNT -eq 0 ]; then
1387         $UMOUNT_PROG $device
1388     else
1389         _remount $device ro
1390     fi
1391     echo "$mountpoint"
1392 }
1393
1394 _mount_or_remount_rw()
1395 {
1396     if [ $# -ne 3 ]
1397     then
1398         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
1399         exit 1
1400     fi
1401     mount_opts=$1
1402     device=$2
1403     mountpoint=$3
1404
1405     if [ $USE_REMOUNT -eq 0 ]
1406     then
1407         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
1408         then
1409             echo "!!! failed to remount $device on $mountpoint"
1410             return 0 # ok=0
1411         fi
1412     else
1413         _remount $device rw
1414     fi
1415
1416     return 1 # ok=1
1417 }
1418
1419 # Check a generic filesystem in no-op mode; this assumes that the
1420 # underlying fsck program accepts "-n" for a no-op (check-only) run,
1421 # and that it will still return an errno for corruption in this mode.
1422 #
1423 # Filesystems which don't support this will need to define their
1424 # own check routine.
1425 #
1426 _check_generic_filesystem()
1427 {
1428     device=$1
1429
1430     # If type is set, we're mounted
1431     type=`_fs_type $device`
1432     ok=1
1433
1434     if [ "$type" = "$FSTYP" ]
1435     then
1436         # mounted ...
1437         mountpoint=`_umount_or_remount_ro $device`
1438     fi
1439
1440     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
1441     if [ $? -ne 0 ]
1442     then
1443         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1444
1445         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
1446         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1447         cat $tmp.fsck                           >>$seqres.full
1448         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1449
1450         ok=0
1451     fi
1452     rm -f $tmp.fsck
1453
1454     if [ $ok -eq 0 ]
1455     then
1456         echo "*** mount output ***"             >>$seqres.full
1457         _mount                                  >>$seqres.full
1458         echo "*** end mount output"             >>$seqres.full
1459     elif [ "$type" = "$FSTYP" ]
1460     then
1461         # was mounted ...
1462         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1463         ok=$?
1464     fi
1465
1466     if [ $ok -eq 0 ]; then
1467         status=1
1468         exit 1
1469     fi
1470
1471     return 0
1472 }
1473
1474 # run xfs_check and friends on a FS.
1475
1476 _check_xfs_filesystem()
1477 {
1478     if [ $# -ne 3 ]
1479     then
1480         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
1481         exit 1
1482     fi
1483
1484     extra_mount_options=""
1485     device=$1
1486     if [ "$2" != "none" ]; then
1487         extra_log_options="-l$2"
1488         extra_mount_options="-ologdev=$2"
1489     fi
1490
1491     if [ "$3" != "none" ]; then
1492         extra_rt_options="-r$3"
1493         extra_mount_options=$extra_mount_options" -ortdev=$3"
1494     fi
1495     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
1496
1497     [ "$FSTYP" != xfs ] && return 0
1498
1499     type=`_fs_type $device`
1500     ok=1
1501
1502     if [ "$type" = "xfs" ]
1503     then
1504         # mounted ...
1505         mountpoint=`_umount_or_remount_ro $device`
1506     fi
1507
1508     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1509                 | tee $tmp.logprint | grep -q "<CLEAN>"
1510     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1511     then
1512         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
1513
1514         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
1515         echo "*** xfs_logprint -t output ***"   >>$seqres.full
1516         cat $tmp.logprint                       >>$seqres.full
1517         echo "*** end xfs_logprint output"      >>$seqres.full
1518
1519         ok=0
1520     fi
1521
1522     # xfs_check runs out of memory on large files, so even providing the test
1523     # option (-t) to avoid indexing the free space trees doesn't make it pass on
1524     # large filesystems. Avoid it.
1525     if [ "$LARGE_SCRATCH_DEV" != yes ]; then
1526             _xfs_check $extra_log_options $device 2>&1 |\
1527                  _fix_malloc >$tmp.fs_check
1528     fi
1529     if [ -s $tmp.fs_check ]
1530     then
1531         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
1532
1533         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1534         echo "*** xfs_check output ***"         >>$seqres.full
1535         cat $tmp.fs_check                       >>$seqres.full
1536         echo "*** end xfs_check output"         >>$seqres.full
1537
1538         ok=0
1539     fi
1540
1541     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1542     if [ $? -ne 0 ]
1543     then
1544         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
1545
1546         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1547         echo "*** xfs_repair -n output ***"     >>$seqres.full
1548         cat $tmp.repair | _fix_malloc           >>$seqres.full
1549         echo "*** end xfs_repair output"        >>$seqres.full
1550
1551         ok=0
1552     fi
1553     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1554
1555     if [ $ok -eq 0 ]
1556     then
1557         echo "*** mount output ***"             >>$seqres.full
1558         _mount                                  >>$seqres.full
1559         echo "*** end mount output"             >>$seqres.full
1560     elif [ "$type" = "xfs" ]
1561     then
1562         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1563     fi
1564
1565     if [ $ok -eq 0 ]; then
1566         status=1
1567         exit 1
1568     fi
1569
1570     return 0
1571 }
1572
1573 # Filter the knowen errors the UDF Verifier reports.
1574 _udf_test_known_error_filter()
1575 {
1576         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."
1577
1578 }
1579
1580 _check_udf_filesystem()
1581 {
1582     [ "$DISABLE_UDF_TEST" == "1" ] && return
1583
1584     if [ $# -ne 1 -a $# -ne 2 ]
1585     then
1586         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1587         exit 1
1588     fi
1589
1590     if [ ! -x $here/src/udf_test ]
1591     then
1592         echo "udf_test not installed, please download and build the Philips"
1593         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1594         echo "Then copy the udf_test binary to $here/src/."
1595         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1596         echo "to 1."
1597         return
1598     fi
1599
1600     device=$1
1601     if [ $# -eq 2 ];
1602     then
1603         LAST_BLOCK=`expr \( $2 - 1 \)`
1604         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1605     fi
1606
1607     rm -f $seqres.checkfs
1608     sleep 1 # Due to a problem with time stamps in udf_test
1609     $here/src/udf_test $OPT_ARG $device | tee $seqres.checkfs | egrep "Error|Warning" | \
1610         _udf_test_known_error_filter | \
1611         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" | \
1612         sed "s/^.*$/Warning UDF Verifier reported errors see $seqres.checkfs./g"
1613
1614 }
1615
1616 _check_xfs_test_fs()
1617 {
1618     TEST_LOG="none"
1619     TEST_RT="none"
1620     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1621         TEST_LOG="$TEST_LOGDEV"
1622
1623     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1624         TEST_RT="$TEST_RTDEV"
1625
1626     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1627
1628     # check for ipath consistency
1629     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1630         # errors go to stderr
1631         xfs_check_ipaths $TEST_DIR >/dev/null
1632         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1633     fi
1634 }
1635
1636 _check_btrfs_filesystem()
1637 {
1638     device=$1
1639
1640     # If type is set, we're mounted
1641     type=`_fs_type $device`
1642     ok=1
1643
1644     if [ "$type" = "$FSTYP" ]
1645     then
1646         # mounted ...
1647         mountpoint=`_umount_or_remount_ro $device`
1648     fi
1649
1650     btrfsck $device >$tmp.fsck 2>&1
1651     if [ $? -ne 0 ]
1652     then
1653         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1654
1655         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1656         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1657         cat $tmp.fsck                           >>$seqres.full
1658         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1659
1660         ok=0
1661     fi
1662     rm -f $tmp.fsck
1663
1664     if [ $ok -eq 0 ]
1665     then
1666         echo "*** mount output ***"             >>$seqres.full
1667         _mount                                  >>$seqres.full
1668         echo "*** end mount output"             >>$seqres.full
1669     elif [ "$type" = "$FSTYP" ]
1670     then
1671         # was mounted ...
1672         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1673         ok=$?
1674     fi
1675
1676     if [ $ok -eq 0 ]; then
1677         status=1
1678         exit 1
1679     fi
1680
1681     return 0
1682 }
1683
1684 _check_test_fs()
1685 {
1686     case $FSTYP in
1687     xfs)
1688         _check_xfs_test_fs
1689         ;;
1690     nfs)
1691         # no way to check consistency for nfs
1692         ;;
1693     udf)
1694         # do nothing for now
1695         ;;
1696     btrfs)
1697         _check_btrfs_filesystem $TEST_DEV
1698         ;;
1699     *)
1700         _check_generic_filesystem $TEST_DEV
1701         ;;
1702     esac
1703 }
1704
1705 _check_scratch_fs()
1706 {
1707     device=$SCRATCH_DEV
1708     [ $# -eq 1 ] && device=$1
1709
1710     case $FSTYP in
1711     xfs)
1712         SCRATCH_LOG="none"
1713         SCRATCH_RT="none"
1714         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1715             SCRATCH_LOG="$SCRATCH_LOGDEV"
1716
1717         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1718             SCRATCH_RT="$SCRATCH_RTDEV"
1719
1720         _check_xfs_filesystem $device $SCRATCH_LOG $SCRATCH_RT
1721         ;;
1722     udf)
1723         _check_udf_filesystem $device $udf_fsize
1724         ;;
1725     nfs*)
1726         # Don't know how to check an NFS filesystem, yet.
1727         ;;
1728     btrfs)
1729         _check_btrfs_filesystem $device
1730         ;;
1731     *)
1732         _check_generic_filesystem $device
1733         ;;
1734     esac
1735 }
1736
1737 _full_fstyp_details()
1738 {
1739      [ -z "$FSTYP" ] && FSTYP=xfs
1740      if [ $FSTYP = xfs ]; then
1741         if [ -d /proc/fs/xfs ]; then
1742             if grep -q 'debug 0' /proc/fs/xfs/stat; then
1743                 FSTYP="$FSTYP (non-debug)"
1744             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
1745                 FSTYP="$FSTYP (debug)"
1746             fi
1747         else
1748             if uname -a | grep -qi 'debug'; then
1749                 FSTYP="$FSTYP (debug)"
1750             else
1751                 FSTYP="$FSTYP (non-debug)"
1752             fi
1753         fi
1754      fi
1755      echo $FSTYP
1756 }
1757
1758 _full_platform_details()
1759 {
1760      os=`uname -s`
1761      host=`hostname -s`
1762      kernel=`uname -r`
1763      platform=`uname -m`
1764      echo "$os/$platform $host $kernel"
1765 }
1766
1767 _setup_udf_scratchdir()
1768 {
1769     [ "$FSTYP" != "udf" ] \
1770         && _fail "setup_udf_testdir: \$FSTYP is not udf"
1771     [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
1772         && _notrun "this test requires a valid \$SCRATCH_DEV"
1773     [ -z "$SCRATCH_MNT" ] \
1774         && _notrun "this test requires a valid \$SCRATCH_MNT"
1775
1776     # mounted?
1777     if _mount | grep -q $SCRATCH_DEV
1778     then
1779         # if it's mounted, make sure its on $TEST_RW_DIR
1780         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1781         then
1782             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1783         fi
1784         $UMOUNT_PROG $SCRATCH_DEV
1785     fi
1786
1787     _scratch_mkfs
1788     _scratch_mount
1789
1790     testdir=$SCRATCH_MNT
1791 }
1792
1793 _setup_nfs_scratchdir()
1794 {
1795     [ "$FSTYP" != "nfs" ] \
1796         && _fail "setup_nfs_testdir: \$FSTYP is not nfs"
1797     [ -z "$SCRATCH_DEV" ] \
1798         && _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
1799     [ -z "$SCRATCH_MNT" ] \
1800         && _notrun "this test requires a valid \$SCRATCH_MNT"
1801
1802     # mounted?
1803     if _mount | grep -q $SCRATCH_DEV
1804     then
1805         # if it's mounted, make sure its on $SCRATCH_MNT
1806         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1807         then
1808             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1809         fi
1810         $UMOUNT_PROG $SCRATCH_DEV
1811     fi
1812
1813     _scratch_mkfs
1814     _scratch_mount
1815
1816     testdir=$SCRATCH_MNT
1817 }
1818
1819 #
1820 # Warning for UDF and NFS:
1821 # this function calls _setup_udf_scratchdir and _setup_udf_scratchdir
1822 # which actually uses the scratch dir for the test dir.
1823 #
1824 # This was done because testdir was intended to be a persistent
1825 # XFS only partition.  This should eventually change, and treat
1826 # at least local filesystems all the same.
1827 #
1828 _setup_testdir()
1829 {
1830     case $FSTYP in
1831     udf)
1832         _setup_udf_scratchdir
1833         ;;
1834     nfs*)
1835         _setup_nfs_scratchdir
1836         ;;
1837     *)
1838         testdir=$TEST_DIR
1839         ;;
1840     esac
1841 }
1842
1843 _cleanup_testdir()
1844 {
1845     case $FSTYP in
1846     udf)
1847         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1848         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1849         ;;
1850     nfs*)
1851         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1852         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1853         ;;
1854     *)
1855         # do nothing, testdir is $TEST_DIR
1856         :
1857         ;;
1858     esac
1859 }
1860
1861 _link_out_file()
1862 {
1863         if [ -z "$1" -o -z "$2" ]; then
1864                 echo Error must pass src and dst.
1865                 exit
1866         fi
1867         rm -f $2
1868         if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1869                 ln -s $1.irix $2
1870         elif [ "`uname`" == "Linux" ]; then
1871                 ln -s $1.linux $2
1872         else
1873                 echo Error test $seq does not run on the operating system: `uname`
1874                 exit
1875         fi
1876 }
1877
1878 _die()
1879 {
1880         echo $@
1881         exit 1
1882 }
1883
1884 #takes files, randomdata
1885 _nfiles()
1886 {
1887         f=0
1888         while [ $f -lt $1 ]
1889         do
1890                 file=f$f
1891                 echo > $file
1892                 if [ $size -gt 0 ]; then
1893                     if [ "$2" == "false" ]; then
1894                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
1895                     else
1896                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
1897                     fi
1898                 fi
1899                 let f=$f+1
1900         done
1901 }
1902
1903 # takes dirname, depth, randomdata
1904 _descend()
1905 {
1906         dirname=$1; depth=$2; randomdata=$3
1907         mkdir $dirname  || die "mkdir $dirname failed"
1908         cd $dirname
1909
1910         _nfiles $files $randomdata          # files for this dir and data type
1911
1912         [ $depth -eq 0 ] && return
1913         let deep=$depth-1 # go 1 down
1914
1915         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
1916
1917         d=0
1918         while [ $d -lt $dirs ]
1919         do
1920                 _descend d$d $deep &
1921                 let d=$d+1
1922                 wait
1923         done
1924 }
1925
1926 # Populate a filesystem with inodes for performance experiments
1927 #
1928 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
1929 #
1930 _populate_fs()
1931 {
1932     here=`pwd`
1933     dirs=5          # ndirs in each subdir till leaves
1934     size=0          # sizeof files in K
1935     files=100       # num files in _each_ subdir
1936     depth=2         # depth of tree from root to leaves
1937     verbose=false
1938     root=root       # path of initial root of directory tree
1939     randomdata=false # -x data type urandom or zero
1940
1941     OPTIND=1
1942     while getopts "d:f:n:r:s:v:x" c
1943     do
1944         case $c in
1945         d)      depth=$OPTARG;;
1946         n)      dirs=$OPTARG;;
1947         f)      files=$OPTARG;;
1948         s)      size=$OPTARG;;
1949         v)      verbose=true;;
1950         r)      root=$OPTARG;;
1951         x)      randomdata=true;;
1952         esac
1953     done
1954
1955     _descend $root $depth $randomdata
1956     wait
1957
1958     cd $here
1959
1960     [ $verbose = true ] && echo done
1961 }
1962
1963 # query whether the given file has the given inode flag set
1964 #
1965 _test_inode_flag()
1966 {
1967     flag=$1
1968     file=$2
1969
1970     if which $XFS_IO_PROG >/dev/null; then
1971         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
1972             return 0
1973         fi
1974     fi
1975     return 1
1976 }
1977
1978 # query the given files extsize allocator hint in bytes (if any)
1979 #
1980 _test_inode_extsz()
1981 {
1982     file=$1
1983     blocks=""
1984
1985     if which $XFS_IO_PROG >/dev/null; then
1986         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
1987                 awk '/^xattr.extsize =/ { print $3 }'`
1988     fi
1989     [ -z "$blocks" ] && blocks="0"
1990     echo $blocks
1991 }
1992
1993 # scratch_dev_pool should contain the disks pool for the btrfs raid
1994 _require_scratch_dev_pool()
1995 {
1996         local i
1997         if [ -z "$SCRATCH_DEV_POOL" ]; then
1998                 _notrun "this test requires a valid \$SCRATCH_DEV_POOL"
1999         fi
2000
2001         # btrfs test case needs 2 or more scratch_dev_pool; other FS not sure
2002         # so fail it
2003         case $FSTYP in
2004         btrfs)
2005                 if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt 2 ]; then
2006                         _notrun "btrfs and this test needs 2 or more disks in SCRATCH_DEV_POOL"
2007                 fi
2008         ;;
2009         *)
2010                 _notrun "dev_pool is not supported by fstype \"$FSTYP\""
2011         ;;
2012         esac
2013
2014         for i in $SCRATCH_DEV_POOL; do
2015                 if [ "`_is_block_dev $i`" = "" ]; then
2016                         _notrun "this test requires valid block disk $i"
2017                 fi
2018                 if [ "`_is_block_dev $i`" = "`_is_block_dev $TEST_DEV`" ]; then
2019                         _notrun "$i is part of TEST_DEV, this test requires unique disks"
2020                 fi
2021                 if _mount | grep -q $i; then
2022                         if ! $UMOUNT_PROG $i; then
2023                             echo "failed to unmount $i - aborting"
2024                             exit 1
2025                         fi
2026                 fi
2027                 # to help better debug when something fails, we remove
2028                 # traces of previous btrfs FS on the dev.
2029                 dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
2030         done
2031 }
2032
2033 # We will check if the device is virtual (eg: loop device) since it does not
2034 # have the delete entry-point. Otherwise SCSI and USB devices are fine. 
2035 _require_deletable_scratch_dev_pool()
2036 {
2037         local i
2038         local x
2039         for i in $SCRATCH_DEV_POOL; do
2040                 x=`echo $i | cut -d"/" -f 3`
2041                 ls -l /sys/class/block/${x} | grep -q "virtual" 
2042                 if [ $? == "0" ]; then
2043                         _notrun "$i is a virtual device which is not deletable"
2044                 fi
2045         done
2046 }
2047
2048 # We check for btrfs and (optionally) features of the btrfs command
2049 _require_btrfs()
2050 {
2051         cmd=$1
2052         _require_command $BTRFS_UTIL_PROG btrfs
2053         if [ -z "$1" ]; then
2054                 return 1;
2055         fi
2056         $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
2057         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
2058 }
2059
2060 # Check that fio is present, and it is able to execute given jobfile
2061 _require_fio()
2062 {
2063         job=$1
2064
2065         _require_command $FIO_PROG
2066         if [ -z "$1" ]; then
2067                 return 1;
2068         fi
2069
2070         $FIO_PROG --warnings-fatal --showcmd $job >> $seqres.full 2>&1
2071         [ $? -eq 0 ] || _notrun "$FIO_PROG too old, see $seqres.full"
2072 }
2073
2074 # Does freeze work on this fs?
2075 _require_freeze()
2076 {
2077         xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
2078         result=$? 
2079         xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
2080         [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
2081 }
2082
2083 # arg 1 is dev to remove and is output of the below eg.
2084 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2085 _devmgt_remove()
2086 {
2087         echo 1 > /sys/class/scsi_device/${1}/device/delete || _fail "Remove disk failed"
2088 }
2089
2090 # arg 1 is dev to add and is output of the below eg.
2091 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2092 _devmgt_add()
2093 {
2094         local h
2095         local tdl
2096         # arg 1 will be in h:t:d:l format now in the h and "t d l" format
2097         h=`echo ${1} | cut -d":" -f 1`
2098         tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`
2099
2100         echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"
2101 }
2102
2103 _require_fstrim()
2104 {
2105         if [ -z "$FSTRIM_PROG" ]; then
2106                 _notrun "This test requires fstrim utility."
2107         fi
2108 }
2109
2110 _test_batched_discard()
2111 {
2112         if [ $# -ne 1 ]; then
2113                 echo "Usage: _test_batched_discard mnt_point" 1>&2
2114                 exit 1
2115         fi
2116         _require_fstrim
2117         $FSTRIM_PROG ${1} &>/dev/null
2118 }
2119
2120 _require_dumpe2fs()
2121 {
2122         if [ -z "$DUMPE2FS_PROG" ]; then
2123                 _notrun "This test requires dumpe2fs utility."
2124         fi
2125 }
2126
2127 _create_loop_device()
2128 {
2129         file=$1
2130         dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"
2131         echo $dev
2132 }
2133
2134 _destroy_loop_device()
2135 {
2136         dev=$1
2137         losetup -d $dev || _fail "Cannot destroy loop device $dev"
2138 }
2139
2140 _scale_fsstress_args()
2141 {
2142     args=""
2143     while [ $# -gt 0 ]; do
2144         case "$1" in
2145             -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
2146             -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
2147             *) args="$args $1" ;;
2148         esac
2149         shift
2150     done
2151     echo $args
2152 }
2153
2154 run_check()
2155 {
2156         echo "# $@" >> $seqres.full 2>&1
2157         "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
2158 }
2159
2160 init_rc()
2161 {
2162         if [ "$iam" == new ]
2163         then
2164                 return
2165         fi
2166         # make some further configuration checks here
2167         if [ "$TEST_DEV" = ""  ]
2168         then
2169                 echo "common/rc: Error: \$TEST_DEV is not set"
2170                 exit 1
2171         fi
2172
2173         # if $TEST_DEV is not mounted, mount it now as XFS
2174         if [ -z "`_fs_type $TEST_DEV`" ]
2175         then
2176                 # $TEST_DEV is not mounted
2177                 if ! _test_mount
2178                 then
2179                         echo "common/rc: retrying test device mount with external set"
2180                         [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
2181                         if ! _test_mount
2182                         then
2183                                 echo "common/rc: could not mount $TEST_DEV on $TEST_DIR"
2184                                 exit 1
2185                         fi
2186                 fi
2187         fi
2188
2189         if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
2190         then
2191                 echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
2192                 $DF_PROG $TEST_DEV
2193                 exit 1
2194         fi
2195         # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
2196         xfs_io -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
2197         export XFS_IO_PROG="$XFS_IO_PROG -F"
2198 }
2199
2200 init_rc
2201
2202 ################################################################################
2203 # make sure this script returns success
2204 /bin/true