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