blob: a564340d1584500509f6139329b8daf1b92e3a7e [file] [log] [blame]
Peter Lieven6542aa92014-02-03 10:26:13 +01001/*
2 * QEMU Block driver for native access to files on NFS shares
3 *
Peter Lieven38f8d5e2016-05-19 14:48:02 +02004 * Copyright (c) 2014-2016 Peter Lieven <pl@kamp.de>
Peter Lieven6542aa92014-02-03 10:26:13 +01005 *
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
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010026
27#include <poll.h>
28#include "qemu-common.h"
29#include "qemu/config-file.h"
30#include "qemu/error-report.h"
Stefan Hajnoczid165b8c2016-03-30 13:46:33 +010031#include "qapi/error.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010032#include "block/block_int.h"
33#include "trace.h"
34#include "qemu/iov.h"
35#include "qemu/uri.h"
Stefan Hajnoczi0d94b742016-03-30 13:46:34 +010036#include "qemu/cutils.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010037#include "sysemu/sysemu.h"
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053038#include "qapi/qmp/qdict.h"
39#include "qapi/qmp/qint.h"
40#include "qapi/qmp/qstring.h"
41#include "qapi-visit.h"
42#include "qapi/qobject-input-visitor.h"
43#include "qapi/qobject-output-visitor.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010044#include <nfsc/libnfs.h>
45
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053046
Peter Lieven29c838c2015-06-26 13:14:01 +020047#define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
Peter Lievend99b26c2016-05-19 14:48:03 +020048#define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
Peter Lieven7725b8b2015-11-09 08:09:33 +010049#define QEMU_NFS_MAX_DEBUG_LEVEL 2
Peter Lieven29c838c2015-06-26 13:14:01 +020050
Peter Lieven6542aa92014-02-03 10:26:13 +010051typedef struct NFSClient {
52 struct nfs_context *context;
53 struct nfsfh *fh;
54 int events;
55 bool has_zero_init;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020056 AioContext *aio_context;
Peter Lieven18a80562015-08-27 12:30:41 +020057 blkcnt_t st_blocks;
Peter Lieven38f8d5e2016-05-19 14:48:02 +020058 bool cache_used;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053059 NFSServer *server;
60 char *path;
61 int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
Peter Lieven6542aa92014-02-03 10:26:13 +010062} NFSClient;
63
64typedef struct NFSRPC {
Paolo Bonzinid7464272016-10-27 12:48:57 +020065 BlockDriverState *bs;
Peter Lieven6542aa92014-02-03 10:26:13 +010066 int ret;
67 int complete;
68 QEMUIOVector *iov;
69 struct stat *st;
70 Coroutine *co;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020071 NFSClient *client;
Peter Lieven6542aa92014-02-03 10:26:13 +010072} NFSRPC;
73
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053074static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
75{
76 URI *uri = NULL;
77 QueryParams *qp = NULL;
78 int ret = -EINVAL, i;
79
80 uri = uri_parse(filename);
81 if (!uri) {
82 error_setg(errp, "Invalid URI specified");
83 goto out;
84 }
85 if (strcmp(uri->scheme, "nfs") != 0) {
86 error_setg(errp, "URI scheme must be 'nfs'");
87 goto out;
88 }
89
90 if (!uri->server) {
91 error_setg(errp, "missing hostname in URI");
92 goto out;
93 }
94
95 if (!uri->path) {
96 error_setg(errp, "missing file path in URI");
97 goto out;
98 }
99
100 qp = query_params_parse(uri->query);
101 if (!qp) {
102 error_setg(errp, "could not parse query parameters");
103 goto out;
104 }
105
106 qdict_put(options, "server.host", qstring_from_str(uri->server));
107 qdict_put(options, "server.type", qstring_from_str("inet"));
108 qdict_put(options, "path", qstring_from_str(uri->path));
109
110 for (i = 0; i < qp->n; i++) {
111 if (!qp->p[i].value) {
112 error_setg(errp, "Value for NFS parameter expected: %s",
113 qp->p[i].name);
114 goto out;
115 }
116 if (parse_uint_full(qp->p[i].value, NULL, 0)) {
117 error_setg(errp, "Illegal value for NFS parameter: %s",
118 qp->p[i].name);
119 goto out;
120 }
121 if (!strcmp(qp->p[i].name, "uid")) {
122 qdict_put(options, "user",
123 qstring_from_str(qp->p[i].value));
124 } else if (!strcmp(qp->p[i].name, "gid")) {
125 qdict_put(options, "group",
126 qstring_from_str(qp->p[i].value));
127 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
128 qdict_put(options, "tcp-syn-count",
129 qstring_from_str(qp->p[i].value));
130 } else if (!strcmp(qp->p[i].name, "readahead")) {
131 qdict_put(options, "readahead-size",
132 qstring_from_str(qp->p[i].value));
133 } else if (!strcmp(qp->p[i].name, "pagecache")) {
134 qdict_put(options, "page-cache-size",
135 qstring_from_str(qp->p[i].value));
136 } else if (!strcmp(qp->p[i].name, "debug")) {
Prasanna Kumar Kalever7103d912016-11-02 22:20:37 +0530137 qdict_put(options, "debug",
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530138 qstring_from_str(qp->p[i].value));
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") ||
Prasanna Kumar Kalever7103d912016-11-02 22:20:37 +0530168 !strcmp(qe->key, "debug") ||
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530169 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
Peter Lieven6542aa92014-02-03 10:26:13 +0100190static void nfs_process_read(void *arg);
191static void nfs_process_write(void *arg);
192
193static void nfs_set_events(NFSClient *client)
194{
195 int ev = nfs_which_events(client->context);
196 if (ev != client->events) {
Fam Zhengdca21ef2015-10-23 11:08:05 +0800197 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
198 false,
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200199 (ev & POLLIN) ? nfs_process_read : NULL,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000200 (ev & POLLOUT) ? nfs_process_write : NULL,
201 NULL, client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100202
203 }
204 client->events = ev;
205}
206
207static void nfs_process_read(void *arg)
208{
209 NFSClient *client = arg;
210 nfs_service(client->context, POLLIN);
211 nfs_set_events(client);
212}
213
214static void nfs_process_write(void *arg)
215{
216 NFSClient *client = arg;
217 nfs_service(client->context, POLLOUT);
218 nfs_set_events(client);
219}
220
Paolo Bonzinid7464272016-10-27 12:48:57 +0200221static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
Peter Lieven6542aa92014-02-03 10:26:13 +0100222{
223 *task = (NFSRPC) {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200224 .co = qemu_coroutine_self(),
Paolo Bonzinid7464272016-10-27 12:48:57 +0200225 .bs = bs,
226 .client = bs->opaque,
Peter Lieven6542aa92014-02-03 10:26:13 +0100227 };
228}
229
230static void nfs_co_generic_bh_cb(void *opaque)
231{
232 NFSRPC *task = opaque;
Peter Lievena2c0fe22014-06-10 09:42:47 +0200233 task->complete = 1;
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200234 qemu_coroutine_enter(task->co);
Peter Lieven6542aa92014-02-03 10:26:13 +0100235}
236
237static void
238nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
239 void *private_data)
240{
241 NFSRPC *task = private_data;
Peter Lieven6542aa92014-02-03 10:26:13 +0100242 task->ret = ret;
Paolo Bonzinid7464272016-10-27 12:48:57 +0200243 assert(!task->st);
Peter Lieven6542aa92014-02-03 10:26:13 +0100244 if (task->ret > 0 && task->iov) {
245 if (task->ret <= task->iov->size) {
246 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
247 } else {
248 task->ret = -EIO;
249 }
250 }
Peter Lieven20fccb12014-03-17 09:37:21 +0100251 if (task->ret < 0) {
252 error_report("NFS Error: %s", nfs_get_error(nfs));
253 }
Paolo Bonzinid7464272016-10-27 12:48:57 +0200254 aio_bh_schedule_oneshot(task->client->aio_context,
255 nfs_co_generic_bh_cb, task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100256}
257
258static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
259 int64_t sector_num, int nb_sectors,
260 QEMUIOVector *iov)
261{
262 NFSClient *client = bs->opaque;
263 NFSRPC task;
264
Paolo Bonzinid7464272016-10-27 12:48:57 +0200265 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100266 task.iov = iov;
267
268 if (nfs_pread_async(client->context, client->fh,
269 sector_num * BDRV_SECTOR_SIZE,
270 nb_sectors * BDRV_SECTOR_SIZE,
271 nfs_co_generic_cb, &task) != 0) {
272 return -ENOMEM;
273 }
274
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200275 nfs_set_events(client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100276 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100277 qemu_coroutine_yield();
278 }
279
280 if (task.ret < 0) {
281 return task.ret;
282 }
283
284 /* zero pad short reads */
285 if (task.ret < iov->size) {
286 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
287 }
288
289 return 0;
290}
291
292static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
293 int64_t sector_num, int nb_sectors,
294 QEMUIOVector *iov)
295{
296 NFSClient *client = bs->opaque;
297 NFSRPC task;
298 char *buf = NULL;
299
Paolo Bonzinid7464272016-10-27 12:48:57 +0200300 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100301
Kevin Wolf2347dd72014-05-20 13:31:20 +0200302 buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
303 if (nb_sectors && buf == NULL) {
304 return -ENOMEM;
305 }
306
Peter Lieven6542aa92014-02-03 10:26:13 +0100307 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
308
309 if (nfs_pwrite_async(client->context, client->fh,
310 sector_num * BDRV_SECTOR_SIZE,
311 nb_sectors * BDRV_SECTOR_SIZE,
312 buf, nfs_co_generic_cb, &task) != 0) {
313 g_free(buf);
314 return -ENOMEM;
315 }
316
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200317 nfs_set_events(client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100318 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100319 qemu_coroutine_yield();
320 }
321
322 g_free(buf);
323
324 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
325 return task.ret < 0 ? task.ret : -EIO;
326 }
327
328 return 0;
329}
330
331static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
332{
333 NFSClient *client = bs->opaque;
334 NFSRPC task;
335
Paolo Bonzinid7464272016-10-27 12:48:57 +0200336 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100337
338 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
339 &task) != 0) {
340 return -ENOMEM;
341 }
342
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200343 nfs_set_events(client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100344 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100345 qemu_coroutine_yield();
346 }
347
348 return task.ret;
349}
350
Peter Lieven6542aa92014-02-03 10:26:13 +0100351static QemuOptsList runtime_opts = {
352 .name = "nfs",
353 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
354 .desc = {
355 {
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530356 .name = "path",
Peter Lieven6542aa92014-02-03 10:26:13 +0100357 .type = QEMU_OPT_STRING,
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530358 .help = "Path of the image on the host",
359 },
360 {
361 .name = "uid",
362 .type = QEMU_OPT_NUMBER,
363 .help = "UID value to use when talking to the server",
364 },
365 {
366 .name = "gid",
367 .type = QEMU_OPT_NUMBER,
368 .help = "GID value to use when talking to the server",
369 },
370 {
371 .name = "tcp-syncnt",
372 .type = QEMU_OPT_NUMBER,
373 .help = "Number of SYNs to send during the session establish",
374 },
375 {
376 .name = "readahead",
377 .type = QEMU_OPT_NUMBER,
378 .help = "Set the readahead size in bytes",
379 },
380 {
381 .name = "pagecache",
382 .type = QEMU_OPT_NUMBER,
383 .help = "Set the pagecache size in bytes",
384 },
385 {
386 .name = "debug",
387 .type = QEMU_OPT_NUMBER,
388 .help = "Set the NFS debug level (max 2)",
Peter Lieven6542aa92014-02-03 10:26:13 +0100389 },
390 { /* end of list */ }
391 },
392};
393
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200394static void nfs_detach_aio_context(BlockDriverState *bs)
395{
396 NFSClient *client = bs->opaque;
397
Fam Zhengdca21ef2015-10-23 11:08:05 +0800398 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000399 false, NULL, NULL, NULL, NULL);
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200400 client->events = 0;
401}
402
403static void nfs_attach_aio_context(BlockDriverState *bs,
404 AioContext *new_context)
405{
406 NFSClient *client = bs->opaque;
407
408 client->aio_context = new_context;
409 nfs_set_events(client);
410}
411
Peter Lieven6542aa92014-02-03 10:26:13 +0100412static void nfs_client_close(NFSClient *client)
413{
414 if (client->context) {
415 if (client->fh) {
416 nfs_close(client->context, client->fh);
417 }
Fam Zhengdca21ef2015-10-23 11:08:05 +0800418 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000419 false, NULL, NULL, NULL, NULL);
Peter Lieven6542aa92014-02-03 10:26:13 +0100420 nfs_destroy_context(client->context);
421 }
422 memset(client, 0, sizeof(NFSClient));
423}
424
425static void nfs_file_close(BlockDriverState *bs)
426{
427 NFSClient *client = bs->opaque;
428 nfs_client_close(client);
429}
430
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530431static NFSServer *nfs_config(QDict *options, Error **errp)
432{
433 NFSServer *server = NULL;
434 QDict *addr = NULL;
435 QObject *crumpled_addr = NULL;
436 Visitor *iv = NULL;
437 Error *local_error = NULL;
438
439 qdict_extract_subqdict(options, &addr, "server.");
440 if (!qdict_size(addr)) {
441 error_setg(errp, "NFS server address missing");
442 goto out;
443 }
444
445 crumpled_addr = qdict_crumple(addr, errp);
446 if (!crumpled_addr) {
447 goto out;
448 }
449
450 iv = qobject_input_visitor_new(crumpled_addr, true);
451 visit_type_NFSServer(iv, NULL, &server, &local_error);
452 if (local_error) {
453 error_propagate(errp, local_error);
454 goto out;
455 }
456
457out:
458 QDECREF(addr);
459 qobject_decref(crumpled_addr);
460 visit_free(iv);
461 return server;
462}
463
464
465static int64_t nfs_client_open(NFSClient *client, QDict *options,
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200466 int flags, Error **errp, int open_flags)
Peter Lieven6542aa92014-02-03 10:26:13 +0100467{
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530468 int ret = -EINVAL;
469 QemuOpts *opts = NULL;
470 Error *local_err = NULL;
Peter Lieven6542aa92014-02-03 10:26:13 +0100471 struct stat st;
Peter Lieven6542aa92014-02-03 10:26:13 +0100472 char *file = NULL, *strp = NULL;
473
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530474 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
475 qemu_opts_absorb_qdict(opts, options, &local_err);
476 if (local_err) {
477 error_propagate(errp, local_err);
478 ret = -EINVAL;
Peter Lieven6542aa92014-02-03 10:26:13 +0100479 goto fail;
480 }
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530481
482 client->path = g_strdup(qemu_opt_get(opts, "path"));
483 if (!client->path) {
484 ret = -EINVAL;
485 error_setg(errp, "No path was specified");
Max Reitz5f4d5e12014-05-05 20:27:49 +0200486 goto fail;
487 }
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530488
489 strp = strrchr(client->path, '/');
Peter Lieven6542aa92014-02-03 10:26:13 +0100490 if (strp == NULL) {
491 error_setg(errp, "Invalid URL specified");
492 goto fail;
493 }
494 file = g_strdup(strp);
495 *strp = 0;
496
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530497 /* Pop the config into our state object, Exit if invalid */
498 client->server = nfs_config(options, errp);
499 if (!client->server) {
500 ret = -EINVAL;
501 goto fail;
502 }
503
Peter Lieven6542aa92014-02-03 10:26:13 +0100504 client->context = nfs_init_context();
505 if (client->context == NULL) {
506 error_setg(errp, "Failed to init NFS context");
507 goto fail;
508 }
509
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530510 if (qemu_opt_get(opts, "uid")) {
511 client->uid = qemu_opt_get_number(opts, "uid", 0);
512 nfs_set_uid(client->context, client->uid);
Peter Lieven6542aa92014-02-03 10:26:13 +0100513 }
514
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530515 if (qemu_opt_get(opts, "gid")) {
516 client->gid = qemu_opt_get_number(opts, "gid", 0);
517 nfs_set_gid(client->context, client->gid);
518 }
519
520 if (qemu_opt_get(opts, "tcp-syncnt")) {
521 client->tcp_syncnt = qemu_opt_get_number(opts, "tcp-syncnt", 0);
522 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
523 }
524
525#ifdef LIBNFS_FEATURE_READAHEAD
526 if (qemu_opt_get(opts, "readahead")) {
527 if (open_flags & BDRV_O_NOCACHE) {
528 error_setg(errp, "Cannot enable NFS readahead "
529 "if cache.direct = on");
530 goto fail;
531 }
532 client->readahead = qemu_opt_get_number(opts, "readahead", 0);
533 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
534 error_report("NFS Warning: Truncating NFS readahead "
535 "size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
536 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
537 }
538 nfs_set_readahead(client->context, client->readahead);
539#ifdef LIBNFS_FEATURE_PAGECACHE
540 nfs_set_pagecache_ttl(client->context, 0);
541#endif
542 client->cache_used = true;
543 }
544#endif
545
546#ifdef LIBNFS_FEATURE_PAGECACHE
547 if (qemu_opt_get(opts, "pagecache")) {
548 if (open_flags & BDRV_O_NOCACHE) {
549 error_setg(errp, "Cannot enable NFS pagecache "
550 "if cache.direct = on");
551 goto fail;
552 }
553 client->pagecache = qemu_opt_get_number(opts, "pagecache", 0);
554 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
555 error_report("NFS Warning: Truncating NFS pagecache "
556 "size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
557 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
558 }
559 nfs_set_pagecache(client->context, client->pagecache);
560 nfs_set_pagecache_ttl(client->context, 0);
561 client->cache_used = true;
562 }
563#endif
564
565#ifdef LIBNFS_FEATURE_DEBUG
566 if (qemu_opt_get(opts, "debug")) {
567 client->debug = qemu_opt_get_number(opts, "debug", 0);
568 /* limit the maximum debug level to avoid potential flooding
569 * of our log files. */
570 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
571 error_report("NFS Warning: Limiting NFS debug level "
572 "to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
573 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
574 }
575 nfs_set_debug(client->context, client->debug);
576 }
577#endif
578
579 ret = nfs_mount(client->context, client->server->host, client->path);
Peter Lieven6542aa92014-02-03 10:26:13 +0100580 if (ret < 0) {
581 error_setg(errp, "Failed to mount nfs share: %s",
582 nfs_get_error(client->context));
583 goto fail;
584 }
585
586 if (flags & O_CREAT) {
587 ret = nfs_creat(client->context, file, 0600, &client->fh);
588 if (ret < 0) {
589 error_setg(errp, "Failed to create file: %s",
590 nfs_get_error(client->context));
591 goto fail;
592 }
593 } else {
594 ret = nfs_open(client->context, file, flags, &client->fh);
595 if (ret < 0) {
596 error_setg(errp, "Failed to open file : %s",
597 nfs_get_error(client->context));
598 goto fail;
599 }
600 }
601
602 ret = nfs_fstat(client->context, client->fh, &st);
603 if (ret < 0) {
604 error_setg(errp, "Failed to fstat file: %s",
605 nfs_get_error(client->context));
606 goto fail;
607 }
608
609 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
Peter Lieven18a80562015-08-27 12:30:41 +0200610 client->st_blocks = st.st_blocks;
Peter Lieven6542aa92014-02-03 10:26:13 +0100611 client->has_zero_init = S_ISREG(st.st_mode);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530612 *strp = '/';
Peter Lieven6542aa92014-02-03 10:26:13 +0100613 goto out;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530614
Peter Lieven6542aa92014-02-03 10:26:13 +0100615fail:
616 nfs_client_close(client);
617out:
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530618 qemu_opts_del(opts);
Peter Lieven6542aa92014-02-03 10:26:13 +0100619 g_free(file);
620 return ret;
621}
622
623static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
624 Error **errp) {
625 NFSClient *client = bs->opaque;
626 int64_t ret;
Peter Lieven6542aa92014-02-03 10:26:13 +0100627
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200628 client->aio_context = bdrv_get_aio_context(bs);
629
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530630 ret = nfs_client_open(client, options,
Peter Lieven6542aa92014-02-03 10:26:13 +0100631 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200632 errp, bs->open_flags);
Peter Lieven6542aa92014-02-03 10:26:13 +0100633 if (ret < 0) {
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530634 return ret;
Peter Lieven6542aa92014-02-03 10:26:13 +0100635 }
636 bs->total_sectors = ret;
Fam Zheng810f4f82014-08-28 13:56:10 +0800637 ret = 0;
Fam Zheng810f4f82014-08-28 13:56:10 +0800638 return ret;
Peter Lieven6542aa92014-02-03 10:26:13 +0100639}
640
Max Reitzfd752802014-12-02 18:32:44 +0100641static QemuOptsList nfs_create_opts = {
642 .name = "nfs-create-opts",
643 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
644 .desc = {
645 {
646 .name = BLOCK_OPT_SIZE,
647 .type = QEMU_OPT_SIZE,
648 .help = "Virtual disk size"
649 },
650 { /* end of list */ }
651 }
652};
653
Chunyan Liu98c10b82014-06-05 17:20:56 +0800654static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
Peter Lieven6542aa92014-02-03 10:26:13 +0100655{
656 int ret = 0;
657 int64_t total_size = 0;
Markus Armbruster5839e532014-08-19 10:31:08 +0200658 NFSClient *client = g_new0(NFSClient, 1);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530659 QDict *options = NULL;
Peter Lieven6542aa92014-02-03 10:26:13 +0100660
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200661 client->aio_context = qemu_get_aio_context();
662
Peter Lieven6542aa92014-02-03 10:26:13 +0100663 /* Read out options */
Hu Taoc2eb9182014-09-10 17:05:45 +0800664 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
665 BDRV_SECTOR_SIZE);
Peter Lieven6542aa92014-02-03 10:26:13 +0100666
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530667 options = qdict_new();
668 ret = nfs_parse_uri(url, options, errp);
669 if (ret < 0) {
670 goto out;
671 }
672
673 ret = nfs_client_open(client, options, O_CREAT, errp, 0);
Peter Lieven6542aa92014-02-03 10:26:13 +0100674 if (ret < 0) {
675 goto out;
676 }
677 ret = nfs_ftruncate(client->context, client->fh, total_size);
678 nfs_client_close(client);
679out:
Kevin Wolf07555ba2016-11-07 10:27:51 +0100680 QDECREF(options);
Peter Lieven6542aa92014-02-03 10:26:13 +0100681 g_free(client);
682 return ret;
683}
684
685static int nfs_has_zero_init(BlockDriverState *bs)
686{
687 NFSClient *client = bs->opaque;
688 return client->has_zero_init;
689}
690
Paolo Bonzinid7464272016-10-27 12:48:57 +0200691static void
692nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
693 void *private_data)
694{
695 NFSRPC *task = private_data;
696 task->ret = ret;
697 if (task->ret == 0) {
698 memcpy(task->st, data, sizeof(struct stat));
699 }
700 if (task->ret < 0) {
701 error_report("NFS Error: %s", nfs_get_error(nfs));
702 }
703 task->complete = 1;
Paolo Bonzinic9d1a562016-10-27 12:49:05 +0200704 bdrv_wakeup(task->bs);
Paolo Bonzinid7464272016-10-27 12:48:57 +0200705}
706
Peter Lieven6542aa92014-02-03 10:26:13 +0100707static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
708{
709 NFSClient *client = bs->opaque;
710 NFSRPC task = {0};
711 struct stat st;
712
Peter Lieven18a80562015-08-27 12:30:41 +0200713 if (bdrv_is_read_only(bs) &&
714 !(bs->open_flags & BDRV_O_NOCACHE)) {
715 return client->st_blocks * 512;
716 }
717
Paolo Bonzinid7464272016-10-27 12:48:57 +0200718 task.bs = bs;
Peter Lieven6542aa92014-02-03 10:26:13 +0100719 task.st = &st;
Paolo Bonzinid7464272016-10-27 12:48:57 +0200720 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
Peter Lieven6542aa92014-02-03 10:26:13 +0100721 &task) != 0) {
722 return -ENOMEM;
723 }
724
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200725 nfs_set_events(client);
Paolo Bonzinid7464272016-10-27 12:48:57 +0200726 BDRV_POLL_WHILE(bs, !task.complete);
Peter Lieven6542aa92014-02-03 10:26:13 +0100727
Peter Lieven055c6f92015-08-20 12:46:47 +0200728 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
Peter Lieven6542aa92014-02-03 10:26:13 +0100729}
730
731static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
732{
733 NFSClient *client = bs->opaque;
734 return nfs_ftruncate(client->context, client->fh, offset);
735}
736
Peter Lieven18a80562015-08-27 12:30:41 +0200737/* Note that this will not re-establish a connection with the NFS server
738 * - it is effectively a NOP. */
739static int nfs_reopen_prepare(BDRVReopenState *state,
740 BlockReopenQueue *queue, Error **errp)
741{
742 NFSClient *client = state->bs->opaque;
743 struct stat st;
744 int ret = 0;
745
746 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
747 error_setg(errp, "Cannot open a read-only mount as read-write");
748 return -EACCES;
749 }
750
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200751 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
Peter Lievend99b26c2016-05-19 14:48:03 +0200752 error_setg(errp, "Cannot disable cache if libnfs readahead or"
753 " pagecache is enabled");
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200754 return -EINVAL;
755 }
756
Peter Lieven18a80562015-08-27 12:30:41 +0200757 /* Update cache for read-only reopens */
758 if (!(state->flags & BDRV_O_RDWR)) {
759 ret = nfs_fstat(client->context, client->fh, &st);
760 if (ret < 0) {
761 error_setg(errp, "Failed to fstat file: %s",
762 nfs_get_error(client->context));
763 return ret;
764 }
765 client->st_blocks = st.st_blocks;
766 }
767
768 return 0;
769}
770
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530771static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)
772{
773 NFSClient *client = bs->opaque;
774 QDict *opts = qdict_new();
775 QObject *server_qdict;
776 Visitor *ov;
777
778 qdict_put(opts, "driver", qstring_from_str("nfs"));
779
780 if (client->uid && !client->gid) {
781 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
782 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
783 client->uid);
784 } else if (!client->uid && client->gid) {
785 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
786 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
787 client->gid);
788 } else if (client->uid && client->gid) {
789 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
790 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
791 client->server->host, client->path, client->uid, client->gid);
792 } else {
793 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
794 "nfs://%s%s", client->server->host, client->path);
795 }
796
797 ov = qobject_output_visitor_new(&server_qdict);
798 visit_type_NFSServer(ov, NULL, &client->server, &error_abort);
799 visit_complete(ov, &server_qdict);
800 assert(qobject_type(server_qdict) == QTYPE_QDICT);
801
802 qdict_put_obj(opts, "server", server_qdict);
803 qdict_put(opts, "path", qstring_from_str(client->path));
804
805 if (client->uid) {
806 qdict_put(opts, "uid", qint_from_int(client->uid));
807 }
808 if (client->gid) {
809 qdict_put(opts, "gid", qint_from_int(client->gid));
810 }
811 if (client->tcp_syncnt) {
812 qdict_put(opts, "tcp-syncnt",
813 qint_from_int(client->tcp_syncnt));
814 }
815 if (client->readahead) {
816 qdict_put(opts, "readahead",
817 qint_from_int(client->readahead));
818 }
819 if (client->pagecache) {
820 qdict_put(opts, "pagecache",
821 qint_from_int(client->pagecache));
822 }
823 if (client->debug) {
824 qdict_put(opts, "debug", qint_from_int(client->debug));
825 }
826
827 visit_free(ov);
828 qdict_flatten(opts);
829 bs->full_open_options = opts;
830}
831
Peter Lievend99b26c2016-05-19 14:48:03 +0200832#ifdef LIBNFS_FEATURE_PAGECACHE
833static void nfs_invalidate_cache(BlockDriverState *bs,
834 Error **errp)
835{
836 NFSClient *client = bs->opaque;
837 nfs_pagecache_invalidate(client->context, client->fh);
838}
839#endif
840
Peter Lieven6542aa92014-02-03 10:26:13 +0100841static BlockDriver bdrv_nfs = {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200842 .format_name = "nfs",
843 .protocol_name = "nfs",
Peter Lieven6542aa92014-02-03 10:26:13 +0100844
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200845 .instance_size = sizeof(NFSClient),
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530846 .bdrv_parse_filename = nfs_parse_filename,
Max Reitzfd752802014-12-02 18:32:44 +0100847 .create_opts = &nfs_create_opts,
848
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200849 .bdrv_has_zero_init = nfs_has_zero_init,
850 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
851 .bdrv_truncate = nfs_file_truncate,
Peter Lieven6542aa92014-02-03 10:26:13 +0100852
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200853 .bdrv_file_open = nfs_file_open,
854 .bdrv_close = nfs_file_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800855 .bdrv_create = nfs_file_create,
Peter Lieven18a80562015-08-27 12:30:41 +0200856 .bdrv_reopen_prepare = nfs_reopen_prepare,
Peter Lieven6542aa92014-02-03 10:26:13 +0100857
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200858 .bdrv_co_readv = nfs_co_readv,
859 .bdrv_co_writev = nfs_co_writev,
860 .bdrv_co_flush_to_disk = nfs_co_flush,
861
862 .bdrv_detach_aio_context = nfs_detach_aio_context,
863 .bdrv_attach_aio_context = nfs_attach_aio_context,
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530864 .bdrv_refresh_filename = nfs_refresh_filename,
Peter Lievend99b26c2016-05-19 14:48:03 +0200865
866#ifdef LIBNFS_FEATURE_PAGECACHE
867 .bdrv_invalidate_cache = nfs_invalidate_cache,
868#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100869};
870
871static void nfs_block_init(void)
872{
873 bdrv_register(&bdrv_nfs);
874}
875
876block_init(nfs_block_init);