#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2022 Red Hat, Inc. All Rights Reserved.
#
# FS QA Test No. 545
#
# Create a filesystem which contains an inode with a lower number
# than the root inode. Ensure that xfsdump/xfsrestore handles this.
#
. ./common/preamble
_begin_fstest auto quick dump

# Import common functions.
. ./common/dump

_supported_fs xfs
_require_xfs_io_command "falloc"
_require_scratch

# A large stripe unit will put the root inode out quite far
# due to alignment, leaving free blocks ahead of it.
_scratch_mkfs_xfs -d sunit=1024,swidth=1024 > $seqres.full 2>&1

# Mounting /without/ a stripe should allow inodes to be allocated
# in lower free blocks, without the stripe alignment.
_scratch_mount -o sunit=0,swidth=0

root_inum=$(stat -c %i $SCRATCH_MNT)

# Consume space after the root inode so that the blocks before
# root look "close" for the next inode chunk allocation
$XFS_IO_PROG -f -c "falloc 0 16m" $SCRATCH_MNT/fillfile

# And make a bunch of inodes until we (hopefully) get one lower
# than root, in a new inode chunk.
echo "root_inum: $root_inum" >> $seqres.full
for i in $(seq 0 4096) ; do
	fname=$SCRATCH_MNT/$(printf "FILE_%03d" $i)
	touch $fname
	inum=$(stat -c "%i" $fname)
	[[ $inum -lt $root_inum ]] && break
done

echo "created: $inum" >> $seqres.full

[[ $inum -lt $root_inum ]] || _notrun "Could not set up test"

# Now try a dump and restore. Cribbed from xfs/068
_create_dumpdir_stress

echo -n "Before: " >> $seqres.full
_count_dumpdir_files | tee $tmp.before >> $seqres.full

# filter out the file count, it changes as fsstress adds new operations
_do_dump_restore | sed -e "/entries processed$/s/[0-9][0-9]*/NUM/g"

echo -n "After: " >> $seqres.full
_count_restoredir_files | tee $tmp.after >> $seqres.full
diff -u $tmp.before $tmp.after

# success, all done
status=0
exit
