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