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