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