/*
 * DF-2819 trigger: fill a tmpfs with empty files until the kmalloc_obj
 * node-zone limit (mis-derived from unaligned sizeof) trips ENOSPC.
 *
 * Run as an unprivileged user inside the tmpfs mount:
 *	./fill /mnt/tfill 1500000
 */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char **argv)
{
	const char *dir = argv[1];
	long max = atol(argv[2]);
	char path[512];
	long i;
	int fd;

	for (i = 0; i < max; i++) {
		snprintf(path, sizeof(path), "%s/f%ld", dir, i);
		fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
		if (fd < 0) {
			fprintf(stderr, "stopped at %ld: %s\n", i, strerror(errno));
			return 1;
		}
		close(fd);
		if ((i % 100000) == 0)
			fprintf(stderr, "progress %ld\n", i);
	}
	fprintf(stderr, "done %ld\n", max);
	return 0;
}
