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
{
63 static void nfs_process_read(void *arg
);
64 static void nfs_process_write(void *arg
);
66 static void nfs_set_events(NFSClient
*client
)
68 int ev
= nfs_which_events(client
->context
);
69 if (ev
!= client
->events
) {
70 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
72 (ev
& POLLIN
) ? nfs_process_read
: NULL
,
73 (ev
& POLLOUT
) ? nfs_process_write
: NULL
, client
);
79 static void nfs_process_read(void *arg
)
81 NFSClient
*client
= arg
;
82 nfs_service(client
->context
, POLLIN
);
83 nfs_set_events(client
);
86 static void nfs_process_write(void *arg
)
88 NFSClient
*client
= arg
;
89 nfs_service(client
->context
, POLLOUT
);
90 nfs_set_events(client
);
93 static void nfs_co_init_task(NFSClient
*client
, NFSRPC
*task
)
96 .co
= qemu_coroutine_self(),
101 static void nfs_co_generic_bh_cb(void *opaque
)
103 NFSRPC
*task
= opaque
;
105 qemu_coroutine_enter(task
->co
);
109 nfs_co_generic_cb(int ret
, struct nfs_context
*nfs
, void *data
,
112 NFSRPC
*task
= private_data
;
114 if (task
->ret
> 0 && task
->iov
) {
115 if (task
->ret
<= task
->iov
->size
) {
116 qemu_iovec_from_buf(task
->iov
, 0, data
, task
->ret
);
121 if (task
->ret
== 0 && task
->st
) {
122 memcpy(task
->st
, data
, sizeof(struct stat
));
125 error_report("NFS Error: %s", nfs_get_error(nfs
));
128 aio_bh_schedule_oneshot(task
->client
->aio_context
,
129 nfs_co_generic_bh_cb
, task
);
135 static int coroutine_fn
nfs_co_readv(BlockDriverState
*bs
,
136 int64_t sector_num
, int nb_sectors
,
139 NFSClient
*client
= bs
->opaque
;
142 nfs_co_init_task(client
, &task
);
145 if (nfs_pread_async(client
->context
, client
->fh
,
146 sector_num
* BDRV_SECTOR_SIZE
,
147 nb_sectors
* BDRV_SECTOR_SIZE
,
148 nfs_co_generic_cb
, &task
) != 0) {
152 while (!task
.complete
) {
153 nfs_set_events(client
);
154 qemu_coroutine_yield();
161 /* zero pad short reads */
162 if (task
.ret
< iov
->size
) {
163 qemu_iovec_memset(iov
, task
.ret
, 0, iov
->size
- task
.ret
);
169 static int coroutine_fn
nfs_co_writev(BlockDriverState
*bs
,
170 int64_t sector_num
, int nb_sectors
,
173 NFSClient
*client
= bs
->opaque
;
177 nfs_co_init_task(client
, &task
);
179 buf
= g_try_malloc(nb_sectors
* BDRV_SECTOR_SIZE
);
180 if (nb_sectors
&& buf
== NULL
) {
184 qemu_iovec_to_buf(iov
, 0, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
186 if (nfs_pwrite_async(client
->context
, client
->fh
,
187 sector_num
* BDRV_SECTOR_SIZE
,
188 nb_sectors
* BDRV_SECTOR_SIZE
,
189 buf
, nfs_co_generic_cb
, &task
) != 0) {
194 while (!task
.complete
) {
195 nfs_set_events(client
);
196 qemu_coroutine_yield();
201 if (task
.ret
!= nb_sectors
* BDRV_SECTOR_SIZE
) {
202 return task
.ret
< 0 ? task
.ret
: -EIO
;
208 static int coroutine_fn
nfs_co_flush(BlockDriverState
*bs
)
210 NFSClient
*client
= bs
->opaque
;
213 nfs_co_init_task(client
, &task
);
215 if (nfs_fsync_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
220 while (!task
.complete
) {
221 nfs_set_events(client
);
222 qemu_coroutine_yield();
228 /* TODO Convert to fine grained options */
229 static QemuOptsList runtime_opts
= {
231 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
235 .type
= QEMU_OPT_STRING
,
236 .help
= "URL to the NFS file",
238 { /* end of list */ }
242 static void nfs_detach_aio_context(BlockDriverState
*bs
)
244 NFSClient
*client
= bs
->opaque
;
246 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
247 false, NULL
, NULL
, NULL
);
251 static void nfs_attach_aio_context(BlockDriverState
*bs
,
252 AioContext
*new_context
)
254 NFSClient
*client
= bs
->opaque
;
256 client
->aio_context
= new_context
;
257 nfs_set_events(client
);
260 static void nfs_client_close(NFSClient
*client
)
262 if (client
->context
) {
264 nfs_close(client
->context
, client
->fh
);
266 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
267 false, NULL
, NULL
, NULL
);
268 nfs_destroy_context(client
->context
);
270 memset(client
, 0, sizeof(NFSClient
));
273 static void nfs_file_close(BlockDriverState
*bs
)
275 NFSClient
*client
= bs
->opaque
;
276 nfs_client_close(client
);
279 static int64_t nfs_client_open(NFSClient
*client
, const char *filename
,
280 int flags
, Error
**errp
, int open_flags
)
282 int ret
= -EINVAL
, i
;
285 QueryParams
*qp
= NULL
;
286 char *file
= NULL
, *strp
= NULL
;
288 uri
= uri_parse(filename
);
290 error_setg(errp
, "Invalid URL specified");
294 error_setg(errp
, "Invalid URL specified");
297 strp
= strrchr(uri
->path
, '/');
299 error_setg(errp
, "Invalid URL specified");
302 file
= g_strdup(strp
);
305 client
->context
= nfs_init_context();
306 if (client
->context
== NULL
) {
307 error_setg(errp
, "Failed to init NFS context");
311 qp
= query_params_parse(uri
->query
);
312 for (i
= 0; i
< qp
->n
; i
++) {
313 unsigned long long val
;
314 if (!qp
->p
[i
].value
) {
315 error_setg(errp
, "Value for NFS parameter expected: %s",
319 if (parse_uint_full(qp
->p
[i
].value
, &val
, 0)) {
320 error_setg(errp
, "Illegal value for NFS parameter: %s",
324 if (!strcmp(qp
->p
[i
].name
, "uid")) {
325 nfs_set_uid(client
->context
, val
);
326 } else if (!strcmp(qp
->p
[i
].name
, "gid")) {
327 nfs_set_gid(client
->context
, val
);
328 } else if (!strcmp(qp
->p
[i
].name
, "tcp-syncnt")) {
329 nfs_set_tcp_syncnt(client
->context
, val
);
330 #ifdef LIBNFS_FEATURE_READAHEAD
331 } else if (!strcmp(qp
->p
[i
].name
, "readahead")) {
332 if (open_flags
& BDRV_O_NOCACHE
) {
333 error_setg(errp
, "Cannot enable NFS readahead "
334 "if cache.direct = on");
337 if (val
> QEMU_NFS_MAX_READAHEAD_SIZE
) {
338 error_report("NFS Warning: Truncating NFS readahead"
339 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE
);
340 val
= QEMU_NFS_MAX_READAHEAD_SIZE
;
342 nfs_set_readahead(client
->context
, val
);
343 #ifdef LIBNFS_FEATURE_PAGECACHE
344 nfs_set_pagecache_ttl(client
->context
, 0);
346 client
->cache_used
= true;
348 #ifdef LIBNFS_FEATURE_PAGECACHE
349 nfs_set_pagecache_ttl(client
->context
, 0);
350 } else if (!strcmp(qp
->p
[i
].name
, "pagecache")) {
351 if (open_flags
& BDRV_O_NOCACHE
) {
352 error_setg(errp
, "Cannot enable NFS pagecache "
353 "if cache.direct = on");
356 if (val
> QEMU_NFS_MAX_PAGECACHE_SIZE
) {
357 error_report("NFS Warning: Truncating NFS pagecache"
358 " size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE
);
359 val
= QEMU_NFS_MAX_PAGECACHE_SIZE
;
361 nfs_set_pagecache(client
->context
, val
);
362 nfs_set_pagecache_ttl(client
->context
, 0);
363 client
->cache_used
= true;
365 #ifdef LIBNFS_FEATURE_DEBUG
366 } else if (!strcmp(qp
->p
[i
].name
, "debug")) {
367 /* limit the maximum debug level to avoid potential flooding
368 * of our log files. */
369 if (val
> QEMU_NFS_MAX_DEBUG_LEVEL
) {
370 error_report("NFS Warning: Limiting NFS debug level"
371 " to %d", QEMU_NFS_MAX_DEBUG_LEVEL
);
372 val
= QEMU_NFS_MAX_DEBUG_LEVEL
;
374 nfs_set_debug(client
->context
, val
);
377 error_setg(errp
, "Unknown NFS parameter name: %s",
383 ret
= nfs_mount(client
->context
, uri
->server
, uri
->path
);
385 error_setg(errp
, "Failed to mount nfs share: %s",
386 nfs_get_error(client
->context
));
390 if (flags
& O_CREAT
) {
391 ret
= nfs_creat(client
->context
, file
, 0600, &client
->fh
);
393 error_setg(errp
, "Failed to create file: %s",
394 nfs_get_error(client
->context
));
398 ret
= nfs_open(client
->context
, file
, flags
, &client
->fh
);
400 error_setg(errp
, "Failed to open file : %s",
401 nfs_get_error(client
->context
));
406 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
408 error_setg(errp
, "Failed to fstat file: %s",
409 nfs_get_error(client
->context
));
413 ret
= DIV_ROUND_UP(st
.st_size
, BDRV_SECTOR_SIZE
);
414 client
->st_blocks
= st
.st_blocks
;
415 client
->has_zero_init
= S_ISREG(st
.st_mode
);
418 nfs_client_close(client
);
421 query_params_free(qp
);
428 static int nfs_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
430 NFSClient
*client
= bs
->opaque
;
433 Error
*local_err
= NULL
;
435 client
->aio_context
= bdrv_get_aio_context(bs
);
437 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
438 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
440 error_propagate(errp
, local_err
);
444 ret
= nfs_client_open(client
, qemu_opt_get(opts
, "filename"),
445 (flags
& BDRV_O_RDWR
) ? O_RDWR
: O_RDONLY
,
446 errp
, bs
->open_flags
);
450 bs
->total_sectors
= ret
;
457 static QemuOptsList nfs_create_opts
= {
458 .name
= "nfs-create-opts",
459 .head
= QTAILQ_HEAD_INITIALIZER(nfs_create_opts
.head
),
462 .name
= BLOCK_OPT_SIZE
,
463 .type
= QEMU_OPT_SIZE
,
464 .help
= "Virtual disk size"
466 { /* end of list */ }
470 static int nfs_file_create(const char *url
, QemuOpts
*opts
, Error
**errp
)
473 int64_t total_size
= 0;
474 NFSClient
*client
= g_new0(NFSClient
, 1);
476 client
->aio_context
= qemu_get_aio_context();
478 /* Read out options */
479 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
482 ret
= nfs_client_open(client
, url
, O_CREAT
, errp
, 0);
486 ret
= nfs_ftruncate(client
->context
, client
->fh
, total_size
);
487 nfs_client_close(client
);
493 static int nfs_has_zero_init(BlockDriverState
*bs
)
495 NFSClient
*client
= bs
->opaque
;
496 return client
->has_zero_init
;
499 static int64_t nfs_get_allocated_file_size(BlockDriverState
*bs
)
501 NFSClient
*client
= bs
->opaque
;
505 if (bdrv_is_read_only(bs
) &&
506 !(bs
->open_flags
& BDRV_O_NOCACHE
)) {
507 return client
->st_blocks
* 512;
511 if (nfs_fstat_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
516 while (!task
.complete
) {
517 nfs_set_events(client
);
518 aio_poll(client
->aio_context
, true);
521 return (task
.ret
< 0 ? task
.ret
: st
.st_blocks
* 512);
524 static int nfs_file_truncate(BlockDriverState
*bs
, int64_t offset
)
526 NFSClient
*client
= bs
->opaque
;
527 return nfs_ftruncate(client
->context
, client
->fh
, offset
);
530 /* Note that this will not re-establish a connection with the NFS server
531 * - it is effectively a NOP. */
532 static int nfs_reopen_prepare(BDRVReopenState
*state
,
533 BlockReopenQueue
*queue
, Error
**errp
)
535 NFSClient
*client
= state
->bs
->opaque
;
539 if (state
->flags
& BDRV_O_RDWR
&& bdrv_is_read_only(state
->bs
)) {
540 error_setg(errp
, "Cannot open a read-only mount as read-write");
544 if ((state
->flags
& BDRV_O_NOCACHE
) && client
->cache_used
) {
545 error_setg(errp
, "Cannot disable cache if libnfs readahead or"
546 " pagecache is enabled");
550 /* Update cache for read-only reopens */
551 if (!(state
->flags
& BDRV_O_RDWR
)) {
552 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
554 error_setg(errp
, "Failed to fstat file: %s",
555 nfs_get_error(client
->context
));
558 client
->st_blocks
= st
.st_blocks
;
564 #ifdef LIBNFS_FEATURE_PAGECACHE
565 static void nfs_invalidate_cache(BlockDriverState
*bs
,
568 NFSClient
*client
= bs
->opaque
;
569 nfs_pagecache_invalidate(client
->context
, client
->fh
);
573 static BlockDriver bdrv_nfs
= {
574 .format_name
= "nfs",
575 .protocol_name
= "nfs",
577 .instance_size
= sizeof(NFSClient
),
578 .bdrv_needs_filename
= true,
579 .create_opts
= &nfs_create_opts
,
581 .bdrv_has_zero_init
= nfs_has_zero_init
,
582 .bdrv_get_allocated_file_size
= nfs_get_allocated_file_size
,
583 .bdrv_truncate
= nfs_file_truncate
,
585 .bdrv_file_open
= nfs_file_open
,
586 .bdrv_close
= nfs_file_close
,
587 .bdrv_create
= nfs_file_create
,
588 .bdrv_reopen_prepare
= nfs_reopen_prepare
,
590 .bdrv_co_readv
= nfs_co_readv
,
591 .bdrv_co_writev
= nfs_co_writev
,
592 .bdrv_co_flush_to_disk
= nfs_co_flush
,
594 .bdrv_detach_aio_context
= nfs_detach_aio_context
,
595 .bdrv_attach_aio_context
= nfs_attach_aio_context
,
597 #ifdef LIBNFS_FEATURE_PAGECACHE
598 .bdrv_invalidate_cache
= nfs_invalidate_cache
,
602 static void nfs_block_init(void)
604 bdrv_register(&bdrv_nfs
);
607 block_init(nfs_block_init
);