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