* See the COPYING file in the top-level directory.
*
*/
+
#include "qemu/osdep.h"
#include <glusterfs/api/glfs.h>
#include "block/block_int.h"
#include "qapi/error.h"
+#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
-#include "qapi/util.h"
#include "qemu/uri.h"
#include "qemu/error-report.h"
+#include "qemu/option.h"
#include "qemu/cutils.h"
#define GLUSTER_OPT_FILENAME "filename"
static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf,
const char *filename)
{
- SocketAddressFlat *gsconf;
+ SocketAddress *gsconf;
URI *uri;
QueryParams *qp = NULL;
bool is_unix = false;
return -EINVAL;
}
- gconf->server = g_new0(SocketAddressFlatList, 1);
- gconf->server->value = gsconf = g_new0(SocketAddressFlat, 1);
+ gconf->server = g_new0(SocketAddressList, 1);
+ gconf->server->value = gsconf = g_new0(SocketAddress, 1);
/* transport */
if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
- gsconf->type = SOCKET_ADDRESS_FLAT_TYPE_INET;
+ gsconf->type = SOCKET_ADDRESS_TYPE_INET;
} else if (!strcmp(uri->scheme, "gluster+tcp")) {
- gsconf->type = SOCKET_ADDRESS_FLAT_TYPE_INET;
+ gsconf->type = SOCKET_ADDRESS_TYPE_INET;
} else if (!strcmp(uri->scheme, "gluster+unix")) {
- gsconf->type = SOCKET_ADDRESS_FLAT_TYPE_UNIX;
+ gsconf->type = SOCKET_ADDRESS_TYPE_UNIX;
is_unix = true;
} else if (!strcmp(uri->scheme, "gluster+rdma")) {
- gsconf->type = SOCKET_ADDRESS_FLAT_TYPE_INET;
- error_report("Warning: rdma feature is not supported, falling "
- "back to tcp");
+ gsconf->type = SOCKET_ADDRESS_TYPE_INET;
+ warn_report("rdma feature is not supported, falling back to tcp");
} else {
ret = -EINVAL;
goto out;
struct glfs *glfs;
int ret;
int old_errno;
- SocketAddressFlatList *server;
+ SocketAddressList *server;
unsigned long long port;
glfs = glfs_find_preopened(gconf->volume);
for (server = gconf->server; server; server = server->next) {
switch (server->value->type) {
- case SOCKET_ADDRESS_FLAT_TYPE_UNIX:
+ case SOCKET_ADDRESS_TYPE_UNIX:
ret = glfs_set_volfile_server(glfs, "unix",
server->value->u.q_unix.path, 0);
break;
- case SOCKET_ADDRESS_FLAT_TYPE_INET:
+ case SOCKET_ADDRESS_TYPE_INET:
if (parse_uint_full(server->value->u.inet.port, &port, 10) < 0 ||
port > 65535) {
error_setg(errp, "'%s' is not a valid port number",
server->value->u.inet.host,
(int)port);
break;
- case SOCKET_ADDRESS_FLAT_TYPE_VSOCK:
- case SOCKET_ADDRESS_FLAT_TYPE_FD:
+ case SOCKET_ADDRESS_TYPE_VSOCK:
+ case SOCKET_ADDRESS_TYPE_FD:
default:
abort();
}
error_setg(errp, "Gluster connection for volume %s, path %s failed"
" to connect", gconf->volume, gconf->path);
for (server = gconf->server; server; server = server->next) {
- if (server->value->type == SOCKET_ADDRESS_FLAT_TYPE_UNIX) {
+ if (server->value->type == SOCKET_ADDRESS_TYPE_UNIX) {
error_append_hint(errp, "hint: failed on socket %s ",
server->value->u.q_unix.path);
} else {
QDict *options, Error **errp)
{
QemuOpts *opts;
- SocketAddressFlat *gsconf = NULL;
- SocketAddressFlatList *curr = NULL;
+ SocketAddress *gsconf = NULL;
+ SocketAddressList *curr = NULL;
QDict *backing_options = NULL;
Error *local_err = NULL;
char *str = NULL;
const char *ptr;
- size_t num_servers;
- int i, type;
+ int i, type, num_servers;
/* create opts info from runtime_json_opts list */
opts = qemu_opts_create(&runtime_json_opts, NULL, 0, &error_abort);
goto out;
}
- gsconf = g_new0(SocketAddressFlat, 1);
+ gsconf = g_new0(SocketAddress, 1);
if (!strcmp(ptr, "tcp")) {
ptr = "inet"; /* accept legacy "tcp" */
}
- type = qapi_enum_parse(SocketAddressFlatType_lookup, ptr,
- SOCKET_ADDRESS_FLAT_TYPE__MAX, -1, NULL);
- if (type != SOCKET_ADDRESS_FLAT_TYPE_INET
- && type != SOCKET_ADDRESS_FLAT_TYPE_UNIX) {
+ type = qapi_enum_parse(&SocketAddressType_lookup, ptr, -1, NULL);
+ if (type != SOCKET_ADDRESS_TYPE_INET
+ && type != SOCKET_ADDRESS_TYPE_UNIX) {
error_setg(&local_err,
"Parameter '%s' may be 'inet' or 'unix'",
GLUSTER_OPT_TYPE);
gsconf->type = type;
qemu_opts_del(opts);
- if (gsconf->type == SOCKET_ADDRESS_FLAT_TYPE_INET) {
+ if (gsconf->type == SOCKET_ADDRESS_TYPE_INET) {
/* create opts info from runtime_inet_opts list */
opts = qemu_opts_create(&runtime_inet_opts, NULL, 0, &error_abort);
qemu_opts_absorb_qdict(opts, backing_options, &local_err);
}
if (gconf->server == NULL) {
- gconf->server = g_new0(SocketAddressFlatList, 1);
+ gconf->server = g_new0(SocketAddressList, 1);
gconf->server->value = gsconf;
curr = gconf->server;
} else {
- curr->next = g_new0(SocketAddressFlatList, 1);
+ curr->next = g_new0(SocketAddressList, 1);
curr->next->value = gsconf;
curr = curr->next;
}
out:
error_propagate(errp, local_err);
- qapi_free_SocketAddressFlat(gsconf);
+ qapi_free_SocketAddress(gsconf);
qemu_opts_del(opts);
g_free(str);
QDECREF(backing_options);
return -errno;
}
-static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf,
- const char *filename,
- QDict *options, Error **errp)
+/* Converts options given in @filename and the @options QDict into the QAPI
+ * object @gconf. */
+static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
+ const char *filename,
+ QDict *options, Error **errp)
{
int ret;
if (filename) {
ret = qemu_gluster_parse_uri(gconf, filename);
if (ret < 0) {
- error_setg(errp, "invalid URI");
+ error_setg(errp, "invalid URI %s", filename);
error_append_hint(errp, "Usage: file=gluster[+transport]://"
"[host[:port]]volume/path[?socket=...]"
"[,file.debug=N]"
"[,file.logfile=/path/filename.log]\n");
- errno = -ret;
- return NULL;
+ return ret;
}
} else {
ret = qemu_gluster_parse_json(gconf, options, errp);
"file.server.1.transport=unix,"
"file.server.1.socket=/var/run/glusterd.socket ..."
"\n");
- errno = -ret;
- return NULL;
+ return ret;
}
+ }
+
+ return 0;
+}
+static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf,
+ const char *filename,
+ QDict *options, Error **errp)
+{
+ int ret;
+
+ ret = qemu_gluster_parse(gconf, filename, options, errp);
+ if (ret < 0) {
+ errno = -ret;
+ return NULL;
}
return qemu_gluster_glfs_init(gconf, errp);
qemu_coroutine_yield();
return acb.ret;
}
+#endif
-static inline bool gluster_supports_zerofill(void)
+static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
+ PreallocMode prealloc, Error **errp)
{
- return 1;
-}
+ int64_t current_length;
-static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
- int64_t size)
-{
- return glfs_zerofill(fd, offset, size);
-}
+ current_length = glfs_lseek(fd, 0, SEEK_END);
+ if (current_length < 0) {
+ error_setg_errno(errp, errno, "Failed to determine current size");
+ return -errno;
+ }
+
+ if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
+ error_setg(errp, "Cannot use preallocation for shrinking files");
+ return -ENOTSUP;
+ }
+
+ if (current_length == offset) {
+ return 0;
+ }
+
+ switch (prealloc) {
+#ifdef CONFIG_GLUSTERFS_FALLOCATE
+ case PREALLOC_MODE_FALLOC:
+ if (glfs_fallocate(fd, 0, current_length, offset - current_length)) {
+ error_setg_errno(errp, errno, "Could not preallocate data");
+ return -errno;
+ }
+ break;
+#endif /* CONFIG_GLUSTERFS_FALLOCATE */
+#ifdef CONFIG_GLUSTERFS_ZEROFILL
+ case PREALLOC_MODE_FULL:
+ if (glfs_ftruncate(fd, offset)) {
+ error_setg_errno(errp, errno, "Could not resize file");
+ return -errno;
+ }
+ if (glfs_zerofill(fd, current_length, offset - current_length)) {
+ error_setg_errno(errp, errno, "Could not zerofill the new area");
+ return -errno;
+ }
+ break;
+#endif /* CONFIG_GLUSTERFS_ZEROFILL */
+ case PREALLOC_MODE_OFF:
+ if (glfs_ftruncate(fd, offset)) {
+ error_setg_errno(errp, errno, "Could not resize file");
+ return -errno;
+ }
+ break;
+ default:
+ error_setg(errp, "Unsupported preallocation mode: %s",
+ PreallocMode_str(prealloc));
+ return -EINVAL;
+ }
-#else
-static inline bool gluster_supports_zerofill(void)
-{
return 0;
}
-static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
- int64_t size)
+static int qemu_gluster_co_create(BlockdevCreateOptions *options,
+ Error **errp)
{
- return 0;
+ BlockdevCreateOptionsGluster *opts = &options->u.gluster;
+ struct glfs *glfs;
+ struct glfs_fd *fd = NULL;
+ int ret = 0;
+
+ assert(options->driver == BLOCKDEV_DRIVER_GLUSTER);
+
+ glfs = qemu_gluster_glfs_init(opts->location, errp);
+ if (!glfs) {
+ ret = -errno;
+ goto out;
+ }
+
+ fd = glfs_creat(glfs, opts->location->path,
+ O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
+ if (!fd) {
+ ret = -errno;
+ goto out;
+ }
+
+ ret = qemu_gluster_do_truncate(fd, opts->size, opts->preallocation, errp);
+
+out:
+ if (fd) {
+ if (glfs_close(fd) != 0 && ret == 0) {
+ ret = -errno;
+ }
+ }
+ glfs_clear_preopened(glfs);
+ return ret;
}
-#endif
-static int qemu_gluster_create(const char *filename,
- QemuOpts *opts, Error **errp)
+static int coroutine_fn qemu_gluster_co_create_opts(const char *filename,
+ QemuOpts *opts,
+ Error **errp)
{
+ BlockdevCreateOptions *options;
+ BlockdevCreateOptionsGluster *gopts;
BlockdevOptionsGluster *gconf;
- struct glfs *glfs;
- struct glfs_fd *fd;
- int ret = 0;
- int prealloc = 0;
- int64_t total_size = 0;
char *tmp = NULL;
+ Error *local_err = NULL;
+ int ret;
+
+ options = g_new0(BlockdevCreateOptions, 1);
+ options->driver = BLOCKDEV_DRIVER_GLUSTER;
+ gopts = &options->u.gluster;
gconf = g_new0(BlockdevOptionsGluster, 1);
+ gopts->location = gconf;
+
+ gopts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
+ BDRV_SECTOR_SIZE);
+
+ tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
+ gopts->preallocation = qapi_enum_parse(&PreallocMode_lookup, tmp,
+ PREALLOC_MODE_OFF, &local_err);
+ g_free(tmp);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ ret = -EINVAL;
+ goto fail;
+ }
+
gconf->debug = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG,
GLUSTER_DEBUG_DEFAULT);
if (gconf->debug < 0) {
}
gconf->has_logfile = true;
- glfs = qemu_gluster_init(gconf, filename, NULL, errp);
- if (!glfs) {
- ret = -errno;
- goto out;
+ ret = qemu_gluster_parse(gconf, filename, NULL, errp);
+ if (ret < 0) {
+ goto fail;
}
- total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
- BDRV_SECTOR_SIZE);
-
- tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
- if (!tmp || !strcmp(tmp, "off")) {
- prealloc = 0;
- } else if (!strcmp(tmp, "full") && gluster_supports_zerofill()) {
- prealloc = 1;
- } else {
- error_setg(errp, "Invalid preallocation mode: '%s'"
- " or GlusterFS doesn't support zerofill API", tmp);
- ret = -EINVAL;
- goto out;
+ ret = qemu_gluster_co_create(options, errp);
+ if (ret < 0) {
+ goto fail;
}
- fd = glfs_creat(glfs, gconf->path,
- O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
- if (!fd) {
- ret = -errno;
- } else {
- if (!glfs_ftruncate(fd, total_size)) {
- if (prealloc && qemu_gluster_zerofill(fd, 0, total_size)) {
- ret = -errno;
- }
- } else {
- ret = -errno;
- }
-
- if (glfs_close(fd) != 0) {
- ret = -errno;
- }
- }
-out:
- g_free(tmp);
- qapi_free_BlockdevOptionsGluster(gconf);
- glfs_clear_preopened(glfs);
+ ret = 0;
+fail:
+ qapi_free_BlockdevCreateOptions(options);
return ret;
}
}
static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset,
- Error **errp)
+ PreallocMode prealloc, Error **errp)
{
- int ret;
BDRVGlusterState *s = bs->opaque;
-
- ret = glfs_ftruncate(s->fd, offset);
- if (ret < 0) {
- ret = -errno;
- error_setg_errno(errp, -ret, "Failed to truncate file");
- return ret;
- }
-
- return 0;
+ return qemu_gluster_do_truncate(s->fd, offset, prealloc, errp);
}
static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
if (offs < 0) {
return -errno; /* D3 or D4 */
}
- assert(offs >= start);
+
+ if (offs < start) {
+ /* This is not a valid return by lseek(). We are safe to just return
+ * -EIO in this case, and we'll treat it like D4. Unfortunately some
+ * versions of gluster server will return offs < start, so an assert
+ * here will unnecessarily abort QEMU. */
+ return -EIO;
+ }
if (offs > start) {
/* D2: in hole, next data at offs */
if (offs < 0) {
return -errno; /* D1 and (H3 or H4) */
}
- assert(offs >= start);
+
+ if (offs < start) {
+ /* This is not a valid return by lseek(). We are safe to just return
+ * -EIO in this case, and we'll treat it like H4. Unfortunately some
+ * versions of gluster server will return offs < start, so an assert
+ * here will unnecessarily abort QEMU. */
+ return -EIO;
+ }
if (offs > start) {
/*
}
/*
- * Returns the allocation status of the specified sectors.
+ * Returns the allocation status of the specified offset.
*
- * If 'sector_num' is beyond the end of the disk image the return value is 0
- * and 'pnum' is set to 0.
+ * The block layer guarantees 'offset' and 'bytes' are within bounds.
*
- * 'pnum' is set to the number of sectors (including and immediately following
- * the specified sector) that are known to be in the same
+ * 'pnum' is set to the number of bytes (including and immediately following
+ * the specified offset) that are known to be in the same
* allocated/unallocated state.
*
- * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
- * beyond the end of the disk image it will be clamped.
+ * 'bytes' is the max value 'pnum' should be set to.
*
- * (Based on raw_co_get_block_status() from file-posix.c.)
+ * (Based on raw_co_block_status() from file-posix.c.)
*/
-static int64_t coroutine_fn qemu_gluster_co_get_block_status(
- BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum,
- BlockDriverState **file)
+static int coroutine_fn qemu_gluster_co_block_status(BlockDriverState *bs,
+ bool want_zero,
+ int64_t offset,
+ int64_t bytes,
+ int64_t *pnum,
+ int64_t *map,
+ BlockDriverState **file)
{
BDRVGlusterState *s = bs->opaque;
- off_t start, data = 0, hole = 0;
- int64_t total_size;
+ off_t data = 0, hole = 0;
int ret = -EINVAL;
if (!s->fd) {
return ret;
}
- start = sector_num * BDRV_SECTOR_SIZE;
- total_size = bdrv_getlength(bs);
- if (total_size < 0) {
- return total_size;
- } else if (start >= total_size) {
- *pnum = 0;
- return 0;
- } else if (start + nb_sectors * BDRV_SECTOR_SIZE > total_size) {
- nb_sectors = DIV_ROUND_UP(total_size - start, BDRV_SECTOR_SIZE);
+ if (!want_zero) {
+ *pnum = bytes;
+ *map = offset;
+ *file = bs;
+ return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
}
- ret = find_allocation(bs, start, &data, &hole);
+ ret = find_allocation(bs, offset, &data, &hole);
if (ret == -ENXIO) {
/* Trailing hole */
- *pnum = nb_sectors;
+ *pnum = bytes;
ret = BDRV_BLOCK_ZERO;
} else if (ret < 0) {
/* No info available, so pretend there are no holes */
- *pnum = nb_sectors;
+ *pnum = bytes;
ret = BDRV_BLOCK_DATA;
- } else if (data == start) {
- /* On a data extent, compute sectors to the end of the extent,
+ } else if (data == offset) {
+ /* On a data extent, compute bytes to the end of the extent,
* possibly including a partial sector at EOF. */
- *pnum = MIN(nb_sectors, DIV_ROUND_UP(hole - start, BDRV_SECTOR_SIZE));
+ *pnum = MIN(bytes, hole - offset);
ret = BDRV_BLOCK_DATA;
} else {
- /* On a hole, compute sectors to the beginning of the next extent. */
- assert(hole == start);
- *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
+ /* On a hole, compute bytes to the beginning of the next extent. */
+ assert(hole == offset);
+ *pnum = MIN(bytes, data - offset);
ret = BDRV_BLOCK_ZERO;
}
+ *map = offset;
*file = bs;
- return ret | BDRV_BLOCK_OFFSET_VALID | start;
+ return ret | BDRV_BLOCK_OFFSET_VALID;
}
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
.bdrv_close = qemu_gluster_close,
- .bdrv_create = qemu_gluster_create,
+ .bdrv_co_create = qemu_gluster_co_create,
+ .bdrv_co_create_opts = qemu_gluster_co_create_opts,
.bdrv_getlength = qemu_gluster_getlength,
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
.bdrv_truncate = qemu_gluster_truncate,
#ifdef CONFIG_GLUSTERFS_ZEROFILL
.bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
#endif
- .bdrv_co_get_block_status = qemu_gluster_co_get_block_status,
+ .bdrv_co_block_status = qemu_gluster_co_block_status,
.create_opts = &qemu_gluster_create_opts,
};
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
.bdrv_close = qemu_gluster_close,
- .bdrv_create = qemu_gluster_create,
+ .bdrv_co_create = qemu_gluster_co_create,
+ .bdrv_co_create_opts = qemu_gluster_co_create_opts,
.bdrv_getlength = qemu_gluster_getlength,
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
.bdrv_truncate = qemu_gluster_truncate,
#ifdef CONFIG_GLUSTERFS_ZEROFILL
.bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
#endif
- .bdrv_co_get_block_status = qemu_gluster_co_get_block_status,
+ .bdrv_co_block_status = qemu_gluster_co_block_status,
.create_opts = &qemu_gluster_create_opts,
};
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
.bdrv_close = qemu_gluster_close,
- .bdrv_create = qemu_gluster_create,
+ .bdrv_co_create = qemu_gluster_co_create,
+ .bdrv_co_create_opts = qemu_gluster_co_create_opts,
.bdrv_getlength = qemu_gluster_getlength,
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
.bdrv_truncate = qemu_gluster_truncate,
#ifdef CONFIG_GLUSTERFS_ZEROFILL
.bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
#endif
- .bdrv_co_get_block_status = qemu_gluster_co_get_block_status,
+ .bdrv_co_block_status = qemu_gluster_co_block_status,
.create_opts = &qemu_gluster_create_opts,
};
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
.bdrv_close = qemu_gluster_close,
- .bdrv_create = qemu_gluster_create,
+ .bdrv_co_create = qemu_gluster_co_create,
+ .bdrv_co_create_opts = qemu_gluster_co_create_opts,
.bdrv_getlength = qemu_gluster_getlength,
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
.bdrv_truncate = qemu_gluster_truncate,
#ifdef CONFIG_GLUSTERFS_ZEROFILL
.bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
#endif
- .bdrv_co_get_block_status = qemu_gluster_co_get_block_status,
+ .bdrv_co_block_status = qemu_gluster_co_block_status,
.create_opts = &qemu_gluster_create_opts,
};