blob: e53d1569eef732d8db9f0b45e0a3c636e1a253d7 [file] [log] [blame]
##/bin/bash
# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
. common/config
BC=$(which bc 2> /dev/null) || BC=
_require_math()
{
if [ -z "$BC" ]; then
_notrun "this test requires 'bc' tool for doing math operations"
fi
}
_math()
{
[ $# -le 0 ] && return
LANG=C echo "scale=0; $@" | "$BC" -q 2> /dev/null
}
dd()
{
command dd --help 2>&1 | grep noxfer >/dev/null
if [ "$?" -eq 0 ]
then
command dd status=noxfer $@
else
command dd $@
fi
}
# Prints the md5 checksum of a given file
_md5_checksum()
{
md5sum $1 | cut -d ' ' -f1
}
# Write a byte into a range of a file
_pwrite_byte() {
local pattern="$1"
local offset="$2"
local len="$3"
local file="$4"
local xfs_io_args="$5"
$XFS_IO_PROG $xfs_io_args -f -c "pwrite -S $pattern $offset $len" "$file"
}
# mmap-write a byte into a range of a file
_mwrite_byte() {
local pattern="$1"
local offset="$2"
local len="$3"
local mmap_len="$4"
local file="$5"
$XFS_IO_PROG -f -c "mmap -rw 0 $mmap_len" -c "mwrite -S $pattern $offset $len" "$file"
}
# ls -l w/ selinux sometimes puts a dot at the end:
# -rwxrw-r--. id1 id2 file1
# Also filter out lost+found directory on extN file system if present
_ls_l()
{
ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /" | grep -v 'lost+found'
}
_dump_err()
{
_err_msg="$*"
echo "$_err_msg"
}
_dump_err_cont()
{
_err_msg="$*"
echo -n "$_err_msg"
}
_dump_err2()
{
_err_msg="$*"
>&2 echo "$_err_msg"
}
_log_err()
{
_err_msg="$*"
echo "$_err_msg" | tee -a $seqres.full
echo "(see $seqres.full for details)"
}
# make sure we have a standard umask
umask 022
# check for correct setup and source the $FSTYP specific functions now
case "$FSTYP" in
xfs)
[ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
[ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
[ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
[ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
[ "$XFS_INFO_PROG" = "" ] && _fatal "xfs_info not found"
. ./common/xfs
;;
udf)
[ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
;;
btrfs)
[ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
. ./common/btrfs
;;
ext4)
[ "$MKFS_EXT4_PROG" = "" ] && _fatal "mkfs.ext4 not found"
;;
f2fs)
[ "$MKFS_F2FS_PROG" = "" ] && _fatal "mkfs.f2fs not found"
;;
nfs)
. ./common/nfs
;;
cifs)
;;
9p)
;;
ceph)
. ./common/ceph
;;
glusterfs)
;;
overlay)
. ./common/overlay
;;
reiser4)
[ "$MKFS_REISER4_PROG" = "" ] && _fatal "mkfs.reiser4 not found"
;;
pvfs2)
;;
ubifs)
[ "$UBIUPDATEVOL_PROG" = "" ] && _fatal "ubiupdatevol not found"
;;
esac
if [ ! -z "$REPORT_LIST" ]; then
. ./common/report
_assert_report_list
fi
_get_filesize()
{
stat -c %s "$1"
}
# Get hugepagesize in bytes
_get_hugepagesize()
{
local hugepgsz=$(awk '/Hugepagesize/ {print $2}' /proc/meminfo)
# Call _notrun if $hugepgsz is not a number
echo "$hugepgsz" | egrep -q ^[0-9]+$ || \
_notrun "Cannot get the value of Hugepagesize"
echo $((hugepgsz * 1024))
}
_mount()
{
$MOUNT_PROG `_mount_ops_filter $*`
}
# Call _mount to do mount operation but also save mountpoint to
# MOUNTED_POINT_STACK. Note that the mount point must be the last parameter
_get_mount()
{
local mnt_point=${!#}
local mnt_dev=${@:(-2):1}
local scratch_opts=""
if [ "$mnt_dev" = "$SCRATCH_DEV" ]; then
_scratch_options mount
scratch_opts="$SCRATCH_OPTIONS"
fi
_mount $scratch_opts $*
if [ $? -eq 0 ]; then
# mount --move operation updates the mountpoint, so remove
# the old one and insert the new one
if [[ "$*" =~ --move|-M ]]; then
MOUNTED_POINT_STACK=`echo $MOUNTED_POINT_STACK | \
cut -d\ -f2-`
fi
MOUNTED_POINT_STACK="$mnt_point $MOUNTED_POINT_STACK"
else
return 1
fi
}
# Unmount the last mounted mountpoint in MOUNTED_POINT_STACK
# and return it to caller
_put_mount()
{
local last_mnt=`echo $MOUNTED_POINT_STACK | awk '{print $1}'`
if [ -n "$last_mnt" ]; then
$UMOUNT_PROG $last_mnt
fi
MOUNTED_POINT_STACK=`echo $MOUNTED_POINT_STACK | cut -d\ -f2-`
}
# Unmount all mountpoints in MOUNTED_POINT_STACK and clear the stack
_clear_mount_stack()
{
if [ -n "$MOUNTED_POINT_STACK" ]; then
$UMOUNT_PROG $MOUNTED_POINT_STACK
fi
MOUNTED_POINT_STACK=""
}
_scratch_options()
{
local type=$1
local rt_opt=""
local log_opt=""
SCRATCH_OPTIONS=""
if [ "$FSTYP" != "xfs" ]; then
return
fi
case $type in
mkfs)
SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
rt_opt="-r"
log_opt="-l"
;;
mount)
rt_opt="-o"
log_opt="-o"
;;
esac
[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
}
_test_options()
{
local type=$1
local rt_opt=""
local log_opt=""
TEST_OPTIONS=""
if [ "$FSTYP" != "xfs" ]; then
return
fi
case $type in
mkfs)
rt_opt="-r"
log_opt="-l"
;;
mount)
rt_opt="-o"
log_opt="-o"
;;
esac
[ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
[ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
}
_mount_ops_filter()
{
local params="$*"
local last_index=$(( $# - 1 ))
[ $last_index -gt 0 ] && shift $last_index
local fs_escaped=$1
echo $params | \
$PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$fs_escaped\1\2#; print;"
}
# Used for mounting non-scratch devices (e.g. loop, dm constructs)
# with the safe set of scratch mount options (e.g. loop image may be
# hosted on $SCRATCH_DEV, so can't use external scratch devices).
_common_dev_mount_options()
{
echo $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $*
}
_scratch_mount_options()
{
_scratch_options mount
echo `_common_dev_mount_options $*` $SCRATCH_OPTIONS \
$SCRATCH_DEV $SCRATCH_MNT
}
_supports_filetype()
{
local dir=$1
local fstyp=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $2}'`
case "$fstyp" in
xfs)
$XFS_INFO_PROG $dir | grep -q "ftype=1"
;;
ext2|ext3|ext4)
local dev=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $1}'`
tune2fs -l $dev | grep -q filetype
;;
*)
local testfile=$dir/$$.ftype
touch $testfile
# look for DT_UNKNOWN files
local unknowns=$($here/src/t_dir_type $dir u | wc -l)
rm $testfile
# 0 unknowns is success
return $unknowns
;;
esac
}
# mount scratch device with given options but don't check mount status
_try_scratch_mount()
{
if [ "$FSTYP" == "overlay" ]; then
_overlay_scratch_mount $*
return $?
fi
_mount -t $FSTYP `_scratch_mount_options $*`
}
# mount scratch device with given options and _fail if mount fails
_scratch_mount()
{
_try_scratch_mount $* || _fail "mount failed"
}
_scratch_mount_idmapped()
{
local type="$1"
local id="$2"
if [ "$type" = "u" ]; then
# This means root will be able to create files as uid %id in
# the underlying filesystem by going through the idmapped mount.
$here/src/idmapped-mounts/mount-idmapped --map-mount u:0:$id:1 \
--map-mount u:$id:0:1 \
--map-mount g:0:0:1 \
"$SCRATCH_MNT" "$SCRATCH_MNT" || _fail "mount-idmapped failed"
elif [ "$type" = "g" ]; then
# This means root will be able to create files as gid %id in
# the underlying filesystem by going through the idmapped mount.
$here/src/idmapped-mounts/mount-idmapped --map-mount g:0:$id:1 \
--map-mount g:$id:0:1 \
--map-mount u:0:0:1 \
"$SCRATCH_MNT" "$SCRATCH_MNT" || _fail "mount-idmapped failed"
elif [ "$type" = "b" ]; then
# This means root will be able to create files as uid and gid
# %id in the underlying filesystem by going through the idmapped mount.
$here/src/idmapped-mounts/mount-idmapped --map-mount b:0:$id:1 \
--map-mount b:$id:0:1 \
"$SCRATCH_MNT" "$SCRATCH_MNT" || _fail "mount-idmapped failed"
else
_fail "usage: either \"u\" (uid), \"g\" (gid), or \"b\" (uid and gid) must be specified "
fi
}
_scratch_unmount()
{
case "$FSTYP" in
overlay)
_overlay_scratch_unmount
;;
btrfs)
$UMOUNT_PROG $SCRATCH_MNT
;;
*)
$UMOUNT_PROG $SCRATCH_DEV
;;
esac
}
_scratch_umount_idmapped()
{
$UMOUNT_PROG $SCRATCH_MNT
}
_scratch_remount()
{
local opts="$1"
if test -n "$opts"; then
_try_scratch_mount "-o remount,$opts"
fi
}
_scratch_cycle_mount()
{
local opts="$1"
if [ "$FSTYP" = tmpfs ]; then
_scratch_remount "$opts"
return
fi
if test -n "$opts"; then
opts="-o $opts"
fi
_scratch_unmount
_try_scratch_mount "$opts" || _fail "cycle mount failed"
}
_scratch_shutdown()
{
if [ $FSTYP = "overlay" ]; then
# In lagacy overlay usage, it may specify directory as
# SCRATCH_DEV, in this case OVL_BASE_SCRATCH_DEV
# will be null, so check OVL_BASE_SCRATCH_DEV before
# running shutdown to avoid shutting down base fs accidently.
if [ -z $OVL_BASE_SCRATCH_DEV ]; then
_fail "_scratch_shutdown: call _require_scratch_shutdown first in test"
else
$here/src/godown $* $OVL_BASE_SCRATCH_MNT
fi
else
$here/src/godown $* $SCRATCH_MNT
fi
}
# Return a file path that can be used to shut down the scratch filesystem.
# Caller should _require_scratch_shutdown before using this.
_scratch_shutdown_handle()
{
if [ $FSTYP = "overlay" ]; then
echo $OVL_BASE_SCRATCH_MNT
else
echo $SCRATCH_MNT
fi
}
_test_mount()
{
if [ "$FSTYP" == "overlay" ]; then
_overlay_test_mount $*
return $?
fi
_test_options mount
_mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
}
_test_unmount()
{
if [ "$FSTYP" == "overlay" ]; then
_overlay_test_unmount
else
$UMOUNT_PROG $TEST_DEV
fi
}
_test_cycle_mount()
{
if [ "$FSTYP" = tmpfs ]; then
return
fi
_test_unmount
_test_mount
}
_scratch_mkfs_options()
{
_scratch_options mkfs
echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
}
# Do the actual mkfs work on SCRATCH_DEV. Firstly mkfs with both MKFS_OPTIONS
# and user specified mkfs options, if that fails (due to conflicts between mkfs
# options), do a second mkfs with only user provided mkfs options.
#
# First param is the mkfs command without any mkfs options and device.
# Second param is the filter to remove unnecessary messages from mkfs stderr.
# Other extra mkfs options are followed.
_scratch_do_mkfs()
{
local mkfs_cmd=$1
local mkfs_filter=$2
shift 2
local extra_mkfs_options=$*
local mkfs_status
local tmp=`mktemp -u`
# save mkfs output in case conflict means we need to run again.
# only the output for the mkfs that applies should be shown
eval "$mkfs_cmd $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV" \
2>$tmp.mkfserr 1>$tmp.mkfsstd
mkfs_status=$?
# a mkfs failure may be caused by conflicts between $MKFS_OPTIONS and
# $extra_mkfs_options
if [ $mkfs_status -ne 0 -a -n "$extra_mkfs_options" ]; then
(
echo -n "** mkfs failed with extra mkfs options "
echo "added to \"$MKFS_OPTIONS\" by test $seq **"
echo -n "** attempting to mkfs using only test $seq "
echo "options: $extra_mkfs_options **"
) >> $seqres.full
# running mkfs again. overwrite previous mkfs output files
eval "$mkfs_cmd $extra_mkfs_options $SCRATCH_DEV" \
2>$tmp.mkfserr 1>$tmp.mkfsstd
mkfs_status=$?
fi
# output stored mkfs output, filtering unnecessary output from stderr
cat $tmp.mkfsstd
eval "cat $tmp.mkfserr | $mkfs_filter" >&2
rm -f $tmp.mkfserr $tmp.mkfsstd
return $mkfs_status
}
_setup_large_ext4_fs()
{
local fs_size=$1
local tmp_dir=/tmp/
[ "$LARGE_SCRATCH_DEV" != yes ] && return 0
[ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
[ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
# Default free space in the FS is 50GB, but you can specify more via
# SCRATCH_DEV_EMPTY_SPACE
local space_to_consume=$(($fs_size - 50*1024*1024*1024 - $SCRATCH_DEV_EMPTY_SPACE))
# mount the filesystem and create 16TB - 4KB files until we consume
# all the necessary space.
_try_scratch_mount 2>&1 >$tmp_dir/mnt.err
local status=$?
if [ $status -ne 0 ]; then
echo "mount failed"
cat $tmp_dir/mnt.err >&2
rm -f $tmp_dir/mnt.err
return $status
fi
rm -f $tmp_dir/mnt.err
local file_size=$((16*1024*1024*1024*1024 - 4096))
local nfiles=0
while [ $space_to_consume -gt $file_size ]; do
xfs_io -F -f \
-c "truncate $file_size" \
-c "falloc -k 0 $file_size" \
$SCRATCH_MNT/.use_space.$nfiles 2>&1
status=$?
if [ $status -ne 0 ]; then
break;
fi
space_to_consume=$(( $space_to_consume - $file_size ))
nfiles=$(($nfiles + 1))
done
# consume the remaining space.
if [ $space_to_consume -gt 0 ]; then
xfs_io -F -f \
-c "truncate $space_to_consume" \
-c "falloc -k 0 $space_to_consume" \
$SCRATCH_MNT/.use_space.$nfiles 2>&1
status=$?
fi
export NUM_SPACE_FILES=$nfiles
_scratch_unmount
if [ $status -ne 0 ]; then
echo "large file prealloc failed"
cat $tmp_dir/mnt.err >&2
return $status
fi
return 0
}
_scratch_mkfs_ext4()
{
local mkfs_cmd="$MKFS_EXT4_PROG -F"
local mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \" | grep -v \"^$\""
local tmp=`mktemp -u`
local mkfs_status
[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
$mkfs_cmd -O journal_dev $MKFS_OPTIONS $SCRATCH_LOGDEV && \
mkfs_cmd="$mkfs_cmd -J device=$SCRATCH_LOGDEV"
_scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
mkfs_status=$?
if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
# manually parse the mkfs output to get the fs size in bytes
local fs_size=`cat $tmp.mkfsstd | awk ' \
/^Block size/ { split($2, a, "="); bs = a[2] ; } \
/ inodes, / { blks = $3 } \
/reserved for the super user/ { resv = $1 } \
END { fssize = bs * blks - resv; print fssize }'`
_setup_large_ext4_fs $fs_size
mkfs_status=$?
fi
# output mkfs stdout and stderr
cat $tmp.mkfsstd
cat $tmp.mkfserr >&2
rm -f $tmp.mkfserr $tmp.mkfsstd
return $mkfs_status
}
_ext4_metadump()
{
local device="$1"
local dumpfile="$2"
local compressopt="$3"
test -n "$E2IMAGE_PROG" || _fail "e2image not installed"
$E2IMAGE_PROG -Q "$device" "$dumpfile"
[ "$compressopt" = "compress" ] && [ -n "$DUMP_COMPRESSOR" ] &&
$DUMP_COMPRESSOR "$dumpfile" &>> "$seqres.full"
}
_test_mkfs()
{
case $FSTYP in
nfs*)
# do nothing for nfs
;;
cifs)
# do nothing for cifs
;;
9p)
# do nothing for 9p
;;
virtiofs)
# do nothing for virtiofs
;;
ceph)
# do nothing for ceph
;;
glusterfs)
# do nothing for glusterfs
;;
overlay)
# do nothing for overlay
;;
pvfs2)
# do nothing for pvfs2
;;
udf)
$MKFS_UDF_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
;;
btrfs)
$MKFS_BTRFS_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
;;
ext2|ext3|ext4)
$MKFS_PROG -t $FSTYP -- -F $MKFS_OPTIONS $* $TEST_DEV
;;
*)
yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $TEST_DEV
;;
esac
}
_mkfs_dev()
{
local tmp=`mktemp -u`
case $FSTYP in
nfs*)
# do nothing for nfs
;;
9p)
# do nothing for 9p
;;
virtiofs)
# do nothing for virtiofs
;;
overlay)
# do nothing for overlay
;;
pvfs2)
# do nothing for pvfs2
;;
udf)
$MKFS_UDF_PROG $MKFS_OPTIONS $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
;;
btrfs)
$MKFS_BTRFS_PROG $MKFS_OPTIONS $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
;;
ext2|ext3|ext4)
$MKFS_PROG -t $FSTYP -- -F $MKFS_OPTIONS $* \
2>$tmp.mkfserr 1>$tmp.mkfsstd
;;
xfs)
$MKFS_PROG -t $FSTYP -- -f $MKFS_OPTIONS $* \
2>$tmp.mkfserr 1>$tmp.mkfsstd
;;
*)
yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* \
2>$tmp.mkfserr 1>$tmp.mkfsstd
;;
esac
if [ $? -ne 0 ]; then
# output stored mkfs output
cat $tmp.mkfserr >&2
cat $tmp.mkfsstd
status=1
exit 1
fi
rm -f $tmp.mkfserr $tmp.mkfsstd
}
# remove all files in $SCRATCH_MNT, useful when testing on NFS/CIFS
_scratch_cleanup_files()
{
case $FSTYP in
overlay)
# Avoid rm -rf /* if we messed up
[ -n "$OVL_BASE_SCRATCH_MNT" ] || return 1
_overlay_base_scratch_mount || return 1
rm -rf $OVL_BASE_SCRATCH_MNT/* || return 1
_overlay_mkdirs $OVL_BASE_SCRATCH_MNT
# leave base fs mouted so tests can setup lower/upper dir files
;;
*)
[ -n "$SCRATCH_MNT" ] || return 1
_scratch_mount
rm -rf $SCRATCH_MNT/*
_scratch_unmount
;;
esac
}
_scratch_mkfs()
{
local mkfs_cmd=""
local mkfs_filter=""
local mkfs_status
case $FSTYP in
nfs*|cifs|ceph|overlay|glusterfs|pvfs2|9p|virtiofs)
# unable to re-create this fstyp, just remove all files in
# $SCRATCH_MNT to avoid EEXIST caused by the leftover files
# created in previous runs
_scratch_cleanup_files
return $?
;;
tmpfs)
# do nothing for tmpfs
return 0
;;
ubifs)
# erase the UBI volume; reformated automatically on next mount
$UBIUPDATEVOL_PROG ${SCRATCH_DEV} -t
return 0
;;
ext4)
_scratch_mkfs_ext4 $*
return $?
;;
xfs)
_scratch_mkfs_xfs $*
return $?
;;
udf)
mkfs_cmd="$MKFS_UDF_PROG"
mkfs_filter="cat"
;;
btrfs)
mkfs_cmd="$MKFS_BTRFS_PROG"
mkfs_filter="cat"
;;
ext3)
mkfs_cmd="$MKFS_PROG -t $FSTYP -- -F"
mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \""
# put journal on separate device?
[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
$mkfs_cmd -O journal_dev $MKFS_OPTIONS $SCRATCH_LOGDEV && \
mkfs_cmd="$mkfs_cmd -J device=$SCRATCH_LOGDEV"
;;
ext2)
mkfs_cmd="$MKFS_PROG -t $FSTYP -- -F"
mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \""
;;
f2fs)
mkfs_cmd="$MKFS_F2FS_PROG"
mkfs_filter="cat"
;;
ocfs2)
mkfs_cmd="yes | $MKFS_PROG -t $FSTYP --"
mkfs_filter="grep -v -e ^mkfs\.ocfs2"
;;
*)
mkfs_cmd="yes | $MKFS_PROG -t $FSTYP --"
mkfs_filter="cat"
;;
esac
_scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $*
return $?
}
# Helper function to get a spare or replace-target device from
# configured SCRATCH_DEV_POLL, must call _scratch_dev_pool_get()
# before _spare_dev_get(). Replace-target-device/Spare-device will
# be assigned to SPARE_DEV.
# As of now only one replace-target-device/spare-device can be
# assigned.
#
# Usage:
# _scratch_dev_pool_get() <ndevs>
# _spare_dev_get()
# :: do stuff
# _spare_dev_put()
# _scratch_dev_pool_put()
#
_spare_dev_get()
{
typeset -p SCRATCH_DEV_POOL_SAVED >/dev/null 2>&1
if [ $? -ne 0 ]; then
_fail "Bug: unset val, must call _scratch_dev_pool_get before _spare_dev_get"
fi
if [ -z "$SCRATCH_DEV_POOL_SAVED" ]; then
_fail "Bug: str empty, must call _scratch_dev_pool_get before _spare_dev_get"
fi
# Check if the spare is already assigned
typeset -p SPARE_DEV >/dev/null 2>&1
if [ $? -eq 0 ]; then
if [ ! -z "$SPARE_DEV" ]; then
_fail "Bug: SPARE_DEV = $SPARE_DEV already assigned"
fi
fi
local ndevs=`echo $SCRATCH_DEV_POOL| wc -w`
local config_ndevs=`echo $SCRATCH_DEV_POOL_SAVED| wc -w`
if [ $ndevs -eq $config_ndevs ]; then
_notrun "All devs used no spare"
fi
# Get a dev that is not used
local -a devs="( $SCRATCH_DEV_POOL_SAVED )"
SPARE_DEV=${devs[@]:$ndevs:1}
export SPARE_DEV
}
_spare_dev_put()
{
typeset -p SPARE_DEV >/dev/null 2>&1
if [ $? -ne 0 ]; then
_fail "Bug: unset val, must call _spare_dev_get before its put"
fi
if [ -z "$SPARE_DEV" ]; then
_fail "Bug: str empty, must call _spare_dev_get before its put"
fi
export SPARE_DEV=""
}
#
# Generally test cases will have..
# _require_scratch_dev_pool X
# to make sure it has the enough scratch devices including
# replace-target and spare device. Now arg1 here is the
# required number of scratch devices by a-test-case excluding
# the replace-target and spare device. So this function will
# set SCRATCH_DEV_POOL to the specified number of devices.
#
# Usage:
# _scratch_dev_pool_get() <ndevs>
# :: do stuff
#
# _scratch_dev_pool_put()
#
_scratch_dev_pool_get()
{
if [ $# -ne 1 ]; then
_fail "Usage: _scratch_dev_pool_get ndevs"
fi
local test_ndevs=$1
local config_ndevs=`echo $SCRATCH_DEV_POOL| wc -w`
local -a devs="( $SCRATCH_DEV_POOL )"
typeset -p config_ndevs >/dev/null 2>&1
if [ $? -ne 0 ]; then
_fail "Bug: cant find SCRATCH_DEV_POOL ndevs"
fi
if [ $config_ndevs -lt $test_ndevs ]; then
_notrun "Need at least test requested number of ndevs $test_ndevs"
fi
SCRATCH_DEV_POOL_SAVED=${SCRATCH_DEV_POOL}
export SCRATCH_DEV_POOL_SAVED
SCRATCH_DEV_POOL=${devs[@]:0:$test_ndevs}
export SCRATCH_DEV_POOL
}
_scratch_dev_pool_put()
{
typeset -p SCRATCH_DEV_POOL_SAVED >/dev/null 2>&1
if [ $? -ne 0 ]; then
_fail "Bug: unset val, must call _scratch_dev_pool_get before _scratch_dev_pool_put"
fi
if [ -z "$SCRATCH_DEV_POOL_SAVED" ]; then
_fail "Bug: str empty, must call _scratch_dev_pool_get before _scratch_dev_pool_put"
fi
export SCRATCH_DEV_POOL=$SCRATCH_DEV_POOL_SAVED
export SCRATCH_DEV_POOL_SAVED=""
}
_scratch_pool_mkfs()
{
case $FSTYP in
btrfs)
$MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV_POOL > /dev/null
;;
*)
echo "_scratch_pool_mkfs is not implemented for $FSTYP" 1>&2
;;
esac
}
# Return the amount of free memory available on the system
_free_memory_bytes()
{
free -b | grep ^Mem | awk '{print $4}'
}
_available_memory_bytes()
{
nf=`free -b | grep ^Mem | awk '{print NF}'`
if [[ nf -lt 7 ]]; then
# Doesn't have available field. Fallback.
_free_memory_bytes
else
free -b | grep ^Mem | awk '{print $7}'
fi
}
# Create fs of certain size on scratch device
# _scratch_mkfs_sized <size in bytes> [optional blocksize]
_scratch_mkfs_sized()
{
local fssize=$1
local blocksize=$2
local def_blksz
case $FSTYP in
xfs)
def_blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*-b ?size= ?+([0-9]+).*/\1/p'`
;;
ext2|ext3|ext4|ext4dev|udf|btrfs|reiser4|ocfs2|reiserfs)
def_blksz=`echo $MKFS_OPTIONS | sed -rn 's/.*-b ?+([0-9]+).*/\1/p'`
;;
jfs)
def_blksz=4096
;;
esac
[ -n "$def_blksz" ] && blocksize=$def_blksz
[ -z "$blocksize" ] && blocksize=4096
local re='^[0-9]+$'
if ! [[ $fssize =~ $re ]] ; then
_notrun "error: _scratch_mkfs_sized: fs size \"$fssize\" not an integer."
fi
if ! [[ $blocksize =~ $re ]] ; then
_notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
fi
local blocks=`expr $fssize / $blocksize`
if [ -b "$SCRATCH_DEV" ]; then
local devsize=`blockdev --getsize64 $SCRATCH_DEV`
[ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
fi
if [ "$FSTYP" = "xfs" ] && [ -b "$SCRATCH_RTDEV" ]; then
local rtdevsize=`blockdev --getsize64 $SCRATCH_RTDEV`
[ "$fssize" -gt "$rtdevsize" ] && _notrun "Scratch rt device too small"
rt_ops="-r size=$fssize"
fi
case $FSTYP in
xfs)
# don't override MKFS_OPTIONS that set a block size.
echo $MKFS_OPTIONS |egrep -q "b?size="
if [ $? -eq 0 ]; then
_scratch_mkfs_xfs -d size=$fssize $rt_ops
else
_scratch_mkfs_xfs -d size=$fssize $rt_ops -b size=$blocksize
fi
;;
ext2|ext3|ext4|ext4dev)
${MKFS_PROG} -t $FSTYP -F $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
;;
gfs2)
# mkfs.gfs2 doesn't automatically shrink journal files on small
# filesystems, so the journal files may end up being bigger than the
# filesystem, which will cause mkfs.gfs2 to fail. Until that's fixed,
# shrink the journal size to at most one eigth of the filesystem and at
# least 8 MiB, the minimum size allowed.
local min_journal_size=8
local default_journal_size=128
if (( fssize/8 / (1024*1024) < default_journal_size )); then
local journal_size=$(( fssize/8 / (1024*1024) ))
(( journal_size >= min_journal_size )) || journal_size=$min_journal_size
MKFS_OPTIONS="-J $journal_size $MKFS_OPTIONS"
fi
${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS -O -b $blocksize $SCRATCH_DEV $blocks
;;
ocfs2)
yes | ${MKFS_PROG} -t $FSTYP -F $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
;;
udf)
$MKFS_UDF_PROG $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
;;
btrfs)
local mixed_opt=
# minimum size that's needed without the mixed option.
# Ref: btrfs-prog: btrfs_min_dev_size()
# Non mixed mode is also the default option.
(( fssize < $((256 * 1024 *1024)) )) && mixed_opt='--mixed'
$MKFS_BTRFS_PROG $MKFS_OPTIONS $mixed_opt -b $fssize $SCRATCH_DEV
;;
jfs)
${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS $SCRATCH_DEV $blocks
;;
reiserfs)
${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
;;
reiser4)
# mkfs.resier4 requires size in KB as input for creating filesystem
$MKFS_REISER4_PROG $MKFS_OPTIONS -y -b $blocksize $SCRATCH_DEV \
`expr $fssize / 1024`
;;
f2fs)
# mkfs.f2fs requires # of sectors as an input for the size
local sector_size=`blockdev --getss $SCRATCH_DEV`
$MKFS_F2FS_PROG $MKFS_OPTIONS $SCRATCH_DEV `expr $fssize / $sector_size`
;;
tmpfs)
local free_mem=`_free_memory_bytes`
if [ "$free_mem" -lt "$fssize" ] ; then
_notrun "Not enough memory ($free_mem) for tmpfs with $fssize bytes"
fi
export MOUNT_OPTIONS="-o size=$fssize $TMPFS_MOUNT_OPTIONS"
;;
*)
_notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
;;
esac
}
# Emulate an N-data-disk stripe w/ various stripe units
# _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
_scratch_mkfs_geom()
{
local sunit_bytes=$1
local swidth_mult=$2
local blocksize=$3
[ -z "$blocksize" ] && blocksize=4096
local sunit_blocks=$(( sunit_bytes / blocksize ))
local swidth_blocks=$(( sunit_blocks * swidth_mult ))
case $FSTYP in
xfs)
if echo "$MKFS_OPTIONS" | egrep -q "b?size="; then
MKFS_OPTIONS=$(echo "$MKFS_OPTIONS" | sed -r "s/(b?size=)[0-9]+k?/\1$blocksize/")
else
MKFS_OPTIONS+=" -b size=$blocksize"
fi
if echo "$MKFS_OPTIONS" | egrep -q "(su|sunit|sw|swidth)="; then
MKFS_OPTIONS=$(echo "$MKFS_OPTIONS" | sed -r \
-e "s/(su|sunit)=[0-9kmg]+/su=$sunit_bytes/" \
-e "s/(sw|swidth)=[0-9kmg]+/sw=$swidth_mult/")
else
MKFS_OPTIONS+=" -d su=$sunit_bytes,sw=$swidth_mult"
fi
;;
ext4|ext4dev)
MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
;;
*)
_notrun "can't mkfs $FSTYP with geometry"
;;
esac
_scratch_mkfs
}
# Create fs of certain blocksize on scratch device
# _scratch_mkfs_blocksized blocksize
_scratch_mkfs_blocksized()
{
local blocksize=$1
local re='^[0-9]+$'
if ! [[ $blocksize =~ $re ]] ; then
_notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
fi
case $FSTYP in
xfs)
_scratch_mkfs_xfs $MKFS_OPTIONS -b size=$blocksize
;;
ext2|ext3|ext4)
${MKFS_PROG} -t $FSTYP -F $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV
;;
gfs2)
${MKFS_PROG} -t $FSTYP $MKFS_OPTIONS -O -b $blocksize $SCRATCH_DEV
;;
ocfs2)
yes | ${MKFS_PROG} -t $FSTYP -F $MKFS_OPTIONS -b $blocksize -C $blocksize $SCRATCH_DEV
;;
*)
_notrun "Filesystem $FSTYP not supported in _scratch_mkfs_blocksized"
;;
esac
}
_scratch_resvblks()
{
case $FSTYP in
xfs)
xfs_io -x -c "resblks $1" $SCRATCH_MNT
;;
*)
;;
esac
}
# Repair scratch filesystem. Returns 0 if the FS is good to go (either no
# errors found or errors were fixed) and nonzero otherwise; also spits out
# a complaint on stderr if fsck didn't tell us that the FS is good to go.
_repair_scratch_fs()
{
case $FSTYP in
xfs)
_scratch_xfs_repair "$@" 2>&1
local res=$?
if [ "$res" -ne 0 ]; then
echo "xfs_repair returns $res; replay log?"
_try_scratch_mount
res=$?
if [ "$res" -gt 0 ]; then
echo "mount returns $res; zap log?"
_scratch_xfs_repair -L 2>&1
echo "log zap returns $?"
else
umount "$SCRATCH_MNT"
fi
_scratch_xfs_repair "$@" 2>&1
res=$?
fi
if [ $res -ne 0 ]; then
_dump_err2 "xfs_repair failed, err=$res"
fi
return $res
;;
*)
local dev=$SCRATCH_DEV
local fstyp=$FSTYP
if [ $FSTYP = "overlay" -a -n "$OVL_BASE_SCRATCH_DEV" ]; then
_repair_overlay_scratch_fs
# Fall through to repair base fs
dev=$OVL_BASE_SCRATCH_DEV
fstyp=$OVL_BASE_FSTYP
$UMOUNT_PROG $OVL_BASE_SCRATCH_MNT
fi
# Let's hope fsck -y suffices...
fsck -t $fstyp -y $dev 2>&1
local res=$?
case $res in
$FSCK_OK|$FSCK_NONDESTRUCT|$FSCK_REBOOT)
res=0
;;
*)
_dump_err2 "fsck.$FSTYP failed, err=$res"
;;
esac
return $res
;;
esac
}
_get_pids_by_name()
{
if [ $# -ne 1 ]
then
echo "Usage: _get_pids_by_name process-name" 1>&2
exit 1
fi
# Algorithm ... all ps(1) variants have a time of the form MM:SS or
# HH:MM:SS before the psargs field, use this as the search anchor.
#
# Matches with $1 (process-name) occur if the first psarg is $1
# or ends in /$1 ... the matching uses sed's regular expressions,
# so passing a regex into $1 will work.
ps $PS_ALL_FLAGS \
| sed -n \
-e 's/$/ /' \
-e 's/[ ][ ]*/ /g' \
-e 's/^ //' \
-e 's/^[^ ]* //' \
-e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
-e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
}
#
# _df_device : get an IRIX style df line for a given device
#
# - returns "" if not mounted
# - returns fs type in field two (ala IRIX)
# - joins line together if split by fancy df formatting
# - strips header etc
#
_df_device()
{
if [ $# -ne 1 ]
then
echo "Usage: _df_device device" 1>&2
exit 1
fi
# Note that we use "==" here so awk doesn't try to interpret an NFS over
# IPv6 server as a regular expression.
$DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
($1==what) && (NF==1) {
v=$1
getline
print v, $0
exit
}
($1==what) {
print
exit
}
'
}
#
# _df_dir : get an IRIX style df line for device where a directory resides
#
# - returns fs type in field two (ala IRIX)
# - joins line together if split by fancy df formatting
# - strips header etc
#
_df_dir()
{
if [ $# -ne 1 ]
then
echo "Usage: _df_dir device" 1>&2
exit 1
fi
$DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
NR == 2 && NF==1 {
v=$1
getline
print v, $0;
exit 0
}
NR == 2 {
print;
exit 0
}
{}
'
# otherwise, nada
}
# return percentage used disk space for mounted device
_used()
{
if [ $# -ne 1 ]
then
echo "Usage: _used device" 1>&2
exit 1
fi
_df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
}
# return the FS type of a mounted device
#
_fs_type()
{
if [ $# -ne 1 ]
then
echo "Usage: _fs_type device" 1>&2
exit 1
fi
#
# The Linux kernel shows NFSv4 filesystems in df output as
# filesystem type nfs4, although we mounted it as nfs earlier.
# Fix the filesystem type up here so that the callers don't
# have to bother with this quirk.
#
_df_device $1 | $AWK_PROG '{ print $2 }' | \
sed -e 's/nfs4/nfs/' -e 's/fuse.glusterfs/glusterfs/'
}
# return the FS mount options of a mounted device
#
# should write a version which just parses the output of mount for IRIX
# compatibility, but since this isn't used at all, at the moment I'll leave
# this for now
#
_fs_options()
{
if [ $# -ne 1 ]
then
echo "Usage: _fs_options device" 1>&2
exit 1
fi
$AWK_PROG -v dev=$1 '
match($1,dev) { print $4 }
' </proc/mounts
}
# returns device number if a file is a block device
#
_is_block_dev()
{
if [ $# -ne 1 ]
then
echo "Usage: _is_block_dev dev" 1>&2
exit 1
fi
local dev=$1
if [ -L "$dev" ]; then
dev=`readlink -f "$dev"`
fi
if [ -b "$dev" ]; then
$here/src/lstat64 "$dev" | $AWK_PROG '/Device type:/ { print $9 }'
fi
}
# returns device number if a file is a character device
#
_is_char_dev()
{
if [ $# -ne 1 ]; then
echo "Usage: _is_char_dev dev" 1>&2
exit 1
fi
local dev=$1
if [ -L "$dev" ]; then
dev=`readlink -f "$dev"`
fi
if [ -c "$dev" ]; then
$here/src/lstat64 "$dev" | $AWK_PROG '/Device type:/ { print $9 }'
fi
}
# Do a command, log it to $seqres.full, optionally test return status
# and die if command fails. If called with one argument _do executes the
# command, logs it, and returns its exit status. With two arguments _do
# first prints the message passed in the first argument, and then "done"
# or "fail" depending on the return status of the command passed in the
# second argument. If the command fails and the variable _do_die_on_error
# is set to "always" or the two argument form is used and _do_die_on_error
# is set to "message_only" _do will print an error message to
# $seqres.out and exit.
_do()
{
if [ $# -eq 1 ]; then
local cmd=$1
elif [ $# -eq 2 ]; then
local note=$1
local cmd=$2
echo -n "$note... "
else
echo "Usage: _do [note] cmd" 1>&2
status=1; exit
fi
(eval "echo '---' \"$cmd\"") >>$seqres.full
(eval "$cmd") >$tmp._out 2>&1
local ret=$?
cat $tmp._out >>$seqres.full
rm -f $tmp._out
if [ $# -eq 2 ]; then
if [ $ret -eq 0 ]; then
echo "done"
else
echo "fail"
fi
fi
if [ $ret -ne 0 ] \
&& [ "$_do_die_on_error" = "always" \
-o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
then
[ $# -ne 2 ] && echo
eval "echo \"$cmd\" failed \(returned $ret\): see $seqres.full"
status=1; exit
fi
return $ret
}
# bail out, setting up .notrun file. Need to kill the filesystem check files
# here, otherwise they are set incorrectly for the next test.
#
_notrun()
{
echo "$*" > $seqres.notrun
echo "$seq not run: $*"
rm -f ${RESULT_DIR}/require_test*
rm -f ${RESULT_DIR}/require_scratch*
status=0
exit
}
# just plain bail out
#
_fail()
{
echo "$*" | tee -a $seqres.full
echo "(see $seqres.full for details)"
status=1
exit 1
}
# tests whether $FSTYP is one of the supported filesystems for a test
#
_supported_fs()
{
local f
for f
do
if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
then
return
fi
done
_notrun "not suitable for this filesystem type: $FSTYP"
}
# check if a FS on a device is mounted
# if so, verify that it is mounted on mount point
# if fstype is given as argument, verify that it is also
# mounted with correct fs type
#
_check_mounted_on()
{
local devname=$1
local dev=$2
local mntname=$3
local mnt=$4
local type=$5
# find $dev as the source, and print result in "$dev $mnt" format
local mount_rec=`findmnt -rncv -S $dev -o SOURCE,TARGET`
[ -n "$mount_rec" ] || return 1 # 1 = not mounted
# if it's mounted, make sure its on $mnt
if [ "$mount_rec" != "$dev $mnt" ]; then
echo "$devname=$dev is mounted but not on $mntname=$mnt - aborting"
echo "Already mounted result:"
echo $mount_rec
return 2 # 2 = mounted on wrong mnt
fi
if [ -n "$type" -a "`_fs_type $dev`" != "$type" ]; then
echo "$devname=$dev is mounted but not a type $type filesystem"
# raw $DF_PROG cannot handle NFS/CIFS/overlay correctly
_df_device $dev
return 3 # 3 = mounted as wrong type
fi
return 0 # 0 = mounted as expected
}
# this test needs a scratch partition - check we're ok & unmount it
# No post-test check of the device is required. e.g. the test intentionally
# finishes the test with the filesystem in a corrupt state
_require_scratch_nocheck()
{
case "$FSTYP" in
glusterfs)
echo $SCRATCH_DEV | egrep -q ":/?" > /dev/null 2>&1
if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$SCRATCH_DEV"
fi
if [ ! -d "$SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
9p|virtiofs)
if [ -z "$SCRATCH_DEV" ]; then
_notrun "this test requires a valid \$SCRATCH_DEV"
fi
if [ ! -d "$SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
nfs*|ceph)
echo $SCRATCH_DEV | grep -q ":/" > /dev/null 2>&1
if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$SCRATCH_DEV"
fi
if [ ! -d "$SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
pvfs2)
echo $SCRATCH_DEV | grep -q "://" > /dev/null 2>&1
if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$SCRATCH_DEV"
fi
if [ ! -d "$SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
cifs)
echo $SCRATCH_DEV | grep -q "//" > /dev/null 2>&1
if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$SCRATCH_DEV"
fi
if [ ! -d "$SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
overlay)
if [ -z "$OVL_BASE_SCRATCH_MNT" -o ! -d "$OVL_BASE_SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$OVL_BASE_SCRATCH_MNT as ovl base dir"
fi
# if $SCRATCH_MNT is derived from $OVL_BASE_SCRATCH_MNT then
# don't check $SCRATCH_MNT dir here because base fs may not be mounted
# and we will create the mount point anyway on _overlay_mount
if [ "$SCRATCH_MNT" != "$OVL_BASE_SCRATCH_MNT/$OVL_MNT" -a ! -d "$SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
tmpfs)
if [ -z "$SCRATCH_DEV" -o ! -d "$SCRATCH_MNT" ];
then
_notrun "this test requires a valid \$SCRATCH_MNT and unique \$SCRATCH_DEV"
fi
;;
ubifs)
# ubifs needs an UBI volume. This will be a char device, not a block device.
if [ ! -c "$SCRATCH_DEV" ]; then
_notrun "this test requires a valid UBI volume for \$SCRATCH_DEV"
fi
if [ ! -d "$SCRATCH_MNT" ]; then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
*)
if [ -z "$SCRATCH_DEV" -o "`_is_block_dev "$SCRATCH_DEV"`" = "" ]
then
_notrun "this test requires a valid \$SCRATCH_DEV"
fi
if [ "`_is_block_dev "$SCRATCH_DEV"`" = "`_is_block_dev "$TEST_DEV"`" ]
then
_notrun "this test requires a valid \$SCRATCH_DEV"
fi
if [ ! -d "$SCRATCH_MNT" ]
then
_notrun "this test requires a valid \$SCRATCH_MNT"
fi
;;
esac
_check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT
local err=$?
[ $err -le 1 ] || exit 1
if [ $err -eq 0 ]
then
# if it's mounted, unmount it
if ! _scratch_unmount
then
echo "failed to unmount $SCRATCH_DEV"
exit 1
fi
fi
rm -f ${RESULT_DIR}/require_scratch
}
# we need the scratch device and it should be checked post test.
_require_scratch()
{
_require_scratch_nocheck
touch ${RESULT_DIR}/require_scratch
}
# require a scratch dev of a minimum size (in kb)
_require_scratch_size()
{
[ $# -eq 1 ] || _fail "_require_scratch_size: expected size param"
_require_scratch
local devsize=`_get_device_size $SCRATCH_DEV`
[ $devsize -lt $1 ] && _notrun "scratch dev too small"
}
# require a scratch dev of a minimum size (in kb) and should not be checked
# post test
_require_scratch_size_nocheck()
{
[ $# -eq 1 ] || _fail "_require_scratch_size: expected size param"
_require_scratch_nocheck
local devsize=`_get_device_size $SCRATCH_DEV`
[ $devsize -lt $1 ] && _notrun "scratch dev too small"
}
# Require scratch fs which supports >16T of filesystem size.
# _require_scratch must be called before this function is called.
_require_scratch_16T_support()
{
case $FSTYP in
ext2|ext3|f2fs)
_notrun "$FSTYP doesn't support >16T filesystem"
;;
ext4)
_scratch_mkfs >> $seqres.full 2>&1
_scratch_mount
local blocksize=$(_get_block_size $SCRATCH_MNT)
if [ $blocksize -lt 4096 ]; then
_notrun "This test requires >16T fs support"
fi
_scratch_unmount
;;
*)
;;
esac
}
# this test needs a test partition - check we're ok & mount it
#
_require_test()
{
case "$FSTYP" in
glusterfs)
echo $TEST_DEV | egrep -q ":/?" > /dev/null 2>&1
if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$TEST_DEV"
fi
if [ ! -d "$TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
9p|virtiofs)
if [ -z "$TEST_DEV" ]; then
_notrun "this test requires a valid \$TEST_DEV"
fi
if [ ! -d "$TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
nfs*|ceph)
echo $TEST_DEV | grep -q ":/" > /dev/null 2>&1
if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$TEST_DEV"
fi
if [ ! -d "$TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
cifs)
echo $TEST_DEV | grep -q "//" > /dev/null 2>&1
if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$TEST_DEV"
fi
if [ ! -d "$TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
pvfs2)
echo $TEST_DEV | grep -q "://" > /dev/null 2>&1
if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
if [ ! -d "$TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
overlay)
if [ -z "$OVL_BASE_TEST_DIR" -o ! -d "$OVL_BASE_TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR as ovl base dir"
fi
if [ ! -d "$TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
tmpfs)
if [ -z "$TEST_DEV" -o ! -d "$TEST_DIR" ];
then
_notrun "this test requires a valid \$TEST_DIR and unique $TEST_DEV"
fi
;;
ubifs)
# ubifs needs an UBI volume. This will be a char device, not a block device.
if [ ! -c "$TEST_DEV" ]; then
_notrun "this test requires a valid UBI volume for \$TEST_DEV"
fi
if [ ! -d "$TEST_DIR" ]; then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
*)
if [ -z "$TEST_DEV" ] || [ "`_is_block_dev "$TEST_DEV"`" = "" ]
then
_notrun "this test requires a valid \$TEST_DEV"
fi
if [ "`_is_block_dev "$SCRATCH_DEV"`" = "`_is_block_dev "$TEST_DEV"`" ]
then
_notrun "this test requires a valid \$TEST_DEV"
fi
if [ ! -d "$TEST_DIR" ]
then
_notrun "this test requires a valid \$TEST_DIR"
fi
;;
esac
_check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR
local err=$?
[ $err -le 1 ] || exit 1
if [ $err -ne 0 ]
then
if ! _test_mount
then
echo "!!! failed to mount $TEST_DEV on $TEST_DIR"
exit 1
fi
fi
touch ${RESULT_DIR}/require_test
}
_has_logdev()
{
local ret=0
[ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && ret=1
[ "$USE_EXTERNAL" != yes ] && ret=1
return $ret
}
# this test needs a logdev
#
_require_logdev()
{
[ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
_notrun "This test requires a valid \$SCRATCH_LOGDEV"
[ "$USE_EXTERNAL" != yes ] && \
_notrun "This test requires USE_EXTERNAL to be enabled"
# ensure its not mounted
$UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
}
# this test requires loopback device support
#
_require_loop()
{
modprobe loop >/dev/null 2>&1
if grep loop /proc/devices >/dev/null 2>&1
then
:
else
_notrun "This test requires loopback device support"
fi
}
# this test requires kernel support for a secondary filesystem
#
_require_extra_fs()
{
modprobe "$1" >/dev/null 2>&1
grep -q -w "$1" /proc/filesystems ||
_notrun "this test requires $1 support"
}
# this test requires that (large) loopback device files are not in use
#
_require_no_large_scratch_dev()
{
[ "$LARGE_SCRATCH_DEV" = yes ] && \
_notrun "Large filesystem testing in progress, skipped this test"
}
# this test requires that a realtime subvolume is in use, and
# that the kernel supports realtime as well.
#
_require_realtime()
{
[ "$USE_EXTERNAL" = yes ] || \
_notrun "External volumes not in use, skipped this test"
[ "$SCRATCH_RTDEV" = "" ] && \
_notrun "Realtime device required, skipped this test"
}
# This test requires that a realtime subvolume is not in use
#
_require_no_realtime()
{
[ "$USE_EXTERNAL" = "yes" ] && [ -n "$SCRATCH_RTDEV" ] && \
_notrun "Test not compatible with realtime subvolumes, skipped this test"
}
# this test requires that a specified command (executable) exists
# $1 - command, $2 - name for error message
#
# Note: the command string might have parameters, so strip them before checking
# whether it is executable.
_require_command()
{
if [ $# -eq 2 ]; then
local name="$2"
elif [ $# -eq 1 ]; then
local name="$1"
else
_fail "usage: _require_command <command> [<name>]"
fi
local command=`echo "$1" | awk '{ print $1 }'`
if [ ! -x "$command" ]; then
_notrun "$name utility required, skipped this test"
fi
}
# this test requires the device to be valid block device
# $1 - device
_require_block_device()
{
if [ -z "$1" ]; then
echo "Usage: _require_block_device <dev>" 1>&2
exit 1
fi
if [ "`_is_block_dev "$1"`" == "" ]; then
_notrun "require $1 to be valid block disk"
fi
}
# this test requires a path to refere to a local block or character device
# $1 - device
_require_local_device()
{
if [ -z "$1" ]; then
echo "Usage: _require_local_device <dev>" 1>&2
exit 1
fi
if [ "`_is_block_dev "$1"`" != "" ]; then
return 0
fi
if [ "`_is_char_dev "$1"`" != "" ]; then
return 0
fi
_notrun "require $1 to be local device"
}
# brd based ram disks erase the device when they receive a flush command when no
# active references are present. This causes problems for DM devices sitting on
# top of brd devices as DM doesn't hold active references to the brd device.
_require_sane_bdev_flush()
{
echo $1 | grep -q "^/dev/ram[0-9]\+$"
if [ $? -eq 0 ]; then
_notrun "This test requires a sane block device flush"
fi
}
# this test requires a specific device mapper target
_require_dm_target()
{
local target=$1
# require SCRATCH_DEV to be a valid block device with sane BLKFLSBUF
# behaviour
_require_block_device $SCRATCH_DEV
_require_sane_bdev_flush $SCRATCH_DEV
_require_command "$DMSETUP_PROG" dmsetup
_normalize_mount_options | egrep -q "dax(=always| |$)" || \
test -e "/sys/block/$(_short_dev $SCRATCH_DEV)/dax"
if [ $? -eq 0 ]; then
case $target in
stripe|linear|log-writes)
;;
*)
_notrun "Cannot run tests with DAX on $target devices."
;;
esac
fi
modprobe dm-$target >/dev/null 2>&1
$DMSETUP_PROG targets 2>&1 | grep -q ^$target
if [ $? -ne 0 ]; then
_notrun "This test requires dm $target support"
fi
}
_zone_type()
{
local target=$1
if [ -z $target ]; then
echo "Usage: _zone_type <device>"
exit 1
fi
local sdev=`_short_dev $target`
if [ -e /sys/block/${sdev}/queue/zoned ]; then
cat /sys/block/${sdev}/queue/zoned
else
echo none
fi
}
_require_zoned_device()
{
local target=$1
if [ -z $target ]; then
echo "Usage: _require_zoned_device <device>"
exit 1
fi
local type=`_zone_type ${target}`
if [ "${type}" = "none" ]; then
_notrun "this test require zoned block device"
fi
}
_require_non_zoned_device()
{
local target=$1
if [ -z $target ]; then
echo "Usage: _require_non_zoned_device <device>"
exit 1
fi
local type=`_zone_type ${target}`
if [ "${type}" != "none" ]; then
_notrun "this test require non-zoned block device"
fi
}
# this test requires the ext4 kernel support crc feature on scratch device
#
_require_scratch_ext4_crc()
{
_scratch_mkfs_ext4 >/dev/null 2>&1
dumpe2fs -h $SCRATCH_DEV 2> /dev/null | grep -q metadata_csum || _notrun "metadata_csum not supported by this filesystem"
_try_scratch_mount >/dev/null 2>&1 \
|| _notrun "Kernel doesn't support metadata_csum feature"
_scratch_unmount
}
# Check whether the specified feature whether it is supported by
# mkfs.ext4 and the kernel.
_require_scratch_ext4_feature()
{
if [ -z "$1" ]; then
echo "Usage: _require_scratch_ext4_feature feature"
exit 1
fi
$MKFS_EXT4_PROG -F $MKFS_OPTIONS -O "$1" \
$SCRATCH_DEV 512m >/dev/null 2>&1 \
|| _notrun "mkfs.ext4 doesn't support $1 feature"
_try_scratch_mount >/dev/null 2>&1 \
|| _notrun "Kernel doesn't support the ext4 feature(s): $1"
_scratch_unmount
}
# this test requires that external log/realtime devices are not in use
#
_require_nonexternal()
{
[ "$USE_EXTERNAL" = yes ] && \
_notrun "External device testing in progress, skipped this test"
}
# this test requires that the kernel supports asynchronous I/O
_require_aio()
{
$here/src/feature -A
case $? in
0)
;;
1)
_notrun "kernel does not support asynchronous I/O"
;;
*)
_fail "unexpected error testing for asynchronous I/O support"
;;
esac
}
# this test requires that a (specified) aio-dio executable exists
# and that the kernel supports asynchronous I/O.
# $1 - command (optional)
#
_require_aiodio()
{
if [ -z "$1" ]
then
AIO_TEST=$here/src/aio-dio-regress/aiodio_sparse2
[ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
else
AIO_TEST=$here/src/aio-dio-regress/$1
[ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
fi
_require_aio
_require_odirect
}
# this test requires that the kernel supports IO_URING
_require_io_uring()
{
$here/src/feature -R
case $? in
0)
;;
1)
_notrun "kernel does not support IO_URING"
;;
*)
_fail "unexpected error testing for IO_URING support"
;;
esac
}
# test whether the mount_setattr syscall is available
_require_mount_setattr()
{
$here/src/feature -r
case $? in
0)
;;
1)
_notrun "kernel does not support mount_setattr syscall"
;;
*)
_fail "unexpected error testing for mount_setattr support"
;;
esac
}
# test whether idmapped mounts are supported
_require_idmapped_mounts()
{
IDMAPPED_MOUNTS_TEST=$here/src/idmapped-mounts/idmapped-mounts
[ -x $IDMAPPED_MOUNTS_TEST ] || _notrun "idmapped-mounts utilities required"
_require_mount_setattr
$here/src/idmapped-mounts/idmapped-mounts --supported \
--device "$TEST_DEV" \
--mount "$TEST_DIR" \
--fstype "$FSTYP"
if [ $? -ne 0 ]; then
_notrun "idmapped-mounts not support by $FSTYP"
fi
}
# this test requires that a test program exists under src/
# $1 - command (require)
#
_require_test_program()
{
local prog=$here/src/$1
[ -x $prog ] || _notrun "$prog not built"
}
# run an aio-dio program
# $1 - command
_run_aiodio()
{
if [ -z "$1" ]
then
echo "usage: _run_aiodio command_name" 2>&1
status=1; exit 1
fi
_require_aiodio $1
local testtemp=$TEST_DIR/aio-testfile
rm -f $testtemp
$AIO_TEST $testtemp 2>&1
status=$?
rm -f $testtemp
return $status
}
_require_timestamp_range()
{
local device=${1:-$TEST_DEV}
local tsmin tsmax
read tsmin tsmax <<<$(_filesystem_timestamp_range $device)
if [ $tsmin -eq -1 -a $tsmax -eq -1 ]; then
_notrun "filesystem $FSTYP timestamp bounds are unknown"
fi
# expect console warning from rw scratch mount if fs limit is near
if [ $tsmax -le $((1<<31)) ] && \
! _check_dmesg_for "filesystem being mounted at .* supports timestamps until"
then
_notrun "Kernel does not support timestamp limits"
fi
}
_filesystem_timestamp_range()
{
local device=${1:-$TEST_DEV}
local fstyp=${2:-$FSTYP}
u32max=$(((1<<32)-1))
s32min=-$((1<<31))
s32max=$(((1<<31)-1))
s64max=$(((1<<63)-1))
s64min=$((1<<63))
case $fstyp in
ext2)
echo "$s32min $s32max"
;;
ext3|ext4)
if [ $(dumpe2fs -h $device 2>/dev/null | grep "Inode size:" | cut -d: -f2) -gt 128 ]; then
printf "%d %d\n" $s32min 0x37fffffff
else
echo "$s32min $s32max"
fi
;;
jfs)
echo "0 $u32max"
;;
xfs)
_xfs_timestamp_range "$device"
;;
btrfs)
echo "$s64min $s64max"
;;
overlay)
if [ ! -z $OVL_BASE_FSTYP -a $OVL_BASE_FSTYP != "overlay" ]; then
_filesystem_timestamp_range $OVL_BASE_TEST_DEV $OVL_BASE_FSTYP
else
echo "-1 -1"
fi
;;
*)
echo "-1 -1"
;;
esac
}
# indicate whether YP/NIS is active or not
#
_yp_active()
{
local dn
dn=$(domainname 2>/dev/null)
local ypcat=$(type -P ypcat)
local rpcinfo=$(type -P rpcinfo)
test -n "${dn}" -a "${dn}" != "(none)" -a "${dn}" != "localdomain" -a -n "${ypcat}" -a -n "${rpcinfo}" && \
"${rpcinfo}" -p localhost 2>/dev/null | grep -q ypbind
echo $?
}
# cat the password file
#
_cat_passwd()
{
[ $(_yp_active) -eq 0 ] && ypcat passwd
cat /etc/passwd
}
# cat the group file
#
_cat_group()
{
[ $(_yp_active) -eq 0 ] && ypcat group
cat /etc/group
}
# check for a user on the machine, fsgqa as default
#
_require_user()
{
qa_user=fsgqa
if [ -n "$1" ];then
qa_user=$1
fi
_cat_passwd | grep -q $qa_user
[ "$?" == "0" ] || _notrun "$qa_user user not defined."
echo /bin/true | su $qa_user
[ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
}
# check for a chown support
#
_require_chown()
{
local rnd_uid=4242
local file="$TEST_DIR/chown_testfile"
rm -f $file
touch $file
chown ${rnd_uid}:${rnd_uid} $file >/dev/null 2>&1 \
|| _notrun "chown is not supported ${FSTYP}"
}
# check for a chmod support
# Since chmod sometimes fails silently actual functionality test is done
#
_require_chmod()
{
local file="$TEST_DIR/chmod_testfile"
rm -f $file
touch $file
# get original file mode
local mode=`stat --format="0%a" $file`
# flip the user's read bit
let mode^=0400
chmod `printf '%o' "$mode"` $file
# check that the chmod actually flipped the bit
[ `stat --format="0%a" $file` == `printf '0%o' "$mode"` ] \
|| _notrun "chmod is not supported ${FSTYP}"
}
# check for a group on the machine, fsgqa as default
#
_require_group()
{
qa_group=fsgqa
if [ -n "$1" ];then
qa_group=$1
fi
_cat_group | grep -q $qa_group
[ "$?" == "0" ] || _notrun "$qa_group group not defined."
}
_filter_user_do()
{
perl -ne "
s,.*Permission\sdenied.*,Permission denied,;
s,.*no\saccess\sto\stty.*,,;
s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
s,^\s*$,,;
print;"
}
_user_do()
{
echo $1 | su -s /bin/bash $qa_user 2>&1 | _filter_user_do
}
_require_xfs_io_command()
{
if [ -z "$1" ]
then
echo "Usage: _require_xfs_io_command command [switch]" 1>&2
exit 1
fi
local command=$1
shift
local param="$*"
local param_checked=""
local opts=""
local attr_info=""
local testfile=$TEST_DIR/$$.xfs_io
local testio
case $command in
"lsattr")
# Test xfs_io lsattr support and filesystem FS_IOC_FSSETXATTR
# support.
testio=`$XFS_IO_PROG -F -f -c "lsattr $param" $testfile 2>&1`
param_checked="$param"
;;
"chattr")
local testdir=$TEST_DIR/$$.attr_dir
mkdir $TEST_DIR/$$.attr_dir
if [ -z "$param" ]; then
param=s
fi
# Test xfs_io chattr support AND
# filesystem FS_IOC_FSSETXATTR support
# 'tPnE' flags are only valid for a directory so check them on a directory.
if echo "$param" | egrep -q 't|P|n|E'; then
testio=`$XFS_IO_PROG -F -c "chattr +$param" $testdir 2>&1`
attr_info=`$XFS_IO_PROG -F -r -c "lsattr" $testdir | awk '{print $1}'`
$XFS_IO_PROG -F -r -c "chattr -$param" $testdir 2>&1
else
testio=`$XFS_IO_PROG -F -f -c "chattr +$param" $testfile 2>&1`
attr_info=`$XFS_IO_PROG -F -r -c "lsattr" $testfile | awk '{print $1}'`
$XFS_IO_PROG -F -r -c "chattr -$param" $testfile 2>&1
fi
param_checked="+$param"
rm -rf $testdir 2>&1 > /dev/null
;;
"chproj")
testio=`$XFS_IO_PROG -F -f -c "chproj 0" $testfile 2>&1`
;;
"copy_range")
local testcopy=$TEST_DIR/$$.copy.xfs_io
local copy_opts=$testfile
if [ "$param" == "-f" ]; then
# source file is the open destination file
testcopy=$testfile
copy_opts="0 -d 4k -l 4k"
fi
$XFS_IO_PROG -F -f -c "pwrite 0 4k" $testfile > /dev/null 2>&1
testio=`$XFS_IO_PROG -F -f -c "copy_range $param $copy_opts" $testcopy 2>&1`
rm -f $testcopy > /dev/null 2>&1
param_checked="$param"
;;
"falloc" )
testio=`$XFS_IO_PROG -F -f -c "falloc $param 0 1m" $testfile 2>&1`
param_checked="$param"
;;
"fpunch" | "fcollapse" | "zero" | "fzero" | "finsert" | "funshare")
local blocksize=$(_get_block_size $TEST_DIR)
testio=`$XFS_IO_PROG -F -f -c "pwrite 0 $((5 * $blocksize))" \
-c "fsync" -c "$command $blocksize $((2 * $blocksize))" \
$testfile 2>&1`
;;
"fiemap")
# If 'ranged' is passed as argument then we check to see if fiemap supports
# ranged query params
if echo "$param" | grep -q "ranged"; then
param=$(echo "$param" | sed "s/ranged//")
$XFS_IO_PROG -c "help fiemap" | grep -q "\[offset \[len\]\]"
[ $? -eq 0 ] || _notrun "xfs_io $command ranged support is missing"
fi
testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
-c "fiemap -v $param" $testfile 2>&1`
param_checked="$param"
;;
"flink")
local testlink=$TEST_DIR/$$.link.xfs_io
testio=`$XFS_IO_PROG -F -f -c "flink $testlink" $testfile 2>&1`
rm -f $testlink > /dev/null 2>&1
;;
"-T")
# Check O_TMPFILE support in xfs_io, kernel and fs
testio=`$XFS_IO_PROG -T -c quit $TEST_DIR 2>&1`
echo $testio | egrep -q "invalid option|Is a directory" && \
_notrun "xfs_io $command support is missing"
echo $testio | grep -q "Operation not supported" && \
_notrun "O_TMPFILE is not supported"
;;
"fsmap")
testio=`$XFS_IO_PROG -f -c "fsmap" $testfile 2>&1`
echo $testio | grep -q "Inappropriate ioctl" && \
_notrun "xfs_io $command support is missing"
;;
"label")
testio=`$XFS_IO_PROG -c "label" $TEST_DIR 2>&1`
;;
"open")
# -c "open $f" is broken in xfs_io <= 4.8. Along with the fix,
# a new -C flag was introduced to execute one shot commands.
# Check for -C flag support as an indication for the bug fix.
testio=`$XFS_IO_PROG -F -f -C "open $testfile" $testfile 2>&1`
echo $testio | grep -q "invalid option" && \
_notrun "xfs_io $command support is missing"
;;
"pwrite")
# -N (RWF_NOWAIT) only works with direct vectored I/O writes
local pwrite_opts=" "
if [ "$param" == "-N" ]; then
opts+=" -d"
pwrite_opts+="-V 1 -b 4k"
fi
testio=`$XFS_IO_PROG -f $opts -c \
"pwrite $pwrite_opts $param 0 4k" $testfile 2>&1`
param_checked="$pwrite_opts $param"
;;
"scrub"|"repair")
testio=`$XFS_IO_PROG -x -c "$command probe" $TEST_DIR 2>&1`
echo $testio | grep -q "Inappropriate ioctl" && \
_notrun "xfs_io $command support is missing"
;;
"utimes" )
testio=`$XFS_IO_PROG -f -c "utimes 0 0 0 0" $testfile 2>&1`
;;
"syncfs")
touch $testfile
testio=`$XFS_IO_PROG -c "syncfs" $testfile 2>&1`
;;
*)
testio=`$XFS_IO_PROG -c "help $command" 2>&1`
esac
rm -f $testfile 2>&1 > /dev/null
echo $testio | grep -q "not found" && \
_notrun "xfs_io $command $param_checked support is missing"
echo $testio | grep -q "Operation not supported\|Inappropriate ioctl" && \
_notrun "xfs_io $command $param_checked failed (old kernel/wrong fs?)"
echo $testio | grep -q "Invalid" && \
_notrun "xfs_io $command $param_checked failed (old kernel/wrong fs/bad args?)"
echo $testio | grep -q "foreign file active" && \
_notrun "xfs_io $command $param_checked not supported on $FSTYP"
echo $testio | grep -q "Function not implemented" && \
_notrun "xfs_io $command $param_checked support is missing (missing syscall?)"
echo $testio | grep -q "unknown flag" && \
_notrun "xfs_io $command $param_checked support is missing (unknown flag)"
[ -n "$param" ] || return
if [ -z "$param_checked" ]; then
$XFS_IO_PROG -c "help $command" | grep -E -q "^ $param ([a-zA-Z_]+ )?--" || \
_notrun "xfs_io $command doesn't support $param"
else
# xfs_io could result in "command %c not supported" if it was
# built on kernels not supporting pwritev2() calls
echo $testio | grep -q "\(invalid option\|not supported\)" && \
_notrun "xfs_io $command doesn't support $param"
fi
# On XFS, ioctl(FSSETXATTR)(called by xfs_io -c "chattr") maskes off unsupported
# or invalid flags silently so need to check these flags by extra ioctl(FSGETXATTR)
# (called by xfs_io -c "lsattr").
# The following URL explains why we don't change the behavior of XFS.
# https://www.spinics.net/lists/linux-xfs/msg44725.html
if [ -n "$attr_info" -a "$FSTYP" = "xfs" ]; then
local num=${#param}
for i in $(seq 0 $((num-1))); do
echo $attr_info | grep -q "${param:$i:1}" || \
_notrun "xfs_io $command +${param:$i:1} support is missing (unknown flag in kernel)"
done
fi
}
# check that kernel and filesystem support direct I/O
_require_odirect()
{
if [ $FSTYP = "ext4" ] || [ $FSTYP = "f2fs" ] ; then
if echo "$MOUNT_OPTIONS" | grep -q "test_dummy_encryption"; then
_notrun "$FSTYP encryption doesn't support O_DIRECT"
fi
fi
if [ $FSTYP = "ext4" ] ; then
if echo "$MOUNT_OPTIONS" | grep -q "data=journal"; then
_notrun "ext4 data journaling doesn't support O_DIRECT"
fi
fi
local testfile=$TEST_DIR/$$.direct
$XFS_IO_PROG -F -f -d -c "pwrite 0 20k" $testfile > /dev/null 2>&1
if [ $? -ne 0 ]; then
_notrun "O_DIRECT is not supported"
fi
rm -f $testfile 2>&1 > /dev/null
}
_format_swapfile() {
local fname="$1"
local sz="$2"
rm -f "$fname"
touch "$fname"
chmod 0600 "$fname"
# Swap files must be nocow on Btrfs.
$CHATTR_PROG +C "$fname" > /dev/null 2>&1
_pwrite_byte 0x61 0 "$sz" "$fname" >> $seqres.full
# Ignore permission complaints on filesystems that don't support perms
$MKSWAP_PROG "$fname" 2> >(grep -v 'insecure permission' >&2) >> $seqres.full
}
_swapon_file() {
local fname="$1"
# Ignore permission complaints on filesystems that don't support perms
swapon "$fname" 2> >(grep -v "insecure permissions" >&2)
}
# Check that the filesystem supports swapfiles
_require_scratch_swapfile()
{
_require_scratch
_require_command "$MKSWAP_PROG" "mkswap"
_scratch_mkfs >/dev/null 2>&1
# With mounting SELinux context(e.g. system_u:object_r:root_t:s0),
# standard mkswap tried to reset the type of default context to
# swapfile_t if it's not swapfile_t, and then it failed and returned
# ENOTSUP expectedly as we don't want to create any SELinux attr on
# purpose. standard mkswap ignored this relabel error by commit
# d97dc0e of util-linux, but it still reported the error before
# commit d97dc0e. We mount swapfile context directly to skip the
# reset step.
[ -n "$SELINUX_MOUNT_OPTIONS" ] && export \
SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:swapfile_t:s0"
_scratch_mount
# Minimum size for mkswap is 10 pages
_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10))
# ext* has supported all variants of swap files since their
# introduction, so swapon should not fail.
case "$FSTYP" in
ext2|ext3|ext4)
if ! swapon "$SCRATCH_MNT/swap" >/dev/null 2>&1; then
if _check_s_dax "$SCRATCH_MNT/swap" 1 >/dev/null; then
_scratch_unmount
_notrun "swapfiles are not supported"
else
_scratch_unmount
_fail "swapon failed for $FSTYP"
fi
fi
;;
*)
if ! swapon "$SCRATCH_MNT/swap" >/dev/null 2>&1; then
_scratch_unmount
_notrun "swapfiles are not supported"
fi
;;
esac
swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
_scratch_unmount
}
# Check that a fs has enough free space (in 1024b blocks)
#
_require_fs_space()
{
local mnt=$1
local blocks=$2 # in units of 1024
local gb=$(( blocks / 1024 / 1024 ))
local free_blocks=`df -kP $mnt | grep -v Filesystem | awk '{print $4}'`
[ $free_blocks -lt $blocks ] && \
_notrun "This test requires at least ${gb}GB free on $mnt to run"
}
#
# Check if the filesystem supports sparse files.
#
# Unfortunately there is no better way to do this than a manual black list.
#
_require_sparse_files()
{
case $FSTYP in
hfsplus|exfat)
_notrun "Sparse files not supported by this filesystem type: $FSTYP"
;;
*)
;;
esac
}
_require_debugfs()
{
#boot_params always present in debugfs
[ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
}
_require_fail_make_request()
{
[ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
|| _notrun "$DEBUGFS_MNT/fail_make_request \
not found. Seems that CONFIG_FAULT_INJECTION_DEBUG_FS kernel config option not enabled"
}
# Disable extent zeroing for ext4 on the given device
_ext4_disable_extent_zeroout()
{
local dev=${1:-$TEST_DEV}
local sdev=`_short_dev $dev`
[ -f /sys/fs/ext4/$sdev/extent_max_zeroout_kb ] && \
echo 0 >/sys/fs/ext4/$sdev/extent_max_zeroout_kb
}
# The default behavior of SEEK_HOLE is to always return EOF.
# Filesystems that implement non-default behavior return the offset
# of holes with SEEK_HOLE. There is no way to query the filesystem
# of which behavior it is implementing.
# We use this whitelist FSTYP, to set expectation and avoid silent
# regression of filesystem seek hole behavior.
#
# Return 0 for true
_fstyp_has_non_default_seek_data_hole()
{
if [ -z $1 ]; then
local fstyp=$FSTYP
else
local fstyp=$1
fi
case "$fstyp" in
btrfs|ext4|xfs|cifs|f2fs|gfs2|ocfs2|tmpfs)
return 0
;;
nfs*)
# NFSv2 and NFSv3 only support default behavior of SEEK_HOLE,
# while NFSv4 supports non-default behavior
local nfsvers=`_df_device $TEST_DEV | $AWK_PROG '{ print $2 }'`
[ "$nfsvers" = "nfs4" ]
return $?
;;
overlay)
if [ ! -z $OVL_BASE_FSTYP -a $OVL_BASE_FSTYP != "overlay" ]; then
_fstyp_has_non_default_seek_data_hole $OVL_BASE_FSTYP
return $?
else
# Assume that base fs has default behavior
return 1
fi
;;
*)
# by default fstyp has default SEEK_HOLE behavior;
# if your fs has non-default behavior, add it to whitelist above!
return 1
;;
esac
}
# Run seek sanity test with predefined expectation for SEEK_DATA/HOLE behavior
_run_seek_sanity_test()
{
local testseekargs
if _fstyp_has_non_default_seek_data_hole; then
testseekargs+="-f"
fi
$here/src/seek_sanity_test $testseekargs $*
}
# Check if the file system supports seek_data/hole
_require_seek_data_hole()
{
local dev=${1:-$TEST_DEV}
local testfile=$TEST_DIR/$$.seek
local testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`
rm -f $testfile &>/dev/null
echo $testseek | grep -q "Kernel does not support" && \
_notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
# Disable extent zeroing for ext4 as that change where holes are
# created
if [ "$FSTYP" = "ext4" ]; then
_ext4_disable_extent_zeroout $dev
fi
}
_require_runas()
{
_require_test_program "runas"
}
_runas()
{
"$here/src/runas" "$@"
}
_require_richacl_prog()
{
_require_command "$GETRICHACL_PROG" getrichacl
_require_command "$SETRICHACL_PROG" setrichacl
}
_require_scratch_richacl_xfs()
{
_scratch_mkfs_xfs_supported -m richacl=1 >/dev/null 2>&1 \
|| _notrun "mkfs.xfs doesn't have richacl feature"
_scratch_mkfs_xfs -m richacl=1 >/dev/null 2>&1
_try_scratch_mount >/dev/null 2>&1 \
|| _notrun "kernel doesn't support richacl feature on $FSTYP"
_scratch_unmount
}
_require_scratch_richacl_ext4()
{
_scratch_mkfs -O richacl >/dev/null 2>&1 \
|| _notrun "can't mkfs $FSTYP with option -O richacl"
_try_scratch_mount >/dev/null 2>&1 \
|| _notrun "kernel doesn't support richacl feature on $FSTYP"
_scratch_unmount
}
_require_scratch_richacl_support()
{
_scratch_mount
$GETFATTR_PROG -n system.richacl >/dev/null 2>&1 \
|| _notrun "this test requires richacl support on \$SCRATCH_DEV"
_scratch_unmount
}
_require_scratch_richacl()
{
case "$FSTYP" in
xfs) _require_scratch_richacl_xfs
;;
ext4) _require_scratch_richacl_ext4
;;
nfs*|cifs|overlay)
_require_scratch_richacl_support
;;
*) _notrun "this test requires richacl support on \$SCRATCH_DEV"
;;
esac
}
_scratch_mkfs_richacl()
{
case "$FSTYP" in
xfs) _scratch_mkfs_xfs -m richacl=1
;;
ext4) _scratch_mkfs -O richacl
;;
nfs*|cifs|overlay)
_scratch_mkfs
;;
esac
}
# check if the given device is mounted, if so, return mount point
_is_dev_mounted()
{
local dev=$1
local fstype=${2:-$FSTYP}
if [ $# -lt 1 ]; then
echo "Usage: _is_dev_mounted <device> [fstype]" 1>&2
exit 1
fi
findmnt -rncv -S $dev -t $fstype -o TARGET | head -1
}
# check if the given dir is a mount point, if so, return mount point
_is_dir_mountpoint()
{
local dir=$1
local fstype=${2:-$FSTYP}
if [ $# -lt 1 ]; then
echo "Uasge: _is_dir_mountpoint <dir> [fstype]" 1>&2
exit 1
fi
findmnt -rncv -t $fstype -o TARGET $dir | head -1
}
# remount a FS to a new mode (ro or rw)
#
_remount()
{
if [ $# -ne 2 ]
then
echo "Usage: _remount device ro/rw" 1>&2
exit 1
fi
local device=$1
local mode=$2
if ! mount -o remount,$mode $device
then
echo "_remount: failed to remount filesystem on $device as $mode"
exit 1
fi
}
# Run the appropriate repair/check on a filesystem
#
# if the filesystem is mounted, it's either remounted ro before being
# checked or it's unmounted and then remounted
#
# If set, we remount ro instead of unmounting for fsck
USE_REMOUNT=0
_umount_or_remount_ro()
{
if [ $# -ne 1 ]
then
echo "Usage: _umount_or_remount_ro <device>" 1>&2
exit 1
fi
local device=$1
local mountpoint=`_is_dev_mounted $device`
if [ $USE_REMOUNT -eq 0 ]; then
$UMOUNT_PROG $device
else
_remount $device ro
fi
echo "$mountpoint"
}
_mount_or_remount_rw()
{
if [ $# -ne 3 ]; then
echo "Usage: _mount_or_remount_rw <opts> <dev> <mnt>" 1>&2
exit 1
fi
local mount_opts=$1
local device=$2
local mountpoint=$3
if [ $USE_REMOUNT -eq 0 ]; then
if [ "$FSTYP" != "overlay" ]; then
_mount -t $FSTYP $mount_opts $device $mountpoint
else
_overlay_mount $device $mountpoint
fi
if [ $? -ne 0 ]; then
_dump_err "!!! failed to remount $device on $mountpoint"
return 0 # ok=0
fi
else
_remount $device rw
fi
return 1 # ok=1
}
# Check a generic filesystem in no-op mode; this assumes that the
# underlying fsck program accepts "-n" for a no-op (check-only) run,
# and that it will still return an errno for corruption in this mode.
#
# Filesystems which don't support this will need to define their
# own check routine.
#
_check_generic_filesystem()
{
local device=$1
# If type is set, we're mounted
local type=`_fs_type $device`
local ok=1
if [ "$type" = "$FSTYP" ]
then
# mounted ...
local mountpoint=`_umount_or_remount_ro $device`
fi
fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
if [ $? -ne 0 ]
then
_log_err "_check_generic_filesystem: filesystem on $device is inconsistent"
echo "*** fsck.$FSTYP output ***" >>$seqres.full
cat $tmp.fsck >>$seqres.full
echo "*** end fsck.$FSTYP output" >>$seqres.full
ok=0
fi
rm -f $tmp.fsck
if [ $ok -eq 0 ] && [ -n "$DUMP_CORRUPT_FS" ]; then
case "$FSTYP" in
ext*)
local flatdev="$(basename "$device")"
_ext4_metadump "$seqres.$flatdev.check.qcow2" "$device" compress
;;
esac
fi
if [ $ok -eq 0 ]
then
echo "*** mount output ***" >>$seqres.full
_mount >>$seqres.full
echo "*** end mount output" >>$seqres.full
elif [ "$type" = "$FSTYP" ]
then
# was mounted ...
_mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
ok=$?
fi
if [ $ok -eq 0 ]; then
status=1
if [ "$iam" != "check" ]; then
exit 1
fi
return 1
fi
return 0
}
# Filter the knowen errors the UDF Verifier reports.
_udf_test_known_error_filter()
{
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."
}
_check_udf_filesystem()
{
[ "$DISABLE_UDF_TEST" == "1" ] && return
if [ $# -ne 1 -a $# -ne 2 ]
then
echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
exit 1
fi
if [ ! -x $here/src/udf_test ]
then
echo "udf_test not installed, please download and build the Philips"
echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
echo "Then copy the udf_test binary to $here/src/."
echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
echo "to 1."
return
fi
local device=$1
local opt_arg=""
if [ $# -eq 2 ]; then
opt_arg="-lastvalidblock $(( $2 - 1 ))"
fi
rm -f $seqres.checkfs
sleep 1 # Due to a problem with time stamps in udf_test
$here/src/udf_test $opt_arg $device | tee $seqres.checkfs | egrep "Error|Warning" | \
_udf_test_known_error_filter | \
egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" && \
echo "Warning UDF Verifier reported errors see $seqres.checkfs." && return 1
return 0
}
_check_test_fs()
{
case $FSTYP in
xfs)
_check_xfs_test_fs
;;
nfs)
# no way to check consistency for nfs
;;
cifs)
# no way to check consistency for cifs
;;
9p)
# no way to check consistency for 9p
;;
virtiofs)
# no way to check consistency for virtiofs
;;
ceph)
# no way to check consistency for CephFS
;;
glusterfs)
# no way to check consistency for GlusterFS
;;
overlay)
_check_overlay_test_fs
;;
pvfs2)
;;
udf)
# do nothing for now
;;
btrfs)
_check_btrfs_filesystem $TEST_DEV
;;
tmpfs)
# no way to check consistency for tmpfs
;;
ubifs)
# there is no fsck program for ubifs yet
;;
*)
_check_generic_filesystem $TEST_DEV
;;
esac
}
_check_scratch_fs()
{
local device=$SCRATCH_DEV
[ $# -eq 1 ] && device=$1
case $FSTYP in
xfs)
local scratch_log="none"
local scratch_rt="none"
[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
scratch_log="$SCRATCH_LOGDEV"
[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
scratch_rt="$SCRATCH_RTDEV"
_check_xfs_filesystem $device $scratch_log $scratch_rt
;;
udf)
_check_udf_filesystem $device $udf_fsize
;;
nfs*)
# Don't know how to check an NFS filesystem, yet.
;;
cifs)
# Don't know how to check a CIFS filesystem, yet.
;;
9p)
# no way to check consistency for 9p
;;
virtiofs)
# no way to check consistency for virtiofs
;;
ceph)
# no way to check consistency for CephFS
;;
glusterfs)
# no way to check consistency for GlusterFS
;;
overlay)
_check_overlay_scratch_fs
;;
pvfs2)
;;
btrfs)
_check_btrfs_filesystem $device
;;
tmpfs)
# no way to check consistency for tmpfs
;;
ubifs)
# there is no fsck program for ubifs yet
;;
*)
_check_generic_filesystem $device
;;
esac
}
_full_fstyp_details()
{
[ -z "$FSTYP" ] && FSTYP=xfs
if [ $FSTYP = xfs ]; then
if [ -d /proc/fs/xfs ]; then
if grep -q 'debug 0' /proc/fs/xfs/stat; then
FSTYP="$FSTYP (non-debug)"
elif grep -q 'debug 1' /proc/fs/xfs/stat; then
FSTYP="$FSTYP (debug)"
fi
else
if uname -a | grep -qi 'debug'; then
FSTYP="$FSTYP (debug)"
else
FSTYP="$FSTYP (non-debug)"