int
main (int argc, char *argv[])
{
    glfs_t    *fs = NULL;
    glfs_fd_t *fd = NULL;
    int        ret = 1;

    if (argc != 4) {
        fprintf (stderr, "Syntax: %s <host> <volname> <file-path> \n", argv[0]);
        return 1;
    }

    fs = glfs_new (argv[2]);
    if (!fs) {
        fprintf (stderr, "glfs_new: returned NULL\n");
        return 1;
    }

    ret = glfs_set_volfile_server (fs, "tcp", argv[1], 24007);
    if (ret != 0) {
        fprintf (stderr, "glfs_set_volfile_server: retuned %d\n", ret);
        goto out;
    }
    ret = glfs_set_logging (fs, "/var/log/glusterfs/glfs-dis.log", 7);
    if (ret != 0) {
        fprintf (stderr, "glfs_set_logging: returned %d\n", ret);
        goto out;
    }
    ret = glfs_init (fs);
    if (ret != 0) {
        fprintf (stderr, "glfs_init: returned %d\n", ret);
        goto out;
    }

    fd = glfs_open (fs, argv[3], O_RDWR);
    if (fd == NULL) {
        fprintf (stderr, "glfs_open: returned NULL\n");
        goto out;
    }

    /* Zero-fill "foo" with 10MB of data */
    ret = glfs_zerofill (fd, 0, 10485760);
    if (ret <= 0) {
        fprintf (stderr, "glfs_zerofill: returned %d\n", ret);
        goto out;
    }

    glfs_close(fd);

    ret = 0;

out:
    glfs_fini (fs);
    return ret;
}
Beispiel #2
0
static int qemu_gluster_open(BlockDriverState *bs, const char *filename,
    QDict *options, int bdrv_flags)
{
    BDRVGlusterState *s = bs->opaque;
    int open_flags = O_BINARY;
    int ret = 0;
    GlusterConf *gconf = g_malloc0(sizeof(GlusterConf));

    s->glfs = qemu_gluster_init(gconf, filename);
    if (!s->glfs) {
        ret = -errno;
        goto out;
    }

    if (bdrv_flags & BDRV_O_RDWR) {
        open_flags |= O_RDWR;
    } else {
        open_flags |= O_RDONLY;
    }

    if ((bdrv_flags & BDRV_O_NOCACHE)) {
        open_flags |= O_DIRECT;
    }

    s->fd = glfs_open(s->glfs, gconf->image, open_flags);
    if (!s->fd) {
        ret = -errno;
        goto out;
    }

    ret = qemu_pipe(s->fds);
    if (ret < 0) {
        ret = -errno;
        goto out;
    }
    fcntl(s->fds[GLUSTER_FD_READ], F_SETFL, O_NONBLOCK);
    qemu_aio_set_fd_handler(s->fds[GLUSTER_FD_READ],
        qemu_gluster_aio_event_reader, NULL, qemu_gluster_aio_flush_cb, s);

out:
    qemu_gluster_gconf_free(gconf);
    if (!ret) {
        return ret;
    }
    if (s->fd) {
        glfs_close(s->fd);
    }
    if (s->glfs) {
        glfs_fini(s->glfs);
    }
    return ret;
}
Beispiel #3
0
static int qemu_gluster_open(BlockDriverState *bs,  QDict *options,
                             int bdrv_flags, Error **errp)
{
    BDRVGlusterState *s = bs->opaque;
    int open_flags = 0;
    int ret = 0;
    GlusterConf *gconf = g_malloc0(sizeof(GlusterConf));
    QemuOpts *opts;
    Error *local_err = NULL;
    const char *filename;

    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
    qemu_opts_absorb_qdict(opts, options, &local_err);
    if (local_err) {
        error_propagate(errp, local_err);
        ret = -EINVAL;
        goto out;
    }

    filename = qemu_opt_get(opts, "filename");

    s->glfs = qemu_gluster_init(gconf, filename, errp);
    if (!s->glfs) {
        ret = -errno;
        goto out;
    }

    qemu_gluster_parse_flags(bdrv_flags, &open_flags);

    s->fd = glfs_open(s->glfs, gconf->image, open_flags);
    if (!s->fd) {
        ret = -errno;
    }

out:
    qemu_opts_del(opts);
    qemu_gluster_gconf_free(gconf);
    if (!ret) {
        return ret;
    }
    if (s->fd) {
        glfs_close(s->fd);
    }
    if (s->glfs) {
        glfs_fini(s->glfs);
    }
    return ret;
}
Beispiel #4
0
static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
                                       BlockReopenQueue *queue, Error **errp)
{
    int ret = 0;
    BDRVGlusterReopenState *reop_s;
    GlusterConf *gconf = NULL;
    int open_flags = 0;

    assert(state != NULL);
    assert(state->bs != NULL);

    state->opaque = g_malloc0(sizeof(BDRVGlusterReopenState));
    reop_s = state->opaque;

    qemu_gluster_parse_flags(state->flags, &open_flags);

    gconf = g_malloc0(sizeof(GlusterConf));

    reop_s->glfs = qemu_gluster_init(gconf, state->bs->filename, errp);
    if (reop_s->glfs == NULL) {
        ret = -errno;
        goto exit;
    }

    reop_s->fd = glfs_open(reop_s->glfs, gconf->image, open_flags);
    if (reop_s->fd == NULL) {
        /* reops->glfs will be cleaned up in _abort */
        ret = -errno;
        goto exit;
    }

exit:
    /* state->opaque will be freed in either the _abort or _commit */
    qemu_gluster_gconf_free(gconf);
    return ret;
}
static int
gluster_get (glfs_t *fs, const char *filename) {
        glfs_fd_t *fd = NULL;
        int ret = -1;

        fd = glfs_open (fs, filename, O_RDONLY);
        if (fd == NULL) {
                error (0, errno, state->url);
                goto out;
        }

        // don't allow concurrent reads and writes.
        ret = gluster_lock (fd, F_WRLCK);
        if (ret == -1) {
                error (0, errno, state->url);
                goto out;
        }

        if ((ret = gluster_read (fd, STDOUT_FILENO)) == -1) {
                error (0, errno, "write error");
                goto out;
        }

        ret = 0;

out:
        if (fd) {
                if (glfs_close (fd) == -1) {
                        ret = -1;
                        error (0, errno, "cannot close file %s",
                                        state->gluster_url->path);
                }
        }

        return ret;
}
Beispiel #6
0
int
main(int argc, char *argv[])
{
    int ret = 1;
    int opcode = -1;
    off_t offset = 0;
    size_t len = 0;
    glfs_t *fs = NULL;
    glfs_fd_t *fd = NULL;

    if (argc != 8) {
        fprintf(stderr,
                "Syntax: %s <host> <volname> <opcode> <offset> <len> "
                "<file-path> <log-file>\n",
                argv[0]);
        return 1;
    }

    fs = glfs_new(argv[2]);
    if (!fs) {
        fprintf(stderr, "glfs_new: returned NULL\n");
        return 1;
    }

    ret = glfs_set_volfile_server(fs, "tcp", argv[1], 24007);
    if (ret != 0) {
        fprintf(stderr, "glfs_set_volfile_server: returned %d\n", ret);
        goto out;
    }

    ret = glfs_set_logging(fs, argv[7], 7);
    if (ret != 0) {
        fprintf(stderr, "glfs_set_logging: returned %d\n", ret);
        goto out;
    }

    ret = glfs_init(fs);
    if (ret != 0) {
        fprintf(stderr, "glfs_init: returned %d\n", ret);
        goto out;
    }

    opcode = atoi(argv[3]);
    opcode = get_fallocate_flag(opcode);
    if (opcode < 0) {
        fprintf(stderr, "get_fallocate_flag: invalid flag \n");
        goto out;
    }

    offset = atoi(argv[4]);
    len = atoi(argv[5]);

    fd = glfs_open(fs, argv[6], O_RDWR);
    if (fd == NULL) {
        fprintf(stderr, "glfs_open: returned NULL\n");
        goto out;
    }

    ret = glfs_fallocate(fd, opcode, offset, len);
    if (ret <= 0) {
        fprintf(stderr, "glfs_fallocate: returned %d\n", ret);
        goto out;
    }

    ret = 0;

out:
    if (fd)
        glfs_close(fd);
    glfs_fini(fs);
    return ret;
}
Beispiel #7
0
int
main(int argc, char *argv[])
{
    glfs_t *fs = NULL;
    int ret = 0, i, status = 0;
    glfs_fd_t *fd1 = NULL;
    glfs_fd_t *fd2 = NULL;
    glfs_fd_t *fd3 = NULL;
    char *filename = "file_tmp";
    char *volname = NULL;
    char *logfile = NULL;
    char *hostname = NULL;

    if (argc != 4) {
        fprintf(stderr, "Invalid argument\n");
        exit(1);
    }

    hostname = argv[1];
    volname = argv[2];
    logfile = argv[3];

    fs = glfs_new(volname);
    if (!fs) {
        fprintf(stderr, "glfs_new: returned NULL\n");
        return -1;
    }

    ret = glfs_set_volfile_server(fs, "tcp", hostname, 24007);
    LOG_ERR("glfs_set_volfile_server", ret);

    ret = glfs_set_logging(fs, logfile, 7);
    LOG_ERR("glfs_set_logging", ret);

    ret = glfs_init(fs);
    LOG_ERR("glfs_init", ret);

    fd1 = glfs_creat(fs, filename, O_RDWR | O_SYNC, 0644);
    if (fd1 <= 0) {
        ret = -1;
        LOG_ERR("glfs_creat", ret);
    }
    fprintf(stderr, "glfs-create fd1 - %d\n", fd1);

    fd2 = glfs_dup(fd1);
    fprintf(stderr, "glfs-dup fd2 - %d\n", fd2);

    fd3 = glfs_open(fs, filename, O_RDWR | O_SYNC);
    if (fd2 <= 0) {
        ret = -1;
        LOG_ERR("glfs_open", ret);
    }
    fprintf(stderr, "glfs-open fd3 - %d\n", fd3);

    /* TEST 1: Conflicting ranges, same lk_owner
     * lock1 (0, 10, lownera)
     * lock2 (5, 10, lownera)
     * Expected: should not fail but get merged
     */
    ret = lock_test(fd1, fd2, false, 0, 10, lownera, 8, 5, 10, lownera, 8);
    LOG_ERR("==== glfs_lock_test_1", ret);

    /* TEST 2: Conflicting ranges, different lk_owner
     * lock1 (0, 10, lownera) - already taken
     * lock2 (5, 10, lownerb)
     * Expected: should fail and not get merged
     */
    ret = lock_test(NULL, fd2, true, 0, 10, lownera, 8, 5, 10, lownerb, 8);
    LOG_ERR("==== glfs_lock_test_2", ret);

    /* TEST 3: Different ranges, same lk_owner
     * lock1 (0, 10, lownera) - already taken
     * lock2 (30, 10, lownera)
     * Expected: should not fail
     */
    ret = lock_test(NULL, fd2, false, 0, 10, lownera, 8, 30, 10, lownera, 8);
    LOG_ERR("==== glfs_lock_test_3", ret);

    /* TEST 4: Conflicting ranges, different lk_owner
     * lock1 (0, 10, lownera) - already taken
     * lock2 (50, 10, lownerb)
     * Expected: should not fail
     */
    ret = lock_test(NULL, fd2, false, 0, 10, lownera, 8, 50, 10, lownerb, 8);
    LOG_ERR("==== glfs_lock_test_4", ret);

    /* TEST 5: Close fd1 & retry TEST2
     * lock1 (not applicable)
     * lock2 (5, 10, lownerb)
     * Expected: should succeed now
     */
    ret = glfs_close(fd1);
    LOG_ERR("glfs_close", ret);

    ret = lock_test(NULL, fd2, false, 0, 10, lownera, 8, 5, 10, lownerb, 8);
    LOG_ERR("==== glfs_lock_test_5", ret);

    /* TEST 6: Check closing fd1 doesn't flush fd2 locks
     * retry TEST 4 but with fd2 and fd3.
     * lock1 (50, 10, lownerb) - already taken
     * lock2 (55, 10, lownerc)
     * Expected: should fail
     */
    ret = lock_test(NULL, fd3, true, 50, 10, lownerb, 8, 55, 10, lownerc, 8);
    LOG_ERR("==== glfs_lock_test_6", ret);

err:
    ret = glfs_close(fd2);
    LOG_ERR("glfs_close", ret);

    ret = glfs_close(fd3);
    LOG_ERR("glfs_close", ret);

out:
    if (fs) {
        ret = glfs_fini(fs);
        fprintf(stderr, "glfs_fini(fs) returned %d\n", ret);
    }

    if (ret)
        exit(1);
    exit(0);
}
Beispiel #8
0
int
main (int argc, char *argv[])
{
	glfs_t    *fs = NULL;
	glfs_t    *fs2 = NULL;
	int        ret = 0;
	glfs_fd_t *fd = NULL;
	glfs_fd_t *fd2 = NULL;
	struct stat sb = {0, };
	char       readbuf[32];
	char       writebuf[32];

	char      *filename = "/filename2";

	fs = glfs_new ("fsync");
	if (!fs) {
		fprintf (stderr, "glfs_new: returned NULL\n");
		return 1;
	}

//	ret = glfs_set_volfile (fs, "/tmp/posix.vol");

	ret = glfs_set_volfile_server (fs, "tcp", "localhost", 24007);

//	ret = glfs_set_volfile_server (fs, "unix", "/tmp/gluster.sock", 0);

	ret = glfs_set_logging (fs, "/dev/stderr", 7);

	ret = glfs_init (fs);

	fprintf (stderr, "glfs_init: returned %d\n", ret);

	sleep (2);

	fs2 = glfs_new ("fsync");
	if (!fs2) {
		fprintf (stderr, "glfs_new: returned NULL\n");
		return 1;
	}


//	ret = glfs_set_volfile (fs2, "/tmp/posix.vol");

	ret = glfs_set_volfile_server (fs2, "tcp", "localhost", 24007);

	ret = glfs_set_logging (fs2, "/dev/stderr", 7);

	ret = glfs_init (fs2);

	fprintf (stderr, "glfs_init: returned %d\n", ret);

	ret = glfs_lstat (fs, filename, &sb);
	fprintf (stderr, "%s: (%d) %s\n", filename, ret, strerror (errno));

	fd = glfs_creat (fs, filename, O_RDWR, 0644);
	fprintf (stderr, "%s: (%p) %s\n", filename, fd, strerror (errno));

	fd2 = glfs_open (fs2, filename, O_RDWR);
	fprintf (stderr, "%s: (%p) %s\n", filename, fd, strerror (errno));

	sprintf (writebuf, "hi there\n");
	ret = glfs_write (fd, writebuf, 32, 0);

	glfs_lseek (fd2, 0, SEEK_SET);

	ret = glfs_read (fd2, readbuf, 32, 0);

	printf ("read %d, %s", ret, readbuf);

	glfs_close (fd);
	glfs_close (fd2);

	filename = "/filename3";
	ret = glfs_mknod (fs, filename, S_IFIFO, 0);
	fprintf (stderr, "%s: (%d) %s\n", filename, ret, strerror (errno));

	ret = glfs_lstat (fs, filename, &sb);
	fprintf (stderr, "%s: (%d) %s\n", filename, ret, strerror (errno));


	ret = glfs_rename (fs, filename, "/filename4");
	fprintf (stderr, "rename(%s): (%d) %s\n", filename, ret,
		 strerror (errno));

	ret = glfs_unlink (fs, "/filename4");
	fprintf (stderr, "unlink(%s): (%d) %s\n", "/filename4", ret,
		 strerror (errno));

	filename = "/dirname2";
	ret = glfs_mkdir (fs, filename, 0);
	fprintf (stderr, "%s: (%d) %s\n", filename, ret, strerror (errno));

	ret = glfs_lstat (fs, filename, &sb);
	fprintf (stderr, "lstat(%s): (%d) %s\n", filename, ret, strerror (errno));

	ret = glfs_rmdir (fs, filename);
	fprintf (stderr, "rmdir(%s): (%d) %s\n", filename, ret, strerror (errno));

	test_dirops (fs);

	test_xattr (fs);

	test_chdir (fs);

	// done

	glfs_fini (fs);
	glfs_fini (fs2);

	return ret;
}
Beispiel #9
0
static int tcmu_glfs_open(struct tcmu_device *dev)
{
	struct glfs_state *gfsp;
	int ret = 0;
	char *config;
	struct stat st;
	int attribute;

	gfsp = calloc(1, sizeof(*gfsp));
	if (!gfsp)
		return -ENOMEM;

	tcmu_set_dev_private(dev, gfsp);

	attribute = tcmu_get_attribute(dev, "hw_block_size");
	if (attribute == -1) {
		errp("Could not get hw_block_size setting\n");
		goto fail;
	}
	gfsp->block_size = attribute;

	config = strchr(tcmu_get_dev_cfgstring(dev), '/');
	if (!config) {
		errp("no configuration found in cfgstring\n");
		goto fail;
	}
	config += 1; /* get past '/' */

	if (parse_imagepath(config, &gfsp->servername, &gfsp->volname, &gfsp->pathname) == -1) {
		errp("servername, volname, or pathname not set\n");
		goto fail;
	}

	gfsp->fs = glfs_new(gfsp->volname);
	if (!gfsp->fs) {
		errp("glfs_new failed\n");
		goto fail;
	}

	ret = glfs_set_volfile_server(gfsp->fs, "tcp", gfsp->servername,
				      GLUSTER_PORT);
	if (ret) {
		errp("glfs_set_volfile_server failed: %m\n");
		goto fail;
	}


	ret = glfs_init(gfsp->fs);
	if (ret) {
		errp("glfs_init failed: %m\n");
		goto fail;
	}

	gfsp->gfd = glfs_open(gfsp->fs, gfsp->pathname, ALLOWED_BSOFLAGS);
	if (!gfsp->gfd) {
		errp("glfs_open failed: %m\n");
		goto fail;
	}

	ret = glfs_lstat(gfsp->fs, gfsp->pathname, &st);
	if (ret) {
		errp("glfs_lstat failed: %m\n");
		goto fail;
	}

	if (st.st_size != tcmu_get_device_size(dev)) {
		errp("device size and backing size disagree: "
		       "device %lld backing %lld\n",
		       tcmu_get_device_size(dev),
		       (long long) st.st_size);
		goto fail;
	}

	return 0;

fail:
	if (gfsp->gfd)
		glfs_close(gfsp->gfd);
	if (gfsp->fs)
		glfs_fini(gfsp->fs);
	free(gfsp->volname);
	free(gfsp->pathname);
	free(gfsp->servername);
	free(gfsp);

	return -EIO;
}
Beispiel #10
0
static bool glfs_check_config(const char *cfgstring, char **reason)
{
	char *path;
	char *servername = NULL;
	char *volname = NULL;
	char *pathname = NULL;
	glfs_t *fs = NULL;
	glfs_fd_t *gfd = NULL;
	struct stat st;
	int ret;
	bool result = true;

	path = strchr(cfgstring, '/');
	if (!path) {
		if (asprintf(reason, "No path found") == -1)
			*reason = NULL;
		result = false;
		goto done;
	}
	path += 1; /* get past '/' */

	if (parse_imagepath(path, &servername, &volname, &pathname) == -1) {
		if (asprintf(reason, "Invalid imagepath") == -1)
			*reason = NULL;
		result = false;
		goto done;
	}

	/* Actually attempt to open the volume to verify things are working */
	/* TODO: consolidate this with v. similar tcmu_glfs_open code? */
	fs = glfs_new(volname);
	if (!fs) {
		if (asprintf(reason, "glfs_new failed") == -1)
			*reason = NULL;
		result = false;
		goto done;
	}

	ret = glfs_set_volfile_server(fs, "tcp", servername,
				      GLUSTER_PORT);
	if (ret) {
		if (asprintf(reason, "glfs_set_volfile_server failed: %m") == -1)
			*reason = NULL;
		result = false;
		goto done;
	}

	ret = glfs_init(fs);
	if (ret) {
		if (asprintf(reason, "glfs_init failed: %m") == -1)
			*reason = NULL;
		result = false;
		goto done;
	}

	gfd = glfs_open(fs, pathname, ALLOWED_BSOFLAGS);
	if (!gfd) {
		if (asprintf(reason, "glfs_open failed: %m") == -1)
			*reason = NULL;
		result = false;
		goto done;
	}

	ret = glfs_lstat(fs, pathname, &st);
	if (ret) {
		if (asprintf(reason, "glfs_lstat failed: %m") == -1)
			*reason = NULL;
		result = false;
		goto done;
	}

done:
	if (gfd)
		glfs_close(gfd);
	if (fs)
		glfs_fini(fs);
	free(servername);
	free(volname);
	free(pathname);

	return result;
}
Beispiel #11
0
static int
large_read_write (glfs_t *fs) {

        int       ret       = 0;
        int       j         = 0;
        glfs_fd_t *fd       = NULL;
        glfs_fd_t *fd1      = NULL;
        char      *filename = NULL;
        char      *buf      = NULL;

        ret = asprintf (&filename, "filerw%d", extension);
        if (ret < 0) {
                fprintf (stderr, "cannot construct filename (%s)",
                         strerror (errno));
                return ret;
        }

        extension++;

        fd = glfs_creat (fs, filename, O_RDWR, 0644);
        if (!fd) {
                fprintf (stderr, "%s: (%p) %s\n", filename, fd,
                        strerror (errno));
                return -1;
        }

        buf = (char *) malloc (WRITE_SIZE);
        memset (buf, '-', WRITE_SIZE);

        for (j = 0; j < READ_WRITE_LOOP; j++) {
                ret = glfs_write (fd, buf, WRITE_SIZE, 0);
                if (ret < 0) {
                        fprintf (stderr, "Write(%s): %d (%s)\n", filename, ret,
                                strerror (errno));
                        return ret;
                }
        }

        fd1 = glfs_open (fs, filename, O_RDWR);
        if (fd1 < 0) {
                fprintf (stderr, "Open(%s): %d (%s)\n", filename, ret,
                        strerror (errno));
                return -1;
        }

        glfs_lseek (fd1, 0, SEEK_SET);
        for (j = 0; j < READ_WRITE_LOOP; j++) {
                ret = glfs_read (fd1, buf, WRITE_SIZE, 0);
                if (ret < 0) {
                        fprintf (stderr, "Read(%s): %d (%s)\n", filename, ret,
                                strerror (errno));
                        return ret;
                }
        }

        for (j = 0; j < READ_WRITE_LOOP; j++) {
                ret = glfs_write (fd1, buf, WRITE_SIZE, 0);
                if (ret < 0) {
                        fprintf (stderr, "Write(%s): %d (%s)\n", filename, ret,
                                strerror (errno));
                        return ret;
                }
        }

        glfs_close (fd);
        glfs_close (fd1);
        ret = glfs_unlink (fs, filename);
        if (ret < 0) {
                fprintf (stderr, "glfs_unlink failed with ret: %d (%s)\n",
                        ret, strerror (errno));
                return -1;
        }

        free (buf);
        free (filename);
}
Beispiel #12
0
int
main (int argc, char *argv[])
{
        int        ret         = 0;
        glfs_t    *fs          = NULL;
        struct glfs_fd *fd     = NULL;
        char      *volname     = NULL;
        char      *log_file    = NULL;
        char      *hostname    = NULL;
        char      *buf = NULL;
        struct stat stat;

        if (argc != 4) {
                fprintf (stderr,
                                "Expect following args %s <hostname> <Vol> <log file location>\n"
                                , argv[0]);
                return -1;
        }

        hostname = argv[1];
        volname = argv[2];
        log_file = argv[3];

        fs = setup_new_client (hostname, volname, log_file, 0);
        if (!fs) {
                fprintf (stderr, "\nsetup_new_client: returned NULL (%s)\n",
                                 strerror (errno));
                goto error;
        }

        fd = glfs_opendir (fs, "/");
        if (!fd) {
                fprintf (stderr, "/: %s\n", strerror (errno));
                return -1;
        }

        glfs_readdirplus (fd, &stat);

        fd = glfs_open (fs, "/test", O_RDWR);
        if (fd == NULL) {
                fprintf (stderr, "glfs_open: returned NULL\n");
                goto error;
        }

        buf = (char *) malloc (5);

        ret = glfs_pread (fd, buf, 5, 0, 0);
        if (ret < 0) {
                fprintf (stderr, "Read(%s): %d (%s)\n", "test", ret,
                         strerror (errno));
                return ret;
        }

        free (buf);
        glfs_close (fd);

        ret = glfs_fini (fs);
        if (ret < 0) {
                fprintf (stderr, "glfs_fini failed with ret: %d (%s)\n",
                         ret, strerror (errno));
                return -1;
        }

        return 0;
error:
        return -1;
}
Beispiel #13
0
bool GlusterBackend::get(const SyncEvent* _getEvent) {
  if(_getEvent == nullptr || _getEvent->fullPathBuffer.empty())
    return false;

  //Try to find file
  /**
   * RETURN VALUES
   * NULL   : Failure. @errno will be set with the type of failure.
   * Others : Pointer to the opened glfs_fd_t.
   */
  glfs_fd_t *fd = glfs_open((glfs_t*)fs, _getEvent->fullPathBuffer.c_str(), O_RDONLY);
  if(!fd){
    LOG(ERROR)<<"Error while openeing a handle to:"<<_getEvent->fullPathBuffer;
    return false;
  }

  FileNode* fileNode = FileSystem::getInstance().findAndOpenNode(_getEvent->fullPathBuffer);
  //If File exist then we won't download it!
  if(fileNode!=nullptr){
    LOG(DEBUG)<<"File "<<fileNode->getFullPath()<<" already exist! no need to download.";
    //Close it! so it can be removed if needed
    uint64_t inodeNum = FileSystem::getInstance().assignINodeNum((intptr_t)fileNode);
    fileNode->close(inodeNum);
    glfs_close(fd);
    return false;
  }

  //Now create a file in FS
  //handle directories
  //FileSystem::getInstance().createHierarchy(_getEvent->fullPathBuffer,false);
  //FileNode *newFile = FileSystem::getInstance().mkFile(_getEvent->fullPathBuffer,false,true);//open
  string name = FileSystem::getInstance().getFileNameFromPath(_getEvent->fullPathBuffer);
  FileNode* newFile = new FileNode(name,_getEvent->fullPathBuffer, false,false);
  if(newFile == nullptr){
    LOG(ERROR)<<"Failed to create a newNode:"<<_getEvent->fullPathBuffer;
    glfs_close(fd);
    return false;
  }

  //and write the content
  uint64_t bufSize = 64ll*1024ll*1024ll;
  char *buff = new char[bufSize];//64MB buffer
  size_t offset = 0;
  int64_t read = 0;
  do {

    //No need to download it anymore.
    if(newFile->mustBeDeleted()){
      delete newFile;
      glfs_close(fd);
      return true;
    }

    read = glfs_pread(fd,buff,bufSize,offset,0);

    if(read < 0){
      LOG(ERROR)<<"Error while reading: "<<_getEvent->fullPathBuffer;
      glfs_close(fd);
      return false;
    }

    if(read>0){
      FileNode* afterMove = nullptr;
      long retCode = newFile->writeHandler(buff,offset,read,afterMove,true);

      while(retCode == -1)//-1 means moving
        retCode = newFile->writeHandler(buff,offset,read,afterMove,true);

      if(afterMove){
        newFile = afterMove;
        FileSystem::getInstance().replaceAllInodesByNewNode((intptr_t)newFile,(intptr_t)afterMove);
      }
      //Check space availability
      if(retCode < 0) {
        LOG(ERROR)<<"Error in writing file:"<<newFile->getFullPath()<<", probably no diskspace, Code:"<<retCode;
        delete newFile;
        glfs_close(fd);
        delete []buff;
        return false;
      }
    }

    offset += read;
  }while(read);

  newFile->setNeedSync(false);//We have just created this file so it's upload flag false
  glfs_close(fd);
  delete []buff;
  //Add it to File system
  if(FileSystem::getInstance().createHierarchy(_getEvent->fullPathBuffer,false)==nullptr){
    LOG(ERROR)<<"Error in creating hierarchy for newly downloaded file:"<<newFile->getFullPath();
    delete newFile;
    return false;
  }
  if(!FileSystem::getInstance().addFile(newFile)){
    LOG(ERROR)<<"Error in adding newly downloaded file:"<<newFile->getFullPath();
    delete newFile;
    return false;
  }
  //Gone well
  return true;
}
Beispiel #14
0
/*
 * Open a volume using gfapi.
 */
