tests/vm: avoid image presence check and removal
[qemu/ar7.git] / block / nfs.c
blobd93241b3bb84cf0a662f0ddec5829744fa9b5a47
1 /*
2 * QEMU Block driver for native access to files on NFS shares
4 * Copyright (c) 2014-2017 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/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"
33 #include "trace.h"
34 #include "qemu/iov.h"
35 #include "qemu/module.h"
36 #include "qemu/option.h"
37 #include "qemu/uri.h"
38 #include "qemu/cutils.h"
39 #include "sysemu/sysemu.h"
40 #include "qapi/qapi-visit-block-core.h"
41 #include "qapi/qmp/qdict.h"
42 #include "qapi/qmp/qstring.h"
43 #include "qapi/qobject-input-visitor.h"
44 #include "qapi/qobject-output-visitor.h"
45 #include <nfsc/libnfs.h>
48 #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
49 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
50 #define QEMU_NFS_MAX_DEBUG_LEVEL 2
52 typedef struct NFSClient {
53 struct nfs_context *context;
54 struct nfsfh *fh;
55 int events;
56 bool has_zero_init;
57 AioContext *aio_context;
58 QemuMutex mutex;
59 blkcnt_t st_blocks;
60 bool cache_used;
61 NFSServer *server;
62 char *path;
63 int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
64 } NFSClient;
66 typedef struct NFSRPC {
67 BlockDriverState *bs;
68 int ret;
69 int complete;
70 QEMUIOVector *iov;
71 struct stat *st;
72 Coroutine *co;
73 NFSClient *client;
74 } NFSRPC;
76 static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
78 URI *uri = NULL;
79 QueryParams *qp = NULL;
80 int ret = -EINVAL, i;
82 uri = uri_parse(filename);
83 if (!uri) {
84 error_setg(errp, "Invalid URI specified");
85 goto out;
87 if (g_strcmp0(uri->scheme, "nfs") != 0) {
88 error_setg(errp, "URI scheme must be 'nfs'");
89 goto out;
92 if (!uri->server) {
93 error_setg(errp, "missing hostname in URI");
94 goto out;
97 if (!uri->path) {
98 error_setg(errp, "missing file path in URI");
99 goto out;
102 qp = query_params_parse(uri->query);
103 if (!qp) {
104 error_setg(errp, "could not parse query parameters");
105 goto out;
108 qdict_put_str(options, "server.host", uri->server);
109 qdict_put_str(options, "server.type", "inet");
110 qdict_put_str(options, "path", uri->path);
112 for (i = 0; i < qp->n; i++) {
113 unsigned long long val;
114 if (!qp->p[i].value) {
115 error_setg(errp, "Value for NFS parameter expected: %s",
116 qp->p[i].name);
117 goto out;
119 if (parse_uint_full(qp->p[i].value, &val, 0)) {
120 error_setg(errp, "Illegal value for NFS parameter: %s",
121 qp->p[i].name);
122 goto out;
124 if (!strcmp(qp->p[i].name, "uid")) {
125 qdict_put_str(options, "user", qp->p[i].value);
126 } else if (!strcmp(qp->p[i].name, "gid")) {
127 qdict_put_str(options, "group", qp->p[i].value);
128 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
129 qdict_put_str(options, "tcp-syn-count", qp->p[i].value);
130 } else if (!strcmp(qp->p[i].name, "readahead")) {
131 qdict_put_str(options, "readahead-size", qp->p[i].value);
132 } else if (!strcmp(qp->p[i].name, "pagecache")) {
133 qdict_put_str(options, "page-cache-size", qp->p[i].value);
134 } else if (!strcmp(qp->p[i].name, "debug")) {
135 qdict_put_str(options, "debug", qp->p[i].value);
136 } else {
137 error_setg(errp, "Unknown NFS parameter name: %s",
138 qp->p[i].name);
139 goto out;
142 ret = 0;
143 out:
144 if (qp) {
145 query_params_free(qp);
147 if (uri) {
148 uri_free(uri);
150 return ret;
153 static bool nfs_has_filename_options_conflict(QDict *options, Error **errp)
155 const QDictEntry *qe;
157 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
158 if (!strcmp(qe->key, "host") ||
159 !strcmp(qe->key, "path") ||
160 !strcmp(qe->key, "user") ||
161 !strcmp(qe->key, "group") ||
162 !strcmp(qe->key, "tcp-syn-count") ||
163 !strcmp(qe->key, "readahead-size") ||
164 !strcmp(qe->key, "page-cache-size") ||
165 !strcmp(qe->key, "debug") ||
166 strstart(qe->key, "server.", NULL))
168 error_setg(errp, "Option %s cannot be used with a filename",
169 qe->key);
170 return true;
174 return false;
177 static void nfs_parse_filename(const char *filename, QDict *options,
178 Error **errp)
180 if (nfs_has_filename_options_conflict(options, errp)) {
181 return;
184 nfs_parse_uri(filename, options, errp);
187 static void nfs_process_read(void *arg);
188 static void nfs_process_write(void *arg);
190 /* Called with QemuMutex held. */
191 static void nfs_set_events(NFSClient *client)
193 int ev = nfs_which_events(client->context);
194 if (ev != client->events) {
195 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
196 false,
197 (ev & POLLIN) ? nfs_process_read : NULL,
198 (ev & POLLOUT) ? nfs_process_write : NULL,
199 NULL, client);
202 client->events = ev;
205 static void nfs_process_read(void *arg)
207 NFSClient *client = arg;
209 qemu_mutex_lock(&client->mutex);
210 nfs_service(client->context, POLLIN);
211 nfs_set_events(client);
212 qemu_mutex_unlock(&client->mutex);
215 static void nfs_process_write(void *arg)
217 NFSClient *client = arg;
219 qemu_mutex_lock(&client->mutex);
220 nfs_service(client->context, POLLOUT);
221 nfs_set_events(client);
222 qemu_mutex_unlock(&client->mutex);
225 static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
227 *task = (NFSRPC) {
228 .co = qemu_coroutine_self(),
229 .bs = bs,
230 .client = bs->opaque,
234 static void nfs_co_generic_bh_cb(void *opaque)
236 NFSRPC *task = opaque;
238 task->complete = 1;
239 aio_co_wake(task->co);
242 /* Called (via nfs_service) with QemuMutex held. */
243 static void
244 nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
245 void *private_data)
247 NFSRPC *task = private_data;
248 task->ret = ret;
249 assert(!task->st);
250 if (task->ret > 0 && task->iov) {
251 if (task->ret <= task->iov->size) {
252 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
253 } else {
254 task->ret = -EIO;
257 if (task->ret < 0) {
258 error_report("NFS Error: %s", nfs_get_error(nfs));
260 aio_bh_schedule_oneshot(task->client->aio_context,
261 nfs_co_generic_bh_cb, task);
264 static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, uint64_t offset,
265 uint64_t bytes, QEMUIOVector *iov,
266 int flags)
268 NFSClient *client = bs->opaque;
269 NFSRPC task;
271 nfs_co_init_task(bs, &task);
272 task.iov = iov;
274 qemu_mutex_lock(&client->mutex);
275 if (nfs_pread_async(client->context, client->fh,
276 offset, bytes, nfs_co_generic_cb, &task) != 0) {
277 qemu_mutex_unlock(&client->mutex);
278 return -ENOMEM;
281 nfs_set_events(client);
282 qemu_mutex_unlock(&client->mutex);
283 while (!task.complete) {
284 qemu_coroutine_yield();
287 if (task.ret < 0) {
288 return task.ret;
291 /* zero pad short reads */
292 if (task.ret < iov->size) {
293 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
296 return 0;
299 static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, uint64_t offset,
300 uint64_t bytes, QEMUIOVector *iov,
301 int flags)
303 NFSClient *client = bs->opaque;
304 NFSRPC task;
305 char *buf = NULL;
306 bool my_buffer = false;
308 nfs_co_init_task(bs, &task);
310 if (iov->niov != 1) {
311 buf = g_try_malloc(bytes);
312 if (bytes && buf == NULL) {
313 return -ENOMEM;
315 qemu_iovec_to_buf(iov, 0, buf, bytes);
316 my_buffer = true;
317 } else {
318 buf = iov->iov[0].iov_base;
321 qemu_mutex_lock(&client->mutex);
322 if (nfs_pwrite_async(client->context, client->fh,
323 offset, bytes, buf,
324 nfs_co_generic_cb, &task) != 0) {
325 qemu_mutex_unlock(&client->mutex);
326 if (my_buffer) {
327 g_free(buf);
329 return -ENOMEM;
332 nfs_set_events(client);
333 qemu_mutex_unlock(&client->mutex);
334 while (!task.complete) {
335 qemu_coroutine_yield();
338 if (my_buffer) {
339 g_free(buf);
342 if (task.ret != bytes) {
343 return task.ret < 0 ? task.ret : -EIO;
346 return 0;
349 static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
351 NFSClient *client = bs->opaque;
352 NFSRPC task;
354 nfs_co_init_task(bs, &task);
356 qemu_mutex_lock(&client->mutex);
357 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
358 &task) != 0) {
359 qemu_mutex_unlock(&client->mutex);
360 return -ENOMEM;
363 nfs_set_events(client);
364 qemu_mutex_unlock(&client->mutex);
365 while (!task.complete) {
366 qemu_coroutine_yield();
369 return task.ret;
372 static void nfs_detach_aio_context(BlockDriverState *bs)
374 NFSClient *client = bs->opaque;
376 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
377 false, NULL, NULL, NULL, NULL);
378 client->events = 0;
381 static void nfs_attach_aio_context(BlockDriverState *bs,
382 AioContext *new_context)
384 NFSClient *client = bs->opaque;
386 client->aio_context = new_context;
387 nfs_set_events(client);
390 static void nfs_client_close(NFSClient *client)
392 if (client->context) {
393 if (client->fh) {
394 nfs_close(client->context, client->fh);
395 client->fh = NULL;
397 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
398 false, NULL, NULL, NULL, NULL);
399 nfs_destroy_context(client->context);
400 client->context = NULL;
402 g_free(client->path);
403 qemu_mutex_destroy(&client->mutex);
404 qapi_free_NFSServer(client->server);
405 client->server = NULL;
408 static void nfs_file_close(BlockDriverState *bs)
410 NFSClient *client = bs->opaque;
411 nfs_client_close(client);
414 static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts,
415 int flags, int open_flags, Error **errp)
417 int64_t ret = -EINVAL;
418 struct stat st;
419 char *file = NULL, *strp = NULL;
421 qemu_mutex_init(&client->mutex);
423 client->path = g_strdup(opts->path);
425 strp = strrchr(client->path, '/');
426 if (strp == NULL) {
427 error_setg(errp, "Invalid URL specified");
428 goto fail;
430 file = g_strdup(strp);
431 *strp = 0;
433 /* Steal the NFSServer object from opts; set the original pointer to NULL
434 * to avoid use after free and double free. */
435 client->server = opts->server;
436 opts->server = NULL;
438 client->context = nfs_init_context();
439 if (client->context == NULL) {
440 error_setg(errp, "Failed to init NFS context");
441 goto fail;
444 if (opts->has_user) {
445 client->uid = opts->user;
446 nfs_set_uid(client->context, client->uid);
449 if (opts->has_group) {
450 client->gid = opts->group;
451 nfs_set_gid(client->context, client->gid);
454 if (opts->has_tcp_syn_count) {
455 client->tcp_syncnt = opts->tcp_syn_count;
456 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
459 #ifdef LIBNFS_FEATURE_READAHEAD
460 if (opts->has_readahead_size) {
461 if (open_flags & BDRV_O_NOCACHE) {
462 error_setg(errp, "Cannot enable NFS readahead "
463 "if cache.direct = on");
464 goto fail;
466 client->readahead = opts->readahead_size;
467 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
468 warn_report("Truncating NFS readahead size to %d",
469 QEMU_NFS_MAX_READAHEAD_SIZE);
470 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
472 nfs_set_readahead(client->context, client->readahead);
473 #ifdef LIBNFS_FEATURE_PAGECACHE
474 nfs_set_pagecache_ttl(client->context, 0);
475 #endif
476 client->cache_used = true;
478 #endif
480 #ifdef LIBNFS_FEATURE_PAGECACHE
481 if (opts->has_page_cache_size) {
482 if (open_flags & BDRV_O_NOCACHE) {
483 error_setg(errp, "Cannot enable NFS pagecache "
484 "if cache.direct = on");
485 goto fail;
487 client->pagecache = opts->page_cache_size;
488 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
489 warn_report("Truncating NFS pagecache size to %d pages",
490 QEMU_NFS_MAX_PAGECACHE_SIZE);
491 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
493 nfs_set_pagecache(client->context, client->pagecache);
494 nfs_set_pagecache_ttl(client->context, 0);
495 client->cache_used = true;
497 #endif
499 #ifdef LIBNFS_FEATURE_DEBUG
500 if (opts->has_debug) {
501 client->debug = opts->debug;
502 /* limit the maximum debug level to avoid potential flooding
503 * of our log files. */
504 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
505 warn_report("Limiting NFS debug level to %d",
506 QEMU_NFS_MAX_DEBUG_LEVEL);
507 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
509 nfs_set_debug(client->context, client->debug);
511 #endif
513 ret = nfs_mount(client->context, client->server->host, client->path);
514 if (ret < 0) {
515 error_setg(errp, "Failed to mount nfs share: %s",
516 nfs_get_error(client->context));
517 goto fail;
520 if (flags & O_CREAT) {
521 ret = nfs_creat(client->context, file, 0600, &client->fh);
522 if (ret < 0) {
523 error_setg(errp, "Failed to create file: %s",
524 nfs_get_error(client->context));
525 goto fail;
527 } else {
528 ret = nfs_open(client->context, file, flags, &client->fh);
529 if (ret < 0) {
530 error_setg(errp, "Failed to open file : %s",
531 nfs_get_error(client->context));
532 goto fail;
536 ret = nfs_fstat(client->context, client->fh, &st);
537 if (ret < 0) {
538 error_setg(errp, "Failed to fstat file: %s",
539 nfs_get_error(client->context));
540 goto fail;
543 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
544 client->st_blocks = st.st_blocks;
545 client->has_zero_init = S_ISREG(st.st_mode);
546 *strp = '/';
547 goto out;
549 fail:
550 nfs_client_close(client);
551 out:
552 g_free(file);
553 return ret;
556 static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options,
557 Error **errp)
559 BlockdevOptionsNfs *opts = NULL;
560 Visitor *v;
561 const QDictEntry *e;
562 Error *local_err = NULL;
564 v = qobject_input_visitor_new_flat_confused(options, errp);
565 if (!v) {
566 return NULL;
569 visit_type_BlockdevOptionsNfs(v, NULL, &opts, &local_err);
570 visit_free(v);
572 if (local_err) {
573 error_propagate(errp, local_err);
574 return NULL;
577 /* Remove the processed options from the QDict (the visitor processes
578 * _all_ options in the QDict) */
579 while ((e = qdict_first(options))) {
580 qdict_del(options, e->key);
583 return opts;
586 static int64_t nfs_client_open_qdict(NFSClient *client, QDict *options,
587 int flags, int open_flags, Error **errp)
589 BlockdevOptionsNfs *opts;
590 int ret;
592 opts = nfs_options_qdict_to_qapi(options, errp);
593 if (opts == NULL) {
594 ret = -EINVAL;
595 goto fail;
598 ret = nfs_client_open(client, opts, flags, open_flags, errp);
599 fail:
600 qapi_free_BlockdevOptionsNfs(opts);
601 return ret;
604 static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
605 Error **errp) {
606 NFSClient *client = bs->opaque;
607 int64_t ret;
609 client->aio_context = bdrv_get_aio_context(bs);
611 ret = nfs_client_open_qdict(client, options,
612 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
613 bs->open_flags, errp);
614 if (ret < 0) {
615 return ret;
618 bs->total_sectors = ret;
619 ret = 0;
620 return ret;
623 static QemuOptsList nfs_create_opts = {
624 .name = "nfs-create-opts",
625 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
626 .desc = {
628 .name = BLOCK_OPT_SIZE,
629 .type = QEMU_OPT_SIZE,
630 .help = "Virtual disk size"
632 { /* end of list */ }
636 static int nfs_file_co_create(BlockdevCreateOptions *options, Error **errp)
638 BlockdevCreateOptionsNfs *opts = &options->u.nfs;
639 NFSClient *client = g_new0(NFSClient, 1);
640 int ret;
642 assert(options->driver == BLOCKDEV_DRIVER_NFS);
644 client->aio_context = qemu_get_aio_context();
646 ret = nfs_client_open(client, opts->location, O_CREAT, 0, errp);
647 if (ret < 0) {
648 goto out;
650 ret = nfs_ftruncate(client->context, client->fh, opts->size);
651 nfs_client_close(client);
653 out:
654 g_free(client);
655 return ret;
658 static int coroutine_fn nfs_file_co_create_opts(const char *url, QemuOpts *opts,
659 Error **errp)
661 BlockdevCreateOptions *create_options;
662 BlockdevCreateOptionsNfs *nfs_opts;
663 QDict *options;
664 int ret;
666 create_options = g_new0(BlockdevCreateOptions, 1);
667 create_options->driver = BLOCKDEV_DRIVER_NFS;
668 nfs_opts = &create_options->u.nfs;
670 /* Read out options */
671 nfs_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
672 BDRV_SECTOR_SIZE);
674 options = qdict_new();
675 ret = nfs_parse_uri(url, options, errp);
676 if (ret < 0) {
677 goto out;
680 nfs_opts->location = nfs_options_qdict_to_qapi(options, errp);
681 if (nfs_opts->location == NULL) {
682 ret = -EINVAL;
683 goto out;
686 ret = nfs_file_co_create(create_options, errp);
687 if (ret < 0) {
688 goto out;
691 ret = 0;
692 out:
693 qobject_unref(options);
694 qapi_free_BlockdevCreateOptions(create_options);
695 return ret;
698 static int nfs_has_zero_init(BlockDriverState *bs)
700 NFSClient *client = bs->opaque;
701 return client->has_zero_init;
704 /* Called (via nfs_service) with QemuMutex held. */
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));
718 /* Set task->complete before reading bs->wakeup. */
719 atomic_mb_set(&task->complete, 1);
720 bdrv_wakeup(task->bs);
723 static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
725 NFSClient *client = bs->opaque;
726 NFSRPC task = {0};
727 struct stat st;
729 if (bdrv_is_read_only(bs) &&
730 !(bs->open_flags & BDRV_O_NOCACHE)) {
731 return client->st_blocks * 512;
734 task.bs = bs;
735 task.st = &st;
736 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
737 &task) != 0) {
738 return -ENOMEM;
741 nfs_set_events(client);
742 BDRV_POLL_WHILE(bs, !task.complete);
744 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
747 static int coroutine_fn
748 nfs_file_co_truncate(BlockDriverState *bs, int64_t offset,
749 PreallocMode prealloc, Error **errp)
751 NFSClient *client = bs->opaque;
752 int ret;
754 if (prealloc != PREALLOC_MODE_OFF) {
755 error_setg(errp, "Unsupported preallocation mode '%s'",
756 PreallocMode_str(prealloc));
757 return -ENOTSUP;
760 ret = nfs_ftruncate(client->context, client->fh, offset);
761 if (ret < 0) {
762 error_setg_errno(errp, -ret, "Failed to truncate file");
763 return ret;
766 return 0;
769 /* Note that this will not re-establish a connection with the NFS server
770 * - it is effectively a NOP. */
771 static int nfs_reopen_prepare(BDRVReopenState *state,
772 BlockReopenQueue *queue, Error **errp)
774 NFSClient *client = state->bs->opaque;
775 struct stat st;
776 int ret = 0;
778 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
779 error_setg(errp, "Cannot open a read-only mount as read-write");
780 return -EACCES;
783 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
784 error_setg(errp, "Cannot disable cache if libnfs readahead or"
785 " pagecache is enabled");
786 return -EINVAL;
789 /* Update cache for read-only reopens */
790 if (!(state->flags & BDRV_O_RDWR)) {
791 ret = nfs_fstat(client->context, client->fh, &st);
792 if (ret < 0) {
793 error_setg(errp, "Failed to fstat file: %s",
794 nfs_get_error(client->context));
795 return ret;
797 client->st_blocks = st.st_blocks;
800 return 0;
803 static void nfs_refresh_filename(BlockDriverState *bs)
805 NFSClient *client = bs->opaque;
807 if (client->uid && !client->gid) {
808 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
809 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
810 client->uid);
811 } else if (!client->uid && client->gid) {
812 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
813 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
814 client->gid);
815 } else if (client->uid && client->gid) {
816 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
817 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
818 client->server->host, client->path, client->uid, client->gid);
819 } else {
820 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
821 "nfs://%s%s", client->server->host, client->path);
825 static char *nfs_dirname(BlockDriverState *bs, Error **errp)
827 NFSClient *client = bs->opaque;
829 if (client->uid || client->gid) {
830 bdrv_refresh_filename(bs);
831 error_setg(errp, "Cannot generate a base directory for NFS node '%s'",
832 bs->filename);
833 return NULL;
836 return g_strdup_printf("nfs://%s%s/", client->server->host, client->path);
839 #ifdef LIBNFS_FEATURE_PAGECACHE
840 static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs,
841 Error **errp)
843 NFSClient *client = bs->opaque;
844 nfs_pagecache_invalidate(client->context, client->fh);
846 #endif
848 static const char *nfs_strong_runtime_opts[] = {
849 "path",
850 "user",
851 "group",
852 "server.",
854 NULL
857 static BlockDriver bdrv_nfs = {
858 .format_name = "nfs",
859 .protocol_name = "nfs",
861 .instance_size = sizeof(NFSClient),
862 .bdrv_parse_filename = nfs_parse_filename,
863 .create_opts = &nfs_create_opts,
865 .bdrv_has_zero_init = nfs_has_zero_init,
866 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
867 .bdrv_co_truncate = nfs_file_co_truncate,
869 .bdrv_file_open = nfs_file_open,
870 .bdrv_close = nfs_file_close,
871 .bdrv_co_create = nfs_file_co_create,
872 .bdrv_co_create_opts = nfs_file_co_create_opts,
873 .bdrv_reopen_prepare = nfs_reopen_prepare,
875 .bdrv_co_preadv = nfs_co_preadv,
876 .bdrv_co_pwritev = nfs_co_pwritev,
877 .bdrv_co_flush_to_disk = nfs_co_flush,
879 .bdrv_detach_aio_context = nfs_detach_aio_context,
880 .bdrv_attach_aio_context = nfs_attach_aio_context,
881 .bdrv_refresh_filename = nfs_refresh_filename,
882 .bdrv_dirname = nfs_dirname,
884 .strong_runtime_opts = nfs_strong_runtime_opts,
886 #ifdef LIBNFS_FEATURE_PAGECACHE
887 .bdrv_co_invalidate_cache = nfs_co_invalidate_cache,
888 #endif
891 static void nfs_block_init(void)
893 bdrv_register(&bdrv_nfs);
896 block_init(nfs_block_init);