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_DEBUG_LEVEL 2
43 typedef struct NFSClient
{
44 struct nfs_context
*context
;
48 AioContext
*aio_context
;
53 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_bh_delete(task
->bh
);
106 qemu_coroutine_enter(task
->co
, NULL
);
110 nfs_co_generic_cb(int ret
, struct nfs_context
*nfs
, void *data
,
113 NFSRPC
*task
= private_data
;
115 if (task
->ret
> 0 && task
->iov
) {
116 if (task
->ret
<= task
->iov
->size
) {
117 qemu_iovec_from_buf(task
->iov
, 0, data
, task
->ret
);
122 if (task
->ret
== 0 && task
->st
) {
123 memcpy(task
->st
, data
, sizeof(struct stat
));
126 error_report("NFS Error: %s", nfs_get_error(nfs
));
129 task
->bh
= aio_bh_new(task
->client
->aio_context
,
130 nfs_co_generic_bh_cb
, task
);
131 qemu_bh_schedule(task
->bh
);
137 static int coroutine_fn
nfs_co_readv(BlockDriverState
*bs
,
138 int64_t sector_num
, int nb_sectors
,
141 NFSClient
*client
= bs
->opaque
;
144 nfs_co_init_task(client
, &task
);
147 if (nfs_pread_async(client
->context
, client
->fh
,
148 sector_num
* BDRV_SECTOR_SIZE
,
149 nb_sectors
* BDRV_SECTOR_SIZE
,
150 nfs_co_generic_cb
, &task
) != 0) {
154 while (!task
.complete
) {
155 nfs_set_events(client
);
156 qemu_coroutine_yield();
163 /* zero pad short reads */
164 if (task
.ret
< iov
->size
) {
165 qemu_iovec_memset(iov
, task
.ret
, 0, iov
->size
- task
.ret
);
171 static int coroutine_fn
nfs_co_writev(BlockDriverState
*bs
,
172 int64_t sector_num
, int nb_sectors
,
175 NFSClient
*client
= bs
->opaque
;
179 nfs_co_init_task(client
, &task
);
181 buf
= g_try_malloc(nb_sectors
* BDRV_SECTOR_SIZE
);
182 if (nb_sectors
&& buf
== NULL
) {
186 qemu_iovec_to_buf(iov
, 0, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
188 if (nfs_pwrite_async(client
->context
, client
->fh
,
189 sector_num
* BDRV_SECTOR_SIZE
,
190 nb_sectors
* BDRV_SECTOR_SIZE
,
191 buf
, nfs_co_generic_cb
, &task
) != 0) {
196 while (!task
.complete
) {
197 nfs_set_events(client
);
198 qemu_coroutine_yield();
203 if (task
.ret
!= nb_sectors
* BDRV_SECTOR_SIZE
) {
204 return task
.ret
< 0 ? task
.ret
: -EIO
;
210 static int coroutine_fn
nfs_co_flush(BlockDriverState
*bs
)
212 NFSClient
*client
= bs
->opaque
;
215 nfs_co_init_task(client
, &task
);
217 if (nfs_fsync_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
222 while (!task
.complete
) {
223 nfs_set_events(client
);
224 qemu_coroutine_yield();
230 /* TODO Convert to fine grained options */
231 static QemuOptsList runtime_opts
= {
233 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
237 .type
= QEMU_OPT_STRING
,
238 .help
= "URL to the NFS file",
240 { /* end of list */ }
244 static void nfs_detach_aio_context(BlockDriverState
*bs
)
246 NFSClient
*client
= bs
->opaque
;
248 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
249 false, NULL
, NULL
, NULL
);
253 static void nfs_attach_aio_context(BlockDriverState
*bs
,
254 AioContext
*new_context
)
256 NFSClient
*client
= bs
->opaque
;
258 client
->aio_context
= new_context
;
259 nfs_set_events(client
);
262 static void nfs_client_close(NFSClient
*client
)
264 if (client
->context
) {
266 nfs_close(client
->context
, client
->fh
);
268 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
269 false, NULL
, NULL
, NULL
);
270 nfs_destroy_context(client
->context
);
272 memset(client
, 0, sizeof(NFSClient
));
275 static void nfs_file_close(BlockDriverState
*bs
)
277 NFSClient
*client
= bs
->opaque
;
278 nfs_client_close(client
);
281 static int64_t nfs_client_open(NFSClient
*client
, const char *filename
,
282 int flags
, Error
**errp
, int open_flags
)
284 int ret
= -EINVAL
, i
;
287 QueryParams
*qp
= NULL
;
288 char *file
= NULL
, *strp
= NULL
;
290 uri
= uri_parse(filename
);
292 error_setg(errp
, "Invalid URL specified");
296 error_setg(errp
, "Invalid URL specified");
299 strp
= strrchr(uri
->path
, '/');
301 error_setg(errp
, "Invalid URL specified");
304 file
= g_strdup(strp
);
307 client
->context
= nfs_init_context();
308 if (client
->context
== NULL
) {
309 error_setg(errp
, "Failed to init NFS context");
313 qp
= query_params_parse(uri
->query
);
314 for (i
= 0; i
< qp
->n
; i
++) {
315 unsigned long long val
;
316 if (!qp
->p
[i
].value
) {
317 error_setg(errp
, "Value for NFS parameter expected: %s",
321 if (parse_uint_full(qp
->p
[i
].value
, &val
, 0)) {
322 error_setg(errp
, "Illegal value for NFS parameter: %s",
326 if (!strcmp(qp
->p
[i
].name
, "uid")) {
327 nfs_set_uid(client
->context
, val
);
328 } else if (!strcmp(qp
->p
[i
].name
, "gid")) {
329 nfs_set_gid(client
->context
, val
);
330 } else if (!strcmp(qp
->p
[i
].name
, "tcp-syncnt")) {
331 nfs_set_tcp_syncnt(client
->context
, val
);
332 #ifdef LIBNFS_FEATURE_READAHEAD
333 } else if (!strcmp(qp
->p
[i
].name
, "readahead")) {
334 if (open_flags
& BDRV_O_NOCACHE
) {
335 error_setg(errp
, "Cannot enable NFS readahead "
336 "if cache.direct = on");
339 if (val
> QEMU_NFS_MAX_READAHEAD_SIZE
) {
340 error_report("NFS Warning: Truncating NFS readahead"
341 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE
);
342 val
= QEMU_NFS_MAX_READAHEAD_SIZE
;
344 nfs_set_readahead(client
->context
, val
);
345 client
->cache_used
= true;
347 #ifdef LIBNFS_FEATURE_DEBUG
348 } else if (!strcmp(qp
->p
[i
].name
, "debug")) {
349 /* limit the maximum debug level to avoid potential flooding
350 * of our log files. */
351 if (val
> QEMU_NFS_MAX_DEBUG_LEVEL
) {
352 error_report("NFS Warning: Limiting NFS debug level"
353 " to %d", QEMU_NFS_MAX_DEBUG_LEVEL
);
354 val
= QEMU_NFS_MAX_DEBUG_LEVEL
;
356 nfs_set_debug(client
->context
, val
);
359 error_setg(errp
, "Unknown NFS parameter name: %s",
365 ret
= nfs_mount(client
->context
, uri
->server
, uri
->path
);
367 error_setg(errp
, "Failed to mount nfs share: %s",
368 nfs_get_error(client
->context
));
372 if (flags
& O_CREAT
) {
373 ret
= nfs_creat(client
->context
, file
, 0600, &client
->fh
);
375 error_setg(errp
, "Failed to create file: %s",
376 nfs_get_error(client
->context
));
380 ret
= nfs_open(client
->context
, file
, flags
, &client
->fh
);
382 error_setg(errp
, "Failed to open file : %s",
383 nfs_get_error(client
->context
));
388 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
390 error_setg(errp
, "Failed to fstat file: %s",
391 nfs_get_error(client
->context
));
395 ret
= DIV_ROUND_UP(st
.st_size
, BDRV_SECTOR_SIZE
);
396 client
->st_blocks
= st
.st_blocks
;
397 client
->has_zero_init
= S_ISREG(st
.st_mode
);
400 nfs_client_close(client
);
403 query_params_free(qp
);
410 static int nfs_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
412 NFSClient
*client
= bs
->opaque
;
415 Error
*local_err
= NULL
;
417 client
->aio_context
= bdrv_get_aio_context(bs
);
419 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
420 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
422 error_propagate(errp
, local_err
);
426 ret
= nfs_client_open(client
, qemu_opt_get(opts
, "filename"),
427 (flags
& BDRV_O_RDWR
) ? O_RDWR
: O_RDONLY
,
428 errp
, bs
->open_flags
);
432 bs
->total_sectors
= ret
;
439 static QemuOptsList nfs_create_opts
= {
440 .name
= "nfs-create-opts",
441 .head
= QTAILQ_HEAD_INITIALIZER(nfs_create_opts
.head
),
444 .name
= BLOCK_OPT_SIZE
,
445 .type
= QEMU_OPT_SIZE
,
446 .help
= "Virtual disk size"
448 { /* end of list */ }
452 static int nfs_file_create(const char *url
, QemuOpts
*opts
, Error
**errp
)
455 int64_t total_size
= 0;
456 NFSClient
*client
= g_new0(NFSClient
, 1);
458 client
->aio_context
= qemu_get_aio_context();
460 /* Read out options */
461 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
464 ret
= nfs_client_open(client
, url
, O_CREAT
, errp
, 0);
468 ret
= nfs_ftruncate(client
->context
, client
->fh
, total_size
);
469 nfs_client_close(client
);
475 static int nfs_has_zero_init(BlockDriverState
*bs
)
477 NFSClient
*client
= bs
->opaque
;
478 return client
->has_zero_init
;
481 static int64_t nfs_get_allocated_file_size(BlockDriverState
*bs
)
483 NFSClient
*client
= bs
->opaque
;
487 if (bdrv_is_read_only(bs
) &&
488 !(bs
->open_flags
& BDRV_O_NOCACHE
)) {
489 return client
->st_blocks
* 512;
493 if (nfs_fstat_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
498 while (!task
.complete
) {
499 nfs_set_events(client
);
500 aio_poll(client
->aio_context
, true);
503 return (task
.ret
< 0 ? task
.ret
: st
.st_blocks
* 512);
506 static int nfs_file_truncate(BlockDriverState
*bs
, int64_t offset
)
508 NFSClient
*client
= bs
->opaque
;
509 return nfs_ftruncate(client
->context
, client
->fh
, offset
);
512 /* Note that this will not re-establish a connection with the NFS server
513 * - it is effectively a NOP. */
514 static int nfs_reopen_prepare(BDRVReopenState
*state
,
515 BlockReopenQueue
*queue
, Error
**errp
)
517 NFSClient
*client
= state
->bs
->opaque
;
521 if (state
->flags
& BDRV_O_RDWR
&& bdrv_is_read_only(state
->bs
)) {
522 error_setg(errp
, "Cannot open a read-only mount as read-write");
526 if ((state
->flags
& BDRV_O_NOCACHE
) && client
->cache_used
) {
527 error_setg(errp
, "Cannot disable cache if libnfs readahead is enabled");
531 /* Update cache for read-only reopens */
532 if (!(state
->flags
& BDRV_O_RDWR
)) {
533 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
535 error_setg(errp
, "Failed to fstat file: %s",
536 nfs_get_error(client
->context
));
539 client
->st_blocks
= st
.st_blocks
;
545 static BlockDriver bdrv_nfs
= {
546 .format_name
= "nfs",
547 .protocol_name
= "nfs",
549 .instance_size
= sizeof(NFSClient
),
550 .bdrv_needs_filename
= true,
551 .create_opts
= &nfs_create_opts
,
553 .bdrv_has_zero_init
= nfs_has_zero_init
,
554 .bdrv_get_allocated_file_size
= nfs_get_allocated_file_size
,
555 .bdrv_truncate
= nfs_file_truncate
,
557 .bdrv_file_open
= nfs_file_open
,
558 .bdrv_close
= nfs_file_close
,
559 .bdrv_create
= nfs_file_create
,
560 .bdrv_reopen_prepare
= nfs_reopen_prepare
,
562 .bdrv_co_readv
= nfs_co_readv
,
563 .bdrv_co_writev
= nfs_co_writev
,
564 .bdrv_co_flush_to_disk
= nfs_co_flush
,
566 .bdrv_detach_aio_context
= nfs_detach_aio_context
,
567 .bdrv_attach_aio_context
= nfs_attach_aio_context
,
570 static void nfs_block_init(void)
572 bdrv_register(&bdrv_nfs
);
575 block_init(nfs_block_init
);