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