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
25
#include "qemu/osdep.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"
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;
56
AioContext *aio_context;
61
int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
64
typedef struct NFSRPC {
74
static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
77
QueryParams *qp = NULL;
80
uri = uri_parse(filename);
82
error_setg(errp, "Invalid URI specified");
85
if (strcmp(uri->scheme, "nfs") != 0) {
86
error_setg(errp, "URI scheme must be 'nfs'");
91
error_setg(errp, "missing hostname in URI");
96
error_setg(errp, "missing file path in URI");
100
qp = query_params_parse(uri->query);
102
error_setg(errp, "could not parse query parameters");
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
if (!qp->p[i].value) {
112
error_setg(errp, "Value for NFS parameter expected: %s",
116
if (parse_uint_full(qp->p[i].value, NULL, 0)) {
117
error_setg(errp, "Illegal value for NFS parameter: %s",
121
if (!strcmp(qp->p[i].name, "uid")) {
122
qdict_put(options, "user",
123
qstring_from_str(qp->p[i].value));
124
} else if (!strcmp(qp->p[i].name, "gid")) {
125
qdict_put(options, "group",
126
qstring_from_str(qp->p[i].value));
127
} else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
128
qdict_put(options, "tcp-syn-count",
129
qstring_from_str(qp->p[i].value));
130
} else if (!strcmp(qp->p[i].name, "readahead")) {
131
qdict_put(options, "readahead-size",
132
qstring_from_str(qp->p[i].value));
133
} else if (!strcmp(qp->p[i].name, "pagecache")) {
134
qdict_put(options, "page-cache-size",
135
qstring_from_str(qp->p[i].value));
136
} else if (!strcmp(qp->p[i].name, "debug")) {
137
qdict_put(options, "debug",
138
qstring_from_str(qp->p[i].value));
140
error_setg(errp, "Unknown NFS parameter name: %s",
148
query_params_free(qp);
156
static bool nfs_has_filename_options_conflict(QDict *options, Error **errp)
158
const QDictEntry *qe;
160
for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
161
if (!strcmp(qe->key, "host") ||
162
!strcmp(qe->key, "path") ||
163
!strcmp(qe->key, "user") ||
164
!strcmp(qe->key, "group") ||
165
!strcmp(qe->key, "tcp-syn-count") ||
166
!strcmp(qe->key, "readahead-size") ||
167
!strcmp(qe->key, "page-cache-size") ||
168
!strcmp(qe->key, "debug") ||
169
strstart(qe->key, "server.", NULL))
171
error_setg(errp, "Option %s cannot be used with a filename",
180
static void nfs_parse_filename(const char *filename, QDict *options,
183
if (nfs_has_filename_options_conflict(options, errp)) {
187
nfs_parse_uri(filename, options, errp);
190
static void nfs_process_read(void *arg);
191
static void nfs_process_write(void *arg);
193
static void nfs_set_events(NFSClient *client)
195
int ev = nfs_which_events(client->context);
196
if (ev != client->events) {
197
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
199
(ev & POLLIN) ? nfs_process_read : NULL,
200
(ev & POLLOUT) ? nfs_process_write : NULL, client);
206
static void nfs_process_read(void *arg)
208
NFSClient *client = arg;
209
nfs_service(client->context, POLLIN);
210
nfs_set_events(client);
213
static void nfs_process_write(void *arg)
215
NFSClient *client = arg;
216
nfs_service(client->context, POLLOUT);
217
nfs_set_events(client);
220
static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
223
.co = qemu_coroutine_self(),
225
.client = bs->opaque,
229
static void nfs_co_generic_bh_cb(void *opaque)
231
NFSRPC *task = opaque;
233
qemu_coroutine_enter(task->co);
237
nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
240
NFSRPC *task = private_data;
243
if (task->ret > 0 && task->iov) {
244
if (task->ret <= task->iov->size) {
245
qemu_iovec_from_buf(task->iov, 0, data, task->ret);
251
error_report("NFS Error: %s", nfs_get_error(nfs));
253
aio_bh_schedule_oneshot(task->client->aio_context,
254
nfs_co_generic_bh_cb, task);
257
static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
258
int64_t sector_num, int nb_sectors,
261
NFSClient *client = bs->opaque;
264
nfs_co_init_task(bs, &task);
267
if (nfs_pread_async(client->context, client->fh,
268
sector_num * BDRV_SECTOR_SIZE,
269
nb_sectors * BDRV_SECTOR_SIZE,
270
nfs_co_generic_cb, &task) != 0) {
274
nfs_set_events(client);
275
while (!task.complete) {
276
qemu_coroutine_yield();
283
/* zero pad short reads */
284
if (task.ret < iov->size) {
285
qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
291
static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
292
int64_t sector_num, int nb_sectors,
295
NFSClient *client = bs->opaque;
299
nfs_co_init_task(bs, &task);
301
buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
302
if (nb_sectors && buf == NULL) {
306
qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
308
if (nfs_pwrite_async(client->context, client->fh,
309
sector_num * BDRV_SECTOR_SIZE,
310
nb_sectors * BDRV_SECTOR_SIZE,
311
buf, nfs_co_generic_cb, &task) != 0) {
316
nfs_set_events(client);
317
while (!task.complete) {
318
qemu_coroutine_yield();
323
if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
324
return task.ret < 0 ? task.ret : -EIO;
330
static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
332
NFSClient *client = bs->opaque;
335
nfs_co_init_task(bs, &task);
337
if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
342
nfs_set_events(client);
343
while (!task.complete) {
344
qemu_coroutine_yield();
350
static QemuOptsList runtime_opts = {
352
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
356
.type = QEMU_OPT_STRING,
357
.help = "Path of the image on the host",
361
.type = QEMU_OPT_NUMBER,
362
.help = "UID value to use when talking to the server",
366
.type = QEMU_OPT_NUMBER,
367
.help = "GID value to use when talking to the server",
370
.name = "tcp-syncnt",
371
.type = QEMU_OPT_NUMBER,
372
.help = "Number of SYNs to send during the session establish",
376
.type = QEMU_OPT_NUMBER,
377
.help = "Set the readahead size in bytes",
381
.type = QEMU_OPT_NUMBER,
382
.help = "Set the pagecache size in bytes",
386
.type = QEMU_OPT_NUMBER,
387
.help = "Set the NFS debug level (max 2)",
389
{ /* end of list */ }
393
static void nfs_detach_aio_context(BlockDriverState *bs)
395
NFSClient *client = bs->opaque;
397
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
398
false, NULL, NULL, NULL);
402
static void nfs_attach_aio_context(BlockDriverState *bs,
403
AioContext *new_context)
405
NFSClient *client = bs->opaque;
407
client->aio_context = new_context;
408
nfs_set_events(client);
411
static void nfs_client_close(NFSClient *client)
413
if (client->context) {
415
nfs_close(client->context, client->fh);
417
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
418
false, NULL, NULL, NULL);
419
nfs_destroy_context(client->context);
421
memset(client, 0, sizeof(NFSClient));
424
static void nfs_file_close(BlockDriverState *bs)
426
NFSClient *client = bs->opaque;
427
nfs_client_close(client);
430
static NFSServer *nfs_config(QDict *options, Error **errp)
432
NFSServer *server = NULL;
434
QObject *crumpled_addr = NULL;
436
Error *local_error = NULL;
438
qdict_extract_subqdict(options, &addr, "server.");
439
if (!qdict_size(addr)) {
440
error_setg(errp, "NFS server address missing");
444
crumpled_addr = qdict_crumple(addr, errp);
445
if (!crumpled_addr) {
449
iv = qobject_input_visitor_new(crumpled_addr, true);
450
visit_type_NFSServer(iv, NULL, &server, &local_error);
452
error_propagate(errp, local_error);
458
qobject_decref(crumpled_addr);
464
static int64_t nfs_client_open(NFSClient *client, QDict *options,
465
int flags, Error **errp, int open_flags)
468
QemuOpts *opts = NULL;
469
Error *local_err = NULL;
471
char *file = NULL, *strp = NULL;
473
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
474
qemu_opts_absorb_qdict(opts, options, &local_err);
476
error_propagate(errp, local_err);
481
client->path = g_strdup(qemu_opt_get(opts, "path"));
484
error_setg(errp, "No path was specified");
488
strp = strrchr(client->path, '/');
490
error_setg(errp, "Invalid URL specified");
493
file = g_strdup(strp);
496
/* Pop the config into our state object, Exit if invalid */
497
client->server = nfs_config(options, errp);
498
if (!client->server) {
503
client->context = nfs_init_context();
504
if (client->context == NULL) {
505
error_setg(errp, "Failed to init NFS context");
509
if (qemu_opt_get(opts, "uid")) {
510
client->uid = qemu_opt_get_number(opts, "uid", 0);
511
nfs_set_uid(client->context, client->uid);
514
if (qemu_opt_get(opts, "gid")) {
515
client->gid = qemu_opt_get_number(opts, "gid", 0);
516
nfs_set_gid(client->context, client->gid);
519
if (qemu_opt_get(opts, "tcp-syncnt")) {
520
client->tcp_syncnt = qemu_opt_get_number(opts, "tcp-syncnt", 0);
521
nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
524
#ifdef LIBNFS_FEATURE_READAHEAD
525
if (qemu_opt_get(opts, "readahead")) {
526
if (open_flags & BDRV_O_NOCACHE) {
527
error_setg(errp, "Cannot enable NFS readahead "
528
"if cache.direct = on");
531
client->readahead = qemu_opt_get_number(opts, "readahead", 0);
532
if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
533
error_report("NFS Warning: Truncating NFS readahead "
534
"size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
535
client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
537
nfs_set_readahead(client->context, client->readahead);
538
#ifdef LIBNFS_FEATURE_PAGECACHE
539
nfs_set_pagecache_ttl(client->context, 0);
541
client->cache_used = true;
545
#ifdef LIBNFS_FEATURE_PAGECACHE
546
if (qemu_opt_get(opts, "pagecache")) {
547
if (open_flags & BDRV_O_NOCACHE) {
548
error_setg(errp, "Cannot enable NFS pagecache "
549
"if cache.direct = on");
552
client->pagecache = qemu_opt_get_number(opts, "pagecache", 0);
553
if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
554
error_report("NFS Warning: Truncating NFS pagecache "
555
"size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
556
client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
558
nfs_set_pagecache(client->context, client->pagecache);
559
nfs_set_pagecache_ttl(client->context, 0);
560
client->cache_used = true;
564
#ifdef LIBNFS_FEATURE_DEBUG
565
if (qemu_opt_get(opts, "debug")) {
566
client->debug = qemu_opt_get_number(opts, "debug", 0);
567
/* limit the maximum debug level to avoid potential flooding
568
* of our log files. */
569
if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
570
error_report("NFS Warning: Limiting NFS debug level "
571
"to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
572
client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
574
nfs_set_debug(client->context, client->debug);
578
ret = nfs_mount(client->context, client->server->host, client->path);
580
error_setg(errp, "Failed to mount nfs share: %s",
581
nfs_get_error(client->context));
585
if (flags & O_CREAT) {
586
ret = nfs_creat(client->context, file, 0600, &client->fh);
588
error_setg(errp, "Failed to create file: %s",
589
nfs_get_error(client->context));
593
ret = nfs_open(client->context, file, flags, &client->fh);
595
error_setg(errp, "Failed to open file : %s",
596
nfs_get_error(client->context));
601
ret = nfs_fstat(client->context, client->fh, &st);
603
error_setg(errp, "Failed to fstat file: %s",
604
nfs_get_error(client->context));
608
ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
609
client->st_blocks = st.st_blocks;
610
client->has_zero_init = S_ISREG(st.st_mode);
615
nfs_client_close(client);
622
static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
624
NFSClient *client = bs->opaque;
627
client->aio_context = bdrv_get_aio_context(bs);
629
ret = nfs_client_open(client, options,
630
(flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
631
errp, bs->open_flags);
635
bs->total_sectors = ret;
640
static QemuOptsList nfs_create_opts = {
641
.name = "nfs-create-opts",
642
.head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
645
.name = BLOCK_OPT_SIZE,
646
.type = QEMU_OPT_SIZE,
647
.help = "Virtual disk size"
649
{ /* end of list */ }
653
static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
656
int64_t total_size = 0;
657
NFSClient *client = g_new0(NFSClient, 1);
658
QDict *options = NULL;
660
client->aio_context = qemu_get_aio_context();
662
/* Read out options */
663
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
666
options = qdict_new();
667
ret = nfs_parse_uri(url, options, errp);
672
ret = nfs_client_open(client, options, O_CREAT, errp, 0);
676
ret = nfs_ftruncate(client->context, client->fh, total_size);
677
nfs_client_close(client);
684
static int nfs_has_zero_init(BlockDriverState *bs)
686
NFSClient *client = bs->opaque;
687
return client->has_zero_init;
691
nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
694
NFSRPC *task = private_data;
696
if (task->ret == 0) {
697
memcpy(task->st, data, sizeof(struct stat));
700
error_report("NFS Error: %s", nfs_get_error(nfs));
703
bdrv_wakeup(task->bs);
706
static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
708
NFSClient *client = bs->opaque;
712
if (bdrv_is_read_only(bs) &&
713
!(bs->open_flags & BDRV_O_NOCACHE)) {
714
return client->st_blocks * 512;
719
if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
724
nfs_set_events(client);
725
BDRV_POLL_WHILE(bs, !task.complete);
727
return (task.ret < 0 ? task.ret : st.st_blocks * 512);
730
static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
732
NFSClient *client = bs->opaque;
733
return nfs_ftruncate(client->context, client->fh, offset);
736
/* Note that this will not re-establish a connection with the NFS server
737
* - it is effectively a NOP. */
738
static int nfs_reopen_prepare(BDRVReopenState *state,
739
BlockReopenQueue *queue, Error **errp)
741
NFSClient *client = state->bs->opaque;
745
if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
746
error_setg(errp, "Cannot open a read-only mount as read-write");
750
if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
751
error_setg(errp, "Cannot disable cache if libnfs readahead or"
752
" pagecache is enabled");
756
/* Update cache for read-only reopens */
757
if (!(state->flags & BDRV_O_RDWR)) {
758
ret = nfs_fstat(client->context, client->fh, &st);
760
error_setg(errp, "Failed to fstat file: %s",
761
nfs_get_error(client->context));
764
client->st_blocks = st.st_blocks;
770
static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)
772
NFSClient *client = bs->opaque;
773
QDict *opts = qdict_new();
774
QObject *server_qdict;
777
qdict_put(opts, "driver", qstring_from_str("nfs"));
779
if (client->uid && !client->gid) {
780
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
781
"nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
783
} else if (!client->uid && client->gid) {
784
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
785
"nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
787
} else if (client->uid && client->gid) {
788
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
789
"nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
790
client->server->host, client->path, client->uid, client->gid);
792
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
793
"nfs://%s%s", client->server->host, client->path);
796
ov = qobject_output_visitor_new(&server_qdict);
797
visit_type_NFSServer(ov, NULL, &client->server, &error_abort);
798
visit_complete(ov, &server_qdict);
799
assert(qobject_type(server_qdict) == QTYPE_QDICT);
801
qdict_put_obj(opts, "server", server_qdict);
802
qdict_put(opts, "path", qstring_from_str(client->path));
805
qdict_put(opts, "uid", qint_from_int(client->uid));
808
qdict_put(opts, "gid", qint_from_int(client->gid));
810
if (client->tcp_syncnt) {
811
qdict_put(opts, "tcp-syncnt",
812
qint_from_int(client->tcp_syncnt));
814
if (client->readahead) {
815
qdict_put(opts, "readahead",
816
qint_from_int(client->readahead));
818
if (client->pagecache) {
819
qdict_put(opts, "pagecache",
820
qint_from_int(client->pagecache));
823
qdict_put(opts, "debug", qint_from_int(client->debug));
828
bs->full_open_options = opts;
831
#ifdef LIBNFS_FEATURE_PAGECACHE
832
static void nfs_invalidate_cache(BlockDriverState *bs,
835
NFSClient *client = bs->opaque;
836
nfs_pagecache_invalidate(client->context, client->fh);
840
static BlockDriver bdrv_nfs = {
841
.format_name = "nfs",
842
.protocol_name = "nfs",
844
.instance_size = sizeof(NFSClient),
845
.bdrv_parse_filename = nfs_parse_filename,
846
.create_opts = &nfs_create_opts,
848
.bdrv_has_zero_init = nfs_has_zero_init,
849
.bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
850
.bdrv_truncate = nfs_file_truncate,
852
.bdrv_file_open = nfs_file_open,
853
.bdrv_close = nfs_file_close,
854
.bdrv_create = nfs_file_create,
855
.bdrv_reopen_prepare = nfs_reopen_prepare,
857
.bdrv_co_readv = nfs_co_readv,
858
.bdrv_co_writev = nfs_co_writev,
859
.bdrv_co_flush_to_disk = nfs_co_flush,
861
.bdrv_detach_aio_context = nfs_detach_aio_context,
862
.bdrv_attach_aio_context = nfs_attach_aio_context,
863
.bdrv_refresh_filename = nfs_refresh_filename,
865
#ifdef LIBNFS_FEATURE_PAGECACHE
866
.bdrv_invalidate_cache = nfs_invalidate_cache,
870
static void nfs_block_init(void)
872
bdrv_register(&bdrv_nfs);
875
block_init(nfs_block_init);