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