#!/bin/bash
dev="/dev/arm_nvme/test"
mnt="/mnt/btrfs"
fsx="/home/adam/xfstests-dev/ltp/fsx"

nr_loops=1
umount $dev &> /dev/null
umount $mnt &> /dev/null

#echo 256 > /sys/kernel/debug/tracing/buffer_size_kb
#echo stacktrace > /sys/kernel/debug/tracing/trace_options
rmmod btrfs
insmod /home/adam/linux/fs/btrfs/btrfs.ko
sync

_fail()
{
	echo "!!! failed !!!"
	exit 1
}

workload()
{
	dmesg -C
	trace-cmd clear
	mkfs.btrfs -f $dev -d DUP -m DUP -s 4k
	mount $dev -o nospace_cache $mnt
	xfs_io -f -c "pwrite -S 0xcd 0 12K" $mnt/file
	umount $mnt
	xfs_io -c "pwrite -S 0x00 575668224 4k" $dev
	xfs_io -c "pwrite -S 0x00 1649414144 4k" $dev
	mount $dev -o nospace_cache $mnt
	cat $mnt/file > /dev/null
	btrfs scrub start -B $mnt
	#btrfs scrub start -B $mnt
	umount $mnt
	cp /sys/kernel/debug/tracing/trace /tmp
}

for (( i = 0; i < $nr_loops; i++ ))
do
	echo "=== $i / $nr_loops ==="
	workload
done
