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