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