1 /* 2 * QEMU Block driver for native access to files on NFS shares 3 * 4 * Copyright (c) 2014-2016 Peter Lieven <pl@kamp.de> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 27 #include <poll.h> 28 #include "qemu-common.h" 29 #include "qemu/config-file.h" 30 #include "qemu/error-report.h" 31 #include "qapi/error.h" 32 #include "block/block_int.h" 33 #include "trace.h" 34 #include "qemu/iov.h" 35 #include "qemu/uri.h" 36 #include "qemu/cutils.h" 37 #include "sysemu/sysemu.h" 38 #include "qapi/qmp/qdict.h" 39 #include "qapi/qmp/qint.h" 40 #include "qapi/qmp/qstring.h" 41 #include "qapi-visit.h" 42 #include "qapi/qobject-input-visitor.h" 43 #include "qapi/qobject-output-visitor.h" 44 #include <nfsc/libnfs.h> 45 46 47 #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576 48 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE) 49 #define QEMU_NFS_MAX_DEBUG_LEVEL 2 50 51 typedef struct NFSClient { 52 struct nfs_context *context; 53 struct nfsfh *fh; 54 int events; 55 bool has_zero_init; 56 AioContext *aio_context; 57 blkcnt_t st_blocks; 58 bool cache_used; 59 NFSServer *server; 60 char *path; 61 int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug; 62 } NFSClient; 63 64 typedef struct NFSRPC { 65 BlockDriverState *bs; 66 int ret; 67 int complete; 68 QEMUIOVector *iov; 69 struct stat *st; 70 Coroutine *co; 71 NFSClient *client; 72 } NFSRPC; 73 74 static int nfs_parse_uri(const char *filename, QDict *options, Error **errp) 75 { 76 URI *uri = NULL; 77 QueryParams *qp = NULL; 78 int ret = -EINVAL, i; 79 80 uri = uri_parse(filename); 81 if (!uri) { 82 error_setg(errp, "Invalid URI specified"); 83 goto out; 84 } 85 if (strcmp(uri->scheme, "nfs") != 0) { 86 error_setg(errp, "URI scheme must be 'nfs'"); 87 goto out; 88 } 89 90 if (!uri->server) { 91 error_setg(errp, "missing hostname in URI"); 92 goto out; 93 } 94 95 if (!uri->path) { 96 error_setg(errp, "missing file path in URI"); 97 goto out; 98 } 99 100 qp = query_params_parse(uri->query); 101 if (!qp) { 102 error_setg(errp, "could not parse query parameters"); 103 goto out; 104 } 105 106 qdict_put(options, "server.host", qstring_from_str(uri->server)); 107 qdict_put(options, "server.type", qstring_from_str("inet")); 108 qdict_put(options, "path", qstring_from_str(uri->path)); 109 110 for (i = 0; i < qp->n; i++) { 111 if (!qp->p[i].value) { 112 error_setg(errp, "Value for NFS parameter expected: %s", 113 qp->p[i].name); 114 goto out; 115 } 116 if (parse_uint_full(qp->p[i].value, NULL, 0)) { 117 error_setg(errp, "Illegal value for NFS parameter: %s", 118 qp->p[i].name); 119 goto out; 120 } 121 if (!strcmp(qp->p[i].name, "uid")) { 122 qdict_put(options, "user", 123 qstring_from_str(qp->p[i].value)); 124 } else if (!strcmp(qp->p[i].name, "gid")) { 125 qdict_put(options, "group", 126 qstring_from_str(qp->p[i].value)); 127 } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) { 128 qdict_put(options, "tcp-syn-count", 129 qstring_from_str(qp->p[i].value)); 130 } else if (!strcmp(qp->p[i].name, "readahead")) { 131 qdict_put(options, "readahead-size", 132 qstring_from_str(qp->p[i].value)); 133 } else if (!strcmp(qp->p[i].name, "pagecache")) { 134 qdict_put(options, "page-cache-size", 135 qstring_from_str(qp->p[i].value)); 136 } else if (!strcmp(qp->p[i].name, "debug")) { 137 qdict_put(options, "debug-level", 138 qstring_from_str(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 if (uri) { 151 uri_free(uri); 152 } 153 return ret; 154 } 155 156 static bool nfs_has_filename_options_conflict(QDict *options, Error **errp) 157 { 158 const QDictEntry *qe; 159 160 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) { 161 if (!strcmp(qe->key, "host") || 162 !strcmp(qe->key, "path") || 163 !strcmp(qe->key, "user") || 164 !strcmp(qe->key, "group") || 165 !strcmp(qe->key, "tcp-syn-count") || 166 !strcmp(qe->key, "readahead-size") || 167 !strcmp(qe->key, "page-cache-size") || 168 !strcmp(qe->key, "debug-level") || 169 strstart(qe->key, "server.", NULL)) 170 { 171 error_setg(errp, "Option %s cannot be used with a filename", 172 qe->key); 173 return true; 174 } 175 } 176 177 return false; 178 } 179 180 static void nfs_parse_filename(const char *filename, QDict *options, 181 Error **errp) 182 { 183 if (nfs_has_filename_options_conflict(options, errp)) { 184 return; 185 } 186 187 nfs_parse_uri(filename, options, errp); 188 } 189 190 static void nfs_process_read(void *arg); 191 static void nfs_process_write(void *arg); 192 193 static void nfs_set_events(NFSClient *client) 194 { 195 int ev = nfs_which_events(client->context); 196 if (ev != client->events) { 197 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), 198 false, 199 (ev & POLLIN) ? nfs_process_read : NULL, 200 (ev & POLLOUT) ? nfs_process_write : NULL, client); 201 202 } 203 client->events = ev; 204 } 205 206 static void nfs_process_read(void *arg) 207 { 208 NFSClient *client = arg; 209 nfs_service(client->context, POLLIN); 210 nfs_set_events(client); 211 } 212 213 static void nfs_process_write(void *arg) 214 { 215 NFSClient *client = arg; 216 nfs_service(client->context, POLLOUT); 217 nfs_set_events(client); 218 } 219 220 static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task) 221 { 222 *task = (NFSRPC) { 223 .co = qemu_coroutine_self(), 224 .bs = bs, 225 .client = bs->opaque, 226 }; 227 } 228 229 static void nfs_co_generic_bh_cb(void *opaque) 230 { 231 NFSRPC *task = opaque; 232 task->complete = 1; 233 qemu_coroutine_enter(task->co); 234 } 235 236 static void 237 nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data, 238 void *private_data) 239 { 240 NFSRPC *task = private_data; 241 task->ret = ret; 242 assert(!task->st); 243 if (task->ret > 0 && task->iov) { 244 if (task->ret <= task->iov->size) { 245 qemu_iovec_from_buf(task->iov, 0, data, task->ret); 246 } else { 247 task->ret = -EIO; 248 } 249 } 250 if (task->ret < 0) { 251 error_report("NFS Error: %s", nfs_get_error(nfs)); 252 } 253 aio_bh_schedule_oneshot(task->client->aio_context, 254 nfs_co_generic_bh_cb, task); 255 } 256 257 static int coroutine_fn nfs_co_readv(BlockDriverState *bs, 258 int64_t sector_num, int nb_sectors, 259 QEMUIOVector *iov) 260 { 261 NFSClient *client = bs->opaque; 262 NFSRPC task; 263 264 nfs_co_init_task(bs, &task); 265 task.iov = iov; 266 267 if (nfs_pread_async(client->context, client->fh, 268 sector_num * BDRV_SECTOR_SIZE, 269 nb_sectors * BDRV_SECTOR_SIZE, 270 nfs_co_generic_cb, &task) != 0) { 271 return -ENOMEM; 272 } 273 274 nfs_set_events(client); 275 while (!task.complete) { 276 qemu_coroutine_yield(); 277 } 278 279 if (task.ret < 0) { 280 return task.ret; 281 } 282 283 /* zero pad short reads */ 284 if (task.ret < iov->size) { 285 qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret); 286 } 287 288 return 0; 289 } 290 291 static int coroutine_fn nfs_co_writev(BlockDriverState *bs, 292 int64_t sector_num, int nb_sectors, 293 QEMUIOVector *iov) 294 { 295 NFSClient *client = bs->opaque; 296 NFSRPC task; 297 char *buf = NULL; 298 299 nfs_co_init_task(bs, &task); 300 301 buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE); 302 if (nb_sectors && buf == NULL) { 303 return -ENOMEM; 304 } 305 306 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE); 307 308 if (nfs_pwrite_async(client->context, client->fh, 309 sector_num * BDRV_SECTOR_SIZE, 310 nb_sectors * BDRV_SECTOR_SIZE, 311 buf, nfs_co_generic_cb, &task) != 0) { 312 g_free(buf); 313 return -ENOMEM; 314 } 315 316 nfs_set_events(client); 317 while (!task.complete) { 318 qemu_coroutine_yield(); 319 } 320 321 g_free(buf); 322 323 if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) { 324 return task.ret < 0 ? task.ret : -EIO; 325 } 326 327 return 0; 328 } 329 330 static int coroutine_fn nfs_co_flush(BlockDriverState *bs) 331 { 332 NFSClient *client = bs->opaque; 333 NFSRPC task; 334 335 nfs_co_init_task(bs, &task); 336 337 if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb, 338 &task) != 0) { 339 return -ENOMEM; 340 } 341 342 nfs_set_events(client); 343 while (!task.complete) { 344 qemu_coroutine_yield(); 345 } 346 347 return task.ret; 348 } 349 350 static QemuOptsList runtime_opts = { 351 .name = "nfs", 352 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 353 .desc = { 354 { 355 .name = "path", 356 .type = QEMU_OPT_STRING, 357 .help = "Path of the image on the host", 358 }, 359 { 360 .name = "uid", 361 .type = QEMU_OPT_NUMBER, 362 .help = "UID value to use when talking to the server", 363 }, 364 { 365 .name = "gid", 366 .type = QEMU_OPT_NUMBER, 367 .help = "GID value to use when talking to the server", 368 }, 369 { 370 .name = "tcp-syncnt", 371 .type = QEMU_OPT_NUMBER, 372 .help = "Number of SYNs to send during the session establish", 373 }, 374 { 375 .name = "readahead", 376 .type = QEMU_OPT_NUMBER, 377 .help = "Set the readahead size in bytes", 378 }, 379 { 380 .name = "pagecache", 381 .type = QEMU_OPT_NUMBER, 382 .help = "Set the pagecache size in bytes", 383 }, 384 { 385 .name = "debug", 386 .type = QEMU_OPT_NUMBER, 387 .help = "Set the NFS debug level (max 2)", 388 }, 389 { /* end of list */ } 390 }, 391 }; 392 393 static void nfs_detach_aio_context(BlockDriverState *bs) 394 { 395 NFSClient *client = bs->opaque; 396 397 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), 398 false, NULL, NULL, NULL); 399 client->events = 0; 400 } 401 402 static void nfs_attach_aio_context(BlockDriverState *bs, 403 AioContext *new_context) 404 { 405 NFSClient *client = bs->opaque; 406 407 client->aio_context = new_context; 408 nfs_set_events(client); 409 } 410 411 static void nfs_client_close(NFSClient *client) 412 { 413 if (client->context) { 414 if (client->fh) { 415 nfs_close(client->context, client->fh); 416 } 417 aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), 418 false, NULL, NULL, NULL); 419 nfs_destroy_context(client->context); 420 } 421 memset(client, 0, sizeof(NFSClient)); 422 } 423 424 static void nfs_file_close(BlockDriverState *bs) 425 { 426 NFSClient *client = bs->opaque; 427 nfs_client_close(client); 428 } 429 430 static NFSServer *nfs_config(QDict *options, Error **errp) 431 { 432 NFSServer *server = NULL; 433 QDict *addr = NULL; 434 QObject *crumpled_addr = NULL; 435 Visitor *iv = NULL; 436 Error *local_error = NULL; 437 438 qdict_extract_subqdict(options, &addr, "server."); 439 if (!qdict_size(addr)) { 440 error_setg(errp, "NFS server address missing"); 441 goto out; 442 } 443 444 crumpled_addr = qdict_crumple(addr, errp); 445 if (!crumpled_addr) { 446 goto out; 447 } 448 449 iv = qobject_input_visitor_new(crumpled_addr, true); 450 visit_type_NFSServer(iv, NULL, &server, &local_error); 451 if (local_error) { 452 error_propagate(errp, local_error); 453 goto out; 454 } 455 456 out: 457 QDECREF(addr); 458 qobject_decref(crumpled_addr); 459 visit_free(iv); 460 return server; 461 } 462 463 464 static int64_t nfs_client_open(NFSClient *client, QDict *options, 465 int flags, Error **errp, int open_flags) 466 { 467 int ret = -EINVAL; 468 QemuOpts *opts = NULL; 469 Error *local_err = NULL; 470 struct stat st; 471 char *file = NULL, *strp = NULL; 472 473 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 474 qemu_opts_absorb_qdict(opts, options, &local_err); 475 if (local_err) { 476 error_propagate(errp, local_err); 477 ret = -EINVAL; 478 goto fail; 479 } 480 481 client->path = g_strdup(qemu_opt_get(opts, "path")); 482 if (!client->path) { 483 ret = -EINVAL; 484 error_setg(errp, "No path was specified"); 485 goto fail; 486 } 487 488 strp = strrchr(client->path, '/'); 489 if (strp == NULL) { 490 error_setg(errp, "Invalid URL specified"); 491 goto fail; 492 } 493 file = g_strdup(strp); 494 *strp = 0; 495 496 /* Pop the config into our state object, Exit if invalid */ 497 client->server = nfs_config(options, errp); 498 if (!client->server) { 499 ret = -EINVAL; 500 goto fail; 501 } 502 503 client->context = nfs_init_context(); 504 if (client->context == NULL) { 505 error_setg(errp, "Failed to init NFS context"); 506 goto fail; 507 } 508 509 if (qemu_opt_get(opts, "uid")) { 510 client->uid = qemu_opt_get_number(opts, "uid", 0); 511 nfs_set_uid(client->context, client->uid); 512 } 513 514 if (qemu_opt_get(opts, "gid")) { 515 client->gid = qemu_opt_get_number(opts, "gid", 0); 516 nfs_set_gid(client->context, client->gid); 517 } 518 519 if (qemu_opt_get(opts, "tcp-syncnt")) { 520 client->tcp_syncnt = qemu_opt_get_number(opts, "tcp-syncnt", 0); 521 nfs_set_tcp_syncnt(client->context, client->tcp_syncnt); 522 } 523 524 #ifdef LIBNFS_FEATURE_READAHEAD 525 if (qemu_opt_get(opts, "readahead")) { 526 if (open_flags & BDRV_O_NOCACHE) { 527 error_setg(errp, "Cannot enable NFS readahead " 528 "if cache.direct = on"); 529 goto fail; 530 } 531 client->readahead = qemu_opt_get_number(opts, "readahead", 0); 532 if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) { 533 error_report("NFS Warning: Truncating NFS readahead " 534 "size to %d", QEMU_NFS_MAX_READAHEAD_SIZE); 535 client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE; 536 } 537 nfs_set_readahead(client->context, client->readahead); 538 #ifdef LIBNFS_FEATURE_PAGECACHE 539 nfs_set_pagecache_ttl(client->context, 0); 540 #endif 541 client->cache_used = true; 542 } 543 #endif 544 545 #ifdef LIBNFS_FEATURE_PAGECACHE 546 if (qemu_opt_get(opts, "pagecache")) { 547 if (open_flags & BDRV_O_NOCACHE) { 548 error_setg(errp, "Cannot enable NFS pagecache " 549 "if cache.direct = on"); 550 goto fail; 551 } 552 client->pagecache = qemu_opt_get_number(opts, "pagecache", 0); 553 if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) { 554 error_report("NFS Warning: Truncating NFS pagecache " 555 "size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE); 556 client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE; 557 } 558 nfs_set_pagecache(client->context, client->pagecache); 559 nfs_set_pagecache_ttl(client->context, 0); 560 client->cache_used = true; 561 } 562 #endif 563 564 #ifdef LIBNFS_FEATURE_DEBUG 565 if (qemu_opt_get(opts, "debug")) { 566 client->debug = qemu_opt_get_number(opts, "debug", 0); 567 /* limit the maximum debug level to avoid potential flooding 568 * of our log files. */ 569 if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) { 570 error_report("NFS Warning: Limiting NFS debug level " 571 "to %d", QEMU_NFS_MAX_DEBUG_LEVEL); 572 client->debug = QEMU_NFS_MAX_DEBUG_LEVEL; 573 } 574 nfs_set_debug(client->context, client->debug); 575 } 576 #endif 577 578 ret = nfs_mount(client->context, client->server->host, client->path); 579 if (ret < 0) { 580 error_setg(errp, "Failed to mount nfs share: %s", 581 nfs_get_error(client->context)); 582 goto fail; 583 } 584 585 if (flags & O_CREAT) { 586 ret = nfs_creat(client->context, file, 0600, &client->fh); 587 if (ret < 0) { 588 error_setg(errp, "Failed to create file: %s", 589 nfs_get_error(client->context)); 590 goto fail; 591 } 592 } else { 593 ret = nfs_open(client->context, file, flags, &client->fh); 594 if (ret < 0) { 595 error_setg(errp, "Failed to open file : %s", 596 nfs_get_error(client->context)); 597 goto fail; 598 } 599 } 600 601 ret = nfs_fstat(client->context, client->fh, &st); 602 if (ret < 0) { 603 error_setg(errp, "Failed to fstat file: %s", 604 nfs_get_error(client->context)); 605 goto fail; 606 } 607 608 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE); 609 client->st_blocks = st.st_blocks; 610 client->has_zero_init = S_ISREG(st.st_mode); 611 *strp = '/'; 612 goto out; 613 614 fail: 615 nfs_client_close(client); 616 out: 617 qemu_opts_del(opts); 618 g_free(file); 619 return ret; 620 } 621 622 static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, 623 Error **errp) { 624 NFSClient *client = bs->opaque; 625 int64_t ret; 626 627 client->aio_context = bdrv_get_aio_context(bs); 628 629 ret = nfs_client_open(client, options, 630 (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY, 631 errp, bs->open_flags); 632 if (ret < 0) { 633 return ret; 634 } 635 bs->total_sectors = ret; 636 ret = 0; 637 return ret; 638 } 639 640 static QemuOptsList nfs_create_opts = { 641 .name = "nfs-create-opts", 642 .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head), 643 .desc = { 644 { 645 .name = BLOCK_OPT_SIZE, 646 .type = QEMU_OPT_SIZE, 647 .help = "Virtual disk size" 648 }, 649 { /* end of list */ } 650 } 651 }; 652 653 static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp) 654 { 655 int ret = 0; 656 int64_t total_size = 0; 657 NFSClient *client = g_new0(NFSClient, 1); 658 QDict *options = NULL; 659 660 client->aio_context = qemu_get_aio_context(); 661 662 /* Read out options */ 663 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 664 BDRV_SECTOR_SIZE); 665 666 options = qdict_new(); 667 ret = nfs_parse_uri(url, options, errp); 668 if (ret < 0) { 669 goto out; 670 } 671 672 ret = nfs_client_open(client, options, O_CREAT, errp, 0); 673 if (ret < 0) { 674 goto out; 675 } 676 ret = nfs_ftruncate(client->context, client->fh, total_size); 677 nfs_client_close(client); 678 out: 679 g_free(client); 680 return ret; 681 } 682 683 static int nfs_has_zero_init(BlockDriverState *bs) 684 { 685 NFSClient *client = bs->opaque; 686 return client->has_zero_init; 687 } 688 689 static void 690 nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data, 691 void *private_data) 692 { 693 NFSRPC *task = private_data; 694 task->ret = ret; 695 if (task->ret == 0) { 696 memcpy(task->st, data, sizeof(struct stat)); 697 } 698 if (task->ret < 0) { 699 error_report("NFS Error: %s", nfs_get_error(nfs)); 700 } 701 task->complete = 1; 702 bdrv_wakeup(task->bs); 703 } 704 705 static int64_t nfs_get_allocated_file_size(BlockDriverState *bs) 706 { 707 NFSClient *client = bs->opaque; 708 NFSRPC task = {0}; 709 struct stat st; 710 711 if (bdrv_is_read_only(bs) && 712 !(bs->open_flags & BDRV_O_NOCACHE)) { 713 return client->st_blocks * 512; 714 } 715 716 task.bs = bs; 717 task.st = &st; 718 if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb, 719 &task) != 0) { 720 return -ENOMEM; 721 } 722 723 nfs_set_events(client); 724 BDRV_POLL_WHILE(bs, !task.complete); 725 726 return (task.ret < 0 ? task.ret : st.st_blocks * 512); 727 } 728 729 static int nfs_file_truncate(BlockDriverState *bs, int64_t offset) 730 { 731 NFSClient *client = bs->opaque; 732 return nfs_ftruncate(client->context, client->fh, offset); 733 } 734 735 /* Note that this will not re-establish a connection with the NFS server 736 * - it is effectively a NOP. */ 737 static int nfs_reopen_prepare(BDRVReopenState *state, 738 BlockReopenQueue *queue, Error **errp) 739 { 740 NFSClient *client = state->bs->opaque; 741 struct stat st; 742 int ret = 0; 743 744 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) { 745 error_setg(errp, "Cannot open a read-only mount as read-write"); 746 return -EACCES; 747 } 748 749 if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) { 750 error_setg(errp, "Cannot disable cache if libnfs readahead or" 751 " pagecache is enabled"); 752 return -EINVAL; 753 } 754 755 /* Update cache for read-only reopens */ 756 if (!(state->flags & BDRV_O_RDWR)) { 757 ret = nfs_fstat(client->context, client->fh, &st); 758 if (ret < 0) { 759 error_setg(errp, "Failed to fstat file: %s", 760 nfs_get_error(client->context)); 761 return ret; 762 } 763 client->st_blocks = st.st_blocks; 764 } 765 766 return 0; 767 } 768 769 static void nfs_refresh_filename(BlockDriverState *bs, QDict *options) 770 { 771 NFSClient *client = bs->opaque; 772 QDict *opts = qdict_new(); 773 QObject *server_qdict; 774 Visitor *ov; 775 776 qdict_put(opts, "driver", qstring_from_str("nfs")); 777 778 if (client->uid && !client->gid) { 779 snprintf(bs->exact_filename, sizeof(bs->exact_filename), 780 "nfs://%s%s?uid=%" PRId64, client->server->host, client->path, 781 client->uid); 782 } else if (!client->uid && client->gid) { 783 snprintf(bs->exact_filename, sizeof(bs->exact_filename), 784 "nfs://%s%s?gid=%" PRId64, client->server->host, client->path, 785 client->gid); 786 } else if (client->uid && client->gid) { 787 snprintf(bs->exact_filename, sizeof(bs->exact_filename), 788 "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64, 789 client->server->host, client->path, client->uid, client->gid); 790 } else { 791 snprintf(bs->exact_filename, sizeof(bs->exact_filename), 792 "nfs://%s%s", client->server->host, client->path); 793 } 794 795 ov = qobject_output_visitor_new(&server_qdict); 796 visit_type_NFSServer(ov, NULL, &client->server, &error_abort); 797 visit_complete(ov, &server_qdict); 798 assert(qobject_type(server_qdict) == QTYPE_QDICT); 799 800 qdict_put_obj(opts, "server", server_qdict); 801 qdict_put(opts, "path", qstring_from_str(client->path)); 802 803 if (client->uid) { 804 qdict_put(opts, "uid", qint_from_int(client->uid)); 805 } 806 if (client->gid) { 807 qdict_put(opts, "gid", qint_from_int(client->gid)); 808 } 809 if (client->tcp_syncnt) { 810 qdict_put(opts, "tcp-syncnt", 811 qint_from_int(client->tcp_syncnt)); 812 } 813 if (client->readahead) { 814 qdict_put(opts, "readahead", 815 qint_from_int(client->readahead)); 816 } 817 if (client->pagecache) { 818 qdict_put(opts, "pagecache", 819 qint_from_int(client->pagecache)); 820 } 821 if (client->debug) { 822 qdict_put(opts, "debug", qint_from_int(client->debug)); 823 } 824 825 visit_free(ov); 826 qdict_flatten(opts); 827 bs->full_open_options = opts; 828 } 829 830 #ifdef LIBNFS_FEATURE_PAGECACHE 831 static void nfs_invalidate_cache(BlockDriverState *bs, 832 Error **errp) 833 { 834 NFSClient *client = bs->opaque; 835 nfs_pagecache_invalidate(client->context, client->fh); 836 } 837 #endif 838 839 static BlockDriver bdrv_nfs = { 840 .format_name = "nfs", 841 .protocol_name = "nfs", 842 843 .instance_size = sizeof(NFSClient), 844 .bdrv_parse_filename = nfs_parse_filename, 845 .create_opts = &nfs_create_opts, 846 847 .bdrv_has_zero_init = nfs_has_zero_init, 848 .bdrv_get_allocated_file_size = nfs_get_allocated_file_size, 849 .bdrv_truncate = nfs_file_truncate, 850 851 .bdrv_file_open = nfs_file_open, 852 .bdrv_close = nfs_file_close, 853 .bdrv_create = nfs_file_create, 854 .bdrv_reopen_prepare = nfs_reopen_prepare, 855 856 .bdrv_co_readv = nfs_co_readv, 857 .bdrv_co_writev = nfs_co_writev, 858 .bdrv_co_flush_to_disk = nfs_co_flush, 859 860 .bdrv_detach_aio_context = nfs_detach_aio_context, 861 .bdrv_attach_aio_context = nfs_attach_aio_context, 862 .bdrv_refresh_filename = nfs_refresh_filename, 863 864 #ifdef LIBNFS_FEATURE_PAGECACHE 865 .bdrv_invalidate_cache = nfs_invalidate_cache, 866 #endif 867 }; 868 869 static void nfs_block_init(void) 870 { 871 bdrv_register(&bdrv_nfs); 872 } 873 874 block_init(nfs_block_init); 875