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