2 * QEMU Block driver for native access to files on NFS shares
4 * Copyright (c) 2014 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 "config-host.h"
28 #include "qemu-common.h"
29 #include "qemu/config-file.h"
30 #include "qemu/error-report.h"
31 #include "block/block_int.h"
35 #include "sysemu/sysemu.h"
36 #include <nfsc/libnfs.h>
38 #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
40 typedef struct NFSClient
{
41 struct nfs_context
*context
;
45 AioContext
*aio_context
;
49 typedef struct NFSRPC
{
59 static void nfs_process_read(void *arg
);
60 static void nfs_process_write(void *arg
);
62 static void nfs_set_events(NFSClient
*client
)
64 int ev
= nfs_which_events(client
->context
);
65 if (ev
!= client
->events
) {
66 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
68 (ev
& POLLIN
) ? nfs_process_read
: NULL
,
69 (ev
& POLLOUT
) ? nfs_process_write
: NULL
, client
);
75 static void nfs_process_read(void *arg
)
77 NFSClient
*client
= arg
;
78 nfs_service(client
->context
, POLLIN
);
79 nfs_set_events(client
);
82 static void nfs_process_write(void *arg
)
84 NFSClient
*client
= arg
;
85 nfs_service(client
->context
, POLLOUT
);
86 nfs_set_events(client
);
89 static void nfs_co_init_task(NFSClient
*client
, NFSRPC
*task
)
92 .co
= qemu_coroutine_self(),
97 static void nfs_co_generic_bh_cb(void *opaque
)
99 NFSRPC
*task
= opaque
;
101 qemu_bh_delete(task
->bh
);
102 qemu_coroutine_enter(task
->co
, NULL
);
106 nfs_co_generic_cb(int ret
, struct nfs_context
*nfs
, void *data
,
109 NFSRPC
*task
= private_data
;
111 if (task
->ret
> 0 && task
->iov
) {
112 if (task
->ret
<= task
->iov
->size
) {
113 qemu_iovec_from_buf(task
->iov
, 0, data
, task
->ret
);
118 if (task
->ret
== 0 && task
->st
) {
119 memcpy(task
->st
, data
, sizeof(struct stat
));
122 error_report("NFS Error: %s", nfs_get_error(nfs
));
125 task
->bh
= aio_bh_new(task
->client
->aio_context
,
126 nfs_co_generic_bh_cb
, task
);
127 qemu_bh_schedule(task
->bh
);
133 static int coroutine_fn
nfs_co_readv(BlockDriverState
*bs
,
134 int64_t sector_num
, int nb_sectors
,
137 NFSClient
*client
= bs
->opaque
;
140 nfs_co_init_task(client
, &task
);
143 if (nfs_pread_async(client
->context
, client
->fh
,
144 sector_num
* BDRV_SECTOR_SIZE
,
145 nb_sectors
* BDRV_SECTOR_SIZE
,
146 nfs_co_generic_cb
, &task
) != 0) {
150 while (!task
.complete
) {
151 nfs_set_events(client
);
152 qemu_coroutine_yield();
159 /* zero pad short reads */
160 if (task
.ret
< iov
->size
) {
161 qemu_iovec_memset(iov
, task
.ret
, 0, iov
->size
- task
.ret
);
167 static int coroutine_fn
nfs_co_writev(BlockDriverState
*bs
,
168 int64_t sector_num
, int nb_sectors
,
171 NFSClient
*client
= bs
->opaque
;
175 nfs_co_init_task(client
, &task
);
177 buf
= g_try_malloc(nb_sectors
* BDRV_SECTOR_SIZE
);
178 if (nb_sectors
&& buf
== NULL
) {
182 qemu_iovec_to_buf(iov
, 0, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
184 if (nfs_pwrite_async(client
->context
, client
->fh
,
185 sector_num
* BDRV_SECTOR_SIZE
,
186 nb_sectors
* BDRV_SECTOR_SIZE
,
187 buf
, nfs_co_generic_cb
, &task
) != 0) {
192 while (!task
.complete
) {
193 nfs_set_events(client
);
194 qemu_coroutine_yield();
199 if (task
.ret
!= nb_sectors
* BDRV_SECTOR_SIZE
) {
200 return task
.ret
< 0 ? task
.ret
: -EIO
;
206 static int coroutine_fn
nfs_co_flush(BlockDriverState
*bs
)
208 NFSClient
*client
= bs
->opaque
;
211 nfs_co_init_task(client
, &task
);
213 if (nfs_fsync_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
218 while (!task
.complete
) {
219 nfs_set_events(client
);
220 qemu_coroutine_yield();
226 /* TODO Convert to fine grained options */
227 static QemuOptsList runtime_opts
= {
229 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
233 .type
= QEMU_OPT_STRING
,
234 .help
= "URL to the NFS file",
236 { /* end of list */ }
240 static void nfs_detach_aio_context(BlockDriverState
*bs
)
242 NFSClient
*client
= bs
->opaque
;
244 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
245 false, NULL
, NULL
, NULL
);
249 static void nfs_attach_aio_context(BlockDriverState
*bs
,
250 AioContext
*new_context
)
252 NFSClient
*client
= bs
->opaque
;
254 client
->aio_context
= new_context
;
255 nfs_set_events(client
);
258 static void nfs_client_close(NFSClient
*client
)
260 if (client
->context
) {
262 nfs_close(client
->context
, client
->fh
);
264 aio_set_fd_handler(client
->aio_context
, nfs_get_fd(client
->context
),
265 false, NULL
, NULL
, NULL
);
266 nfs_destroy_context(client
->context
);
268 memset(client
, 0, sizeof(NFSClient
));
271 static void nfs_file_close(BlockDriverState
*bs
)
273 NFSClient
*client
= bs
->opaque
;
274 nfs_client_close(client
);
277 static int64_t nfs_client_open(NFSClient
*client
, const char *filename
,
278 int flags
, Error
**errp
)
280 int ret
= -EINVAL
, i
;
283 QueryParams
*qp
= NULL
;
284 char *file
= NULL
, *strp
= NULL
;
286 uri
= uri_parse(filename
);
288 error_setg(errp
, "Invalid URL specified");
292 error_setg(errp
, "Invalid URL specified");
295 strp
= strrchr(uri
->path
, '/');
297 error_setg(errp
, "Invalid URL specified");
300 file
= g_strdup(strp
);
303 client
->context
= nfs_init_context();
304 if (client
->context
== NULL
) {
305 error_setg(errp
, "Failed to init NFS context");
309 qp
= query_params_parse(uri
->query
);
310 for (i
= 0; i
< qp
->n
; i
++) {
311 unsigned long long val
;
312 if (!qp
->p
[i
].value
) {
313 error_setg(errp
, "Value for NFS parameter expected: %s",
317 if (parse_uint_full(qp
->p
[i
].value
, &val
, 0)) {
318 error_setg(errp
, "Illegal value for NFS parameter: %s",
322 if (!strcmp(qp
->p
[i
].name
, "uid")) {
323 nfs_set_uid(client
->context
, val
);
324 } else if (!strcmp(qp
->p
[i
].name
, "gid")) {
325 nfs_set_gid(client
->context
, val
);
326 } else if (!strcmp(qp
->p
[i
].name
, "tcp-syncnt")) {
327 nfs_set_tcp_syncnt(client
->context
, val
);
328 #ifdef LIBNFS_FEATURE_READAHEAD
329 } else if (!strcmp(qp
->p
[i
].name
, "readahead")) {
330 if (val
> QEMU_NFS_MAX_READAHEAD_SIZE
) {
331 error_report("NFS Warning: Truncating NFS readahead"
332 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE
);
333 val
= QEMU_NFS_MAX_READAHEAD_SIZE
;
335 nfs_set_readahead(client
->context
, val
);
338 error_setg(errp
, "Unknown NFS parameter name: %s",
344 ret
= nfs_mount(client
->context
, uri
->server
, uri
->path
);
346 error_setg(errp
, "Failed to mount nfs share: %s",
347 nfs_get_error(client
->context
));
351 if (flags
& O_CREAT
) {
352 ret
= nfs_creat(client
->context
, file
, 0600, &client
->fh
);
354 error_setg(errp
, "Failed to create file: %s",
355 nfs_get_error(client
->context
));
359 ret
= nfs_open(client
->context
, file
, flags
, &client
->fh
);
361 error_setg(errp
, "Failed to open file : %s",
362 nfs_get_error(client
->context
));
367 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
369 error_setg(errp
, "Failed to fstat file: %s",
370 nfs_get_error(client
->context
));
374 ret
= DIV_ROUND_UP(st
.st_size
, BDRV_SECTOR_SIZE
);
375 client
->st_blocks
= st
.st_blocks
;
376 client
->has_zero_init
= S_ISREG(st
.st_mode
);
379 nfs_client_close(client
);
382 query_params_free(qp
);
389 static int nfs_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
391 NFSClient
*client
= bs
->opaque
;
394 Error
*local_err
= NULL
;
396 client
->aio_context
= bdrv_get_aio_context(bs
);
398 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
399 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
401 error_propagate(errp
, local_err
);
405 ret
= nfs_client_open(client
, qemu_opt_get(opts
, "filename"),
406 (flags
& BDRV_O_RDWR
) ? O_RDWR
: O_RDONLY
,
411 bs
->total_sectors
= ret
;
418 static QemuOptsList nfs_create_opts
= {
419 .name
= "nfs-create-opts",
420 .head
= QTAILQ_HEAD_INITIALIZER(nfs_create_opts
.head
),
423 .name
= BLOCK_OPT_SIZE
,
424 .type
= QEMU_OPT_SIZE
,
425 .help
= "Virtual disk size"
427 { /* end of list */ }
431 static int nfs_file_create(const char *url
, QemuOpts
*opts
, Error
**errp
)
434 int64_t total_size
= 0;
435 NFSClient
*client
= g_new0(NFSClient
, 1);
437 client
->aio_context
= qemu_get_aio_context();
439 /* Read out options */
440 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
443 ret
= nfs_client_open(client
, url
, O_CREAT
, errp
);
447 ret
= nfs_ftruncate(client
->context
, client
->fh
, total_size
);
448 nfs_client_close(client
);
454 static int nfs_has_zero_init(BlockDriverState
*bs
)
456 NFSClient
*client
= bs
->opaque
;
457 return client
->has_zero_init
;
460 static int64_t nfs_get_allocated_file_size(BlockDriverState
*bs
)
462 NFSClient
*client
= bs
->opaque
;
466 if (bdrv_is_read_only(bs
) &&
467 !(bs
->open_flags
& BDRV_O_NOCACHE
)) {
468 return client
->st_blocks
* 512;
472 if (nfs_fstat_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
477 while (!task
.complete
) {
478 nfs_set_events(client
);
479 aio_poll(client
->aio_context
, true);
482 return (task
.ret
< 0 ? task
.ret
: st
.st_blocks
* 512);
485 static int nfs_file_truncate(BlockDriverState
*bs
, int64_t offset
)
487 NFSClient
*client
= bs
->opaque
;
488 return nfs_ftruncate(client
->context
, client
->fh
, offset
);
491 /* Note that this will not re-establish a connection with the NFS server
492 * - it is effectively a NOP. */
493 static int nfs_reopen_prepare(BDRVReopenState
*state
,
494 BlockReopenQueue
*queue
, Error
**errp
)
496 NFSClient
*client
= state
->bs
->opaque
;
500 if (state
->flags
& BDRV_O_RDWR
&& bdrv_is_read_only(state
->bs
)) {
501 error_setg(errp
, "Cannot open a read-only mount as read-write");
505 /* Update cache for read-only reopens */
506 if (!(state
->flags
& BDRV_O_RDWR
)) {
507 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
509 error_setg(errp
, "Failed to fstat file: %s",
510 nfs_get_error(client
->context
));
513 client
->st_blocks
= st
.st_blocks
;
519 static BlockDriver bdrv_nfs
= {
520 .format_name
= "nfs",
521 .protocol_name
= "nfs",
523 .instance_size
= sizeof(NFSClient
),
524 .bdrv_needs_filename
= true,
525 .create_opts
= &nfs_create_opts
,
527 .bdrv_has_zero_init
= nfs_has_zero_init
,
528 .bdrv_get_allocated_file_size
= nfs_get_allocated_file_size
,
529 .bdrv_truncate
= nfs_file_truncate
,
531 .bdrv_file_open
= nfs_file_open
,
532 .bdrv_close
= nfs_file_close
,
533 .bdrv_create
= nfs_file_create
,
534 .bdrv_reopen_prepare
= nfs_reopen_prepare
,
536 .bdrv_co_readv
= nfs_co_readv
,
537 .bdrv_co_writev
= nfs_co_writev
,
538 .bdrv_co_flush_to_disk
= nfs_co_flush
,
540 .bdrv_detach_aio_context
= nfs_detach_aio_context
,
541 .bdrv_attach_aio_context
= nfs_attach_aio_context
,
544 static void nfs_block_init(void)
546 bdrv_register(&bdrv_nfs
);
549 block_init(nfs_block_init
);