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