common: Re-enable testing over NFS
[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 2>&1 | grep noxfer >/dev/null
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 # Also filter out lost+found directory on extN file system if present
78
79 _ls_l()
80 {
81         ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /" | grep -v 'lost+found'
82 }
83
84 # we need common/config
85 if [ "$iam" != "check" ]
86 then
87     if ! . ./common/config
88         then
89         echo "$iam: failed to source common/config"
90         exit 1
91     fi
92 fi
93
94 # check for correct setup
95 case "$FSTYP" in
96     xfs)
97          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
98          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
99          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
100          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
101          ;;
102     udf)
103          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
104          ;;
105     btrfs)
106          [ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
107          ;;
108     ext4)
109          [ "$MKFS_EXT4_PROG" = "" ] && _fatal "mkfs.ext4 not found"
110          ;;
111     nfs)
112          ;;
113     cifs)
114          ;;
115 esac
116
117 # make sure we have a standard umask
118 umask 022
119
120 _mount()
121 {
122     $MOUNT_PROG `_mount_ops_filter $*`
123 }
124
125 _scratch_options()
126 {
127     type=$1
128     SCRATCH_OPTIONS=""
129
130     if [ "$FSTYP" != "xfs" ]; then
131         return
132     fi
133
134     case $type in
135     mkfs)
136         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
137         rt_opt="-r"
138         log_opt="-l"
139         ;;
140     mount)
141         rt_opt="-o"
142         log_opt="-o"
143         ;;
144     esac
145     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
146         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
147     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
148         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
149 }
150
151 _test_options()
152 {
153     type=$1
154     TEST_OPTIONS=""
155
156     if [ "$FSTYP" != "xfs" ]; then
157         return
158     fi
159
160     case $type in
161     mkfs)
162         rt_opt="-r"
163         log_opt="-l"
164         ;;
165     mount)
166         rt_opt="-o"
167         log_opt="-o"
168         ;;
169     esac
170     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
171         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
172     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
173         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
174 }
175
176 _mount_ops_filter()
177 {
178     params="$*"
179     
180     #get mount point to handle dmapi mtpt option correctly
181     let last_index=$#-1
182     [ $last_index -gt 0 ] && shift $last_index
183     FS_ESCAPED=$1
184     
185     # irix is fussy about how it is fed its mount options
186     # - multiple -o's are not allowed
187     # - no spaces between comma delimitered options
188     # the sed script replaces all -o's (except the first) with a comma
189     # not required for linux, but won't hurt
190     
191     echo $params | sed -e 's/[[:space:]]\+-o[[:space:]]*/UnIqUe/1; s/[[:space:]]\+-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
192         | sed -e 's/dmapi/dmi/' \
193         | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"
194
195 }
196
197 _scratch_mount_options()
198 {
199     _scratch_options mount
200
201     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
202 }
203
204 _scratch_mount()
205 {
206     _mount -t $FSTYP `_scratch_mount_options $*`
207 }
208
209 _scratch_unmount()
210 {
211     $UMOUNT_PROG $SCRATCH_DEV
212 }
213
214 _scratch_remount()
215 {
216     _scratch_unmount
217     _scratch_mount
218 }
219
220 _test_mount()
221 {
222     _test_options mount
223     _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
224 }
225
226 _test_remount()
227 {
228     $UMOUNT_PROG $TEST_DEV
229     _test_mount
230 }
231
232 _scratch_mkfs_options()
233 {
234     _scratch_options mkfs
235     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
236 }
237
238 _scratch_metadump()
239 {
240         dumpfile=$1
241         options=
242
243         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
244                 options="-l $SCRATCH_LOGDEV"
245
246         xfs_metadump $options $SCRATCH_DEV $dumpfile
247 }
248
249 _setup_large_xfs_fs()
250 {
251         fs_size=$1
252         local tmp_dir=/tmp/
253
254         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
255         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
256         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
257
258         # calculate the size of the file we need to allocate.
259         # Default free space in the FS is 50GB, but you can specify more via
260         # SCRATCH_DEV_EMPTY_SPACE
261         file_size=$(($fs_size - 50*1024*1024*1024))
262         file_size=$(($file_size - $SCRATCH_DEV_EMPTY_SPACE))
263
264         # mount the filesystem, create the file, unmount it
265         _scratch_mount 2>&1 >$tmp_dir/mnt.err
266         local status=$?
267         if [ $status -ne 0 ]; then
268                 echo "mount failed"
269                 cat $tmp_dir/mnt.err >&2
270                 rm -f $tmp_dir/mnt.err
271                 return $status
272         fi
273         rm -f $tmp_dir/mnt.err
274
275         xfs_io -F -f \
276                 -c "truncate $file_size" \
277                 -c "falloc -k 0 $file_size" \
278                 -c "chattr +d" \
279                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
280         export NUM_SPACE_FILES=1
281         status=$?
282         umount $SCRATCH_MNT
283         if [ $status -ne 0 ]; then
284                 echo "large file prealloc failed"
285                 cat $tmp_dir/mnt.err >&2
286                 return $status
287         fi
288         return 0
289 }
290
291 _scratch_mkfs_xfs_opts()
292 {
293         mkfs_opts=$*
294
295         _scratch_options mkfs
296
297         $MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
298 }
299
300
301 _scratch_mkfs_xfs_supported()
302 {
303         mkfs_opts=$*
304
305         _scratch_options mkfs
306
307         $MKFS_XFS_PROG -N $MKFS_OPTIONS $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
308 }
309
310 _scratch_mkfs_xfs()
311 {
312         # extra mkfs options can be added by tests
313         local extra_mkfs_options=$*
314
315         local tmp_dir=/tmp/
316
317         # save mkfs output in case conflict means we need to run again.
318         # only the output for the mkfs that applies should be shown
319         _scratch_mkfs_xfs_opts $MKFS_OPTIONS $extra_mkfs_options \
320                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
321         local mkfs_status=$?
322
323
324         # a mkfs failure may be caused by conflicts between
325         # $MKFS_OPTIONS and $extra_mkfs_options
326         if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
327                 (
328                 echo -n "** mkfs failed with extra mkfs options "
329                 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
330                 echo -n "** attempting to mkfs using only test $seq "
331                 echo "options: $extra_mkfs_options **"
332                 ) >> $seqres.full
333
334                 # running mkfs again. overwrite previous mkfs output files
335                 _scratch_mkfs_xfs_opts $extra_mkfs_options \
336                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
337                 local mkfs_status=$?
338         fi
339
340         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
341                 # manually parse the mkfs output to get the fs size in bytes
342                 local fs_size
343                 fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
344                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
345                                 my $size = $1 * $2;
346                                 print STDOUT "$size\n";
347                         }'`
348                 _setup_large_xfs_fs $fs_size
349                 mkfs_status=$?
350         fi
351
352         # output stored mkfs output
353         cat $tmp_dir.mkfserr >&2
354         cat $tmp_dir.mkfsstd
355         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
356
357         return $mkfs_status
358 }
359
360 # xfs_check script is planned to be deprecated. But, we want to
361 # be able to invoke "xfs_check" behavior in xfstests in order to
362 # maintain the current verification levels.
363 _xfs_check()
364 {
365     OPTS=" "
366     DBOPTS=" "
367     USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
368
369     while getopts "b:fi:l:stvV" c
370     do
371         case $c in
372             s) OPTS=$OPTS"-s ";;
373             t) OPTS=$OPTS"-t ";;
374             v) OPTS=$OPTS"-v ";;
375             i) OPTS=$OPTS"-i "$OPTARG" ";;
376             b) OPTS=$OPTS"-b "$OPTARG" ";;
377             f) DBOPTS=$DBOPTS" -f";;
378             l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
379             V) $XFS_DB_PROG -p xfs_check -V
380                 return $?
381                 ;;
382         esac
383     done
384     set -- extra $@
385     shift $OPTIND
386     case $# in
387         1)    ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
388                status=$?
389                ;;
390         2)    echo $USAGE 1>&1
391               status=2
392               ;;
393     esac
394     return $status
395 }
396
397 _setup_large_ext4_fs()
398 {
399         fs_size=$1
400         local tmp_dir=/tmp/
401
402         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
403         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
404         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
405
406         # Default free space in the FS is 50GB, but you can specify more via
407         # SCRATCH_DEV_EMPTY_SPACE
408         space_to_consume=$(($fs_size - 50*1024*1024*1024 - $SCRATCH_DEV_EMPTY_SPACE))
409
410         # mount the filesystem and create 16TB - 4KB files until we consume
411         # all the necessary space.
412         _scratch_mount 2>&1 >$tmp_dir/mnt.err
413         local status=$?
414         if [ $status -ne 0 ]; then
415                 echo "mount failed"
416                 cat $tmp_dir/mnt.err >&2
417                 rm -f $tmp_dir/mnt.err
418                 return $status
419         fi
420         rm -f $tmp_dir/mnt.err
421
422         file_size=$((16*1024*1024*1024*1024 - 4096))
423         nfiles=0
424         while [ $space_to_consume -gt $file_size ]; do
425
426                 xfs_io -F -f \
427                         -c "truncate $file_size" \
428                         -c "falloc -k 0 $file_size" \
429                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
430                 status=$?
431                 if [ $status -ne 0 ]; then
432                         break;
433                 fi
434
435                 space_to_consume=$(( $space_to_consume - $file_size ))
436                 nfiles=$(($nfiles + 1))
437         done
438
439         # consume the remaining space.
440         if [ $space_to_consume -gt 0 ]; then
441                 xfs_io -F -f \
442                         -c "truncate $space_to_consume" \
443                         -c "falloc -k 0 $space_to_consume" \
444                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
445                 status=$?
446         fi
447         export NUM_SPACE_FILES=$nfiles
448
449         umount $SCRATCH_MNT
450         if [ $status -ne 0 ]; then
451                 echo "large file prealloc failed"
452                 cat $tmp_dir/mnt.err >&2
453                 return $status
454         fi
455         return 0
456 }
457
458 _scratch_mkfs_ext4()
459 {
460         # extra mkfs options can be added by tests
461         local extra_mkfs_options=$*
462
463         local tmp_dir=/tmp/
464
465         $MKFS_EXT4_PROG -F $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
466                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
467         local mkfs_status=$?
468
469         # a mkfs failure may be caused by conflicts between
470         # $MKFS_OPTIONS and $extra_mkfs_options
471         if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
472                 (
473                 echo -n "** mkfs failed with extra mkfs options "
474                 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
475                 echo -n "** attempting to mkfs using only test $seq "
476                 echo "options: $extra_mkfs_options **"
477                 ) >> $seqres.full
478
479                 # running mkfs again. overwrite previous mkfs output files
480                 $MKFS_EXT4_PROG -F $extra_mkfs_options $SCRATCH_DEV \
481                                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
482                 local mkfs_status=$?
483         fi
484
485         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
486                 # manually parse the mkfs output to get the fs size in bytes
487                 fs_size=`cat $tmp_dir.mkfsstd | awk ' \
488                         /^Block size/ { split($2, a, "="); bs = a[2] ; } \
489                         / inodes, / { blks = $3 } \
490                         /reserved for the super user/ { resv = $1 } \
491                         END { fssize = bs * blks - resv; print fssize }'`
492
493                 _setup_large_ext4_fs $fs_size
494                 mkfs_status=$?
495         fi
496
497         # output stored mkfs output
498         cat $tmp_dir.mkfserr >&2
499         cat $tmp_dir.mkfsstd
500         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
501
502         return $mkfs_status
503 }
504
505 _test_mkfs()
506 {
507     case $FSTYP in
508     nfs*)
509         # do nothing for nfs
510         ;;
511     cifs)
512         # do nothing for cifs
513         ;;
514     udf)
515         $MKFS_UDF_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
516         ;;
517     btrfs)
518         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
519         ;;
520     *)
521         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $TEST_DEV
522         ;;
523     esac
524 }
525
526 _mkfs_dev()
527 {
528     case $FSTYP in
529     nfs*)
530         # do nothing for nfs
531         ;;
532     udf)
533         $MKFS_UDF_PROG $MKFS_OPTIONS $* 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
534         ;;
535     btrfs)
536         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
537         ;;
538     *)
539         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* \
540                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
541         ;;
542     esac
543
544     if [ $? -ne 0 ]; then
545         # output stored mkfs output
546         cat $tmp_dir.mkfserr >&2
547         cat $tmp_dir.mkfsstd
548         status=1
549         exit 1
550     fi
551     rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
552 }
553
554 _scratch_mkfs()
555 {
556     case $FSTYP in
557     xfs)
558         _scratch_mkfs_xfs $*
559         ;;
560     nfs*)
561         # do nothing for nfs
562         ;;
563     cifs)
564         # do nothing for cifs
565         ;;
566     udf)
567         $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
568         ;;
569     btrfs)
570         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
571         ;;
572     ext4)
573         _scratch_mkfs_ext4 $*
574         ;;
575     tmpfs)
576         # do nothing for tmpfs
577         ;;
578     *)
579         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV
580         ;;
581     esac
582 }
583
584 _scratch_pool_mkfs()
585 {
586     case $FSTYP in
587     btrfs)
588         # if dup profile is in mkfs options call _scratch_mkfs instead
589         # because dup profile only works with single device
590         if [[ "$*" =~ dup ]]; then
591             _scratch_mkfs $*
592         else
593             $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV_POOL > /dev/null
594         fi
595         ;;
596     *)
597         echo "_scratch_pool_mkfs is not implemented for $FSTYP" 1>&2
598         ;;
599     esac
600 }
601
602 # Create fs of certain size on scratch device
603 # _scratch_mkfs_sized <size in bytes> [optional blocksize]
604 _scratch_mkfs_sized()
605 {
606     fssize=$1
607     blocksize=$2
608
609     case $FSTYP in
610     xfs)
611         def_blksz=`echo $MKFS_OPTIONS|sed -rn 's/.*-b ?size= ?+([0-9]+).*/\1/p'`
612         ;;
613     ext2|ext3|ext4|ext4dev|udf|btrfs)
614         def_blksz=`echo $MKFS_OPTIONS| sed -rn 's/.*-b ?+([0-9]+).*/\1/p'`
615         ;;
616     esac
617
618     [ -n "$def_blksz" ] && blocksize=$def_blksz
619     [ -z "$blocksize" ] && blocksize=4096
620
621
622     re='^[0-9]+$'
623     if ! [[ $fssize =~ $re ]] ; then
624         _notrun "error: _scratch_mkfs_sized: fs size \"$fssize\" not an integer."
625     fi
626     if ! [[ $blocksize =~ $re ]] ; then
627         _notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
628     fi
629
630     blocks=`expr $fssize / $blocksize`
631
632     if [ "$HOSTOS" == "Linux" ]; then
633         devsize=`blockdev --getsize64 $SCRATCH_DEV`
634         [ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
635     fi
636
637     case $FSTYP in
638     xfs)
639         # don't override MKFS_OPTIONS that set a block size.
640         echo $MKFS_OPTIONS |egrep -q "b?size="
641         if [ $? -eq 0 ]; then
642                 _scratch_mkfs_xfs -d size=$fssize
643         else
644                 _scratch_mkfs_xfs -d size=$fssize -b size=$blocksize
645         fi
646         ;;
647     ext2|ext3|ext4|ext4dev)
648         yes | ${MKFS_PROG}.$FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
649         ;;
650     udf)
651         $MKFS_UDF_PROG $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
652         ;;
653     btrfs)
654         $MKFS_BTRFS_PROG $MKFS_OPTIONS -b $fssize $SCRATCH_DEV
655         ;;
656     *)
657         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
658         ;;
659     esac
660 }
661
662 # Emulate an N-data-disk stripe w/ various stripe units
663 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
664 _scratch_mkfs_geom()
665 {
666     sunit_bytes=$1
667     swidth_mult=$2
668     blocksize=$3
669     [ -z "$blocksize" ] && blocksize=4096
670
671     let sunit_blocks=$sunit_bytes/$blocksize
672     let swidth_blocks=$sunit_blocks*$swidth_mult
673
674     case $FSTYP in
675     xfs)
676         MKFS_OPTIONS+=" -b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
677         ;;
678     ext4|ext4dev)
679         MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
680         ;;
681     *)
682         _notrun "can't mkfs $FSTYP with geometry"
683         ;;
684     esac
685     _scratch_mkfs
686 }
687
688 _scratch_resvblks()
689 {
690         case $FSTYP in
691         xfs)
692                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
693                 ;;
694         *)
695                 ;;
696         esac
697 }
698
699 _scratch_xfs_db_options()
700 {
701     SCRATCH_OPTIONS=""
702     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
703         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
704     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
705 }
706
707 _scratch_xfs_logprint()
708 {
709     SCRATCH_OPTIONS=""
710     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
711         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
712     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
713 }
714
715 _scratch_xfs_check()
716 {
717     SCRATCH_OPTIONS=""
718     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
719         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
720     [ "$LARGE_SCRATCH_DEV" = yes ] && \
721         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
722     _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
723 }
724
725 _scratch_xfs_repair()
726 {
727     SCRATCH_OPTIONS=""
728     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
729         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
730     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
731         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
732     [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
733     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
734 }
735
736 _get_pids_by_name()
737 {
738     if [ $# -ne 1 ]
739     then
740         echo "Usage: _get_pids_by_name process-name" 1>&2
741         exit 1
742     fi
743
744     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
745     # HH:MM:SS before the psargs field, use this as the search anchor.
746     #
747     # Matches with $1 (process-name) occur if the first psarg is $1
748     # or ends in /$1 ... the matching uses sed's regular expressions,
749     # so passing a regex into $1 will work.
750
751     ps $PS_ALL_FLAGS \
752     | sed -n \
753         -e 's/$/ /' \
754         -e 's/[         ][      ]*/ /g' \
755         -e 's/^ //' \
756         -e 's/^[^ ]* //' \
757         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
758         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
759 }
760
761 # fix malloc libs output
762 #
763 _fix_malloc()
764 {
765     # filter out the Electric Fence notice
766     $PERL_PROG -e '
767         while (<>) {
768             if (defined $o && /^\s+Electric Fence/) {
769                 chomp($o);
770                 print "$o";
771                 undef $o;
772                 next;
773             }
774             print $o if (defined $o);
775
776             $o=$_;
777         }
778         print $o if (defined $o);
779     '
780 }
781
782 # check if run as root
783 #
784 _need_to_be_root()
785 {
786     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
787     if [ "$id" -ne 0 ]
788     then
789         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
790         exit 1
791     fi
792 }
793
794
795 #
796 # _df_device : get an IRIX style df line for a given device
797 #
798 #       - returns "" if not mounted
799 #       - returns fs type in field two (ala IRIX)
800 #       - joins line together if split by fancy df formatting
801 #       - strips header etc
802 #
803
804 _df_device()
805 {
806     if [ $# -ne 1 ]
807     then
808         echo "Usage: _df_device device" 1>&2
809         exit 1
810     fi
811
812     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
813         match($1,what) && NF==1 {
814             v=$1
815             getline
816             print v, $0
817             exit
818         }
819         match($1,what) {
820             print
821             exit
822         }
823     '
824 }
825
826 #
827 # _df_dir : get an IRIX style df line for device where a directory resides
828 #
829 #       - returns fs type in field two (ala IRIX)
830 #       - joins line together if split by fancy df formatting
831 #       - strips header etc
832 #
833
834 _df_dir()
835 {
836     if [ $# -ne 1 ]
837     then
838         echo "Usage: _df_dir device" 1>&2
839         exit 1
840     fi
841
842     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
843         NR == 2 && NF==1 {
844             v=$1
845             getline
846             print v, $0;
847             exit 0
848         }
849         NR == 2 {
850             print;
851             exit 0
852         }
853         {}
854     '
855     # otherwise, nada
856 }
857
858 # return percentage used disk space for mounted device
859
860 _used()
861 {
862     if [ $# -ne 1 ]
863     then
864         echo "Usage: _used device" 1>&2
865         exit 1
866     fi
867
868     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
869 }
870
871 # return the FS type of a mounted device
872 #
873 _fs_type()
874 {
875     if [ $# -ne 1 ]
876     then
877         echo "Usage: _fs_type device" 1>&2
878         exit 1
879     fi
880
881     #
882     # The Linux kernel shows NFSv4 filesystems in df output as
883     # filesystem type nfs4, although we mounted it as nfs earlier.
884     # Fix the filesystem type up here so that the callers don't
885     # have to bother with this quirk.
886     #
887     _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
888 }
889
890 # return the FS mount options of a mounted device
891 #
892 # should write a version which just parses the output of mount for IRIX
893 # compatibility, but since this isn't used at all, at the moment I'll leave
894 # this for now
895 #
896 _fs_options()
897 {
898     if [ $# -ne 1 ]
899     then
900         echo "Usage: _fs_options device" 1>&2
901         exit 1
902     fi
903
904     $AWK_PROG -v dev=$1 '
905         match($1,dev) { print $4 }
906     ' </proc/mounts
907 }
908
909 # returns device number if a file is a block device
910 #
911 _is_block_dev()
912 {
913     if [ $# -ne 1 ]
914     then
915         echo "Usage: _is_block_dev dev" 1>&2
916         exit 1
917     fi
918
919     _dev=$1
920     if [ -L "${_dev}" ]; then
921         _dev=`readlink -f ${_dev}`
922     fi
923
924     if [ -b "${_dev}" ]; then
925         src/lstat64 ${_dev} | $AWK_PROG '/Device type:/ { print $9 }'
926     fi
927 }
928
929 # Do a command, log it to $seqres.full, optionally test return status
930 # and die if command fails. If called with one argument _do executes the
931 # command, logs it, and returns its exit status. With two arguments _do
932 # first prints the message passed in the first argument, and then "done"
933 # or "fail" depending on the return status of the command passed in the
934 # second argument. If the command fails and the variable _do_die_on_error
935 # is set to "always" or the two argument form is used and _do_die_on_error
936 # is set to "message_only" _do will print an error message to
937 # $seqres.out and exit.
938
939 _do()
940 {
941     if [ $# -eq 1 ]; then
942         _cmd=$1
943     elif [ $# -eq 2 ]; then
944         _note=$1
945         _cmd=$2
946         echo -n "$_note... "
947     else
948         echo "Usage: _do [note] cmd" 1>&2
949         status=1; exit
950     fi
951
952     (eval "echo '---' \"$_cmd\"") >>$seqres.full
953     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
954     cat $tmp._out | _fix_malloc >>$seqres.full
955     if [ $# -eq 2 ]; then
956         if [ $ret -eq 0 ]; then
957             echo "done"
958         else
959             echo "fail"
960         fi
961     fi
962     if [ $ret -ne 0  ] \
963         && [ "$_do_die_on_error" = "always" \
964             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
965     then
966         [ $# -ne 2 ] && echo
967         eval "echo \"$_cmd\" failed \(returned $ret\): see $seqres.full"
968         status=1; exit
969     fi
970
971     return $ret
972 }
973
974 # bail out, setting up .notrun file. Need to kill the filesystem check files
975 # here, otherwise they are set incorrectly for the next test.
976 #
977 _notrun()
978 {
979     echo "$*" > $seqres.notrun
980     echo "$seq not run: $*"
981     rm -f ${RESULT_DIR}/require_test
982     rm -f ${RESULT_DIR}/require_scratch
983     status=0
984     exit
985 }
986
987 # just plain bail out
988 #
989 _fail()
990 {
991     echo "$*" | tee -a $seqres.full
992     echo "(see $seqres.full for details)"
993     status=1
994     exit 1
995 }
996
997 # tests whether $FSTYP is one of the supported filesystems for a test
998 #
999 _supported_fs()
1000 {
1001     for f
1002     do
1003         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
1004         then
1005             return
1006         fi
1007     done
1008
1009     _notrun "not suitable for this filesystem type: $FSTYP"
1010 }
1011
1012
1013 # tests whether $FSTYP is one of the supported OSes for a test
1014 #
1015 _supported_os()
1016 {
1017     for h
1018     do
1019         if [ "$h" = "$HOSTOS" ]
1020         then
1021             return
1022         fi
1023     done
1024
1025     _notrun "not suitable for this OS: $HOSTOS"
1026 }
1027
1028 # this test needs a scratch partition - check we're ok & unmount it
1029 # No post-test check of the device is required. e.g. the test intentionally
1030 # finishes the test with the filesystem in a corrupt state
1031 _require_scratch_nocheck()
1032 {
1033     case "$FSTYP" in
1034         nfs*)
1035                  _notrun "requires a scratch device"
1036                  ;;
1037         cifs)
1038                 _notrun "requires a scratch device"
1039                 ;;
1040         tmpfs)
1041                 if [ -z "$SCRATCH_DEV" -o ! -d "$SCRATCH_MNT" ];
1042                 then
1043                     _notrun "this test requires a valid \$SCRATCH_MNT and unique $SCRATCH_DEV"
1044                 fi
1045                 ;;
1046         *)
1047                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
1048                  then
1049                      _notrun "this test requires a valid \$SCRATCH_DEV"
1050                  fi
1051                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
1052                  then
1053                      _notrun "this test requires a valid \$SCRATCH_DEV"
1054                  fi
1055                 if [ ! -d "$SCRATCH_MNT" ]
1056                 then
1057                      _notrun "this test requires a valid \$SCRATCH_MNT"
1058                 fi
1059                  ;;
1060     esac
1061
1062     # mounted?
1063     if _mount | grep -q $SCRATCH_DEV
1064     then
1065         # if it's mounted, make sure its on $SCRATCH_MNT
1066         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1067         then
1068             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1069             exit 1
1070         fi
1071         # and then unmount it
1072         if ! $UMOUNT_PROG $SCRATCH_DEV
1073         then
1074             echo "failed to unmount $SCRATCH_DEV"
1075             exit 1
1076         fi
1077     fi
1078     rm -f ${RESULT_DIR}/require_scratch
1079 }
1080
1081 # we need the scratch device and it should be checked post test.
1082 _require_scratch()
1083 {
1084         _require_scratch_nocheck
1085         touch ${RESULT_DIR}/require_scratch
1086 }
1087
1088
1089 # this test needs a test partition - check we're ok & unmount it
1090 #
1091 _require_test()
1092 {
1093     case "$FSTYP" in
1094         nfs*)
1095                 echo $TEST_DEV | grep -q ":/" > /dev/null 2>&1
1096                 if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
1097                         _notrun "this test requires a valid \$TEST_DIR"
1098                 fi
1099                 if [ ! -d "$TEST_DIR" ]; then
1100                         _notrun "this test requires a valid \$TEST_DIR"
1101                 fi
1102                 ;;
1103         cifs)
1104                 echo $TEST_DEV | grep -q "//" > /dev/null 2>&1
1105                 if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
1106                         _notrun "this test requires a valid \$TEST_DEV"
1107                 fi
1108                 if [ ! -d "$TEST_DIR" ]; then
1109                      _notrun "this test requires a valid \$TEST_DIR"
1110                 fi
1111                 ;;
1112         tmpfs)
1113                 if [ -z "$TEST_DEV" -o ! -d "$TEST_DIR" ];
1114                 then
1115                     _notrun "this test requires a valid \$TEST_DIR and unique $TEST_DEV"
1116                 fi
1117                 ;;
1118         *)
1119                  if [ -z "$TEST_DEV" -o "`_is_block_dev $TEST_DEV`" = "" ]
1120                  then
1121                      _notrun "this test requires a valid \$TEST_DEV"
1122                  fi
1123                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
1124                  then
1125                      _notrun "this test requires a valid \$TEST_DEV"
1126                  fi
1127                 if [ ! -d "$TEST_DIR" ]
1128                 then
1129                      _notrun "this test requires a valid \$TEST_DIR"
1130                 fi
1131                  ;;
1132     esac
1133
1134     # mounted?
1135     if _mount | grep -q $TEST_DEV
1136     then
1137         # if it's mounted, make sure its on $TEST_DIR
1138         if ! _mount | grep $TEST_DEV | grep -q $TEST_DIR
1139         then
1140             echo "\$TEST_DEV is mounted but not on \$TEST_DIR - aborting"
1141             exit 1
1142         fi
1143     else
1144         out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
1145         if [ $? -ne 1 ]; then
1146                 echo $out
1147                 exit 1
1148         fi
1149     fi
1150     touch ${RESULT_DIR}/require_test
1151 }
1152
1153 # this test needs a logdev
1154 #
1155 _require_logdev()
1156 {
1157     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
1158         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
1159     [ "$USE_EXTERNAL" != yes ] && \
1160         _notrun "This test requires USE_EXTERNAL to be enabled"
1161
1162     # ensure its not mounted
1163     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
1164 }
1165
1166 # this test requires loopback device support
1167 #
1168 _require_loop()
1169 {
1170     if [ "$HOSTOS" != "Linux" ]
1171     then
1172         _notrun "This test requires linux for loopback device support"
1173     fi
1174
1175     modprobe loop >/dev/null 2>&1
1176     if grep loop /proc/devices >/dev/null 2>&1
1177     then
1178         :
1179     else
1180         _notrun "This test requires loopback device support"
1181     fi
1182 }
1183
1184 # this test requires ext2 filesystem support
1185 #
1186 _require_ext2()
1187 {
1188     if [ "$HOSTOS" != "Linux" ]
1189     then
1190         _notrun "This test requires linux for ext2 filesystem support"
1191     fi
1192
1193     modprobe ext2 >/dev/null 2>&1
1194     if grep ext2 /proc/filesystems >/dev/null 2>&1
1195     then
1196         :
1197     else
1198         _notrun "This test requires ext2 filesystem support"
1199     fi
1200 }
1201
1202 # this test requires that (large) loopback device files are not in use
1203 #
1204 _require_no_large_scratch_dev()
1205 {
1206     [ "$LARGE_SCRATCH_DEV" = yes ] && \
1207         _notrun "Large filesystem testing in progress, skipped this test"
1208 }
1209
1210 # this test requires that a realtime subvolume is in use, and
1211 # that the kernel supports realtime as well.
1212 #
1213 _require_realtime()
1214 {
1215     [ "$USE_EXTERNAL" = yes ] || \
1216         _notrun "External volumes not in use, skipped this test"
1217     [ "$SCRATCH_RTDEV" = "" ] && \
1218         _notrun "Realtime device required, skipped this test"
1219 }
1220
1221 # this test requires that a specified command (executable) exists
1222 # $1 - command, $2 - name for error message
1223 #
1224 _require_command()
1225 {
1226     [ -n "$1" ] && _cmd="$1" || _cmd="$2"
1227     [ -n "$1" -a -x "$1" ] || _notrun "$_cmd utility required, skipped this test"
1228 }
1229
1230 # this test requires the device mapper flakey target
1231 #
1232 _require_dm_flakey()
1233 {
1234     _require_command $DMSETUP_PROG
1235
1236     modprobe dm-flakey >/dev/null 2>&1
1237     $DMSETUP_PROG targets | grep flakey >/dev/null 2>&1
1238     if [ $? -eq 0 ]
1239     then
1240         :
1241     else
1242         _notrun "This test requires dm flakey support"
1243     fi
1244 }
1245
1246 # this test requires the projid32bit feature to be available in mkfs.xfs.
1247 #
1248 _require_projid32bit()
1249 {
1250        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
1251            || _notrun "mkfs.xfs doesn't have projid32bit feature"
1252 }
1253
1254 _require_projid16bit()
1255 {
1256         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
1257            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
1258 }
1259
1260 # this test requires the crc feature to be available in mkfs.xfs
1261 #
1262 _require_xfs_mkfs_crc()
1263 {
1264         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
1265            || _notrun "mkfs.xfs doesn't have crc feature"
1266 }
1267
1268 # this test requires the xfs kernel support crc feature
1269 #
1270 _require_xfs_crc()
1271 {
1272         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
1273         _scratch_mount >/dev/null 2>&1 \
1274            || _notrun "Kernel doesn't support crc feature"
1275         umount $SCRATCH_MNT
1276 }
1277
1278 # this test requires the bigalloc feature to be available in mkfs.ext4
1279 #
1280 _require_ext4_mkfs_bigalloc()
1281 {
1282         $MKFS_EXT4_PROG -F -O bigalloc -n $SCRATCH_DEV 200k >/dev/null 2>&1 \
1283            || _notrun "mkfs.ext4 doesn't have bigalloc feature"
1284 }
1285
1286 # this test requires the ext4 kernel support bigalloc feature
1287 #
1288 _require_ext4_bigalloc()
1289 {
1290         $MKFS_EXT4_PROG -F -O bigalloc $SCRATCH_DEV 200k >/dev/null 2>&1
1291         _scratch_mount >/dev/null 2>&1 \
1292            || _notrun "Ext4 kernel doesn't support bigalloc feature"
1293         umount $SCRATCH_MNT
1294 }
1295
1296 # this test requires the finobt feature to be available in mkfs.xfs
1297 #
1298 _require_xfs_mkfs_finobt()
1299 {
1300         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
1301            || _notrun "mkfs.xfs doesn't have finobt feature"
1302 }
1303
1304 # this test requires the xfs kernel support finobt feature
1305 #
1306 _require_xfs_finobt()
1307 {
1308         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
1309         _scratch_mount >/dev/null 2>&1 \
1310            || _notrun "Kernel doesn't support finobt feature"
1311         umount $SCRATCH_MNT
1312 }
1313
1314 # this test requires xfs sysfs attribute support
1315 #
1316 _require_xfs_sysfs()
1317 {
1318         attr=$1
1319         sysfsdir=/sys/fs/xfs
1320
1321         if [ ! -e $sysfsdir ]; then
1322                 _notrun "no kernel support for XFS sysfs attributes"
1323         fi
1324
1325         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
1326                 _notrun "sysfs attribute '$attr' is not supported"
1327         fi
1328 }
1329
1330 # this test requires that external log/realtime devices are not in use
1331 #
1332 _require_nonexternal()
1333 {
1334     [ "$USE_EXTERNAL" = yes ] && \
1335         _notrun "External device testing in progress, skipped this test"
1336 }
1337
1338 # this test requires that a (specified) aio-dio executable exists
1339 # $1 - command (optional)
1340 #
1341 _require_aiodio()
1342 {
1343     if [ -z "$1" ]
1344     then
1345         AIO_TEST=src/aio-dio-regress/aiodio_sparse2
1346         [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
1347     else
1348         AIO_TEST=src/aio-dio-regress/$1
1349         [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
1350     fi
1351 }
1352
1353 # run an aio-dio program
1354 # $1 - command
1355 _run_aiodio()
1356 {
1357     if [ -z "$1" ]
1358     then
1359         echo "usage: _run_aiodio command_name" 2>&1
1360         status=1; exit 1
1361     fi
1362
1363     _require_aiodio $1
1364
1365     local testtemp=$TEST_DIR/aio-testfile
1366     rm -f $testtemp
1367     $AIO_TEST $testtemp 2>&1
1368     status=$?
1369     rm -f $testtemp
1370
1371     return $status
1372 }
1373
1374 # indicate whether YP/NIS is active or not
1375 #
1376 _yp_active()
1377 {
1378         local dn
1379         dn=$(domainname 2>/dev/null)
1380         test -n "${dn}" -a "${dn}" != "(none)"
1381         echo $?
1382 }
1383
1384 # cat the password file
1385 #
1386 _cat_passwd()
1387 {
1388         [ $(_yp_active) -eq 0 ] && ypcat passwd
1389         cat /etc/passwd
1390 }
1391
1392 # cat the group file
1393 #
1394 _cat_group()
1395 {
1396         [ $(_yp_active) -eq 0 ] && ypcat group
1397         cat /etc/group
1398 }
1399
1400 # check for the fsgqa user on the machine
1401 #
1402 _require_user()
1403 {
1404     qa_user=fsgqa
1405     _cat_passwd | grep -q $qa_user
1406     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
1407     echo /bin/true | su $qa_user
1408     [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
1409 }
1410
1411 # check for the fsgqa group on the machine
1412 #
1413 _require_group()
1414 {
1415     qa_group=fsgqa
1416     _cat_group | grep -q $qa_group
1417     [ "$?" == "0" ] || _notrun "$qa_group user not defined."
1418 }
1419
1420 _filter_user_do()
1421 {
1422         perl -ne "
1423 s,.*Permission\sdenied.*,Permission denied,;
1424 s,.*no\saccess\sto\stty.*,,;
1425 s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
1426 s,^\s*$,,;
1427         print;"
1428 }
1429
1430 _user_do()
1431 {
1432     if [ "$HOSTOS" == "IRIX" ]
1433         then
1434         echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
1435     else
1436         echo $1 | su $qa_user 2>&1 | _filter_user_do
1437     fi
1438 }
1439
1440 _require_xfs_io_command()
1441 {
1442         if [ $# -ne 1 ]
1443         then
1444                 echo "Usage: _require_xfs_io_command command" 1>&2
1445                 exit 1
1446         fi
1447         command=$1
1448
1449         testfile=$TEST_DIR/$$.xfs_io
1450         case $command in
1451         "falloc" )
1452                 testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $testfile 2>&1`
1453                 ;;
1454         "fpunch" | "fcollapse" | "zero" | "fzero" )
1455                 testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1456                         -c "$command 4k 8k" $testfile 2>&1`
1457                 ;;
1458         "fiemap")
1459                 testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1460                         -c "fiemap -v" $testfile 2>&1`
1461                 ;;
1462         "flink" )
1463                 testio=`$XFS_IO_PROG -T -F -c "flink $testfile" \
1464                         $TEST_DIR 2>&1`
1465                 echo $testio | egrep -q "invalid option|Is a directory" && \
1466                         _notrun "xfs_io $command support is missing"
1467                 ;;
1468         *)
1469                 testio=`$XFS_IO_PROG -c "$command help" 2>&1`
1470         esac
1471
1472         rm -f $testfile 2>&1 > /dev/null
1473         echo $testio | grep -q "not found" && \
1474                 _notrun "xfs_io $command support is missing"
1475         echo $testio | grep -q "Operation not supported" && \
1476                 _notrun "xfs_io $command failed (old kernel/wrong fs?)"
1477 }
1478
1479 # Check that a fs has enough free space (in 1024b blocks)
1480 #
1481 _require_fs_space()
1482 {
1483         MNT=$1
1484         BLOCKS=$2       # in units of 1024
1485         let GB=$BLOCKS/1024/1024
1486
1487         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
1488         [ $FREE_BLOCKS -lt $BLOCKS ] && \
1489                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
1490 }
1491
1492 #
1493 # Check if the filesystem supports sparse files.
1494 #
1495 # Unfortunately there is no better way to do this than a manual black list.
1496 #
1497 _require_sparse_files()
1498 {
1499     case $FSTYP in
1500     hfsplus)
1501         _notrun "Sparse files not supported by this filesystem type: $FSTYP"
1502         ;;
1503     *)
1504         ;;
1505     esac
1506 }
1507
1508 _require_debugfs()
1509 {
1510     #boot_params always present in debugfs
1511     [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
1512 }
1513
1514 _require_fail_make_request()
1515 {
1516     [ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
1517         || _notrun "$DEBUGFS_MNT/fail_make_request \
1518  not found. Seems that CONFIG_FAIL_MAKE_REQUEST kernel config option not enabled"
1519 }
1520
1521 #
1522 # Check if the file system supports seek_data/hole
1523 #
1524 _require_seek_data_hole()
1525 {
1526     testfile=$TEST_DIR/$$.seek
1527     testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`
1528     rm -f $testfile &>/dev/null
1529     echo $testseek | grep -q "Kernel does not support" && \
1530         _notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
1531 }
1532
1533 # check that a FS on a device is mounted
1534 # if so, return mount point
1535 #
1536 _is_mounted()
1537 {
1538     if [ $# -ne 1 ]
1539     then
1540         echo "Usage: _is_mounted device" 1>&2
1541         exit 1
1542     fi
1543
1544     device=$1
1545
1546     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
1547         pattern        { print $3 ; exit 0 }
1548         END            { exit 1 }
1549     '
1550     then
1551         echo "_is_mounted: $device is not a mounted $FSTYP FS"
1552         exit 1
1553     fi
1554 }
1555
1556 # remount a FS to a new mode (ro or rw)
1557 #
1558 _remount()
1559 {
1560     if [ $# -ne 2 ]
1561     then
1562         echo "Usage: _remount device ro/rw" 1>&2
1563         exit 1
1564     fi
1565     device=$1
1566     mode=$2
1567
1568     if ! mount -o remount,$mode $device
1569     then
1570         echo "_remount: failed to remount filesystem on $device as $mode"
1571         exit 1
1572     fi
1573 }
1574
1575 # Run the appropriate repair/check on a filesystem
1576 #
1577 # if the filesystem is mounted, it's either remounted ro before being
1578 # checked or it's unmounted and then remounted
1579 #
1580
1581 # If set, we remount ro instead of unmounting for fsck
1582 USE_REMOUNT=0
1583
1584 _umount_or_remount_ro()
1585 {
1586     if [ $# -ne 1 ]
1587     then
1588         echo "Usage: _umount_or_remount_ro <device>" 1>&2
1589         exit 1
1590     fi
1591
1592     device=$1
1593     mountpoint=`_is_mounted $device`
1594
1595     if [ $USE_REMOUNT -eq 0 ]; then
1596         $UMOUNT_PROG $device
1597     else
1598         _remount $device ro
1599     fi
1600     echo "$mountpoint"
1601 }
1602
1603 _mount_or_remount_rw()
1604 {
1605     if [ $# -ne 3 ]
1606     then
1607         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
1608         exit 1
1609     fi
1610     mount_opts=$1
1611     device=$2
1612     mountpoint=$3
1613
1614     if [ $USE_REMOUNT -eq 0 ]
1615     then
1616         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
1617         then
1618             echo "!!! failed to remount $device on $mountpoint"
1619             return 0 # ok=0
1620         fi
1621     else
1622         _remount $device rw
1623     fi
1624
1625     return 1 # ok=1
1626 }
1627
1628 # Check a generic filesystem in no-op mode; this assumes that the
1629 # underlying fsck program accepts "-n" for a no-op (check-only) run,
1630 # and that it will still return an errno for corruption in this mode.
1631 #
1632 # Filesystems which don't support this will need to define their
1633 # own check routine.
1634 #
1635 _check_generic_filesystem()
1636 {
1637     device=$1
1638
1639     # If type is set, we're mounted
1640     type=`_fs_type $device`
1641     ok=1
1642
1643     if [ "$type" = "$FSTYP" ]
1644     then
1645         # mounted ...
1646         mountpoint=`_umount_or_remount_ro $device`
1647     fi
1648
1649     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
1650     if [ $? -ne 0 ]
1651     then
1652         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1653
1654         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
1655         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1656         cat $tmp.fsck                           >>$seqres.full
1657         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1658
1659         ok=0
1660     fi
1661     rm -f $tmp.fsck
1662
1663     if [ $ok -eq 0 ]
1664     then
1665         echo "*** mount output ***"             >>$seqres.full
1666         _mount                                  >>$seqres.full
1667         echo "*** end mount output"             >>$seqres.full
1668     elif [ "$type" = "$FSTYP" ]
1669     then
1670         # was mounted ...
1671         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1672         ok=$?
1673     fi
1674
1675     if [ $ok -eq 0 ]; then
1676         status=1
1677         exit 1
1678     fi
1679
1680     return 0
1681 }
1682
1683 # run xfs_check and friends on a FS.
1684
1685 _check_xfs_filesystem()
1686 {
1687     if [ $# -ne 3 ]
1688     then
1689         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
1690         exit 1
1691     fi
1692
1693     extra_mount_options=""
1694     device=$1
1695     if [ "$2" != "none" ]; then
1696         extra_log_options="-l$2"
1697         extra_mount_options="-ologdev=$2"
1698     fi
1699
1700     if [ "$3" != "none" ]; then
1701         extra_rt_options="-r$3"
1702         extra_mount_options=$extra_mount_options" -ortdev=$3"
1703     fi
1704     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
1705
1706     [ "$FSTYP" != xfs ] && return 0
1707
1708     type=`_fs_type $device`
1709     ok=1
1710
1711     if [ "$type" = "xfs" ]
1712     then
1713         # mounted ...
1714         mountpoint=`_umount_or_remount_ro $device`
1715     fi
1716
1717     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1718                 | tee $tmp.logprint | grep -q "<CLEAN>"
1719     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1720     then
1721         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
1722
1723         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
1724         echo "*** xfs_logprint -t output ***"   >>$seqres.full
1725         cat $tmp.logprint                       >>$seqres.full
1726         echo "*** end xfs_logprint output"      >>$seqres.full
1727
1728         ok=0
1729     fi
1730
1731     # xfs_check runs out of memory on large files, so even providing the test
1732     # option (-t) to avoid indexing the free space trees doesn't make it pass on
1733     # large filesystems. Avoid it.
1734     if [ "$LARGE_SCRATCH_DEV" != yes ]; then
1735             _xfs_check $extra_log_options $device 2>&1 |\
1736                  _fix_malloc >$tmp.fs_check
1737     fi
1738     if [ -s $tmp.fs_check ]
1739     then
1740         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
1741
1742         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1743         echo "*** xfs_check output ***"         >>$seqres.full
1744         cat $tmp.fs_check                       >>$seqres.full
1745         echo "*** end xfs_check output"         >>$seqres.full
1746
1747         ok=0
1748     fi
1749
1750     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1751     if [ $? -ne 0 ]
1752     then
1753         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
1754
1755         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1756         echo "*** xfs_repair -n output ***"     >>$seqres.full
1757         cat $tmp.repair | _fix_malloc           >>$seqres.full
1758         echo "*** end xfs_repair output"        >>$seqres.full
1759
1760         ok=0
1761     fi
1762     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1763
1764     if [ $ok -eq 0 ]
1765     then
1766         echo "*** mount output ***"             >>$seqres.full
1767         _mount                                  >>$seqres.full
1768         echo "*** end mount output"             >>$seqres.full
1769     elif [ "$type" = "xfs" ]
1770     then
1771         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1772     fi
1773
1774     if [ $ok -eq 0 ]; then
1775         status=1
1776         if [ "$iam" != "check" ]; then
1777                 exit 1
1778         fi
1779     fi
1780
1781     return 0
1782 }
1783
1784 # Filter the knowen errors the UDF Verifier reports.
1785 _udf_test_known_error_filter()
1786 {
1787         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."
1788
1789 }
1790
1791 _check_udf_filesystem()
1792 {
1793     [ "$DISABLE_UDF_TEST" == "1" ] && return
1794
1795     if [ $# -ne 1 -a $# -ne 2 ]
1796     then
1797         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1798         exit 1
1799     fi
1800
1801     if [ ! -x $here/src/udf_test ]
1802     then
1803         echo "udf_test not installed, please download and build the Philips"
1804         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1805         echo "Then copy the udf_test binary to $here/src/."
1806         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1807         echo "to 1."
1808         return
1809     fi
1810
1811     device=$1
1812     if [ $# -eq 2 ];
1813     then
1814         LAST_BLOCK=`expr \( $2 - 1 \)`
1815         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1816     fi
1817
1818     rm -f $seqres.checkfs
1819     sleep 1 # Due to a problem with time stamps in udf_test
1820     $here/src/udf_test $OPT_ARG $device | tee $seqres.checkfs | egrep "Error|Warning" | \
1821         _udf_test_known_error_filter | \
1822         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" && \
1823         echo "Warning UDF Verifier reported errors see $seqres.checkfs."
1824 }
1825
1826 _check_xfs_test_fs()
1827 {
1828     TEST_LOG="none"
1829     TEST_RT="none"
1830     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1831         TEST_LOG="$TEST_LOGDEV"
1832
1833     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1834         TEST_RT="$TEST_RTDEV"
1835
1836     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1837
1838     # check for ipath consistency
1839     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1840         # errors go to stderr
1841         xfs_check_ipaths $TEST_DIR >/dev/null
1842         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1843     fi
1844 }
1845
1846 _check_btrfs_filesystem()
1847 {
1848     device=$1
1849
1850     # If type is set, we're mounted
1851     type=`_fs_type $device`
1852     ok=1
1853
1854     if [ "$type" = "$FSTYP" ]
1855     then
1856         # mounted ...
1857         mountpoint=`_umount_or_remount_ro $device`
1858     fi
1859
1860     btrfsck $device >$tmp.fsck 2>&1
1861     if [ $? -ne 0 ]
1862     then
1863         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1864
1865         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1866         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1867         cat $tmp.fsck                           >>$seqres.full
1868         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1869
1870         ok=0
1871     fi
1872     rm -f $tmp.fsck
1873
1874     if [ $ok -eq 0 ]
1875     then
1876         echo "*** mount output ***"             >>$seqres.full
1877         _mount                                  >>$seqres.full
1878         echo "*** end mount output"             >>$seqres.full
1879     elif [ "$type" = "$FSTYP" ]
1880     then
1881         # was mounted ...
1882         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1883         ok=$?
1884     fi
1885
1886     if [ $ok -eq 0 ]; then
1887         status=1
1888         exit 1
1889     fi
1890
1891     return 0
1892 }
1893
1894 _check_test_fs()
1895 {
1896     case $FSTYP in
1897     xfs)
1898         _check_xfs_test_fs
1899         ;;
1900     nfs)
1901         # no way to check consistency for nfs
1902         ;;
1903     cifs)
1904         # no way to check consistency for cifs
1905         ;;
1906     udf)
1907         # do nothing for now
1908         ;;
1909     btrfs)
1910         _check_btrfs_filesystem $TEST_DEV
1911         ;;
1912     tmpfs)
1913         # no way to check consistency for tmpfs
1914         ;;
1915     *)
1916         _check_generic_filesystem $TEST_DEV
1917         ;;
1918     esac
1919 }
1920
1921 _check_scratch_fs()
1922 {
1923     device=$SCRATCH_DEV
1924     [ $# -eq 1 ] && device=$1
1925
1926     case $FSTYP in
1927     xfs)
1928         SCRATCH_LOG="none"
1929         SCRATCH_RT="none"
1930         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1931             SCRATCH_LOG="$SCRATCH_LOGDEV"
1932
1933         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1934             SCRATCH_RT="$SCRATCH_RTDEV"
1935
1936         _check_xfs_filesystem $device $SCRATCH_LOG $SCRATCH_RT
1937         ;;
1938     udf)
1939         _check_udf_filesystem $device $udf_fsize
1940         ;;
1941     nfs*)
1942         # Don't know how to check an NFS filesystem, yet.
1943         ;;
1944     cifs)
1945         # Don't know how to check a CIFS filesystem, yet.
1946         ;;
1947     btrfs)
1948         _check_btrfs_filesystem $device
1949         ;;
1950     tmpfs)
1951         # no way to check consistency for tmpfs
1952         ;;
1953     *)
1954         _check_generic_filesystem $device
1955         ;;
1956     esac
1957 }
1958
1959 _full_fstyp_details()
1960 {
1961      [ -z "$FSTYP" ] && FSTYP=xfs
1962      if [ $FSTYP = xfs ]; then
1963         if [ -d /proc/fs/xfs ]; then
1964             if grep -q 'debug 0' /proc/fs/xfs/stat; then
1965                 FSTYP="$FSTYP (non-debug)"
1966             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
1967                 FSTYP="$FSTYP (debug)"
1968             fi
1969         else
1970             if uname -a | grep -qi 'debug'; then
1971                 FSTYP="$FSTYP (debug)"
1972             else
1973                 FSTYP="$FSTYP (non-debug)"
1974             fi
1975         fi
1976      fi
1977      echo $FSTYP
1978 }
1979
1980 _full_platform_details()
1981 {
1982      os=`uname -s`
1983      host=`hostname -s`
1984      kernel=`uname -r`
1985      platform=`uname -m`
1986      echo "$os/$platform $host $kernel"
1987 }
1988
1989 _link_out_file()
1990 {
1991         if [ -z "$1" -o -z "$2" ]; then
1992                 echo Error must pass src and dst.
1993                 exit
1994         fi
1995         rm -f $2
1996         if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1997                 ln -s $1.irix $2
1998         elif [ "`uname`" == "Linux" ]; then
1999                 ln -s $1.linux $2
2000         else
2001                 echo Error test $seq does not run on the operating system: `uname`
2002                 exit
2003         fi
2004 }
2005
2006 _die()
2007 {
2008         echo $@
2009         exit 1
2010 }
2011
2012 #takes files, randomdata
2013 _nfiles()
2014 {
2015         f=0
2016         while [ $f -lt $1 ]
2017         do
2018                 file=f$f
2019                 echo > $file
2020                 if [ $size -gt 0 ]; then
2021                     if [ "$2" == "false" ]; then
2022                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
2023                     else
2024                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
2025                     fi
2026                 fi
2027                 let f=$f+1
2028         done
2029 }
2030
2031 # takes dirname, depth, randomdata
2032 _descend()
2033 {
2034         dirname=$1; depth=$2; randomdata=$3
2035         mkdir $dirname  || die "mkdir $dirname failed"
2036         cd $dirname
2037
2038         _nfiles $files $randomdata          # files for this dir and data type
2039
2040         [ $depth -eq 0 ] && return
2041         let deep=$depth-1 # go 1 down
2042
2043         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
2044
2045         d=0
2046         while [ $d -lt $dirs ]
2047         do
2048                 _descend d$d $deep &
2049                 let d=$d+1
2050                 wait
2051         done
2052 }
2053
2054 # Populate a filesystem with inodes for performance experiments
2055 #
2056 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
2057 #
2058 _populate_fs()
2059 {
2060     here=`pwd`
2061     dirs=5          # ndirs in each subdir till leaves
2062     size=0          # sizeof files in K
2063     files=100       # num files in _each_ subdir
2064     depth=2         # depth of tree from root to leaves
2065     verbose=false
2066     root=root       # path of initial root of directory tree
2067     randomdata=false # -x data type urandom or zero
2068
2069     OPTIND=1
2070     while getopts "d:f:n:r:s:v:x" c
2071     do
2072         case $c in
2073         d)      depth=$OPTARG;;
2074         n)      dirs=$OPTARG;;
2075         f)      files=$OPTARG;;
2076         s)      size=$OPTARG;;
2077         v)      verbose=true;;
2078         r)      root=$OPTARG;;
2079         x)      randomdata=true;;
2080         esac
2081     done
2082
2083     _descend $root $depth $randomdata
2084     wait
2085
2086     cd $here
2087
2088     [ $verbose = true ] && echo done
2089 }
2090
2091 # query whether the given file has the given inode flag set
2092 #
2093 _test_inode_flag()
2094 {
2095     flag=$1
2096     file=$2
2097
2098     if which $XFS_IO_PROG >/dev/null; then
2099         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
2100             return 0
2101         fi
2102     fi
2103     return 1
2104 }
2105
2106 # query the given files extsize allocator hint in bytes (if any)
2107 #
2108 _test_inode_extsz()
2109 {
2110     file=$1
2111     blocks=""
2112
2113     if which $XFS_IO_PROG >/dev/null; then
2114         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
2115                 awk '/^xattr.extsize =/ { print $3 }'`
2116     fi
2117     [ -z "$blocks" ] && blocks="0"
2118     echo $blocks
2119 }
2120
2121 # scratch_dev_pool should contain the disks pool for the btrfs raid
2122 _require_scratch_dev_pool()
2123 {
2124         local i
2125         local ndevs
2126
2127         if [ -z "$SCRATCH_DEV_POOL" ]; then
2128                 _notrun "this test requires a valid \$SCRATCH_DEV_POOL"
2129         fi
2130
2131         if [ -z "$1" ]; then
2132                 ndevs=2
2133         else
2134                 ndevs=$1
2135         fi
2136
2137         # btrfs test case needs ndevs or more scratch_dev_pool; other FS not sure
2138         # so fail it
2139         case $FSTYP in
2140         btrfs)
2141                 if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt $ndevs ]; then
2142                         _notrun "btrfs and this test needs $ndevs or more disks in SCRATCH_DEV_POOL"
2143                 fi
2144         ;;
2145         *)
2146                 _notrun "dev_pool is not supported by fstype \"$FSTYP\""
2147         ;;
2148         esac
2149
2150         for i in $SCRATCH_DEV_POOL; do
2151                 if [ "`_is_block_dev $i`" = "" ]; then
2152                         _notrun "this test requires valid block disk $i"
2153                 fi
2154                 if [ "`_is_block_dev $i`" = "`_is_block_dev $TEST_DEV`" ]; then
2155                         _notrun "$i is part of TEST_DEV, this test requires unique disks"
2156                 fi
2157                 if _mount | grep -q $i; then
2158                         if ! $UMOUNT_PROG $i; then
2159                             echo "failed to unmount $i - aborting"
2160                             exit 1
2161                         fi
2162                 fi
2163                 # to help better debug when something fails, we remove
2164                 # traces of previous btrfs FS on the dev.
2165                 dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
2166         done
2167 }
2168
2169 # ensure devices in SCRATCH_DEV_POOL are of the same size
2170 # must be called after _require_scratch_dev_pool
2171 _require_scratch_dev_pool_equal_size()
2172 {
2173         local _size
2174         local _newsize
2175         local _dev
2176
2177         # SCRATCH_DEV has been set to the first device in SCRATCH_DEV_POOL
2178         _size=`_get_device_size $SCRATCH_DEV`
2179         for _dev in $SCRATCH_DEV_POOL; do
2180                 _newsize=`_get_device_size $_dev`
2181                 if [ $_size -ne $_newsize ]; then
2182                         _notrun "This test requires devices in SCRATCH_DEV_POOL have the same size"
2183                 fi
2184         done
2185 }
2186
2187 # We will check if the device is deletable
2188 _require_deletable_scratch_dev_pool()
2189 {
2190         local i
2191         local x
2192         for i in $SCRATCH_DEV_POOL; do
2193                 x=`echo $i | cut -d"/" -f 3`
2194                 if [ ! -f /sys/class/block/${x}/device/delete ]; then
2195                         _notrun "$i is a device which is not deletable"
2196                 fi
2197         done
2198 }
2199
2200 # We check for btrfs and (optionally) features of the btrfs command
2201 _require_btrfs()
2202 {
2203         cmd=$1
2204         _require_command $BTRFS_UTIL_PROG btrfs
2205         if [ -z "$1" ]; then
2206                 return 1;
2207         fi
2208         $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
2209         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
2210 }
2211
2212 # Check that fio is present, and it is able to execute given jobfile
2213 _require_fio()
2214 {
2215         job=$1
2216
2217         _require_command $FIO_PROG
2218         if [ -z "$1" ]; then
2219                 return 1;
2220         fi
2221
2222         $FIO_PROG --warnings-fatal --showcmd $job >> $seqres.full 2>&1
2223         [ $? -eq 0 ] || _notrun "$FIO_PROG too old, see $seqres.full"
2224 }
2225
2226 # Does freeze work on this fs?
2227 _require_freeze()
2228 {
2229         xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
2230         result=$? 
2231         xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
2232         [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
2233 }
2234
2235 # arg 1 is dev to remove and is output of the below eg.
2236 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2237 _devmgt_remove()
2238 {
2239         local lun=$1
2240         local disk=$2
2241
2242         echo 1 > /sys/class/scsi_device/${lun}/device/delete || _fail "Remove disk failed"
2243
2244         stat $disk > /dev/null 2>&1
2245         while [ $? -eq 0 ]; do
2246                 sleep 1
2247                 stat $disk > /dev/null 2>&1
2248         done
2249 }
2250
2251 # arg 1 is dev to add and is output of the below eg.
2252 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2253 _devmgt_add()
2254 {
2255         local h
2256         local tdl
2257         # arg 1 will be in h:t:d:l format now in the h and "t d l" format
2258         h=`echo ${1} | cut -d":" -f 1`
2259         tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`
2260
2261         echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"
2262
2263         # ensure the device comes online
2264         dev_back_oneline=0
2265         for i in `seq 1 10`; do
2266                 if [ -d /sys/class/scsi_device/${1}/device/block ]; then
2267                         dev=`ls /sys/class/scsi_device/${1}/device/block`
2268                         for j in `seq 1 10`;
2269                         do
2270                                 stat /dev/$dev > /dev/null 2>&1
2271                                 if [ $? -eq 0 ]; then
2272                                         dev_back_oneline=1
2273                                         break
2274                                 fi
2275                                 sleep 1
2276                         done
2277                         break
2278                 else
2279                         sleep 1
2280                 fi
2281         done
2282         if [ $dev_back_oneline -eq 0 ]; then
2283                 echo "/dev/$dev online failed" >> $seqres.full
2284         else
2285                 echo "/dev/$dev is back online" >> $seqres.full
2286         fi
2287 }
2288
2289 _require_fstrim()
2290 {
2291         if [ -z "$FSTRIM_PROG" ]; then
2292                 _notrun "This test requires fstrim utility."
2293         fi
2294 }
2295
2296 _test_batched_discard()
2297 {
2298         if [ $# -ne 1 ]; then
2299                 echo "Usage: _test_batched_discard mnt_point" 1>&2
2300                 exit 1
2301         fi
2302         _require_fstrim
2303         $FSTRIM_PROG ${1} &>/dev/null
2304 }
2305
2306 _require_dumpe2fs()
2307 {
2308         if [ -z "$DUMPE2FS_PROG" ]; then
2309                 _notrun "This test requires dumpe2fs utility."
2310         fi
2311 }
2312
2313 _require_ugid_map()
2314 {
2315         if [ ! -e /proc/self/uid_map ]; then
2316                 _notrun "This test requires procfs uid_map support."
2317         fi
2318         if [ ! -e /proc/self/gid_map ]; then
2319                 _notrun "This test requires procfs gid_map support."
2320         fi
2321 }
2322
2323 _require_cp_reflink()
2324 {
2325        cp --help | grep -q reflink || \
2326                _notrun "This test requires a cp with --reflink support."
2327 }
2328
2329 _require_fssum()
2330 {
2331         FSSUM_PROG=$here/src/fssum
2332         [ -x $FSSUM_PROG ] || _notrun "fssum not built"
2333 }
2334
2335 _require_btrfs_cloner()
2336 {
2337         CLONER_PROG=$here/src/cloner
2338         [ -x $CLONER_PROG ] || \
2339                 _notrun "cloner binary not present at $CLONER_PROG"
2340 }
2341
2342 # Given 2 files, verify that they have the same mapping but different
2343 # inodes - i.e. an undisturbed reflink
2344 # Silent if so, make noise if not
2345 _verify_reflink()
2346 {
2347        # not a hard link or symlink?
2348        cmp -s  <(stat -c '%i' $1) <(stat -c '%i' $2) \
2349                && echo "$1 and $2 are not reflinks: same inode number"
2350
2351        # same mapping?
2352        diff -u <($XFS_IO_PROG -c "fiemap" $1 | grep -v $1) \
2353                <($XFS_IO_PROG -c "fiemap" $2 | grep -v $2) \
2354                || echo "$1 and $2 are not reflinks: different extents"
2355 }
2356
2357 _require_relatime()
2358 {
2359         _scratch_mkfs > /dev/null 2>&1
2360         _mount -t $FSTYP -o relatime $SCRATCH_DEV $SCRATCH_MNT || \
2361                 _notrun "relatime not supported by the current kernel"
2362         _scratch_unmount
2363 }
2364
2365 _require_userns()
2366 {
2367         [ -x src/nsexec ] || _notrun "src/nsexec executable not found"
2368         src/nsexec -U true 2>/dev/null || _notrun "userns not supported by this kernel"
2369 }
2370
2371 _create_loop_device()
2372 {
2373         file=$1
2374         dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"
2375         echo $dev
2376 }
2377
2378 _destroy_loop_device()
2379 {
2380         dev=$1
2381         losetup -d $dev || _fail "Cannot destroy loop device $dev"
2382 }
2383
2384 _scale_fsstress_args()
2385 {
2386     args=""
2387     while [ $# -gt 0 ]; do
2388         case "$1" in
2389             -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
2390             -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
2391             *) args="$args $1" ;;
2392         esac
2393         shift
2394     done
2395     echo $args
2396 }
2397
2398 #
2399 # Return the logical block size if running on a block device,
2400 # else substitute the page size.
2401 #
2402 _min_dio_alignment()
2403 {
2404     dev=$1
2405
2406     if [ -b "$dev" ]; then
2407         blockdev --getss $dev
2408     else
2409         $here/src/feature -s
2410     fi
2411 }
2412
2413 run_check()
2414 {
2415         echo "# $@" >> $seqres.full 2>&1
2416         "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
2417 }
2418
2419 _run_btrfs_util_prog()
2420 {
2421         run_check $BTRFS_UTIL_PROG $*
2422 }
2423
2424 _require_btrfs_send_stream_version()
2425 {
2426         $BTRFS_UTIL_PROG send 2>&1 | \
2427                 grep '^[ \t]*\-\-stream\-version <version>' > /dev/null 2>&1
2428         if [ $? -ne 0 ]; then
2429                 _notrun "Missing btrfs-progs send --stream-version command line option, skipped this test"
2430         fi
2431
2432         # test if btrfs kernel supports send stream version 2
2433         if [ ! -f /sys/fs/btrfs/send/stream_version ]; then
2434                 _notrun "Missing btrfs kernel patch for send stream version 2, skipped this test"
2435         fi
2436 }
2437
2438 _require_btrfs_mkfs_feature()
2439 {
2440         if [ -z $1 ]; then
2441                 echo "Missing feature name argument for _require_btrfs_mkfs_feature"
2442                 exit 1
2443         fi
2444         feat=$1
2445         $MKFS_BTRFS_PROG -O list-all 2>&1 | \
2446                 grep '^[ \t]*'"$feat"'\b' > /dev/null 2>&1
2447         [ $? -eq 0 ] || \
2448                 _notrun "Feature $feat not supported in the available version of mkfs.btrfs"
2449 }
2450
2451 _require_btrfs_fs_feature()
2452 {
2453         if [ -z $1 ]; then
2454                 echo "Missing feature name argument for _require_btrfs_fs_feature"
2455                 exit 1
2456         fi
2457         feat=$1
2458         modprobe btrfs > /dev/null 2>&1
2459         [ -e /sys/fs/btrfs/features/$feat ] || \
2460                 _notrun "Feature $feat not supported by the available btrfs version"
2461 }
2462
2463 _require_test_symlinks()
2464 {
2465         # IRIX UDF does not support symlinks
2466         [ "$HOSTOS" = "IRIX" -a "$FSTYP" = 'udf' ] && \
2467                 _notrun "Require symlinks support"
2468         target=`mktemp -p $TEST_DIR`
2469         link=`mktemp -p $TEST_DIR -u`
2470         ln -s `basename $target` $link
2471         if [ "$?" -ne 0 ]; then
2472                 rm -f $target
2473                 _notrun "Require symlinks support"
2474         fi
2475         rm -f $target $link
2476 }
2477
2478 _require_test_fcntl_advisory_locks()
2479 {
2480         [ "$FSTYP" != "cifs" ] && return 0
2481         cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -q "nobrl" && return 0
2482         cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -qE "nounix|forcemand" && \
2483                 _notrun "Require fcntl advisory locks support"
2484 }
2485
2486 _get_total_inode()
2487 {
2488         if [ -z "$1" ]; then
2489                 echo "Usage: _get_total_inode <mnt>"
2490                 exit 1
2491         fi
2492         local nr_inode;
2493         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $3}'`
2494         echo $nr_inode
2495 }
2496
2497 _get_used_inode()
2498 {
2499         if [ -z "$1" ]; then
2500                 echo "Usage: _get_used_inode <mnt>"
2501                 exit 1
2502         fi
2503         local nr_inode;
2504         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $4}'`
2505         echo $nr_inode
2506 }
2507
2508 _get_free_inode()
2509 {
2510         if [ -z "$1" ]; then
2511                 echo "Usage: _get_free_inode <mnt>"
2512                 exit 1
2513         fi
2514         local nr_inode;
2515         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $5}'`
2516         echo $nr_inode
2517 }
2518
2519 # get btrfs profile configs being tested
2520 #
2521 # A set of pre-set profile configs are exported via _btrfs_profile_configs
2522 # array. Default configs can be overridden by setting BTRFS_PROFILE_CONFIGS
2523 # var in the format "metadata_profile:data_profile", multiple configs can be
2524 # seperated by space, e.g.
2525 # export BTRFS_PROFILE_CONFIGS="raid0:raid0 raid1:raid1 dup:single"
2526 _btrfs_get_profile_configs()
2527 {
2528         if [ "$FSTYP" != "btrfs" ]; then
2529                 return
2530         fi
2531
2532         # no user specified btrfs profile configs, export the default configs
2533         if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
2534                 # default configs
2535                 _btrfs_profile_configs=(
2536                         "-m single -d single"
2537                         "-m dup -d single"
2538                         "-m raid0 -d raid0"
2539                         "-m raid1 -d raid0"
2540                         "-m raid1 -d raid1"
2541                         "-m raid10 -d raid10"
2542                         "-m raid5 -d raid5"
2543                         "-m raid6 -d raid6"
2544                 )
2545
2546                 # remove dup/raid5/raid6 profiles if we're doing device replace
2547                 # dup profile indicates only one device being used (SCRATCH_DEV),
2548                 # but we don't want to replace SCRATCH_DEV, which will be used in
2549                 # _scratch_mount/_check_scratch_fs etc.
2550                 # and raid5/raid6 doesn't support replace yet
2551                 if [ "$1" == "replace" ]; then
2552                         _btrfs_profile_configs=(
2553                                 "-m single -d single"
2554                                 "-m raid0 -d raid0"
2555                                 "-m raid1 -d raid0"
2556                                 "-m raid1 -d raid1"
2557                                 "-m raid10 -d raid10"
2558                                 # add these back when raid5/6 is working with replace
2559                                 #"-m raid5 -d raid5"
2560                                 #"-m raid6 -d raid6"
2561                         )
2562                 fi
2563                 export _btrfs_profile_configs
2564                 return
2565         fi
2566
2567         # parse user specified btrfs profile configs
2568         local i=0
2569         local cfg=""
2570         for cfg in $BTRFS_PROFILE_CONFIGS; do
2571                 # turn "metadata:data" format to "-m metadata -d data"
2572                 # and assign it to _btrfs_profile_configs array
2573                 cfg=`echo "$cfg" | sed -e 's/^/-m /' -e 's/:/ -d /'`
2574                 _btrfs_profile_configs[$i]="$cfg"
2575                 let i=i+1
2576         done
2577
2578         if [ "$1" == "replace" ]; then
2579                 if echo ${_btrfs_profile_configs[*]} | grep -q raid[56]; then
2580                         _notrun "RAID5/6 doesn't support btrfs device replace yet"
2581                 fi
2582                 if echo ${_btrfs_profile_configs[*]} | grep -q dup; then
2583                         _notrun "Do not set dup profile in btrfs device replace test"
2584                 fi
2585         fi
2586         export _btrfs_profile_configs
2587 }
2588
2589 # stress btrfs by running balance operation in a loop
2590 _btrfs_stress_balance()
2591 {
2592         local btrfs_mnt=$1
2593         while true; do
2594                 $BTRFS_UTIL_PROG balance start $btrfs_mnt
2595         done
2596 }
2597
2598 # stress btrfs by creating/mounting/umounting/deleting subvolume in a loop
2599 _btrfs_stress_subvolume()
2600 {
2601         local btrfs_dev=$1
2602         local btrfs_mnt=$2
2603         local subvol_name=$3
2604         local subvol_mnt=$4
2605
2606         mkdir -p $subvol_mnt
2607         while true; do
2608                 $BTRFS_UTIL_PROG subvolume create $btrfs_mnt/$subvol_name
2609                 $MOUNT_PROG -o subvol=$subvol_name $btrfs_dev $subvol_mnt
2610                 $UMOUNT_PROG $subvol_mnt
2611                 $BTRFS_UTIL_PROG subvolume delete $btrfs_mnt/$subvol_name
2612         done
2613 }
2614
2615 # stress btrfs by running scrub in a loop
2616 _btrfs_stress_scrub()
2617 {
2618         local btrfs_mnt=$1
2619         while true; do
2620                 $BTRFS_UTIL_PROG scrub start -B $btrfs_mnt
2621         done
2622 }
2623
2624 # stress btrfs by defragmenting every file/dir in a loop and compress file
2625 # contents while defragmenting if second argument is not "nocompress"
2626 _btrfs_stress_defrag()
2627 {
2628         local btrfs_mnt=$1
2629         local compress=$2
2630
2631         while true; do
2632                 if [ "$compress" == "nocompress" ]; then
2633                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2634                         $BTRFS_UTIL_PROG filesystem defrag {} \;
2635                 else
2636                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2637                         $BTRFS_UTIL_PROG filesystem defrag -clzo {} \;
2638                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2639                         $BTRFS_UTIL_PROG filesystem defrag -czlib {} \;
2640                 fi
2641         done
2642 }
2643
2644 # stress btrfs by remounting it with different compression algorithms in a loop
2645 # run this with fsstress running at background could exercise the compression
2646 # code path and ensure no race when switching compression algorithm with constant
2647 # I/O activity.
2648 _btrfs_stress_remount_compress()
2649 {
2650         local btrfs_mnt=$1
2651         while true; do
2652                 for algo in no zlib lzo; do
2653                         $MOUNT_PROG -o remount,compress=$algo $btrfs_mnt
2654                 done
2655         done
2656 }
2657
2658 # stress btrfs by replacing devices in a loop
2659 # Note that at least 3 devices are needed in SCRATCH_DEV_POOL and the last
2660 # device should be free(not used by btrfs)
2661 _btrfs_stress_replace()
2662 {
2663         local btrfs_mnt=$1
2664
2665         # The device number in SCRATCH_DEV_POOL should be at least 3,
2666         # one is SCRATCH_DEV, one is to be replaced, one is free device
2667         # we won't replace SCRATCH_DEV, see below for reason
2668         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt 3 ]; then
2669                 echo "_btrfs_stress_replace requires at least 3 devices in SCRATCH_DEV_POOL"
2670                 return
2671         fi
2672
2673         # take the last device as the first free_dev
2674         local free_dev="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $NF}'`"
2675
2676         # free_dev should be really free
2677         if $BTRFS_UTIL_PROG filesystem show $btrfs_mnt | grep -q "$free_dev"; then
2678                 echo "_btrfs_stress_replace: $free_dev is used by btrfs"
2679                 return
2680         fi
2681
2682         # dev_pool is device list being currently used by btrfs (excluding SCRATCH_DEV)
2683         # and can be replaced. We don't replace SCRATCH_DEV because it will be used in
2684         # _scratch_mount and _check_scratch_fs etc.
2685         local dev_pool=`echo $SCRATCH_DEV_POOL | sed -e "s# *$SCRATCH_DEV *##" \
2686                         -e "s# *$free_dev *##"`
2687
2688         # set the first device in dev_pool as the first src_dev to be replaced
2689         local src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
2690
2691         echo "dev_pool=$dev_pool"
2692         echo "free_dev=$free_dev, src_dev=$src_dev"
2693         while true; do
2694                 echo "Replacing $src_dev with $free_dev"
2695                 $BTRFS_UTIL_PROG replace start -fB $src_dev $free_dev $btrfs_mnt
2696                 if [ $? -ne 0 ]; then
2697                         # don't update src_dev and free_dev if replace failed
2698                         continue
2699                 fi
2700                 dev_pool="$dev_pool $free_dev"
2701                 dev_pool=`echo $dev_pool | sed -e "s# *$src_dev *##"`
2702                 free_dev=$src_dev
2703                 src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
2704         done
2705 }
2706
2707 # return device size in kb
2708 _get_device_size()
2709 {
2710         grep `_short_dev $1` /proc/partitions | awk '{print $3}'
2711 }
2712
2713 init_rc()
2714 {
2715         if [ "$iam" == new ]
2716         then
2717                 return
2718         fi
2719         # make some further configuration checks here
2720         if [ "$TEST_DEV" = ""  ]
2721         then
2722                 echo "common/rc: Error: \$TEST_DEV is not set"
2723                 exit 1
2724         fi
2725
2726         # if $TEST_DEV is not mounted, mount it now as XFS
2727         if [ -z "`_fs_type $TEST_DEV`" ]
2728         then
2729                 # $TEST_DEV is not mounted
2730                 if ! _test_mount
2731                 then
2732                         echo "common/rc: retrying test device mount with external set"
2733                         [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
2734                         if ! _test_mount
2735                         then
2736                                 echo "common/rc: could not mount $TEST_DEV on $TEST_DIR"
2737                                 exit 1
2738                         fi
2739                 fi
2740         fi
2741
2742         if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
2743         then
2744                 echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
2745                 $DF_PROG $TEST_DEV
2746                 exit 1
2747         fi
2748         # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
2749         xfs_io -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
2750         export XFS_IO_PROG="$XFS_IO_PROG -F"
2751 }
2752
2753 # get real device path name by following link
2754 _real_dev()
2755 {
2756         local _dev=$1
2757         if [ -b "$_dev" ] && [ -L "$_dev" ]; then
2758                 _dev=`readlink -f "$_dev"`
2759         fi
2760         echo $_dev
2761 }
2762
2763 # basename of a device
2764 _short_dev()
2765 {
2766         echo `basename $(_real_dev $1)`
2767 }
2768
2769 get_block_size()
2770 {
2771         if [ -z $1 ] || [ ! -d $1 ]; then
2772                 echo "Missing mount point argument for get_block_size"
2773                 exit 1
2774         fi
2775         echo `stat -f -c %S $1`
2776 }
2777
2778 init_rc
2779
2780 ################################################################################
2781 # make sure this script returns success
2782 /bin/true