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 typedef struct NFSClient
{
39 struct nfs_context
*context
;
43 AioContext
*aio_context
;
46 typedef struct NFSRPC
{
56 static void nfs_process_read(void *arg
);
57 static void nfs_process_write(void *arg
);
59 static void nfs_set_events(NFSClient
*client
)
61 int ev
= nfs_which_events(client
->context
);
62 if (ev
!= client
->events
) {
63 aio_set_fd_handler(client
->aio_context
,
64 nfs_get_fd(client
->context
),
65 (ev
& POLLIN
) ? nfs_process_read
: NULL
,
66 (ev
& POLLOUT
) ? nfs_process_write
: NULL
,
73 static void nfs_process_read(void *arg
)
75 NFSClient
*client
= arg
;
76 nfs_service(client
->context
, POLLIN
);
77 nfs_set_events(client
);
80 static void nfs_process_write(void *arg
)
82 NFSClient
*client
= arg
;
83 nfs_service(client
->context
, POLLOUT
);
84 nfs_set_events(client
);
87 static void nfs_co_init_task(NFSClient
*client
, NFSRPC
*task
)
90 .co
= qemu_coroutine_self(),
95 static void nfs_co_generic_bh_cb(void *opaque
)
97 NFSRPC
*task
= opaque
;
99 qemu_bh_delete(task
->bh
);
100 qemu_coroutine_enter(task
->co
, NULL
);
104 nfs_co_generic_cb(int ret
, struct nfs_context
*nfs
, void *data
,
107 NFSRPC
*task
= private_data
;
109 if (task
->ret
> 0 && task
->iov
) {
110 if (task
->ret
<= task
->iov
->size
) {
111 qemu_iovec_from_buf(task
->iov
, 0, data
, task
->ret
);
116 if (task
->ret
== 0 && task
->st
) {
117 memcpy(task
->st
, data
, sizeof(struct stat
));
120 error_report("NFS Error: %s", nfs_get_error(nfs
));
123 task
->bh
= aio_bh_new(task
->client
->aio_context
,
124 nfs_co_generic_bh_cb
, task
);
125 qemu_bh_schedule(task
->bh
);
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(client
, &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 while (!task
.complete
) {
149 nfs_set_events(client
);
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(client
, &task
);
175 buf
= g_malloc(nb_sectors
* BDRV_SECTOR_SIZE
);
176 qemu_iovec_to_buf(iov
, 0, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
178 if (nfs_pwrite_async(client
->context
, client
->fh
,
179 sector_num
* BDRV_SECTOR_SIZE
,
180 nb_sectors
* BDRV_SECTOR_SIZE
,
181 buf
, nfs_co_generic_cb
, &task
) != 0) {
186 while (!task
.complete
) {
187 nfs_set_events(client
);
188 qemu_coroutine_yield();
193 if (task
.ret
!= nb_sectors
* BDRV_SECTOR_SIZE
) {
194 return task
.ret
< 0 ? task
.ret
: -EIO
;
200 static int coroutine_fn
nfs_co_flush(BlockDriverState
*bs
)
202 NFSClient
*client
= bs
->opaque
;
205 nfs_co_init_task(client
, &task
);
207 if (nfs_fsync_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
212 while (!task
.complete
) {
213 nfs_set_events(client
);
214 qemu_coroutine_yield();
220 /* TODO Convert to fine grained options */
221 static QemuOptsList runtime_opts
= {
223 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
227 .type
= QEMU_OPT_STRING
,
228 .help
= "URL to the NFS file",
230 { /* end of list */ }
234 static void nfs_detach_aio_context(BlockDriverState
*bs
)
236 NFSClient
*client
= bs
->opaque
;
238 aio_set_fd_handler(client
->aio_context
,
239 nfs_get_fd(client
->context
),
244 static void nfs_attach_aio_context(BlockDriverState
*bs
,
245 AioContext
*new_context
)
247 NFSClient
*client
= bs
->opaque
;
249 client
->aio_context
= new_context
;
250 nfs_set_events(client
);
253 static void nfs_client_close(NFSClient
*client
)
255 if (client
->context
) {
257 nfs_close(client
->context
, client
->fh
);
259 aio_set_fd_handler(client
->aio_context
,
260 nfs_get_fd(client
->context
),
262 nfs_destroy_context(client
->context
);
264 memset(client
, 0, sizeof(NFSClient
));
267 static void nfs_file_close(BlockDriverState
*bs
)
269 NFSClient
*client
= bs
->opaque
;
270 nfs_client_close(client
);
273 static int64_t nfs_client_open(NFSClient
*client
, const char *filename
,
274 int flags
, Error
**errp
)
276 int ret
= -EINVAL
, i
;
279 QueryParams
*qp
= NULL
;
280 char *file
= NULL
, *strp
= NULL
;
282 uri
= uri_parse(filename
);
284 error_setg(errp
, "Invalid URL specified");
288 error_setg(errp
, "Invalid URL specified");
291 strp
= strrchr(uri
->path
, '/');
293 error_setg(errp
, "Invalid URL specified");
296 file
= g_strdup(strp
);
299 client
->context
= nfs_init_context();
300 if (client
->context
== NULL
) {
301 error_setg(errp
, "Failed to init NFS context");
305 qp
= query_params_parse(uri
->query
);
306 for (i
= 0; i
< qp
->n
; i
++) {
307 if (!qp
->p
[i
].value
) {
308 error_setg(errp
, "Value for NFS parameter expected: %s",
312 if (!strncmp(qp
->p
[i
].name
, "uid", 3)) {
313 nfs_set_uid(client
->context
, atoi(qp
->p
[i
].value
));
314 } else if (!strncmp(qp
->p
[i
].name
, "gid", 3)) {
315 nfs_set_gid(client
->context
, atoi(qp
->p
[i
].value
));
316 } else if (!strncmp(qp
->p
[i
].name
, "tcp-syncnt", 10)) {
317 nfs_set_tcp_syncnt(client
->context
, atoi(qp
->p
[i
].value
));
319 error_setg(errp
, "Unknown NFS parameter name: %s",
325 ret
= nfs_mount(client
->context
, uri
->server
, uri
->path
);
327 error_setg(errp
, "Failed to mount nfs share: %s",
328 nfs_get_error(client
->context
));
332 if (flags
& O_CREAT
) {
333 ret
= nfs_creat(client
->context
, file
, 0600, &client
->fh
);
335 error_setg(errp
, "Failed to create file: %s",
336 nfs_get_error(client
->context
));
340 ret
= nfs_open(client
->context
, file
, flags
, &client
->fh
);
342 error_setg(errp
, "Failed to open file : %s",
343 nfs_get_error(client
->context
));
348 ret
= nfs_fstat(client
->context
, client
->fh
, &st
);
350 error_setg(errp
, "Failed to fstat file: %s",
351 nfs_get_error(client
->context
));
355 ret
= DIV_ROUND_UP(st
.st_size
, BDRV_SECTOR_SIZE
);
356 client
->has_zero_init
= S_ISREG(st
.st_mode
);
359 nfs_client_close(client
);
362 query_params_free(qp
);
369 static int nfs_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
371 NFSClient
*client
= bs
->opaque
;
374 Error
*local_err
= NULL
;
376 client
->aio_context
= bdrv_get_aio_context(bs
);
378 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
379 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
381 error_propagate(errp
, local_err
);
384 ret
= nfs_client_open(client
, qemu_opt_get(opts
, "filename"),
385 (flags
& BDRV_O_RDWR
) ? O_RDWR
: O_RDONLY
,
390 bs
->total_sectors
= ret
;
394 static int nfs_file_create(const char *url
, QemuOpts
*opts
, Error
**errp
)
397 int64_t total_size
= 0;
398 NFSClient
*client
= g_malloc0(sizeof(NFSClient
));
400 client
->aio_context
= qemu_get_aio_context();
402 /* Read out options */
403 total_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
405 ret
= nfs_client_open(client
, url
, O_CREAT
, errp
);
409 ret
= nfs_ftruncate(client
->context
, client
->fh
, total_size
);
410 nfs_client_close(client
);
416 static int nfs_has_zero_init(BlockDriverState
*bs
)
418 NFSClient
*client
= bs
->opaque
;
419 return client
->has_zero_init
;
422 static int64_t nfs_get_allocated_file_size(BlockDriverState
*bs
)
424 NFSClient
*client
= bs
->opaque
;
429 if (nfs_fstat_async(client
->context
, client
->fh
, nfs_co_generic_cb
,
434 while (!task
.complete
) {
435 nfs_set_events(client
);
436 aio_poll(client
->aio_context
, true);
439 return (task
.ret
< 0 ? task
.ret
: st
.st_blocks
* st
.st_blksize
);
442 static int nfs_file_truncate(BlockDriverState
*bs
, int64_t offset
)
444 NFSClient
*client
= bs
->opaque
;
445 return nfs_ftruncate(client
->context
, client
->fh
, offset
);
448 static BlockDriver bdrv_nfs
= {
449 .format_name
= "nfs",
450 .protocol_name
= "nfs",
452 .instance_size
= sizeof(NFSClient
),
453 .bdrv_needs_filename
= true,
454 .bdrv_has_zero_init
= nfs_has_zero_init
,
455 .bdrv_get_allocated_file_size
= nfs_get_allocated_file_size
,
456 .bdrv_truncate
= nfs_file_truncate
,
458 .bdrv_file_open
= nfs_file_open
,
459 .bdrv_close
= nfs_file_close
,
460 .bdrv_create
= nfs_file_create
,
462 .bdrv_co_readv
= nfs_co_readv
,
463 .bdrv_co_writev
= nfs_co_writev
,
464 .bdrv_co_flush_to_disk
= nfs_co_flush
,
466 .bdrv_detach_aio_context
= nfs_detach_aio_context
,
467 .bdrv_attach_aio_context
= nfs_attach_aio_context
,
470 static void nfs_block_init(void)
472 bdrv_register(&bdrv_nfs
);
475 block_init(nfs_block_init
);