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