blob: 351dc6ec8d14562bc59dc34110f67adca1fbc7e7 [file] [log] [blame]
Peter Lieven6542aa92014-02-03 10:26:13 +01001/*
2 * QEMU Block driver for native access to files on NFS shares
3 *
Peter Lievenf1a7ff72017-11-27 17:00:07 +01004 * Copyright (c) 2014-2017 Peter Lieven <pl@kamp.de>
Peter Lieven6542aa92014-02-03 10:26:13 +01005 *
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
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010026
Yonggang Luoc63b0202020-11-05 20:31:15 +080027#if !defined(_WIN32)
Peter Lieven6542aa92014-02-03 10:26:13 +010028#include <poll.h>
Yonggang Luoc63b0202020-11-05 20:31:15 +080029#endif
Peter Lieven6542aa92014-02-03 10:26:13 +010030#include "qemu/config-file.h"
31#include "qemu/error-report.h"
Stefan Hajnoczid165b8c2016-03-30 13:46:33 +010032#include "qapi/error.h"
Markus Armbrustere2c1c342022-12-21 14:35:49 +010033#include "block/block-io.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010034#include "block/block_int.h"
Max Reitz609f45e2018-06-14 21:14:28 +020035#include "block/qdict.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010036#include "trace.h"
37#include "qemu/iov.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020038#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020039#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010040#include "qemu/option.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010041#include "qemu/uri.h"
Stefan Hajnoczi0d94b742016-03-30 13:46:34 +010042#include "qemu/cutils.h"
Pavel Dovgalyuke4ec5ad2019-09-17 14:58:19 +030043#include "sysemu/replay.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010044#include "qapi/qapi-visit-block-core.h"
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053045#include "qapi/qmp/qdict.h"
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053046#include "qapi/qmp/qstring.h"
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053047#include "qapi/qobject-input-visitor.h"
48#include "qapi/qobject-output-visitor.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010049#include <nfsc/libnfs.h>
50
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053051
Peter Lieven29c838c2015-06-26 13:14:01 +020052#define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
Peter Lievend99b26c2016-05-19 14:48:03 +020053#define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
Peter Lieven7725b8b2015-11-09 08:09:33 +010054#define QEMU_NFS_MAX_DEBUG_LEVEL 2
Peter Lieven29c838c2015-06-26 13:14:01 +020055
Peter Lieven6542aa92014-02-03 10:26:13 +010056typedef struct NFSClient {
57 struct nfs_context *context;
58 struct nfsfh *fh;
59 int events;
60 bool has_zero_init;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020061 AioContext *aio_context;
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +010062 QemuMutex mutex;
Yonggang Luoc63b0202020-11-05 20:31:15 +080063 uint64_t st_blocks;
Peter Lieven38f8d5e2016-05-19 14:48:02 +020064 bool cache_used;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053065 NFSServer *server;
66 char *path;
67 int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
Peter Lieven6542aa92014-02-03 10:26:13 +010068} NFSClient;
69
70typedef struct NFSRPC {
Paolo Bonzinid7464272016-10-27 12:48:57 +020071 BlockDriverState *bs;
Peter Lieven6542aa92014-02-03 10:26:13 +010072 int ret;
73 int complete;
74 QEMUIOVector *iov;
75 struct stat *st;
76 Coroutine *co;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020077 NFSClient *client;
Peter Lieven6542aa92014-02-03 10:26:13 +010078} NFSRPC;
79
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053080static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
81{
82 URI *uri = NULL;
83 QueryParams *qp = NULL;
84 int ret = -EINVAL, i;
85
86 uri = uri_parse(filename);
87 if (!uri) {
88 error_setg(errp, "Invalid URI specified");
89 goto out;
90 }
Max Reitzf69165a2017-06-13 22:57:26 +020091 if (g_strcmp0(uri->scheme, "nfs") != 0) {
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +053092 error_setg(errp, "URI scheme must be 'nfs'");
93 goto out;
94 }
95
96 if (!uri->server) {
97 error_setg(errp, "missing hostname in URI");
98 goto out;
99 }
100
101 if (!uri->path) {
102 error_setg(errp, "missing file path in URI");
103 goto out;
104 }
105
106 qp = query_params_parse(uri->query);
107 if (!qp) {
108 error_setg(errp, "could not parse query parameters");
109 goto out;
110 }
111
Eric Blake46f5ac22017-04-27 16:58:17 -0500112 qdict_put_str(options, "server.host", uri->server);
113 qdict_put_str(options, "server.type", "inet");
114 qdict_put_str(options, "path", uri->path);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530115
116 for (i = 0; i < qp->n; i++) {
Peter Lieven8d20abe2017-02-01 10:53:48 +0100117 unsigned long long val;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530118 if (!qp->p[i].value) {
119 error_setg(errp, "Value for NFS parameter expected: %s",
120 qp->p[i].name);
121 goto out;
122 }
Peter Lieven8d20abe2017-02-01 10:53:48 +0100123 if (parse_uint_full(qp->p[i].value, &val, 0)) {
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530124 error_setg(errp, "Illegal value for NFS parameter: %s",
125 qp->p[i].name);
126 goto out;
127 }
128 if (!strcmp(qp->p[i].name, "uid")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500129 qdict_put_str(options, "user", qp->p[i].value);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530130 } else if (!strcmp(qp->p[i].name, "gid")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500131 qdict_put_str(options, "group", qp->p[i].value);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530132 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500133 qdict_put_str(options, "tcp-syn-count", qp->p[i].value);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530134 } else if (!strcmp(qp->p[i].name, "readahead")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500135 qdict_put_str(options, "readahead-size", qp->p[i].value);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530136 } else if (!strcmp(qp->p[i].name, "pagecache")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500137 qdict_put_str(options, "page-cache-size", qp->p[i].value);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530138 } else if (!strcmp(qp->p[i].name, "debug")) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500139 qdict_put_str(options, "debug", qp->p[i].value);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530140 } else {
141 error_setg(errp, "Unknown NFS parameter name: %s",
142 qp->p[i].name);
143 goto out;
144 }
145 }
146 ret = 0;
147out:
148 if (qp) {
149 query_params_free(qp);
150 }
Heinrich Schuchardtc2615bd2021-06-29 08:36:02 +0200151 uri_free(uri);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530152 return ret;
153}
154
155static bool nfs_has_filename_options_conflict(QDict *options, Error **errp)
156{
157 const QDictEntry *qe;
158
159 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
160 if (!strcmp(qe->key, "host") ||
161 !strcmp(qe->key, "path") ||
162 !strcmp(qe->key, "user") ||
163 !strcmp(qe->key, "group") ||
164 !strcmp(qe->key, "tcp-syn-count") ||
165 !strcmp(qe->key, "readahead-size") ||
166 !strcmp(qe->key, "page-cache-size") ||
Prasanna Kumar Kalever7103d912016-11-02 22:20:37 +0530167 !strcmp(qe->key, "debug") ||
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530168 strstart(qe->key, "server.", NULL))
169 {
170 error_setg(errp, "Option %s cannot be used with a filename",
171 qe->key);
172 return true;
173 }
174 }
175
176 return false;
177}
178
179static void nfs_parse_filename(const char *filename, QDict *options,
180 Error **errp)
181{
182 if (nfs_has_filename_options_conflict(options, errp)) {
183 return;
184 }
185
186 nfs_parse_uri(filename, options, errp);
187}
188
Peter Lieven6542aa92014-02-03 10:26:13 +0100189static void nfs_process_read(void *arg);
190static void nfs_process_write(void *arg);
191
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +0100192/* Called with QemuMutex held. */
Peter Lieven6542aa92014-02-03 10:26:13 +0100193static void nfs_set_events(NFSClient *client)
194{
195 int ev = nfs_which_events(client->context);
196 if (ev != client->events) {
Fam Zhengdca21ef2015-10-23 11:08:05 +0800197 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
198 false,
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200199 (ev & POLLIN) ? nfs_process_read : NULL,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000200 (ev & POLLOUT) ? nfs_process_write : NULL,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000201 NULL, NULL, client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100202
203 }
204 client->events = ev;
205}
206
207static void nfs_process_read(void *arg)
208{
209 NFSClient *client = arg;
Paolo Bonzini9d456652017-02-13 14:52:30 +0100210
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +0100211 qemu_mutex_lock(&client->mutex);
Peter Lieven6542aa92014-02-03 10:26:13 +0100212 nfs_service(client->context, POLLIN);
213 nfs_set_events(client);
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +0100214 qemu_mutex_unlock(&client->mutex);
Peter Lieven6542aa92014-02-03 10:26:13 +0100215}
216
217static void nfs_process_write(void *arg)
218{
219 NFSClient *client = arg;
Paolo Bonzini9d456652017-02-13 14:52:30 +0100220
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +0100221 qemu_mutex_lock(&client->mutex);
Peter Lieven6542aa92014-02-03 10:26:13 +0100222 nfs_service(client->context, POLLOUT);
223 nfs_set_events(client);
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +0100224 qemu_mutex_unlock(&client->mutex);
Peter Lieven6542aa92014-02-03 10:26:13 +0100225}
226
Paolo Bonziniee15ee32022-09-22 10:49:09 +0200227static void coroutine_fn nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
Peter Lieven6542aa92014-02-03 10:26:13 +0100228{
229 *task = (NFSRPC) {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200230 .co = qemu_coroutine_self(),
Paolo Bonzinid7464272016-10-27 12:48:57 +0200231 .bs = bs,
232 .client = bs->opaque,
Peter Lieven6542aa92014-02-03 10:26:13 +0100233 };
234}
235
236static void nfs_co_generic_bh_cb(void *opaque)
237{
238 NFSRPC *task = opaque;
Paolo Bonzini19196312017-02-13 14:52:31 +0100239
Peter Lievena2c0fe22014-06-10 09:42:47 +0200240 task->complete = 1;
Paolo Bonzini19196312017-02-13 14:52:31 +0100241 aio_co_wake(task->co);
Peter Lieven6542aa92014-02-03 10:26:13 +0100242}
243
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +0100244/* Called (via nfs_service) with QemuMutex held. */
Peter Lieven6542aa92014-02-03 10:26:13 +0100245static void
246nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
247 void *private_data)
248{
249 NFSRPC *task = private_data;
Peter Lieven6542aa92014-02-03 10:26:13 +0100250 task->ret = ret;
Paolo Bonzinid7464272016-10-27 12:48:57 +0200251 assert(!task->st);
Peter Lieven6542aa92014-02-03 10:26:13 +0100252 if (task->ret > 0 && task->iov) {
253 if (task->ret <= task->iov->size) {
254 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
255 } else {
256 task->ret = -EIO;
257 }
258 }
Peter Lieven20fccb12014-03-17 09:37:21 +0100259 if (task->ret < 0) {
260 error_report("NFS Error: %s", nfs_get_error(nfs));
261 }
Pavel Dovgalyuke4ec5ad2019-09-17 14:58:19 +0300262 replay_bh_schedule_oneshot_event(task->client->aio_context,
263 nfs_co_generic_bh_cb, task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100264}
265
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300266static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, int64_t offset,
267 int64_t bytes, QEMUIOVector *iov,
268 BdrvRequestFlags flags)
Peter Lieven6542aa92014-02-03 10:26:13 +0100269{
270 NFSClient *client = bs->opaque;
271 NFSRPC task;
272
Paolo Bonzinid7464272016-10-27 12:48:57 +0200273 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100274 task.iov = iov;
275
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700276 WITH_QEMU_LOCK_GUARD(&client->mutex) {
277 if (nfs_pread_async(client->context, client->fh,
278 offset, bytes, nfs_co_generic_cb, &task) != 0) {
279 return -ENOMEM;
280 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100281
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700282 nfs_set_events(client);
283 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100284 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100285 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
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +0300300static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, int64_t offset,
301 int64_t bytes, QEMUIOVector *iov,
302 BdrvRequestFlags flags)
Peter Lieven6542aa92014-02-03 10:26:13 +0100303{
304 NFSClient *client = bs->opaque;
305 NFSRPC task;
306 char *buf = NULL;
Peter Lievenef503a82017-02-17 17:39:01 +0100307 bool my_buffer = false;
Peter Lieven6542aa92014-02-03 10:26:13 +0100308
Paolo Bonzinid7464272016-10-27 12:48:57 +0200309 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100310
Peter Lievenef503a82017-02-17 17:39:01 +0100311 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;
Kevin Wolf2347dd72014-05-20 13:31:20 +0200320 }
321
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700322 WITH_QEMU_LOCK_GUARD(&client->mutex) {
323 if (nfs_pwrite_async(client->context, client->fh,
324 offset, bytes, buf,
325 nfs_co_generic_cb, &task) != 0) {
326 if (my_buffer) {
327 g_free(buf);
328 }
329 return -ENOMEM;
Peter Lievenef503a82017-02-17 17:39:01 +0100330 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100331
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700332 nfs_set_events(client);
333 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100334 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100335 qemu_coroutine_yield();
336 }
337
Peter Lievenef503a82017-02-17 17:39:01 +0100338 if (my_buffer) {
339 g_free(buf);
340 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100341
Peter Lieven69785a22017-02-17 17:39:00 +0100342 if (task.ret != bytes) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100343 return task.ret < 0 ? task.ret : -EIO;
344 }
345
346 return 0;
347}
348
349static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
350{
351 NFSClient *client = bs->opaque;
352 NFSRPC task;
353
Paolo Bonzinid7464272016-10-27 12:48:57 +0200354 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100355
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700356 WITH_QEMU_LOCK_GUARD(&client->mutex) {
357 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
358 &task) != 0) {
359 return -ENOMEM;
360 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100361
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700362 nfs_set_events(client);
363 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100364 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100365 qemu_coroutine_yield();
366 }
367
368 return task.ret;
369}
370
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200371static void nfs_detach_aio_context(BlockDriverState *bs)
372{
373 NFSClient *client = bs->opaque;
374
Fam Zhengdca21ef2015-10-23 11:08:05 +0800375 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000376 false, NULL, NULL, NULL, NULL, NULL);
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200377 client->events = 0;
378}
379
380static void nfs_attach_aio_context(BlockDriverState *bs,
381 AioContext *new_context)
382{
383 NFSClient *client = bs->opaque;
384
385 client->aio_context = new_context;
386 nfs_set_events(client);
387}
388
Peter Lieven6542aa92014-02-03 10:26:13 +0100389static void nfs_client_close(NFSClient *client)
390{
391 if (client->context) {
Peter Lieven601dc652019-09-10 17:41:09 +0200392 qemu_mutex_lock(&client->mutex);
393 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000394 false, NULL, NULL, NULL, NULL, NULL);
Peter Lieven601dc652019-09-10 17:41:09 +0200395 qemu_mutex_unlock(&client->mutex);
Peter Lieven6542aa92014-02-03 10:26:13 +0100396 if (client->fh) {
397 nfs_close(client->context, client->fh);
Jeff Cody113fe792017-08-07 18:29:09 -0400398 client->fh = NULL;
Peter Lieven6542aa92014-02-03 10:26:13 +0100399 }
Peter Lievend2c6bec2019-09-10 17:41:10 +0200400#ifdef LIBNFS_FEATURE_UMOUNT
401 nfs_umount(client->context);
402#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100403 nfs_destroy_context(client->context);
Jeff Cody113fe792017-08-07 18:29:09 -0400404 client->context = NULL;
Peter Lieven6542aa92014-02-03 10:26:13 +0100405 }
Jeff Cody113fe792017-08-07 18:29:09 -0400406 g_free(client->path);
407 qemu_mutex_destroy(&client->mutex);
408 qapi_free_NFSServer(client->server);
409 client->server = NULL;
Peter Lieven6542aa92014-02-03 10:26:13 +0100410}
411
412static void nfs_file_close(BlockDriverState *bs)
413{
414 NFSClient *client = bs->opaque;
415 nfs_client_close(client);
416}
417
Kevin Wolfc22a0342018-01-31 19:47:48 +0100418static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts,
Fam Zhengcb8d4bf2017-04-21 20:27:05 +0800419 int flags, int open_flags, Error **errp)
Peter Lieven6542aa92014-02-03 10:26:13 +0100420{
Peter Lievenf1a7ff72017-11-27 17:00:07 +0100421 int64_t ret = -EINVAL;
Bin Mengebdebe42022-09-08 21:28:15 +0800422#ifdef _WIN32
423 struct __stat64 st;
424#else
Peter Lieven6542aa92014-02-03 10:26:13 +0100425 struct stat st;
Bin Mengebdebe42022-09-08 21:28:15 +0800426#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100427 char *file = NULL, *strp = NULL;
428
Jeff Cody113fe792017-08-07 18:29:09 -0400429 qemu_mutex_init(&client->mutex);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530430
Kevin Wolfc22a0342018-01-31 19:47:48 +0100431 client->path = g_strdup(opts->path);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530432
433 strp = strrchr(client->path, '/');
Peter Lieven6542aa92014-02-03 10:26:13 +0100434 if (strp == NULL) {
435 error_setg(errp, "Invalid URL specified");
436 goto fail;
437 }
438 file = g_strdup(strp);
439 *strp = 0;
440
Kevin Wolfc22a0342018-01-31 19:47:48 +0100441 /* Steal the NFSServer object from opts; set the original pointer to NULL
442 * to avoid use after free and double free. */
443 client->server = opts->server;
444 opts->server = NULL;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530445
Peter Lieven6542aa92014-02-03 10:26:13 +0100446 client->context = nfs_init_context();
447 if (client->context == NULL) {
448 error_setg(errp, "Failed to init NFS context");
449 goto fail;
450 }
451
Kevin Wolfc22a0342018-01-31 19:47:48 +0100452 if (opts->has_user) {
453 client->uid = opts->user;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530454 nfs_set_uid(client->context, client->uid);
Peter Lieven6542aa92014-02-03 10:26:13 +0100455 }
456
Kevin Wolfc22a0342018-01-31 19:47:48 +0100457 if (opts->has_group) {
458 client->gid = opts->group;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530459 nfs_set_gid(client->context, client->gid);
460 }
461
Kevin Wolfc22a0342018-01-31 19:47:48 +0100462 if (opts->has_tcp_syn_count) {
463 client->tcp_syncnt = opts->tcp_syn_count;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530464 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
465 }
466
467#ifdef LIBNFS_FEATURE_READAHEAD
Kevin Wolfc22a0342018-01-31 19:47:48 +0100468 if (opts->has_readahead_size) {
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530469 if (open_flags & BDRV_O_NOCACHE) {
470 error_setg(errp, "Cannot enable NFS readahead "
471 "if cache.direct = on");
472 goto fail;
473 }
Kevin Wolfc22a0342018-01-31 19:47:48 +0100474 client->readahead = opts->readahead_size;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530475 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700476 warn_report("Truncating NFS readahead size to %d",
477 QEMU_NFS_MAX_READAHEAD_SIZE);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530478 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
479 }
480 nfs_set_readahead(client->context, client->readahead);
481#ifdef LIBNFS_FEATURE_PAGECACHE
482 nfs_set_pagecache_ttl(client->context, 0);
483#endif
484 client->cache_used = true;
485 }
486#endif
487
488#ifdef LIBNFS_FEATURE_PAGECACHE
Kevin Wolfc22a0342018-01-31 19:47:48 +0100489 if (opts->has_page_cache_size) {
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530490 if (open_flags & BDRV_O_NOCACHE) {
491 error_setg(errp, "Cannot enable NFS pagecache "
492 "if cache.direct = on");
493 goto fail;
494 }
Kevin Wolfc22a0342018-01-31 19:47:48 +0100495 client->pagecache = opts->page_cache_size;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530496 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700497 warn_report("Truncating NFS pagecache size to %d pages",
498 QEMU_NFS_MAX_PAGECACHE_SIZE);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530499 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
500 }
501 nfs_set_pagecache(client->context, client->pagecache);
502 nfs_set_pagecache_ttl(client->context, 0);
503 client->cache_used = true;
504 }
505#endif
506
507#ifdef LIBNFS_FEATURE_DEBUG
Kevin Wolfc22a0342018-01-31 19:47:48 +0100508 if (opts->has_debug) {
509 client->debug = opts->debug;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530510 /* limit the maximum debug level to avoid potential flooding
511 * of our log files. */
512 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700513 warn_report("Limiting NFS debug level to %d",
514 QEMU_NFS_MAX_DEBUG_LEVEL);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530515 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
516 }
517 nfs_set_debug(client->context, client->debug);
518 }
519#endif
520
521 ret = nfs_mount(client->context, client->server->host, client->path);
Peter Lieven6542aa92014-02-03 10:26:13 +0100522 if (ret < 0) {
523 error_setg(errp, "Failed to mount nfs share: %s",
524 nfs_get_error(client->context));
525 goto fail;
526 }
527
528 if (flags & O_CREAT) {
529 ret = nfs_creat(client->context, file, 0600, &client->fh);
530 if (ret < 0) {
531 error_setg(errp, "Failed to create file: %s",
532 nfs_get_error(client->context));
533 goto fail;
534 }
535 } else {
536 ret = nfs_open(client->context, file, flags, &client->fh);
537 if (ret < 0) {
538 error_setg(errp, "Failed to open file : %s",
539 nfs_get_error(client->context));
540 goto fail;
541 }
542 }
543
544 ret = nfs_fstat(client->context, client->fh, &st);
545 if (ret < 0) {
546 error_setg(errp, "Failed to fstat file: %s",
547 nfs_get_error(client->context));
548 goto fail;
549 }
550
551 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
Yonggang Luoc63b0202020-11-05 20:31:15 +0800552#if !defined(_WIN32)
Peter Lieven18a80562015-08-27 12:30:41 +0200553 client->st_blocks = st.st_blocks;
Yonggang Luoc63b0202020-11-05 20:31:15 +0800554#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100555 client->has_zero_init = S_ISREG(st.st_mode);
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530556 *strp = '/';
Peter Lieven6542aa92014-02-03 10:26:13 +0100557 goto out;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530558
Peter Lieven6542aa92014-02-03 10:26:13 +0100559fail:
560 nfs_client_close(client);
561out:
Peter Lieven6542aa92014-02-03 10:26:13 +0100562 g_free(file);
563 return ret;
564}
565
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100566static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options,
567 Error **errp)
Kevin Wolfc22a0342018-01-31 19:47:48 +0100568{
569 BlockdevOptionsNfs *opts = NULL;
Kevin Wolfc22a0342018-01-31 19:47:48 +0100570 Visitor *v;
Kevin Wolfc82be422018-05-16 18:08:16 +0200571 const QDictEntry *e;
Kevin Wolfc22a0342018-01-31 19:47:48 +0100572
Markus Armbrusteraf910622018-06-14 21:14:33 +0200573 v = qobject_input_visitor_new_flat_confused(options, errp);
574 if (!v) {
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100575 return NULL;
Kevin Wolfc22a0342018-01-31 19:47:48 +0100576 }
577
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200578 visit_type_BlockdevOptionsNfs(v, NULL, &opts, errp);
Kevin Wolfc22a0342018-01-31 19:47:48 +0100579 visit_free(v);
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200580 if (!opts) {
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100581 return NULL;
582 }
583
Kevin Wolfc82be422018-05-16 18:08:16 +0200584 /* Remove the processed options from the QDict (the visitor processes
585 * _all_ options in the QDict) */
586 while ((e = qdict_first(options))) {
587 qdict_del(options, e->key);
588 }
589
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100590 return opts;
591}
592
593static int64_t nfs_client_open_qdict(NFSClient *client, QDict *options,
594 int flags, int open_flags, Error **errp)
595{
596 BlockdevOptionsNfs *opts;
Peter Lieven182454d2020-12-09 13:17:35 +0100597 int64_t ret;
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100598
599 opts = nfs_options_qdict_to_qapi(options, errp);
600 if (opts == NULL) {
Kevin Wolfc22a0342018-01-31 19:47:48 +0100601 ret = -EINVAL;
602 goto fail;
603 }
604
605 ret = nfs_client_open(client, opts, flags, open_flags, errp);
606fail:
Kevin Wolfc22a0342018-01-31 19:47:48 +0100607 qapi_free_BlockdevOptionsNfs(opts);
608 return ret;
609}
610
Peter Lieven6542aa92014-02-03 10:26:13 +0100611static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
612 Error **errp) {
613 NFSClient *client = bs->opaque;
614 int64_t ret;
Peter Lieven6542aa92014-02-03 10:26:13 +0100615
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200616 client->aio_context = bdrv_get_aio_context(bs);
617
Kevin Wolfc22a0342018-01-31 19:47:48 +0100618 ret = nfs_client_open_qdict(client, options,
619 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
620 bs->open_flags, errp);
Peter Lieven6542aa92014-02-03 10:26:13 +0100621 if (ret < 0) {
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530622 return ret;
Peter Lieven6542aa92014-02-03 10:26:13 +0100623 }
Jeff Cody113fe792017-08-07 18:29:09 -0400624
Peter Lieven6542aa92014-02-03 10:26:13 +0100625 bs->total_sectors = ret;
Eric Blake8f23aaf2020-04-28 15:28:59 -0500626 if (client->has_zero_init) {
627 bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
628 }
Simran Singhalb3ac2b92020-04-01 22:23:14 +0530629 return 0;
Peter Lieven6542aa92014-02-03 10:26:13 +0100630}
631
Max Reitzfd752802014-12-02 18:32:44 +0100632static QemuOptsList nfs_create_opts = {
633 .name = "nfs-create-opts",
634 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
635 .desc = {
636 {
637 .name = BLOCK_OPT_SIZE,
638 .type = QEMU_OPT_SIZE,
639 .help = "Virtual disk size"
640 },
641 { /* end of list */ }
642 }
643};
644
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100645static int nfs_file_co_create(BlockdevCreateOptions *options, Error **errp)
Peter Lieven6542aa92014-02-03 10:26:13 +0100646{
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100647 BlockdevCreateOptionsNfs *opts = &options->u.nfs;
Markus Armbruster5839e532014-08-19 10:31:08 +0200648 NFSClient *client = g_new0(NFSClient, 1);
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100649 int ret;
650
651 assert(options->driver == BLOCKDEV_DRIVER_NFS);
Peter Lieven6542aa92014-02-03 10:26:13 +0100652
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200653 client->aio_context = qemu_get_aio_context();
654
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100655 ret = nfs_client_open(client, opts->location, O_CREAT, 0, errp);
656 if (ret < 0) {
657 goto out;
658 }
659 ret = nfs_ftruncate(client->context, client->fh, opts->size);
660 nfs_client_close(client);
661
662out:
663 g_free(client);
664 return ret;
665}
666
Maxim Levitskyb92902d2020-03-26 03:12:17 +0200667static int coroutine_fn nfs_file_co_create_opts(BlockDriver *drv,
668 const char *url,
669 QemuOpts *opts,
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100670 Error **errp)
671{
672 BlockdevCreateOptions *create_options;
673 BlockdevCreateOptionsNfs *nfs_opts;
674 QDict *options;
675 int ret;
676
677 create_options = g_new0(BlockdevCreateOptions, 1);
678 create_options->driver = BLOCKDEV_DRIVER_NFS;
679 nfs_opts = &create_options->u.nfs;
680
Peter Lieven6542aa92014-02-03 10:26:13 +0100681 /* Read out options */
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100682 nfs_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
683 BDRV_SECTOR_SIZE);
Peter Lieven6542aa92014-02-03 10:26:13 +0100684
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530685 options = qdict_new();
686 ret = nfs_parse_uri(url, options, errp);
687 if (ret < 0) {
688 goto out;
689 }
690
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100691 nfs_opts->location = nfs_options_qdict_to_qapi(options, errp);
692 if (nfs_opts->location == NULL) {
693 ret = -EINVAL;
694 goto out;
695 }
696
697 ret = nfs_file_co_create(create_options, errp);
Peter Lieven6542aa92014-02-03 10:26:13 +0100698 if (ret < 0) {
699 goto out;
700 }
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100701
702 ret = 0;
Peter Lieven6542aa92014-02-03 10:26:13 +0100703out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200704 qobject_unref(options);
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100705 qapi_free_BlockdevCreateOptions(create_options);
Peter Lieven6542aa92014-02-03 10:26:13 +0100706 return ret;
707}
708
709static int nfs_has_zero_init(BlockDriverState *bs)
710{
711 NFSClient *client = bs->opaque;
712 return client->has_zero_init;
713}
714
Yonggang Luoc63b0202020-11-05 20:31:15 +0800715#if !defined(_WIN32)
Paolo Bonzini37d1e4d2017-02-22 19:07:24 +0100716/* Called (via nfs_service) with QemuMutex held. */
Paolo Bonzinid7464272016-10-27 12:48:57 +0200717static void
718nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
719 void *private_data)
720{
721 NFSRPC *task = private_data;
722 task->ret = ret;
723 if (task->ret == 0) {
724 memcpy(task->st, data, sizeof(struct stat));
725 }
726 if (task->ret < 0) {
727 error_report("NFS Error: %s", nfs_get_error(nfs));
728 }
Paolo Bonzinie2a6ae72017-06-05 14:38:54 +0200729
730 /* Set task->complete before reading bs->wakeup. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100731 qatomic_mb_set(&task->complete, 1);
Paolo Bonzinic9d1a562016-10-27 12:49:05 +0200732 bdrv_wakeup(task->bs);
Paolo Bonzinid7464272016-10-27 12:48:57 +0200733}
734
Emanuele Giuseppe Esposito82618d72023-01-13 21:42:07 +0100735static int64_t coroutine_fn nfs_co_get_allocated_file_size(BlockDriverState *bs)
Peter Lieven6542aa92014-02-03 10:26:13 +0100736{
737 NFSClient *client = bs->opaque;
738 NFSRPC task = {0};
739 struct stat st;
740
Peter Lieven18a80562015-08-27 12:30:41 +0200741 if (bdrv_is_read_only(bs) &&
742 !(bs->open_flags & BDRV_O_NOCACHE)) {
743 return client->st_blocks * 512;
744 }
745
Paolo Bonzinid7464272016-10-27 12:48:57 +0200746 task.bs = bs;
Peter Lieven6542aa92014-02-03 10:26:13 +0100747 task.st = &st;
Paolo Bonzinid7464272016-10-27 12:48:57 +0200748 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
Peter Lieven6542aa92014-02-03 10:26:13 +0100749 &task) != 0) {
750 return -ENOMEM;
751 }
752
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200753 nfs_set_events(client);
Paolo Bonzinid7464272016-10-27 12:48:57 +0200754 BDRV_POLL_WHILE(bs, !task.complete);
Peter Lieven6542aa92014-02-03 10:26:13 +0100755
Peter Lieven055c6f92015-08-20 12:46:47 +0200756 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
Peter Lieven6542aa92014-02-03 10:26:13 +0100757}
Yonggang Luoc63b0202020-11-05 20:31:15 +0800758#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100759
Kevin Wolf061ca8a2018-06-21 17:54:35 +0200760static int coroutine_fn
Max Reitzc80d8b02019-09-18 11:51:40 +0200761nfs_file_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
Kevin Wolf92b92792020-04-24 14:54:39 +0200762 PreallocMode prealloc, BdrvRequestFlags flags,
763 Error **errp)
Peter Lieven6542aa92014-02-03 10:26:13 +0100764{
765 NFSClient *client = bs->opaque;
Max Reitzf59adb32017-03-28 22:51:29 +0200766 int ret;
767
Max Reitz8243ccb2017-06-13 22:20:52 +0200768 if (prealloc != PREALLOC_MODE_OFF) {
769 error_setg(errp, "Unsupported preallocation mode '%s'",
Markus Armbruster977c7362017-08-24 10:46:08 +0200770 PreallocMode_str(prealloc));
Max Reitz8243ccb2017-06-13 22:20:52 +0200771 return -ENOTSUP;
772 }
773
Max Reitzf59adb32017-03-28 22:51:29 +0200774 ret = nfs_ftruncate(client->context, client->fh, offset);
775 if (ret < 0) {
776 error_setg_errno(errp, -ret, "Failed to truncate file");
777 return ret;
778 }
779
780 return 0;
Peter Lieven6542aa92014-02-03 10:26:13 +0100781}
782
Peter Lieven18a80562015-08-27 12:30:41 +0200783/* Note that this will not re-establish a connection with the NFS server
784 * - it is effectively a NOP. */
785static int nfs_reopen_prepare(BDRVReopenState *state,
786 BlockReopenQueue *queue, Error **errp)
787{
788 NFSClient *client = state->bs->opaque;
Bin Mengebdebe42022-09-08 21:28:15 +0800789#ifdef _WIN32
790 struct __stat64 st;
791#else
Peter Lieven18a80562015-08-27 12:30:41 +0200792 struct stat st;
Bin Mengebdebe42022-09-08 21:28:15 +0800793#endif
Peter Lieven18a80562015-08-27 12:30:41 +0200794 int ret = 0;
795
796 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
797 error_setg(errp, "Cannot open a read-only mount as read-write");
798 return -EACCES;
799 }
800
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200801 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
Peter Lievend99b26c2016-05-19 14:48:03 +0200802 error_setg(errp, "Cannot disable cache if libnfs readahead or"
803 " pagecache is enabled");
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200804 return -EINVAL;
805 }
806
Peter Lieven18a80562015-08-27 12:30:41 +0200807 /* Update cache for read-only reopens */
808 if (!(state->flags & BDRV_O_RDWR)) {
809 ret = nfs_fstat(client->context, client->fh, &st);
810 if (ret < 0) {
811 error_setg(errp, "Failed to fstat file: %s",
812 nfs_get_error(client->context));
813 return ret;
814 }
Yonggang Luoc63b0202020-11-05 20:31:15 +0800815#if !defined(_WIN32)
Peter Lieven18a80562015-08-27 12:30:41 +0200816 client->st_blocks = st.st_blocks;
Yonggang Luoc63b0202020-11-05 20:31:15 +0800817#endif
Peter Lieven18a80562015-08-27 12:30:41 +0200818 }
819
820 return 0;
821}
822
Max Reitz998b3a12019-02-01 20:29:28 +0100823static void nfs_refresh_filename(BlockDriverState *bs)
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530824{
825 NFSClient *client = bs->opaque;
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530826
827 if (client->uid && !client->gid) {
828 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
829 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
830 client->uid);
831 } else if (!client->uid && client->gid) {
832 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
833 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
834 client->gid);
835 } else if (client->uid && client->gid) {
836 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
837 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
838 client->server->host, client->path, client->uid, client->gid);
839 } else {
840 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
841 "nfs://%s%s", client->server->host, client->path);
842 }
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530843}
844
Max Reitz0dcbc542019-02-01 20:29:22 +0100845static char *nfs_dirname(BlockDriverState *bs, Error **errp)
846{
847 NFSClient *client = bs->opaque;
848
849 if (client->uid || client->gid) {
850 bdrv_refresh_filename(bs);
851 error_setg(errp, "Cannot generate a base directory for NFS node '%s'",
852 bs->filename);
853 return NULL;
854 }
855
856 return g_strdup_printf("nfs://%s%s/", client->server->host, client->path);
857}
858
Peter Lievend99b26c2016-05-19 14:48:03 +0200859#ifdef LIBNFS_FEATURE_PAGECACHE
Paolo Bonzini2b148f32018-03-01 17:36:18 +0100860static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs,
861 Error **errp)
Peter Lievend99b26c2016-05-19 14:48:03 +0200862{
863 NFSClient *client = bs->opaque;
864 nfs_pagecache_invalidate(client->context, client->fh);
865}
866#endif
867
Max Reitz26542672019-02-01 20:29:25 +0100868static const char *nfs_strong_runtime_opts[] = {
869 "path",
870 "user",
871 "group",
872 "server.",
873
874 NULL
875};
876
Peter Lieven6542aa92014-02-03 10:26:13 +0100877static BlockDriver bdrv_nfs = {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200878 .format_name = "nfs",
879 .protocol_name = "nfs",
Peter Lieven6542aa92014-02-03 10:26:13 +0100880
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200881 .instance_size = sizeof(NFSClient),
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530882 .bdrv_parse_filename = nfs_parse_filename,
Max Reitzfd752802014-12-02 18:32:44 +0100883 .create_opts = &nfs_create_opts,
884
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200885 .bdrv_has_zero_init = nfs_has_zero_init,
Yonggang Luoc63b0202020-11-05 20:31:15 +0800886/* libnfs does not provide the allocated filesize of a file on win32. */
887#if !defined(_WIN32)
Emanuele Giuseppe Esposito82618d72023-01-13 21:42:07 +0100888 .bdrv_co_get_allocated_file_size = nfs_co_get_allocated_file_size,
Yonggang Luoc63b0202020-11-05 20:31:15 +0800889#endif
Kevin Wolf061ca8a2018-06-21 17:54:35 +0200890 .bdrv_co_truncate = nfs_file_co_truncate,
Peter Lieven6542aa92014-02-03 10:26:13 +0100891
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200892 .bdrv_file_open = nfs_file_open,
893 .bdrv_close = nfs_file_close,
Kevin Wolfa1a42af2018-01-31 16:27:38 +0100894 .bdrv_co_create = nfs_file_co_create,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +0100895 .bdrv_co_create_opts = nfs_file_co_create_opts,
Peter Lieven18a80562015-08-27 12:30:41 +0200896 .bdrv_reopen_prepare = nfs_reopen_prepare,
Peter Lieven6542aa92014-02-03 10:26:13 +0100897
Peter Lieven69785a22017-02-17 17:39:00 +0100898 .bdrv_co_preadv = nfs_co_preadv,
899 .bdrv_co_pwritev = nfs_co_pwritev,
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200900 .bdrv_co_flush_to_disk = nfs_co_flush,
901
902 .bdrv_detach_aio_context = nfs_detach_aio_context,
903 .bdrv_attach_aio_context = nfs_attach_aio_context,
Ashijeet Acharya94d6a7a2016-10-31 20:35:49 +0530904 .bdrv_refresh_filename = nfs_refresh_filename,
Max Reitz0dcbc542019-02-01 20:29:22 +0100905 .bdrv_dirname = nfs_dirname,
Peter Lievend99b26c2016-05-19 14:48:03 +0200906
Max Reitz26542672019-02-01 20:29:25 +0100907 .strong_runtime_opts = nfs_strong_runtime_opts,
908
Peter Lievend99b26c2016-05-19 14:48:03 +0200909#ifdef LIBNFS_FEATURE_PAGECACHE
Paolo Bonzini2b148f32018-03-01 17:36:18 +0100910 .bdrv_co_invalidate_cache = nfs_co_invalidate_cache,
Peter Lievend99b26c2016-05-19 14:48:03 +0200911#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100912};
913
914static void nfs_block_init(void)
915{
916 bdrv_register(&bdrv_nfs);
917}
918
919block_init(nfs_block_init);