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