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