blob: 7474fbc7cca24f20abc52ff2c67d6e43578e8a69 [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 Lieven38f8d5e2016-05-19 14:48:02 +02004 * Copyright (c) 2014-2016 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
27#include <poll.h>
28#include "qemu-common.h"
29#include "qemu/config-file.h"
30#include "qemu/error-report.h"
Stefan Hajnoczid165b8c2016-03-30 13:46:33 +010031#include "qapi/error.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010032#include "block/block_int.h"
33#include "trace.h"
34#include "qemu/iov.h"
35#include "qemu/uri.h"
Stefan Hajnoczi0d94b742016-03-30 13:46:34 +010036#include "qemu/cutils.h"
Peter Lieven6542aa92014-02-03 10:26:13 +010037#include "sysemu/sysemu.h"
38#include <nfsc/libnfs.h>
39
Peter Lieven29c838c2015-06-26 13:14:01 +020040#define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
Peter Lievend99b26c2016-05-19 14:48:03 +020041#define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
Peter Lieven7725b8b2015-11-09 08:09:33 +010042#define QEMU_NFS_MAX_DEBUG_LEVEL 2
Peter Lieven29c838c2015-06-26 13:14:01 +020043
Peter Lieven6542aa92014-02-03 10:26:13 +010044typedef struct NFSClient {
45 struct nfs_context *context;
46 struct nfsfh *fh;
47 int events;
48 bool has_zero_init;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020049 AioContext *aio_context;
Peter Lieven18a80562015-08-27 12:30:41 +020050 blkcnt_t st_blocks;
Peter Lieven38f8d5e2016-05-19 14:48:02 +020051 bool cache_used;
Peter Lieven6542aa92014-02-03 10:26:13 +010052} NFSClient;
53
54typedef struct NFSRPC {
Paolo Bonzinid7464272016-10-27 12:48:57 +020055 BlockDriverState *bs;
Peter Lieven6542aa92014-02-03 10:26:13 +010056 int ret;
57 int complete;
58 QEMUIOVector *iov;
59 struct stat *st;
60 Coroutine *co;
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020061 NFSClient *client;
Peter Lieven6542aa92014-02-03 10:26:13 +010062} NFSRPC;
63
64static void nfs_process_read(void *arg);
65static void nfs_process_write(void *arg);
66
67static void nfs_set_events(NFSClient *client)
68{
69 int ev = nfs_which_events(client->context);
70 if (ev != client->events) {
Fam Zhengdca21ef2015-10-23 11:08:05 +080071 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
72 false,
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020073 (ev & POLLIN) ? nfs_process_read : NULL,
Fam Zhengdca21ef2015-10-23 11:08:05 +080074 (ev & POLLOUT) ? nfs_process_write : NULL, client);
Peter Lieven6542aa92014-02-03 10:26:13 +010075
76 }
77 client->events = ev;
78}
79
80static void nfs_process_read(void *arg)
81{
82 NFSClient *client = arg;
83 nfs_service(client->context, POLLIN);
84 nfs_set_events(client);
85}
86
87static void nfs_process_write(void *arg)
88{
89 NFSClient *client = arg;
90 nfs_service(client->context, POLLOUT);
91 nfs_set_events(client);
92}
93
Paolo Bonzinid7464272016-10-27 12:48:57 +020094static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
Peter Lieven6542aa92014-02-03 10:26:13 +010095{
96 *task = (NFSRPC) {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +020097 .co = qemu_coroutine_self(),
Paolo Bonzinid7464272016-10-27 12:48:57 +020098 .bs = bs,
99 .client = bs->opaque,
Peter Lieven6542aa92014-02-03 10:26:13 +0100100 };
101}
102
103static void nfs_co_generic_bh_cb(void *opaque)
104{
105 NFSRPC *task = opaque;
Peter Lievena2c0fe22014-06-10 09:42:47 +0200106 task->complete = 1;
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200107 qemu_coroutine_enter(task->co);
Peter Lieven6542aa92014-02-03 10:26:13 +0100108}
109
110static void
111nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
112 void *private_data)
113{
114 NFSRPC *task = private_data;
Peter Lieven6542aa92014-02-03 10:26:13 +0100115 task->ret = ret;
Paolo Bonzinid7464272016-10-27 12:48:57 +0200116 assert(!task->st);
Peter Lieven6542aa92014-02-03 10:26:13 +0100117 if (task->ret > 0 && task->iov) {
118 if (task->ret <= task->iov->size) {
119 qemu_iovec_from_buf(task->iov, 0, data, task->ret);
120 } else {
121 task->ret = -EIO;
122 }
123 }
Peter Lieven20fccb12014-03-17 09:37:21 +0100124 if (task->ret < 0) {
125 error_report("NFS Error: %s", nfs_get_error(nfs));
126 }
Paolo Bonzinid7464272016-10-27 12:48:57 +0200127 aio_bh_schedule_oneshot(task->client->aio_context,
128 nfs_co_generic_bh_cb, task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100129}
130
131static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
132 int64_t sector_num, int nb_sectors,
133 QEMUIOVector *iov)
134{
135 NFSClient *client = bs->opaque;
136 NFSRPC task;
137
Paolo Bonzinid7464272016-10-27 12:48:57 +0200138 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100139 task.iov = iov;
140
141 if (nfs_pread_async(client->context, client->fh,
142 sector_num * BDRV_SECTOR_SIZE,
143 nb_sectors * BDRV_SECTOR_SIZE,
144 nfs_co_generic_cb, &task) != 0) {
145 return -ENOMEM;
146 }
147
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200148 nfs_set_events(client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100149 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100150 qemu_coroutine_yield();
151 }
152
153 if (task.ret < 0) {
154 return task.ret;
155 }
156
157 /* zero pad short reads */
158 if (task.ret < iov->size) {
159 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
160 }
161
162 return 0;
163}
164
165static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
166 int64_t sector_num, int nb_sectors,
167 QEMUIOVector *iov)
168{
169 NFSClient *client = bs->opaque;
170 NFSRPC task;
171 char *buf = NULL;
172
Paolo Bonzinid7464272016-10-27 12:48:57 +0200173 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100174
Kevin Wolf2347dd72014-05-20 13:31:20 +0200175 buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
176 if (nb_sectors && buf == NULL) {
177 return -ENOMEM;
178 }
179
Peter Lieven6542aa92014-02-03 10:26:13 +0100180 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
181
182 if (nfs_pwrite_async(client->context, client->fh,
183 sector_num * BDRV_SECTOR_SIZE,
184 nb_sectors * BDRV_SECTOR_SIZE,
185 buf, nfs_co_generic_cb, &task) != 0) {
186 g_free(buf);
187 return -ENOMEM;
188 }
189
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200190 nfs_set_events(client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100191 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100192 qemu_coroutine_yield();
193 }
194
195 g_free(buf);
196
197 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
198 return task.ret < 0 ? task.ret : -EIO;
199 }
200
201 return 0;
202}
203
204static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
205{
206 NFSClient *client = bs->opaque;
207 NFSRPC task;
208
Paolo Bonzinid7464272016-10-27 12:48:57 +0200209 nfs_co_init_task(bs, &task);
Peter Lieven6542aa92014-02-03 10:26:13 +0100210
211 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
212 &task) != 0) {
213 return -ENOMEM;
214 }
215
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200216 nfs_set_events(client);
Peter Lieven6542aa92014-02-03 10:26:13 +0100217 while (!task.complete) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100218 qemu_coroutine_yield();
219 }
220
221 return task.ret;
222}
223
224/* TODO Convert to fine grained options */
225static QemuOptsList runtime_opts = {
226 .name = "nfs",
227 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
228 .desc = {
229 {
230 .name = "filename",
231 .type = QEMU_OPT_STRING,
232 .help = "URL to the NFS file",
233 },
234 { /* end of list */ }
235 },
236};
237
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200238static void nfs_detach_aio_context(BlockDriverState *bs)
239{
240 NFSClient *client = bs->opaque;
241
Fam Zhengdca21ef2015-10-23 11:08:05 +0800242 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
243 false, NULL, NULL, NULL);
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200244 client->events = 0;
245}
246
247static void nfs_attach_aio_context(BlockDriverState *bs,
248 AioContext *new_context)
249{
250 NFSClient *client = bs->opaque;
251
252 client->aio_context = new_context;
253 nfs_set_events(client);
254}
255
Peter Lieven6542aa92014-02-03 10:26:13 +0100256static void nfs_client_close(NFSClient *client)
257{
258 if (client->context) {
259 if (client->fh) {
260 nfs_close(client->context, client->fh);
261 }
Fam Zhengdca21ef2015-10-23 11:08:05 +0800262 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
263 false, NULL, NULL, NULL);
Peter Lieven6542aa92014-02-03 10:26:13 +0100264 nfs_destroy_context(client->context);
265 }
266 memset(client, 0, sizeof(NFSClient));
267}
268
269static void nfs_file_close(BlockDriverState *bs)
270{
271 NFSClient *client = bs->opaque;
272 nfs_client_close(client);
273}
274
275static int64_t nfs_client_open(NFSClient *client, const char *filename,
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200276 int flags, Error **errp, int open_flags)
Peter Lieven6542aa92014-02-03 10:26:13 +0100277{
278 int ret = -EINVAL, i;
279 struct stat st;
280 URI *uri;
281 QueryParams *qp = NULL;
282 char *file = NULL, *strp = NULL;
283
284 uri = uri_parse(filename);
285 if (!uri) {
286 error_setg(errp, "Invalid URL specified");
287 goto fail;
288 }
Max Reitz5f4d5e12014-05-05 20:27:49 +0200289 if (!uri->server) {
290 error_setg(errp, "Invalid URL specified");
291 goto fail;
292 }
Peter Lieven6542aa92014-02-03 10:26:13 +0100293 strp = strrchr(uri->path, '/');
294 if (strp == NULL) {
295 error_setg(errp, "Invalid URL specified");
296 goto fail;
297 }
298 file = g_strdup(strp);
299 *strp = 0;
300
301 client->context = nfs_init_context();
302 if (client->context == NULL) {
303 error_setg(errp, "Failed to init NFS context");
304 goto fail;
305 }
306
307 qp = query_params_parse(uri->query);
308 for (i = 0; i < qp->n; i++) {
Peter Lieven7c243842014-06-25 00:06:00 +0200309 unsigned long long val;
Peter Lieven6542aa92014-02-03 10:26:13 +0100310 if (!qp->p[i].value) {
311 error_setg(errp, "Value for NFS parameter expected: %s",
312 qp->p[i].name);
313 goto fail;
314 }
Peter Lieven7c243842014-06-25 00:06:00 +0200315 if (parse_uint_full(qp->p[i].value, &val, 0)) {
316 error_setg(errp, "Illegal value for NFS parameter: %s",
317 qp->p[i].name);
318 goto fail;
319 }
320 if (!strcmp(qp->p[i].name, "uid")) {
321 nfs_set_uid(client->context, val);
322 } else if (!strcmp(qp->p[i].name, "gid")) {
323 nfs_set_gid(client->context, val);
324 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
325 nfs_set_tcp_syncnt(client->context, val);
Peter Lievenf42ca3c2014-06-25 00:06:01 +0200326#ifdef LIBNFS_FEATURE_READAHEAD
327 } else if (!strcmp(qp->p[i].name, "readahead")) {
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200328 if (open_flags & BDRV_O_NOCACHE) {
329 error_setg(errp, "Cannot enable NFS readahead "
330 "if cache.direct = on");
331 goto fail;
332 }
Peter Lieven29c838c2015-06-26 13:14:01 +0200333 if (val > QEMU_NFS_MAX_READAHEAD_SIZE) {
334 error_report("NFS Warning: Truncating NFS readahead"
335 " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
336 val = QEMU_NFS_MAX_READAHEAD_SIZE;
337 }
Peter Lievenf42ca3c2014-06-25 00:06:01 +0200338 nfs_set_readahead(client->context, val);
Peter Lievend99b26c2016-05-19 14:48:03 +0200339#ifdef LIBNFS_FEATURE_PAGECACHE
340 nfs_set_pagecache_ttl(client->context, 0);
341#endif
342 client->cache_used = true;
343#endif
344#ifdef LIBNFS_FEATURE_PAGECACHE
345 nfs_set_pagecache_ttl(client->context, 0);
346 } else if (!strcmp(qp->p[i].name, "pagecache")) {
347 if (open_flags & BDRV_O_NOCACHE) {
348 error_setg(errp, "Cannot enable NFS pagecache "
349 "if cache.direct = on");
350 goto fail;
351 }
352 if (val > QEMU_NFS_MAX_PAGECACHE_SIZE) {
353 error_report("NFS Warning: Truncating NFS pagecache"
354 " size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE);
355 val = QEMU_NFS_MAX_PAGECACHE_SIZE;
356 }
357 nfs_set_pagecache(client->context, val);
358 nfs_set_pagecache_ttl(client->context, 0);
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200359 client->cache_used = true;
Peter Lievenf42ca3c2014-06-25 00:06:01 +0200360#endif
Peter Lieven7725b8b2015-11-09 08:09:33 +0100361#ifdef LIBNFS_FEATURE_DEBUG
362 } else if (!strcmp(qp->p[i].name, "debug")) {
363 /* limit the maximum debug level to avoid potential flooding
364 * of our log files. */
365 if (val > QEMU_NFS_MAX_DEBUG_LEVEL) {
366 error_report("NFS Warning: Limiting NFS debug level"
367 " to %d", QEMU_NFS_MAX_DEBUG_LEVEL);
368 val = QEMU_NFS_MAX_DEBUG_LEVEL;
369 }
370 nfs_set_debug(client->context, val);
371#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100372 } else {
373 error_setg(errp, "Unknown NFS parameter name: %s",
374 qp->p[i].name);
375 goto fail;
376 }
377 }
378
379 ret = nfs_mount(client->context, uri->server, uri->path);
380 if (ret < 0) {
381 error_setg(errp, "Failed to mount nfs share: %s",
382 nfs_get_error(client->context));
383 goto fail;
384 }
385
386 if (flags & O_CREAT) {
387 ret = nfs_creat(client->context, file, 0600, &client->fh);
388 if (ret < 0) {
389 error_setg(errp, "Failed to create file: %s",
390 nfs_get_error(client->context));
391 goto fail;
392 }
393 } else {
394 ret = nfs_open(client->context, file, flags, &client->fh);
395 if (ret < 0) {
396 error_setg(errp, "Failed to open file : %s",
397 nfs_get_error(client->context));
398 goto fail;
399 }
400 }
401
402 ret = nfs_fstat(client->context, client->fh, &st);
403 if (ret < 0) {
404 error_setg(errp, "Failed to fstat file: %s",
405 nfs_get_error(client->context));
406 goto fail;
407 }
408
409 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
Peter Lieven18a80562015-08-27 12:30:41 +0200410 client->st_blocks = st.st_blocks;
Peter Lieven6542aa92014-02-03 10:26:13 +0100411 client->has_zero_init = S_ISREG(st.st_mode);
412 goto out;
413fail:
414 nfs_client_close(client);
415out:
416 if (qp) {
417 query_params_free(qp);
418 }
419 uri_free(uri);
420 g_free(file);
421 return ret;
422}
423
424static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
425 Error **errp) {
426 NFSClient *client = bs->opaque;
427 int64_t ret;
428 QemuOpts *opts;
429 Error *local_err = NULL;
430
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200431 client->aio_context = bdrv_get_aio_context(bs);
432
Peter Lieven6542aa92014-02-03 10:26:13 +0100433 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
434 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster0fb63952014-04-25 16:50:31 +0200435 if (local_err) {
Peter Lieven6542aa92014-02-03 10:26:13 +0100436 error_propagate(errp, local_err);
Fam Zheng810f4f82014-08-28 13:56:10 +0800437 ret = -EINVAL;
438 goto out;
Peter Lieven6542aa92014-02-03 10:26:13 +0100439 }
440 ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
441 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200442 errp, bs->open_flags);
Peter Lieven6542aa92014-02-03 10:26:13 +0100443 if (ret < 0) {
Fam Zheng810f4f82014-08-28 13:56:10 +0800444 goto out;
Peter Lieven6542aa92014-02-03 10:26:13 +0100445 }
446 bs->total_sectors = ret;
Fam Zheng810f4f82014-08-28 13:56:10 +0800447 ret = 0;
448out:
449 qemu_opts_del(opts);
450 return ret;
Peter Lieven6542aa92014-02-03 10:26:13 +0100451}
452
Max Reitzfd752802014-12-02 18:32:44 +0100453static QemuOptsList nfs_create_opts = {
454 .name = "nfs-create-opts",
455 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
456 .desc = {
457 {
458 .name = BLOCK_OPT_SIZE,
459 .type = QEMU_OPT_SIZE,
460 .help = "Virtual disk size"
461 },
462 { /* end of list */ }
463 }
464};
465
Chunyan Liu98c10b82014-06-05 17:20:56 +0800466static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
Peter Lieven6542aa92014-02-03 10:26:13 +0100467{
468 int ret = 0;
469 int64_t total_size = 0;
Markus Armbruster5839e532014-08-19 10:31:08 +0200470 NFSClient *client = g_new0(NFSClient, 1);
Peter Lieven6542aa92014-02-03 10:26:13 +0100471
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200472 client->aio_context = qemu_get_aio_context();
473
Peter Lieven6542aa92014-02-03 10:26:13 +0100474 /* Read out options */
Hu Taoc2eb9182014-09-10 17:05:45 +0800475 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
476 BDRV_SECTOR_SIZE);
Peter Lieven6542aa92014-02-03 10:26:13 +0100477
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200478 ret = nfs_client_open(client, url, O_CREAT, errp, 0);
Peter Lieven6542aa92014-02-03 10:26:13 +0100479 if (ret < 0) {
480 goto out;
481 }
482 ret = nfs_ftruncate(client->context, client->fh, total_size);
483 nfs_client_close(client);
484out:
485 g_free(client);
486 return ret;
487}
488
489static int nfs_has_zero_init(BlockDriverState *bs)
490{
491 NFSClient *client = bs->opaque;
492 return client->has_zero_init;
493}
494
Paolo Bonzinid7464272016-10-27 12:48:57 +0200495static void
496nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
497 void *private_data)
498{
499 NFSRPC *task = private_data;
500 task->ret = ret;
501 if (task->ret == 0) {
502 memcpy(task->st, data, sizeof(struct stat));
503 }
504 if (task->ret < 0) {
505 error_report("NFS Error: %s", nfs_get_error(nfs));
506 }
507 task->complete = 1;
508}
509
Peter Lieven6542aa92014-02-03 10:26:13 +0100510static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
511{
512 NFSClient *client = bs->opaque;
513 NFSRPC task = {0};
514 struct stat st;
515
Peter Lieven18a80562015-08-27 12:30:41 +0200516 if (bdrv_is_read_only(bs) &&
517 !(bs->open_flags & BDRV_O_NOCACHE)) {
518 return client->st_blocks * 512;
519 }
520
Paolo Bonzinid7464272016-10-27 12:48:57 +0200521 task.bs = bs;
Peter Lieven6542aa92014-02-03 10:26:13 +0100522 task.st = &st;
Paolo Bonzinid7464272016-10-27 12:48:57 +0200523 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
Peter Lieven6542aa92014-02-03 10:26:13 +0100524 &task) != 0) {
525 return -ENOMEM;
526 }
527
Paolo Bonziniaa92d6c2016-10-27 12:48:56 +0200528 nfs_set_events(client);
Paolo Bonzinid7464272016-10-27 12:48:57 +0200529 BDRV_POLL_WHILE(bs, !task.complete);
Peter Lieven6542aa92014-02-03 10:26:13 +0100530
Peter Lieven055c6f92015-08-20 12:46:47 +0200531 return (task.ret < 0 ? task.ret : st.st_blocks * 512);
Peter Lieven6542aa92014-02-03 10:26:13 +0100532}
533
534static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
535{
536 NFSClient *client = bs->opaque;
537 return nfs_ftruncate(client->context, client->fh, offset);
538}
539
Peter Lieven18a80562015-08-27 12:30:41 +0200540/* Note that this will not re-establish a connection with the NFS server
541 * - it is effectively a NOP. */
542static int nfs_reopen_prepare(BDRVReopenState *state,
543 BlockReopenQueue *queue, Error **errp)
544{
545 NFSClient *client = state->bs->opaque;
546 struct stat st;
547 int ret = 0;
548
549 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
550 error_setg(errp, "Cannot open a read-only mount as read-write");
551 return -EACCES;
552 }
553
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200554 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
Peter Lievend99b26c2016-05-19 14:48:03 +0200555 error_setg(errp, "Cannot disable cache if libnfs readahead or"
556 " pagecache is enabled");
Peter Lieven38f8d5e2016-05-19 14:48:02 +0200557 return -EINVAL;
558 }
559
Peter Lieven18a80562015-08-27 12:30:41 +0200560 /* Update cache for read-only reopens */
561 if (!(state->flags & BDRV_O_RDWR)) {
562 ret = nfs_fstat(client->context, client->fh, &st);
563 if (ret < 0) {
564 error_setg(errp, "Failed to fstat file: %s",
565 nfs_get_error(client->context));
566 return ret;
567 }
568 client->st_blocks = st.st_blocks;
569 }
570
571 return 0;
572}
573
Peter Lievend99b26c2016-05-19 14:48:03 +0200574#ifdef LIBNFS_FEATURE_PAGECACHE
575static void nfs_invalidate_cache(BlockDriverState *bs,
576 Error **errp)
577{
578 NFSClient *client = bs->opaque;
579 nfs_pagecache_invalidate(client->context, client->fh);
580}
581#endif
582
Peter Lieven6542aa92014-02-03 10:26:13 +0100583static BlockDriver bdrv_nfs = {
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200584 .format_name = "nfs",
585 .protocol_name = "nfs",
Peter Lieven6542aa92014-02-03 10:26:13 +0100586
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200587 .instance_size = sizeof(NFSClient),
588 .bdrv_needs_filename = true,
Max Reitzfd752802014-12-02 18:32:44 +0100589 .create_opts = &nfs_create_opts,
590
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200591 .bdrv_has_zero_init = nfs_has_zero_init,
592 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
593 .bdrv_truncate = nfs_file_truncate,
Peter Lieven6542aa92014-02-03 10:26:13 +0100594
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200595 .bdrv_file_open = nfs_file_open,
596 .bdrv_close = nfs_file_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800597 .bdrv_create = nfs_file_create,
Peter Lieven18a80562015-08-27 12:30:41 +0200598 .bdrv_reopen_prepare = nfs_reopen_prepare,
Peter Lieven6542aa92014-02-03 10:26:13 +0100599
Stefan Hajnoczi471799d2014-05-08 16:34:44 +0200600 .bdrv_co_readv = nfs_co_readv,
601 .bdrv_co_writev = nfs_co_writev,
602 .bdrv_co_flush_to_disk = nfs_co_flush,
603
604 .bdrv_detach_aio_context = nfs_detach_aio_context,
605 .bdrv_attach_aio_context = nfs_attach_aio_context,
Peter Lievend99b26c2016-05-19 14:48:03 +0200606
607#ifdef LIBNFS_FEATURE_PAGECACHE
608 .bdrv_invalidate_cache = nfs_invalidate_cache,
609#endif
Peter Lieven6542aa92014-02-03 10:26:13 +0100610};
611
612static void nfs_block_init(void)
613{
614 bdrv_register(&bdrv_nfs);
615}
616
617block_init(nfs_block_init);