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
;
48 typedef struct NFSRPC
{
58 static void nfs_process_read(void *arg
);
59 static void nfs_process_write(void *arg
);
61 static void nfs_set_events(NFSClient
*client
)
63 int ev
= nfs_which_events(client
->context
);
64 if (ev
!= client
->events
) {
65 aio_set_fd_handler(client
->aio_context
,
66 nfs_get_fd(client
->context
),
67 (ev
& POLLIN
) ? nfs_process_read
: NULL
,
68 (ev
& POLLOUT
) ? nfs_process_write
: NULL
,
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
,
245 nfs_get_fd(client
->context
),
250 static void nfs_attach_aio_context(BlockDriverState
*bs
,
251 AioContext
*new_context
)
253 NFSClient
*client
= bs
->opaque
;
255 client
->aio_context
= new_context
;
256 nfs_set_events(client
);
259 static void nfs_client_close(NFSClient
*client
)
261 if (client
->context
) {
263 nfs_close(client
->context
, client
->fh
);
265 aio_set_fd_handler(client
->aio_context
,
266 nfs_get_fd(client
->context
),
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
)
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 (val
> QEMU_NFS_MAX_READAHEAD_SIZE
) {
333 error_report("NFS Warning: Truncating NFS readahead"
334 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE
);
335 val
= QEMU_NFS_MAX_READAHEAD_SIZE
;
337 nfs_set_readahead(client
->context
, val
);
340 error_setg(errp
, "Unknown NFS parameter name: %s",
346 ret
= nfs_mount(client
->context
, uri
->server
, uri
->path
);
348 error_setg(errp
, "Failed to mount nfs share: %s",
349 nfs_get_error(client
->context
));
353 if (flags
& O_CREAT
) {
354 ret
= nfs_creat(client
->context
, file
, 0600, &client
->fh
);
356 error_setg(errp
, "Failed to create file: %s",
357 nfs_get_error(client
->context
));
361 ret
= nfs_open(client
->context
, file
, flags
, &client
->fh
);
363 error_setg(errp
, "Failed to open file : %s",
364 nfs_get_error(client
->context
));
369 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
371 error_setg(errp
, "Failed to fstat file: %s",
372 nfs_get_error(client
->context
));
376 ret
= DIV_ROUND_UP(st
.st_size
, BDRV_SECTOR_SIZE
);
377 client
->has_zero_init
= S_ISREG(st
.st_mode
);
380 nfs_client_close(client
);
383 query_params_free(qp
);
390 static int nfs_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
392 NFSClient
*client
= bs
->opaque
;
395 Error
*local_err
= NULL
;
397 client
->aio_context
= bdrv_get_aio_context(bs
);
399 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
400 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
402 error_propagate(errp
, local_err
);
406 ret
= nfs_client_open(client
, qemu_opt_get(opts
, "filename"),
407 (flags
& BDRV_O_RDWR
) ? O_RDWR
: O_RDONLY
,
412 bs
->total_sectors
= ret
;
419 static QemuOptsList nfs_create_opts
= {
420 .name
= "nfs-create-opts",
421 .head
= QTAILQ_HEAD_INITIALIZER(nfs_create_opts
.head
),
424 .name
= BLOCK_OPT_SIZE
,
425 .type
= QEMU_OPT_SIZE
,
426 .help
= "Virtual disk size"
428 { /* end of list */ }
432 static int nfs_file_create(const char *url
, QemuOpts
*opts
, Error
**errp
)
435 int64_t total_size
= 0;
436 NFSClient
*client
= g_new0(NFSClient
, 1);
438 client
->aio_context
= qemu_get_aio_context();
440 /* Read out options */
441 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
444 ret
= nfs_client_open(client
, url
, O_CREAT
, errp
);
448 ret
= nfs_ftruncate(client
->context
, client
->fh
, total_size
);
449 nfs_client_close(client
);
455 static int nfs_has_zero_init(BlockDriverState
*bs
)
457 NFSClient
*client
= bs
->opaque
;
458 return client
->has_zero_init
;
461 static int64_t nfs_get_allocated_file_size(BlockDriverState
*bs
)
463 NFSClient
*client
= bs
->opaque
;
468 if (nfs_fstat_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
473 while (!task
.complete
) {
474 nfs_set_events(client
);
475 aio_poll(client
->aio_context
, true);
478 return (task
.ret
< 0 ? task
.ret
: st
.st_blocks
* st
.st_blksize
);
481 static int nfs_file_truncate(BlockDriverState
*bs
, int64_t offset
)
483 NFSClient
*client
= bs
->opaque
;
484 return nfs_ftruncate(client
->context
, client
->fh
, offset
);
487 static BlockDriver bdrv_nfs
= {
488 .format_name
= "nfs",
489 .protocol_name
= "nfs",
491 .instance_size
= sizeof(NFSClient
),
492 .bdrv_needs_filename
= true,
493 .create_opts
= &nfs_create_opts
,
495 .bdrv_has_zero_init
= nfs_has_zero_init
,
496 .bdrv_get_allocated_file_size
= nfs_get_allocated_file_size
,
497 .bdrv_truncate
= nfs_file_truncate
,
499 .bdrv_file_open
= nfs_file_open
,
500 .bdrv_close
= nfs_file_close
,
501 .bdrv_create
= nfs_file_create
,
503 .bdrv_co_readv
= nfs_co_readv
,
504 .bdrv_co_writev
= nfs_co_writev
,
505 .bdrv_co_flush_to_disk
= nfs_co_flush
,
507 .bdrv_detach_aio_context
= nfs_detach_aio_context
,
508 .bdrv_attach_aio_context
= nfs_attach_aio_context
,
511 static void nfs_block_init(void)
513 bdrv_register(&bdrv_nfs
);
516 block_init(nfs_block_init
);