]> git.proxmox.com Git - mirror_qemu.git/blame - block/nfs.c
include/block: Untangle inclusion loops
[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++) {
8d20abe8 117 unsigned long long 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 }
8d20abe8 123 if (parse_uint_full(qp->p[i].value, &val, 0)) {
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
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 200 (ev & POLLOUT) ? nfs_process_write : NULL,
826cc324 201 NULL, 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
ee15ee36 227static void coroutine_fn 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
f7ef38dd
VSO
266static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, int64_t offset,
267 int64_t bytes, QEMUIOVector *iov,
268 BdrvRequestFlags 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
e75abeda
VSO
300static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, int64_t offset,
301 int64_t bytes, QEMUIOVector *iov,
302 BdrvRequestFlags 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),
826cc324 376 false, NULL, 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),
826cc324 394 false, NULL, NULL, NULL, NULL, NULL);
601dc655 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;
ebdebe47
BM
422#ifdef _WIN32
423 struct __stat64 st;
424#else
6542aa9c 425 struct stat st;
ebdebe47 426#endif
6542aa9c
PL
427 char *file = NULL, *strp = NULL;
428
113fe792 429 qemu_mutex_init(&client->mutex);
94d6a7a7 430
c22a0345 431 client->path = g_strdup(opts->path);
94d6a7a7
AA
432
433 strp = strrchr(client->path, '/');
6542aa9c
PL
434 if (strp == NULL) {
435 error_setg(errp, "Invalid URL specified");
436 goto fail;
437 }
438 file = g_strdup(strp);
439 *strp = 0;
440
c22a0345
KW
441 /* Steal the NFSServer object from opts; set the original pointer to NULL
442 * to avoid use after free and double free. */
443 client->server = opts->server;
444 opts->server = NULL;
94d6a7a7 445
6542aa9c
PL
446 client->context = nfs_init_context();
447 if (client->context == NULL) {
448 error_setg(errp, "Failed to init NFS context");
449 goto fail;
450 }
451
c22a0345
KW
452 if (opts->has_user) {
453 client->uid = opts->user;
94d6a7a7
AA
454 nfs_set_uid(client->context, client->uid);
455 }
456
c22a0345
KW
457 if (opts->has_group) {
458 client->gid = opts->group;
94d6a7a7
AA
459 nfs_set_gid(client->context, client->gid);
460 }
461
c22a0345
KW
462 if (opts->has_tcp_syn_count) {
463 client->tcp_syncnt = opts->tcp_syn_count;
94d6a7a7
AA
464 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
465 }
466
467#ifdef LIBNFS_FEATURE_READAHEAD
c22a0345 468 if (opts->has_readahead_size) {
94d6a7a7
AA
469 if (open_flags & BDRV_O_NOCACHE) {
470 error_setg(errp, "Cannot enable NFS readahead "
471 "if cache.direct = on");
6542aa9c
PL
472 goto fail;
473 }
c22a0345 474 client->readahead = opts->readahead_size;
94d6a7a7 475 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
3dc6f869
AF
476 warn_report("Truncating NFS readahead size to %d",
477 QEMU_NFS_MAX_READAHEAD_SIZE);
94d6a7a7 478 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
7c24384b 479 }
94d6a7a7 480 nfs_set_readahead(client->context, client->readahead);
d99b26c4 481#ifdef LIBNFS_FEATURE_PAGECACHE
94d6a7a7 482 nfs_set_pagecache_ttl(client->context, 0);
d99b26c4 483#endif
94d6a7a7
AA
484 client->cache_used = true;
485 }
d99b26c4 486#endif
94d6a7a7 487
d99b26c4 488#ifdef LIBNFS_FEATURE_PAGECACHE
c22a0345 489 if (opts->has_page_cache_size) {
94d6a7a7
AA
490 if (open_flags & BDRV_O_NOCACHE) {
491 error_setg(errp, "Cannot enable NFS pagecache "
492 "if cache.direct = on");
493 goto fail;
494 }
c22a0345 495 client->pagecache = opts->page_cache_size;
94d6a7a7 496 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
3dc6f869
AF
497 warn_report("Truncating NFS pagecache size to %d pages",
498 QEMU_NFS_MAX_PAGECACHE_SIZE);
94d6a7a7
AA
499 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
500 }
501 nfs_set_pagecache(client->context, client->pagecache);
502 nfs_set_pagecache_ttl(client->context, 0);
503 client->cache_used = true;
504 }
7725b8bf 505#endif
94d6a7a7 506
7725b8bf 507#ifdef LIBNFS_FEATURE_DEBUG
c22a0345
KW
508 if (opts->has_debug) {
509 client->debug = opts->debug;
94d6a7a7
AA
510 /* limit the maximum debug level to avoid potential flooding
511 * of our log files. */
512 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
3dc6f869
AF
513 warn_report("Limiting NFS debug level to %d",
514 QEMU_NFS_MAX_DEBUG_LEVEL);
94d6a7a7 515 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
6542aa9c 516 }
94d6a7a7 517 nfs_set_debug(client->context, client->debug);
6542aa9c 518 }
94d6a7a7 519#endif
6542aa9c 520
94d6a7a7 521 ret = nfs_mount(client->context, client->server->host, client->path);
6542aa9c
PL
522 if (ret < 0) {
523 error_setg(errp, "Failed to mount nfs share: %s",
524 nfs_get_error(client->context));
525 goto fail;
526 }
527
528 if (flags & O_CREAT) {
529 ret = nfs_creat(client->context, file, 0600, &client->fh);
530 if (ret < 0) {
531 error_setg(errp, "Failed to create file: %s",
532 nfs_get_error(client->context));
533 goto fail;
534 }
535 } else {
536 ret = nfs_open(client->context, file, flags, &client->fh);
537 if (ret < 0) {
538 error_setg(errp, "Failed to open file : %s",
539 nfs_get_error(client->context));
540 goto fail;
541 }
542 }
543
544 ret = nfs_fstat(client->context, client->fh, &st);
545 if (ret < 0) {
546 error_setg(errp, "Failed to fstat file: %s",
547 nfs_get_error(client->context));
548 goto fail;
549 }
550
551 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
c63b0201 552#if !defined(_WIN32)
18a8056e 553 client->st_blocks = st.st_blocks;
c63b0201 554#endif
6542aa9c 555 client->has_zero_init = S_ISREG(st.st_mode);
94d6a7a7 556 *strp = '/';
6542aa9c 557 goto out;
94d6a7a7 558
6542aa9c
PL
559fail:
560 nfs_client_close(client);
561out:
6542aa9c
PL
562 g_free(file);
563 return ret;
564}
565
a1a42af4
KW
566static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options,
567 Error **errp)
c22a0345
KW
568{
569 BlockdevOptionsNfs *opts = NULL;
c22a0345 570 Visitor *v;
c82be42c 571 const QDictEntry *e;
c22a0345 572
af91062e
MA
573 v = qobject_input_visitor_new_flat_confused(options, errp);
574 if (!v) {
a1a42af4 575 return NULL;
c22a0345
KW
576 }
577
b11a093c 578 visit_type_BlockdevOptionsNfs(v, NULL, &opts, errp);
c22a0345 579 visit_free(v);
b11a093c 580 if (!opts) {
a1a42af4
KW
581 return NULL;
582 }
583
c82be42c
KW
584 /* Remove the processed options from the QDict (the visitor processes
585 * _all_ options in the QDict) */
586 while ((e = qdict_first(options))) {
587 qdict_del(options, e->key);
588 }
589
a1a42af4
KW
590 return opts;
591}
592
593static int64_t nfs_client_open_qdict(NFSClient *client, QDict *options,
594 int flags, int open_flags, Error **errp)
595{
596 BlockdevOptionsNfs *opts;
182454dc 597 int64_t ret;
a1a42af4
KW
598
599 opts = nfs_options_qdict_to_qapi(options, errp);
600 if (opts == NULL) {
c22a0345
KW
601 ret = -EINVAL;
602 goto fail;
603 }
604
605 ret = nfs_client_open(client, opts, flags, open_flags, errp);
606fail:
c22a0345
KW
607 qapi_free_BlockdevOptionsNfs(opts);
608 return ret;
609}
610
6542aa9c
PL
611static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
612 Error **errp) {
613 NFSClient *client = bs->opaque;
614 int64_t ret;
6542aa9c 615
471799d1
SH
616 client->aio_context = bdrv_get_aio_context(bs);
617
c22a0345
KW
618 ret = nfs_client_open_qdict(client, options,
619 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
620 bs->open_flags, errp);
6542aa9c 621 if (ret < 0) {
94d6a7a7 622 return ret;
6542aa9c 623 }
113fe792 624
6542aa9c 625 bs->total_sectors = ret;
8f23aaf5
EB
626 if (client->has_zero_init) {
627 bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
628 }
b3ac2b94 629 return 0;
6542aa9c
PL
630}
631
fd752801
HR
632static QemuOptsList nfs_create_opts = {
633 .name = "nfs-create-opts",
634 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
635 .desc = {
636 {
637 .name = BLOCK_OPT_SIZE,
638 .type = QEMU_OPT_SIZE,
639 .help = "Virtual disk size"
640 },
641 { /* end of list */ }
642 }
643};
644
a1a42af4 645static int nfs_file_co_create(BlockdevCreateOptions *options, Error **errp)
6542aa9c 646{
a1a42af4 647 BlockdevCreateOptionsNfs *opts = &options->u.nfs;
5839e53b 648 NFSClient *client = g_new0(NFSClient, 1);
a1a42af4
KW
649 int ret;
650
651 assert(options->driver == BLOCKDEV_DRIVER_NFS);
6542aa9c 652
471799d1
SH
653 client->aio_context = qemu_get_aio_context();
654
a1a42af4
KW
655 ret = nfs_client_open(client, opts->location, O_CREAT, 0, errp);
656 if (ret < 0) {
657 goto out;
658 }
659 ret = nfs_ftruncate(client->context, client->fh, opts->size);
660 nfs_client_close(client);
661
662out:
663 g_free(client);
664 return ret;
665}
666
b92902df
ML
667static int coroutine_fn nfs_file_co_create_opts(BlockDriver *drv,
668 const char *url,
669 QemuOpts *opts,
a1a42af4
KW
670 Error **errp)
671{
672 BlockdevCreateOptions *create_options;
673 BlockdevCreateOptionsNfs *nfs_opts;
674 QDict *options;
675 int ret;
676
677 create_options = g_new0(BlockdevCreateOptions, 1);
678 create_options->driver = BLOCKDEV_DRIVER_NFS;
679 nfs_opts = &create_options->u.nfs;
680
6542aa9c 681 /* Read out options */
a1a42af4
KW
682 nfs_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
683 BDRV_SECTOR_SIZE);
6542aa9c 684
94d6a7a7
AA
685 options = qdict_new();
686 ret = nfs_parse_uri(url, options, errp);
687 if (ret < 0) {
688 goto out;
689 }
690
a1a42af4
KW
691 nfs_opts->location = nfs_options_qdict_to_qapi(options, errp);
692 if (nfs_opts->location == NULL) {
693 ret = -EINVAL;
694 goto out;
695 }
696
697 ret = nfs_file_co_create(create_options, errp);
6542aa9c
PL
698 if (ret < 0) {
699 goto out;
700 }
a1a42af4
KW
701
702 ret = 0;
6542aa9c 703out:
cb3e7f08 704 qobject_unref(options);
a1a42af4 705 qapi_free_BlockdevCreateOptions(create_options);
6542aa9c
PL
706 return ret;
707}
708
709static int nfs_has_zero_init(BlockDriverState *bs)
710{
711 NFSClient *client = bs->opaque;
712 return client->has_zero_init;
713}
714
c63b0201 715#if !defined(_WIN32)
37d1e4d9 716/* Called (via nfs_service) with QemuMutex held. */
d746427a
PB
717static void
718nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
719 void *private_data)
720{
721 NFSRPC *task = private_data;
722 task->ret = ret;
723 if (task->ret == 0) {
724 memcpy(task->st, data, sizeof(struct stat));
725 }
726 if (task->ret < 0) {
727 error_report("NFS Error: %s", nfs_get_error(nfs));
728 }
e2a6ae7f
PB
729
730 /* Set task->complete before reading bs->wakeup. */
d73415a3 731 qatomic_mb_set(&task->complete, 1);
c9d1a561 732 bdrv_wakeup(task->bs);
d746427a
PB
733}
734
6542aa9c
PL
735static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
736{
737 NFSClient *client = bs->opaque;
738 NFSRPC task = {0};
739 struct stat st;
740
18a8056e
PL
741 if (bdrv_is_read_only(bs) &&
742 !(bs->open_flags & BDRV_O_NOCACHE)) {
743 return client->st_blocks * 512;
744 }
745
d746427a 746 task.bs = bs;
6542aa9c 747 task.st = &st;
d746427a 748 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
6542aa9c
PL
749 &task) != 0) {
750 return -ENOMEM;
751 }
752
aa92d6c4 753 nfs_set_events(client);
d746427a 754 BDRV_POLL_WHILE(bs, !task.complete);
6542aa9c 755
055c6f91 756 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
6542aa9c 757}
c63b0201 758#endif
6542aa9c 759
061ca8a3 760static int coroutine_fn
c80d8b06 761nfs_file_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
92b92799
KW
762 PreallocMode prealloc, BdrvRequestFlags flags,
763 Error **errp)
6542aa9c
PL
764{
765 NFSClient *client = bs->opaque;
f59adb32
HR
766 int ret;
767
8243ccb7
HR
768 if (prealloc != PREALLOC_MODE_OFF) {
769 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 770 PreallocMode_str(prealloc));
8243ccb7
HR
771 return -ENOTSUP;
772 }
773
f59adb32
HR
774 ret = nfs_ftruncate(client->context, client->fh, offset);
775 if (ret < 0) {
776 error_setg_errno(errp, -ret, "Failed to truncate file");
777 return ret;
778 }
779
780 return 0;
6542aa9c
PL
781}
782
18a8056e
PL
783/* Note that this will not re-establish a connection with the NFS server
784 * - it is effectively a NOP. */
785static int nfs_reopen_prepare(BDRVReopenState *state,
786 BlockReopenQueue *queue, Error **errp)
787{
788 NFSClient *client = state->bs->opaque;
ebdebe47
BM
789#ifdef _WIN32
790 struct __stat64 st;
791#else
18a8056e 792 struct stat st;
ebdebe47 793#endif
18a8056e
PL
794 int ret = 0;
795
796 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
797 error_setg(errp, "Cannot open a read-only mount as read-write");
798 return -EACCES;
799 }
800
38f8d5e0 801 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
d99b26c4
PL
802 error_setg(errp, "Cannot disable cache if libnfs readahead or"
803 " pagecache is enabled");
38f8d5e0
PL
804 return -EINVAL;
805 }
806
18a8056e
PL
807 /* Update cache for read-only reopens */
808 if (!(state->flags & BDRV_O_RDWR)) {
809 ret = nfs_fstat(client->context, client->fh, &st);
810 if (ret < 0) {
811 error_setg(errp, "Failed to fstat file: %s",
812 nfs_get_error(client->context));
813 return ret;
814 }
c63b0201 815#if !defined(_WIN32)
18a8056e 816 client->st_blocks = st.st_blocks;
c63b0201 817#endif
18a8056e
PL
818 }
819
820 return 0;
821}
822
998b3a1e 823static void nfs_refresh_filename(BlockDriverState *bs)
94d6a7a7
AA
824{
825 NFSClient *client = bs->opaque;
94d6a7a7
AA
826
827 if (client->uid && !client->gid) {
828 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
829 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
830 client->uid);
831 } else if (!client->uid && client->gid) {
832 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
833 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
834 client->gid);
835 } else if (client->uid && client->gid) {
836 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
837 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
838 client->server->host, client->path, client->uid, client->gid);
839 } else {
840 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
841 "nfs://%s%s", client->server->host, client->path);
842 }
94d6a7a7
AA
843}
844
0dcbc54a
HR
845static char *nfs_dirname(BlockDriverState *bs, Error **errp)
846{
847 NFSClient *client = bs->opaque;
848
849 if (client->uid || client->gid) {
850 bdrv_refresh_filename(bs);
851 error_setg(errp, "Cannot generate a base directory for NFS node '%s'",
852 bs->filename);
853 return NULL;
854 }
855
856 return g_strdup_printf("nfs://%s%s/", client->server->host, client->path);
857}
858
d99b26c4 859#ifdef LIBNFS_FEATURE_PAGECACHE
2b148f39
PB
860static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs,
861 Error **errp)
d99b26c4
PL
862{
863 NFSClient *client = bs->opaque;
864 nfs_pagecache_invalidate(client->context, client->fh);
865}
866#endif
867
2654267c
HR
868static const char *nfs_strong_runtime_opts[] = {
869 "path",
870 "user",
871 "group",
872 "server.",
873
874 NULL
875};
876
6542aa9c 877static BlockDriver bdrv_nfs = {
471799d1
SH
878 .format_name = "nfs",
879 .protocol_name = "nfs",
880
881 .instance_size = sizeof(NFSClient),
94d6a7a7 882 .bdrv_parse_filename = nfs_parse_filename,
fd752801
HR
883 .create_opts = &nfs_create_opts,
884
471799d1 885 .bdrv_has_zero_init = nfs_has_zero_init,
c63b0201
YL
886/* libnfs does not provide the allocated filesize of a file on win32. */
887#if !defined(_WIN32)
471799d1 888 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
c63b0201 889#endif
061ca8a3 890 .bdrv_co_truncate = nfs_file_co_truncate,
471799d1
SH
891
892 .bdrv_file_open = nfs_file_open,
893 .bdrv_close = nfs_file_close,
a1a42af4 894 .bdrv_co_create = nfs_file_co_create,
efc75e2a 895 .bdrv_co_create_opts = nfs_file_co_create_opts,
18a8056e 896 .bdrv_reopen_prepare = nfs_reopen_prepare,
471799d1 897
69785a22
PL
898 .bdrv_co_preadv = nfs_co_preadv,
899 .bdrv_co_pwritev = nfs_co_pwritev,
471799d1
SH
900 .bdrv_co_flush_to_disk = nfs_co_flush,
901
902 .bdrv_detach_aio_context = nfs_detach_aio_context,
903 .bdrv_attach_aio_context = nfs_attach_aio_context,
94d6a7a7 904 .bdrv_refresh_filename = nfs_refresh_filename,
0dcbc54a 905 .bdrv_dirname = nfs_dirname,
d99b26c4 906
2654267c
HR
907 .strong_runtime_opts = nfs_strong_runtime_opts,
908
d99b26c4 909#ifdef LIBNFS_FEATURE_PAGECACHE
2b148f39 910 .bdrv_co_invalidate_cache = nfs_co_invalidate_cache,
d99b26c4 911#endif
6542aa9c
PL
912};
913
914static void nfs_block_init(void)
915{
916 bdrv_register(&bdrv_nfs);
917}
918
919block_init(nfs_block_init);