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