]>
Commit | Line | Data |
---|---|---|
6542aa9c PL |
1 | /* |
2 | * QEMU Block driver for native access to files on NFS shares | |
3 | * | |
f1a7ff77 | 4 | * Copyright (c) 2014-2017 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> | |
6542aa9c PL |
28 | #include "qemu/config-file.h" |
29 | #include "qemu/error-report.h" | |
d165b8cb | 30 | #include "qapi/error.h" |
6542aa9c PL |
31 | #include "block/block_int.h" |
32 | #include "trace.h" | |
33 | #include "qemu/iov.h" | |
922a01a0 | 34 | #include "qemu/option.h" |
6542aa9c | 35 | #include "qemu/uri.h" |
0d94b746 | 36 | #include "qemu/cutils.h" |
6542aa9c | 37 | #include "sysemu/sysemu.h" |
9af23989 | 38 | #include "qapi/qapi-visit-block-core.h" |
94d6a7a7 | 39 | #include "qapi/qmp/qdict.h" |
94d6a7a7 | 40 | #include "qapi/qmp/qstring.h" |
94d6a7a7 AA |
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 | ||
471799d1 SH |
370 | static void nfs_detach_aio_context(BlockDriverState *bs) |
371 | { | |
372 | NFSClient *client = bs->opaque; | |
373 | ||
dca21ef2 | 374 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
f6a51c84 | 375 | false, NULL, NULL, NULL, NULL); |
471799d1 SH |
376 | client->events = 0; |
377 | } | |
378 | ||
379 | static void nfs_attach_aio_context(BlockDriverState *bs, | |
380 | AioContext *new_context) | |
381 | { | |
382 | NFSClient *client = bs->opaque; | |
383 | ||
384 | client->aio_context = new_context; | |
385 | nfs_set_events(client); | |
386 | } | |
387 | ||
6542aa9c PL |
388 | static void nfs_client_close(NFSClient *client) |
389 | { | |
390 | if (client->context) { | |
391 | if (client->fh) { | |
392 | nfs_close(client->context, client->fh); | |
113fe792 | 393 | client->fh = NULL; |
6542aa9c | 394 | } |
dca21ef2 | 395 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
f6a51c84 | 396 | false, NULL, NULL, NULL, NULL); |
6542aa9c | 397 | nfs_destroy_context(client->context); |
113fe792 | 398 | client->context = NULL; |
6542aa9c | 399 | } |
113fe792 JC |
400 | g_free(client->path); |
401 | qemu_mutex_destroy(&client->mutex); | |
402 | qapi_free_NFSServer(client->server); | |
403 | client->server = NULL; | |
6542aa9c PL |
404 | } |
405 | ||
406 | static void nfs_file_close(BlockDriverState *bs) | |
407 | { | |
408 | NFSClient *client = bs->opaque; | |
409 | nfs_client_close(client); | |
410 | } | |
411 | ||
c22a0345 | 412 | static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts, |
cb8d4bf6 | 413 | int flags, int open_flags, Error **errp) |
6542aa9c | 414 | { |
f1a7ff77 | 415 | int64_t ret = -EINVAL; |
6542aa9c | 416 | struct stat st; |
6542aa9c PL |
417 | char *file = NULL, *strp = NULL; |
418 | ||
113fe792 | 419 | qemu_mutex_init(&client->mutex); |
94d6a7a7 | 420 | |
c22a0345 | 421 | client->path = g_strdup(opts->path); |
94d6a7a7 AA |
422 | |
423 | strp = strrchr(client->path, '/'); | |
6542aa9c PL |
424 | if (strp == NULL) { |
425 | error_setg(errp, "Invalid URL specified"); | |
426 | goto fail; | |
427 | } | |
428 | file = g_strdup(strp); | |
429 | *strp = 0; | |
430 | ||
c22a0345 KW |
431 | /* Steal the NFSServer object from opts; set the original pointer to NULL |
432 | * to avoid use after free and double free. */ | |
433 | client->server = opts->server; | |
434 | opts->server = NULL; | |
94d6a7a7 | 435 | |
6542aa9c PL |
436 | client->context = nfs_init_context(); |
437 | if (client->context == NULL) { | |
438 | error_setg(errp, "Failed to init NFS context"); | |
439 | goto fail; | |
440 | } | |
441 | ||
c22a0345 KW |
442 | if (opts->has_user) { |
443 | client->uid = opts->user; | |
94d6a7a7 AA |
444 | nfs_set_uid(client->context, client->uid); |
445 | } | |
446 | ||
c22a0345 KW |
447 | if (opts->has_group) { |
448 | client->gid = opts->group; | |
94d6a7a7 AA |
449 | nfs_set_gid(client->context, client->gid); |
450 | } | |
451 | ||
c22a0345 KW |
452 | if (opts->has_tcp_syn_count) { |
453 | client->tcp_syncnt = opts->tcp_syn_count; | |
94d6a7a7 AA |
454 | nfs_set_tcp_syncnt(client->context, client->tcp_syncnt); |
455 | } | |
456 | ||
457 | #ifdef LIBNFS_FEATURE_READAHEAD | |
c22a0345 | 458 | if (opts->has_readahead_size) { |
94d6a7a7 AA |
459 | if (open_flags & BDRV_O_NOCACHE) { |
460 | error_setg(errp, "Cannot enable NFS readahead " | |
461 | "if cache.direct = on"); | |
6542aa9c PL |
462 | goto fail; |
463 | } | |
c22a0345 | 464 | client->readahead = opts->readahead_size; |
94d6a7a7 | 465 | if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) { |
3dc6f869 AF |
466 | warn_report("Truncating NFS readahead size to %d", |
467 | QEMU_NFS_MAX_READAHEAD_SIZE); | |
94d6a7a7 | 468 | client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE; |
7c24384b | 469 | } |
94d6a7a7 | 470 | nfs_set_readahead(client->context, client->readahead); |
d99b26c4 | 471 | #ifdef LIBNFS_FEATURE_PAGECACHE |
94d6a7a7 | 472 | nfs_set_pagecache_ttl(client->context, 0); |
d99b26c4 | 473 | #endif |
94d6a7a7 AA |
474 | client->cache_used = true; |
475 | } | |
d99b26c4 | 476 | #endif |
94d6a7a7 | 477 | |
d99b26c4 | 478 | #ifdef LIBNFS_FEATURE_PAGECACHE |
c22a0345 | 479 | if (opts->has_page_cache_size) { |
94d6a7a7 AA |
480 | if (open_flags & BDRV_O_NOCACHE) { |
481 | error_setg(errp, "Cannot enable NFS pagecache " | |
482 | "if cache.direct = on"); | |
483 | goto fail; | |
484 | } | |
c22a0345 | 485 | client->pagecache = opts->page_cache_size; |
94d6a7a7 | 486 | if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) { |
3dc6f869 AF |
487 | warn_report("Truncating NFS pagecache size to %d pages", |
488 | QEMU_NFS_MAX_PAGECACHE_SIZE); | |
94d6a7a7 AA |
489 | client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE; |
490 | } | |
491 | nfs_set_pagecache(client->context, client->pagecache); | |
492 | nfs_set_pagecache_ttl(client->context, 0); | |
493 | client->cache_used = true; | |
494 | } | |
7725b8bf | 495 | #endif |
94d6a7a7 | 496 | |
7725b8bf | 497 | #ifdef LIBNFS_FEATURE_DEBUG |
c22a0345 KW |
498 | if (opts->has_debug) { |
499 | client->debug = opts->debug; | |
94d6a7a7 AA |
500 | /* limit the maximum debug level to avoid potential flooding |
501 | * of our log files. */ | |
502 | if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) { | |
3dc6f869 AF |
503 | warn_report("Limiting NFS debug level to %d", |
504 | QEMU_NFS_MAX_DEBUG_LEVEL); | |
94d6a7a7 | 505 | client->debug = QEMU_NFS_MAX_DEBUG_LEVEL; |
6542aa9c | 506 | } |
94d6a7a7 | 507 | nfs_set_debug(client->context, client->debug); |
6542aa9c | 508 | } |
94d6a7a7 | 509 | #endif |
6542aa9c | 510 | |
94d6a7a7 | 511 | ret = nfs_mount(client->context, client->server->host, client->path); |
6542aa9c PL |
512 | if (ret < 0) { |
513 | error_setg(errp, "Failed to mount nfs share: %s", | |
514 | nfs_get_error(client->context)); | |
515 | goto fail; | |
516 | } | |
517 | ||
518 | if (flags & O_CREAT) { | |
519 | ret = nfs_creat(client->context, file, 0600, &client->fh); | |
520 | if (ret < 0) { | |
521 | error_setg(errp, "Failed to create file: %s", | |
522 | nfs_get_error(client->context)); | |
523 | goto fail; | |
524 | } | |
525 | } else { | |
526 | ret = nfs_open(client->context, file, flags, &client->fh); | |
527 | if (ret < 0) { | |
528 | error_setg(errp, "Failed to open file : %s", | |
529 | nfs_get_error(client->context)); | |
530 | goto fail; | |
531 | } | |
532 | } | |
533 | ||
534 | ret = nfs_fstat(client->context, client->fh, &st); | |
535 | if (ret < 0) { | |
536 | error_setg(errp, "Failed to fstat file: %s", | |
537 | nfs_get_error(client->context)); | |
538 | goto fail; | |
539 | } | |
540 | ||
541 | ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE); | |
18a8056e | 542 | client->st_blocks = st.st_blocks; |
6542aa9c | 543 | client->has_zero_init = S_ISREG(st.st_mode); |
94d6a7a7 | 544 | *strp = '/'; |
6542aa9c | 545 | goto out; |
94d6a7a7 | 546 | |
6542aa9c PL |
547 | fail: |
548 | nfs_client_close(client); | |
549 | out: | |
6542aa9c PL |
550 | g_free(file); |
551 | return ret; | |
552 | } | |
553 | ||
a1a42af4 KW |
554 | static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options, |
555 | Error **errp) | |
c22a0345 KW |
556 | { |
557 | BlockdevOptionsNfs *opts = NULL; | |
558 | QObject *crumpled = NULL; | |
559 | Visitor *v; | |
c82be42c | 560 | const QDictEntry *e; |
c22a0345 | 561 | Error *local_err = NULL; |
c22a0345 KW |
562 | |
563 | crumpled = qdict_crumple(options, errp); | |
564 | if (crumpled == NULL) { | |
a1a42af4 | 565 | return NULL; |
c22a0345 KW |
566 | } |
567 | ||
568 | v = qobject_input_visitor_new_keyval(crumpled); | |
569 | visit_type_BlockdevOptionsNfs(v, NULL, &opts, &local_err); | |
570 | visit_free(v); | |
cb3e7f08 | 571 | qobject_unref(crumpled); |
c22a0345 KW |
572 | |
573 | if (local_err) { | |
54b7af43 | 574 | error_propagate(errp, local_err); |
a1a42af4 KW |
575 | return NULL; |
576 | } | |
577 | ||
c82be42c KW |
578 | /* Remove the processed options from the QDict (the visitor processes |
579 | * _all_ options in the QDict) */ | |
580 | while ((e = qdict_first(options))) { | |
581 | qdict_del(options, e->key); | |
582 | } | |
583 | ||
a1a42af4 KW |
584 | return opts; |
585 | } | |
586 | ||
587 | static int64_t nfs_client_open_qdict(NFSClient *client, QDict *options, | |
588 | int flags, int open_flags, Error **errp) | |
589 | { | |
590 | BlockdevOptionsNfs *opts; | |
591 | int ret; | |
592 | ||
593 | opts = nfs_options_qdict_to_qapi(options, errp); | |
594 | if (opts == NULL) { | |
c22a0345 KW |
595 | ret = -EINVAL; |
596 | goto fail; | |
597 | } | |
598 | ||
599 | ret = nfs_client_open(client, opts, flags, open_flags, errp); | |
600 | fail: | |
c22a0345 KW |
601 | qapi_free_BlockdevOptionsNfs(opts); |
602 | return ret; | |
603 | } | |
604 | ||
6542aa9c PL |
605 | static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, |
606 | Error **errp) { | |
607 | NFSClient *client = bs->opaque; | |
608 | int64_t ret; | |
6542aa9c | 609 | |
471799d1 SH |
610 | client->aio_context = bdrv_get_aio_context(bs); |
611 | ||
c22a0345 KW |
612 | ret = nfs_client_open_qdict(client, options, |
613 | (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY, | |
614 | bs->open_flags, errp); | |
6542aa9c | 615 | if (ret < 0) { |
94d6a7a7 | 616 | return ret; |
6542aa9c | 617 | } |
113fe792 | 618 | |
6542aa9c | 619 | bs->total_sectors = ret; |
810f4f86 | 620 | ret = 0; |
810f4f86 | 621 | return ret; |
6542aa9c PL |
622 | } |
623 | ||
fd752801 HR |
624 | static QemuOptsList nfs_create_opts = { |
625 | .name = "nfs-create-opts", | |
626 | .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head), | |
627 | .desc = { | |
628 | { | |
629 | .name = BLOCK_OPT_SIZE, | |
630 | .type = QEMU_OPT_SIZE, | |
631 | .help = "Virtual disk size" | |
632 | }, | |
633 | { /* end of list */ } | |
634 | } | |
635 | }; | |
636 | ||
a1a42af4 | 637 | static int nfs_file_co_create(BlockdevCreateOptions *options, Error **errp) |
6542aa9c | 638 | { |
a1a42af4 | 639 | BlockdevCreateOptionsNfs *opts = &options->u.nfs; |
5839e53b | 640 | NFSClient *client = g_new0(NFSClient, 1); |
a1a42af4 KW |
641 | int ret; |
642 | ||
643 | assert(options->driver == BLOCKDEV_DRIVER_NFS); | |
6542aa9c | 644 | |
471799d1 SH |
645 | client->aio_context = qemu_get_aio_context(); |
646 | ||
a1a42af4 KW |
647 | ret = nfs_client_open(client, opts->location, O_CREAT, 0, errp); |
648 | if (ret < 0) { | |
649 | goto out; | |
650 | } | |
651 | ret = nfs_ftruncate(client->context, client->fh, opts->size); | |
652 | nfs_client_close(client); | |
653 | ||
654 | out: | |
655 | g_free(client); | |
656 | return ret; | |
657 | } | |
658 | ||
659 | static int coroutine_fn nfs_file_co_create_opts(const char *url, QemuOpts *opts, | |
660 | Error **errp) | |
661 | { | |
662 | BlockdevCreateOptions *create_options; | |
663 | BlockdevCreateOptionsNfs *nfs_opts; | |
664 | QDict *options; | |
665 | int ret; | |
666 | ||
667 | create_options = g_new0(BlockdevCreateOptions, 1); | |
668 | create_options->driver = BLOCKDEV_DRIVER_NFS; | |
669 | nfs_opts = &create_options->u.nfs; | |
670 | ||
6542aa9c | 671 | /* Read out options */ |
a1a42af4 KW |
672 | nfs_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
673 | BDRV_SECTOR_SIZE); | |
6542aa9c | 674 | |
94d6a7a7 AA |
675 | options = qdict_new(); |
676 | ret = nfs_parse_uri(url, options, errp); | |
677 | if (ret < 0) { | |
678 | goto out; | |
679 | } | |
680 | ||
a1a42af4 KW |
681 | nfs_opts->location = nfs_options_qdict_to_qapi(options, errp); |
682 | if (nfs_opts->location == NULL) { | |
683 | ret = -EINVAL; | |
684 | goto out; | |
685 | } | |
686 | ||
687 | ret = nfs_file_co_create(create_options, errp); | |
6542aa9c PL |
688 | if (ret < 0) { |
689 | goto out; | |
690 | } | |
a1a42af4 KW |
691 | |
692 | ret = 0; | |
6542aa9c | 693 | out: |
cb3e7f08 | 694 | qobject_unref(options); |
a1a42af4 | 695 | qapi_free_BlockdevCreateOptions(create_options); |
6542aa9c PL |
696 | return ret; |
697 | } | |
698 | ||
699 | static int nfs_has_zero_init(BlockDriverState *bs) | |
700 | { | |
701 | NFSClient *client = bs->opaque; | |
702 | return client->has_zero_init; | |
703 | } | |
704 | ||
37d1e4d9 | 705 | /* Called (via nfs_service) with QemuMutex held. */ |
d746427a PB |
706 | static void |
707 | nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data, | |
708 | void *private_data) | |
709 | { | |
710 | NFSRPC *task = private_data; | |
711 | task->ret = ret; | |
712 | if (task->ret == 0) { | |
713 | memcpy(task->st, data, sizeof(struct stat)); | |
714 | } | |
715 | if (task->ret < 0) { | |
716 | error_report("NFS Error: %s", nfs_get_error(nfs)); | |
717 | } | |
e2a6ae7f PB |
718 | |
719 | /* Set task->complete before reading bs->wakeup. */ | |
720 | atomic_mb_set(&task->complete, 1); | |
c9d1a561 | 721 | bdrv_wakeup(task->bs); |
d746427a PB |
722 | } |
723 | ||
6542aa9c PL |
724 | static int64_t nfs_get_allocated_file_size(BlockDriverState *bs) |
725 | { | |
726 | NFSClient *client = bs->opaque; | |
727 | NFSRPC task = {0}; | |
728 | struct stat st; | |
729 | ||
18a8056e PL |
730 | if (bdrv_is_read_only(bs) && |
731 | !(bs->open_flags & BDRV_O_NOCACHE)) { | |
732 | return client->st_blocks * 512; | |
733 | } | |
734 | ||
d746427a | 735 | task.bs = bs; |
6542aa9c | 736 | task.st = &st; |
d746427a | 737 | if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb, |
6542aa9c PL |
738 | &task) != 0) { |
739 | return -ENOMEM; | |
740 | } | |
741 | ||
aa92d6c4 | 742 | nfs_set_events(client); |
d746427a | 743 | BDRV_POLL_WHILE(bs, !task.complete); |
6542aa9c | 744 | |
055c6f91 | 745 | return (task.ret < 0 ? task.ret : st.st_blocks * 512); |
6542aa9c PL |
746 | } |
747 | ||
8243ccb7 HR |
748 | static int nfs_file_truncate(BlockDriverState *bs, int64_t offset, |
749 | PreallocMode prealloc, Error **errp) | |
6542aa9c PL |
750 | { |
751 | NFSClient *client = bs->opaque; | |
f59adb32 HR |
752 | int ret; |
753 | ||
8243ccb7 HR |
754 | if (prealloc != PREALLOC_MODE_OFF) { |
755 | error_setg(errp, "Unsupported preallocation mode '%s'", | |
977c736f | 756 | PreallocMode_str(prealloc)); |
8243ccb7 HR |
757 | return -ENOTSUP; |
758 | } | |
759 | ||
f59adb32 HR |
760 | ret = nfs_ftruncate(client->context, client->fh, offset); |
761 | if (ret < 0) { | |
762 | error_setg_errno(errp, -ret, "Failed to truncate file"); | |
763 | return ret; | |
764 | } | |
765 | ||
766 | return 0; | |
6542aa9c PL |
767 | } |
768 | ||
18a8056e PL |
769 | /* Note that this will not re-establish a connection with the NFS server |
770 | * - it is effectively a NOP. */ | |
771 | static int nfs_reopen_prepare(BDRVReopenState *state, | |
772 | BlockReopenQueue *queue, Error **errp) | |
773 | { | |
774 | NFSClient *client = state->bs->opaque; | |
775 | struct stat st; | |
776 | int ret = 0; | |
777 | ||
778 | if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) { | |
779 | error_setg(errp, "Cannot open a read-only mount as read-write"); | |
780 | return -EACCES; | |
781 | } | |
782 | ||
38f8d5e0 | 783 | if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) { |
d99b26c4 PL |
784 | error_setg(errp, "Cannot disable cache if libnfs readahead or" |
785 | " pagecache is enabled"); | |
38f8d5e0 PL |
786 | return -EINVAL; |
787 | } | |
788 | ||
18a8056e PL |
789 | /* Update cache for read-only reopens */ |
790 | if (!(state->flags & BDRV_O_RDWR)) { | |
791 | ret = nfs_fstat(client->context, client->fh, &st); | |
792 | if (ret < 0) { | |
793 | error_setg(errp, "Failed to fstat file: %s", | |
794 | nfs_get_error(client->context)); | |
795 | return ret; | |
796 | } | |
797 | client->st_blocks = st.st_blocks; | |
798 | } | |
799 | ||
800 | return 0; | |
801 | } | |
802 | ||
94d6a7a7 AA |
803 | static void nfs_refresh_filename(BlockDriverState *bs, QDict *options) |
804 | { | |
805 | NFSClient *client = bs->opaque; | |
806 | QDict *opts = qdict_new(); | |
807 | QObject *server_qdict; | |
808 | Visitor *ov; | |
809 | ||
46f5ac20 | 810 | qdict_put_str(opts, "driver", "nfs"); |
94d6a7a7 AA |
811 | |
812 | if (client->uid && !client->gid) { | |
813 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
814 | "nfs://%s%s?uid=%" PRId64, client->server->host, client->path, | |
815 | client->uid); | |
816 | } else if (!client->uid && client->gid) { | |
817 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
818 | "nfs://%s%s?gid=%" PRId64, client->server->host, client->path, | |
819 | client->gid); | |
820 | } else if (client->uid && client->gid) { | |
821 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
822 | "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64, | |
823 | client->server->host, client->path, client->uid, client->gid); | |
824 | } else { | |
825 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
826 | "nfs://%s%s", client->server->host, client->path); | |
827 | } | |
828 | ||
829 | ov = qobject_output_visitor_new(&server_qdict); | |
830 | visit_type_NFSServer(ov, NULL, &client->server, &error_abort); | |
831 | visit_complete(ov, &server_qdict); | |
94d6a7a7 | 832 | qdict_put_obj(opts, "server", server_qdict); |
46f5ac20 | 833 | qdict_put_str(opts, "path", client->path); |
94d6a7a7 AA |
834 | |
835 | if (client->uid) { | |
46f5ac20 | 836 | qdict_put_int(opts, "user", client->uid); |
94d6a7a7 AA |
837 | } |
838 | if (client->gid) { | |
46f5ac20 | 839 | qdict_put_int(opts, "group", client->gid); |
94d6a7a7 AA |
840 | } |
841 | if (client->tcp_syncnt) { | |
46f5ac20 | 842 | qdict_put_int(opts, "tcp-syn-cnt", client->tcp_syncnt); |
94d6a7a7 AA |
843 | } |
844 | if (client->readahead) { | |
46f5ac20 | 845 | qdict_put_int(opts, "readahead-size", client->readahead); |
94d6a7a7 AA |
846 | } |
847 | if (client->pagecache) { | |
46f5ac20 | 848 | qdict_put_int(opts, "page-cache-size", client->pagecache); |
94d6a7a7 AA |
849 | } |
850 | if (client->debug) { | |
46f5ac20 | 851 | qdict_put_int(opts, "debug", client->debug); |
94d6a7a7 AA |
852 | } |
853 | ||
854 | visit_free(ov); | |
855 | qdict_flatten(opts); | |
856 | bs->full_open_options = opts; | |
857 | } | |
858 | ||
d99b26c4 | 859 | #ifdef LIBNFS_FEATURE_PAGECACHE |
2b148f39 PB |
860 | static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs, |
861 | Error **errp) | |
d99b26c4 PL |
862 | { |
863 | NFSClient *client = bs->opaque; | |
864 | nfs_pagecache_invalidate(client->context, client->fh); | |
865 | } | |
866 | #endif | |
867 | ||
6542aa9c | 868 | static BlockDriver bdrv_nfs = { |
471799d1 SH |
869 | .format_name = "nfs", |
870 | .protocol_name = "nfs", | |
871 | ||
872 | .instance_size = sizeof(NFSClient), | |
94d6a7a7 | 873 | .bdrv_parse_filename = nfs_parse_filename, |
fd752801 HR |
874 | .create_opts = &nfs_create_opts, |
875 | ||
471799d1 SH |
876 | .bdrv_has_zero_init = nfs_has_zero_init, |
877 | .bdrv_get_allocated_file_size = nfs_get_allocated_file_size, | |
878 | .bdrv_truncate = nfs_file_truncate, | |
879 | ||
880 | .bdrv_file_open = nfs_file_open, | |
881 | .bdrv_close = nfs_file_close, | |
a1a42af4 | 882 | .bdrv_co_create = nfs_file_co_create, |
efc75e2a | 883 | .bdrv_co_create_opts = nfs_file_co_create_opts, |
18a8056e | 884 | .bdrv_reopen_prepare = nfs_reopen_prepare, |
471799d1 | 885 | |
69785a22 PL |
886 | .bdrv_co_preadv = nfs_co_preadv, |
887 | .bdrv_co_pwritev = nfs_co_pwritev, | |
471799d1 SH |
888 | .bdrv_co_flush_to_disk = nfs_co_flush, |
889 | ||
890 | .bdrv_detach_aio_context = nfs_detach_aio_context, | |
891 | .bdrv_attach_aio_context = nfs_attach_aio_context, | |
94d6a7a7 | 892 | .bdrv_refresh_filename = nfs_refresh_filename, |
d99b26c4 PL |
893 | |
894 | #ifdef LIBNFS_FEATURE_PAGECACHE | |
2b148f39 | 895 | .bdrv_co_invalidate_cache = nfs_co_invalidate_cache, |
d99b26c4 | 896 | #endif |
6542aa9c PL |
897 | }; |
898 | ||
899 | static void nfs_block_init(void) | |
900 | { | |
901 | bdrv_register(&bdrv_nfs); | |
902 | } | |
903 | ||
904 | block_init(nfs_block_init); |