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