2 * QEMU Block driver for native access to files on NFS shares
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
28 #include "qemu/config-file.h"
29 #include "qemu/error-report.h"
30 #include "qapi/error.h"
31 #include "block/block_int.h"
32 #include "block/qdict.h"
35 #include "qemu/main-loop.h"
36 #include "qemu/module.h"
37 #include "qemu/option.h"
39 #include "qemu/cutils.h"
40 #include "sysemu/sysemu.h"
41 #include "qapi/qapi-visit-block-core.h"
42 #include "qapi/qmp/qdict.h"
43 #include "qapi/qmp/qstring.h"
44 #include "qapi/qobject-input-visitor.h"
45 #include "qapi/qobject-output-visitor.h"
46 #include <nfsc/libnfs.h>
49 #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
50 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
51 #define QEMU_NFS_MAX_DEBUG_LEVEL 2
53 typedef struct NFSClient {
54 struct nfs_context *context;
58 AioContext *aio_context;
64 int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
67 typedef struct NFSRPC {
77 static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
80 QueryParams *qp = NULL;
83 uri = uri_parse(filename);
85 error_setg(errp, "Invalid URI specified");
88 if (g_strcmp0(uri->scheme, "nfs") != 0) {
89 error_setg(errp, "URI scheme must be 'nfs'");
94 error_setg(errp, "missing hostname in URI");
99 error_setg(errp, "missing file path in URI");
103 qp = query_params_parse(uri->query);
105 error_setg(errp, "could not parse query parameters");
109 qdict_put_str(options, "server.host", uri->server);
110 qdict_put_str(options, "server.type", "inet");
111 qdict_put_str(options, "path", uri->path);
113 for (i = 0; i < qp->n; i++) {
114 unsigned long long val;
115 if (!qp->p[i].value) {
116 error_setg(errp, "Value for NFS parameter expected: %s",
120 if (parse_uint_full(qp->p[i].value, &val, 0)) {
121 error_setg(errp, "Illegal value for NFS parameter: %s",
125 if (!strcmp(qp->p[i].name, "uid")) {
126 qdict_put_str(options, "user", qp->p[i].value);
127 } else if (!strcmp(qp->p[i].name, "gid")) {
128 qdict_put_str(options, "group", qp->p[i].value);
129 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
130 qdict_put_str(options, "tcp-syn-count", qp->p[i].value);
131 } else if (!strcmp(qp->p[i].name, "readahead")) {
132 qdict_put_str(options, "readahead-size", qp->p[i].value);
133 } else if (!strcmp(qp->p[i].name, "pagecache")) {
134 qdict_put_str(options, "page-cache-size", qp->p[i].value);
135 } else if (!strcmp(qp->p[i].name, "debug")) {
136 qdict_put_str(options, "debug", qp->p[i].value);
138 error_setg(errp, "Unknown NFS parameter name: %s",
146 query_params_free(qp);
154 static bool nfs_has_filename_options_conflict(QDict *options, Error **errp)
156 const QDictEntry *qe;
158 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
159 if (!strcmp(qe->key, "host") ||
160 !strcmp(qe->key, "path") ||
161 !strcmp(qe->key, "user") ||
162 !strcmp(qe->key, "group") ||
163 !strcmp(qe->key, "tcp-syn-count") ||
164 !strcmp(qe->key, "readahead-size") ||
165 !strcmp(qe->key, "page-cache-size") ||
166 !strcmp(qe->key, "debug") ||
167 strstart(qe->key, "server.", NULL))
169 error_setg(errp, "Option %s cannot be used with a filename",
178 static void nfs_parse_filename(const char *filename, QDict *options,
181 if (nfs_has_filename_options_conflict(options, errp)) {
185 nfs_parse_uri(filename, options, errp);
188 static void nfs_process_read(void *arg);
189 static void nfs_process_write(void *arg);
191 /* Called with QemuMutex held. */
192 static void nfs_set_events(NFSClient *client)
194 int ev = nfs_which_events(client->context);
195 if (ev != client->events) {
196 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
198 (ev & POLLIN) ? nfs_process_read : NULL,
199 (ev & POLLOUT) ? nfs_process_write : NULL,
206 static void nfs_process_read(void *arg)
208 NFSClient *client = arg;
210 qemu_mutex_lock(&client->mutex);
211 nfs_service(client->context, POLLIN);
212 nfs_set_events(client);
213 qemu_mutex_unlock(&client->mutex);
216 static void nfs_process_write(void *arg)
218 NFSClient *client = arg;
220 qemu_mutex_lock(&client->mutex);
221 nfs_service(client->context, POLLOUT);
222 nfs_set_events(client);
223 qemu_mutex_unlock(&client->mutex);
226 static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
229 .co = qemu_coroutine_self(),
231 .client = bs->opaque,
235 static void nfs_co_generic_bh_cb(void *opaque)
237 NFSRPC *task = opaque;
240 aio_co_wake(task->co);
243 /* Called (via nfs_service) with QemuMutex held. */
245 nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
248 NFSRPC *task = private_data;
251 if (task->ret > 0 && task->iov) {
252 if (task->ret <= task->iov->size) {
253 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
259 error_report("NFS Error: %s", nfs_get_error(nfs));
261 aio_bh_schedule_oneshot(task->client->aio_context,
262 nfs_co_generic_bh_cb, task);
265 static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, uint64_t offset,
266 uint64_t bytes, QEMUIOVector *iov,
269 NFSClient *client = bs->opaque;
272 nfs_co_init_task(bs, &task);
275 qemu_mutex_lock(&client->mutex);
276 if (nfs_pread_async(client->context, client->fh,
277 offset, bytes, nfs_co_generic_cb, &task) != 0) {
278 qemu_mutex_unlock(&client->mutex);
282 nfs_set_events(client);
283 qemu_mutex_unlock(&client->mutex);
284 while (!task.complete) {
285 qemu_coroutine_yield();
292 /* zero pad short reads */
293 if (task.ret < iov->size) {
294 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
300 static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, uint64_t offset,
301 uint64_t bytes, QEMUIOVector *iov,
304 NFSClient *client = bs->opaque;
307 bool my_buffer = false;
309 nfs_co_init_task(bs, &task);
311 if (iov->niov != 1) {
312 buf = g_try_malloc(bytes);
313 if (bytes && buf == NULL) {
316 qemu_iovec_to_buf(iov, 0, buf, bytes);
319 buf = iov->iov[0].iov_base;
322 qemu_mutex_lock(&client->mutex);
323 if (nfs_pwrite_async(client->context, client->fh,
325 nfs_co_generic_cb, &task) != 0) {
326 qemu_mutex_unlock(&client->mutex);
333 nfs_set_events(client);
334 qemu_mutex_unlock(&client->mutex);
335 while (!task.complete) {
336 qemu_coroutine_yield();
343 if (task.ret != bytes) {
344 return task.ret < 0 ? task.ret : -EIO;
350 static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
352 NFSClient *client = bs->opaque;
355 nfs_co_init_task(bs, &task);
357 qemu_mutex_lock(&client->mutex);
358 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
360 qemu_mutex_unlock(&client->mutex);
364 nfs_set_events(client);
365 qemu_mutex_unlock(&client->mutex);
366 while (!task.complete) {
367 qemu_coroutine_yield();
373 static void nfs_detach_aio_context(BlockDriverState *bs)
375 NFSClient *client = bs->opaque;
377 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
378 false, NULL, NULL, NULL, NULL);
382 static void nfs_attach_aio_context(BlockDriverState *bs,
383 AioContext *new_context)
385 NFSClient *client = bs->opaque;
387 client->aio_context = new_context;
388 nfs_set_events(client);
391 static void nfs_client_close(NFSClient *client)
393 if (client->context) {
395 nfs_close(client->context, client->fh);
398 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
399 false, NULL, NULL, NULL, NULL);
400 nfs_destroy_context(client->context);
401 client->context = NULL;
403 g_free(client->path);
404 qemu_mutex_destroy(&client->mutex);
405 qapi_free_NFSServer(client->server);
406 client->server = NULL;
409 static void nfs_file_close(BlockDriverState *bs)
411 NFSClient *client = bs->opaque;
412 nfs_client_close(client);
415 static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts,
416 int flags, int open_flags, Error **errp)
418 int64_t ret = -EINVAL;
420 char *file = NULL, *strp = NULL;
422 qemu_mutex_init(&client->mutex);
424 client->path = g_strdup(opts->path);
426 strp = strrchr(client->path, '/');
428 error_setg(errp, "Invalid URL specified");
431 file = g_strdup(strp);
434 /* Steal the NFSServer object from opts; set the original pointer to NULL
435 * to avoid use after free and double free. */
436 client->server = opts->server;
439 client->context = nfs_init_context();
440 if (client->context == NULL) {
441 error_setg(errp, "Failed to init NFS context");
445 if (opts->has_user) {
446 client->uid = opts->user;
447 nfs_set_uid(client->context, client->uid);
450 if (opts->has_group) {
451 client->gid = opts->group;
452 nfs_set_gid(client->context, client->gid);
455 if (opts->has_tcp_syn_count) {
456 client->tcp_syncnt = opts->tcp_syn_count;
457 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
460 #ifdef LIBNFS_FEATURE_READAHEAD
461 if (opts->has_readahead_size) {
462 if (open_flags & BDRV_O_NOCACHE) {
463 error_setg(errp, "Cannot enable NFS readahead "
464 "if cache.direct = on");
467 client->readahead = opts->readahead_size;
468 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
469 warn_report("Truncating NFS readahead size to %d",
470 QEMU_NFS_MAX_READAHEAD_SIZE);
471 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
473 nfs_set_readahead(client->context, client->readahead);
474 #ifdef LIBNFS_FEATURE_PAGECACHE
475 nfs_set_pagecache_ttl(client->context, 0);
477 client->cache_used = true;
481 #ifdef LIBNFS_FEATURE_PAGECACHE
482 if (opts->has_page_cache_size) {
483 if (open_flags & BDRV_O_NOCACHE) {
484 error_setg(errp, "Cannot enable NFS pagecache "
485 "if cache.direct = on");
488 client->pagecache = opts->page_cache_size;
489 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
490 warn_report("Truncating NFS pagecache size to %d pages",
491 QEMU_NFS_MAX_PAGECACHE_SIZE);
492 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
494 nfs_set_pagecache(client->context, client->pagecache);
495 nfs_set_pagecache_ttl(client->context, 0);
496 client->cache_used = true;
500 #ifdef LIBNFS_FEATURE_DEBUG
501 if (opts->has_debug) {
502 client->debug = opts->debug;
503 /* limit the maximum debug level to avoid potential flooding
504 * of our log files. */
505 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
506 warn_report("Limiting NFS debug level to %d",
507 QEMU_NFS_MAX_DEBUG_LEVEL);
508 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
510 nfs_set_debug(client->context, client->debug);
514 ret = nfs_mount(client->context, client->server->host, client->path);
516 error_setg(errp, "Failed to mount nfs share: %s",
517 nfs_get_error(client->context));
521 if (flags & O_CREAT) {
522 ret = nfs_creat(client->context, file, 0600, &client->fh);
524 error_setg(errp, "Failed to create file: %s",
525 nfs_get_error(client->context));
529 ret = nfs_open(client->context, file, flags, &client->fh);
531 error_setg(errp, "Failed to open file : %s",
532 nfs_get_error(client->context));
537 ret = nfs_fstat(client->context, client->fh, &st);
539 error_setg(errp, "Failed to fstat file: %s",
540 nfs_get_error(client->context));
544 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
545 client->st_blocks = st.st_blocks;
546 client->has_zero_init = S_ISREG(st.st_mode);
551 nfs_client_close(client);
557 static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options,
560 BlockdevOptionsNfs *opts = NULL;
563 Error *local_err = NULL;
565 v = qobject_input_visitor_new_flat_confused(options, errp);
570 visit_type_BlockdevOptionsNfs(v, NULL, &opts, &local_err);
574 error_propagate(errp, local_err);
578 /* Remove the processed options from the QDict (the visitor processes
579 * _all_ options in the QDict) */
580 while ((e = qdict_first(options))) {
581 qdict_del(options, e->key);
587 static int64_t nfs_client_open_qdict(NFSClient *client, QDict *options,
588 int flags, int open_flags, Error **errp)
590 BlockdevOptionsNfs *opts;
593 opts = nfs_options_qdict_to_qapi(options, errp);
599 ret = nfs_client_open(client, opts, flags, open_flags, errp);
601 qapi_free_BlockdevOptionsNfs(opts);
605 static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
607 NFSClient *client = bs->opaque;
610 client->aio_context = bdrv_get_aio_context(bs);
612 ret = nfs_client_open_qdict(client, options,
613 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
614 bs->open_flags, errp);
619 bs->total_sectors = ret;
624 static QemuOptsList nfs_create_opts = {
625 .name = "nfs-create-opts",
626 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
629 .name = BLOCK_OPT_SIZE,
630 .type = QEMU_OPT_SIZE,
631 .help = "Virtual disk size"
633 { /* end of list */ }
637 static int nfs_file_co_create(BlockdevCreateOptions *options, Error **errp)
639 BlockdevCreateOptionsNfs *opts = &options->u.nfs;
640 NFSClient *client = g_new0(NFSClient, 1);
643 assert(options->driver == BLOCKDEV_DRIVER_NFS);
645 client->aio_context = qemu_get_aio_context();
647 ret = nfs_client_open(client, opts->location, O_CREAT, 0, errp);
651 ret = nfs_ftruncate(client->context, client->fh, opts->size);
652 nfs_client_close(client);
659 static int coroutine_fn nfs_file_co_create_opts(const char *url, QemuOpts *opts,
662 BlockdevCreateOptions *create_options;
663 BlockdevCreateOptionsNfs *nfs_opts;
667 create_options = g_new0(BlockdevCreateOptions, 1);
668 create_options->driver = BLOCKDEV_DRIVER_NFS;
669 nfs_opts = &create_options->u.nfs;
671 /* Read out options */
672 nfs_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
675 options = qdict_new();
676 ret = nfs_parse_uri(url, options, errp);
681 nfs_opts->location = nfs_options_qdict_to_qapi(options, errp);
682 if (nfs_opts->location == NULL) {
687 ret = nfs_file_co_create(create_options, errp);
694 qobject_unref(options);
695 qapi_free_BlockdevCreateOptions(create_options);
699 static int nfs_has_zero_init(BlockDriverState *bs)
701 NFSClient *client = bs->opaque;
702 return client->has_zero_init;
705 /* Called (via nfs_service) with QemuMutex held. */
707 nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
710 NFSRPC *task = private_data;
712 if (task->ret == 0) {
713 memcpy(task->st, data, sizeof(struct stat));
716 error_report("NFS Error: %s", nfs_get_error(nfs));
719 /* Set task->complete before reading bs->wakeup. */
720 atomic_mb_set(&task->complete, 1);
721 bdrv_wakeup(task->bs);
724 static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
726 NFSClient *client = bs->opaque;
730 if (bdrv_is_read_only(bs) &&
731 !(bs->open_flags & BDRV_O_NOCACHE)) {
732 return client->st_blocks * 512;
737 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
742 nfs_set_events(client);
743 BDRV_POLL_WHILE(bs, !task.complete);
745 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
748 static int coroutine_fn
749 nfs_file_co_truncate(BlockDriverState *bs, int64_t offset,
750 PreallocMode prealloc, Error **errp)
752 NFSClient *client = bs->opaque;
755 if (prealloc != PREALLOC_MODE_OFF) {
756 error_setg(errp, "Unsupported preallocation mode '%s'",
757 PreallocMode_str(prealloc));
761 ret = nfs_ftruncate(client->context, client->fh, offset);
763 error_setg_errno(errp, -ret, "Failed to truncate file");
770 /* Note that this will not re-establish a connection with the NFS server
771 * - it is effectively a NOP. */
772 static int nfs_reopen_prepare(BDRVReopenState *state,
773 BlockReopenQueue *queue, Error **errp)
775 NFSClient *client = state->bs->opaque;
779 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
780 error_setg(errp, "Cannot open a read-only mount as read-write");
784 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
785 error_setg(errp, "Cannot disable cache if libnfs readahead or"
786 " pagecache is enabled");
790 /* Update cache for read-only reopens */
791 if (!(state->flags & BDRV_O_RDWR)) {
792 ret = nfs_fstat(client->context, client->fh, &st);
794 error_setg(errp, "Failed to fstat file: %s",
795 nfs_get_error(client->context));
798 client->st_blocks = st.st_blocks;
804 static void nfs_refresh_filename(BlockDriverState *bs)
806 NFSClient *client = bs->opaque;
808 if (client->uid && !client->gid) {
809 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
810 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
812 } else if (!client->uid && client->gid) {
813 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
814 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
816 } else if (client->uid && client->gid) {
817 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
818 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
819 client->server->host, client->path, client->uid, client->gid);
821 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
822 "nfs://%s%s", client->server->host, client->path);
826 static char *nfs_dirname(BlockDriverState *bs, Error **errp)
828 NFSClient *client = bs->opaque;
830 if (client->uid || client->gid) {
831 bdrv_refresh_filename(bs);
832 error_setg(errp, "Cannot generate a base directory for NFS node '%s'",
837 return g_strdup_printf("nfs://%s%s/", client->server->host, client->path);
840 #ifdef LIBNFS_FEATURE_PAGECACHE
841 static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs,
844 NFSClient *client = bs->opaque;
845 nfs_pagecache_invalidate(client->context, client->fh);
849 static const char *nfs_strong_runtime_opts[] = {
858 static BlockDriver bdrv_nfs = {
859 .format_name = "nfs",
860 .protocol_name = "nfs",
862 .instance_size = sizeof(NFSClient),
863 .bdrv_parse_filename = nfs_parse_filename,
864 .create_opts = &nfs_create_opts,
866 .bdrv_has_zero_init = nfs_has_zero_init,
867 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
868 .bdrv_co_truncate = nfs_file_co_truncate,
870 .bdrv_file_open = nfs_file_open,
871 .bdrv_close = nfs_file_close,
872 .bdrv_co_create = nfs_file_co_create,
873 .bdrv_co_create_opts = nfs_file_co_create_opts,
874 .bdrv_reopen_prepare = nfs_reopen_prepare,
876 .bdrv_co_preadv = nfs_co_preadv,
877 .bdrv_co_pwritev = nfs_co_pwritev,
878 .bdrv_co_flush_to_disk = nfs_co_flush,
880 .bdrv_detach_aio_context = nfs_detach_aio_context,
881 .bdrv_attach_aio_context = nfs_attach_aio_context,
882 .bdrv_refresh_filename = nfs_refresh_filename,
883 .bdrv_dirname = nfs_dirname,
885 .strong_runtime_opts = nfs_strong_runtime_opts,
887 #ifdef LIBNFS_FEATURE_PAGECACHE
888 .bdrv_co_invalidate_cache = nfs_co_invalidate_cache,
892 static void nfs_block_init(void)
894 bdrv_register(&bdrv_nfs);
897 block_init(nfs_block_init);