#!/bin/bash

# compiled from fallocate_mod.c (https://jira.whamcloud.com/browse/LU-19018)
FALLOCATE=posix_fallocate

err() {
	echo "${0}: failed for $1"
	[[ -f $1 ]] && rm $1
	exit 1
}

usage() {
	echo "usage: ${0} <file> <size to write (bytes)>"
	echo "Eg: ${0} /mnt/lustre/dir/f1 18"
	exit 2
}

lfallocate() {
	local testfile="$1"
	local limit=$2
	TSTUSR=quota_usr # User for quota

	count=$((limit/2))
	runuser -l quota_usr -c "$FALLOCATE $testfile 0 $count" ||
		err $testfile ${count1}

	sync; sleep 1

	count1=$((limit - count))
	runuser -l quota_usr -c "$FALLOCATE $testfile ${count} ${count1}" ||
		err $testfile ${count1}

	sync; sleep 3
}

#
# main
#
(( $# == 2 )) || usage
lfallocate $1 $2

