#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2007 Silicon Graphics, Inc.  All Rights Reserved.
#
# FSQA Test No. 166
#
# ->page-mkwrite test - unwritten extents and mmap
#
. ./common/preamble
_begin_fstest rw metadata auto quick

# Import common functions.
. ./common/filter

# assumes 1st, 3rd and 5th blocks are single written blocks,
# the others are unwritten.
_filter_blocks()
{
	$AWK_PROG '
/^ +[0-9]/ {
	if (!written_size) {
		written_size = $6
		unwritten1 = ((1048576/512) / 2) - written_size
		unwritten2 = ((1048576/512) / 2) - 2 * written_size
	}

	# is the extent unwritten?
	unwritten_ext = 0;
	if ($7 >= 10000)
		unwritten_ext = 1;

	size = "RIGHT"
	flags = "GOOD"
	if (unwritten_ext) {
		if (unwritten1) {
			if ($6 != unwritten1)
				size = "WRONG"
			unwritten1 = 0;
		} else if ($6 != unwritten2) {
			size = "WRONG"
		}
	} else {
		if ($6 != written_size)
			size = "WRONG"
	}
	print $1, "[AA..BB]", "XX..YY", "AG", "(AA..BB)", size, flags
}'
}

# real QA test starts here
_supported_fs xfs

_require_scratch
_require_xfs_io_command "falloc"

_scratch_mkfs_xfs >/dev/null 2>&1
_scratch_mount

TEST_FILE=$SCRATCH_MNT/test_file
TEST_PROG=$here/src/unwritten_mmap
FILE_SIZE=1048576

rm -f $TEST_FILE
$TEST_PROG $FILE_SIZE $TEST_FILE

xfs_bmap -vp $TEST_FILE >> $seqres.full
xfs_bmap -vp $TEST_FILE | _filter_blocks

status=0
exit
