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(BlockDriverState
*bs
, NFSRPC
*task
)
97 .co
= qemu_coroutine_self(),
103 static void nfs_co_generic_bh_cb(void *opaque
)
105 NFSRPC
*task
= opaque
;
107 qemu_coroutine_enter(task
->co
);
111 nfs_co_generic_cb(int ret
, struct nfs_context
*nfs
, void *data
,
114 NFSRPC
*task
= private_data
;
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
);
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
,
135 NFSClient
*client
= bs
->opaque
;
138 nfs_co_init_task(bs
, &task
);
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) {
148 nfs_set_events(client
);
149 while (!task
.complete
) {
150 qemu_coroutine_yield();
157 /* zero pad short reads */
158 if (task
.ret
< iov
->size
) {
159 qemu_iovec_memset(iov
, task
.ret
, 0, iov
->size
- task
.ret
);
165 static int coroutine_fn
nfs_co_writev(BlockDriverState
*bs
,
166 int64_t sector_num
, int nb_sectors
,
169 NFSClient
*client
= bs
->opaque
;
173 nfs_co_init_task(bs
, &task
);
175 buf
= g_try_malloc(nb_sectors
* BDRV_SECTOR_SIZE
);
176 if (nb_sectors
&& buf
== NULL
) {
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) {
190 nfs_set_events(client
);
191 while (!task
.complete
) {
192 qemu_coroutine_yield();
197 if (task
.ret
!= nb_sectors
* BDRV_SECTOR_SIZE
) {
198 return task
.ret
< 0 ? task
.ret
: -EIO
;
204 static int coroutine_fn
nfs_co_flush(BlockDriverState
*bs
)
206 NFSClient
*client
= bs
->opaque
;
209 nfs_co_init_task(bs
, &task
);
211 if (nfs_fsync_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
216 nfs_set_events(client
);
217 while (!task
.complete
) {
218 qemu_coroutine_yield();
224 /* TODO Convert to fine grained options */
225 static QemuOptsList runtime_opts
= {
227 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
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
);
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
) {
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
;
281 QueryParams
*qp
= NULL
;
282 char *file
= NULL
, *strp
= NULL
;
284 uri
= uri_parse(filename
);
286 error_setg(errp
, "Invalid URL specified");
290 error_setg(errp
, "Invalid URL specified");
293 strp
= strrchr(uri
->path
, '/');
295 error_setg(errp
, "Invalid URL specified");
298 file
= g_strdup(strp
);
301 client
->context
= nfs_init_context();
302 if (client
->context
== NULL
) {
303 error_setg(errp
, "Failed to init NFS context");
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",
315 if (parse_uint_full(qp
->p
[i
].value
, &val
, 0)) {
316 error_setg(errp
, "Illegal value for NFS parameter: %s",
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");
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);
342 client
->cache_used
= true;
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");
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;
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
);
373 error_setg(errp
, "Unknown NFS parameter name: %s",
379 ret
= nfs_mount(client
->context
, uri
->server
, uri
->path
);
381 error_setg(errp
, "Failed to mount nfs share: %s",
382 nfs_get_error(client
->context
));
386 if (flags
& O_CREAT
) {
387 ret
= nfs_creat(client
->context
, file
, 0600, &client
->fh
);
389 error_setg(errp
, "Failed to create file: %s",
390 nfs_get_error(client
->context
));
394 ret
= nfs_open(client
->context
, file
, flags
, &client
->fh
);
396 error_setg(errp
, "Failed to open file : %s",
397 nfs_get_error(client
->context
));
402 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
404 error_setg(errp
, "Failed to fstat file: %s",
405 nfs_get_error(client
->context
));
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
);
414 nfs_client_close(client
);
417 query_params_free(qp
);
424 static int nfs_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
426 NFSClient
*client
= bs
->opaque
;
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
);
436 error_propagate(errp
, local_err
);
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
);
446 bs
->total_sectors
= ret
;
453 static QemuOptsList nfs_create_opts
= {
454 .name
= "nfs-create-opts",
455 .head
= QTAILQ_HEAD_INITIALIZER(nfs_create_opts
.head
),
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
)
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),
478 ret
= nfs_client_open(client
, url
, O_CREAT
, errp
, 0);
482 ret
= nfs_ftruncate(client
->context
, client
->fh
, total_size
);
483 nfs_client_close(client
);
489 static int nfs_has_zero_init(BlockDriverState
*bs
)
491 NFSClient
*client
= bs
->opaque
;
492 return client
->has_zero_init
;
496 nfs_get_allocated_file_size_cb(int ret
, struct nfs_context
*nfs
, void *data
,
499 NFSRPC
*task
= private_data
;
501 if (task
->ret
== 0) {
502 memcpy(task
->st
, data
, sizeof(struct stat
));
505 error_report("NFS Error: %s", nfs_get_error(nfs
));
508 bdrv_wakeup(task
->bs
);
511 static int64_t nfs_get_allocated_file_size(BlockDriverState
*bs
)
513 NFSClient
*client
= bs
->opaque
;
517 if (bdrv_is_read_only(bs
) &&
518 !(bs
->open_flags
& BDRV_O_NOCACHE
)) {
519 return client
->st_blocks
* 512;
524 if (nfs_fstat_async(client
->context
, client
->fh
, nfs_get_allocated_file_size_cb
,
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
;
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");
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");
561 /* Update cache for read-only reopens */
562 if (!(state
->flags
& BDRV_O_RDWR
)) {
563 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
565 error_setg(errp
, "Failed to fstat file: %s",
566 nfs_get_error(client
->context
));
569 client
->st_blocks
= st
.st_blocks
;
575 #ifdef LIBNFS_FEATURE_PAGECACHE
576 static void nfs_invalidate_cache(BlockDriverState
*bs
,
579 NFSClient
*client
= bs
->opaque
;
580 nfs_pagecache_invalidate(client
->context
, client
->fh
);
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
,
613 static void nfs_block_init(void)
615 bdrv_register(&bdrv_nfs
);
618 block_init(nfs_block_init
);