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