blob: 0b44483871bfe0376a97b9cdb422aa35b0e2a99e [file] [log] [blame]
Peter Lieven6542aa92014-02-03 10:26:13 +01001/*
2 * QEMU Block driver for native access to files on NFS shares
3 *
4 * Copyright (c) 2014 Peter Lieven <pl@kamp.de>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25#include "config-host.h"
26
27#include <poll.h>
28#include "qemu-common.h"
29#include "qemu/config-file.h"
30#include "qemu/error-report.h"
31#include "block/block_int.h"
32#include "trace.h"
33#include "qemu/iov.h"
34#include "qemu/uri.h"
35#include "sysemu/sysemu.h"
36#include <nfsc/libnfs.h>
37
38typedef struct NFSClient {
39 struct nfs_context *context;
40 struct nfsfh *fh;
41 int events;
42 bool has_zero_init;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020043 AioContext *aio_context;
Peter Lieven6542aa92014-02-03 10:26:13 +010044} NFSClient;
45
46typedef struct NFSRPC {
47 int ret;
48 int complete;
49 QEMUIOVector *iov;
50 struct stat *st;
51 Coroutine *co;
52 QEMUBH *bh;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020053 NFSClient *client;
Peter Lieven6542aa92014-02-03 10:26:13 +010054} NFSRPC;
55
56static void nfs_process_read(void *arg);
57static void nfs_process_write(void *arg);
58
59static void nfs_set_events(NFSClient *client)
60{
61 int ev = nfs_which_events(client->context);
62 if (ev != client->events) {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020063 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,
67 client);
Peter Lieven6542aa92014-02-03 10:26:13 +010068
69 }
70 client->events = ev;
71}
72
73static void nfs_process_read(void *arg)
74{
75 NFSClient *client = arg;
76 nfs_service(client->context, POLLIN);
77 nfs_set_events(client);
78}
79
80static void nfs_process_write(void *arg)
81{
82 NFSClient *client = arg;
83 nfs_service(client->context, POLLOUT);
84 nfs_set_events(client);
85}
86
87static void nfs_co_init_task(NFSClient *client, NFSRPC *task)
88{
89 *task = (NFSRPC) {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020090 .co = qemu_coroutine_self(),
91 .client = client,
Peter Lieven6542aa92014-02-03 10:26:13 +010092 };
93}
94
95static void nfs_co_generic_bh_cb(void *opaque)
96{
97 NFSRPC *task = opaque;
Peter Lievena2c0fe22014-06-10 09:42:47 +020098 task->complete = 1;
Peter Lieven6542aa92014-02-03 10:26:13 +010099 qemu_bh_delete(task->bh);
100 qemu_coroutine_enter(task->co, NULL);
101}
102
103static void
104nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
105 void *private_data)
106{
107 NFSRPC *task = private_data;
Peter Lieven6542aa92014-02-03 10:26:13 +0100108 task->ret = ret;
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);
112 } else {
113 task->ret = -EIO;
114 }
115 }
116 if (task->ret == 0 && task->st) {
117 memcpy(task->st, data, sizeof(struct stat));
118 }
Peter Lieven20fccb12014-03-17 09:37:21 +0100119 if (task->ret < 0) {
120 error_report("NFS Error: %s", nfs_get_error(nfs));
121 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100122 if (task->co) {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200123 task->bh = aio_bh_new(task->client->aio_context,
124 nfs_co_generic_bh_cb, task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100125 qemu_bh_schedule(task->bh);
Peter Lievena2c0fe22014-06-10 09:42:47 +0200126 } else {
127 task->complete = 1;
Peter Lieven6542aa92014-02-03 10:26:13 +0100128 }
129}
130
131static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
132 int64_t sector_num, int nb_sectors,
133 QEMUIOVector *iov)
134{
135 NFSClient *client = bs->opaque;
136 NFSRPC task;
137
138 nfs_co_init_task(client, &task);
139 task.iov = iov;
140
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) {
145 return -ENOMEM;
146 }
147
148 while (!task.complete) {
149 nfs_set_events(client);
150 qemu_coroutine_yield();
151 }
152
153 if (task.ret < 0) {
154 return task.ret;
155 }
156
157 /* zero pad short reads */
158 if (task.ret < iov->size) {
159 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
160 }
161
162 return 0;
163}
164
165static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
166 int64_t sector_num, int nb_sectors,
167 QEMUIOVector *iov)
168{
169 NFSClient *client = bs->opaque;
170 NFSRPC task;
171 char *buf = NULL;
172
173 nfs_co_init_task(client, &task);
174
175 buf = g_malloc(nb_sectors * BDRV_SECTOR_SIZE);
176 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
177
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) {
182 g_free(buf);
183 return -ENOMEM;
184 }
185
186 while (!task.complete) {
187 nfs_set_events(client);
188 qemu_coroutine_yield();
189 }
190
191 g_free(buf);
192
193 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
194 return task.ret < 0 ? task.ret : -EIO;
195 }
196
197 return 0;
198}
199
200static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
201{
202 NFSClient *client = bs->opaque;
203 NFSRPC task;
204
205 nfs_co_init_task(client, &task);
206
207 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
208 &task) != 0) {
209 return -ENOMEM;
210 }
211
212 while (!task.complete) {
213 nfs_set_events(client);
214 qemu_coroutine_yield();
215 }
216
217 return task.ret;
218}
219
220/* TODO Convert to fine grained options */
221static QemuOptsList runtime_opts = {
222 .name = "nfs",
223 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
224 .desc = {
225 {
226 .name = "filename",
227 .type = QEMU_OPT_STRING,
228 .help = "URL to the NFS file",
229 },
230 { /* end of list */ }
231 },
232};
233
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200234static void nfs_detach_aio_context(BlockDriverState *bs)
235{
236 NFSClient *client = bs->opaque;
237
238 aio_set_fd_handler(client->aio_context,
239 nfs_get_fd(client->context),
240 NULL, NULL, NULL);
241 client->events = 0;
242}
243
244static void nfs_attach_aio_context(BlockDriverState *bs,
245 AioContext *new_context)
246{
247 NFSClient *client = bs->opaque;
248
249 client->aio_context = new_context;
250 nfs_set_events(client);
251}
252
Peter Lieven6542aa92014-02-03 10:26:13 +0100253static void nfs_client_close(NFSClient *client)
254{
255 if (client->context) {
256 if (client->fh) {
257 nfs_close(client->context, client->fh);
258 }
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200259 aio_set_fd_handler(client->aio_context,
260 nfs_get_fd(client->context),
261 NULL, NULL, NULL);
Peter Lieven6542aa92014-02-03 10:26:13 +0100262 nfs_destroy_context(client->context);
263 }
264 memset(client, 0, sizeof(NFSClient));
265}
266
267static void nfs_file_close(BlockDriverState *bs)
268{
269 NFSClient *client = bs->opaque;
270 nfs_client_close(client);
271}
272
273static int64_t nfs_client_open(NFSClient *client, const char *filename,
274 int flags, Error **errp)
275{
276 int ret = -EINVAL, i;
277 struct stat st;
278 URI *uri;
279 QueryParams *qp = NULL;
280 char *file = NULL, *strp = NULL;
281
282 uri = uri_parse(filename);
283 if (!uri) {
284 error_setg(errp, "Invalid URL specified");
285 goto fail;
286 }
Max Reitz5f4d5e12014-05-05 20:27:49 +0200287 if (!uri->server) {
288 error_setg(errp, "Invalid URL specified");
289 goto fail;
290 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100291 strp = strrchr(uri->path, '/');
292 if (strp == NULL) {
293 error_setg(errp, "Invalid URL specified");
294 goto fail;
295 }
296 file = g_strdup(strp);
297 *strp = 0;
298
299 client->context = nfs_init_context();
300 if (client->context == NULL) {
301 error_setg(errp, "Failed to init NFS context");
302 goto fail;
303 }
304
305 qp = query_params_parse(uri->query);
306 for (i = 0; i < qp->n; i++) {
Peter Lieven7c243842014-06-25 00:06:00 +0200307 unsigned long long val;
Peter Lieven6542aa92014-02-03 10:26:13 +0100308 if (!qp->p[i].value) {
309 error_setg(errp, "Value for NFS parameter expected: %s",
310 qp->p[i].name);
311 goto fail;
312 }
Peter Lieven7c243842014-06-25 00:06:00 +0200313 if (parse_uint_full(qp->p[i].value, &val, 0)) {
314 error_setg(errp, "Illegal value for NFS parameter: %s",
315 qp->p[i].name);
316 goto fail;
317 }
318 if (!strcmp(qp->p[i].name, "uid")) {
319 nfs_set_uid(client->context, val);
320 } else if (!strcmp(qp->p[i].name, "gid")) {
321 nfs_set_gid(client->context, val);
322 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
323 nfs_set_tcp_syncnt(client->context, val);
Peter Lieven6542aa92014-02-03 10:26:13 +0100324 } else {
325 error_setg(errp, "Unknown NFS parameter name: %s",
326 qp->p[i].name);
327 goto fail;
328 }
329 }
330
331 ret = nfs_mount(client->context, uri->server, uri->path);
332 if (ret < 0) {
333 error_setg(errp, "Failed to mount nfs share: %s",
334 nfs_get_error(client->context));
335 goto fail;
336 }
337
338 if (flags & O_CREAT) {
339 ret = nfs_creat(client->context, file, 0600, &client->fh);
340 if (ret < 0) {
341 error_setg(errp, "Failed to create file: %s",
342 nfs_get_error(client->context));
343 goto fail;
344 }
345 } else {
346 ret = nfs_open(client->context, file, flags, &client->fh);
347 if (ret < 0) {
348 error_setg(errp, "Failed to open file : %s",
349 nfs_get_error(client->context));
350 goto fail;
351 }
352 }
353
354 ret = nfs_fstat(client->context, client->fh, &st);
355 if (ret < 0) {
356 error_setg(errp, "Failed to fstat file: %s",
357 nfs_get_error(client->context));
358 goto fail;
359 }
360
361 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
362 client->has_zero_init = S_ISREG(st.st_mode);
363 goto out;
364fail:
365 nfs_client_close(client);
366out:
367 if (qp) {
368 query_params_free(qp);
369 }
370 uri_free(uri);
371 g_free(file);
372 return ret;
373}
374
375static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
376 Error **errp) {
377 NFSClient *client = bs->opaque;
378 int64_t ret;
379 QemuOpts *opts;
380 Error *local_err = NULL;
381
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200382 client->aio_context = bdrv_get_aio_context(bs);
383
Peter Lieven6542aa92014-02-03 10:26:13 +0100384 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
385 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster0fb63952014-04-25 16:50:31 +0200386 if (local_err) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100387 error_propagate(errp, local_err);
388 return -EINVAL;
389 }
390 ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
391 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
392 errp);
393 if (ret < 0) {
394 return ret;
395 }
396 bs->total_sectors = ret;
397 return 0;
398}
399
Chunyan Liu98c10b82014-06-05 17:20:56 +0800400static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
Peter Lieven6542aa92014-02-03 10:26:13 +0100401{
402 int ret = 0;
403 int64_t total_size = 0;
404 NFSClient *client = g_malloc0(sizeof(NFSClient));
405
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200406 client->aio_context = qemu_get_aio_context();
407
Peter Lieven6542aa92014-02-03 10:26:13 +0100408 /* Read out options */
Chunyan Liu98c10b82014-06-05 17:20:56 +0800409 total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
Peter Lieven6542aa92014-02-03 10:26:13 +0100410
411 ret = nfs_client_open(client, url, O_CREAT, errp);
412 if (ret < 0) {
413 goto out;
414 }
415 ret = nfs_ftruncate(client->context, client->fh, total_size);
416 nfs_client_close(client);
417out:
418 g_free(client);
419 return ret;
420}
421
422static int nfs_has_zero_init(BlockDriverState *bs)
423{
424 NFSClient *client = bs->opaque;
425 return client->has_zero_init;
426}
427
428static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
429{
430 NFSClient *client = bs->opaque;
431 NFSRPC task = {0};
432 struct stat st;
433
434 task.st = &st;
435 if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
436 &task) != 0) {
437 return -ENOMEM;
438 }
439
440 while (!task.complete) {
441 nfs_set_events(client);
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200442 aio_poll(client->aio_context, true);
Peter Lieven6542aa92014-02-03 10:26:13 +0100443 }
444
445 return (task.ret < 0 ? task.ret : st.st_blocks * st.st_blksize);
446}
447
448static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
449{
450 NFSClient *client = bs->opaque;
451 return nfs_ftruncate(client->context, client->fh, offset);
452}
453
454static BlockDriver bdrv_nfs = {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200455 .format_name = "nfs",
456 .protocol_name = "nfs",
Peter Lieven6542aa92014-02-03 10:26:13 +0100457
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200458 .instance_size = sizeof(NFSClient),
459 .bdrv_needs_filename = true,
460 .bdrv_has_zero_init = nfs_has_zero_init,
461 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
462 .bdrv_truncate = nfs_file_truncate,
Peter Lieven6542aa92014-02-03 10:26:13 +0100463
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200464 .bdrv_file_open = nfs_file_open,
465 .bdrv_close = nfs_file_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800466 .bdrv_create = nfs_file_create,
Peter Lieven6542aa92014-02-03 10:26:13 +0100467
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200468 .bdrv_co_readv = nfs_co_readv,
469 .bdrv_co_writev = nfs_co_writev,
470 .bdrv_co_flush_to_disk = nfs_co_flush,
471
472 .bdrv_detach_aio_context = nfs_detach_aio_context,
473 .bdrv_attach_aio_context = nfs_attach_aio_context,
Peter Lieven6542aa92014-02-03 10:26:13 +0100474};
475
476static void nfs_block_init(void)
477{
478 bdrv_register(&bdrv_nfs);
479}
480
481block_init(nfs_block_init);