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