#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2021 CTERA Networks. All Rights Reserved.
#
# FS QA Test 076
#
# Support for chattr on overlayfs directories was added in kernel v5.10
# by commit 61536bed2149 ("ovl: support [S|G]ETFLAGS and FS[S|G]ETXATTR
# ioctls for directories").  That commit introduced a deadlock.
#
# This is a regression test for the fix commit b854cc659dcb ("ovl: avoid
# deadlock on directory ioctl")
#
. ./common/preamble
_begin_fstest auto quick perms dangerous

# Override the default cleanup function.
_cleanup()
{
        cd /
        $CHATTR_PROG -i $lowerdir/foo > /dev/null 2>&1
        $CHATTR_PROG -i $upperdir/foo > /dev/null 2>&1
        rm -f $tmp.*
}

# Import common functions.
. ./common/filter

# real QA test starts here
_supported_fs overlay
_require_scratch
_require_chattr i

# remove all files from previous runs
_scratch_mkfs

# prepare lower test file
lowerdir=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
upperdir=$OVL_BASE_SCRATCH_MNT/$OVL_UPPER
workdir=$OVL_BASE_SCRATCH_MNT/$OVL_WORK
mkdir -p $lowerdir
mkdir $lowerdir/foo

# mounting overlay
_scratch_mount

# Try to add the immutable attributes, it will invoke ioctl() on the directory
# The ioctl will fail on kernel < 5.10, succeed on kernel >= 5.10.15 and hang
# on kernel v5.10..v5.10.14.  Anything but hang is considered a test success.
$CHATTR_PROG +i $SCRATCH_MNT/foo > /dev/null 2>&1

$UMOUNT_PROG $SCRATCH_MNT

# success, all done
echo "Silence is golden"
status=0
exit
