#!/bin/sh

set +e
set -x

echo "kernel smoke test, create/mount/umount bcachefs filesystem"

STORAGE_SIZE_MB=512
STORAGE="$(mktemp)"
dd if=/dev/zero of="$STORAGE" bs=1M count=$STORAGE_SIZE_MB > /dev/null 2>&1
LODEVICE="$(losetup --find --show $STORAGE)"

cleanup() {
	losetup -d "$LODEVICE"
}
trap cleanup EXIT

bcachefs format "$LODEVICE"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: bcachefs format failed, exit code=$ret"
	exit 1
fi

MOUNTPOINT="$(mktemp -d)"
mount -t bcachefs "$LODEVICE" "$MOUNTPOINT"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: bcachefs mount failed, exit code=$ret"
	exit 1
fi

cp -r . "$MOUNTPOINT/files"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: failed to copy data to mounted filesystem"
	exit 1
fi

diff -rq . "$MOUNTPOINT/files"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: filesystem content differs from original"
	exit 1
fi

umount "$MOUNTPOINT"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: bcachefs umount failed, exit code=$ret"
	exit 1
fi

mount -t bcachefs "$LODEVICE" "$MOUNTPOINT"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: bcachefs mount failed, exit code=$ret"
	exit 1
fi

diff -rq . "$MOUNTPOINT/files"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: filesystem content differs from original after remounting"
	exit 1
fi

umount "$MOUNTPOINT"
ret=$?
if [ $ret -ne 0 ]; then
	echo "FAILED: bcachefs umount failed, exit code=$ret"
	exit 1
fi

echo "PASSED"
exit 0
