target/s390x: Fix typo
[qemu/ar7.git] / block / nfs.c
blobffb54be0651384ef9854f4a0e8a6376d85433fbf
1 /*
2 * QEMU Block driver for native access to files on NFS shares
4 * Copyright (c) 2014-2016 Peter Lieven <pl@kamp.de>
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
27 #include <poll.h>
28 #include "qemu-common.h"
29 #include "qemu/config-file.h"
30 #include "qemu/error-report.h"
31 #include "qapi/error.h"
32 #include "block/block_int.h"
33 #include "trace.h"
34 #include "qemu/iov.h"
35 #include "qemu/uri.h"
36 #include "qemu/cutils.h"
37 #include "sysemu/sysemu.h"
38 #include "qapi/qmp/qdict.h"
39 #include "qapi/qmp/qint.h"
40 #include "qapi/qmp/qstring.h"
41 #include "qapi-visit.h"
42 #include "qapi/qobject-input-visitor.h"
43 #include "qapi/qobject-output-visitor.h"
44 #include <nfsc/libnfs.h>
47 #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
48 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
49 #define QEMU_NFS_MAX_DEBUG_LEVEL 2
51 typedef struct NFSClient {
52 struct nfs_context *context;
53 struct nfsfh *fh;
54 int events;
55 bool has_zero_init;
56 AioContext *aio_context;
57 blkcnt_t st_blocks;
58 bool cache_used;
59 NFSServer *server;
60 char *path;
61 int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
62 } NFSClient;
64 typedef struct NFSRPC {
65 BlockDriverState *bs;
66 int ret;
67 int complete;
68 QEMUIOVector *iov;
69 struct stat *st;
70 Coroutine *co;
71 NFSClient *client;
72 } NFSRPC;
74 static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
76 URI *uri = NULL;
77 QueryParams *qp = NULL;
78 int ret = -EINVAL, i;
80 uri = uri_parse(filename);
81 if (!uri) {
82 error_setg(errp, "Invalid URI specified");
83 goto out;
85 if (strcmp(uri->scheme, "nfs") != 0) {
86 error_setg(errp, "URI scheme must be 'nfs'");
87 goto out;
90 if (!uri->server) {
91 error_setg(errp, "missing hostname in URI");
92 goto out;
95 if (!uri->path) {
96 error_setg(errp, "missing file path in URI");
97 goto out;
100 qp = query_params_parse(uri->query);
101 if (!qp) {
102 error_setg(errp, "could not parse query parameters");
103 goto out;
106 qdict_put(options, "server.host", qstring_from_str(uri->server));
107 qdict_put(options, "server.type", qstring_from_str("inet"));
108 qdict_put(options, "path", qstring_from_str(uri->path));
110 for (i = 0; i < qp->n; i++) {
111 unsigned long long val;
112 if (!qp->p[i].value) {
113 error_setg(errp, "Value for NFS parameter expected: %s",
114 qp->p[i].name);
115 goto out;
117 if (parse_uint_full(qp->p[i].value, &val, 0)) {
118 error_setg(errp, "Illegal value for NFS parameter: %s",
119 qp->p[i].name);
120 goto out;
122 if (!strcmp(qp->p[i].name, "uid")) {
123 qdict_put(options, "user",
124 qstring_from_str(qp->p[i].value));
125 } else if (!strcmp(qp->p[i].name, "gid")) {
126 qdict_put(options, "group",
127 qstring_from_str(qp->p[i].value));
128 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
129 qdict_put(options, "tcp-syn-count",
130 qstring_from_str(qp->p[i].value));
131 } else if (!strcmp(qp->p[i].name, "readahead")) {
132 qdict_put(options, "readahead-size",
133 qstring_from_str(qp->p[i].value));
134 } else if (!strcmp(qp->p[i].name, "pagecache")) {
135 qdict_put(options, "page-cache-size",
136 qstring_from_str(qp->p[i].value));
137 } else if (!strcmp(qp->p[i].name, "debug")) {
138 qdict_put(options, "debug",
139 qstring_from_str(qp->p[i].value));
140 } else {
141 error_setg(errp, "Unknown NFS parameter name: %s",
142 qp->p[i].name);
143 goto out;
146 ret = 0;
147 out:
148 if (qp) {
149 query_params_free(qp);
151 if (uri) {
152 uri_free(uri);
154 return ret;
157 static bool nfs_has_filename_options_conflict(QDict *options, Error **errp)
159 const QDictEntry *qe;
161 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
162 if (!strcmp(qe->key, "host") ||
163 !strcmp(qe->key, "path") ||
164 !strcmp(qe->key, "user") ||
165 !strcmp(qe->key, "group") ||
166 !strcmp(qe->key, "tcp-syn-count") ||
167 !strcmp(qe->key, "readahead-size") ||
168 !strcmp(qe->key, "page-cache-size") ||
169 !strcmp(qe->key, "debug") ||
170 strstart(qe->key, "server.", NULL))
172 error_setg(errp, "Option %s cannot be used with a filename",
173 qe->key);
174 return true;
178 return false;
181 static void nfs_parse_filename(const char *filename, QDict *options,
182 Error **errp)
184 if (nfs_has_filename_options_conflict(options, errp)) {
185 return;
188 nfs_parse_uri(filename, options, errp);
191 static void nfs_process_read(void *arg);
192 static void nfs_process_write(void *arg);
194 static void nfs_set_events(NFSClient *client)
196 int ev = nfs_which_events(client->context);
197 if (ev != client->events) {
198 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
199 false,
200 (ev & POLLIN) ? nfs_process_read : NULL,
201 (ev & POLLOUT) ? nfs_process_write : NULL,
202 NULL, client);
205 client->events = ev;
208 static void nfs_process_read(void *arg)
210 NFSClient *client = arg;
212 aio_context_acquire(client->aio_context);
213 nfs_service(client->context, POLLIN);
214 nfs_set_events(client);
215 aio_context_release(client->aio_context);
218 static void nfs_process_write(void *arg)
220 NFSClient *client = arg;
222 aio_context_acquire(client->aio_context);
223 nfs_service(client->context, POLLOUT);
224 nfs_set_events(client);
225 aio_context_release(client->aio_context);
228 static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
230 *task = (NFSRPC) {
231 .co = qemu_coroutine_self(),
232 .bs = bs,
233 .client = bs->opaque,
237 static void nfs_co_generic_bh_cb(void *opaque)
239 NFSRPC *task = opaque;
241 task->complete = 1;
242 aio_co_wake(task->co);
245 static void
246 nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
247 void *private_data)
249 NFSRPC *task = private_data;
250 task->ret = ret;
251 assert(!task->st);
252 if (task->ret > 0 && task->iov) {
253 if (task->ret <= task->iov->size) {
254 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
255 } else {
256 task->ret = -EIO;
259 if (task->ret < 0) {
260 error_report("NFS Error: %s", nfs_get_error(nfs));
262 aio_bh_schedule_oneshot(task->client->aio_context,
263 nfs_co_generic_bh_cb, task);
266 static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, uint64_t offset,
267 uint64_t bytes, QEMUIOVector *iov,
268 int flags)
270 NFSClient *client = bs->opaque;
271 NFSRPC task;
273 nfs_co_init_task(bs, &task);
274 task.iov = iov;
276 if (nfs_pread_async(client->context, client->fh,
277 offset, bytes, nfs_co_generic_cb, &task) != 0) {
278 return -ENOMEM;
281 nfs_set_events(client);
282 while (!task.complete) {
283 qemu_coroutine_yield();
286 if (task.ret < 0) {
287 return task.ret;
290 /* zero pad short reads */
291 if (task.ret < iov->size) {
292 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
295 return 0;
298 static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, uint64_t offset,
299 uint64_t bytes, QEMUIOVector *iov,
300 int flags)
302 NFSClient *client = bs->opaque;
303 NFSRPC task;
304 char *buf = NULL;
305 bool my_buffer = false;
307 nfs_co_init_task(bs, &task);
309 if (iov->niov != 1) {
310 buf = g_try_malloc(bytes);
311 if (bytes && buf == NULL) {
312 return -ENOMEM;
314 qemu_iovec_to_buf(iov, 0, buf, bytes);
315 my_buffer = true;
316 } else {
317 buf = iov->iov[0].iov_base;
320 if (nfs_pwrite_async(client->context, client->fh,
321 offset, bytes, buf,
322 nfs_co_generic_cb, &task) != 0) {
323 if (my_buffer) {
324 g_free(buf);
326 return -ENOMEM;
329 nfs_set_events(client);
330 while (!task.complete) {
331 qemu_coroutine_yield();
334 if (my_buffer) {
335 g_free(buf);
338 if (task.ret != bytes) {
339 return task.ret < 0 ? task.ret : -EIO;
342 return 0;
345 static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
347 NFSClient *client = bs->opaque;
348 NFSRPC task;
350 nfs_co_init_task(bs, &task);
352 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
353 &task) != 0) {
354 return -ENOMEM;
357 nfs_set_events(client);
358 while (!task.complete) {
359 qemu_coroutine_yield();
362 return task.ret;
365 static QemuOptsList runtime_opts = {
366 .name = "nfs",
367 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
368 .desc = {
370 .name = "path",
371 .type = QEMU_OPT_STRING,
372 .help = "Path of the image on the host",
375 .name = "user",
376 .type = QEMU_OPT_NUMBER,
377 .help = "UID value to use when talking to the server",
380 .name = "group",
381 .type = QEMU_OPT_NUMBER,
382 .help = "GID value to use when talking to the server",
385 .name = "tcp-syn-count",
386 .type = QEMU_OPT_NUMBER,
387 .help = "Number of SYNs to send during the session establish",
390 .name = "readahead-size",
391 .type = QEMU_OPT_NUMBER,
392 .help = "Set the readahead size in bytes",
395 .name = "page-cache-size",
396 .type = QEMU_OPT_NUMBER,
397 .help = "Set the pagecache size in bytes",
400 .name = "debug",
401 .type = QEMU_OPT_NUMBER,
402 .help = "Set the NFS debug level (max 2)",
404 { /* end of list */ }
408 static void nfs_detach_aio_context(BlockDriverState *bs)
410 NFSClient *client = bs->opaque;
412 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
413 false, NULL, NULL, NULL, NULL);
414 client->events = 0;
417 static void nfs_attach_aio_context(BlockDriverState *bs,
418 AioContext *new_context)
420 NFSClient *client = bs->opaque;
422 client->aio_context = new_context;
423 nfs_set_events(client);
426 static void nfs_client_close(NFSClient *client)
428 if (client->context) {
429 if (client->fh) {
430 nfs_close(client->context, client->fh);
432 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
433 false, NULL, NULL, NULL, NULL);
434 nfs_destroy_context(client->context);
436 memset(client, 0, sizeof(NFSClient));
439 static void nfs_file_close(BlockDriverState *bs)
441 NFSClient *client = bs->opaque;
442 nfs_client_close(client);
445 static NFSServer *nfs_config(QDict *options, Error **errp)
447 NFSServer *server = NULL;
448 QDict *addr = NULL;
449 QObject *crumpled_addr = NULL;
450 Visitor *iv = NULL;
451 Error *local_error = NULL;
453 qdict_extract_subqdict(options, &addr, "server.");
454 if (!qdict_size(addr)) {
455 error_setg(errp, "NFS server address missing");
456 goto out;
459 crumpled_addr = qdict_crumple(addr, errp);
460 if (!crumpled_addr) {
461 goto out;
464 iv = qobject_input_visitor_new(crumpled_addr, true);
465 visit_type_NFSServer(iv, NULL, &server, &local_error);
466 if (local_error) {
467 error_propagate(errp, local_error);
468 goto out;
471 out:
472 QDECREF(addr);
473 qobject_decref(crumpled_addr);
474 visit_free(iv);
475 return server;
479 static int64_t nfs_client_open(NFSClient *client, QDict *options,
480 int flags, Error **errp, int open_flags)
482 int ret = -EINVAL;
483 QemuOpts *opts = NULL;
484 Error *local_err = NULL;
485 struct stat st;
486 char *file = NULL, *strp = NULL;
488 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
489 qemu_opts_absorb_qdict(opts, options, &local_err);
490 if (local_err) {
491 error_propagate(errp, local_err);
492 ret = -EINVAL;
493 goto fail;
496 client->path = g_strdup(qemu_opt_get(opts, "path"));
497 if (!client->path) {
498 ret = -EINVAL;
499 error_setg(errp, "No path was specified");
500 goto fail;
503 strp = strrchr(client->path, '/');
504 if (strp == NULL) {
505 error_setg(errp, "Invalid URL specified");
506 goto fail;
508 file = g_strdup(strp);
509 *strp = 0;
511 /* Pop the config into our state object, Exit if invalid */
512 client->server = nfs_config(options, errp);
513 if (!client->server) {
514 ret = -EINVAL;
515 goto fail;
518 client->context = nfs_init_context();
519 if (client->context == NULL) {
520 error_setg(errp, "Failed to init NFS context");
521 goto fail;
524 if (qemu_opt_get(opts, "user")) {
525 client->uid = qemu_opt_get_number(opts, "user", 0);
526 nfs_set_uid(client->context, client->uid);
529 if (qemu_opt_get(opts, "group")) {
530 client->gid = qemu_opt_get_number(opts, "group", 0);
531 nfs_set_gid(client->context, client->gid);
534 if (qemu_opt_get(opts, "tcp-syn-count")) {
535 client->tcp_syncnt = qemu_opt_get_number(opts, "tcp-syn-count", 0);
536 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
539 #ifdef LIBNFS_FEATURE_READAHEAD
540 if (qemu_opt_get(opts, "readahead-size")) {
541 if (open_flags & BDRV_O_NOCACHE) {
542 error_setg(errp, "Cannot enable NFS readahead "
543 "if cache.direct = on");
544 goto fail;
546 client->readahead = qemu_opt_get_number(opts, "readahead-size", 0);
547 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
548 error_report("NFS Warning: Truncating NFS readahead "
549 "size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
550 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
552 nfs_set_readahead(client->context, client->readahead);
553 #ifdef LIBNFS_FEATURE_PAGECACHE
554 nfs_set_pagecache_ttl(client->context, 0);
555 #endif
556 client->cache_used = true;
558 #endif
560 #ifdef LIBNFS_FEATURE_PAGECACHE
561 if (qemu_opt_get(opts, "page-cache-size")) {
562 if (open_flags & BDRV_O_NOCACHE) {
563 error_setg(errp, "Cannot enable NFS pagecache "
564 "if cache.direct = on");
565 goto fail;
567 client->pagecache = qemu_opt_get_number(opts, "page-cache-size", 0);
568 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
569 error_report("NFS Warning: Truncating NFS pagecache "
570 "size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
571 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
573 nfs_set_pagecache(client->context, client->pagecache);
574 nfs_set_pagecache_ttl(client->context, 0);
575 client->cache_used = true;
577 #endif
579 #ifdef LIBNFS_FEATURE_DEBUG
580 if (qemu_opt_get(opts, "debug")) {
581 client->debug = qemu_opt_get_number(opts, "debug", 0);
582 /* limit the maximum debug level to avoid potential flooding
583 * of our log files. */
584 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
585 error_report("NFS Warning: Limiting NFS debug level "
586 "to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
587 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
589 nfs_set_debug(client->context, client->debug);
591 #endif
593 ret = nfs_mount(client->context, client->server->host, client->path);
594 if (ret < 0) {
595 error_setg(errp, "Failed to mount nfs share: %s",
596 nfs_get_error(client->context));
597 goto fail;
600 if (flags & O_CREAT) {
601 ret = nfs_creat(client->context, file, 0600, &client->fh);
602 if (ret < 0) {
603 error_setg(errp, "Failed to create file: %s",
604 nfs_get_error(client->context));
605 goto fail;
607 } else {
608 ret = nfs_open(client->context, file, flags, &client->fh);
609 if (ret < 0) {
610 error_setg(errp, "Failed to open file : %s",
611 nfs_get_error(client->context));
612 goto fail;
616 ret = nfs_fstat(client->context, client->fh, &st);
617 if (ret < 0) {
618 error_setg(errp, "Failed to fstat file: %s",
619 nfs_get_error(client->context));
620 goto fail;
623 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
624 client->st_blocks = st.st_blocks;
625 client->has_zero_init = S_ISREG(st.st_mode);
626 *strp = '/';
627 goto out;
629 fail:
630 nfs_client_close(client);
631 out:
632 qemu_opts_del(opts);
633 g_free(file);
634 return ret;
637 static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
638 Error **errp) {
639 NFSClient *client = bs->opaque;
640 int64_t ret;
642 client->aio_context = bdrv_get_aio_context(bs);
644 ret = nfs_client_open(client, options,
645 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
646 errp, bs->open_flags);
647 if (ret < 0) {
648 return ret;
650 bs->total_sectors = ret;
651 ret = 0;
652 return ret;
655 static QemuOptsList nfs_create_opts = {
656 .name = "nfs-create-opts",
657 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
658 .desc = {
660 .name = BLOCK_OPT_SIZE,
661 .type = QEMU_OPT_SIZE,
662 .help = "Virtual disk size"
664 { /* end of list */ }
668 static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
670 int ret = 0;
671 int64_t total_size = 0;
672 NFSClient *client = g_new0(NFSClient, 1);
673 QDict *options = NULL;
675 client->aio_context = qemu_get_aio_context();
677 /* Read out options */
678 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
679 BDRV_SECTOR_SIZE);
681 options = qdict_new();
682 ret = nfs_parse_uri(url, options, errp);
683 if (ret < 0) {
684 goto out;
687 ret = nfs_client_open(client, options, O_CREAT, errp, 0);
688 if (ret < 0) {
689 goto out;
691 ret = nfs_ftruncate(client->context, client->fh, total_size);
692 nfs_client_close(client);
693 out:
694 QDECREF(options);
695 g_free(client);
696 return ret;
699 static int nfs_has_zero_init(BlockDriverState *bs)
701 NFSClient *client = bs->opaque;
702 return client->has_zero_init;
705 static void
706 nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
707 void *private_data)
709 NFSRPC *task = private_data;
710 task->ret = ret;
711 if (task->ret == 0) {
712 memcpy(task->st, data, sizeof(struct stat));
714 if (task->ret < 0) {
715 error_report("NFS Error: %s", nfs_get_error(nfs));
717 task->complete = 1;
718 bdrv_wakeup(task->bs);
721 static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
723 NFSClient *client = bs->opaque;
724 NFSRPC task = {0};
725 struct stat st;
727 if (bdrv_is_read_only(bs) &&
728 !(bs->open_flags & BDRV_O_NOCACHE)) {
729 return client->st_blocks * 512;
732 task.bs = bs;
733 task.st = &st;
734 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
735 &task) != 0) {
736 return -ENOMEM;
739 nfs_set_events(client);
740 BDRV_POLL_WHILE(bs, !task.complete);
742 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
745 static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
747 NFSClient *client = bs->opaque;
748 return nfs_ftruncate(client->context, client->fh, offset);
751 /* Note that this will not re-establish a connection with the NFS server
752 * - it is effectively a NOP. */
753 static int nfs_reopen_prepare(BDRVReopenState *state,
754 BlockReopenQueue *queue, Error **errp)
756 NFSClient *client = state->bs->opaque;
757 struct stat st;
758 int ret = 0;
760 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
761 error_setg(errp, "Cannot open a read-only mount as read-write");
762 return -EACCES;
765 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
766 error_setg(errp, "Cannot disable cache if libnfs readahead or"
767 " pagecache is enabled");
768 return -EINVAL;
771 /* Update cache for read-only reopens */
772 if (!(state->flags & BDRV_O_RDWR)) {
773 ret = nfs_fstat(client->context, client->fh, &st);
774 if (ret < 0) {
775 error_setg(errp, "Failed to fstat file: %s",
776 nfs_get_error(client->context));
777 return ret;
779 client->st_blocks = st.st_blocks;
782 return 0;
785 static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)
787 NFSClient *client = bs->opaque;
788 QDict *opts = qdict_new();
789 QObject *server_qdict;
790 Visitor *ov;
792 qdict_put(opts, "driver", qstring_from_str("nfs"));
794 if (client->uid && !client->gid) {
795 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
796 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
797 client->uid);
798 } else if (!client->uid && client->gid) {
799 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
800 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
801 client->gid);
802 } else if (client->uid && client->gid) {
803 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
804 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
805 client->server->host, client->path, client->uid, client->gid);
806 } else {
807 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
808 "nfs://%s%s", client->server->host, client->path);
811 ov = qobject_output_visitor_new(&server_qdict);
812 visit_type_NFSServer(ov, NULL, &client->server, &error_abort);
813 visit_complete(ov, &server_qdict);
814 qdict_put_obj(opts, "server", server_qdict);
815 qdict_put(opts, "path", qstring_from_str(client->path));
817 if (client->uid) {
818 qdict_put(opts, "user", qint_from_int(client->uid));
820 if (client->gid) {
821 qdict_put(opts, "group", qint_from_int(client->gid));
823 if (client->tcp_syncnt) {
824 qdict_put(opts, "tcp-syn-cnt",
825 qint_from_int(client->tcp_syncnt));
827 if (client->readahead) {
828 qdict_put(opts, "readahead-size",
829 qint_from_int(client->readahead));
831 if (client->pagecache) {
832 qdict_put(opts, "page-cache-size",
833 qint_from_int(client->pagecache));
835 if (client->debug) {
836 qdict_put(opts, "debug", qint_from_int(client->debug));
839 visit_free(ov);
840 qdict_flatten(opts);
841 bs->full_open_options = opts;
844 #ifdef LIBNFS_FEATURE_PAGECACHE
845 static void nfs_invalidate_cache(BlockDriverState *bs,
846 Error **errp)
848 NFSClient *client = bs->opaque;
849 nfs_pagecache_invalidate(client->context, client->fh);
851 #endif
853 static BlockDriver bdrv_nfs = {
854 .format_name = "nfs",
855 .protocol_name = "nfs",
857 .instance_size = sizeof(NFSClient),
858 .bdrv_parse_filename = nfs_parse_filename,
859 .create_opts = &nfs_create_opts,
861 .bdrv_has_zero_init = nfs_has_zero_init,
862 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
863 .bdrv_truncate = nfs_file_truncate,
865 .bdrv_file_open = nfs_file_open,
866 .bdrv_close = nfs_file_close,
867 .bdrv_create = nfs_file_create,
868 .bdrv_reopen_prepare = nfs_reopen_prepare,
870 .bdrv_co_preadv = nfs_co_preadv,
871 .bdrv_co_pwritev = nfs_co_pwritev,
872 .bdrv_co_flush_to_disk = nfs_co_flush,
874 .bdrv_detach_aio_context = nfs_detach_aio_context,
875 .bdrv_attach_aio_context = nfs_attach_aio_context,
876 .bdrv_refresh_filename = nfs_refresh_filename,
878 #ifdef LIBNFS_FEATURE_PAGECACHE
879 .bdrv_invalidate_cache = nfs_invalidate_cache,
880 #endif
883 static void nfs_block_init(void)
885 bdrv_register(&bdrv_nfs);
888 block_init(nfs_block_init);