2 * QEMU Block driver for native access to files on NFS shares
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;
98 qemu_bh_delete(task->bh);
99 qemu_coroutine_enter(task->co, NULL);
103 nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
106 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);
129 static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
130 int64_t sector_num, int nb_sectors,
133 NFSClient *client = bs->opaque;
136 nfs_co_init_task(client, &task);
139 if (nfs_pread_async(client->context, client->fh,
140 sector_num * BDRV_SECTOR_SIZE,
141 nb_sectors * BDRV_SECTOR_SIZE,
142 nfs_co_generic_cb, &task) != 0) {
146 while (!task.complete) {
147 nfs_set_events(client);
148 qemu_coroutine_yield();
155 /* zero pad short reads */
156 if (task.ret < iov->size) {
157 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
163 static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
164 int64_t sector_num, int nb_sectors,
167 NFSClient *client = bs->opaque;
171 nfs_co_init_task(client, &task);
173 buf = g_malloc(nb_sectors * BDRV_SECTOR_SIZE);
174 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
176 if (nfs_pwrite_async(client->context, client->fh,
177 sector_num * BDRV_SECTOR_SIZE,
178 nb_sectors * BDRV_SECTOR_SIZE,
179 buf, nfs_co_generic_cb, &task) != 0) {
184 while (!task.complete) {
185 nfs_set_events(client);
186 qemu_coroutine_yield();
191 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
192 return task.ret < 0 ? task.ret : -EIO;
198 static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
200 NFSClient *client = bs->opaque;
203 nfs_co_init_task(client, &task);
205 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
210 while (!task.complete) {
211 nfs_set_events(client);
212 qemu_coroutine_yield();
218 /* TODO Convert to fine grained options */
219 static QemuOptsList runtime_opts = {
221 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
225 .type = QEMU_OPT_STRING,
226 .help = "URL to the NFS file",
228 { /* end of list */ }
232 static void nfs_detach_aio_context(BlockDriverState *bs)
234 NFSClient *client = bs->opaque;
236 aio_set_fd_handler(client->aio_context,
237 nfs_get_fd(client->context),
242 static void nfs_attach_aio_context(BlockDriverState *bs,
243 AioContext *new_context)
245 NFSClient *client = bs->opaque;
247 client->aio_context = new_context;
248 nfs_set_events(client);
251 static void nfs_client_close(NFSClient *client)
253 if (client->context) {
255 nfs_close(client->context, client->fh);
257 aio_set_fd_handler(client->aio_context,
258 nfs_get_fd(client->context),
260 nfs_destroy_context(client->context);
262 memset(client, 0, sizeof(NFSClient));
265 static void nfs_file_close(BlockDriverState *bs)
267 NFSClient *client = bs->opaque;
268 nfs_client_close(client);
271 static int64_t nfs_client_open(NFSClient *client, const char *filename,
272 int flags, Error **errp)
274 int ret = -EINVAL, i;
277 QueryParams *qp = NULL;
278 char *file = NULL, *strp = NULL;
280 uri = uri_parse(filename);
282 error_setg(errp, "Invalid URL specified");
286 error_setg(errp, "Invalid URL specified");
289 strp = strrchr(uri->path, '/');
291 error_setg(errp, "Invalid URL specified");
294 file = g_strdup(strp);
297 client->context = nfs_init_context();
298 if (client->context == NULL) {
299 error_setg(errp, "Failed to init NFS context");
303 qp = query_params_parse(uri->query);
304 for (i = 0; i < qp->n; i++) {
305 if (!qp->p[i].value) {
306 error_setg(errp, "Value for NFS parameter expected: %s",
310 if (!strncmp(qp->p[i].name, "uid", 3)) {
311 nfs_set_uid(client->context, atoi(qp->p[i].value));
312 } else if (!strncmp(qp->p[i].name, "gid", 3)) {
313 nfs_set_gid(client->context, atoi(qp->p[i].value));
314 } else if (!strncmp(qp->p[i].name, "tcp-syncnt", 10)) {
315 nfs_set_tcp_syncnt(client->context, atoi(qp->p[i].value));
317 error_setg(errp, "Unknown NFS parameter name: %s",
323 ret = nfs_mount(client->context, uri->server, uri->path);
325 error_setg(errp, "Failed to mount nfs share: %s",
326 nfs_get_error(client->context));
330 if (flags & O_CREAT) {
331 ret = nfs_creat(client->context, file, 0600, &client->fh);
333 error_setg(errp, "Failed to create file: %s",
334 nfs_get_error(client->context));
338 ret = nfs_open(client->context, file, flags, &client->fh);
340 error_setg(errp, "Failed to open file : %s",
341 nfs_get_error(client->context));
346 ret = nfs_fstat(client->context, client->fh, &st);
348 error_setg(errp, "Failed to fstat file: %s",
349 nfs_get_error(client->context));
353 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
354 client->has_zero_init = S_ISREG(st.st_mode);
357 nfs_client_close(client);
360 query_params_free(qp);
367 static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
369 NFSClient *client = bs->opaque;
372 Error *local_err = NULL;
374 client->aio_context = bdrv_get_aio_context(bs);
376 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
377 qemu_opts_absorb_qdict(opts, options, &local_err);
379 error_propagate(errp, local_err);
382 ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
383 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
388 bs->total_sectors = ret;
392 static int nfs_file_create(const char *url, QEMUOptionParameter *options,
396 int64_t total_size = 0;
397 NFSClient *client = g_malloc0(sizeof(NFSClient));
399 client->aio_context = qemu_get_aio_context();
401 /* Read out options */
402 while (options && options->name) {
403 if (!strcmp(options->name, "size")) {
404 total_size = options->value.n;
409 ret = nfs_client_open(client, url, O_CREAT, errp);
413 ret = nfs_ftruncate(client->context, client->fh, total_size);
414 nfs_client_close(client);
420 static int nfs_has_zero_init(BlockDriverState *bs)
422 NFSClient *client = bs->opaque;
423 return client->has_zero_init;
426 static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
428 NFSClient *client = bs->opaque;
433 if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
438 while (!task.complete) {
439 nfs_set_events(client);
440 aio_poll(client->aio_context, true);
443 return (task.ret < 0 ? task.ret : st.st_blocks * st.st_blksize);
446 static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
448 NFSClient *client = bs->opaque;
449 return nfs_ftruncate(client->context, client->fh, offset);
452 static BlockDriver bdrv_nfs = {
453 .format_name = "nfs",
454 .protocol_name = "nfs",
456 .instance_size = sizeof(NFSClient),
457 .bdrv_needs_filename = true,
458 .bdrv_has_zero_init = nfs_has_zero_init,
459 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
460 .bdrv_truncate = nfs_file_truncate,
462 .bdrv_file_open = nfs_file_open,
463 .bdrv_close = nfs_file_close,
464 .bdrv_create = nfs_file_create,
466 .bdrv_co_readv = nfs_co_readv,
467 .bdrv_co_writev = nfs_co_writev,
468 .bdrv_co_flush_to_disk = nfs_co_flush,
470 .bdrv_detach_aio_context = nfs_detach_aio_context,
471 .bdrv_attach_aio_context = nfs_attach_aio_context,
474 static void nfs_block_init(void)
476 bdrv_register(&bdrv_nfs);
479 block_init(nfs_block_init);