/* Original Hesela experiment. Only operates on one newly created temporary file. */
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/fiemap.h>
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>

#define LENGTH (1024 * 1024)
#define EXTENTS 64

static void fail(const char *operation) {
    perror(operation);
    exit(EXIT_FAILURE);
}

static void observe(int fd, const char *stage, int comma) {
    struct stat st;
    unsigned char buffer[8192];
    size_t total = 0;
    size_t nonzero = 0;
    if (fsync(fd) != 0) fail("fsync");
    if (fstat(fd, &st) != 0) fail("fstat");
    while (total < LENGTH) {
        ssize_t count = pread(fd, buffer, sizeof(buffer), (off_t)total);
        if (count < 0 && errno == EINTR) continue;
        if (count < 0) fail("pread");
        if (count == 0) { fprintf(stderr, "unexpected EOF\n"); exit(1); }
        for (ssize_t i = 0; i < count; i++) nonzero += buffer[i] != 0;
        total += (size_t)count;
    }
    struct fiemap *map = calloc(1, sizeof(*map) + EXTENTS * sizeof(struct fiemap_extent));
    if (!map) fail("calloc");
    map->fm_length = LENGTH;
    map->fm_extent_count = EXTENTS;
    /* No FIEMAP sync request: an explicit checked fsync preceded every observation. */
    if (ioctl(fd, FS_IOC_FIEMAP, map) != 0) fail("FIEMAP");
    if (map->fm_mapped_extents == EXTENTS &&
        !(map->fm_extents[EXTENTS - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
        fprintf(stderr, "extent buffer incomplete\n");
        exit(1);
    }
    printf("%s{\"stage\":\"%s\",\"logicalBytes\":%jd,\"blocks512\":%jd,"
           "\"allocatedBytes\":%jd,\"bytesRead\":%zu,\"nonzeroBytes\":%zu,\"extents\":[",
           comma ? "," : "", stage, (intmax_t)st.st_size, (intmax_t)st.st_blocks,
           (intmax_t)st.st_blocks * 512, total, nonzero);
    for (unsigned i = 0; i < map->fm_mapped_extents; i++) {
        const struct fiemap_extent *e = &map->fm_extents[i];
        printf("%s{\"logicalBytes\":%ju,\"lengthBytes\":%ju,\"flags\":%u}",
               i ? "," : "", (uintmax_t)e->fe_logical, (uintmax_t)e->fe_length, e->fe_flags);
    }
    printf("]}");
    free(map);
    if (nonzero != 0 || st.st_size != LENGTH) {
        fprintf(stderr, "zero-content or file-length invariant failed\n");
        exit(1);
    }
}

int main(void) {
    char path[] = "allocation-XXXXXX";
    int fd = mkstemp(path);
    if (fd < 0) fail("mkstemp");
    if (unlink(path) != 0) fail("unlink temporary file");
    if (ftruncate(fd, LENGTH) != 0) fail("ftruncate");
    printf("[");
    observe(fd, "sparse", 0);
    if (fallocate(fd, 0, 0, LENGTH) != 0) fail("fallocate");
    observe(fd, "preallocated", 1);
    if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, LENGTH) != 0)
        fail("punch hole");
    observe(fd, "punched", 1);
    if (fallocate(fd, FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE, 0, LENGTH) != 0)
        fail("zero range");
    observe(fd, "zero-range", 1);
    if (close(fd) != 0) fail("close");
    printf("]\n");
    return 0;
}
