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