#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2017 Red Hat, Inc.  All Rights Reserved.
#
# FS QA Test 289
#
# Test to ensure xfs_growfs command rejects non-existent mount points
# and accepts mounted targets.
#
. ./common/preamble
_begin_fstest growfs auto quick

# Override the default cleanup function.
_cleanup()
{
    $UMOUNT_PROG $tmpdir
    $UMOUNT_PROG $tmpbind
    rmdir $tmpdir
    rm -f $tmpsymlink
    rmdir $tmpbind
    rm -f $tmpfile
}

# Import common functions.
. ./common/filter

# real QA test starts here

# Modify as appropriate.
_supported_fs xfs
_require_test
_require_loop

tmpfile=$TEST_DIR/fsfile
tmpdir=$TEST_DIR/tmpdir
tmpsymlink=$TEST_DIR/tmpsymlink.$$
tmpbind=$TEST_DIR/tmpbind.$$

mkdir -p $tmpdir || _fail "!!! failed to create temp mount dir"

echo "=== mkfs.xfs ==="
$MKFS_XFS_PROG -d file,name=$tmpfile,size=16m -f >/dev/null 2>&1

echo "=== truncate ==="
$XFS_IO_PROG -fc "truncate 256m" $tmpfile

echo "=== xfs_growfs - unmounted, command should be rejected ==="
$XFS_GROWFS_PROG $tmpdir 2>&1 |  _filter_test_dir

echo "=== xfs_growfs - check relative path, unmounted ==="
cd $TEST_DIR
$XFS_GROWFS_PROG ./tmpdir 2>&1 | _filter_test_dir

echo "=== xfs_growfs - no path, unmounted ==="
$XFS_GROWFS_PROG tmpdir 2>&1 | _filter_test_dir

echo "=== xfs_growfs - plain file - should be rejected ==="
$XFS_GROWFS_PROG $tmpfile 2>&1 | _filter_test_dir

echo "=== mount ==="
$MOUNT_PROG -o loop $tmpfile $tmpdir || _fail "!!! failed to loopback mount"

echo "=== xfs_growfs - mounted - check absolute path ==="
$XFS_GROWFS_PROG -D 8192 $tmpdir | _filter_test_dir > /dev/null

echo "=== xfs_growfs - check relative path ==="
$XFS_GROWFS_PROG -D 12288 ./tmpdir > /dev/null

echo "=== xfs_growfs - no path ==="
$XFS_GROWFS_PROG -D 16384 tmpdir > /dev/null

echo "=== xfs_growfs - symbolic link ==="
ln -s $tmpdir $tmpsymlink
$XFS_GROWFS_PROG -D 20480 $tmpsymlink | _filter_test_dir > /dev/null

echo "=== xfs_growfs - symbolic link using relative path ==="
$XFS_GROWFS_PROG -D 24576 ./tmpsymlink.$$ > /dev/null

echo "=== xfs_growfs - symbolic link using no path ==="
$XFS_GROWFS_PROG -D 28672 tmpsymlink.$$ > /dev/null

echo "=== xfs_growfs - bind mount ==="
mkdir $tmpbind
$MOUNT_PROG -o bind $tmpdir $tmpbind
$XFS_GROWFS_PROG -D 32768 $tmpbind | _filter_test_dir > /dev/null

echo "=== xfs_growfs - bind mount - relative path ==="
$XFS_GROWFS_PROG -D 36864 ./tmpbind.$$ > /dev/null

echo "=== xfs_growfs - bind mount - no path ==="
$XFS_GROWFS_PROG -D 40960 tmpbind.$$ > /dev/null

echo "=== xfs_growfs - plain file - should be rejected ==="
$XFS_GROWFS_PROG $tmpfile 2>&1 | _filter_test_dir

# success, all done
status=0
exit
