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