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 <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
;
49 AioContext
*aio_context
;
54 typedef struct 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
),
73 (ev
& POLLIN
) ? nfs_process_read
: NULL
,
74 (ev
& POLLOUT
) ? nfs_process_write
: NULL
, client
);
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(NFSClient
*client
, NFSRPC
*task
)
97 .co
= qemu_coroutine_self(),
102 static void nfs_co_generic_bh_cb(void *opaque
)
104 NFSRPC
*task
= opaque
;
106 qemu_bh_delete(task
->bh
);
107 qemu_coroutine_enter(task
->co
, NULL
);
111 nfs_co_generic_cb(int ret
, struct nfs_context
*nfs
, void *data
,
114 NFSRPC
*task
= private_data
;
116 if (task
->ret
> 0 && task
->iov
) {
117 if (task
->ret
<= task
->iov
->size
) {
118 qemu_iovec_from_buf(task
->iov
, 0, data
, task
->ret
);
123 if (task
->ret
== 0 && task
->st
) {
124 memcpy(task
->st
, data
, sizeof(struct stat
));
127 error_report("NFS Error: %s", nfs_get_error(nfs
));
130 task
->bh
= aio_bh_new(task
->client
->aio_context
,
131 nfs_co_generic_bh_cb
, task
);
132 qemu_bh_schedule(task
->bh
);
138 static int coroutine_fn
nfs_co_readv(BlockDriverState
*bs
,
139 int64_t sector_num
, int nb_sectors
,
142 NFSClient
*client
= bs
->opaque
;
145 nfs_co_init_task(client
, &task
);
148 if (nfs_pread_async(client
->context
, client
->fh
,
149 sector_num
* BDRV_SECTOR_SIZE
,
150 nb_sectors
* BDRV_SECTOR_SIZE
,
151 nfs_co_generic_cb
, &task
) != 0) {
155 while (!task
.complete
) {
156 nfs_set_events(client
);
157 qemu_coroutine_yield();
164 /* zero pad short reads */
165 if (task
.ret
< iov
->size
) {
166 qemu_iovec_memset(iov
, task
.ret
, 0, iov
->size
- task
.ret
);
172 static int coroutine_fn
nfs_co_writev(BlockDriverState
*bs
,
173 int64_t sector_num
, int nb_sectors
,
176 NFSClient
*client
= bs
->opaque
;
180 nfs_co_init_task(client
, &task
);
182 buf
= g_try_malloc(nb_sectors
* BDRV_SECTOR_SIZE
);
183 if (nb_sectors
&& buf
== NULL
) {
187 qemu_iovec_to_buf(iov
, 0, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
189 if (nfs_pwrite_async(client
->context
, client
->fh
,
190 sector_num
* BDRV_SECTOR_SIZE
,
191 nb_sectors
* BDRV_SECTOR_SIZE
,
192 buf
, nfs_co_generic_cb
, &task
) != 0) {
197 while (!task
.complete
) {
198 nfs_set_events(client
);
199 qemu_coroutine_yield();
204 if (task
.ret
!= nb_sectors
* BDRV_SECTOR_SIZE
) {
205 return task
.ret
< 0 ? task
.ret
: -EIO
;
211 static int coroutine_fn
nfs_co_flush(BlockDriverState
*bs
)
213 NFSClient
*client
= bs
->opaque
;
216 nfs_co_init_task(client
, &task
);
218 if (nfs_fsync_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
223 while (!task
.complete
) {
224 nfs_set_events(client
);
225 qemu_coroutine_yield();
231 /* TODO Convert to fine grained options */
232 static QemuOptsList runtime_opts
= {
234 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
238 .type
= QEMU_OPT_STRING
,
239 .help
= "URL to the NFS file",
241 { /* end of list */ }
245 static void nfs_detach_aio_context(BlockDriverState
*bs
)
247 NFSClient
*client
= bs
->opaque
;
249 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
250 false, NULL
, NULL
, NULL
);
254 static void nfs_attach_aio_context(BlockDriverState
*bs
,
255 AioContext
*new_context
)
257 NFSClient
*client
= bs
->opaque
;
259 client
->aio_context
= new_context
;
260 nfs_set_events(client
);
263 static void nfs_client_close(NFSClient
*client
)
265 if (client
->context
) {
267 nfs_close(client
->context
, client
->fh
);
269 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
270 false, NULL
, NULL
, NULL
);
271 nfs_destroy_context(client
->context
);
273 memset(client
, 0, sizeof(NFSClient
));
276 static void nfs_file_close(BlockDriverState
*bs
)
278 NFSClient
*client
= bs
->opaque
;
279 nfs_client_close(client
);
282 static int64_t nfs_client_open(NFSClient
*client
, const char *filename
,
283 int flags
, Error
**errp
, int open_flags
)
285 int ret
= -EINVAL
, i
;
288 QueryParams
*qp
= NULL
;
289 char *file
= NULL
, *strp
= NULL
;
291 uri
= uri_parse(filename
);
293 error_setg(errp
, "Invalid URL specified");
297 error_setg(errp
, "Invalid URL specified");
300 strp
= strrchr(uri
->path
, '/');
302 error_setg(errp
, "Invalid URL specified");
305 file
= g_strdup(strp
);
308 client
->context
= nfs_init_context();
309 if (client
->context
== NULL
) {
310 error_setg(errp
, "Failed to init NFS context");
314 qp
= query_params_parse(uri
->query
);
315 for (i
= 0; i
< qp
->n
; i
++) {
316 unsigned long long val
;
317 if (!qp
->p
[i
].value
) {
318 error_setg(errp
, "Value for NFS parameter expected: %s",
322 if (parse_uint_full(qp
->p
[i
].value
, &val
, 0)) {
323 error_setg(errp
, "Illegal value for NFS parameter: %s",
327 if (!strcmp(qp
->p
[i
].name
, "uid")) {
328 nfs_set_uid(client
->context
, val
);
329 } else if (!strcmp(qp
->p
[i
].name
, "gid")) {
330 nfs_set_gid(client
->context
, val
);
331 } else if (!strcmp(qp
->p
[i
].name
, "tcp-syncnt")) {
332 nfs_set_tcp_syncnt(client
->context
, val
);
333 #ifdef LIBNFS_FEATURE_READAHEAD
334 } else if (!strcmp(qp
->p
[i
].name
, "readahead")) {
335 if (open_flags
& BDRV_O_NOCACHE
) {
336 error_setg(errp
, "Cannot enable NFS readahead "
337 "if cache.direct = on");
340 if (val
> QEMU_NFS_MAX_READAHEAD_SIZE
) {
341 error_report("NFS Warning: Truncating NFS readahead"
342 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE
);
343 val
= QEMU_NFS_MAX_READAHEAD_SIZE
;
345 nfs_set_readahead(client
->context
, val
);
346 #ifdef LIBNFS_FEATURE_PAGECACHE
347 nfs_set_pagecache_ttl(client
->context
, 0);
349 client
->cache_used
= true;
351 #ifdef LIBNFS_FEATURE_PAGECACHE
352 nfs_set_pagecache_ttl(client
->context
, 0);
353 } else if (!strcmp(qp
->p
[i
].name
, "pagecache")) {
354 if (open_flags
& BDRV_O_NOCACHE
) {
355 error_setg(errp
, "Cannot enable NFS pagecache "
356 "if cache.direct = on");
359 if (val
> QEMU_NFS_MAX_PAGECACHE_SIZE
) {
360 error_report("NFS Warning: Truncating NFS pagecache"
361 " size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE
);
362 val
= QEMU_NFS_MAX_PAGECACHE_SIZE
;
364 nfs_set_pagecache(client
->context
, val
);
365 nfs_set_pagecache_ttl(client
->context
, 0);
366 client
->cache_used
= true;
368 #ifdef LIBNFS_FEATURE_DEBUG
369 } else if (!strcmp(qp
->p
[i
].name
, "debug")) {
370 /* limit the maximum debug level to avoid potential flooding
371 * of our log files. */
372 if (val
> QEMU_NFS_MAX_DEBUG_LEVEL
) {
373 error_report("NFS Warning: Limiting NFS debug level"
374 " to %d", QEMU_NFS_MAX_DEBUG_LEVEL
);
375 val
= QEMU_NFS_MAX_DEBUG_LEVEL
;
377 nfs_set_debug(client
->context
, val
);
380 error_setg(errp
, "Unknown NFS parameter name: %s",
386 ret
= nfs_mount(client
->context
, uri
->server
, uri
->path
);
388 error_setg(errp
, "Failed to mount nfs share: %s",
389 nfs_get_error(client
->context
));
393 if (flags
& O_CREAT
) {
394 ret
= nfs_creat(client
->context
, file
, 0600, &client
->fh
);
396 error_setg(errp
, "Failed to create file: %s",
397 nfs_get_error(client
->context
));
401 ret
= nfs_open(client
->context
, file
, flags
, &client
->fh
);
403 error_setg(errp
, "Failed to open file : %s",
404 nfs_get_error(client
->context
));
409 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
411 error_setg(errp
, "Failed to fstat file: %s",
412 nfs_get_error(client
->context
));
416 ret
= DIV_ROUND_UP(st
.st_size
, BDRV_SECTOR_SIZE
);
417 client
->st_blocks
= st
.st_blocks
;
418 client
->has_zero_init
= S_ISREG(st
.st_mode
);
421 nfs_client_close(client
);
424 query_params_free(qp
);
431 static int nfs_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
433 NFSClient
*client
= bs
->opaque
;
436 Error
*local_err
= NULL
;
438 client
->aio_context
= bdrv_get_aio_context(bs
);
440 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
441 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
443 error_propagate(errp
, local_err
);
447 ret
= nfs_client_open(client
, qemu_opt_get(opts
, "filename"),
448 (flags
& BDRV_O_RDWR
) ? O_RDWR
: O_RDONLY
,
449 errp
, bs
->open_flags
);
453 bs
->total_sectors
= ret
;
460 static QemuOptsList nfs_create_opts
= {
461 .name
= "nfs-create-opts",
462 .head
= QTAILQ_HEAD_INITIALIZER(nfs_create_opts
.head
),
465 .name
= BLOCK_OPT_SIZE
,
466 .type
= QEMU_OPT_SIZE
,
467 .help
= "Virtual disk size"
469 { /* end of list */ }
473 static int nfs_file_create(const char *url
, QemuOpts
*opts
, Error
**errp
)
476 int64_t total_size
= 0;
477 NFSClient
*client
= g_new0(NFSClient
, 1);
479 client
->aio_context
= qemu_get_aio_context();
481 /* Read out options */
482 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
485 ret
= nfs_client_open(client
, url
, O_CREAT
, errp
, 0);
489 ret
= nfs_ftruncate(client
->context
, client
->fh
, total_size
);
490 nfs_client_close(client
);
496 static int nfs_has_zero_init(BlockDriverState
*bs
)
498 NFSClient
*client
= bs
->opaque
;
499 return client
->has_zero_init
;
502 static int64_t nfs_get_allocated_file_size(BlockDriverState
*bs
)
504 NFSClient
*client
= bs
->opaque
;
508 if (bdrv_is_read_only(bs
) &&
509 !(bs
->open_flags
& BDRV_O_NOCACHE
)) {
510 return client
->st_blocks
* 512;
514 if (nfs_fstat_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
519 while (!task
.complete
) {
520 nfs_set_events(client
);
521 aio_poll(client
->aio_context
, true);
524 return (task
.ret
< 0 ? task
.ret
: st
.st_blocks
* 512);
527 static int nfs_file_truncate(BlockDriverState
*bs
, int64_t offset
)
529 NFSClient
*client
= bs
->opaque
;
530 return nfs_ftruncate(client
->context
, client
->fh
, offset
);
533 /* Note that this will not re-establish a connection with the NFS server
534 * - it is effectively a NOP. */
535 static int nfs_reopen_prepare(BDRVReopenState
*state
,
536 BlockReopenQueue
*queue
, Error
**errp
)
538 NFSClient
*client
= state
->bs
->opaque
;
542 if (state
->flags
& BDRV_O_RDWR
&& bdrv_is_read_only(state
->bs
)) {
543 error_setg(errp
, "Cannot open a read-only mount as read-write");
547 if ((state
->flags
& BDRV_O_NOCACHE
) && client
->cache_used
) {
548 error_setg(errp
, "Cannot disable cache if libnfs readahead or"
549 " pagecache is enabled");
553 /* Update cache for read-only reopens */
554 if (!(state
->flags
& BDRV_O_RDWR
)) {
555 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
557 error_setg(errp
, "Failed to fstat file: %s",
558 nfs_get_error(client
->context
));
561 client
->st_blocks
= st
.st_blocks
;
567 #ifdef LIBNFS_FEATURE_PAGECACHE
568 static void nfs_invalidate_cache(BlockDriverState
*bs
,
571 NFSClient
*client
= bs
->opaque
;
572 nfs_pagecache_invalidate(client
->context
, client
->fh
);
576 static BlockDriver bdrv_nfs
= {
577 .format_name
= "nfs",
578 .protocol_name
= "nfs",
580 .instance_size
= sizeof(NFSClient
),
581 .bdrv_needs_filename
= true,
582 .create_opts
= &nfs_create_opts
,
584 .bdrv_has_zero_init
= nfs_has_zero_init
,
585 .bdrv_get_allocated_file_size
= nfs_get_allocated_file_size
,
586 .bdrv_truncate
= nfs_file_truncate
,
588 .bdrv_file_open
= nfs_file_open
,
589 .bdrv_close
= nfs_file_close
,
590 .bdrv_create
= nfs_file_create
,
591 .bdrv_reopen_prepare
= nfs_reopen_prepare
,
593 .bdrv_co_readv
= nfs_co_readv
,
594 .bdrv_co_writev
= nfs_co_writev
,
595 .bdrv_co_flush_to_disk
= nfs_co_flush
,
597 .bdrv_detach_aio_context
= nfs_detach_aio_context
,
598 .bdrv_attach_aio_context
= nfs_attach_aio_context
,
600 #ifdef LIBNFS_FEATURE_PAGECACHE
601 .bdrv_invalidate_cache
= nfs_invalidate_cache
,
605 static void nfs_block_init(void)
607 bdrv_register(&bdrv_nfs
);
610 block_init(nfs_block_init
);