]> git.proxmox.com Git - mirror_qemu.git/blame - block/nfs.c
block/iscsi: bump year in copyright notice
[mirror_qemu.git] / block / nfs.c
CommitLineData
6542aa9c
PL
1/*
2 * QEMU Block driver for native access to files on NFS shares
3 *
4 * Copyright (c) 2014 Peter Lieven <pl@kamp.de>
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
25#include "config-host.h"
26
27#include <poll.h>
28#include "qemu-common.h"
29#include "qemu/config-file.h"
30#include "qemu/error-report.h"
31#include "block/block_int.h"
32#include "trace.h"
33#include "qemu/iov.h"
34#include "qemu/uri.h"
35#include "sysemu/sysemu.h"
36#include <nfsc/libnfs.h>
37
38typedef struct NFSClient {
39 struct nfs_context *context;
40 struct nfsfh *fh;
41 int events;
42 bool has_zero_init;
43} NFSClient;
44
45typedef struct NFSRPC {
46 int ret;
47 int complete;
48 QEMUIOVector *iov;
49 struct stat *st;
50 Coroutine *co;
51 QEMUBH *bh;
52} NFSRPC;
53
54static void nfs_process_read(void *arg);
55static void nfs_process_write(void *arg);
56
57static void nfs_set_events(NFSClient *client)
58{
59 int ev = nfs_which_events(client->context);
60 if (ev != client->events) {
61 qemu_aio_set_fd_handler(nfs_get_fd(client->context),
62 (ev & POLLIN) ? nfs_process_read : NULL,
63 (ev & POLLOUT) ? nfs_process_write : NULL,
64 client);
65
66 }
67 client->events = ev;
68}
69
70static void nfs_process_read(void *arg)
71{
72 NFSClient *client = arg;
73 nfs_service(client->context, POLLIN);
74 nfs_set_events(client);
75}
76
77static void nfs_process_write(void *arg)
78{
79 NFSClient *client = arg;
80 nfs_service(client->context, POLLOUT);
81 nfs_set_events(client);
82}
83
84static void nfs_co_init_task(NFSClient *client, NFSRPC *task)
85{
86 *task = (NFSRPC) {
87 .co = qemu_coroutine_self(),
88 };
89}
90
91static void nfs_co_generic_bh_cb(void *opaque)
92{
93 NFSRPC *task = opaque;
94 qemu_bh_delete(task->bh);
95 qemu_coroutine_enter(task->co, NULL);
96}
97
98static void
99nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
100 void *private_data)
101{
102 NFSRPC *task = private_data;
103 task->complete = 1;
104 task->ret = ret;
105 if (task->ret > 0 && task->iov) {
106 if (task->ret <= task->iov->size) {
107 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
108 } else {
109 task->ret = -EIO;
110 }
111 }
112 if (task->ret == 0 && task->st) {
113 memcpy(task->st, data, sizeof(struct stat));
114 }
20fccb18
PL
115 if (task->ret < 0) {
116 error_report("NFS Error: %s", nfs_get_error(nfs));
117 }
6542aa9c
PL
118 if (task->co) {
119 task->bh = qemu_bh_new(nfs_co_generic_bh_cb, task);
120 qemu_bh_schedule(task->bh);
121 }
122}
123
124static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
125 int64_t sector_num, int nb_sectors,
126 QEMUIOVector *iov)
127{
128 NFSClient *client = bs->opaque;
129 NFSRPC task;
130
131 nfs_co_init_task(client, &task);
132 task.iov = iov;
133
134 if (nfs_pread_async(client->context, client->fh,
135 sector_num * BDRV_SECTOR_SIZE,
136 nb_sectors * BDRV_SECTOR_SIZE,
137 nfs_co_generic_cb, &task) != 0) {
138 return -ENOMEM;
139 }
140
141 while (!task.complete) {
142 nfs_set_events(client);
143 qemu_coroutine_yield();
144 }
145
146 if (task.ret < 0) {
147 return task.ret;
148 }
149
150 /* zero pad short reads */
151 if (task.ret < iov->size) {
152 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
153 }
154
155 return 0;
156}
157
158static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
159 int64_t sector_num, int nb_sectors,
160 QEMUIOVector *iov)
161{
162 NFSClient *client = bs->opaque;
163 NFSRPC task;
164 char *buf = NULL;
165
166 nfs_co_init_task(client, &task);
167
168 buf = g_malloc(nb_sectors * BDRV_SECTOR_SIZE);
169 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
170
171 if (nfs_pwrite_async(client->context, client->fh,
172 sector_num * BDRV_SECTOR_SIZE,
173 nb_sectors * BDRV_SECTOR_SIZE,
174 buf, nfs_co_generic_cb, &task) != 0) {
175 g_free(buf);
176 return -ENOMEM;
177 }
178
179 while (!task.complete) {
180 nfs_set_events(client);
181 qemu_coroutine_yield();
182 }
183
184 g_free(buf);
185
186 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
187 return task.ret < 0 ? task.ret : -EIO;
188 }
189
190 return 0;
191}
192
193static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
194{
195 NFSClient *client = bs->opaque;
196 NFSRPC task;
197
198 nfs_co_init_task(client, &task);
199
200 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
201 &task) != 0) {
202 return -ENOMEM;
203 }
204
205 while (!task.complete) {
206 nfs_set_events(client);
207 qemu_coroutine_yield();
208 }
209
210 return task.ret;
211}
212
213/* TODO Convert to fine grained options */
214static QemuOptsList runtime_opts = {
215 .name = "nfs",
216 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
217 .desc = {
218 {
219 .name = "filename",
220 .type = QEMU_OPT_STRING,
221 .help = "URL to the NFS file",
222 },
223 { /* end of list */ }
224 },
225};
226
227static void nfs_client_close(NFSClient *client)
228{
229 if (client->context) {
230 if (client->fh) {
231 nfs_close(client->context, client->fh);
232 }
233 qemu_aio_set_fd_handler(nfs_get_fd(client->context), NULL, NULL, NULL);
234 nfs_destroy_context(client->context);
235 }
236 memset(client, 0, sizeof(NFSClient));
237}
238
239static void nfs_file_close(BlockDriverState *bs)
240{
241 NFSClient *client = bs->opaque;
242 nfs_client_close(client);
243}
244
245static int64_t nfs_client_open(NFSClient *client, const char *filename,
246 int flags, Error **errp)
247{
248 int ret = -EINVAL, i;
249 struct stat st;
250 URI *uri;
251 QueryParams *qp = NULL;
252 char *file = NULL, *strp = NULL;
253
254 uri = uri_parse(filename);
255 if (!uri) {
256 error_setg(errp, "Invalid URL specified");
5f4d5e1a
HR
257 goto fail;
258 }
259 if (!uri->server) {
260 error_setg(errp, "Invalid URL specified");
6542aa9c
PL
261 goto fail;
262 }
263 strp = strrchr(uri->path, '/');
264 if (strp == NULL) {
265 error_setg(errp, "Invalid URL specified");
266 goto fail;
267 }
268 file = g_strdup(strp);
269 *strp = 0;
270
271 client->context = nfs_init_context();
272 if (client->context == NULL) {
273 error_setg(errp, "Failed to init NFS context");
274 goto fail;
275 }
276
277 qp = query_params_parse(uri->query);
278 for (i = 0; i < qp->n; i++) {
279 if (!qp->p[i].value) {
280 error_setg(errp, "Value for NFS parameter expected: %s",
281 qp->p[i].name);
282 goto fail;
283 }
284 if (!strncmp(qp->p[i].name, "uid", 3)) {
285 nfs_set_uid(client->context, atoi(qp->p[i].value));
286 } else if (!strncmp(qp->p[i].name, "gid", 3)) {
287 nfs_set_gid(client->context, atoi(qp->p[i].value));
288 } else if (!strncmp(qp->p[i].name, "tcp-syncnt", 10)) {
289 nfs_set_tcp_syncnt(client->context, atoi(qp->p[i].value));
290 } else {
291 error_setg(errp, "Unknown NFS parameter name: %s",
292 qp->p[i].name);
293 goto fail;
294 }
295 }
296
297 ret = nfs_mount(client->context, uri->server, uri->path);
298 if (ret < 0) {
299 error_setg(errp, "Failed to mount nfs share: %s",
300 nfs_get_error(client->context));
301 goto fail;
302 }
303
304 if (flags & O_CREAT) {
305 ret = nfs_creat(client->context, file, 0600, &client->fh);
306 if (ret < 0) {
307 error_setg(errp, "Failed to create file: %s",
308 nfs_get_error(client->context));
309 goto fail;
310 }
311 } else {
312 ret = nfs_open(client->context, file, flags, &client->fh);
313 if (ret < 0) {
314 error_setg(errp, "Failed to open file : %s",
315 nfs_get_error(client->context));
316 goto fail;
317 }
318 }
319
320 ret = nfs_fstat(client->context, client->fh, &st);
321 if (ret < 0) {
322 error_setg(errp, "Failed to fstat file: %s",
323 nfs_get_error(client->context));
324 goto fail;
325 }
326
327 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
328 client->has_zero_init = S_ISREG(st.st_mode);
329 goto out;
330fail:
331 nfs_client_close(client);
332out:
333 if (qp) {
334 query_params_free(qp);
335 }
336 uri_free(uri);
337 g_free(file);
338 return ret;
339}
340
341static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
342 Error **errp) {
343 NFSClient *client = bs->opaque;
344 int64_t ret;
345 QemuOpts *opts;
346 Error *local_err = NULL;
347
348 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
349 qemu_opts_absorb_qdict(opts, options, &local_err);
0fb6395c 350 if (local_err) {
6542aa9c
PL
351 error_propagate(errp, local_err);
352 return -EINVAL;
353 }
354 ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
355 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
356 errp);
357 if (ret < 0) {
358 return ret;
359 }
360 bs->total_sectors = ret;
361 return 0;
362}
363
364static int nfs_file_create(const char *url, QEMUOptionParameter *options,
365 Error **errp)
366{
367 int ret = 0;
368 int64_t total_size = 0;
369 NFSClient *client = g_malloc0(sizeof(NFSClient));
370
371 /* Read out options */
372 while (options && options->name) {
373 if (!strcmp(options->name, "size")) {
374 total_size = options->value.n;
375 }
376 options++;
377 }
378
379 ret = nfs_client_open(client, url, O_CREAT, errp);
380 if (ret < 0) {
381 goto out;
382 }
383 ret = nfs_ftruncate(client->context, client->fh, total_size);
384 nfs_client_close(client);
385out:
386 g_free(client);
387 return ret;
388}
389
390static int nfs_has_zero_init(BlockDriverState *bs)
391{
392 NFSClient *client = bs->opaque;
393 return client->has_zero_init;
394}
395
396static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
397{
398 NFSClient *client = bs->opaque;
399 NFSRPC task = {0};
400 struct stat st;
401
402 task.st = &st;
403 if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
404 &task) != 0) {
405 return -ENOMEM;
406 }
407
408 while (!task.complete) {
409 nfs_set_events(client);
410 qemu_aio_wait();
411 }
412
413 return (task.ret < 0 ? task.ret : st.st_blocks * st.st_blksize);
414}
415
416static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
417{
418 NFSClient *client = bs->opaque;
419 return nfs_ftruncate(client->context, client->fh, offset);
420}
421
422static BlockDriver bdrv_nfs = {
423 .format_name = "nfs",
424 .protocol_name = "nfs",
425
426 .instance_size = sizeof(NFSClient),
427 .bdrv_needs_filename = true,
428 .bdrv_has_zero_init = nfs_has_zero_init,
429 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
430 .bdrv_truncate = nfs_file_truncate,
431
432 .bdrv_file_open = nfs_file_open,
433 .bdrv_close = nfs_file_close,
434 .bdrv_create = nfs_file_create,
435
436 .bdrv_co_readv = nfs_co_readv,
437 .bdrv_co_writev = nfs_co_writev,
438 .bdrv_co_flush_to_disk = nfs_co_flush,
439};
440
441static void nfs_block_init(void)
442{
443 bdrv_register(&bdrv_nfs);
444}
445
446block_init(nfs_block_init);