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