1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.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 "qapi/error.h"
32#include "block/block_int.h"
33#include "trace.h"
34#include "qemu/iov.h"
35#include "qemu/uri.h"
36#include "qemu/cutils.h"
37#include "sysemu/sysemu.h"
38#include <nfsc/libnfs.h>
39
40#define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
41#define QEMU_NFS_MAX_DEBUG_LEVEL 2
42
43typedef struct NFSClient {
44 struct nfs_context *context;
45 struct nfsfh *fh;
46 int events;
47 bool has_zero_init;
48 AioContext *aio_context;
49 blkcnt_t st_blocks;
50 bool cache_used;
51} NFSClient;
52
53typedef struct NFSRPC {
54 int ret;
55 int complete;
56 QEMUIOVector *iov;
57 struct stat *st;
58 Coroutine *co;
59 QEMUBH *bh;
60 NFSClient *client;
61} NFSRPC;
62
63static void nfs_process_read(void *arg);
64static void nfs_process_write(void *arg);
65
66static void nfs_set_events(NFSClient *client)
67{
68 int ev = nfs_which_events(client->context);
69 if (ev != client->events) {
70 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
71 false,
72 (ev & POLLIN) ? nfs_process_read : NULL,
73 (ev & POLLOUT) ? nfs_process_write : NULL, client);
74
75 }
76 client->events = ev;
77}
78
79static void nfs_process_read(void *arg)
80{
81 NFSClient *client = arg;
82 nfs_service(client->context, POLLIN);
83 nfs_set_events(client);
84}
85
86static void nfs_process_write(void *arg)
87{
88 NFSClient *client = arg;
89 nfs_service(client->context, POLLOUT);
90 nfs_set_events(client);
91}
92
93static void nfs_co_init_task(NFSClient *client, NFSRPC *task)
94{
95 *task = (NFSRPC) {
96 .co = qemu_coroutine_self(),
97 .client = client,
98 };
99}
100
101static void nfs_co_generic_bh_cb(void *opaque)
102{
103 NFSRPC *task = opaque;
104 task->complete = 1;
105 qemu_bh_delete(task->bh);
106 qemu_coroutine_enter(task->co, NULL);
107}
108
109static void
110nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
111 void *private_data)
112{
113 NFSRPC *task = private_data;
114 task->ret = ret;
115 if (task->ret > 0 && task->iov) {
116 if (task->ret <= task->iov->size) {
117 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
118 } else {
119 task->ret = -EIO;
120 }
121 }
122 if (task->ret == 0 && task->st) {
123 memcpy(task->st, data, sizeof(struct stat));
124 }
125 if (task->ret < 0) {
126 error_report("NFS Error: %s", nfs_get_error(nfs));
127 }
128 if (task->co) {
129 task->bh = aio_bh_new(task->client->aio_context,
130 nfs_co_generic_bh_cb, task);
131 qemu_bh_schedule(task->bh);
132 } else {
133 task->complete = 1;
134 }
135}
136
137static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
138 int64_t sector_num, int nb_sectors,
139 QEMUIOVector *iov)
140{
141 NFSClient *client = bs->opaque;
142 NFSRPC task;
143
144 nfs_co_init_task(client, &task);
145 task.iov = iov;
146
147 if (nfs_pread_async(client->context, client->fh,
148 sector_num * BDRV_SECTOR_SIZE,
149 nb_sectors * BDRV_SECTOR_SIZE,
150 nfs_co_generic_cb, &task) != 0) {
151 return -ENOMEM;
152 }
153
154 while (!task.complete) {
155 nfs_set_events(client);
156 qemu_coroutine_yield();
157 }
158
159 if (task.ret < 0) {
160 return task.ret;
161 }
162
163
164 if (task.ret < iov->size) {
165 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
166 }
167
168 return 0;
169}
170
171static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
172 int64_t sector_num, int nb_sectors,
173 QEMUIOVector *iov)
174{
175 NFSClient *client = bs->opaque;
176 NFSRPC task;
177 char *buf = NULL;
178
179 nfs_co_init_task(client, &task);
180
181 buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
182 if (nb_sectors && buf == NULL) {
183 return -ENOMEM;
184 }
185
186 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
187
188 if (nfs_pwrite_async(client->context, client->fh,
189 sector_num * BDRV_SECTOR_SIZE,
190 nb_sectors * BDRV_SECTOR_SIZE,
191 buf, nfs_co_generic_cb, &task) != 0) {
192 g_free(buf);
193 return -ENOMEM;
194 }
195
196 while (!task.complete) {
197 nfs_set_events(client);
198 qemu_coroutine_yield();
199 }
200
201 g_free(buf);
202
203 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
204 return task.ret < 0 ? task.ret : -EIO;
205 }
206
207 return 0;
208}
209
210static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
211{
212 NFSClient *client = bs->opaque;
213 NFSRPC task;
214
215 nfs_co_init_task(client, &task);
216
217 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
218 &task) != 0) {
219 return -ENOMEM;
220 }
221
222 while (!task.complete) {
223 nfs_set_events(client);
224 qemu_coroutine_yield();
225 }
226
227 return task.ret;
228}
229
230
231static QemuOptsList runtime_opts = {
232 .name = "nfs",
233 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
234 .desc = {
235 {
236 .name = "filename",
237 .type = QEMU_OPT_STRING,
238 .help = "URL to the NFS file",
239 },
240 { }
241 },
242};
243
244static void nfs_detach_aio_context(BlockDriverState *bs)
245{
246 NFSClient *client = bs->opaque;
247
248 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
249 false, NULL, NULL, NULL);
250 client->events = 0;
251}
252
253static void nfs_attach_aio_context(BlockDriverState *bs,
254 AioContext *new_context)
255{
256 NFSClient *client = bs->opaque;
257
258 client->aio_context = new_context;
259 nfs_set_events(client);
260}
261
262static void nfs_client_close(NFSClient *client)
263{
264 if (client->context) {
265 if (client->fh) {
266 nfs_close(client->context, client->fh);
267 }
268 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
269 false, NULL, NULL, NULL);
270 nfs_destroy_context(client->context);
271 }
272 memset(client, 0, sizeof(NFSClient));
273}
274
275static void nfs_file_close(BlockDriverState *bs)
276{
277 NFSClient *client = bs->opaque;
278 nfs_client_close(client);
279}
280
281static int64_t nfs_client_open(NFSClient *client, const char *filename,
282 int flags, Error **errp, int open_flags)
283{
284 int ret = -EINVAL, i;
285 struct stat st;
286 URI *uri;
287 QueryParams *qp = NULL;
288 char *file = NULL, *strp = NULL;
289
290 uri = uri_parse(filename);
291 if (!uri) {
292 error_setg(errp, "Invalid URL specified");
293 goto fail;
294 }
295 if (!uri->server) {
296 error_setg(errp, "Invalid URL specified");
297 goto fail;
298 }
299 strp = strrchr(uri->path, '/');
300 if (strp == NULL) {
301 error_setg(errp, "Invalid URL specified");
302 goto fail;
303 }
304 file = g_strdup(strp);
305 *strp = 0;
306
307 client->context = nfs_init_context();
308 if (client->context == NULL) {
309 error_setg(errp, "Failed to init NFS context");
310 goto fail;
311 }
312
313 qp = query_params_parse(uri->query);
314 for (i = 0; i < qp->n; i++) {
315 unsigned long long val;
316 if (!qp->p[i].value) {
317 error_setg(errp, "Value for NFS parameter expected: %s",
318 qp->p[i].name);
319 goto fail;
320 }
321 if (parse_uint_full(qp->p[i].value, &val, 0)) {
322 error_setg(errp, "Illegal value for NFS parameter: %s",
323 qp->p[i].name);
324 goto fail;
325 }
326 if (!strcmp(qp->p[i].name, "uid")) {
327 nfs_set_uid(client->context, val);
328 } else if (!strcmp(qp->p[i].name, "gid")) {
329 nfs_set_gid(client->context, val);
330 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
331 nfs_set_tcp_syncnt(client->context, val);
332#ifdef LIBNFS_FEATURE_READAHEAD
333 } else if (!strcmp(qp->p[i].name, "readahead")) {
334 if (open_flags & BDRV_O_NOCACHE) {
335 error_setg(errp, "Cannot enable NFS readahead "
336 "if cache.direct = on");
337 goto fail;
338 }
339 if (val > QEMU_NFS_MAX_READAHEAD_SIZE) {
340 error_report("NFS Warning: Truncating NFS readahead"
341 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
342 val = QEMU_NFS_MAX_READAHEAD_SIZE;
343 }
344 nfs_set_readahead(client->context, val);
345 client->cache_used = true;
346#endif
347#ifdef LIBNFS_FEATURE_DEBUG
348 } else if (!strcmp(qp->p[i].name, "debug")) {
349
350
351 if (val > QEMU_NFS_MAX_DEBUG_LEVEL) {
352 error_report("NFS Warning: Limiting NFS debug level"
353 " to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
354 val = QEMU_NFS_MAX_DEBUG_LEVEL;
355 }
356 nfs_set_debug(client->context, val);
357#endif
358 } else {
359 error_setg(errp, "Unknown NFS parameter name: %s",
360 qp->p[i].name);
361 goto fail;
362 }
363 }
364
365 ret = nfs_mount(client->context, uri->server, uri->path);
366 if (ret < 0) {
367 error_setg(errp, "Failed to mount nfs share: %s",
368 nfs_get_error(client->context));
369 goto fail;
370 }
371
372 if (flags & O_CREAT) {
373 ret = nfs_creat(client->context, file, 0600, &client->fh);
374 if (ret < 0) {
375 error_setg(errp, "Failed to create file: %s",
376 nfs_get_error(client->context));
377 goto fail;
378 }
379 } else {
380 ret = nfs_open(client->context, file, flags, &client->fh);
381 if (ret < 0) {
382 error_setg(errp, "Failed to open file : %s",
383 nfs_get_error(client->context));
384 goto fail;
385 }
386 }
387
388 ret = nfs_fstat(client->context, client->fh, &st);
389 if (ret < 0) {
390 error_setg(errp, "Failed to fstat file: %s",
391 nfs_get_error(client->context));
392 goto fail;
393 }
394
395 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
396 client->st_blocks = st.st_blocks;
397 client->has_zero_init = S_ISREG(st.st_mode);
398 goto out;
399fail:
400 nfs_client_close(client);
401out:
402 if (qp) {
403 query_params_free(qp);
404 }
405 uri_free(uri);
406 g_free(file);
407 return ret;
408}
409
410static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
411 Error **errp) {
412 NFSClient *client = bs->opaque;
413 int64_t ret;
414 QemuOpts *opts;
415 Error *local_err = NULL;
416
417 client->aio_context = bdrv_get_aio_context(bs);
418
419 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
420 qemu_opts_absorb_qdict(opts, options, &local_err);
421 if (local_err) {
422 error_propagate(errp, local_err);
423 ret = -EINVAL;
424 goto out;
425 }
426 ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
427 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
428 errp, bs->open_flags);
429 if (ret < 0) {
430 goto out;
431 }
432 bs->total_sectors = ret;
433 ret = 0;
434out:
435 qemu_opts_del(opts);
436 return ret;
437}
438
439static QemuOptsList nfs_create_opts = {
440 .name = "nfs-create-opts",
441 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
442 .desc = {
443 {
444 .name = BLOCK_OPT_SIZE,
445 .type = QEMU_OPT_SIZE,
446 .help = "Virtual disk size"
447 },
448 { }
449 }
450};
451
452static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
453{
454 int ret = 0;
455 int64_t total_size = 0;
456 NFSClient *client = g_new0(NFSClient, 1);
457
458 client->aio_context = qemu_get_aio_context();
459
460
461 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
462 BDRV_SECTOR_SIZE);
463
464 ret = nfs_client_open(client, url, O_CREAT, errp, 0);
465 if (ret < 0) {
466 goto out;
467 }
468 ret = nfs_ftruncate(client->context, client->fh, total_size);
469 nfs_client_close(client);
470out:
471 g_free(client);
472 return ret;
473}
474
475static int nfs_has_zero_init(BlockDriverState *bs)
476{
477 NFSClient *client = bs->opaque;
478 return client->has_zero_init;
479}
480
481static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
482{
483 NFSClient *client = bs->opaque;
484 NFSRPC task = {0};
485 struct stat st;
486
487 if (bdrv_is_read_only(bs) &&
488 !(bs->open_flags & BDRV_O_NOCACHE)) {
489 return client->st_blocks * 512;
490 }
491
492 task.st = &st;
493 if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
494 &task) != 0) {
495 return -ENOMEM;
496 }
497
498 while (!task.complete) {
499 nfs_set_events(client);
500 aio_poll(client->aio_context, true);
501 }
502
503 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
504}
505
506static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
507{
508 NFSClient *client = bs->opaque;
509 return nfs_ftruncate(client->context, client->fh, offset);
510}
511
512
513
514static int nfs_reopen_prepare(BDRVReopenState *state,
515 BlockReopenQueue *queue, Error **errp)
516{
517 NFSClient *client = state->bs->opaque;
518 struct stat st;
519 int ret = 0;
520
521 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
522 error_setg(errp, "Cannot open a read-only mount as read-write");
523 return -EACCES;
524 }
525
526 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
527 error_setg(errp, "Cannot disable cache if libnfs readahead is enabled");
528 return -EINVAL;
529 }
530
531
532 if (!(state->flags & BDRV_O_RDWR)) {
533 ret = nfs_fstat(client->context, client->fh, &st);
534 if (ret < 0) {
535 error_setg(errp, "Failed to fstat file: %s",
536 nfs_get_error(client->context));
537 return ret;
538 }
539 client->st_blocks = st.st_blocks;
540 }
541
542 return 0;
543}
544
545static BlockDriver bdrv_nfs = {
546 .format_name = "nfs",
547 .protocol_name = "nfs",
548
549 .instance_size = sizeof(NFSClient),
550 .bdrv_needs_filename = true,
551 .create_opts = &nfs_create_opts,
552
553 .bdrv_has_zero_init = nfs_has_zero_init,
554 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
555 .bdrv_truncate = nfs_file_truncate,
556
557 .bdrv_file_open = nfs_file_open,
558 .bdrv_close = nfs_file_close,
559 .bdrv_create = nfs_file_create,
560 .bdrv_reopen_prepare = nfs_reopen_prepare,
561
562 .bdrv_co_readv = nfs_co_readv,
563 .bdrv_co_writev = nfs_co_writev,
564 .bdrv_co_flush_to_disk = nfs_co_flush,
565
566 .bdrv_detach_aio_context = nfs_detach_aio_context,
567 .bdrv_attach_aio_context = nfs_attach_aio_context,
568};
569
570static void nfs_block_init(void)
571{
572 bdrv_register(&bdrv_nfs);
573}
574
575block_init(nfs_block_init);
576