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