]>
Commit | Line | Data |
---|---|---|
6542aa9c PL |
1 | /* |
2 | * QEMU Block driver for native access to files on NFS shares | |
3 | * | |
38f8d5e0 | 4 | * Copyright (c) 2014-2016 Peter Lieven <[email protected]> |
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> | |
28 | #include "qemu-common.h" | |
29 | #include "qemu/config-file.h" | |
30 | #include "qemu/error-report.h" | |
d165b8cb | 31 | #include "qapi/error.h" |
6542aa9c PL |
32 | #include "block/block_int.h" |
33 | #include "trace.h" | |
34 | #include "qemu/iov.h" | |
35 | #include "qemu/uri.h" | |
0d94b746 | 36 | #include "qemu/cutils.h" |
6542aa9c | 37 | #include "sysemu/sysemu.h" |
94d6a7a7 AA |
38 | #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" | |
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 |
51 | typedef struct NFSClient { |
52 | struct nfs_context *context; | |
53 | struct nfsfh *fh; | |
54 | int events; | |
55 | bool has_zero_init; | |
471799d1 | 56 | AioContext *aio_context; |
18a8056e | 57 | blkcnt_t st_blocks; |
38f8d5e0 | 58 | bool cache_used; |
94d6a7a7 AA |
59 | NFSServer *server; |
60 | char *path; | |
61 | int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug; | |
6542aa9c PL |
62 | } NFSClient; |
63 | ||
64 | typedef struct NFSRPC { | |
d746427a | 65 | BlockDriverState *bs; |
6542aa9c PL |
66 | int ret; |
67 | int complete; | |
68 | QEMUIOVector *iov; | |
69 | struct stat *st; | |
70 | Coroutine *co; | |
471799d1 | 71 | NFSClient *client; |
6542aa9c PL |
72 | } NFSRPC; |
73 | ||
94d6a7a7 AA |
74 | static 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")) { | |
137 | qdict_put(options, "debug-level", | |
138 | 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; | |
146 | out: | |
147 | if (qp) { | |
148 | query_params_free(qp); | |
149 | } | |
150 | if (uri) { | |
151 | uri_free(uri); | |
152 | } | |
153 | return ret; | |
154 | } | |
155 | ||
156 | static 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") || | |
168 | !strcmp(qe->key, "debug-level") || | |
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 | ||
180 | static 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 |
190 | static void nfs_process_read(void *arg); |
191 | static void nfs_process_write(void *arg); | |
192 | ||
193 | static 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, |
dca21ef2 | 200 | (ev & POLLOUT) ? nfs_process_write : NULL, client); |
6542aa9c PL |
201 | |
202 | } | |
203 | client->events = ev; | |
204 | } | |
205 | ||
206 | static void nfs_process_read(void *arg) | |
207 | { | |
208 | NFSClient *client = arg; | |
209 | nfs_service(client->context, POLLIN); | |
210 | nfs_set_events(client); | |
211 | } | |
212 | ||
213 | static void nfs_process_write(void *arg) | |
214 | { | |
215 | NFSClient *client = arg; | |
216 | nfs_service(client->context, POLLOUT); | |
217 | nfs_set_events(client); | |
218 | } | |
219 | ||
d746427a | 220 | static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task) |
6542aa9c PL |
221 | { |
222 | *task = (NFSRPC) { | |
471799d1 | 223 | .co = qemu_coroutine_self(), |
d746427a PB |
224 | .bs = bs, |
225 | .client = bs->opaque, | |
6542aa9c PL |
226 | }; |
227 | } | |
228 | ||
229 | static void nfs_co_generic_bh_cb(void *opaque) | |
230 | { | |
231 | NFSRPC *task = opaque; | |
a2c0fe2f | 232 | task->complete = 1; |
0b8b8753 | 233 | qemu_coroutine_enter(task->co); |
6542aa9c PL |
234 | } |
235 | ||
236 | static void | |
237 | nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data, | |
238 | void *private_data) | |
239 | { | |
240 | NFSRPC *task = private_data; | |
6542aa9c | 241 | task->ret = ret; |
d746427a | 242 | assert(!task->st); |
6542aa9c PL |
243 | if (task->ret > 0 && task->iov) { |
244 | if (task->ret <= task->iov->size) { | |
245 | qemu_iovec_from_buf(task->iov, 0, data, task->ret); | |
246 | } else { | |
247 | task->ret = -EIO; | |
248 | } | |
249 | } | |
20fccb18 PL |
250 | if (task->ret < 0) { |
251 | error_report("NFS Error: %s", nfs_get_error(nfs)); | |
252 | } | |
d746427a PB |
253 | aio_bh_schedule_oneshot(task->client->aio_context, |
254 | nfs_co_generic_bh_cb, task); | |
6542aa9c PL |
255 | } |
256 | ||
257 | static int coroutine_fn nfs_co_readv(BlockDriverState *bs, | |
258 | int64_t sector_num, int nb_sectors, | |
259 | QEMUIOVector *iov) | |
260 | { | |
261 | NFSClient *client = bs->opaque; | |
262 | NFSRPC task; | |
263 | ||
d746427a | 264 | nfs_co_init_task(bs, &task); |
6542aa9c PL |
265 | task.iov = iov; |
266 | ||
267 | if (nfs_pread_async(client->context, client->fh, | |
268 | sector_num * BDRV_SECTOR_SIZE, | |
269 | nb_sectors * BDRV_SECTOR_SIZE, | |
270 | nfs_co_generic_cb, &task) != 0) { | |
271 | return -ENOMEM; | |
272 | } | |
273 | ||
aa92d6c4 | 274 | nfs_set_events(client); |
6542aa9c | 275 | while (!task.complete) { |
6542aa9c PL |
276 | qemu_coroutine_yield(); |
277 | } | |
278 | ||
279 | if (task.ret < 0) { | |
280 | return task.ret; | |
281 | } | |
282 | ||
283 | /* zero pad short reads */ | |
284 | if (task.ret < iov->size) { | |
285 | qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret); | |
286 | } | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | static int coroutine_fn nfs_co_writev(BlockDriverState *bs, | |
292 | int64_t sector_num, int nb_sectors, | |
293 | QEMUIOVector *iov) | |
294 | { | |
295 | NFSClient *client = bs->opaque; | |
296 | NFSRPC task; | |
297 | char *buf = NULL; | |
298 | ||
d746427a | 299 | nfs_co_init_task(bs, &task); |
6542aa9c | 300 | |
2347dd7b KW |
301 | buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE); |
302 | if (nb_sectors && buf == NULL) { | |
303 | return -ENOMEM; | |
304 | } | |
305 | ||
6542aa9c PL |
306 | qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE); |
307 | ||
308 | if (nfs_pwrite_async(client->context, client->fh, | |
309 | sector_num * BDRV_SECTOR_SIZE, | |
310 | nb_sectors * BDRV_SECTOR_SIZE, | |
311 | buf, nfs_co_generic_cb, &task) != 0) { | |
312 | g_free(buf); | |
313 | return -ENOMEM; | |
314 | } | |
315 | ||
aa92d6c4 | 316 | nfs_set_events(client); |
6542aa9c | 317 | while (!task.complete) { |
6542aa9c PL |
318 | qemu_coroutine_yield(); |
319 | } | |
320 | ||
321 | g_free(buf); | |
322 | ||
323 | if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) { | |
324 | return task.ret < 0 ? task.ret : -EIO; | |
325 | } | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | static int coroutine_fn nfs_co_flush(BlockDriverState *bs) | |
331 | { | |
332 | NFSClient *client = bs->opaque; | |
333 | NFSRPC task; | |
334 | ||
d746427a | 335 | nfs_co_init_task(bs, &task); |
6542aa9c PL |
336 | |
337 | if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb, | |
338 | &task) != 0) { | |
339 | return -ENOMEM; | |
340 | } | |
341 | ||
aa92d6c4 | 342 | nfs_set_events(client); |
6542aa9c | 343 | while (!task.complete) { |
6542aa9c PL |
344 | qemu_coroutine_yield(); |
345 | } | |
346 | ||
347 | return task.ret; | |
348 | } | |
349 | ||
6542aa9c PL |
350 | static QemuOptsList runtime_opts = { |
351 | .name = "nfs", | |
352 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
353 | .desc = { | |
354 | { | |
94d6a7a7 | 355 | .name = "path", |
6542aa9c | 356 | .type = QEMU_OPT_STRING, |
94d6a7a7 AA |
357 | .help = "Path of the image on the host", |
358 | }, | |
359 | { | |
360 | .name = "uid", | |
361 | .type = QEMU_OPT_NUMBER, | |
362 | .help = "UID value to use when talking to the server", | |
363 | }, | |
364 | { | |
365 | .name = "gid", | |
366 | .type = QEMU_OPT_NUMBER, | |
367 | .help = "GID value to use when talking to the server", | |
368 | }, | |
369 | { | |
370 | .name = "tcp-syncnt", | |
371 | .type = QEMU_OPT_NUMBER, | |
372 | .help = "Number of SYNs to send during the session establish", | |
373 | }, | |
374 | { | |
375 | .name = "readahead", | |
376 | .type = QEMU_OPT_NUMBER, | |
377 | .help = "Set the readahead size in bytes", | |
378 | }, | |
379 | { | |
380 | .name = "pagecache", | |
381 | .type = QEMU_OPT_NUMBER, | |
382 | .help = "Set the pagecache size in bytes", | |
383 | }, | |
384 | { | |
385 | .name = "debug", | |
386 | .type = QEMU_OPT_NUMBER, | |
387 | .help = "Set the NFS debug level (max 2)", | |
6542aa9c PL |
388 | }, |
389 | { /* end of list */ } | |
390 | }, | |
391 | }; | |
392 | ||
471799d1 SH |
393 | static void nfs_detach_aio_context(BlockDriverState *bs) |
394 | { | |
395 | NFSClient *client = bs->opaque; | |
396 | ||
dca21ef2 FZ |
397 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
398 | false, NULL, NULL, NULL); | |
471799d1 SH |
399 | client->events = 0; |
400 | } | |
401 | ||
402 | static void nfs_attach_aio_context(BlockDriverState *bs, | |
403 | AioContext *new_context) | |
404 | { | |
405 | NFSClient *client = bs->opaque; | |
406 | ||
407 | client->aio_context = new_context; | |
408 | nfs_set_events(client); | |
409 | } | |
410 | ||
6542aa9c PL |
411 | static void nfs_client_close(NFSClient *client) |
412 | { | |
413 | if (client->context) { | |
414 | if (client->fh) { | |
415 | nfs_close(client->context, client->fh); | |
416 | } | |
dca21ef2 FZ |
417 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
418 | false, NULL, NULL, NULL); | |
6542aa9c PL |
419 | nfs_destroy_context(client->context); |
420 | } | |
421 | memset(client, 0, sizeof(NFSClient)); | |
422 | } | |
423 | ||
424 | static void nfs_file_close(BlockDriverState *bs) | |
425 | { | |
426 | NFSClient *client = bs->opaque; | |
427 | nfs_client_close(client); | |
428 | } | |
429 | ||
94d6a7a7 AA |
430 | static NFSServer *nfs_config(QDict *options, Error **errp) |
431 | { | |
432 | NFSServer *server = NULL; | |
433 | QDict *addr = NULL; | |
434 | QObject *crumpled_addr = NULL; | |
435 | Visitor *iv = NULL; | |
436 | Error *local_error = NULL; | |
437 | ||
438 | qdict_extract_subqdict(options, &addr, "server."); | |
439 | if (!qdict_size(addr)) { | |
440 | error_setg(errp, "NFS server address missing"); | |
441 | goto out; | |
442 | } | |
443 | ||
444 | crumpled_addr = qdict_crumple(addr, errp); | |
445 | if (!crumpled_addr) { | |
446 | goto out; | |
447 | } | |
448 | ||
449 | iv = qobject_input_visitor_new(crumpled_addr, true); | |
450 | visit_type_NFSServer(iv, NULL, &server, &local_error); | |
451 | if (local_error) { | |
452 | error_propagate(errp, local_error); | |
453 | goto out; | |
454 | } | |
455 | ||
456 | out: | |
457 | QDECREF(addr); | |
458 | qobject_decref(crumpled_addr); | |
459 | visit_free(iv); | |
460 | return server; | |
461 | } | |
462 | ||
463 | ||
464 | static int64_t nfs_client_open(NFSClient *client, QDict *options, | |
38f8d5e0 | 465 | int flags, Error **errp, int open_flags) |
6542aa9c | 466 | { |
94d6a7a7 AA |
467 | int ret = -EINVAL; |
468 | QemuOpts *opts = NULL; | |
469 | Error *local_err = NULL; | |
6542aa9c | 470 | struct stat st; |
6542aa9c PL |
471 | char *file = NULL, *strp = NULL; |
472 | ||
94d6a7a7 AA |
473 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
474 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
475 | if (local_err) { | |
476 | error_propagate(errp, local_err); | |
477 | ret = -EINVAL; | |
5f4d5e1a HR |
478 | goto fail; |
479 | } | |
94d6a7a7 AA |
480 | |
481 | client->path = g_strdup(qemu_opt_get(opts, "path")); | |
482 | if (!client->path) { | |
483 | ret = -EINVAL; | |
484 | error_setg(errp, "No path was specified"); | |
6542aa9c PL |
485 | goto fail; |
486 | } | |
94d6a7a7 AA |
487 | |
488 | strp = strrchr(client->path, '/'); | |
6542aa9c PL |
489 | if (strp == NULL) { |
490 | error_setg(errp, "Invalid URL specified"); | |
491 | goto fail; | |
492 | } | |
493 | file = g_strdup(strp); | |
494 | *strp = 0; | |
495 | ||
94d6a7a7 AA |
496 | /* Pop the config into our state object, Exit if invalid */ |
497 | client->server = nfs_config(options, errp); | |
498 | if (!client->server) { | |
499 | ret = -EINVAL; | |
500 | goto fail; | |
501 | } | |
502 | ||
6542aa9c PL |
503 | client->context = nfs_init_context(); |
504 | if (client->context == NULL) { | |
505 | error_setg(errp, "Failed to init NFS context"); | |
506 | goto fail; | |
507 | } | |
508 | ||
94d6a7a7 AA |
509 | if (qemu_opt_get(opts, "uid")) { |
510 | client->uid = qemu_opt_get_number(opts, "uid", 0); | |
511 | nfs_set_uid(client->context, client->uid); | |
512 | } | |
513 | ||
514 | if (qemu_opt_get(opts, "gid")) { | |
515 | client->gid = qemu_opt_get_number(opts, "gid", 0); | |
516 | nfs_set_gid(client->context, client->gid); | |
517 | } | |
518 | ||
519 | if (qemu_opt_get(opts, "tcp-syncnt")) { | |
520 | client->tcp_syncnt = qemu_opt_get_number(opts, "tcp-syncnt", 0); | |
521 | nfs_set_tcp_syncnt(client->context, client->tcp_syncnt); | |
522 | } | |
523 | ||
524 | #ifdef LIBNFS_FEATURE_READAHEAD | |
525 | if (qemu_opt_get(opts, "readahead")) { | |
526 | if (open_flags & BDRV_O_NOCACHE) { | |
527 | error_setg(errp, "Cannot enable NFS readahead " | |
528 | "if cache.direct = on"); | |
6542aa9c PL |
529 | goto fail; |
530 | } | |
94d6a7a7 AA |
531 | client->readahead = qemu_opt_get_number(opts, "readahead", 0); |
532 | if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) { | |
533 | error_report("NFS Warning: Truncating NFS readahead " | |
534 | "size to %d", QEMU_NFS_MAX_READAHEAD_SIZE); | |
535 | client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE; | |
7c24384b | 536 | } |
94d6a7a7 | 537 | nfs_set_readahead(client->context, client->readahead); |
d99b26c4 | 538 | #ifdef LIBNFS_FEATURE_PAGECACHE |
94d6a7a7 | 539 | nfs_set_pagecache_ttl(client->context, 0); |
d99b26c4 | 540 | #endif |
94d6a7a7 AA |
541 | client->cache_used = true; |
542 | } | |
d99b26c4 | 543 | #endif |
94d6a7a7 | 544 | |
d99b26c4 | 545 | #ifdef LIBNFS_FEATURE_PAGECACHE |
94d6a7a7 AA |
546 | if (qemu_opt_get(opts, "pagecache")) { |
547 | if (open_flags & BDRV_O_NOCACHE) { | |
548 | error_setg(errp, "Cannot enable NFS pagecache " | |
549 | "if cache.direct = on"); | |
550 | goto fail; | |
551 | } | |
552 | client->pagecache = qemu_opt_get_number(opts, "pagecache", 0); | |
553 | if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) { | |
554 | error_report("NFS Warning: Truncating NFS pagecache " | |
555 | "size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE); | |
556 | client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE; | |
557 | } | |
558 | nfs_set_pagecache(client->context, client->pagecache); | |
559 | nfs_set_pagecache_ttl(client->context, 0); | |
560 | client->cache_used = true; | |
561 | } | |
7725b8bf | 562 | #endif |
94d6a7a7 | 563 | |
7725b8bf | 564 | #ifdef LIBNFS_FEATURE_DEBUG |
94d6a7a7 AA |
565 | if (qemu_opt_get(opts, "debug")) { |
566 | client->debug = qemu_opt_get_number(opts, "debug", 0); | |
567 | /* limit the maximum debug level to avoid potential flooding | |
568 | * of our log files. */ | |
569 | if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) { | |
570 | error_report("NFS Warning: Limiting NFS debug level " | |
571 | "to %d", QEMU_NFS_MAX_DEBUG_LEVEL); | |
572 | client->debug = QEMU_NFS_MAX_DEBUG_LEVEL; | |
6542aa9c | 573 | } |
94d6a7a7 | 574 | nfs_set_debug(client->context, client->debug); |
6542aa9c | 575 | } |
94d6a7a7 | 576 | #endif |
6542aa9c | 577 | |
94d6a7a7 | 578 | ret = nfs_mount(client->context, client->server->host, client->path); |
6542aa9c PL |
579 | if (ret < 0) { |
580 | error_setg(errp, "Failed to mount nfs share: %s", | |
581 | nfs_get_error(client->context)); | |
582 | goto fail; | |
583 | } | |
584 | ||
585 | if (flags & O_CREAT) { | |
586 | ret = nfs_creat(client->context, file, 0600, &client->fh); | |
587 | if (ret < 0) { | |
588 | error_setg(errp, "Failed to create file: %s", | |
589 | nfs_get_error(client->context)); | |
590 | goto fail; | |
591 | } | |
592 | } else { | |
593 | ret = nfs_open(client->context, file, flags, &client->fh); | |
594 | if (ret < 0) { | |
595 | error_setg(errp, "Failed to open file : %s", | |
596 | nfs_get_error(client->context)); | |
597 | goto fail; | |
598 | } | |
599 | } | |
600 | ||
601 | ret = nfs_fstat(client->context, client->fh, &st); | |
602 | if (ret < 0) { | |
603 | error_setg(errp, "Failed to fstat file: %s", | |
604 | nfs_get_error(client->context)); | |
605 | goto fail; | |
606 | } | |
607 | ||
608 | ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE); | |
18a8056e | 609 | client->st_blocks = st.st_blocks; |
6542aa9c | 610 | client->has_zero_init = S_ISREG(st.st_mode); |
94d6a7a7 | 611 | *strp = '/'; |
6542aa9c | 612 | goto out; |
94d6a7a7 | 613 | |
6542aa9c PL |
614 | fail: |
615 | nfs_client_close(client); | |
616 | out: | |
94d6a7a7 | 617 | qemu_opts_del(opts); |
6542aa9c PL |
618 | g_free(file); |
619 | return ret; | |
620 | } | |
621 | ||
622 | static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, | |
623 | Error **errp) { | |
624 | NFSClient *client = bs->opaque; | |
625 | int64_t ret; | |
6542aa9c | 626 | |
471799d1 SH |
627 | client->aio_context = bdrv_get_aio_context(bs); |
628 | ||
94d6a7a7 | 629 | ret = nfs_client_open(client, options, |
6542aa9c | 630 | (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY, |
38f8d5e0 | 631 | errp, bs->open_flags); |
6542aa9c | 632 | if (ret < 0) { |
94d6a7a7 | 633 | return ret; |
6542aa9c PL |
634 | } |
635 | bs->total_sectors = ret; | |
810f4f86 | 636 | ret = 0; |
810f4f86 | 637 | return ret; |
6542aa9c PL |
638 | } |
639 | ||
fd752801 HR |
640 | static QemuOptsList nfs_create_opts = { |
641 | .name = "nfs-create-opts", | |
642 | .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head), | |
643 | .desc = { | |
644 | { | |
645 | .name = BLOCK_OPT_SIZE, | |
646 | .type = QEMU_OPT_SIZE, | |
647 | .help = "Virtual disk size" | |
648 | }, | |
649 | { /* end of list */ } | |
650 | } | |
651 | }; | |
652 | ||
98c10b81 | 653 | static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp) |
6542aa9c PL |
654 | { |
655 | int ret = 0; | |
656 | int64_t total_size = 0; | |
5839e53b | 657 | NFSClient *client = g_new0(NFSClient, 1); |
94d6a7a7 | 658 | QDict *options = NULL; |
6542aa9c | 659 | |
471799d1 SH |
660 | client->aio_context = qemu_get_aio_context(); |
661 | ||
6542aa9c | 662 | /* Read out options */ |
c2eb918e HT |
663 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
664 | BDRV_SECTOR_SIZE); | |
6542aa9c | 665 | |
94d6a7a7 AA |
666 | options = qdict_new(); |
667 | ret = nfs_parse_uri(url, options, errp); | |
668 | if (ret < 0) { | |
669 | goto out; | |
670 | } | |
671 | ||
672 | ret = nfs_client_open(client, options, O_CREAT, errp, 0); | |
6542aa9c PL |
673 | if (ret < 0) { |
674 | goto out; | |
675 | } | |
676 | ret = nfs_ftruncate(client->context, client->fh, total_size); | |
677 | nfs_client_close(client); | |
678 | out: | |
679 | g_free(client); | |
680 | return ret; | |
681 | } | |
682 | ||
683 | static int nfs_has_zero_init(BlockDriverState *bs) | |
684 | { | |
685 | NFSClient *client = bs->opaque; | |
686 | return client->has_zero_init; | |
687 | } | |
688 | ||
d746427a PB |
689 | static void |
690 | nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data, | |
691 | void *private_data) | |
692 | { | |
693 | NFSRPC *task = private_data; | |
694 | task->ret = ret; | |
695 | if (task->ret == 0) { | |
696 | memcpy(task->st, data, sizeof(struct stat)); | |
697 | } | |
698 | if (task->ret < 0) { | |
699 | error_report("NFS Error: %s", nfs_get_error(nfs)); | |
700 | } | |
701 | task->complete = 1; | |
c9d1a561 | 702 | bdrv_wakeup(task->bs); |
d746427a PB |
703 | } |
704 | ||
6542aa9c PL |
705 | static int64_t nfs_get_allocated_file_size(BlockDriverState *bs) |
706 | { | |
707 | NFSClient *client = bs->opaque; | |
708 | NFSRPC task = {0}; | |
709 | struct stat st; | |
710 | ||
18a8056e PL |
711 | if (bdrv_is_read_only(bs) && |
712 | !(bs->open_flags & BDRV_O_NOCACHE)) { | |
713 | return client->st_blocks * 512; | |
714 | } | |
715 | ||
d746427a | 716 | task.bs = bs; |
6542aa9c | 717 | task.st = &st; |
d746427a | 718 | if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb, |
6542aa9c PL |
719 | &task) != 0) { |
720 | return -ENOMEM; | |
721 | } | |
722 | ||
aa92d6c4 | 723 | nfs_set_events(client); |
d746427a | 724 | BDRV_POLL_WHILE(bs, !task.complete); |
6542aa9c | 725 | |
055c6f91 | 726 | return (task.ret < 0 ? task.ret : st.st_blocks * 512); |
6542aa9c PL |
727 | } |
728 | ||
729 | static int nfs_file_truncate(BlockDriverState *bs, int64_t offset) | |
730 | { | |
731 | NFSClient *client = bs->opaque; | |
732 | return nfs_ftruncate(client->context, client->fh, offset); | |
733 | } | |
734 | ||
18a8056e PL |
735 | /* Note that this will not re-establish a connection with the NFS server |
736 | * - it is effectively a NOP. */ | |
737 | static int nfs_reopen_prepare(BDRVReopenState *state, | |
738 | BlockReopenQueue *queue, Error **errp) | |
739 | { | |
740 | NFSClient *client = state->bs->opaque; | |
741 | struct stat st; | |
742 | int ret = 0; | |
743 | ||
744 | if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) { | |
745 | error_setg(errp, "Cannot open a read-only mount as read-write"); | |
746 | return -EACCES; | |
747 | } | |
748 | ||
38f8d5e0 | 749 | if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) { |
d99b26c4 PL |
750 | error_setg(errp, "Cannot disable cache if libnfs readahead or" |
751 | " pagecache is enabled"); | |
38f8d5e0 PL |
752 | return -EINVAL; |
753 | } | |
754 | ||
18a8056e PL |
755 | /* Update cache for read-only reopens */ |
756 | if (!(state->flags & BDRV_O_RDWR)) { | |
757 | ret = nfs_fstat(client->context, client->fh, &st); | |
758 | if (ret < 0) { | |
759 | error_setg(errp, "Failed to fstat file: %s", | |
760 | nfs_get_error(client->context)); | |
761 | return ret; | |
762 | } | |
763 | client->st_blocks = st.st_blocks; | |
764 | } | |
765 | ||
766 | return 0; | |
767 | } | |
768 | ||
94d6a7a7 AA |
769 | static void nfs_refresh_filename(BlockDriverState *bs, QDict *options) |
770 | { | |
771 | NFSClient *client = bs->opaque; | |
772 | QDict *opts = qdict_new(); | |
773 | QObject *server_qdict; | |
774 | Visitor *ov; | |
775 | ||
776 | qdict_put(opts, "driver", qstring_from_str("nfs")); | |
777 | ||
778 | if (client->uid && !client->gid) { | |
779 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
780 | "nfs://%s%s?uid=%" PRId64, client->server->host, client->path, | |
781 | client->uid); | |
782 | } else if (!client->uid && client->gid) { | |
783 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
784 | "nfs://%s%s?gid=%" PRId64, client->server->host, client->path, | |
785 | client->gid); | |
786 | } else if (client->uid && client->gid) { | |
787 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
788 | "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64, | |
789 | client->server->host, client->path, client->uid, client->gid); | |
790 | } else { | |
791 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
792 | "nfs://%s%s", client->server->host, client->path); | |
793 | } | |
794 | ||
795 | ov = qobject_output_visitor_new(&server_qdict); | |
796 | visit_type_NFSServer(ov, NULL, &client->server, &error_abort); | |
797 | visit_complete(ov, &server_qdict); | |
798 | assert(qobject_type(server_qdict) == QTYPE_QDICT); | |
799 | ||
800 | qdict_put_obj(opts, "server", server_qdict); | |
801 | qdict_put(opts, "path", qstring_from_str(client->path)); | |
802 | ||
803 | if (client->uid) { | |
804 | qdict_put(opts, "uid", qint_from_int(client->uid)); | |
805 | } | |
806 | if (client->gid) { | |
807 | qdict_put(opts, "gid", qint_from_int(client->gid)); | |
808 | } | |
809 | if (client->tcp_syncnt) { | |
810 | qdict_put(opts, "tcp-syncnt", | |
811 | qint_from_int(client->tcp_syncnt)); | |
812 | } | |
813 | if (client->readahead) { | |
814 | qdict_put(opts, "readahead", | |
815 | qint_from_int(client->readahead)); | |
816 | } | |
817 | if (client->pagecache) { | |
818 | qdict_put(opts, "pagecache", | |
819 | qint_from_int(client->pagecache)); | |
820 | } | |
821 | if (client->debug) { | |
822 | qdict_put(opts, "debug", qint_from_int(client->debug)); | |
823 | } | |
824 | ||
825 | visit_free(ov); | |
826 | qdict_flatten(opts); | |
827 | bs->full_open_options = opts; | |
828 | } | |
829 | ||
d99b26c4 PL |
830 | #ifdef LIBNFS_FEATURE_PAGECACHE |
831 | static void nfs_invalidate_cache(BlockDriverState *bs, | |
832 | Error **errp) | |
833 | { | |
834 | NFSClient *client = bs->opaque; | |
835 | nfs_pagecache_invalidate(client->context, client->fh); | |
836 | } | |
837 | #endif | |
838 | ||
6542aa9c | 839 | static BlockDriver bdrv_nfs = { |
471799d1 SH |
840 | .format_name = "nfs", |
841 | .protocol_name = "nfs", | |
842 | ||
843 | .instance_size = sizeof(NFSClient), | |
94d6a7a7 | 844 | .bdrv_parse_filename = nfs_parse_filename, |
fd752801 HR |
845 | .create_opts = &nfs_create_opts, |
846 | ||
471799d1 SH |
847 | .bdrv_has_zero_init = nfs_has_zero_init, |
848 | .bdrv_get_allocated_file_size = nfs_get_allocated_file_size, | |
849 | .bdrv_truncate = nfs_file_truncate, | |
850 | ||
851 | .bdrv_file_open = nfs_file_open, | |
852 | .bdrv_close = nfs_file_close, | |
c282e1fd | 853 | .bdrv_create = nfs_file_create, |
18a8056e | 854 | .bdrv_reopen_prepare = nfs_reopen_prepare, |
471799d1 SH |
855 | |
856 | .bdrv_co_readv = nfs_co_readv, | |
857 | .bdrv_co_writev = nfs_co_writev, | |
858 | .bdrv_co_flush_to_disk = nfs_co_flush, | |
859 | ||
860 | .bdrv_detach_aio_context = nfs_detach_aio_context, | |
861 | .bdrv_attach_aio_context = nfs_attach_aio_context, | |
94d6a7a7 | 862 | .bdrv_refresh_filename = nfs_refresh_filename, |
d99b26c4 PL |
863 | |
864 | #ifdef LIBNFS_FEATURE_PAGECACHE | |
865 | .bdrv_invalidate_cache = nfs_invalidate_cache, | |
866 | #endif | |
6542aa9c PL |
867 | }; |
868 | ||
869 | static void nfs_block_init(void) | |
870 | { | |
871 | bdrv_register(&bdrv_nfs); | |
872 | } | |
873 | ||
874 | block_init(nfs_block_init); |