]> git.proxmox.com Git - mirror_qemu.git/blame - block/nfs.c
qobject: Use simpler QDict/QList scalar insertion macros
[mirror_qemu.git] / block / nfs.c
CommitLineData
6542aa9c
PL
1/*
2 * QEMU Block driver for native access to files on NFS shares
3 *
38f8d5e0 4 * Copyright (c) 2014-2016 Peter Lieven <pl@kamp.de>
6542aa9c
PL
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
80c71a24 25#include "qemu/osdep.h"
6542aa9c
PL
26
27#include <poll.h>
28#include "qemu-common.h"
29#include "qemu/config-file.h"
30#include "qemu/error-report.h"
d165b8cb 31#include "qapi/error.h"
6542aa9c
PL
32#include "block/block_int.h"
33#include "trace.h"
34#include "qemu/iov.h"
35#include "qemu/uri.h"
0d94b746 36#include "qemu/cutils.h"
6542aa9c 37#include "sysemu/sysemu.h"
94d6a7a7
AA
38#include "qapi/qmp/qdict.h"
39#include "qapi/qmp/qint.h"
40#include "qapi/qmp/qstring.h"
41#include "qapi-visit.h"
42#include "qapi/qobject-input-visitor.h"
43#include "qapi/qobject-output-visitor.h"
6542aa9c
PL
44#include <nfsc/libnfs.h>
45
94d6a7a7 46
29c838cd 47#define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
d99b26c4 48#define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
7725b8bf 49#define QEMU_NFS_MAX_DEBUG_LEVEL 2
29c838cd 50
6542aa9c
PL
51typedef struct NFSClient {
52 struct nfs_context *context;
53 struct nfsfh *fh;
54 int events;
55 bool has_zero_init;
471799d1 56 AioContext *aio_context;
37d1e4d9 57 QemuMutex mutex;
18a8056e 58 blkcnt_t st_blocks;
38f8d5e0 59 bool cache_used;
94d6a7a7
AA
60 NFSServer *server;
61 char *path;
62 int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
6542aa9c
PL
63} NFSClient;
64
65typedef struct NFSRPC {
d746427a 66 BlockDriverState *bs;
6542aa9c
PL
67 int ret;
68 int complete;
69 QEMUIOVector *iov;
70 struct stat *st;
71 Coroutine *co;
471799d1 72 NFSClient *client;
6542aa9c
PL
73} NFSRPC;
74
94d6a7a7
AA
75static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
76{
77 URI *uri = NULL;
78 QueryParams *qp = NULL;
79 int ret = -EINVAL, i;
80
81 uri = uri_parse(filename);
82 if (!uri) {
83 error_setg(errp, "Invalid URI specified");
84 goto out;
85 }
86 if (strcmp(uri->scheme, "nfs") != 0) {
87 error_setg(errp, "URI scheme must be 'nfs'");
88 goto out;
89 }
90
91 if (!uri->server) {
92 error_setg(errp, "missing hostname in URI");
93 goto out;
94 }
95
96 if (!uri->path) {
97 error_setg(errp, "missing file path in URI");
98 goto out;
99 }
100
101 qp = query_params_parse(uri->query);
102 if (!qp) {
103 error_setg(errp, "could not parse query parameters");
104 goto out;
105 }
106
e59084b5
EB
107 qdict_put_str(options, "server.host", uri->server);
108 qdict_put_str(options, "server.type", "inet");
109 qdict_put_str(options, "path", uri->path);
94d6a7a7
AA
110
111 for (i = 0; i < qp->n; i++) {
8d20abe8 112 unsigned long long val;
94d6a7a7
AA
113 if (!qp->p[i].value) {
114 error_setg(errp, "Value for NFS parameter expected: %s",
115 qp->p[i].name);
116 goto out;
117 }
8d20abe8 118 if (parse_uint_full(qp->p[i].value, &val, 0)) {
94d6a7a7
AA
119 error_setg(errp, "Illegal value for NFS parameter: %s",
120 qp->p[i].name);
121 goto out;
122 }
123 if (!strcmp(qp->p[i].name, "uid")) {
e59084b5 124 qdict_put_str(options, "user", qp->p[i].value);
94d6a7a7 125 } else if (!strcmp(qp->p[i].name, "gid")) {
e59084b5 126 qdict_put_str(options, "group", qp->p[i].value);
94d6a7a7 127 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
e59084b5 128 qdict_put_str(options, "tcp-syn-count", qp->p[i].value);
94d6a7a7 129 } else if (!strcmp(qp->p[i].name, "readahead")) {
e59084b5 130 qdict_put_str(options, "readahead-size", qp->p[i].value);
94d6a7a7 131 } else if (!strcmp(qp->p[i].name, "pagecache")) {
e59084b5 132 qdict_put_str(options, "page-cache-size", qp->p[i].value);
94d6a7a7 133 } else if (!strcmp(qp->p[i].name, "debug")) {
e59084b5 134 qdict_put_str(options, "debug", qp->p[i].value);
94d6a7a7
AA
135 } else {
136 error_setg(errp, "Unknown NFS parameter name: %s",
137 qp->p[i].name);
138 goto out;
139 }
140 }
141 ret = 0;
142out:
143 if (qp) {
144 query_params_free(qp);
145 }
146 if (uri) {
147 uri_free(uri);
148 }
149 return ret;
150}
151
152static bool nfs_has_filename_options_conflict(QDict *options, Error **errp)
153{
154 const QDictEntry *qe;
155
156 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
157 if (!strcmp(qe->key, "host") ||
158 !strcmp(qe->key, "path") ||
159 !strcmp(qe->key, "user") ||
160 !strcmp(qe->key, "group") ||
161 !strcmp(qe->key, "tcp-syn-count") ||
162 !strcmp(qe->key, "readahead-size") ||
163 !strcmp(qe->key, "page-cache-size") ||
7103d916 164 !strcmp(qe->key, "debug") ||
94d6a7a7
AA
165 strstart(qe->key, "server.", NULL))
166 {
167 error_setg(errp, "Option %s cannot be used with a filename",
168 qe->key);
169 return true;
170 }
171 }
172
173 return false;
174}
175
176static void nfs_parse_filename(const char *filename, QDict *options,
177 Error **errp)
178{
179 if (nfs_has_filename_options_conflict(options, errp)) {
180 return;
181 }
182
183 nfs_parse_uri(filename, options, errp);
184}
185
6542aa9c
PL
186static void nfs_process_read(void *arg);
187static void nfs_process_write(void *arg);
188
37d1e4d9 189/* Called with QemuMutex held. */
6542aa9c
PL
190static void nfs_set_events(NFSClient *client)
191{
192 int ev = nfs_which_events(client->context);
193 if (ev != client->events) {
dca21ef2
FZ
194 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
195 false,
471799d1 196 (ev & POLLIN) ? nfs_process_read : NULL,
f6a51c84
SH
197 (ev & POLLOUT) ? nfs_process_write : NULL,
198 NULL, client);
6542aa9c
PL
199
200 }
201 client->events = ev;
202}
203
204static void nfs_process_read(void *arg)
205{
206 NFSClient *client = arg;
9d456654 207
37d1e4d9 208 qemu_mutex_lock(&client->mutex);
6542aa9c
PL
209 nfs_service(client->context, POLLIN);
210 nfs_set_events(client);
37d1e4d9 211 qemu_mutex_unlock(&client->mutex);
6542aa9c
PL
212}
213
214static void nfs_process_write(void *arg)
215{
216 NFSClient *client = arg;
9d456654 217
37d1e4d9 218 qemu_mutex_lock(&client->mutex);
6542aa9c
PL
219 nfs_service(client->context, POLLOUT);
220 nfs_set_events(client);
37d1e4d9 221 qemu_mutex_unlock(&client->mutex);
6542aa9c
PL
222}
223
d746427a 224static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
6542aa9c
PL
225{
226 *task = (NFSRPC) {
471799d1 227 .co = qemu_coroutine_self(),
d746427a
PB
228 .bs = bs,
229 .client = bs->opaque,
6542aa9c
PL
230 };
231}
232
233static void nfs_co_generic_bh_cb(void *opaque)
234{
235 NFSRPC *task = opaque;
1919631e 236
a2c0fe2f 237 task->complete = 1;
1919631e 238 aio_co_wake(task->co);
6542aa9c
PL
239}
240
37d1e4d9 241/* Called (via nfs_service) with QemuMutex held. */
6542aa9c
PL
242static void
243nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
244 void *private_data)
245{
246 NFSRPC *task = private_data;
6542aa9c 247 task->ret = ret;
d746427a 248 assert(!task->st);
6542aa9c
PL
249 if (task->ret > 0 && task->iov) {
250 if (task->ret <= task->iov->size) {
251 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
252 } else {
253 task->ret = -EIO;
254 }
255 }
20fccb18
PL
256 if (task->ret < 0) {
257 error_report("NFS Error: %s", nfs_get_error(nfs));
258 }
d746427a
PB
259 aio_bh_schedule_oneshot(task->client->aio_context,
260 nfs_co_generic_bh_cb, task);
6542aa9c
PL
261}
262
69785a22
PL
263static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, uint64_t offset,
264 uint64_t bytes, QEMUIOVector *iov,
265 int flags)
6542aa9c
PL
266{
267 NFSClient *client = bs->opaque;
268 NFSRPC task;
269
d746427a 270 nfs_co_init_task(bs, &task);
6542aa9c
PL
271 task.iov = iov;
272
37d1e4d9 273 qemu_mutex_lock(&client->mutex);
6542aa9c 274 if (nfs_pread_async(client->context, client->fh,
69785a22 275 offset, bytes, nfs_co_generic_cb, &task) != 0) {
37d1e4d9 276 qemu_mutex_unlock(&client->mutex);
6542aa9c
PL
277 return -ENOMEM;
278 }
279
aa92d6c4 280 nfs_set_events(client);
37d1e4d9 281 qemu_mutex_unlock(&client->mutex);
6542aa9c 282 while (!task.complete) {
6542aa9c
PL
283 qemu_coroutine_yield();
284 }
285
286 if (task.ret < 0) {
287 return task.ret;
288 }
289
290 /* zero pad short reads */
291 if (task.ret < iov->size) {
292 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
293 }
294
295 return 0;
296}
297
69785a22
PL
298static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, uint64_t offset,
299 uint64_t bytes, QEMUIOVector *iov,
300 int flags)
6542aa9c
PL
301{
302 NFSClient *client = bs->opaque;
303 NFSRPC task;
304 char *buf = NULL;
ef503a84 305 bool my_buffer = false;
6542aa9c 306
d746427a 307 nfs_co_init_task(bs, &task);
6542aa9c 308
ef503a84
PL
309 if (iov->niov != 1) {
310 buf = g_try_malloc(bytes);
311 if (bytes && buf == NULL) {
312 return -ENOMEM;
313 }
314 qemu_iovec_to_buf(iov, 0, buf, bytes);
315 my_buffer = true;
316 } else {
317 buf = iov->iov[0].iov_base;
2347dd7b
KW
318 }
319
37d1e4d9 320 qemu_mutex_lock(&client->mutex);
6542aa9c 321 if (nfs_pwrite_async(client->context, client->fh,
69785a22
PL
322 offset, bytes, buf,
323 nfs_co_generic_cb, &task) != 0) {
37d1e4d9 324 qemu_mutex_unlock(&client->mutex);
ef503a84
PL
325 if (my_buffer) {
326 g_free(buf);
327 }
6542aa9c
PL
328 return -ENOMEM;
329 }
330
aa92d6c4 331 nfs_set_events(client);
37d1e4d9 332 qemu_mutex_unlock(&client->mutex);
6542aa9c 333 while (!task.complete) {
6542aa9c
PL
334 qemu_coroutine_yield();
335 }
336
ef503a84
PL
337 if (my_buffer) {
338 g_free(buf);
339 }
6542aa9c 340
69785a22 341 if (task.ret != bytes) {
6542aa9c
PL
342 return task.ret < 0 ? task.ret : -EIO;
343 }
344
345 return 0;
346}
347
348static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
349{
350 NFSClient *client = bs->opaque;
351 NFSRPC task;
352
d746427a 353 nfs_co_init_task(bs, &task);
6542aa9c 354
37d1e4d9 355 qemu_mutex_lock(&client->mutex);
6542aa9c
PL
356 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
357 &task) != 0) {
37d1e4d9 358 qemu_mutex_unlock(&client->mutex);
6542aa9c
PL
359 return -ENOMEM;
360 }
361
aa92d6c4 362 nfs_set_events(client);
37d1e4d9 363 qemu_mutex_unlock(&client->mutex);
6542aa9c 364 while (!task.complete) {
6542aa9c
PL
365 qemu_coroutine_yield();
366 }
367
368 return task.ret;
369}
370
6542aa9c
PL
371static QemuOptsList runtime_opts = {
372 .name = "nfs",
373 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
374 .desc = {
375 {
94d6a7a7 376 .name = "path",
6542aa9c 377 .type = QEMU_OPT_STRING,
94d6a7a7
AA
378 .help = "Path of the image on the host",
379 },
380 {
f67409a5 381 .name = "user",
94d6a7a7
AA
382 .type = QEMU_OPT_NUMBER,
383 .help = "UID value to use when talking to the server",
384 },
385 {
f67409a5 386 .name = "group",
94d6a7a7
AA
387 .type = QEMU_OPT_NUMBER,
388 .help = "GID value to use when talking to the server",
389 },
390 {
f67409a5 391 .name = "tcp-syn-count",
94d6a7a7
AA
392 .type = QEMU_OPT_NUMBER,
393 .help = "Number of SYNs to send during the session establish",
394 },
395 {
f67409a5 396 .name = "readahead-size",
94d6a7a7
AA
397 .type = QEMU_OPT_NUMBER,
398 .help = "Set the readahead size in bytes",
399 },
400 {
f67409a5 401 .name = "page-cache-size",
94d6a7a7
AA
402 .type = QEMU_OPT_NUMBER,
403 .help = "Set the pagecache size in bytes",
404 },
405 {
406 .name = "debug",
407 .type = QEMU_OPT_NUMBER,
408 .help = "Set the NFS debug level (max 2)",
6542aa9c
PL
409 },
410 { /* end of list */ }
411 },
412};
413
471799d1
SH
414static void nfs_detach_aio_context(BlockDriverState *bs)
415{
416 NFSClient *client = bs->opaque;
417
dca21ef2 418 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
f6a51c84 419 false, NULL, NULL, NULL, NULL);
471799d1
SH
420 client->events = 0;
421}
422
423static void nfs_attach_aio_context(BlockDriverState *bs,
424 AioContext *new_context)
425{
426 NFSClient *client = bs->opaque;
427
428 client->aio_context = new_context;
429 nfs_set_events(client);
430}
431
6542aa9c
PL
432static void nfs_client_close(NFSClient *client)
433{
434 if (client->context) {
435 if (client->fh) {
436 nfs_close(client->context, client->fh);
437 }
dca21ef2 438 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
f6a51c84 439 false, NULL, NULL, NULL, NULL);
6542aa9c
PL
440 nfs_destroy_context(client->context);
441 }
442 memset(client, 0, sizeof(NFSClient));
443}
444
445static void nfs_file_close(BlockDriverState *bs)
446{
447 NFSClient *client = bs->opaque;
448 nfs_client_close(client);
37d1e4d9 449 qemu_mutex_destroy(&client->mutex);
6542aa9c
PL
450}
451
94d6a7a7
AA
452static NFSServer *nfs_config(QDict *options, Error **errp)
453{
454 NFSServer *server = NULL;
455 QDict *addr = NULL;
456 QObject *crumpled_addr = NULL;
457 Visitor *iv = NULL;
458 Error *local_error = NULL;
459
460 qdict_extract_subqdict(options, &addr, "server.");
461 if (!qdict_size(addr)) {
462 error_setg(errp, "NFS server address missing");
463 goto out;
464 }
465
466 crumpled_addr = qdict_crumple(addr, errp);
467 if (!crumpled_addr) {
468 goto out;
469 }
470
129c7d1c
MA
471 /*
472 * Caution: this works only because all scalar members of
473 * NFSServer are QString in @crumpled_addr. The visitor expects
474 * @crumpled_addr to be typed according to the QAPI schema. It
475 * is when @options come from -blockdev or blockdev_add. But when
476 * they come from -drive, they're all QString.
477 */
048abb7b 478 iv = qobject_input_visitor_new(crumpled_addr);
94d6a7a7
AA
479 visit_type_NFSServer(iv, NULL, &server, &local_error);
480 if (local_error) {
481 error_propagate(errp, local_error);
482 goto out;
483 }
484
485out:
486 QDECREF(addr);
487 qobject_decref(crumpled_addr);
488 visit_free(iv);
489 return server;
490}
491
492
493static int64_t nfs_client_open(NFSClient *client, QDict *options,
38f8d5e0 494 int flags, Error **errp, int open_flags)
6542aa9c 495{
94d6a7a7
AA
496 int ret = -EINVAL;
497 QemuOpts *opts = NULL;
498 Error *local_err = NULL;
6542aa9c 499 struct stat st;
6542aa9c
PL
500 char *file = NULL, *strp = NULL;
501
94d6a7a7
AA
502 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
503 qemu_opts_absorb_qdict(opts, options, &local_err);
504 if (local_err) {
505 error_propagate(errp, local_err);
506 ret = -EINVAL;
5f4d5e1a
HR
507 goto fail;
508 }
94d6a7a7
AA
509
510 client->path = g_strdup(qemu_opt_get(opts, "path"));
511 if (!client->path) {
512 ret = -EINVAL;
513 error_setg(errp, "No path was specified");
6542aa9c
PL
514 goto fail;
515 }
94d6a7a7
AA
516
517 strp = strrchr(client->path, '/');
6542aa9c
PL
518 if (strp == NULL) {
519 error_setg(errp, "Invalid URL specified");
520 goto fail;
521 }
522 file = g_strdup(strp);
523 *strp = 0;
524
94d6a7a7
AA
525 /* Pop the config into our state object, Exit if invalid */
526 client->server = nfs_config(options, errp);
527 if (!client->server) {
528 ret = -EINVAL;
529 goto fail;
530 }
531
6542aa9c
PL
532 client->context = nfs_init_context();
533 if (client->context == NULL) {
534 error_setg(errp, "Failed to init NFS context");
535 goto fail;
536 }
537
f67409a5
PL
538 if (qemu_opt_get(opts, "user")) {
539 client->uid = qemu_opt_get_number(opts, "user", 0);
94d6a7a7
AA
540 nfs_set_uid(client->context, client->uid);
541 }
542
f67409a5
PL
543 if (qemu_opt_get(opts, "group")) {
544 client->gid = qemu_opt_get_number(opts, "group", 0);
94d6a7a7
AA
545 nfs_set_gid(client->context, client->gid);
546 }
547
f67409a5
PL
548 if (qemu_opt_get(opts, "tcp-syn-count")) {
549 client->tcp_syncnt = qemu_opt_get_number(opts, "tcp-syn-count", 0);
94d6a7a7
AA
550 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
551 }
552
553#ifdef LIBNFS_FEATURE_READAHEAD
f67409a5 554 if (qemu_opt_get(opts, "readahead-size")) {
94d6a7a7
AA
555 if (open_flags & BDRV_O_NOCACHE) {
556 error_setg(errp, "Cannot enable NFS readahead "
557 "if cache.direct = on");
6542aa9c
PL
558 goto fail;
559 }
f67409a5 560 client->readahead = qemu_opt_get_number(opts, "readahead-size", 0);
94d6a7a7
AA
561 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
562 error_report("NFS Warning: Truncating NFS readahead "
563 "size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
564 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
7c24384b 565 }
94d6a7a7 566 nfs_set_readahead(client->context, client->readahead);
d99b26c4 567#ifdef LIBNFS_FEATURE_PAGECACHE
94d6a7a7 568 nfs_set_pagecache_ttl(client->context, 0);
d99b26c4 569#endif
94d6a7a7
AA
570 client->cache_used = true;
571 }
d99b26c4 572#endif
94d6a7a7 573
d99b26c4 574#ifdef LIBNFS_FEATURE_PAGECACHE
f67409a5 575 if (qemu_opt_get(opts, "page-cache-size")) {
94d6a7a7
AA
576 if (open_flags & BDRV_O_NOCACHE) {
577 error_setg(errp, "Cannot enable NFS pagecache "
578 "if cache.direct = on");
579 goto fail;
580 }
f67409a5 581 client->pagecache = qemu_opt_get_number(opts, "page-cache-size", 0);
94d6a7a7
AA
582 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
583 error_report("NFS Warning: Truncating NFS pagecache "
584 "size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
585 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
586 }
587 nfs_set_pagecache(client->context, client->pagecache);
588 nfs_set_pagecache_ttl(client->context, 0);
589 client->cache_used = true;
590 }
7725b8bf 591#endif
94d6a7a7 592
7725b8bf 593#ifdef LIBNFS_FEATURE_DEBUG
94d6a7a7
AA
594 if (qemu_opt_get(opts, "debug")) {
595 client->debug = qemu_opt_get_number(opts, "debug", 0);
596 /* limit the maximum debug level to avoid potential flooding
597 * of our log files. */
598 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
599 error_report("NFS Warning: Limiting NFS debug level "
600 "to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
601 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
6542aa9c 602 }
94d6a7a7 603 nfs_set_debug(client->context, client->debug);
6542aa9c 604 }
94d6a7a7 605#endif
6542aa9c 606
94d6a7a7 607 ret = nfs_mount(client->context, client->server->host, client->path);
6542aa9c
PL
608 if (ret < 0) {
609 error_setg(errp, "Failed to mount nfs share: %s",
610 nfs_get_error(client->context));
611 goto fail;
612 }
613
614 if (flags & O_CREAT) {
615 ret = nfs_creat(client->context, file, 0600, &client->fh);
616 if (ret < 0) {
617 error_setg(errp, "Failed to create file: %s",
618 nfs_get_error(client->context));
619 goto fail;
620 }
621 } else {
622 ret = nfs_open(client->context, file, flags, &client->fh);
623 if (ret < 0) {
624 error_setg(errp, "Failed to open file : %s",
625 nfs_get_error(client->context));
626 goto fail;
627 }
628 }
629
630 ret = nfs_fstat(client->context, client->fh, &st);
631 if (ret < 0) {
632 error_setg(errp, "Failed to fstat file: %s",
633 nfs_get_error(client->context));
634 goto fail;
635 }
636
637 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
18a8056e 638 client->st_blocks = st.st_blocks;
6542aa9c 639 client->has_zero_init = S_ISREG(st.st_mode);
94d6a7a7 640 *strp = '/';
6542aa9c 641 goto out;
94d6a7a7 642
6542aa9c
PL
643fail:
644 nfs_client_close(client);
645out:
94d6a7a7 646 qemu_opts_del(opts);
6542aa9c
PL
647 g_free(file);
648 return ret;
649}
650
651static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
652 Error **errp) {
653 NFSClient *client = bs->opaque;
654 int64_t ret;
6542aa9c 655
471799d1
SH
656 client->aio_context = bdrv_get_aio_context(bs);
657
94d6a7a7 658 ret = nfs_client_open(client, options,
6542aa9c 659 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
38f8d5e0 660 errp, bs->open_flags);
6542aa9c 661 if (ret < 0) {
94d6a7a7 662 return ret;
6542aa9c 663 }
37d1e4d9 664 qemu_mutex_init(&client->mutex);
6542aa9c 665 bs->total_sectors = ret;
810f4f86 666 ret = 0;
810f4f86 667 return ret;
6542aa9c
PL
668}
669
fd752801
HR
670static QemuOptsList nfs_create_opts = {
671 .name = "nfs-create-opts",
672 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
673 .desc = {
674 {
675 .name = BLOCK_OPT_SIZE,
676 .type = QEMU_OPT_SIZE,
677 .help = "Virtual disk size"
678 },
679 { /* end of list */ }
680 }
681};
682
98c10b81 683static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
6542aa9c
PL
684{
685 int ret = 0;
686 int64_t total_size = 0;
5839e53b 687 NFSClient *client = g_new0(NFSClient, 1);
94d6a7a7 688 QDict *options = NULL;
6542aa9c 689
471799d1
SH
690 client->aio_context = qemu_get_aio_context();
691
6542aa9c 692 /* Read out options */
c2eb918e
HT
693 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
694 BDRV_SECTOR_SIZE);
6542aa9c 695
94d6a7a7
AA
696 options = qdict_new();
697 ret = nfs_parse_uri(url, options, errp);
698 if (ret < 0) {
699 goto out;
700 }
701
702 ret = nfs_client_open(client, options, O_CREAT, errp, 0);
6542aa9c
PL
703 if (ret < 0) {
704 goto out;
705 }
706 ret = nfs_ftruncate(client->context, client->fh, total_size);
707 nfs_client_close(client);
708out:
07555ba6 709 QDECREF(options);
6542aa9c
PL
710 g_free(client);
711 return ret;
712}
713
714static int nfs_has_zero_init(BlockDriverState *bs)
715{
716 NFSClient *client = bs->opaque;
717 return client->has_zero_init;
718}
719
37d1e4d9 720/* Called (via nfs_service) with QemuMutex held. */
d746427a
PB
721static void
722nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
723 void *private_data)
724{
725 NFSRPC *task = private_data;
726 task->ret = ret;
727 if (task->ret == 0) {
728 memcpy(task->st, data, sizeof(struct stat));
729 }
730 if (task->ret < 0) {
731 error_report("NFS Error: %s", nfs_get_error(nfs));
732 }
733 task->complete = 1;
c9d1a561 734 bdrv_wakeup(task->bs);
d746427a
PB
735}
736
6542aa9c
PL
737static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
738{
739 NFSClient *client = bs->opaque;
740 NFSRPC task = {0};
741 struct stat st;
742
18a8056e
PL
743 if (bdrv_is_read_only(bs) &&
744 !(bs->open_flags & BDRV_O_NOCACHE)) {
745 return client->st_blocks * 512;
746 }
747
d746427a 748 task.bs = bs;
6542aa9c 749 task.st = &st;
d746427a 750 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
6542aa9c
PL
751 &task) != 0) {
752 return -ENOMEM;
753 }
754
aa92d6c4 755 nfs_set_events(client);
d746427a 756 BDRV_POLL_WHILE(bs, !task.complete);
6542aa9c 757
055c6f91 758 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
6542aa9c
PL
759}
760
761static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
762{
763 NFSClient *client = bs->opaque;
764 return nfs_ftruncate(client->context, client->fh, offset);
765}
766
18a8056e
PL
767/* Note that this will not re-establish a connection with the NFS server
768 * - it is effectively a NOP. */
769static int nfs_reopen_prepare(BDRVReopenState *state,
770 BlockReopenQueue *queue, Error **errp)
771{
772 NFSClient *client = state->bs->opaque;
773 struct stat st;
774 int ret = 0;
775
776 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
777 error_setg(errp, "Cannot open a read-only mount as read-write");
778 return -EACCES;
779 }
780
38f8d5e0 781 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
d99b26c4
PL
782 error_setg(errp, "Cannot disable cache if libnfs readahead or"
783 " pagecache is enabled");
38f8d5e0
PL
784 return -EINVAL;
785 }
786
18a8056e
PL
787 /* Update cache for read-only reopens */
788 if (!(state->flags & BDRV_O_RDWR)) {
789 ret = nfs_fstat(client->context, client->fh, &st);
790 if (ret < 0) {
791 error_setg(errp, "Failed to fstat file: %s",
792 nfs_get_error(client->context));
793 return ret;
794 }
795 client->st_blocks = st.st_blocks;
796 }
797
798 return 0;
799}
800
94d6a7a7
AA
801static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)
802{
803 NFSClient *client = bs->opaque;
804 QDict *opts = qdict_new();
805 QObject *server_qdict;
806 Visitor *ov;
807
e59084b5 808 qdict_put_str(opts, "driver", "nfs");
94d6a7a7
AA
809
810 if (client->uid && !client->gid) {
811 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
812 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
813 client->uid);
814 } else if (!client->uid && client->gid) {
815 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
816 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
817 client->gid);
818 } else if (client->uid && client->gid) {
819 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
820 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
821 client->server->host, client->path, client->uid, client->gid);
822 } else {
823 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
824 "nfs://%s%s", client->server->host, client->path);
825 }
826
827 ov = qobject_output_visitor_new(&server_qdict);
828 visit_type_NFSServer(ov, NULL, &client->server, &error_abort);
829 visit_complete(ov, &server_qdict);
94d6a7a7 830 qdict_put_obj(opts, "server", server_qdict);
e59084b5 831 qdict_put_str(opts, "path", client->path);
94d6a7a7
AA
832
833 if (client->uid) {
e59084b5 834 qdict_put_int(opts, "user", client->uid);
94d6a7a7
AA
835 }
836 if (client->gid) {
e59084b5 837 qdict_put_int(opts, "group", client->gid);
94d6a7a7
AA
838 }
839 if (client->tcp_syncnt) {
e59084b5 840 qdict_put_int(opts, "tcp-syn-cnt", client->tcp_syncnt);
94d6a7a7
AA
841 }
842 if (client->readahead) {
e59084b5 843 qdict_put_int(opts, "readahead-size", client->readahead);
94d6a7a7
AA
844 }
845 if (client->pagecache) {
e59084b5 846 qdict_put_int(opts, "page-cache-size", client->pagecache);
94d6a7a7
AA
847 }
848 if (client->debug) {
e59084b5 849 qdict_put_int(opts, "debug", client->debug);
94d6a7a7
AA
850 }
851
852 visit_free(ov);
853 qdict_flatten(opts);
854 bs->full_open_options = opts;
855}
856
d99b26c4
PL
857#ifdef LIBNFS_FEATURE_PAGECACHE
858static void nfs_invalidate_cache(BlockDriverState *bs,
859 Error **errp)
860{
861 NFSClient *client = bs->opaque;
862 nfs_pagecache_invalidate(client->context, client->fh);
863}
864#endif
865
6542aa9c 866static BlockDriver bdrv_nfs = {
471799d1
SH
867 .format_name = "nfs",
868 .protocol_name = "nfs",
869
870 .instance_size = sizeof(NFSClient),
94d6a7a7 871 .bdrv_parse_filename = nfs_parse_filename,
fd752801
HR
872 .create_opts = &nfs_create_opts,
873
471799d1
SH
874 .bdrv_has_zero_init = nfs_has_zero_init,
875 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
876 .bdrv_truncate = nfs_file_truncate,
877
878 .bdrv_file_open = nfs_file_open,
879 .bdrv_close = nfs_file_close,
c282e1fd 880 .bdrv_create = nfs_file_create,
18a8056e 881 .bdrv_reopen_prepare = nfs_reopen_prepare,
471799d1 882
69785a22
PL
883 .bdrv_co_preadv = nfs_co_preadv,
884 .bdrv_co_pwritev = nfs_co_pwritev,
471799d1
SH
885 .bdrv_co_flush_to_disk = nfs_co_flush,
886
887 .bdrv_detach_aio_context = nfs_detach_aio_context,
888 .bdrv_attach_aio_context = nfs_attach_aio_context,
94d6a7a7 889 .bdrv_refresh_filename = nfs_refresh_filename,
d99b26c4
PL
890
891#ifdef LIBNFS_FEATURE_PAGECACHE
892 .bdrv_invalidate_cache = nfs_invalidate_cache,
893#endif
6542aa9c
PL
894};
895
896static void nfs_block_init(void)
897{
898 bdrv_register(&bdrv_nfs);
899}
900
901block_init(nfs_block_init);