#include "qemu/cutils.h"
#include "sysemu/sysemu.h"
#include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qint.h"
#include "qapi/qmp/qstring.h"
#include "qapi-visit.h"
#include "qapi/qobject-input-visitor.h"
int events;
bool has_zero_init;
AioContext *aio_context;
+ QemuMutex mutex;
blkcnt_t st_blocks;
bool cache_used;
NFSServer *server;
error_setg(errp, "Invalid URI specified");
goto out;
}
- if (strcmp(uri->scheme, "nfs") != 0) {
+ if (g_strcmp0(uri->scheme, "nfs") != 0) {
error_setg(errp, "URI scheme must be 'nfs'");
goto out;
}
goto out;
}
- qdict_put(options, "server.host", qstring_from_str(uri->server));
- qdict_put(options, "server.type", qstring_from_str("inet"));
- qdict_put(options, "path", qstring_from_str(uri->path));
+ qdict_put_str(options, "server.host", uri->server);
+ qdict_put_str(options, "server.type", "inet");
+ qdict_put_str(options, "path", uri->path);
for (i = 0; i < qp->n; i++) {
unsigned long long val;
goto out;
}
if (!strcmp(qp->p[i].name, "uid")) {
- qdict_put(options, "user",
- qstring_from_str(qp->p[i].value));
+ qdict_put_str(options, "user", qp->p[i].value);
} else if (!strcmp(qp->p[i].name, "gid")) {
- qdict_put(options, "group",
- qstring_from_str(qp->p[i].value));
+ qdict_put_str(options, "group", qp->p[i].value);
} else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
- qdict_put(options, "tcp-syn-count",
- qstring_from_str(qp->p[i].value));
+ qdict_put_str(options, "tcp-syn-count", qp->p[i].value);
} else if (!strcmp(qp->p[i].name, "readahead")) {
- qdict_put(options, "readahead-size",
- qstring_from_str(qp->p[i].value));
+ qdict_put_str(options, "readahead-size", qp->p[i].value);
} else if (!strcmp(qp->p[i].name, "pagecache")) {
- qdict_put(options, "page-cache-size",
- qstring_from_str(qp->p[i].value));
+ qdict_put_str(options, "page-cache-size", qp->p[i].value);
} else if (!strcmp(qp->p[i].name, "debug")) {
- qdict_put(options, "debug",
- qstring_from_str(qp->p[i].value));
+ qdict_put_str(options, "debug", qp->p[i].value);
} else {
error_setg(errp, "Unknown NFS parameter name: %s",
qp->p[i].name);
static void nfs_process_read(void *arg);
static void nfs_process_write(void *arg);
+/* Called with QemuMutex held. */
static void nfs_set_events(NFSClient *client)
{
int ev = nfs_which_events(client->context);
{
NFSClient *client = arg;
- aio_context_acquire(client->aio_context);
+ qemu_mutex_lock(&client->mutex);
nfs_service(client->context, POLLIN);
nfs_set_events(client);
- aio_context_release(client->aio_context);
+ qemu_mutex_unlock(&client->mutex);
}
static void nfs_process_write(void *arg)
{
NFSClient *client = arg;
- aio_context_acquire(client->aio_context);
+ qemu_mutex_lock(&client->mutex);
nfs_service(client->context, POLLOUT);
nfs_set_events(client);
- aio_context_release(client->aio_context);
+ qemu_mutex_unlock(&client->mutex);
}
static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
aio_co_wake(task->co);
}
+/* Called (via nfs_service) with QemuMutex held. */
static void
nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
void *private_data)
nfs_co_generic_bh_cb, task);
}
-static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
- int64_t sector_num, int nb_sectors,
- QEMUIOVector *iov)
+static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, uint64_t offset,
+ uint64_t bytes, QEMUIOVector *iov,
+ int flags)
{
NFSClient *client = bs->opaque;
NFSRPC task;
nfs_co_init_task(bs, &task);
task.iov = iov;
+ qemu_mutex_lock(&client->mutex);
if (nfs_pread_async(client->context, client->fh,
- sector_num * BDRV_SECTOR_SIZE,
- nb_sectors * BDRV_SECTOR_SIZE,
- nfs_co_generic_cb, &task) != 0) {
+ offset, bytes, nfs_co_generic_cb, &task) != 0) {
+ qemu_mutex_unlock(&client->mutex);
return -ENOMEM;
}
nfs_set_events(client);
+ qemu_mutex_unlock(&client->mutex);
while (!task.complete) {
qemu_coroutine_yield();
}
return 0;
}
-static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
- int64_t sector_num, int nb_sectors,
- QEMUIOVector *iov)
+static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, uint64_t offset,
+ uint64_t bytes, QEMUIOVector *iov,
+ int flags)
{
NFSClient *client = bs->opaque;
NFSRPC task;
char *buf = NULL;
+ bool my_buffer = false;
nfs_co_init_task(bs, &task);
- buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
- if (nb_sectors && buf == NULL) {
- return -ENOMEM;
+ if (iov->niov != 1) {
+ buf = g_try_malloc(bytes);
+ if (bytes && buf == NULL) {
+ return -ENOMEM;
+ }
+ qemu_iovec_to_buf(iov, 0, buf, bytes);
+ my_buffer = true;
+ } else {
+ buf = iov->iov[0].iov_base;
}
- qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
-
+ qemu_mutex_lock(&client->mutex);
if (nfs_pwrite_async(client->context, client->fh,
- sector_num * BDRV_SECTOR_SIZE,
- nb_sectors * BDRV_SECTOR_SIZE,
- buf, nfs_co_generic_cb, &task) != 0) {
- g_free(buf);
+ offset, bytes, buf,
+ nfs_co_generic_cb, &task) != 0) {
+ qemu_mutex_unlock(&client->mutex);
+ if (my_buffer) {
+ g_free(buf);
+ }
return -ENOMEM;
}
nfs_set_events(client);
+ qemu_mutex_unlock(&client->mutex);
while (!task.complete) {
qemu_coroutine_yield();
}
- g_free(buf);
+ if (my_buffer) {
+ g_free(buf);
+ }
- if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
+ if (task.ret != bytes) {
return task.ret < 0 ? task.ret : -EIO;
}
nfs_co_init_task(bs, &task);
+ qemu_mutex_lock(&client->mutex);
if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
&task) != 0) {
+ qemu_mutex_unlock(&client->mutex);
return -ENOMEM;
}
nfs_set_events(client);
+ qemu_mutex_unlock(&client->mutex);
while (!task.complete) {
qemu_coroutine_yield();
}
if (client->context) {
if (client->fh) {
nfs_close(client->context, client->fh);
+ client->fh = NULL;
}
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
false, NULL, NULL, NULL, NULL);
nfs_destroy_context(client->context);
+ client->context = NULL;
}
- memset(client, 0, sizeof(NFSClient));
+ g_free(client->path);
+ qemu_mutex_destroy(&client->mutex);
+ qapi_free_NFSServer(client->server);
+ client->server = NULL;
}
static void nfs_file_close(BlockDriverState *bs)
goto out;
}
- iv = qobject_input_visitor_new(crumpled_addr, true);
+ /*
+ * Caution: this works only because all scalar members of
+ * NFSServer are QString in @crumpled_addr. The visitor expects
+ * @crumpled_addr to be typed according to the QAPI schema. It
+ * is when @options come from -blockdev or blockdev_add. But when
+ * they come from -drive, they're all QString.
+ */
+ iv = qobject_input_visitor_new(crumpled_addr);
visit_type_NFSServer(iv, NULL, &server, &local_error);
if (local_error) {
error_propagate(errp, local_error);
static int64_t nfs_client_open(NFSClient *client, QDict *options,
- int flags, Error **errp, int open_flags)
+ int flags, int open_flags, Error **errp)
{
int ret = -EINVAL;
QemuOpts *opts = NULL;
struct stat st;
char *file = NULL, *strp = NULL;
+ qemu_mutex_init(&client->mutex);
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
qemu_opts_absorb_qdict(opts, options, &local_err);
if (local_err) {
}
client->readahead = qemu_opt_get_number(opts, "readahead-size", 0);
if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
- error_report("NFS Warning: Truncating NFS readahead "
- "size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
+ warn_report("Truncating NFS readahead size to %d",
+ QEMU_NFS_MAX_READAHEAD_SIZE);
client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
}
nfs_set_readahead(client->context, client->readahead);
}
client->pagecache = qemu_opt_get_number(opts, "page-cache-size", 0);
if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
- error_report("NFS Warning: Truncating NFS pagecache "
- "size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
+ warn_report("Truncating NFS pagecache size to %d pages",
+ QEMU_NFS_MAX_PAGECACHE_SIZE);
client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
}
nfs_set_pagecache(client->context, client->pagecache);
/* limit the maximum debug level to avoid potential flooding
* of our log files. */
if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
- error_report("NFS Warning: Limiting NFS debug level "
- "to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
+ warn_report("Limiting NFS debug level to %d",
+ QEMU_NFS_MAX_DEBUG_LEVEL);
client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
}
nfs_set_debug(client->context, client->debug);
ret = nfs_client_open(client, options,
(flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
- errp, bs->open_flags);
+ bs->open_flags, errp);
if (ret < 0) {
return ret;
}
+
bs->total_sectors = ret;
ret = 0;
return ret;
goto out;
}
- ret = nfs_client_open(client, options, O_CREAT, errp, 0);
+ ret = nfs_client_open(client, options, O_CREAT, 0, errp);
if (ret < 0) {
goto out;
}
return client->has_zero_init;
}
+/* Called (via nfs_service) with QemuMutex held. */
static void
nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
void *private_data)
if (task->ret < 0) {
error_report("NFS Error: %s", nfs_get_error(nfs));
}
- task->complete = 1;
+
+ /* Set task->complete before reading bs->wakeup. */
+ atomic_mb_set(&task->complete, 1);
bdrv_wakeup(task->bs);
}
return (task.ret < 0 ? task.ret : st.st_blocks * 512);
}
-static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
+static int nfs_file_truncate(BlockDriverState *bs, int64_t offset,
+ PreallocMode prealloc, Error **errp)
{
NFSClient *client = bs->opaque;
- return nfs_ftruncate(client->context, client->fh, offset);
+ int ret;
+
+ if (prealloc != PREALLOC_MODE_OFF) {
+ error_setg(errp, "Unsupported preallocation mode '%s'",
+ PreallocMode_str(prealloc));
+ return -ENOTSUP;
+ }
+
+ ret = nfs_ftruncate(client->context, client->fh, offset);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Failed to truncate file");
+ return ret;
+ }
+
+ return 0;
}
/* Note that this will not re-establish a connection with the NFS server
QObject *server_qdict;
Visitor *ov;
- qdict_put(opts, "driver", qstring_from_str("nfs"));
+ qdict_put_str(opts, "driver", "nfs");
if (client->uid && !client->gid) {
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
ov = qobject_output_visitor_new(&server_qdict);
visit_type_NFSServer(ov, NULL, &client->server, &error_abort);
visit_complete(ov, &server_qdict);
- assert(qobject_type(server_qdict) == QTYPE_QDICT);
-
qdict_put_obj(opts, "server", server_qdict);
- qdict_put(opts, "path", qstring_from_str(client->path));
+ qdict_put_str(opts, "path", client->path);
if (client->uid) {
- qdict_put(opts, "user", qint_from_int(client->uid));
+ qdict_put_int(opts, "user", client->uid);
}
if (client->gid) {
- qdict_put(opts, "group", qint_from_int(client->gid));
+ qdict_put_int(opts, "group", client->gid);
}
if (client->tcp_syncnt) {
- qdict_put(opts, "tcp-syn-cnt",
- qint_from_int(client->tcp_syncnt));
+ qdict_put_int(opts, "tcp-syn-cnt", client->tcp_syncnt);
}
if (client->readahead) {
- qdict_put(opts, "readahead-size",
- qint_from_int(client->readahead));
+ qdict_put_int(opts, "readahead-size", client->readahead);
}
if (client->pagecache) {
- qdict_put(opts, "page-cache-size",
- qint_from_int(client->pagecache));
+ qdict_put_int(opts, "page-cache-size", client->pagecache);
}
if (client->debug) {
- qdict_put(opts, "debug", qint_from_int(client->debug));
+ qdict_put_int(opts, "debug", client->debug);
}
visit_free(ov);
.bdrv_create = nfs_file_create,
.bdrv_reopen_prepare = nfs_reopen_prepare,
- .bdrv_co_readv = nfs_co_readv,
- .bdrv_co_writev = nfs_co_writev,
+ .bdrv_co_preadv = nfs_co_preadv,
+ .bdrv_co_pwritev = nfs_co_pwritev,
.bdrv_co_flush_to_disk = nfs_co_flush,
.bdrv_detach_aio_context = nfs_detach_aio_context,