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