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