xref: /qemu/block/nfs.c (revision 814bb12a)
1 /*
2  * QEMU Block driver for native access to files on NFS shares
3  *
4  * Copyright (c) 2014-2016 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 "qemu/osdep.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 "qapi/error.h"
32 #include "block/block_int.h"
33 #include "trace.h"
34 #include "qemu/iov.h"
35 #include "qemu/uri.h"
36 #include "qemu/cutils.h"
37 #include "sysemu/sysemu.h"
38 #include <nfsc/libnfs.h>
39 
40 #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
41 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
42 #define QEMU_NFS_MAX_DEBUG_LEVEL 2
43 
44 typedef struct NFSClient {
45     struct nfs_context *context;
46     struct nfsfh *fh;
47     int events;
48     bool has_zero_init;
49     AioContext *aio_context;
50     blkcnt_t st_blocks;
51     bool cache_used;
52 } NFSClient;
53 
54 typedef struct NFSRPC {
55     int ret;
56     int complete;
57     QEMUIOVector *iov;
58     struct stat *st;
59     Coroutine *co;
60     NFSClient *client;
61 } NFSRPC;
62 
63 static void nfs_process_read(void *arg);
64 static void nfs_process_write(void *arg);
65 
66 static void nfs_set_events(NFSClient *client)
67 {
68     int ev = nfs_which_events(client->context);
69     if (ev != client->events) {
70         aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
71                            false,
72                            (ev & POLLIN) ? nfs_process_read : NULL,
73                            (ev & POLLOUT) ? nfs_process_write : NULL, client);
74 
75     }
76     client->events = ev;
77 }
78 
79 static void nfs_process_read(void *arg)
80 {
81     NFSClient *client = arg;
82     nfs_service(client->context, POLLIN);
83     nfs_set_events(client);
84 }
85 
86 static void nfs_process_write(void *arg)
87 {
88     NFSClient *client = arg;
89     nfs_service(client->context, POLLOUT);
90     nfs_set_events(client);
91 }
92 
93 static void nfs_co_init_task(NFSClient *client, NFSRPC *task)
94 {
95     *task = (NFSRPC) {
96         .co             = qemu_coroutine_self(),
97         .client         = client,
98     };
99 }
100 
101 static void nfs_co_generic_bh_cb(void *opaque)
102 {
103     NFSRPC *task = opaque;
104     task->complete = 1;
105     qemu_coroutine_enter(task->co);
106 }
107 
108 static void
109 nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
110                   void *private_data)
111 {
112     NFSRPC *task = private_data;
113     task->ret = ret;
114     if (task->ret > 0 && task->iov) {
115         if (task->ret <= task->iov->size) {
116             qemu_iovec_from_buf(task->iov, 0, data, task->ret);
117         } else {
118             task->ret = -EIO;
119         }
120     }
121     if (task->ret == 0 && task->st) {
122         memcpy(task->st, data, sizeof(struct stat));
123     }
124     if (task->ret < 0) {
125         error_report("NFS Error: %s", nfs_get_error(nfs));
126     }
127     if (task->co) {
128         aio_bh_schedule_oneshot(task->client->aio_context,
129                                 nfs_co_generic_bh_cb, task);
130     } else {
131         task->complete = 1;
132     }
133 }
134 
135 static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
136                                      int64_t sector_num, int nb_sectors,
137                                      QEMUIOVector *iov)
138 {
139     NFSClient *client = bs->opaque;
140     NFSRPC task;
141 
142     nfs_co_init_task(client, &task);
143     task.iov = iov;
144 
145     if (nfs_pread_async(client->context, client->fh,
146                         sector_num * BDRV_SECTOR_SIZE,
147                         nb_sectors * BDRV_SECTOR_SIZE,
148                         nfs_co_generic_cb, &task) != 0) {
149         return -ENOMEM;
150     }
151 
152     while (!task.complete) {
153         nfs_set_events(client);
154         qemu_coroutine_yield();
155     }
156 
157     if (task.ret < 0) {
158         return task.ret;
159     }
160 
161     /* zero pad short reads */
162     if (task.ret < iov->size) {
163         qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
164     }
165 
166     return 0;
167 }
168 
169 static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
170                                         int64_t sector_num, int nb_sectors,
171                                         QEMUIOVector *iov)
172 {
173     NFSClient *client = bs->opaque;
174     NFSRPC task;
175     char *buf = NULL;
176 
177     nfs_co_init_task(client, &task);
178 
179     buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
180     if (nb_sectors && buf == NULL) {
181         return -ENOMEM;
182     }
183 
184     qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
185 
186     if (nfs_pwrite_async(client->context, client->fh,
187                          sector_num * BDRV_SECTOR_SIZE,
188                          nb_sectors * BDRV_SECTOR_SIZE,
189                          buf, nfs_co_generic_cb, &task) != 0) {
190         g_free(buf);
191         return -ENOMEM;
192     }
193 
194     while (!task.complete) {
195         nfs_set_events(client);
196         qemu_coroutine_yield();
197     }
198 
199     g_free(buf);
200 
201     if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
202         return task.ret < 0 ? task.ret : -EIO;
203     }
204 
205     return 0;
206 }
207 
208 static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
209 {
210     NFSClient *client = bs->opaque;
211     NFSRPC task;
212 
213     nfs_co_init_task(client, &task);
214 
215     if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
216                         &task) != 0) {
217         return -ENOMEM;
218     }
219 
220     while (!task.complete) {
221         nfs_set_events(client);
222         qemu_coroutine_yield();
223     }
224 
225     return task.ret;
226 }
227 
228 /* TODO Convert to fine grained options */
229 static QemuOptsList runtime_opts = {
230     .name = "nfs",
231     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
232     .desc = {
233         {
234             .name = "filename",
235             .type = QEMU_OPT_STRING,
236             .help = "URL to the NFS file",
237         },
238         { /* end of list */ }
239     },
240 };
241 
242 static void nfs_detach_aio_context(BlockDriverState *bs)
243 {
244     NFSClient *client = bs->opaque;
245 
246     aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
247                        false, NULL, NULL, NULL);
248     client->events = 0;
249 }
250 
251 static void nfs_attach_aio_context(BlockDriverState *bs,
252                                    AioContext *new_context)
253 {
254     NFSClient *client = bs->opaque;
255 
256     client->aio_context = new_context;
257     nfs_set_events(client);
258 }
259 
260 static void nfs_client_close(NFSClient *client)
261 {
262     if (client->context) {
263         if (client->fh) {
264             nfs_close(client->context, client->fh);
265         }
266         aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
267                            false, NULL, NULL, NULL);
268         nfs_destroy_context(client->context);
269     }
270     memset(client, 0, sizeof(NFSClient));
271 }
272 
273 static void nfs_file_close(BlockDriverState *bs)
274 {
275     NFSClient *client = bs->opaque;
276     nfs_client_close(client);
277 }
278 
279 static int64_t nfs_client_open(NFSClient *client, const char *filename,
280                                int flags, Error **errp, int open_flags)
281 {
282     int ret = -EINVAL, i;
283     struct stat st;
284     URI *uri;
285     QueryParams *qp = NULL;
286     char *file = NULL, *strp = NULL;
287 
288     uri = uri_parse(filename);
289     if (!uri) {
290         error_setg(errp, "Invalid URL specified");
291         goto fail;
292     }
293     if (!uri->server) {
294         error_setg(errp, "Invalid URL specified");
295         goto fail;
296     }
297     strp = strrchr(uri->path, '/');
298     if (strp == NULL) {
299         error_setg(errp, "Invalid URL specified");
300         goto fail;
301     }
302     file = g_strdup(strp);
303     *strp = 0;
304 
305     client->context = nfs_init_context();
306     if (client->context == NULL) {
307         error_setg(errp, "Failed to init NFS context");
308         goto fail;
309     }
310 
311     qp = query_params_parse(uri->query);
312     for (i = 0; i < qp->n; i++) {
313         unsigned long long val;
314         if (!qp->p[i].value) {
315             error_setg(errp, "Value for NFS parameter expected: %s",
316                        qp->p[i].name);
317             goto fail;
318         }
319         if (parse_uint_full(qp->p[i].value, &val, 0)) {
320             error_setg(errp, "Illegal value for NFS parameter: %s",
321                        qp->p[i].name);
322             goto fail;
323         }
324         if (!strcmp(qp->p[i].name, "uid")) {
325             nfs_set_uid(client->context, val);
326         } else if (!strcmp(qp->p[i].name, "gid")) {
327             nfs_set_gid(client->context, val);
328         } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
329             nfs_set_tcp_syncnt(client->context, val);
330 #ifdef LIBNFS_FEATURE_READAHEAD
331         } else if (!strcmp(qp->p[i].name, "readahead")) {
332             if (open_flags & BDRV_O_NOCACHE) {
333                 error_setg(errp, "Cannot enable NFS readahead "
334                                  "if cache.direct = on");
335                 goto fail;
336             }
337             if (val > QEMU_NFS_MAX_READAHEAD_SIZE) {
338                 error_report("NFS Warning: Truncating NFS readahead"
339                              " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
340                 val = QEMU_NFS_MAX_READAHEAD_SIZE;
341             }
342             nfs_set_readahead(client->context, val);
343 #ifdef LIBNFS_FEATURE_PAGECACHE
344             nfs_set_pagecache_ttl(client->context, 0);
345 #endif
346             client->cache_used = true;
347 #endif
348 #ifdef LIBNFS_FEATURE_PAGECACHE
349             nfs_set_pagecache_ttl(client->context, 0);
350         } else if (!strcmp(qp->p[i].name, "pagecache")) {
351             if (open_flags & BDRV_O_NOCACHE) {
352                 error_setg(errp, "Cannot enable NFS pagecache "
353                                  "if cache.direct = on");
354                 goto fail;
355             }
356             if (val > QEMU_NFS_MAX_PAGECACHE_SIZE) {
357                 error_report("NFS Warning: Truncating NFS pagecache"
358                              " size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
359                 val = QEMU_NFS_MAX_PAGECACHE_SIZE;
360             }
361             nfs_set_pagecache(client->context, val);
362             nfs_set_pagecache_ttl(client->context, 0);
363             client->cache_used = true;
364 #endif
365 #ifdef LIBNFS_FEATURE_DEBUG
366         } else if (!strcmp(qp->p[i].name, "debug")) {
367             /* limit the maximum debug level to avoid potential flooding
368              * of our log files. */
369             if (val > QEMU_NFS_MAX_DEBUG_LEVEL) {
370                 error_report("NFS Warning: Limiting NFS debug level"
371                              " to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
372                 val = QEMU_NFS_MAX_DEBUG_LEVEL;
373             }
374             nfs_set_debug(client->context, val);
375 #endif
376         } else {
377             error_setg(errp, "Unknown NFS parameter name: %s",
378                        qp->p[i].name);
379             goto fail;
380         }
381     }
382 
383     ret = nfs_mount(client->context, uri->server, uri->path);
384     if (ret < 0) {
385         error_setg(errp, "Failed to mount nfs share: %s",
386                    nfs_get_error(client->context));
387         goto fail;
388     }
389 
390     if (flags & O_CREAT) {
391         ret = nfs_creat(client->context, file, 0600, &client->fh);
392         if (ret < 0) {
393             error_setg(errp, "Failed to create file: %s",
394                        nfs_get_error(client->context));
395             goto fail;
396         }
397     } else {
398         ret = nfs_open(client->context, file, flags, &client->fh);
399         if (ret < 0) {
400             error_setg(errp, "Failed to open file : %s",
401                        nfs_get_error(client->context));
402             goto fail;
403         }
404     }
405 
406     ret = nfs_fstat(client->context, client->fh, &st);
407     if (ret < 0) {
408         error_setg(errp, "Failed to fstat file: %s",
409                    nfs_get_error(client->context));
410         goto fail;
411     }
412 
413     ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
414     client->st_blocks = st.st_blocks;
415     client->has_zero_init = S_ISREG(st.st_mode);
416     goto out;
417 fail:
418     nfs_client_close(client);
419 out:
420     if (qp) {
421         query_params_free(qp);
422     }
423     uri_free(uri);
424     g_free(file);
425     return ret;
426 }
427 
428 static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
429                          Error **errp) {
430     NFSClient *client = bs->opaque;
431     int64_t ret;
432     QemuOpts *opts;
433     Error *local_err = NULL;
434 
435     client->aio_context = bdrv_get_aio_context(bs);
436 
437     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
438     qemu_opts_absorb_qdict(opts, options, &local_err);
439     if (local_err) {
440         error_propagate(errp, local_err);
441         ret = -EINVAL;
442         goto out;
443     }
444     ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
445                           (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
446                           errp, bs->open_flags);
447     if (ret < 0) {
448         goto out;
449     }
450     bs->total_sectors = ret;
451     ret = 0;
452 out:
453     qemu_opts_del(opts);
454     return ret;
455 }
456 
457 static QemuOptsList nfs_create_opts = {
458     .name = "nfs-create-opts",
459     .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
460     .desc = {
461         {
462             .name = BLOCK_OPT_SIZE,
463             .type = QEMU_OPT_SIZE,
464             .help = "Virtual disk size"
465         },
466         { /* end of list */ }
467     }
468 };
469 
470 static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
471 {
472     int ret = 0;
473     int64_t total_size = 0;
474     NFSClient *client = g_new0(NFSClient, 1);
475 
476     client->aio_context = qemu_get_aio_context();
477 
478     /* Read out options */
479     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
480                           BDRV_SECTOR_SIZE);
481 
482     ret = nfs_client_open(client, url, O_CREAT, errp, 0);
483     if (ret < 0) {
484         goto out;
485     }
486     ret = nfs_ftruncate(client->context, client->fh, total_size);
487     nfs_client_close(client);
488 out:
489     g_free(client);
490     return ret;
491 }
492 
493 static int nfs_has_zero_init(BlockDriverState *bs)
494 {
495     NFSClient *client = bs->opaque;
496     return client->has_zero_init;
497 }
498 
499 static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
500 {
501     NFSClient *client = bs->opaque;
502     NFSRPC task = {0};
503     struct stat st;
504 
505     if (bdrv_is_read_only(bs) &&
506         !(bs->open_flags & BDRV_O_NOCACHE)) {
507         return client->st_blocks * 512;
508     }
509 
510     task.st = &st;
511     if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
512                         &task) != 0) {
513         return -ENOMEM;
514     }
515 
516     while (!task.complete) {
517         nfs_set_events(client);
518         aio_poll(client->aio_context, true);
519     }
520 
521     return (task.ret < 0 ? task.ret : st.st_blocks * 512);
522 }
523 
524 static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
525 {
526     NFSClient *client = bs->opaque;
527     return nfs_ftruncate(client->context, client->fh, offset);
528 }
529 
530 /* Note that this will not re-establish a connection with the NFS server
531  * - it is effectively a NOP.  */
532 static int nfs_reopen_prepare(BDRVReopenState *state,
533                               BlockReopenQueue *queue, Error **errp)
534 {
535     NFSClient *client = state->bs->opaque;
536     struct stat st;
537     int ret = 0;
538 
539     if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
540         error_setg(errp, "Cannot open a read-only mount as read-write");
541         return -EACCES;
542     }
543 
544     if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
545         error_setg(errp, "Cannot disable cache if libnfs readahead or"
546                          " pagecache is enabled");
547         return -EINVAL;
548     }
549 
550     /* Update cache for read-only reopens */
551     if (!(state->flags & BDRV_O_RDWR)) {
552         ret = nfs_fstat(client->context, client->fh, &st);
553         if (ret < 0) {
554             error_setg(errp, "Failed to fstat file: %s",
555                        nfs_get_error(client->context));
556             return ret;
557         }
558         client->st_blocks = st.st_blocks;
559     }
560 
561     return 0;
562 }
563 
564 #ifdef LIBNFS_FEATURE_PAGECACHE
565 static void nfs_invalidate_cache(BlockDriverState *bs,
566                                  Error **errp)
567 {
568     NFSClient *client = bs->opaque;
569     nfs_pagecache_invalidate(client->context, client->fh);
570 }
571 #endif
572 
573 static BlockDriver bdrv_nfs = {
574     .format_name                    = "nfs",
575     .protocol_name                  = "nfs",
576 
577     .instance_size                  = sizeof(NFSClient),
578     .bdrv_needs_filename            = true,
579     .create_opts                    = &nfs_create_opts,
580 
581     .bdrv_has_zero_init             = nfs_has_zero_init,
582     .bdrv_get_allocated_file_size   = nfs_get_allocated_file_size,
583     .bdrv_truncate                  = nfs_file_truncate,
584 
585     .bdrv_file_open                 = nfs_file_open,
586     .bdrv_close                     = nfs_file_close,
587     .bdrv_create                    = nfs_file_create,
588     .bdrv_reopen_prepare            = nfs_reopen_prepare,
589 
590     .bdrv_co_readv                  = nfs_co_readv,
591     .bdrv_co_writev                 = nfs_co_writev,
592     .bdrv_co_flush_to_disk          = nfs_co_flush,
593 
594     .bdrv_detach_aio_context        = nfs_detach_aio_context,
595     .bdrv_attach_aio_context        = nfs_attach_aio_context,
596 
597 #ifdef LIBNFS_FEATURE_PAGECACHE
598     .bdrv_invalidate_cache          = nfs_invalidate_cache,
599 #endif
600 };
601 
602 static void nfs_block_init(void)
603 {
604     bdrv_register(&bdrv_nfs);
605 }
606 
607 block_init(nfs_block_init);
608