// https://syzkaller.appspot.com/bug?id=3d67d693e0529df8ac89ba55b00b54e5d967e021
// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include <linux/loop.h>

unsigned long long procid;

struct fs_image_segment {
  void* data;
  uintptr_t size;
  uintptr_t offset;
};

#define IMAGE_MAX_SEGMENTS 4096
#define IMAGE_MAX_SIZE (129 << 20)

#define sys_memfd_create 319

static unsigned long fs_image_segment_check(unsigned long size,
                                            unsigned long nsegs, long segments)
{
  unsigned long i;
  struct fs_image_segment* segs = (struct fs_image_segment*)segments;
  if (nsegs > IMAGE_MAX_SEGMENTS)
    nsegs = IMAGE_MAX_SEGMENTS;
  for (i = 0; i < nsegs; i++) {
    if (segs[i].size > IMAGE_MAX_SIZE)
      segs[i].size = IMAGE_MAX_SIZE;
    segs[i].offset %= IMAGE_MAX_SIZE;
    if (segs[i].offset > IMAGE_MAX_SIZE - segs[i].size)
      segs[i].offset = IMAGE_MAX_SIZE - segs[i].size;
    if (size < segs[i].offset + segs[i].offset)
      size = segs[i].offset + segs[i].offset;
  }
  if (size > IMAGE_MAX_SIZE)
    size = IMAGE_MAX_SIZE;
  return size;
}

static long syz_mount_image(volatile long fsarg, volatile long dir,
                            volatile unsigned long size,
                            volatile unsigned long nsegs,
                            volatile long segments, volatile long flags,
                            volatile long optsarg)
{
  char loopname[64], fs[32], opts[256];
  int loopfd, err = 0, res = -1;
  unsigned long i;

  size = fs_image_segment_check(size, nsegs, segments);
  int memfd = syscall(sys_memfd_create, "syz_mount_image", 0);
  if (memfd == -1) {
    err = errno;
    goto error;
  }
  if (ftruncate(memfd, size)) {
    err = errno;
    goto error_close_memfd;
  }
  for (i = 0; i < nsegs; i++) {
    struct fs_image_segment* segs = (struct fs_image_segment*)segments;
    int res1 = 0;
    res1 = pwrite(memfd, segs[i].data, segs[i].size, segs[i].offset);
    if (res1 < 0) {
    }
  }
  snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid);
  loopfd = open(loopname, O_RDWR);
  if (loopfd == -1) {
    err = errno;
    goto error_close_memfd;
  }
  if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
    if (errno != EBUSY) {
      err = errno;
      goto error_close_loop;
    }
    ioctl(loopfd, LOOP_CLR_FD, 0);
    usleep(1000);
    if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
      err = errno;
      goto error_close_loop;
    }
  }
  mkdir((char*)dir, 0777);
  memset(fs, 0, sizeof(fs));
  strncpy(fs, (char*)fsarg, sizeof(fs) - 1);
  memset(opts, 0, sizeof(opts));
  strncpy(opts, (char*)optsarg, sizeof(opts) - 32);
  if (strcmp(fs, "iso9660") == 0) {
    flags |= MS_RDONLY;
  } else if (strncmp(fs, "ext", 3) == 0) {
    if (strstr(opts, "errors=panic") || strstr(opts, "errors=remount-ro") == 0)
      strcat(opts, ",errors=continue");
  } else if (strcmp(fs, "xfs") == 0) {
    strcat(opts, ",nouuid");
  }
  if (mount(loopname, (char*)dir, fs, flags, opts)) {
    err = errno;
    goto error_clear_loop;
  }
  res = 0;
error_clear_loop:
  ioctl(loopfd, LOOP_CLR_FD, 0);
error_close_loop:
  close(loopfd);
error_close_memfd:
  close(memfd);
error:
  errno = err;
  return res;
}

int main(void)
{
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x32ul, -1, 0);

  memcpy((void*)0x20000040, "tmpfs\000", 6);
  memcpy((void*)0x200000c0, "./file0\000", 8);
  memcpy((void*)0x20000340,
         "\x6d\x70\x6f\x6c\x3d\x3d\x93\x74\x61\x74\x69\x63\x3a\x36\x2d\x36\x3a"
         "\x00\x00\x00\x00\x00\x00\x00\x50\xfe\xf4\x9b\xee\xe0\x08\x00\x00\x00"
         "\xbb\x91\x9b\xe6\x45\xf0\x9b\x20\x29\xaf\x1d\x36\xbd\x98\x17\xf6\x8e"
         "\x5a\x52\x43\x60\xc9\x0f\x28\x8a\x19\x72\x91\x16\x1b\xbf\xa5\xf9\xc1"
         "\x5a\xec\xb9\x9a\xf3\x46\x90\xe9\x69\x27\x29\x67\x44\x14\xd3\x41\x69"
         "\x3e\x71\x51\x6c\xe3\x6d\x18\xc9\xa4\xfc\xbe\x48\xb6\x03\xae\xc5\xd8"
         "\x06\x73\xab\xdd\x34\x4a\xb8\x0d\xab\x94\x1d\xc7\xb0\xcf\x35\xba\xfd"
         "\xc3\x9f\x3b\xd7\x1c\x22\x59\x92\xa0\xd8\x81\x2f\x0d\xf7\x92\x66\x29"
         "\xee\x5f\x35\x60\xcb\xc0\x34\xdb\x90\x0d\x8e\xee\x67\x86\x35\x9c\x66"
         "\xe8\x68\x74\x78\x35\x64\x3e\xb3\xf8\xc1\x42\xf5\x70\xe8\x80\x3c\x6e"
         "\x51\xaf\x30\x7c\xf0\xb9\x26\xc9\xbd\x00\x00\x00\x00\xba\x8b\xc1\x61"
         "\x02\x5d\x8f\x30\xd4\xb2\x9b\x22\xb6\x6e\x30\xe7\xcc\xfd\x97\x5b\xf9"
         "\x6e\xbc\x43\xac\x14\x97\xf3\xa9\x67\x1b\xc9\xc4\x17\x5a\x49\x62\x7e"
         "\x74\x68\x4a\x46\x3d\x85\xf5\xe1\xbd\x31\x1c\x9e\x9a\x56\x54\x8c\xa0"
         "\x8d\x3f\x92\x28\xc1\x8e\x6f\x83\xcf\x75\x46\xd4\x19\xa8\x42\x33\xfc"
         "\xc5\x0a\xb7\xbb\x26\x3f\x49\x3a\xe4\xda\x1c\x87\x50\x3c\x7d\xcd\x2e"
         "\x6f\xbc\xa2\x8a\xa6\x14\x0a\x2c\xb3\xf2\x8a\x9f\x02\xa9\x68\x58\x44"
         "\x9a\xd7\x0d\xcf\xe1\xcc\x14\x27\xb7\x91\x7f\xdb\xf4\xcb\x65\x36\x12"
         "\x3d\xc3\x3a\xee\x9c\x82\xaf\x88\xbd\x12\xee\x14\x9a\xde\x0a\x25\xc4",
         323);
  syz_mount_image(0x20000040, 0x200000c0, 0, 0, 0, 0, 0x20000340);
  return 0;
}