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