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