/*
 module load gcc/8.2.0
 gcc reproducer.c -o reproducer -W -Wall --pedantic
 */

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <lustre/lustre_user.h>

int
main (int argc, char * argv[]) {

    int i, fd;
    struct lov_user_md *lum;
    size_t lumsize = sizeof(struct lov_user_md) + LOV_MAX_STRIPE_COUNT * sizeof(struct lov_user_ost_data);

    if ( (lum = calloc(1, lumsize)) == NULL ) {
        perror("calloc");
        exit(-1);
    }

    lum->lmm_magic = LOV_USER_MAGIC;
    lum->lmm_stripe_count = LOV_MAX_STRIPE_COUNT;

    printf("LOV_USER_MAGIC %x LOV_MAX_STRIPE_COUNT %d lumsize %lu\n", LOV_USER_MAGIC, LOV_MAX_STRIPE_COUNT, lumsize);
    printf("sizeof(struct lov_user_md) %lu sizeof(struct lov_user_ost_data) %lu\n", sizeof(struct lov_user_md), sizeof(struct lov_user_ost_data));

    fd = open(argv[argc - 1], O_RDONLY);

    if ( ioctl( fd, LL_IOC_LOV_GETSTRIPE, (void *)lum ) == -1 ) {
        perror("ioctl");
        exit(-1);
    }

    printf("size %u count %d offset %d\n", lum->lmm_stripe_size, lum->lmm_stripe_count, lum->lmm_stripe_offset);

    for ( i = 0; i < lum->lmm_stripe_count; ++i ) {
      printf("stripe %d index %d\n", i, lum->lmm_objects[i].l_ost_idx);
    }

    close(fd);
    exit(0);
}

