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