]>
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 | 31 | #include "block/block_int.h" |
609f45ea | 32 | #include "block/qdict.h" |
6542aa9c PL |
33 | #include "trace.h" |
34 | #include "qemu/iov.h" | |
db725815 | 35 | #include "qemu/main-loop.h" |
0b8fa32f | 36 | #include "qemu/module.h" |
922a01a0 | 37 | #include "qemu/option.h" |
6542aa9c | 38 | #include "qemu/uri.h" |
0d94b746 | 39 | #include "qemu/cutils.h" |
6542aa9c | 40 | #include "sysemu/sysemu.h" |
9af23989 | 41 | #include "qapi/qapi-visit-block-core.h" |
94d6a7a7 | 42 | #include "qapi/qmp/qdict.h" |
94d6a7a7 | 43 | #include "qapi/qmp/qstring.h" |
94d6a7a7 AA |
44 | #include "qapi/qobject-input-visitor.h" |
45 | #include "qapi/qobject-output-visitor.h" | |
6542aa9c PL |
46 | #include <nfsc/libnfs.h> |
47 | ||
94d6a7a7 | 48 | |
29c838cd | 49 | #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576 |
d99b26c4 | 50 | #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE) |
7725b8bf | 51 | #define QEMU_NFS_MAX_DEBUG_LEVEL 2 |
29c838cd | 52 | |
6542aa9c PL |
53 | typedef struct NFSClient { |
54 | struct nfs_context *context; | |
55 | struct nfsfh *fh; | |
56 | int events; | |
57 | bool has_zero_init; | |
471799d1 | 58 | AioContext *aio_context; |
37d1e4d9 | 59 | QemuMutex mutex; |
18a8056e | 60 | blkcnt_t st_blocks; |
38f8d5e0 | 61 | bool cache_used; |
94d6a7a7 AA |
62 | NFSServer *server; |
63 | char *path; | |
64 | int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug; | |
6542aa9c PL |
65 | } NFSClient; |
66 | ||
67 | typedef struct NFSRPC { | |
d746427a | 68 | BlockDriverState *bs; |
6542aa9c PL |
69 | int ret; |
70 | int complete; | |
71 | QEMUIOVector *iov; | |
72 | struct stat *st; | |
73 | Coroutine *co; | |
471799d1 | 74 | NFSClient *client; |
6542aa9c PL |
75 | } NFSRPC; |
76 | ||
94d6a7a7 AA |
77 | static int nfs_parse_uri(const char *filename, QDict *options, Error **errp) |
78 | { | |
79 | URI *uri = NULL; | |
80 | QueryParams *qp = NULL; | |
81 | int ret = -EINVAL, i; | |
82 | ||
83 | uri = uri_parse(filename); | |
84 | if (!uri) { | |
85 | error_setg(errp, "Invalid URI specified"); | |
86 | goto out; | |
87 | } | |
f69165a8 | 88 | if (g_strcmp0(uri->scheme, "nfs") != 0) { |
94d6a7a7 AA |
89 | error_setg(errp, "URI scheme must be 'nfs'"); |
90 | goto out; | |
91 | } | |
92 | ||
93 | if (!uri->server) { | |
94 | error_setg(errp, "missing hostname in URI"); | |
95 | goto out; | |
96 | } | |
97 | ||
98 | if (!uri->path) { | |
99 | error_setg(errp, "missing file path in URI"); | |
100 | goto out; | |
101 | } | |
102 | ||
103 | qp = query_params_parse(uri->query); | |
104 | if (!qp) { | |
105 | error_setg(errp, "could not parse query parameters"); | |
106 | goto out; | |
107 | } | |
108 | ||
46f5ac20 EB |
109 | qdict_put_str(options, "server.host", uri->server); |
110 | qdict_put_str(options, "server.type", "inet"); | |
111 | qdict_put_str(options, "path", uri->path); | |
94d6a7a7 AA |
112 | |
113 | for (i = 0; i < qp->n; i++) { | |
8d20abe8 | 114 | unsigned long long val; |
94d6a7a7 AA |
115 | if (!qp->p[i].value) { |
116 | error_setg(errp, "Value for NFS parameter expected: %s", | |
117 | qp->p[i].name); | |
118 | goto out; | |
119 | } | |
8d20abe8 | 120 | if (parse_uint_full(qp->p[i].value, &val, 0)) { |
94d6a7a7 AA |
121 | error_setg(errp, "Illegal value for NFS parameter: %s", |
122 | qp->p[i].name); | |
123 | goto out; | |
124 | } | |
125 | if (!strcmp(qp->p[i].name, "uid")) { | |
46f5ac20 | 126 | qdict_put_str(options, "user", qp->p[i].value); |
94d6a7a7 | 127 | } else if (!strcmp(qp->p[i].name, "gid")) { |
46f5ac20 | 128 | qdict_put_str(options, "group", qp->p[i].value); |
94d6a7a7 | 129 | } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) { |
46f5ac20 | 130 | qdict_put_str(options, "tcp-syn-count", qp->p[i].value); |
94d6a7a7 | 131 | } else if (!strcmp(qp->p[i].name, "readahead")) { |
46f5ac20 | 132 | qdict_put_str(options, "readahead-size", qp->p[i].value); |
94d6a7a7 | 133 | } else if (!strcmp(qp->p[i].name, "pagecache")) { |
46f5ac20 | 134 | qdict_put_str(options, "page-cache-size", qp->p[i].value); |
94d6a7a7 | 135 | } else if (!strcmp(qp->p[i].name, "debug")) { |
46f5ac20 | 136 | qdict_put_str(options, "debug", qp->p[i].value); |
94d6a7a7 AA |
137 | } else { |
138 | error_setg(errp, "Unknown NFS parameter name: %s", | |
139 | qp->p[i].name); | |
140 | goto out; | |
141 | } | |
142 | } | |
143 | ret = 0; | |
144 | out: | |
145 | if (qp) { | |
146 | query_params_free(qp); | |
147 | } | |
148 | if (uri) { | |
149 | uri_free(uri); | |
150 | } | |
151 | return ret; | |
152 | } | |
153 | ||
154 | static bool nfs_has_filename_options_conflict(QDict *options, Error **errp) | |
155 | { | |
156 | const QDictEntry *qe; | |
157 | ||
158 | for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) { | |
159 | if (!strcmp(qe->key, "host") || | |
160 | !strcmp(qe->key, "path") || | |
161 | !strcmp(qe->key, "user") || | |
162 | !strcmp(qe->key, "group") || | |
163 | !strcmp(qe->key, "tcp-syn-count") || | |
164 | !strcmp(qe->key, "readahead-size") || | |
165 | !strcmp(qe->key, "page-cache-size") || | |
7103d916 | 166 | !strcmp(qe->key, "debug") || |
94d6a7a7 AA |
167 | strstart(qe->key, "server.", NULL)) |
168 | { | |
169 | error_setg(errp, "Option %s cannot be used with a filename", | |
170 | qe->key); | |
171 | return true; | |
172 | } | |
173 | } | |
174 | ||
175 | return false; | |
176 | } | |
177 | ||
178 | static void nfs_parse_filename(const char *filename, QDict *options, | |
179 | Error **errp) | |
180 | { | |
181 | if (nfs_has_filename_options_conflict(options, errp)) { | |
182 | return; | |
183 | } | |
184 | ||
185 | nfs_parse_uri(filename, options, errp); | |
186 | } | |
187 | ||
6542aa9c PL |
188 | static void nfs_process_read(void *arg); |
189 | static void nfs_process_write(void *arg); | |
190 | ||
37d1e4d9 | 191 | /* Called with QemuMutex held. */ |
6542aa9c PL |
192 | static void nfs_set_events(NFSClient *client) |
193 | { | |
194 | int ev = nfs_which_events(client->context); | |
195 | if (ev != client->events) { | |
dca21ef2 FZ |
196 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
197 | false, | |
471799d1 | 198 | (ev & POLLIN) ? nfs_process_read : NULL, |
f6a51c84 SH |
199 | (ev & POLLOUT) ? nfs_process_write : NULL, |
200 | NULL, client); | |
6542aa9c PL |
201 | |
202 | } | |
203 | client->events = ev; | |
204 | } | |
205 | ||
206 | static void nfs_process_read(void *arg) | |
207 | { | |
208 | NFSClient *client = arg; | |
9d456654 | 209 | |
37d1e4d9 | 210 | qemu_mutex_lock(&client->mutex); |
6542aa9c PL |
211 | nfs_service(client->context, POLLIN); |
212 | nfs_set_events(client); | |
37d1e4d9 | 213 | qemu_mutex_unlock(&client->mutex); |
6542aa9c PL |
214 | } |
215 | ||
216 | static void nfs_process_write(void *arg) | |
217 | { | |
218 | NFSClient *client = arg; | |
9d456654 | 219 | |
37d1e4d9 | 220 | qemu_mutex_lock(&client->mutex); |
6542aa9c PL |
221 | nfs_service(client->context, POLLOUT); |
222 | nfs_set_events(client); | |
37d1e4d9 | 223 | qemu_mutex_unlock(&client->mutex); |
6542aa9c PL |
224 | } |
225 | ||
d746427a | 226 | static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task) |
6542aa9c PL |
227 | { |
228 | *task = (NFSRPC) { | |
471799d1 | 229 | .co = qemu_coroutine_self(), |
d746427a PB |
230 | .bs = bs, |
231 | .client = bs->opaque, | |
6542aa9c PL |
232 | }; |
233 | } | |
234 | ||
235 | static void nfs_co_generic_bh_cb(void *opaque) | |
236 | { | |
237 | NFSRPC *task = opaque; | |
1919631e | 238 | |
a2c0fe2f | 239 | task->complete = 1; |
1919631e | 240 | aio_co_wake(task->co); |
6542aa9c PL |
241 | } |
242 | ||
37d1e4d9 | 243 | /* Called (via nfs_service) with QemuMutex held. */ |
6542aa9c PL |
244 | static void |
245 | nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data, | |
246 | void *private_data) | |
247 | { | |
248 | NFSRPC *task = private_data; | |
6542aa9c | 249 | task->ret = ret; |
d746427a | 250 | assert(!task->st); |
6542aa9c PL |
251 | if (task->ret > 0 && task->iov) { |
252 | if (task->ret <= task->iov->size) { | |
253 | qemu_iovec_from_buf(task->iov, 0, data, task->ret); | |
254 | } else { | |
255 | task->ret = -EIO; | |
256 | } | |
257 | } | |
20fccb18 PL |
258 | if (task->ret < 0) { |
259 | error_report("NFS Error: %s", nfs_get_error(nfs)); | |
260 | } | |
d746427a PB |
261 | aio_bh_schedule_oneshot(task->client->aio_context, |
262 | nfs_co_generic_bh_cb, task); | |
6542aa9c PL |
263 | } |
264 | ||
69785a22 PL |
265 | static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, uint64_t offset, |
266 | uint64_t bytes, QEMUIOVector *iov, | |
267 | int flags) | |
6542aa9c PL |
268 | { |
269 | NFSClient *client = bs->opaque; | |
270 | NFSRPC task; | |
271 | ||
d746427a | 272 | nfs_co_init_task(bs, &task); |
6542aa9c PL |
273 | task.iov = iov; |
274 | ||
37d1e4d9 | 275 | qemu_mutex_lock(&client->mutex); |
6542aa9c | 276 | if (nfs_pread_async(client->context, client->fh, |
69785a22 | 277 | offset, bytes, nfs_co_generic_cb, &task) != 0) { |
37d1e4d9 | 278 | qemu_mutex_unlock(&client->mutex); |
6542aa9c PL |
279 | return -ENOMEM; |
280 | } | |
281 | ||
aa92d6c4 | 282 | nfs_set_events(client); |
37d1e4d9 | 283 | qemu_mutex_unlock(&client->mutex); |
6542aa9c | 284 | while (!task.complete) { |
6542aa9c PL |
285 | qemu_coroutine_yield(); |
286 | } | |
287 | ||
288 | if (task.ret < 0) { | |
289 | return task.ret; | |
290 | } | |
291 | ||
292 | /* zero pad short reads */ | |
293 | if (task.ret < iov->size) { | |
294 | qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret); | |
295 | } | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
69785a22 PL |
300 | static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, uint64_t offset, |
301 | uint64_t bytes, QEMUIOVector *iov, | |
302 | int flags) | |
6542aa9c PL |
303 | { |
304 | NFSClient *client = bs->opaque; | |
305 | NFSRPC task; | |
306 | char *buf = NULL; | |
ef503a84 | 307 | bool my_buffer = false; |
6542aa9c | 308 | |
d746427a | 309 | nfs_co_init_task(bs, &task); |
6542aa9c | 310 | |
ef503a84 PL |
311 | if (iov->niov != 1) { |
312 | buf = g_try_malloc(bytes); | |
313 | if (bytes && buf == NULL) { | |
314 | return -ENOMEM; | |
315 | } | |
316 | qemu_iovec_to_buf(iov, 0, buf, bytes); | |
317 | my_buffer = true; | |
318 | } else { | |
319 | buf = iov->iov[0].iov_base; | |
2347dd7b KW |
320 | } |
321 | ||
37d1e4d9 | 322 | qemu_mutex_lock(&client->mutex); |
6542aa9c | 323 | if (nfs_pwrite_async(client->context, client->fh, |
69785a22 PL |
324 | offset, bytes, buf, |
325 | nfs_co_generic_cb, &task) != 0) { | |
37d1e4d9 | 326 | qemu_mutex_unlock(&client->mutex); |
ef503a84 PL |
327 | if (my_buffer) { |
328 | g_free(buf); | |
329 | } | |
6542aa9c PL |
330 | return -ENOMEM; |
331 | } | |
332 | ||
aa92d6c4 | 333 | nfs_set_events(client); |
37d1e4d9 | 334 | qemu_mutex_unlock(&client->mutex); |
6542aa9c | 335 | while (!task.complete) { |
6542aa9c PL |
336 | qemu_coroutine_yield(); |
337 | } | |
338 | ||
ef503a84 PL |
339 | if (my_buffer) { |
340 | g_free(buf); | |
341 | } | |
6542aa9c | 342 | |
69785a22 | 343 | if (task.ret != bytes) { |
6542aa9c PL |
344 | return task.ret < 0 ? task.ret : -EIO; |
345 | } | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | static int coroutine_fn nfs_co_flush(BlockDriverState *bs) | |
351 | { | |
352 | NFSClient *client = bs->opaque; | |
353 | NFSRPC task; | |
354 | ||
d746427a | 355 | nfs_co_init_task(bs, &task); |
6542aa9c | 356 | |
37d1e4d9 | 357 | qemu_mutex_lock(&client->mutex); |
6542aa9c PL |
358 | if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb, |
359 | &task) != 0) { | |
37d1e4d9 | 360 | qemu_mutex_unlock(&client->mutex); |
6542aa9c PL |
361 | return -ENOMEM; |
362 | } | |
363 | ||
aa92d6c4 | 364 | nfs_set_events(client); |
37d1e4d9 | 365 | qemu_mutex_unlock(&client->mutex); |
6542aa9c | 366 | while (!task.complete) { |
6542aa9c PL |
367 | qemu_coroutine_yield(); |
368 | } | |
369 | ||
370 | return task.ret; | |
371 | } | |
372 | ||
471799d1 SH |
373 | static void nfs_detach_aio_context(BlockDriverState *bs) |
374 | { | |
375 | NFSClient *client = bs->opaque; | |
376 | ||
dca21ef2 | 377 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
f6a51c84 | 378 | false, NULL, NULL, NULL, NULL); |
471799d1 SH |
379 | client->events = 0; |
380 | } | |
381 | ||
382 | static void nfs_attach_aio_context(BlockDriverState *bs, | |
383 | AioContext *new_context) | |
384 | { | |
385 | NFSClient *client = bs->opaque; | |
386 | ||
387 | client->aio_context = new_context; | |
388 | nfs_set_events(client); | |
389 | } | |
390 | ||
6542aa9c PL |
391 | static void nfs_client_close(NFSClient *client) |
392 | { | |
393 | if (client->context) { | |
394 | if (client->fh) { | |
395 | nfs_close(client->context, client->fh); | |
113fe792 | 396 | client->fh = NULL; |
6542aa9c | 397 | } |
dca21ef2 | 398 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
f6a51c84 | 399 | false, NULL, NULL, NULL, NULL); |
6542aa9c | 400 | nfs_destroy_context(client->context); |
113fe792 | 401 | client->context = NULL; |
6542aa9c | 402 | } |
113fe792 JC |
403 | g_free(client->path); |
404 | qemu_mutex_destroy(&client->mutex); | |
405 | qapi_free_NFSServer(client->server); | |
406 | client->server = NULL; | |
6542aa9c PL |
407 | } |
408 | ||
409 | static void nfs_file_close(BlockDriverState *bs) | |
410 | { | |
411 | NFSClient *client = bs->opaque; | |
412 | nfs_client_close(client); | |
413 | } | |
414 | ||
c22a0345 | 415 | static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts, |
cb8d4bf6 | 416 | int flags, int open_flags, Error **errp) |
6542aa9c | 417 | { |
f1a7ff77 | 418 | int64_t ret = -EINVAL; |
6542aa9c | 419 | struct stat st; |
6542aa9c PL |
420 | char *file = NULL, *strp = NULL; |
421 | ||
113fe792 | 422 | qemu_mutex_init(&client->mutex); |
94d6a7a7 | 423 | |
c22a0345 | 424 | client->path = g_strdup(opts->path); |
94d6a7a7 AA |
425 | |
426 | strp = strrchr(client->path, '/'); | |
6542aa9c PL |
427 | if (strp == NULL) { |
428 | error_setg(errp, "Invalid URL specified"); | |
429 | goto fail; | |
430 | } | |
431 | file = g_strdup(strp); | |
432 | *strp = 0; | |
433 | ||
c22a0345 KW |
434 | /* Steal the NFSServer object from opts; set the original pointer to NULL |
435 | * to avoid use after free and double free. */ | |
436 | client->server = opts->server; | |
437 | opts->server = NULL; | |
94d6a7a7 | 438 | |
6542aa9c PL |
439 | client->context = nfs_init_context(); |
440 | if (client->context == NULL) { | |
441 | error_setg(errp, "Failed to init NFS context"); | |
442 | goto fail; | |
443 | } | |
444 | ||
c22a0345 KW |
445 | if (opts->has_user) { |
446 | client->uid = opts->user; | |
94d6a7a7 AA |
447 | nfs_set_uid(client->context, client->uid); |
448 | } | |
449 | ||
c22a0345 KW |
450 | if (opts->has_group) { |
451 | client->gid = opts->group; | |
94d6a7a7 AA |
452 | nfs_set_gid(client->context, client->gid); |
453 | } | |
454 | ||
c22a0345 KW |
455 | if (opts->has_tcp_syn_count) { |
456 | client->tcp_syncnt = opts->tcp_syn_count; | |
94d6a7a7 AA |
457 | nfs_set_tcp_syncnt(client->context, client->tcp_syncnt); |
458 | } | |
459 | ||
460 | #ifdef LIBNFS_FEATURE_READAHEAD | |
c22a0345 | 461 | if (opts->has_readahead_size) { |
94d6a7a7 AA |
462 | if (open_flags & BDRV_O_NOCACHE) { |
463 | error_setg(errp, "Cannot enable NFS readahead " | |
464 | "if cache.direct = on"); | |
6542aa9c PL |
465 | goto fail; |
466 | } | |
c22a0345 | 467 | client->readahead = opts->readahead_size; |
94d6a7a7 | 468 | if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) { |
3dc6f869 AF |
469 | warn_report("Truncating NFS readahead size to %d", |
470 | QEMU_NFS_MAX_READAHEAD_SIZE); | |
94d6a7a7 | 471 | client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE; |
7c24384b | 472 | } |
94d6a7a7 | 473 | nfs_set_readahead(client->context, client->readahead); |
d99b26c4 | 474 | #ifdef LIBNFS_FEATURE_PAGECACHE |
94d6a7a7 | 475 | nfs_set_pagecache_ttl(client->context, 0); |
d99b26c4 | 476 | #endif |
94d6a7a7 AA |
477 | client->cache_used = true; |
478 | } | |
d99b26c4 | 479 | #endif |
94d6a7a7 | 480 | |
d99b26c4 | 481 | #ifdef LIBNFS_FEATURE_PAGECACHE |
c22a0345 | 482 | if (opts->has_page_cache_size) { |
94d6a7a7 AA |
483 | if (open_flags & BDRV_O_NOCACHE) { |
484 | error_setg(errp, "Cannot enable NFS pagecache " | |
485 | "if cache.direct = on"); | |
486 | goto fail; | |
487 | } | |
c22a0345 | 488 | client->pagecache = opts->page_cache_size; |
94d6a7a7 | 489 | if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) { |
3dc6f869 AF |
490 | warn_report("Truncating NFS pagecache size to %d pages", |
491 | QEMU_NFS_MAX_PAGECACHE_SIZE); | |
94d6a7a7 AA |
492 | client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE; |
493 | } | |
494 | nfs_set_pagecache(client->context, client->pagecache); | |
495 | nfs_set_pagecache_ttl(client->context, 0); | |
496 | client->cache_used = true; | |
497 | } | |
7725b8bf | 498 | #endif |
94d6a7a7 | 499 | |
7725b8bf | 500 | #ifdef LIBNFS_FEATURE_DEBUG |
c22a0345 KW |
501 | if (opts->has_debug) { |
502 | client->debug = opts->debug; | |
94d6a7a7 AA |
503 | /* limit the maximum debug level to avoid potential flooding |
504 | * of our log files. */ | |
505 | if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) { | |
3dc6f869 AF |
506 | warn_report("Limiting NFS debug level to %d", |
507 | QEMU_NFS_MAX_DEBUG_LEVEL); | |
94d6a7a7 | 508 | client->debug = QEMU_NFS_MAX_DEBUG_LEVEL; |
6542aa9c | 509 | } |
94d6a7a7 | 510 | nfs_set_debug(client->context, client->debug); |
6542aa9c | 511 | } |
94d6a7a7 | 512 | #endif |
6542aa9c | 513 | |
94d6a7a7 | 514 | ret = nfs_mount(client->context, client->server->host, client->path); |
6542aa9c PL |
515 | if (ret < 0) { |
516 | error_setg(errp, "Failed to mount nfs share: %s", | |
517 | nfs_get_error(client->context)); | |
518 | goto fail; | |
519 | } | |
520 | ||
521 | if (flags & O_CREAT) { | |
522 | ret = nfs_creat(client->context, file, 0600, &client->fh); | |
523 | if (ret < 0) { | |
524 | error_setg(errp, "Failed to create file: %s", | |
525 | nfs_get_error(client->context)); | |
526 | goto fail; | |
527 | } | |
528 | } else { | |
529 | ret = nfs_open(client->context, file, flags, &client->fh); | |
530 | if (ret < 0) { | |
531 | error_setg(errp, "Failed to open file : %s", | |
532 | nfs_get_error(client->context)); | |
533 | goto fail; | |
534 | } | |
535 | } | |
536 | ||
537 | ret = nfs_fstat(client->context, client->fh, &st); | |
538 | if (ret < 0) { | |
539 | error_setg(errp, "Failed to fstat file: %s", | |
540 | nfs_get_error(client->context)); | |
541 | goto fail; | |
542 | } | |
543 | ||
544 | ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE); | |
18a8056e | 545 | client->st_blocks = st.st_blocks; |
6542aa9c | 546 | client->has_zero_init = S_ISREG(st.st_mode); |
94d6a7a7 | 547 | *strp = '/'; |
6542aa9c | 548 | goto out; |
94d6a7a7 | 549 | |
6542aa9c PL |
550 | fail: |
551 | nfs_client_close(client); | |
552 | out: | |
6542aa9c PL |
553 | g_free(file); |
554 | return ret; | |
555 | } | |
556 | ||
a1a42af4 KW |
557 | static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options, |
558 | Error **errp) | |
c22a0345 KW |
559 | { |
560 | BlockdevOptionsNfs *opts = NULL; | |
c22a0345 | 561 | Visitor *v; |
c82be42c | 562 | const QDictEntry *e; |
c22a0345 | 563 | Error *local_err = NULL; |
c22a0345 | 564 | |
af91062e MA |
565 | v = qobject_input_visitor_new_flat_confused(options, errp); |
566 | if (!v) { | |
a1a42af4 | 567 | return NULL; |
c22a0345 KW |
568 | } |
569 | ||
c22a0345 KW |
570 | visit_type_BlockdevOptionsNfs(v, NULL, &opts, &local_err); |
571 | visit_free(v); | |
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 | ||
061ca8a3 KW |
748 | static int coroutine_fn |
749 | nfs_file_co_truncate(BlockDriverState *bs, int64_t offset, | |
750 | PreallocMode prealloc, Error **errp) | |
6542aa9c PL |
751 | { |
752 | NFSClient *client = bs->opaque; | |
f59adb32 HR |
753 | int ret; |
754 | ||
8243ccb7 HR |
755 | if (prealloc != PREALLOC_MODE_OFF) { |
756 | error_setg(errp, "Unsupported preallocation mode '%s'", | |
977c736f | 757 | PreallocMode_str(prealloc)); |
8243ccb7 HR |
758 | return -ENOTSUP; |
759 | } | |
760 | ||
f59adb32 HR |
761 | ret = nfs_ftruncate(client->context, client->fh, offset); |
762 | if (ret < 0) { | |
763 | error_setg_errno(errp, -ret, "Failed to truncate file"); | |
764 | return ret; | |
765 | } | |
766 | ||
767 | return 0; | |
6542aa9c PL |
768 | } |
769 | ||
18a8056e PL |
770 | /* Note that this will not re-establish a connection with the NFS server |
771 | * - it is effectively a NOP. */ | |
772 | static int nfs_reopen_prepare(BDRVReopenState *state, | |
773 | BlockReopenQueue *queue, Error **errp) | |
774 | { | |
775 | NFSClient *client = state->bs->opaque; | |
776 | struct stat st; | |
777 | int ret = 0; | |
778 | ||
779 | if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) { | |
780 | error_setg(errp, "Cannot open a read-only mount as read-write"); | |
781 | return -EACCES; | |
782 | } | |
783 | ||
38f8d5e0 | 784 | if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) { |
d99b26c4 PL |
785 | error_setg(errp, "Cannot disable cache if libnfs readahead or" |
786 | " pagecache is enabled"); | |
38f8d5e0 PL |
787 | return -EINVAL; |
788 | } | |
789 | ||
18a8056e PL |
790 | /* Update cache for read-only reopens */ |
791 | if (!(state->flags & BDRV_O_RDWR)) { | |
792 | ret = nfs_fstat(client->context, client->fh, &st); | |
793 | if (ret < 0) { | |
794 | error_setg(errp, "Failed to fstat file: %s", | |
795 | nfs_get_error(client->context)); | |
796 | return ret; | |
797 | } | |
798 | client->st_blocks = st.st_blocks; | |
799 | } | |
800 | ||
801 | return 0; | |
802 | } | |
803 | ||
998b3a1e | 804 | static void nfs_refresh_filename(BlockDriverState *bs) |
94d6a7a7 AA |
805 | { |
806 | NFSClient *client = bs->opaque; | |
94d6a7a7 AA |
807 | |
808 | if (client->uid && !client->gid) { | |
809 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
810 | "nfs://%s%s?uid=%" PRId64, client->server->host, client->path, | |
811 | client->uid); | |
812 | } else if (!client->uid && client->gid) { | |
813 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
814 | "nfs://%s%s?gid=%" PRId64, client->server->host, client->path, | |
815 | client->gid); | |
816 | } else if (client->uid && client->gid) { | |
817 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
818 | "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64, | |
819 | client->server->host, client->path, client->uid, client->gid); | |
820 | } else { | |
821 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
822 | "nfs://%s%s", client->server->host, client->path); | |
823 | } | |
94d6a7a7 AA |
824 | } |
825 | ||
0dcbc54a HR |
826 | static char *nfs_dirname(BlockDriverState *bs, Error **errp) |
827 | { | |
828 | NFSClient *client = bs->opaque; | |
829 | ||
830 | if (client->uid || client->gid) { | |
831 | bdrv_refresh_filename(bs); | |
832 | error_setg(errp, "Cannot generate a base directory for NFS node '%s'", | |
833 | bs->filename); | |
834 | return NULL; | |
835 | } | |
836 | ||
837 | return g_strdup_printf("nfs://%s%s/", client->server->host, client->path); | |
838 | } | |
839 | ||
d99b26c4 | 840 | #ifdef LIBNFS_FEATURE_PAGECACHE |
2b148f39 PB |
841 | static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs, |
842 | Error **errp) | |
d99b26c4 PL |
843 | { |
844 | NFSClient *client = bs->opaque; | |
845 | nfs_pagecache_invalidate(client->context, client->fh); | |
846 | } | |
847 | #endif | |
848 | ||
2654267c HR |
849 | static const char *nfs_strong_runtime_opts[] = { |
850 | "path", | |
851 | "user", | |
852 | "group", | |
853 | "server.", | |
854 | ||
855 | NULL | |
856 | }; | |
857 | ||
6542aa9c | 858 | static BlockDriver bdrv_nfs = { |
471799d1 SH |
859 | .format_name = "nfs", |
860 | .protocol_name = "nfs", | |
861 | ||
862 | .instance_size = sizeof(NFSClient), | |
94d6a7a7 | 863 | .bdrv_parse_filename = nfs_parse_filename, |
fd752801 HR |
864 | .create_opts = &nfs_create_opts, |
865 | ||
471799d1 SH |
866 | .bdrv_has_zero_init = nfs_has_zero_init, |
867 | .bdrv_get_allocated_file_size = nfs_get_allocated_file_size, | |
061ca8a3 | 868 | .bdrv_co_truncate = nfs_file_co_truncate, |
471799d1 SH |
869 | |
870 | .bdrv_file_open = nfs_file_open, | |
871 | .bdrv_close = nfs_file_close, | |
a1a42af4 | 872 | .bdrv_co_create = nfs_file_co_create, |
efc75e2a | 873 | .bdrv_co_create_opts = nfs_file_co_create_opts, |
18a8056e | 874 | .bdrv_reopen_prepare = nfs_reopen_prepare, |
471799d1 | 875 | |
69785a22 PL |
876 | .bdrv_co_preadv = nfs_co_preadv, |
877 | .bdrv_co_pwritev = nfs_co_pwritev, | |
471799d1 SH |
878 | .bdrv_co_flush_to_disk = nfs_co_flush, |
879 | ||
880 | .bdrv_detach_aio_context = nfs_detach_aio_context, | |
881 | .bdrv_attach_aio_context = nfs_attach_aio_context, | |
94d6a7a7 | 882 | .bdrv_refresh_filename = nfs_refresh_filename, |
0dcbc54a | 883 | .bdrv_dirname = nfs_dirname, |
d99b26c4 | 884 | |
2654267c HR |
885 | .strong_runtime_opts = nfs_strong_runtime_opts, |
886 | ||
d99b26c4 | 887 | #ifdef LIBNFS_FEATURE_PAGECACHE |
2b148f39 | 888 | .bdrv_co_invalidate_cache = nfs_co_invalidate_cache, |
d99b26c4 | 889 | #endif |
6542aa9c PL |
890 | }; |
891 | ||
892 | static void nfs_block_init(void) | |
893 | { | |
894 | bdrv_register(&bdrv_nfs); | |
895 | } | |
896 | ||
897 | block_init(nfs_block_init); |