int gfapi_device::d_open(const char *pathname, int flags, int mode)
{
   int status;
   POOL_MEM virtual_filename(PM_FNAME);

   /*
    * Parse the gluster URI.
    */
   if (!m_gfapi_volume) {
      m_gfapi_volume = bstrdup(dev_name);
      if (!parse_gfapi_devicename(m_gfapi_volume,
                                  &m_transport,
                                  &m_servername,
                                  &m_volumename,
                                  &m_basedir,
                                  &m_serverport)) {
         Mmsg1(errmsg, _("Unable to parse device URI %s.\n"), dev_name);
         Emsg0(M_FATAL, 0, errmsg);
         goto bail_out;
      }
   }

   /*
    * See if we need to setup a Gluster context.
    */
   if (!m_glfs) {
      m_glfs = glfs_new(m_volumename);
      if (!m_glfs) {
         Mmsg1(errmsg, _("Unable to create new Gluster context for volumename %s.\n"), m_volumename);
         Emsg0(M_FATAL, 0, errmsg);
         goto bail_out;
      }

      status = glfs_set_volfile_server(m_glfs, (m_transport) ? m_transport : "tcp", m_servername, m_serverport);
      if (status < 0) {
         Mmsg3(errmsg, _("Unable to initialize Gluster management server for transport %s, servername %s, serverport %d\n"),
               (m_transport) ? m_transport : "tcp", m_servername, m_serverport);
         Emsg0(M_FATAL, 0, errmsg);
         goto bail_out;
      }

      status = glfs_init(m_glfs);
      if (status < 0) {
         Mmsg1(errmsg, _("Unable to initialize Gluster for volumename %s.\n"), m_volumename);
         Emsg0(M_FATAL, 0, errmsg);
         goto bail_out;
      }
   }

   /*
    * See if we don't have a file open already.
    */
   if (m_gfd) {
      glfs_close(m_gfd);
      m_gfd = NULL;
   }

   /*
    * See if we store in an explicit directory.
    */
   if (m_basedir) {
      struct stat st;

      /*
       * Make sure the dir exists if one is defined.
       */
      Mmsg(virtual_filename, "/%s", m_basedir);
      if (glfs_stat(m_glfs, virtual_filename.c_str(), &st) != 0) {
         switch (errno) {
         case ENOENT:
            if (!gfapi_makedir(m_glfs, virtual_filename.c_str())) {
               Mmsg1(errmsg, _("Specified glusterfs direcory %s cannot be created.\n"), virtual_filename.c_str());
               Emsg0(M_FATAL, 0, errmsg);
               goto bail_out;
            }
            break;
         default:
            goto bail_out;
         }
      } else {
         if (!S_ISDIR(st.st_mode)) {
            Mmsg1(errmsg, _("Specified glusterfs direcory %s is not a directory.\n"), virtual_filename.c_str());
            Emsg0(M_FATAL, 0, errmsg);
            goto bail_out;
         }
      }

      Mmsg(virtual_filename, "/%s/%s", m_basedir, getVolCatName());
   } else {
      Mmsg(virtual_filename, "%s", getVolCatName());
   }

   /*
    * See if the O_CREAT flag is set as glfs_open doesn't support that flag and you have to call glfs_creat then.
    */
   if (flags & O_CREAT) {
      m_gfd = glfs_creat(m_glfs, virtual_filename.c_str(), flags, mode);
   } else {
      m_gfd = glfs_open(m_glfs, virtual_filename.c_str(), flags);
   }

   if (!m_gfd) {
      goto bail_out;
   }

   return 0;

bail_out:
   /*
    * Cleanup the Gluster context.
    */
   if (m_glfs) {
      glfs_fini(m_glfs);
      m_glfs = NULL;
   }

   return -1;
}
int
main (int argc, char *argv[])
{
        glfs_t                   *fs = NULL;
        glfs_t                   *fs2 = NULL;
        glfs_t                   *fs_tmp = NULL;
        glfs_t                   *fs_tmp2 = NULL;
        int                       ret = 0, i;
        glfs_fd_t                *fd = NULL;
        glfs_fd_t                *fd2 = NULL;
        glfs_fd_t                *fd_tmp = NULL;
        glfs_fd_t                *fd_tmp2 = NULL;
        char                      readbuf[32];
        char                     *filename = "file_tmp";
        char                     *writebuf = NULL;
        char                     *vol_id  = NULL;
        unsigned int              cnt = 1;
        struct glfs_upcall       *cbk = NULL;
        char                     *logfile = NULL;
        char                     *volname = NULL;
        char                     *hostname = NULL;

        if (argc != 4) {
                fprintf (stderr, "Invalid argument\n");
                exit(1);
        }

        hostname = argv[1];
        volname = argv[2];
        logfile = argv[3];

        fs = glfs_new (volname);
        if (!fs) {
                fprintf (stderr, "glfs_new: returned NULL\n");
                return -1;
        }

        ret = glfs_set_volfile_server (fs, "tcp", hostname, 24007);
        LOG_ERR("glfs_set_volfile_server", ret);

        ret = glfs_set_logging (fs, logfile, 7);
        LOG_ERR("glfs_set_logging", ret);

        ret = glfs_init (fs);
        LOG_ERR("glfs_init", ret);

        /* This does not block, but enables caching of events. Real
         * applications like NFS-Ganesha run this in a thread before activity
         * on the fs (through this instance) happens. */
        ret = glfs_h_poll_upcall(fs_tmp, &cbk);
        LOG_ERR ("glfs_h_poll_upcall", ret);

        fs2 = glfs_new (volname);
        if (!fs2) {
                fprintf (stderr, "glfs_new fs2: returned NULL\n");
                return 1;
        }

        ret = glfs_set_volfile_server (fs2, "tcp", hostname, 24007);
        LOG_ERR("glfs_set_volfile_server-fs2", ret);

        ret = glfs_set_logging (fs2, logfile, 7);
        LOG_ERR("glfs_set_logging-fs2", ret);

        ret = glfs_init (fs2);
        LOG_ERR("glfs_init-fs2", ret);

        fd = glfs_creat(fs, filename, O_RDWR|O_SYNC, 0644);
        if (fd <= 0) {
                ret = -1;
                LOG_ERR ("glfs_creat", ret);
        }
        fprintf (stderr, "glfs-create fd - %d\n", fd);

        fd2 = glfs_open(fs2, filename, O_SYNC|O_RDWR|O_CREAT);
        if (fd2 <= 0) {
                ret = -1;
                LOG_ERR ("glfs_open-fs2", ret);
        }
        fprintf (stderr, "glfs-open fd2 - %d\n", fd2);

        do {
                if (cnt%2) {
                        fd_tmp = fd;
                        fs_tmp = fs;
                        fd_tmp2 = fd2;
                        fs_tmp2 = fs2;
                } else {
                        fd_tmp = fd2;
                        fs_tmp = fs2;
                        fd_tmp2 = fd;
                        fs_tmp2 = fs;
                }

                /* WRITE on fd_tmp */
                writebuf = malloc(10);
                if (writebuf) {
                        memcpy (writebuf, "abcd", 4);
                        ret = glfs_write (fd_tmp, writebuf, 4, 0);
                        if (ret <= 0)   {
                                ret = -1;
                                LOG_ERR ("glfs_write", ret);
                        } else {
                                fprintf (stderr,
                                         "glfs_write suceeded\n");
                        }
                        free(writebuf);
                } else {
                        fprintf (stderr,
                                 "Could not allocate writebuf\n");
                        return -1;
                }

                /* READ on fd_tmp2 */
                ret = glfs_lseek (fd_tmp2, 0, SEEK_SET);
                LOG_ERR ("glfs_lseek", ret);

                memset (readbuf, 0, sizeof(readbuf));
                ret = glfs_pread (fd_tmp2, readbuf, 4, 0, 0, NULL);

                if (ret <= 0) {
                        ret = -1;
                        LOG_ERR ("glfs_pread", ret);
                } else {
                        fprintf (stderr, "glfs_read: %s\n", readbuf);
                }

                /* Open() fops seem to be not performed on server side until
                 * there are I/Os on that fd
                 */
                if (cnt > 2) {
                        struct glfs_upcall_inode *in_arg = NULL;
                        enum glfs_upcall_reason   reason = 0;
                        struct glfs_object       *object = NULL;
                        uint64_t                  flags = 0;
                        uint64_t                  expire = 0;

                        ret = glfs_h_poll_upcall(fs_tmp, &cbk);
                        LOG_ERR ("glfs_h_poll_upcall", ret);

                        reason = glfs_upcall_get_reason (cbk);

                        /* Expect 'GLFS_INODE_INVALIDATE' upcall event. */
                        if (reason == GLFS_UPCALL_INODE_INVALIDATE) {
                                in_arg = glfs_upcall_get_event (cbk);

                                object = glfs_upcall_inode_get_object (in_arg);
                                flags = glfs_upcall_inode_get_flags (in_arg);
                                expire = glfs_upcall_inode_get_expire (in_arg);

                                fprintf (stderr, " upcall event type - %d,"
                                         " object(%p), flags(%d), "
                                         " expire_time_attr(%d)\n" ,
                                         reason, object, flags, expire);
                        } else {
                                fprintf (stderr,
                                         "Didnt receive upcall notify event");
                                ret = -1;
                                goto err;
                        }

                        glfs_free (cbk);
                }

                sleep(5);
        } while (++cnt < 5);

err:
        glfs_close(fd);
        LOG_ERR ("glfs_close", ret);

        glfs_close(fd2);
        LOG_ERR ("glfs_close-fd2", ret);

out:
        if (fs) {
                ret = glfs_fini(fs);
                fprintf (stderr, "glfs_fini(fs) returned %d \n", ret);
        }

        if (fs2) {
                ret = glfs_fini(fs2);
                fprintf (stderr, "glfs_fini(fs2) returned %d \n", ret);
        }

        if (ret)
                exit(1);
        exit(0);
}
Beispiel #16
0
glfs_fd_t *
glfs_java_open_read (glfs_t *glfs, const char *path)
{
	return glfs_open (glfs, path, O_LARGEFILE|O_RDONLY);
}
Beispiel #17
0
static int qemu_gluster_open(BlockDriverState *bs,  QDict *options,
                             int bdrv_flags)
{
    BDRVGlusterState *s = bs->opaque;
    int open_flags = O_BINARY;
    int ret = 0;
    GlusterConf *gconf = g_malloc0(sizeof(GlusterConf));
    QemuOpts *opts;
    Error *local_err = NULL;
    const char *filename;

    opts = qemu_opts_create_nofail(&runtime_opts);
    qemu_opts_absorb_qdict(opts, options, &local_err);
    if (error_is_set(&local_err)) {
        qerror_report_err(local_err);
        error_free(local_err);
        ret = -EINVAL;
        goto out;
    }

    filename = qemu_opt_get(opts, "filename");


    s->glfs = qemu_gluster_init(gconf, filename);
    if (!s->glfs) {
        ret = -errno;
        goto out;
    }

    if (bdrv_flags & BDRV_O_RDWR) {
        open_flags |= O_RDWR;
    } else {
        open_flags |= O_RDONLY;
    }

    if ((bdrv_flags & BDRV_O_NOCACHE)) {
        open_flags |= O_DIRECT;
    }

    s->fd = glfs_open(s->glfs, gconf->image, open_flags);
    if (!s->fd) {
        ret = -errno;
        goto out;
    }

    ret = qemu_pipe(s->fds);
    if (ret < 0) {
        ret = -errno;
        goto out;
    }
    fcntl(s->fds[GLUSTER_FD_READ], F_SETFL, O_NONBLOCK);
    qemu_aio_set_fd_handler(s->fds[GLUSTER_FD_READ],
        qemu_gluster_aio_event_reader, NULL, s);

out:
    qemu_opts_del(opts);
    qemu_gluster_gconf_free(gconf);
    if (!ret) {
        return ret;
    }
    if (s->fd) {
        glfs_close(s->fd);
    }
    if (s->glfs) {
        glfs_fini(s->glfs);
    }
    return ret;
}
Beispiel #18
0
glfs_fd_t *
glfs_java_open_write (glfs_t *glfs, const char *path)
{
	return glfs_open (glfs, path, O_LARGEFILE|O_WRONLY);
}
Beispiel #19
0
static int uwsgi_glusterfs_request(struct wsgi_request *wsgi_req) {
	char filename[PATH_MAX+1];
	/* Standard GlusterFS request */
        if (!wsgi_req->uh->pktsize) {
                uwsgi_log( "Empty GlusterFS request. skip.\n");
                return -1;
        }

        if (uwsgi_parse_vars(wsgi_req)) {
                return -1;
        }

	// blocks empty paths
	if (wsgi_req->path_info_len == 0 || wsgi_req->path_info_len > PATH_MAX) {
                uwsgi_403(wsgi_req);
                return UWSGI_OK;
	}

        wsgi_req->app_id = uwsgi_get_app_id(wsgi_req, wsgi_req->appid, wsgi_req->appid_len, glusterfs_plugin.modifier1);
	if (wsgi_req->app_id == -1 && !uwsgi.no_default_app && uwsgi.default_app > -1) {
        	if (uwsgi_apps[uwsgi.default_app].modifier1 == glusterfs_plugin.modifier1) {
                	wsgi_req->app_id = uwsgi.default_app;
                }
        }
        if (wsgi_req->app_id == -1) {
                uwsgi_404(wsgi_req);
                return UWSGI_OK;
        }

        struct uwsgi_app *ua = &uwsgi_apps[wsgi_req->app_id];

	memcpy(filename, wsgi_req->path_info, wsgi_req->path_info_len);
	filename[wsgi_req->path_info_len] = 0;

	glfs_fd_t *fd = glfs_open((glfs_t *) ua->interpreter, filename, O_RDONLY);
	if (!fd) {
                uwsgi_404(wsgi_req);
                return UWSGI_OK;
	}
	

	struct stat st;
	if (glfs_fstat(fd, &st)) {
		uwsgi_403(wsgi_req);
                return UWSGI_OK;
	}	

	if (uwsgi_response_prepare_headers(wsgi_req, "200 OK", 6)) goto end;
	size_t mime_type_len = 0;
        char *mime_type = uwsgi_get_mime_type(wsgi_req->path_info, wsgi_req->path_info_len, &mime_type_len);
        if (mime_type) {
        	if (uwsgi_response_add_content_type(wsgi_req, mime_type, mime_type_len)) goto end;
        }

        if (uwsgi_response_add_last_modified(wsgi_req, (uint64_t) st.st_mtime)) goto end;
	if (uwsgi_response_add_content_length(wsgi_req, st.st_size)) goto end;

	// skip body on HEAD
	if (uwsgi_strncmp(wsgi_req->method, wsgi_req->method_len, "HEAD", 4)) {
		size_t remains = st.st_size;
		while(remains > 0) {
			char buf[8192];
			ssize_t rlen = glfs_read (fd, buf, UMIN(remains, 8192), 0);
			if (rlen <= 0) goto end;
			if (uwsgi_response_write_body_do(wsgi_req, buf, rlen)) goto end;
			remains -= rlen;
		}
	}

end:
	glfs_close(fd);
	return UWSGI_OK;
}
int
main (int argc, char *argv[])
{
        glfs_t    *fs = NULL;
        glfs_t    *fs2 = NULL;
        glfs_t    *fs_tmp = NULL;
        glfs_t    *fs_tmp2 = NULL;
        int        ret = 0, i;
        glfs_fd_t *fd = NULL;
        glfs_fd_t *fd2 = NULL;
        glfs_fd_t *fd_tmp = NULL;
        glfs_fd_t *fd_tmp2 = NULL;
        char       readbuf[32];
        char      *filename = "file_tmp";
        char      *writebuf = NULL;
        char      *vol_id  = NULL;
        unsigned int       cnt = 1;
        struct    callback_arg cbk;
        char      *logfile = NULL;
        char      *volname = NULL;

        cbk.object = NULL;

        if (argc != 3) {
                fprintf (stderr, "Invalid argument\n");
                exit(1);
        }

        volname = argv[1];
        logfile = argv[2];

        fs = glfs_new (volname);
        if (!fs) {
                fprintf (stderr, "glfs_new: returned NULL\n");
                return -1;
        }

        ret = glfs_set_volfile_server (fs, "tcp", "localhost", 24007);
        LOG_ERR("glfs_set_volfile_server", ret);

        ret = glfs_set_logging (fs, logfile, 7);
        LOG_ERR("glfs_set_logging", ret);

        ret = glfs_init (fs);
        LOG_ERR("glfs_init", ret);

        fs2 = glfs_new (volname);
        if (!fs2) {
                fprintf (stderr, "glfs_new fs2: returned NULL\n");
                return 1;
        }

        ret = glfs_set_volfile_server (fs2, "tcp", "localhost", 24007);
        LOG_ERR("glfs_set_volfile_server-fs2", ret);

        ret = glfs_set_logging (fs2, logfile, 7);
        LOG_ERR("glfs_set_logging-fs2", ret);

        ret = glfs_init (fs2);
        LOG_ERR("glfs_init-fs2", ret);

        fd = glfs_creat(fs, filename, O_RDWR|O_SYNC, 0644);
        if (fd <= 0) {
                ret = -1;
                LOG_ERR ("glfs_creat", ret);
        }
        fprintf (stderr, "glfs-create fd - %d\n", fd);

        fd2 = glfs_open(fs2, filename, O_SYNC|O_RDWR|O_CREAT);
        if (fd2 <= 0) {
                ret = -1;
                LOG_ERR ("glfs_open-fs2", ret);
        }
        fprintf (stderr, "glfs-open fd2 - %d\n", fd2);

        do {
                if (cnt%2) {
                        fd_tmp = fd;
                        fs_tmp = fs;
                        fd_tmp2 = fd2;
                        fs_tmp2 = fs2;
                } else {
                        fd_tmp = fd2;
                        fs_tmp = fs2;
                        fd_tmp2 = fd;
                        fs_tmp2 = fs;
                }

                /* WRITE on fd_tmp */
                writebuf = malloc(10);
                if (writebuf) {
                        memcpy (writebuf, "abcd", 4);
                        ret = glfs_write (fd_tmp, writebuf, 4, 0);
                        if (ret <= 0)   {
                                ret = -1;
                                LOG_ERR ("glfs_write", ret);
                        } else {
                                fprintf (stderr,
                                         "glfs_write suceeded\n");
                        }
                        free(writebuf);
                } else {
                        fprintf (stderr,
                                 "Could not allocate writebuf\n");
                        return -1;
                }

                /* READ on fd_tmp2 */
                ret = glfs_lseek (fd_tmp2, 0, SEEK_SET);
                LOG_ERR ("glfs_lseek", ret);

                ret = glfs_pread (fd_tmp2, readbuf, 4, 0, 0);

                if (ret <= 0) {
                        ret = -1;
                        LOG_ERR ("glfs_pread", ret);
                } else {
                        fprintf (stderr, "glfs_read: %s\n", readbuf);
                }

                /* Open() fops seem to be not performed on server side until
                 * there are I/Os on that fd
                 */
                if (cnt > 2) {
                        ret = glfs_h_poll_upcall(fs_tmp, &cbk);
                        LOG_ERR ("glfs_h_poll_upcall", ret);
                        if (cbk.object) {
                                fprintf (stderr, " upcall event type - %d,"
                                                 " flags - %d, expire_time_attr - %d\n" ,
                                         cbk.reason, cbk.flags, cbk.expire_time_attr);
                        } else {
                                fprintf (stderr,
                                         "Dint receive upcall notify event");
                                ret = -1;
                                goto err;
                        }
                }

                sleep(5);
        } while (++cnt < 5);

err:
        glfs_close(fd);
        LOG_ERR ("glfs_close", ret);

        glfs_close(fd2);
        LOG_ERR ("glfs_close-fd2", ret);

out:
        if (fs) {
                ret = glfs_fini(fs);
                fprintf (stderr, "glfs_fini(fs) returned %d \n", ret);
        }

        if (fs2) {
                ret = glfs_fini(fs2);
                fprintf (stderr, "glfs_fini(fs2) returned %d \n", ret);
        }

        if (ret)
                exit(1);
        exit(0);
}