Merge remote-tracking branch 'remotes/sstabellini/tags/xen-20161028-tag' into staging
[qemu/ar7.git] / block / nfs.c
blob88c60a9118f742feb873ad591bd55e6fe354de6a
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 <nfsc/libnfs.h>
40 #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
41 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
42 #define QEMU_NFS_MAX_DEBUG_LEVEL 2
44 typedef struct NFSClient {
45 struct nfs_context *context;
46 struct nfsfh *fh;
47 int events;
48 bool has_zero_init;
49 AioContext *aio_context;
50 blkcnt_t st_blocks;
51 bool cache_used;
52 } NFSClient;
54 typedef struct NFSRPC {
55 BlockDriverState *bs;
56 int ret;
57 int complete;
58 QEMUIOVector *iov;
59 struct stat *st;
60 Coroutine *co;
61 NFSClient *client;
62 } NFSRPC;
64 static void nfs_process_read(void *arg);
65 static void nfs_process_write(void *arg);
67 static void nfs_set_events(NFSClient *client)
69 int ev = nfs_which_events(client->context);
70 if (ev != client->events) {
71 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
72 false,
73 (ev & POLLIN) ? nfs_process_read : NULL,
74 (ev & POLLOUT) ? nfs_process_write : NULL, client);
77 client->events = ev;
80 static void nfs_process_read(void *arg)
82 NFSClient *client = arg;
83 nfs_service(client->context, POLLIN);
84 nfs_set_events(client);
87 static void nfs_process_write(void *arg)
89 NFSClient *client = arg;
90 nfs_service(client->context, POLLOUT);
91 nfs_set_events(client);
94 static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
96 *task = (NFSRPC) {
97 .co = qemu_coroutine_self(),
98 .bs = bs,
99 .client = bs->opaque,
103 static void nfs_co_generic_bh_cb(void *opaque)
105 NFSRPC *task = opaque;
106 task->complete = 1;
107 qemu_coroutine_enter(task->co);
110 static void
111 nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
112 void *private_data)
114 NFSRPC *task = private_data;
115 task->ret = ret;
116 assert(!task->st);
117 if (task->ret > 0 && task->iov) {
118 if (task->ret <= task->iov->size) {
119 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
120 } else {
121 task->ret = -EIO;
124 if (task->ret < 0) {
125 error_report("NFS Error: %s", nfs_get_error(nfs));
127 aio_bh_schedule_oneshot(task->client->aio_context,
128 nfs_co_generic_bh_cb, task);
131 static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
132 int64_t sector_num, int nb_sectors,
133 QEMUIOVector *iov)
135 NFSClient *client = bs->opaque;
136 NFSRPC task;
138 nfs_co_init_task(bs, &task);
139 task.iov = iov;
141 if (nfs_pread_async(client->context, client->fh,
142 sector_num * BDRV_SECTOR_SIZE,
143 nb_sectors * BDRV_SECTOR_SIZE,
144 nfs_co_generic_cb, &task) != 0) {
145 return -ENOMEM;
148 nfs_set_events(client);
149 while (!task.complete) {
150 qemu_coroutine_yield();
153 if (task.ret < 0) {
154 return task.ret;
157 /* zero pad short reads */
158 if (task.ret < iov->size) {
159 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
162 return 0;
165 static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
166 int64_t sector_num, int nb_sectors,
167 QEMUIOVector *iov)
169 NFSClient *client = bs->opaque;
170 NFSRPC task;
171 char *buf = NULL;
173 nfs_co_init_task(bs, &task);
175 buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
176 if (nb_sectors && buf == NULL) {
177 return -ENOMEM;
180 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
182 if (nfs_pwrite_async(client->context, client->fh,
183 sector_num * BDRV_SECTOR_SIZE,
184 nb_sectors * BDRV_SECTOR_SIZE,
185 buf, nfs_co_generic_cb, &task) != 0) {
186 g_free(buf);
187 return -ENOMEM;
190 nfs_set_events(client);
191 while (!task.complete) {
192 qemu_coroutine_yield();
195 g_free(buf);
197 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
198 return task.ret < 0 ? task.ret : -EIO;
201 return 0;
204 static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
206 NFSClient *client = bs->opaque;
207 NFSRPC task;
209 nfs_co_init_task(bs, &task);
211 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
212 &task) != 0) {
213 return -ENOMEM;
216 nfs_set_events(client);
217 while (!task.complete) {
218 qemu_coroutine_yield();
221 return task.ret;
224 /* TODO Convert to fine grained options */
225 static QemuOptsList runtime_opts = {
226 .name = "nfs",
227 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
228 .desc = {
230 .name = "filename",
231 .type = QEMU_OPT_STRING,
232 .help = "URL to the NFS file",
234 { /* end of list */ }
238 static void nfs_detach_aio_context(BlockDriverState *bs)
240 NFSClient *client = bs->opaque;
242 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
243 false, NULL, NULL, NULL);
244 client->events = 0;
247 static void nfs_attach_aio_context(BlockDriverState *bs,
248 AioContext *new_context)
250 NFSClient *client = bs->opaque;
252 client->aio_context = new_context;
253 nfs_set_events(client);
256 static void nfs_client_close(NFSClient *client)
258 if (client->context) {
259 if (client->fh) {
260 nfs_close(client->context, client->fh);
262 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
263 false, NULL, NULL, NULL);
264 nfs_destroy_context(client->context);
266 memset(client, 0, sizeof(NFSClient));
269 static void nfs_file_close(BlockDriverState *bs)
271 NFSClient *client = bs->opaque;
272 nfs_client_close(client);
275 static int64_t nfs_client_open(NFSClient *client, const char *filename,
276 int flags, Error **errp, int open_flags)
278 int ret = -EINVAL, i;
279 struct stat st;
280 URI *uri;
281 QueryParams *qp = NULL;
282 char *file = NULL, *strp = NULL;
284 uri = uri_parse(filename);
285 if (!uri) {
286 error_setg(errp, "Invalid URL specified");
287 goto fail;
289 if (!uri->server) {
290 error_setg(errp, "Invalid URL specified");
291 goto fail;
293 strp = strrchr(uri->path, '/');
294 if (strp == NULL) {
295 error_setg(errp, "Invalid URL specified");
296 goto fail;
298 file = g_strdup(strp);
299 *strp = 0;
301 client->context = nfs_init_context();
302 if (client->context == NULL) {
303 error_setg(errp, "Failed to init NFS context");
304 goto fail;
307 qp = query_params_parse(uri->query);
308 for (i = 0; i < qp->n; i++) {
309 unsigned long long val;
310 if (!qp->p[i].value) {
311 error_setg(errp, "Value for NFS parameter expected: %s",
312 qp->p[i].name);
313 goto fail;
315 if (parse_uint_full(qp->p[i].value, &val, 0)) {
316 error_setg(errp, "Illegal value for NFS parameter: %s",
317 qp->p[i].name);
318 goto fail;
320 if (!strcmp(qp->p[i].name, "uid")) {
321 nfs_set_uid(client->context, val);
322 } else if (!strcmp(qp->p[i].name, "gid")) {
323 nfs_set_gid(client->context, val);
324 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
325 nfs_set_tcp_syncnt(client->context, val);
326 #ifdef LIBNFS_FEATURE_READAHEAD
327 } else if (!strcmp(qp->p[i].name, "readahead")) {
328 if (open_flags & BDRV_O_NOCACHE) {
329 error_setg(errp, "Cannot enable NFS readahead "
330 "if cache.direct = on");
331 goto fail;
333 if (val > QEMU_NFS_MAX_READAHEAD_SIZE) {
334 error_report("NFS Warning: Truncating NFS readahead"
335 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
336 val = QEMU_NFS_MAX_READAHEAD_SIZE;
338 nfs_set_readahead(client->context, val);
339 #ifdef LIBNFS_FEATURE_PAGECACHE
340 nfs_set_pagecache_ttl(client->context, 0);
341 #endif
342 client->cache_used = true;
343 #endif
344 #ifdef LIBNFS_FEATURE_PAGECACHE
345 nfs_set_pagecache_ttl(client->context, 0);
346 } else if (!strcmp(qp->p[i].name, "pagecache")) {
347 if (open_flags & BDRV_O_NOCACHE) {
348 error_setg(errp, "Cannot enable NFS pagecache "
349 "if cache.direct = on");
350 goto fail;
352 if (val > QEMU_NFS_MAX_PAGECACHE_SIZE) {
353 error_report("NFS Warning: Truncating NFS pagecache"
354 " size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
355 val = QEMU_NFS_MAX_PAGECACHE_SIZE;
357 nfs_set_pagecache(client->context, val);
358 nfs_set_pagecache_ttl(client->context, 0);
359 client->cache_used = true;
360 #endif
361 #ifdef LIBNFS_FEATURE_DEBUG
362 } else if (!strcmp(qp->p[i].name, "debug")) {
363 /* limit the maximum debug level to avoid potential flooding
364 * of our log files. */
365 if (val > QEMU_NFS_MAX_DEBUG_LEVEL) {
366 error_report("NFS Warning: Limiting NFS debug level"
367 " to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
368 val = QEMU_NFS_MAX_DEBUG_LEVEL;
370 nfs_set_debug(client->context, val);
371 #endif
372 } else {
373 error_setg(errp, "Unknown NFS parameter name: %s",
374 qp->p[i].name);
375 goto fail;
379 ret = nfs_mount(client->context, uri->server, uri->path);
380 if (ret < 0) {
381 error_setg(errp, "Failed to mount nfs share: %s",
382 nfs_get_error(client->context));
383 goto fail;
386 if (flags & O_CREAT) {
387 ret = nfs_creat(client->context, file, 0600, &client->fh);
388 if (ret < 0) {
389 error_setg(errp, "Failed to create file: %s",
390 nfs_get_error(client->context));
391 goto fail;
393 } else {
394 ret = nfs_open(client->context, file, flags, &client->fh);
395 if (ret < 0) {
396 error_setg(errp, "Failed to open file : %s",
397 nfs_get_error(client->context));
398 goto fail;
402 ret = nfs_fstat(client->context, client->fh, &st);
403 if (ret < 0) {
404 error_setg(errp, "Failed to fstat file: %s",
405 nfs_get_error(client->context));
406 goto fail;
409 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
410 client->st_blocks = st.st_blocks;
411 client->has_zero_init = S_ISREG(st.st_mode);
412 goto out;
413 fail:
414 nfs_client_close(client);
415 out:
416 if (qp) {
417 query_params_free(qp);
419 uri_free(uri);
420 g_free(file);
421 return ret;
424 static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
425 Error **errp) {
426 NFSClient *client = bs->opaque;
427 int64_t ret;
428 QemuOpts *opts;
429 Error *local_err = NULL;
431 client->aio_context = bdrv_get_aio_context(bs);
433 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
434 qemu_opts_absorb_qdict(opts, options, &local_err);
435 if (local_err) {
436 error_propagate(errp, local_err);
437 ret = -EINVAL;
438 goto out;
440 ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
441 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
442 errp, bs->open_flags);
443 if (ret < 0) {
444 goto out;
446 bs->total_sectors = ret;
447 ret = 0;
448 out:
449 qemu_opts_del(opts);
450 return ret;
453 static QemuOptsList nfs_create_opts = {
454 .name = "nfs-create-opts",
455 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
456 .desc = {
458 .name = BLOCK_OPT_SIZE,
459 .type = QEMU_OPT_SIZE,
460 .help = "Virtual disk size"
462 { /* end of list */ }
466 static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
468 int ret = 0;
469 int64_t total_size = 0;
470 NFSClient *client = g_new0(NFSClient, 1);
472 client->aio_context = qemu_get_aio_context();
474 /* Read out options */
475 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
476 BDRV_SECTOR_SIZE);
478 ret = nfs_client_open(client, url, O_CREAT, errp, 0);
479 if (ret < 0) {
480 goto out;
482 ret = nfs_ftruncate(client->context, client->fh, total_size);
483 nfs_client_close(client);
484 out:
485 g_free(client);
486 return ret;
489 static int nfs_has_zero_init(BlockDriverState *bs)
491 NFSClient *client = bs->opaque;
492 return client->has_zero_init;
495 static void
496 nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
497 void *private_data)
499 NFSRPC *task = private_data;
500 task->ret = ret;
501 if (task->ret == 0) {
502 memcpy(task->st, data, sizeof(struct stat));
504 if (task->ret < 0) {
505 error_report("NFS Error: %s", nfs_get_error(nfs));
507 task->complete = 1;
508 bdrv_wakeup(task->bs);
511 static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
513 NFSClient *client = bs->opaque;
514 NFSRPC task = {0};
515 struct stat st;
517 if (bdrv_is_read_only(bs) &&
518 !(bs->open_flags & BDRV_O_NOCACHE)) {
519 return client->st_blocks * 512;
522 task.bs = bs;
523 task.st = &st;
524 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
525 &task) != 0) {
526 return -ENOMEM;
529 nfs_set_events(client);
530 BDRV_POLL_WHILE(bs, !task.complete);
532 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
535 static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
537 NFSClient *client = bs->opaque;
538 return nfs_ftruncate(client->context, client->fh, offset);
541 /* Note that this will not re-establish a connection with the NFS server
542 * - it is effectively a NOP. */
543 static int nfs_reopen_prepare(BDRVReopenState *state,
544 BlockReopenQueue *queue, Error **errp)
546 NFSClient *client = state->bs->opaque;
547 struct stat st;
548 int ret = 0;
550 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
551 error_setg(errp, "Cannot open a read-only mount as read-write");
552 return -EACCES;
555 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
556 error_setg(errp, "Cannot disable cache if libnfs readahead or"
557 " pagecache is enabled");
558 return -EINVAL;
561 /* Update cache for read-only reopens */
562 if (!(state->flags & BDRV_O_RDWR)) {
563 ret = nfs_fstat(client->context, client->fh, &st);
564 if (ret < 0) {
565 error_setg(errp, "Failed to fstat file: %s",
566 nfs_get_error(client->context));
567 return ret;
569 client->st_blocks = st.st_blocks;
572 return 0;
575 #ifdef LIBNFS_FEATURE_PAGECACHE
576 static void nfs_invalidate_cache(BlockDriverState *bs,
577 Error **errp)
579 NFSClient *client = bs->opaque;
580 nfs_pagecache_invalidate(client->context, client->fh);
582 #endif
584 static BlockDriver bdrv_nfs = {
585 .format_name = "nfs",
586 .protocol_name = "nfs",
588 .instance_size = sizeof(NFSClient),
589 .bdrv_needs_filename = true,
590 .create_opts = &nfs_create_opts,
592 .bdrv_has_zero_init = nfs_has_zero_init,
593 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
594 .bdrv_truncate = nfs_file_truncate,
596 .bdrv_file_open = nfs_file_open,
597 .bdrv_close = nfs_file_close,
598 .bdrv_create = nfs_file_create,
599 .bdrv_reopen_prepare = nfs_reopen_prepare,
601 .bdrv_co_readv = nfs_co_readv,
602 .bdrv_co_writev = nfs_co_writev,
603 .bdrv_co_flush_to_disk = nfs_co_flush,
605 .bdrv_detach_aio_context = nfs_detach_aio_context,
606 .bdrv_attach_aio_context = nfs_attach_aio_context,
608 #ifdef LIBNFS_FEATURE_PAGECACHE
609 .bdrv_invalidate_cache = nfs_invalidate_cache,
610 #endif
613 static void nfs_block_init(void)
615 bdrv_register(&bdrv_nfs);
618 block_init(nfs_block_init);