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