1 /* 2 * GlusterFS backend for QEMU 3 * 4 * Copyright (C) 2012 Bharata B Rao <bharata@linux.vnet.ibm.com> 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 * 9 */ 10 11 #include "qemu/osdep.h" 12 #include "qemu/units.h" 13 #include <glusterfs/api/glfs.h> 14 #include "block/block_int.h" 15 #include "block/qdict.h" 16 #include "qapi/error.h" 17 #include "qapi/qmp/qdict.h" 18 #include "qapi/qmp/qerror.h" 19 #include "qemu/uri.h" 20 #include "qemu/error-report.h" 21 #include "qemu/module.h" 22 #include "qemu/option.h" 23 #include "qemu/cutils.h" 24 25 #ifdef CONFIG_GLUSTERFS_FTRUNCATE_HAS_STAT 26 # define glfs_ftruncate(fd, offset) glfs_ftruncate(fd, offset, NULL, NULL) 27 #endif 28 29 #define GLUSTER_OPT_FILENAME "filename" 30 #define GLUSTER_OPT_VOLUME "volume" 31 #define GLUSTER_OPT_PATH "path" 32 #define GLUSTER_OPT_TYPE "type" 33 #define GLUSTER_OPT_SERVER_PATTERN "server." 34 #define GLUSTER_OPT_HOST "host" 35 #define GLUSTER_OPT_PORT "port" 36 #define GLUSTER_OPT_TO "to" 37 #define GLUSTER_OPT_IPV4 "ipv4" 38 #define GLUSTER_OPT_IPV6 "ipv6" 39 #define GLUSTER_OPT_SOCKET "socket" 40 #define GLUSTER_OPT_DEBUG "debug" 41 #define GLUSTER_DEFAULT_PORT 24007 42 #define GLUSTER_DEBUG_DEFAULT 4 43 #define GLUSTER_DEBUG_MAX 9 44 #define GLUSTER_OPT_LOGFILE "logfile" 45 #define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */ 46 /* 47 * Several versions of GlusterFS (3.12? -> 6.0.1) fail when the transfer size 48 * is greater or equal to 1024 MiB, so we are limiting the transfer size to 512 49 * MiB to avoid this rare issue. 50 */ 51 #define GLUSTER_MAX_TRANSFER (512 * MiB) 52 53 #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n" 54 55 typedef struct GlusterAIOCB { 56 int64_t size; 57 int ret; 58 Coroutine *coroutine; 59 AioContext *aio_context; 60 } GlusterAIOCB; 61 62 typedef struct BDRVGlusterState { 63 struct glfs *glfs; 64 struct glfs_fd *fd; 65 char *logfile; 66 bool supports_seek_data; 67 int debug; 68 } BDRVGlusterState; 69 70 typedef struct BDRVGlusterReopenState { 71 struct glfs *glfs; 72 struct glfs_fd *fd; 73 } BDRVGlusterReopenState; 74 75 76 typedef struct GlfsPreopened { 77 char *volume; 78 glfs_t *fs; 79 int ref; 80 } GlfsPreopened; 81 82 typedef struct ListElement { 83 QLIST_ENTRY(ListElement) list; 84 GlfsPreopened saved; 85 } ListElement; 86 87 static QLIST_HEAD(, ListElement) glfs_list; 88 89 static QemuOptsList qemu_gluster_create_opts = { 90 .name = "qemu-gluster-create-opts", 91 .head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head), 92 .desc = { 93 { 94 .name = BLOCK_OPT_SIZE, 95 .type = QEMU_OPT_SIZE, 96 .help = "Virtual disk size" 97 }, 98 { 99 .name = BLOCK_OPT_PREALLOC, 100 .type = QEMU_OPT_STRING, 101 .help = "Preallocation mode (allowed values: off" 102 #ifdef CONFIG_GLUSTERFS_FALLOCATE 103 ", falloc" 104 #endif 105 #ifdef CONFIG_GLUSTERFS_ZEROFILL 106 ", full" 107 #endif 108 ")" 109 }, 110 { 111 .name = GLUSTER_OPT_DEBUG, 112 .type = QEMU_OPT_NUMBER, 113 .help = "Gluster log level, valid range is 0-9", 114 }, 115 { 116 .name = GLUSTER_OPT_LOGFILE, 117 .type = QEMU_OPT_STRING, 118 .help = "Logfile path of libgfapi", 119 }, 120 { /* end of list */ } 121 } 122 }; 123 124 static QemuOptsList runtime_opts = { 125 .name = "gluster", 126 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 127 .desc = { 128 { 129 .name = GLUSTER_OPT_FILENAME, 130 .type = QEMU_OPT_STRING, 131 .help = "URL to the gluster image", 132 }, 133 { 134 .name = GLUSTER_OPT_DEBUG, 135 .type = QEMU_OPT_NUMBER, 136 .help = "Gluster log level, valid range is 0-9", 137 }, 138 { 139 .name = GLUSTER_OPT_LOGFILE, 140 .type = QEMU_OPT_STRING, 141 .help = "Logfile path of libgfapi", 142 }, 143 { /* end of list */ } 144 }, 145 }; 146 147 static QemuOptsList runtime_json_opts = { 148 .name = "gluster_json", 149 .head = QTAILQ_HEAD_INITIALIZER(runtime_json_opts.head), 150 .desc = { 151 { 152 .name = GLUSTER_OPT_VOLUME, 153 .type = QEMU_OPT_STRING, 154 .help = "name of gluster volume where VM image resides", 155 }, 156 { 157 .name = GLUSTER_OPT_PATH, 158 .type = QEMU_OPT_STRING, 159 .help = "absolute path to image file in gluster volume", 160 }, 161 { 162 .name = GLUSTER_OPT_DEBUG, 163 .type = QEMU_OPT_NUMBER, 164 .help = "Gluster log level, valid range is 0-9", 165 }, 166 { /* end of list */ } 167 }, 168 }; 169 170 static QemuOptsList runtime_type_opts = { 171 .name = "gluster_type", 172 .head = QTAILQ_HEAD_INITIALIZER(runtime_type_opts.head), 173 .desc = { 174 { 175 .name = GLUSTER_OPT_TYPE, 176 .type = QEMU_OPT_STRING, 177 .help = "inet|unix", 178 }, 179 { /* end of list */ } 180 }, 181 }; 182 183 static QemuOptsList runtime_unix_opts = { 184 .name = "gluster_unix", 185 .head = QTAILQ_HEAD_INITIALIZER(runtime_unix_opts.head), 186 .desc = { 187 { 188 .name = GLUSTER_OPT_SOCKET, 189 .type = QEMU_OPT_STRING, 190 .help = "socket file path (legacy)", 191 }, 192 { 193 .name = GLUSTER_OPT_PATH, 194 .type = QEMU_OPT_STRING, 195 .help = "socket file path (QAPI)", 196 }, 197 { /* end of list */ } 198 }, 199 }; 200 201 static QemuOptsList runtime_inet_opts = { 202 .name = "gluster_inet", 203 .head = QTAILQ_HEAD_INITIALIZER(runtime_inet_opts.head), 204 .desc = { 205 { 206 .name = GLUSTER_OPT_TYPE, 207 .type = QEMU_OPT_STRING, 208 .help = "inet|unix", 209 }, 210 { 211 .name = GLUSTER_OPT_HOST, 212 .type = QEMU_OPT_STRING, 213 .help = "host address (hostname/ipv4/ipv6 addresses)", 214 }, 215 { 216 .name = GLUSTER_OPT_PORT, 217 .type = QEMU_OPT_STRING, 218 .help = "port number on which glusterd is listening (default 24007)", 219 }, 220 { 221 .name = "to", 222 .type = QEMU_OPT_NUMBER, 223 .help = "max port number, not supported by gluster", 224 }, 225 { 226 .name = "ipv4", 227 .type = QEMU_OPT_BOOL, 228 .help = "ipv4 bool value, not supported by gluster", 229 }, 230 { 231 .name = "ipv6", 232 .type = QEMU_OPT_BOOL, 233 .help = "ipv6 bool value, not supported by gluster", 234 }, 235 { /* end of list */ } 236 }, 237 }; 238 239 static void glfs_set_preopened(const char *volume, glfs_t *fs) 240 { 241 ListElement *entry = NULL; 242 243 entry = g_new(ListElement, 1); 244 245 entry->saved.volume = g_strdup(volume); 246 247 entry->saved.fs = fs; 248 entry->saved.ref = 1; 249 250 QLIST_INSERT_HEAD(&glfs_list, entry, list); 251 } 252 253 static glfs_t *glfs_find_preopened(const char *volume) 254 { 255 ListElement *entry = NULL; 256 257 QLIST_FOREACH(entry, &glfs_list, list) { 258 if (strcmp(entry->saved.volume, volume) == 0) { 259 entry->saved.ref++; 260 return entry->saved.fs; 261 } 262 } 263 264 return NULL; 265 } 266 267 static void glfs_clear_preopened(glfs_t *fs) 268 { 269 ListElement *entry = NULL; 270 ListElement *next; 271 272 if (fs == NULL) { 273 return; 274 } 275 276 QLIST_FOREACH_SAFE(entry, &glfs_list, list, next) { 277 if (entry->saved.fs == fs) { 278 if (--entry->saved.ref) { 279 return; 280 } 281 282 QLIST_REMOVE(entry, list); 283 284 glfs_fini(entry->saved.fs); 285 g_free(entry->saved.volume); 286 g_free(entry); 287 } 288 } 289 } 290 291 static int parse_volume_options(BlockdevOptionsGluster *gconf, char *path) 292 { 293 char *p, *q; 294 295 if (!path) { 296 return -EINVAL; 297 } 298 299 /* volume */ 300 p = q = path + strspn(path, "/"); 301 p += strcspn(p, "/"); 302 if (*p == '\0') { 303 return -EINVAL; 304 } 305 gconf->volume = g_strndup(q, p - q); 306 307 /* path */ 308 p += strspn(p, "/"); 309 if (*p == '\0') { 310 return -EINVAL; 311 } 312 gconf->path = g_strdup(p); 313 return 0; 314 } 315 316 /* 317 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...] 318 * 319 * 'gluster' is the protocol. 320 * 321 * 'transport' specifies the transport type used to connect to gluster 322 * management daemon (glusterd). Valid transport types are 323 * tcp or unix. If a transport type isn't specified, then tcp type is assumed. 324 * 325 * 'host' specifies the host where the volume file specification for 326 * the given volume resides. This can be either hostname or ipv4 address. 327 * If transport type is 'unix', then 'host' field should not be specified. 328 * The 'socket' field needs to be populated with the path to unix domain 329 * socket. 330 * 331 * 'port' is the port number on which glusterd is listening. This is optional 332 * and if not specified, QEMU will send 0 which will make gluster to use the 333 * default port. If the transport type is unix, then 'port' should not be 334 * specified. 335 * 336 * 'volume' is the name of the gluster volume which contains the VM image. 337 * 338 * 'path' is the path to the actual VM image that resides on gluster volume. 339 * 340 * Examples: 341 * 342 * file=gluster://1.2.3.4/testvol/a.img 343 * file=gluster+tcp://1.2.3.4/testvol/a.img 344 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img 345 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img 346 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket 347 */ 348 static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf, 349 const char *filename) 350 { 351 SocketAddress *gsconf; 352 URI *uri; 353 QueryParams *qp = NULL; 354 bool is_unix = false; 355 int ret = 0; 356 357 uri = uri_parse(filename); 358 if (!uri) { 359 return -EINVAL; 360 } 361 362 gconf->server = g_new0(SocketAddressList, 1); 363 gconf->server->value = gsconf = g_new0(SocketAddress, 1); 364 365 /* transport */ 366 if (!uri->scheme || !strcmp(uri->scheme, "gluster")) { 367 gsconf->type = SOCKET_ADDRESS_TYPE_INET; 368 } else if (!strcmp(uri->scheme, "gluster+tcp")) { 369 gsconf->type = SOCKET_ADDRESS_TYPE_INET; 370 } else if (!strcmp(uri->scheme, "gluster+unix")) { 371 gsconf->type = SOCKET_ADDRESS_TYPE_UNIX; 372 is_unix = true; 373 } else if (!strcmp(uri->scheme, "gluster+rdma")) { 374 gsconf->type = SOCKET_ADDRESS_TYPE_INET; 375 warn_report("rdma feature is not supported, falling back to tcp"); 376 } else { 377 ret = -EINVAL; 378 goto out; 379 } 380 381 ret = parse_volume_options(gconf, uri->path); 382 if (ret < 0) { 383 goto out; 384 } 385 386 qp = query_params_parse(uri->query); 387 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { 388 ret = -EINVAL; 389 goto out; 390 } 391 392 if (is_unix) { 393 if (uri->server || uri->port) { 394 ret = -EINVAL; 395 goto out; 396 } 397 if (strcmp(qp->p[0].name, "socket")) { 398 ret = -EINVAL; 399 goto out; 400 } 401 gsconf->u.q_unix.path = g_strdup(qp->p[0].value); 402 } else { 403 gsconf->u.inet.host = g_strdup(uri->server ? uri->server : "localhost"); 404 if (uri->port) { 405 gsconf->u.inet.port = g_strdup_printf("%d", uri->port); 406 } else { 407 gsconf->u.inet.port = g_strdup_printf("%d", GLUSTER_DEFAULT_PORT); 408 } 409 } 410 411 out: 412 if (qp) { 413 query_params_free(qp); 414 } 415 uri_free(uri); 416 return ret; 417 } 418 419 static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf, 420 Error **errp) 421 { 422 struct glfs *glfs; 423 int ret; 424 int old_errno; 425 SocketAddressList *server; 426 unsigned long long port; 427 428 glfs = glfs_find_preopened(gconf->volume); 429 if (glfs) { 430 return glfs; 431 } 432 433 glfs = glfs_new(gconf->volume); 434 if (!glfs) { 435 goto out; 436 } 437 438 glfs_set_preopened(gconf->volume, glfs); 439 440 for (server = gconf->server; server; server = server->next) { 441 switch (server->value->type) { 442 case SOCKET_ADDRESS_TYPE_UNIX: 443 ret = glfs_set_volfile_server(glfs, "unix", 444 server->value->u.q_unix.path, 0); 445 break; 446 case SOCKET_ADDRESS_TYPE_INET: 447 if (parse_uint_full(server->value->u.inet.port, &port, 10) < 0 || 448 port > 65535) { 449 error_setg(errp, "'%s' is not a valid port number", 450 server->value->u.inet.port); 451 errno = EINVAL; 452 goto out; 453 } 454 ret = glfs_set_volfile_server(glfs, "tcp", 455 server->value->u.inet.host, 456 (int)port); 457 break; 458 case SOCKET_ADDRESS_TYPE_VSOCK: 459 case SOCKET_ADDRESS_TYPE_FD: 460 default: 461 abort(); 462 } 463 464 if (ret < 0) { 465 goto out; 466 } 467 } 468 469 ret = glfs_set_logging(glfs, gconf->logfile, gconf->debug); 470 if (ret < 0) { 471 goto out; 472 } 473 474 ret = glfs_init(glfs); 475 if (ret) { 476 error_setg(errp, "Gluster connection for volume %s, path %s failed" 477 " to connect", gconf->volume, gconf->path); 478 for (server = gconf->server; server; server = server->next) { 479 if (server->value->type == SOCKET_ADDRESS_TYPE_UNIX) { 480 error_append_hint(errp, "hint: failed on socket %s ", 481 server->value->u.q_unix.path); 482 } else { 483 error_append_hint(errp, "hint: failed on host %s and port %s ", 484 server->value->u.inet.host, 485 server->value->u.inet.port); 486 } 487 } 488 489 error_append_hint(errp, "Please refer to gluster logs for more info\n"); 490 491 /* glfs_init sometimes doesn't set errno although docs suggest that */ 492 if (errno == 0) { 493 errno = EINVAL; 494 } 495 496 goto out; 497 } 498 return glfs; 499 500 out: 501 if (glfs) { 502 old_errno = errno; 503 glfs_clear_preopened(glfs); 504 errno = old_errno; 505 } 506 return NULL; 507 } 508 509 /* 510 * Convert the json formatted command line into qapi. 511 */ 512 static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf, 513 QDict *options, Error **errp) 514 { 515 QemuOpts *opts; 516 SocketAddress *gsconf = NULL; 517 SocketAddressList *curr = NULL; 518 QDict *backing_options = NULL; 519 Error *local_err = NULL; 520 char *str = NULL; 521 const char *ptr; 522 int i, type, num_servers; 523 524 /* create opts info from runtime_json_opts list */ 525 opts = qemu_opts_create(&runtime_json_opts, NULL, 0, &error_abort); 526 if (!qemu_opts_absorb_qdict(opts, options, &local_err)) { 527 goto out; 528 } 529 530 num_servers = qdict_array_entries(options, GLUSTER_OPT_SERVER_PATTERN); 531 if (num_servers < 1) { 532 error_setg(&local_err, QERR_MISSING_PARAMETER, "server"); 533 goto out; 534 } 535 536 ptr = qemu_opt_get(opts, GLUSTER_OPT_VOLUME); 537 if (!ptr) { 538 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_VOLUME); 539 goto out; 540 } 541 gconf->volume = g_strdup(ptr); 542 543 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH); 544 if (!ptr) { 545 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_PATH); 546 goto out; 547 } 548 gconf->path = g_strdup(ptr); 549 qemu_opts_del(opts); 550 551 for (i = 0; i < num_servers; i++) { 552 str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i); 553 qdict_extract_subqdict(options, &backing_options, str); 554 555 /* create opts info from runtime_type_opts list */ 556 opts = qemu_opts_create(&runtime_type_opts, NULL, 0, &error_abort); 557 if (!qemu_opts_absorb_qdict(opts, backing_options, &local_err)) { 558 goto out; 559 } 560 561 ptr = qemu_opt_get(opts, GLUSTER_OPT_TYPE); 562 if (!ptr) { 563 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_TYPE); 564 error_append_hint(&local_err, GERR_INDEX_HINT, i); 565 goto out; 566 567 } 568 gsconf = g_new0(SocketAddress, 1); 569 if (!strcmp(ptr, "tcp")) { 570 ptr = "inet"; /* accept legacy "tcp" */ 571 } 572 type = qapi_enum_parse(&SocketAddressType_lookup, ptr, -1, NULL); 573 if (type != SOCKET_ADDRESS_TYPE_INET 574 && type != SOCKET_ADDRESS_TYPE_UNIX) { 575 error_setg(&local_err, 576 "Parameter '%s' may be 'inet' or 'unix'", 577 GLUSTER_OPT_TYPE); 578 error_append_hint(&local_err, GERR_INDEX_HINT, i); 579 goto out; 580 } 581 gsconf->type = type; 582 qemu_opts_del(opts); 583 584 if (gsconf->type == SOCKET_ADDRESS_TYPE_INET) { 585 /* create opts info from runtime_inet_opts list */ 586 opts = qemu_opts_create(&runtime_inet_opts, NULL, 0, &error_abort); 587 if (!qemu_opts_absorb_qdict(opts, backing_options, &local_err)) { 588 goto out; 589 } 590 591 ptr = qemu_opt_get(opts, GLUSTER_OPT_HOST); 592 if (!ptr) { 593 error_setg(&local_err, QERR_MISSING_PARAMETER, 594 GLUSTER_OPT_HOST); 595 error_append_hint(&local_err, GERR_INDEX_HINT, i); 596 goto out; 597 } 598 gsconf->u.inet.host = g_strdup(ptr); 599 ptr = qemu_opt_get(opts, GLUSTER_OPT_PORT); 600 if (!ptr) { 601 error_setg(&local_err, QERR_MISSING_PARAMETER, 602 GLUSTER_OPT_PORT); 603 error_append_hint(&local_err, GERR_INDEX_HINT, i); 604 goto out; 605 } 606 gsconf->u.inet.port = g_strdup(ptr); 607 608 /* defend for unsupported fields in InetSocketAddress, 609 * i.e. @ipv4, @ipv6 and @to 610 */ 611 ptr = qemu_opt_get(opts, GLUSTER_OPT_TO); 612 if (ptr) { 613 gsconf->u.inet.has_to = true; 614 } 615 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV4); 616 if (ptr) { 617 gsconf->u.inet.has_ipv4 = true; 618 } 619 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV6); 620 if (ptr) { 621 gsconf->u.inet.has_ipv6 = true; 622 } 623 if (gsconf->u.inet.has_to) { 624 error_setg(&local_err, "Parameter 'to' not supported"); 625 goto out; 626 } 627 if (gsconf->u.inet.has_ipv4 || gsconf->u.inet.has_ipv6) { 628 error_setg(&local_err, "Parameters 'ipv4/ipv6' not supported"); 629 goto out; 630 } 631 qemu_opts_del(opts); 632 } else { 633 /* create opts info from runtime_unix_opts list */ 634 opts = qemu_opts_create(&runtime_unix_opts, NULL, 0, &error_abort); 635 if (!qemu_opts_absorb_qdict(opts, backing_options, &local_err)) { 636 goto out; 637 } 638 639 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH); 640 if (!ptr) { 641 ptr = qemu_opt_get(opts, GLUSTER_OPT_SOCKET); 642 } else if (qemu_opt_get(opts, GLUSTER_OPT_SOCKET)) { 643 error_setg(&local_err, 644 "Conflicting parameters 'path' and 'socket'"); 645 error_append_hint(&local_err, GERR_INDEX_HINT, i); 646 goto out; 647 } 648 if (!ptr) { 649 error_setg(&local_err, QERR_MISSING_PARAMETER, 650 GLUSTER_OPT_PATH); 651 error_append_hint(&local_err, GERR_INDEX_HINT, i); 652 goto out; 653 } 654 gsconf->u.q_unix.path = g_strdup(ptr); 655 qemu_opts_del(opts); 656 } 657 658 if (gconf->server == NULL) { 659 gconf->server = g_new0(SocketAddressList, 1); 660 gconf->server->value = gsconf; 661 curr = gconf->server; 662 } else { 663 curr->next = g_new0(SocketAddressList, 1); 664 curr->next->value = gsconf; 665 curr = curr->next; 666 } 667 gsconf = NULL; 668 669 qobject_unref(backing_options); 670 backing_options = NULL; 671 g_free(str); 672 str = NULL; 673 } 674 675 return 0; 676 677 out: 678 error_propagate(errp, local_err); 679 qapi_free_SocketAddress(gsconf); 680 qemu_opts_del(opts); 681 g_free(str); 682 qobject_unref(backing_options); 683 errno = EINVAL; 684 return -errno; 685 } 686 687 /* Converts options given in @filename and the @options QDict into the QAPI 688 * object @gconf. */ 689 static int qemu_gluster_parse(BlockdevOptionsGluster *gconf, 690 const char *filename, 691 QDict *options, Error **errp) 692 { 693 int ret; 694 if (filename) { 695 ret = qemu_gluster_parse_uri(gconf, filename); 696 if (ret < 0) { 697 error_setg(errp, "invalid URI %s", filename); 698 error_append_hint(errp, "Usage: file=gluster[+transport]://" 699 "[host[:port]]volume/path[?socket=...]" 700 "[,file.debug=N]" 701 "[,file.logfile=/path/filename.log]\n"); 702 return ret; 703 } 704 } else { 705 ret = qemu_gluster_parse_json(gconf, options, errp); 706 if (ret < 0) { 707 error_append_hint(errp, "Usage: " 708 "-drive driver=qcow2,file.driver=gluster," 709 "file.volume=testvol,file.path=/path/a.qcow2" 710 "[,file.debug=9]" 711 "[,file.logfile=/path/filename.log]," 712 "file.server.0.type=inet," 713 "file.server.0.host=1.2.3.4," 714 "file.server.0.port=24007," 715 "file.server.1.transport=unix," 716 "file.server.1.path=/var/run/glusterd.socket ..." 717 "\n"); 718 return ret; 719 } 720 } 721 722 return 0; 723 } 724 725 static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf, 726 const char *filename, 727 QDict *options, Error **errp) 728 { 729 int ret; 730 731 ret = qemu_gluster_parse(gconf, filename, options, errp); 732 if (ret < 0) { 733 errno = -ret; 734 return NULL; 735 } 736 737 return qemu_gluster_glfs_init(gconf, errp); 738 } 739 740 /* 741 * AIO callback routine called from GlusterFS thread. 742 */ 743 static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret, 744 #ifdef CONFIG_GLUSTERFS_IOCB_HAS_STAT 745 struct glfs_stat *pre, struct glfs_stat *post, 746 #endif 747 void *arg) 748 { 749 GlusterAIOCB *acb = (GlusterAIOCB *)arg; 750 751 if (!ret || ret == acb->size) { 752 acb->ret = 0; /* Success */ 753 } else if (ret < 0) { 754 acb->ret = -errno; /* Read/Write failed */ 755 } else { 756 acb->ret = -EIO; /* Partial read/write - fail it */ 757 } 758 759 aio_co_schedule(acb->aio_context, acb->coroutine); 760 } 761 762 static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags) 763 { 764 assert(open_flags != NULL); 765 766 *open_flags |= O_BINARY; 767 768 if (bdrv_flags & BDRV_O_RDWR) { 769 *open_flags |= O_RDWR; 770 } else { 771 *open_flags |= O_RDONLY; 772 } 773 774 if ((bdrv_flags & BDRV_O_NOCACHE)) { 775 *open_flags |= O_DIRECT; 776 } 777 } 778 779 /* 780 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of 781 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used. 782 * - Corrected versions return -1 and set errno to EINVAL. 783 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set 784 * errno to ENXIO when SEEK_DATA is called with a position of EOF. 785 */ 786 static bool qemu_gluster_test_seek(struct glfs_fd *fd) 787 { 788 off_t ret = 0; 789 790 #if defined SEEK_HOLE && defined SEEK_DATA 791 off_t eof; 792 793 eof = glfs_lseek(fd, 0, SEEK_END); 794 if (eof < 0) { 795 /* this should never occur */ 796 return false; 797 } 798 799 /* this should always fail with ENXIO if SEEK_DATA is supported */ 800 ret = glfs_lseek(fd, eof, SEEK_DATA); 801 #endif 802 803 return (ret < 0) && (errno == ENXIO); 804 } 805 806 static int qemu_gluster_open(BlockDriverState *bs, QDict *options, 807 int bdrv_flags, Error **errp) 808 { 809 BDRVGlusterState *s = bs->opaque; 810 int open_flags = 0; 811 int ret = 0; 812 BlockdevOptionsGluster *gconf = NULL; 813 QemuOpts *opts; 814 Error *local_err = NULL; 815 const char *filename, *logfile; 816 817 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 818 if (!qemu_opts_absorb_qdict(opts, options, &local_err)) { 819 error_propagate(errp, local_err); 820 ret = -EINVAL; 821 goto out; 822 } 823 824 filename = qemu_opt_get(opts, GLUSTER_OPT_FILENAME); 825 826 s->debug = qemu_opt_get_number(opts, GLUSTER_OPT_DEBUG, 827 GLUSTER_DEBUG_DEFAULT); 828 if (s->debug < 0) { 829 s->debug = 0; 830 } else if (s->debug > GLUSTER_DEBUG_MAX) { 831 s->debug = GLUSTER_DEBUG_MAX; 832 } 833 834 gconf = g_new0(BlockdevOptionsGluster, 1); 835 gconf->debug = s->debug; 836 gconf->has_debug = true; 837 838 logfile = qemu_opt_get(opts, GLUSTER_OPT_LOGFILE); 839 s->logfile = g_strdup(logfile ? logfile : GLUSTER_LOGFILE_DEFAULT); 840 841 gconf->logfile = g_strdup(s->logfile); 842 gconf->has_logfile = true; 843 844 s->glfs = qemu_gluster_init(gconf, filename, options, errp); 845 if (!s->glfs) { 846 ret = -errno; 847 goto out; 848 } 849 850 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT 851 /* Without this, if fsync fails for a recoverable reason (for instance, 852 * ENOSPC), gluster will dump its cache, preventing retries. This means 853 * almost certain data loss. Not all gluster versions support the 854 * 'resync-failed-syncs-after-fsync' key value, but there is no way to 855 * discover during runtime if it is supported (this api returns success for 856 * unknown key/value pairs) */ 857 ret = glfs_set_xlator_option(s->glfs, "*-write-behind", 858 "resync-failed-syncs-after-fsync", 859 "on"); 860 if (ret < 0) { 861 error_setg_errno(errp, errno, "Unable to set xlator key/value pair"); 862 ret = -errno; 863 goto out; 864 } 865 #endif 866 867 qemu_gluster_parse_flags(bdrv_flags, &open_flags); 868 869 s->fd = glfs_open(s->glfs, gconf->path, open_flags); 870 ret = s->fd ? 0 : -errno; 871 872 if (ret == -EACCES || ret == -EROFS) { 873 /* Try to degrade to read-only, but if it doesn't work, still use the 874 * normal error message. */ 875 if (bdrv_apply_auto_read_only(bs, NULL, NULL) == 0) { 876 open_flags = (open_flags & ~O_RDWR) | O_RDONLY; 877 s->fd = glfs_open(s->glfs, gconf->path, open_flags); 878 ret = s->fd ? 0 : -errno; 879 } 880 } 881 882 s->supports_seek_data = qemu_gluster_test_seek(s->fd); 883 884 out: 885 qemu_opts_del(opts); 886 qapi_free_BlockdevOptionsGluster(gconf); 887 if (!ret) { 888 return ret; 889 } 890 g_free(s->logfile); 891 if (s->fd) { 892 glfs_close(s->fd); 893 } 894 895 glfs_clear_preopened(s->glfs); 896 897 return ret; 898 } 899 900 static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp) 901 { 902 bs->bl.max_transfer = GLUSTER_MAX_TRANSFER; 903 } 904 905 static int qemu_gluster_reopen_prepare(BDRVReopenState *state, 906 BlockReopenQueue *queue, Error **errp) 907 { 908 int ret = 0; 909 BDRVGlusterState *s; 910 BDRVGlusterReopenState *reop_s; 911 BlockdevOptionsGluster *gconf; 912 int open_flags = 0; 913 914 assert(state != NULL); 915 assert(state->bs != NULL); 916 917 s = state->bs->opaque; 918 919 state->opaque = g_new0(BDRVGlusterReopenState, 1); 920 reop_s = state->opaque; 921 922 qemu_gluster_parse_flags(state->flags, &open_flags); 923 924 gconf = g_new0(BlockdevOptionsGluster, 1); 925 gconf->debug = s->debug; 926 gconf->has_debug = true; 927 gconf->logfile = g_strdup(s->logfile); 928 gconf->has_logfile = true; 929 930 /* 931 * If 'state->bs->exact_filename' is empty, 'state->options' should contain 932 * the JSON parameters already parsed. 933 */ 934 if (state->bs->exact_filename[0] != '\0') { 935 reop_s->glfs = qemu_gluster_init(gconf, state->bs->exact_filename, NULL, 936 errp); 937 } else { 938 reop_s->glfs = qemu_gluster_init(gconf, NULL, state->options, errp); 939 } 940 if (reop_s->glfs == NULL) { 941 ret = -errno; 942 goto exit; 943 } 944 945 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT 946 ret = glfs_set_xlator_option(reop_s->glfs, "*-write-behind", 947 "resync-failed-syncs-after-fsync", "on"); 948 if (ret < 0) { 949 error_setg_errno(errp, errno, "Unable to set xlator key/value pair"); 950 ret = -errno; 951 goto exit; 952 } 953 #endif 954 955 reop_s->fd = glfs_open(reop_s->glfs, gconf->path, open_flags); 956 if (reop_s->fd == NULL) { 957 /* reops->glfs will be cleaned up in _abort */ 958 ret = -errno; 959 goto exit; 960 } 961 962 exit: 963 /* state->opaque will be freed in either the _abort or _commit */ 964 qapi_free_BlockdevOptionsGluster(gconf); 965 return ret; 966 } 967 968 static void qemu_gluster_reopen_commit(BDRVReopenState *state) 969 { 970 BDRVGlusterReopenState *reop_s = state->opaque; 971 BDRVGlusterState *s = state->bs->opaque; 972 973 974 /* close the old */ 975 if (s->fd) { 976 glfs_close(s->fd); 977 } 978 979 glfs_clear_preopened(s->glfs); 980 981 /* use the newly opened image / connection */ 982 s->fd = reop_s->fd; 983 s->glfs = reop_s->glfs; 984 985 g_free(state->opaque); 986 state->opaque = NULL; 987 988 return; 989 } 990 991 992 static void qemu_gluster_reopen_abort(BDRVReopenState *state) 993 { 994 BDRVGlusterReopenState *reop_s = state->opaque; 995 996 if (reop_s == NULL) { 997 return; 998 } 999 1000 if (reop_s->fd) { 1001 glfs_close(reop_s->fd); 1002 } 1003 1004 glfs_clear_preopened(reop_s->glfs); 1005 1006 g_free(state->opaque); 1007 state->opaque = NULL; 1008 1009 return; 1010 } 1011 1012 #ifdef CONFIG_GLUSTERFS_ZEROFILL 1013 static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs, 1014 int64_t offset, 1015 int size, 1016 BdrvRequestFlags flags) 1017 { 1018 int ret; 1019 GlusterAIOCB acb; 1020 BDRVGlusterState *s = bs->opaque; 1021 1022 acb.size = size; 1023 acb.ret = 0; 1024 acb.coroutine = qemu_coroutine_self(); 1025 acb.aio_context = bdrv_get_aio_context(bs); 1026 1027 ret = glfs_zerofill_async(s->fd, offset, size, gluster_finish_aiocb, &acb); 1028 if (ret < 0) { 1029 return -errno; 1030 } 1031 1032 qemu_coroutine_yield(); 1033 return acb.ret; 1034 } 1035 #endif 1036 1037 static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset, 1038 PreallocMode prealloc, Error **errp) 1039 { 1040 int64_t current_length; 1041 1042 current_length = glfs_lseek(fd, 0, SEEK_END); 1043 if (current_length < 0) { 1044 error_setg_errno(errp, errno, "Failed to determine current size"); 1045 return -errno; 1046 } 1047 1048 if (current_length > offset && prealloc != PREALLOC_MODE_OFF) { 1049 error_setg(errp, "Cannot use preallocation for shrinking files"); 1050 return -ENOTSUP; 1051 } 1052 1053 if (current_length == offset) { 1054 return 0; 1055 } 1056 1057 switch (prealloc) { 1058 #ifdef CONFIG_GLUSTERFS_FALLOCATE 1059 case PREALLOC_MODE_FALLOC: 1060 if (glfs_fallocate(fd, 0, current_length, offset - current_length)) { 1061 error_setg_errno(errp, errno, "Could not preallocate data"); 1062 return -errno; 1063 } 1064 break; 1065 #endif /* CONFIG_GLUSTERFS_FALLOCATE */ 1066 #ifdef CONFIG_GLUSTERFS_ZEROFILL 1067 case PREALLOC_MODE_FULL: 1068 if (glfs_ftruncate(fd, offset)) { 1069 error_setg_errno(errp, errno, "Could not resize file"); 1070 return -errno; 1071 } 1072 if (glfs_zerofill(fd, current_length, offset - current_length)) { 1073 error_setg_errno(errp, errno, "Could not zerofill the new area"); 1074 return -errno; 1075 } 1076 break; 1077 #endif /* CONFIG_GLUSTERFS_ZEROFILL */ 1078 case PREALLOC_MODE_OFF: 1079 if (glfs_ftruncate(fd, offset)) { 1080 error_setg_errno(errp, errno, "Could not resize file"); 1081 return -errno; 1082 } 1083 break; 1084 default: 1085 error_setg(errp, "Unsupported preallocation mode: %s", 1086 PreallocMode_str(prealloc)); 1087 return -EINVAL; 1088 } 1089 1090 return 0; 1091 } 1092 1093 static int qemu_gluster_co_create(BlockdevCreateOptions *options, 1094 Error **errp) 1095 { 1096 BlockdevCreateOptionsGluster *opts = &options->u.gluster; 1097 struct glfs *glfs; 1098 struct glfs_fd *fd = NULL; 1099 int ret = 0; 1100 1101 assert(options->driver == BLOCKDEV_DRIVER_GLUSTER); 1102 1103 glfs = qemu_gluster_glfs_init(opts->location, errp); 1104 if (!glfs) { 1105 ret = -errno; 1106 goto out; 1107 } 1108 1109 fd = glfs_creat(glfs, opts->location->path, 1110 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR); 1111 if (!fd) { 1112 ret = -errno; 1113 goto out; 1114 } 1115 1116 ret = qemu_gluster_do_truncate(fd, opts->size, opts->preallocation, errp); 1117 1118 out: 1119 if (fd) { 1120 if (glfs_close(fd) != 0 && ret == 0) { 1121 ret = -errno; 1122 } 1123 } 1124 glfs_clear_preopened(glfs); 1125 return ret; 1126 } 1127 1128 static int coroutine_fn qemu_gluster_co_create_opts(BlockDriver *drv, 1129 const char *filename, 1130 QemuOpts *opts, 1131 Error **errp) 1132 { 1133 BlockdevCreateOptions *options; 1134 BlockdevCreateOptionsGluster *gopts; 1135 BlockdevOptionsGluster *gconf; 1136 char *tmp = NULL; 1137 Error *local_err = NULL; 1138 int ret; 1139 1140 options = g_new0(BlockdevCreateOptions, 1); 1141 options->driver = BLOCKDEV_DRIVER_GLUSTER; 1142 gopts = &options->u.gluster; 1143 1144 gconf = g_new0(BlockdevOptionsGluster, 1); 1145 gopts->location = gconf; 1146 1147 gopts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 1148 BDRV_SECTOR_SIZE); 1149 1150 tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); 1151 gopts->preallocation = qapi_enum_parse(&PreallocMode_lookup, tmp, 1152 PREALLOC_MODE_OFF, &local_err); 1153 g_free(tmp); 1154 if (local_err) { 1155 error_propagate(errp, local_err); 1156 ret = -EINVAL; 1157 goto fail; 1158 } 1159 1160 gconf->debug = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG, 1161 GLUSTER_DEBUG_DEFAULT); 1162 if (gconf->debug < 0) { 1163 gconf->debug = 0; 1164 } else if (gconf->debug > GLUSTER_DEBUG_MAX) { 1165 gconf->debug = GLUSTER_DEBUG_MAX; 1166 } 1167 gconf->has_debug = true; 1168 1169 gconf->logfile = qemu_opt_get_del(opts, GLUSTER_OPT_LOGFILE); 1170 if (!gconf->logfile) { 1171 gconf->logfile = g_strdup(GLUSTER_LOGFILE_DEFAULT); 1172 } 1173 gconf->has_logfile = true; 1174 1175 ret = qemu_gluster_parse(gconf, filename, NULL, errp); 1176 if (ret < 0) { 1177 goto fail; 1178 } 1179 1180 ret = qemu_gluster_co_create(options, errp); 1181 if (ret < 0) { 1182 goto fail; 1183 } 1184 1185 ret = 0; 1186 fail: 1187 qapi_free_BlockdevCreateOptions(options); 1188 return ret; 1189 } 1190 1191 static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs, 1192 int64_t sector_num, int nb_sectors, 1193 QEMUIOVector *qiov, int write) 1194 { 1195 int ret; 1196 GlusterAIOCB acb; 1197 BDRVGlusterState *s = bs->opaque; 1198 size_t size = nb_sectors * BDRV_SECTOR_SIZE; 1199 off_t offset = sector_num * BDRV_SECTOR_SIZE; 1200 1201 acb.size = size; 1202 acb.ret = 0; 1203 acb.coroutine = qemu_coroutine_self(); 1204 acb.aio_context = bdrv_get_aio_context(bs); 1205 1206 if (write) { 1207 ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0, 1208 gluster_finish_aiocb, &acb); 1209 } else { 1210 ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0, 1211 gluster_finish_aiocb, &acb); 1212 } 1213 1214 if (ret < 0) { 1215 return -errno; 1216 } 1217 1218 qemu_coroutine_yield(); 1219 return acb.ret; 1220 } 1221 1222 static coroutine_fn int qemu_gluster_co_truncate(BlockDriverState *bs, 1223 int64_t offset, 1224 bool exact, 1225 PreallocMode prealloc, 1226 BdrvRequestFlags flags, 1227 Error **errp) 1228 { 1229 BDRVGlusterState *s = bs->opaque; 1230 return qemu_gluster_do_truncate(s->fd, offset, prealloc, errp); 1231 } 1232 1233 static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs, 1234 int64_t sector_num, 1235 int nb_sectors, 1236 QEMUIOVector *qiov) 1237 { 1238 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0); 1239 } 1240 1241 static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs, 1242 int64_t sector_num, 1243 int nb_sectors, 1244 QEMUIOVector *qiov, 1245 int flags) 1246 { 1247 assert(!flags); 1248 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1); 1249 } 1250 1251 static void qemu_gluster_close(BlockDriverState *bs) 1252 { 1253 BDRVGlusterState *s = bs->opaque; 1254 1255 g_free(s->logfile); 1256 if (s->fd) { 1257 glfs_close(s->fd); 1258 s->fd = NULL; 1259 } 1260 glfs_clear_preopened(s->glfs); 1261 } 1262 1263 static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs) 1264 { 1265 int ret; 1266 GlusterAIOCB acb; 1267 BDRVGlusterState *s = bs->opaque; 1268 1269 acb.size = 0; 1270 acb.ret = 0; 1271 acb.coroutine = qemu_coroutine_self(); 1272 acb.aio_context = bdrv_get_aio_context(bs); 1273 1274 ret = glfs_fsync_async(s->fd, gluster_finish_aiocb, &acb); 1275 if (ret < 0) { 1276 ret = -errno; 1277 goto error; 1278 } 1279 1280 qemu_coroutine_yield(); 1281 if (acb.ret < 0) { 1282 ret = acb.ret; 1283 goto error; 1284 } 1285 1286 return acb.ret; 1287 1288 error: 1289 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache 1290 * after a fsync failure, so we have no way of allowing the guest to safely 1291 * continue. Gluster versions prior to 3.5.6 don't retain the cache 1292 * either, but will invalidate the fd on error, so this is again our only 1293 * option. 1294 * 1295 * The 'resync-failed-syncs-after-fsync' xlator option for the 1296 * write-behind cache will cause later gluster versions to retain its 1297 * cache after error, so long as the fd remains open. However, we 1298 * currently have no way of knowing if this option is supported. 1299 * 1300 * TODO: Once gluster provides a way for us to determine if the option 1301 * is supported, bypass the closure and setting drv to NULL. */ 1302 qemu_gluster_close(bs); 1303 bs->drv = NULL; 1304 return ret; 1305 } 1306 1307 #ifdef CONFIG_GLUSTERFS_DISCARD 1308 static coroutine_fn int qemu_gluster_co_pdiscard(BlockDriverState *bs, 1309 int64_t offset, int size) 1310 { 1311 int ret; 1312 GlusterAIOCB acb; 1313 BDRVGlusterState *s = bs->opaque; 1314 1315 acb.size = 0; 1316 acb.ret = 0; 1317 acb.coroutine = qemu_coroutine_self(); 1318 acb.aio_context = bdrv_get_aio_context(bs); 1319 1320 ret = glfs_discard_async(s->fd, offset, size, gluster_finish_aiocb, &acb); 1321 if (ret < 0) { 1322 return -errno; 1323 } 1324 1325 qemu_coroutine_yield(); 1326 return acb.ret; 1327 } 1328 #endif 1329 1330 static int64_t qemu_gluster_getlength(BlockDriverState *bs) 1331 { 1332 BDRVGlusterState *s = bs->opaque; 1333 int64_t ret; 1334 1335 ret = glfs_lseek(s->fd, 0, SEEK_END); 1336 if (ret < 0) { 1337 return -errno; 1338 } else { 1339 return ret; 1340 } 1341 } 1342 1343 static int64_t qemu_gluster_allocated_file_size(BlockDriverState *bs) 1344 { 1345 BDRVGlusterState *s = bs->opaque; 1346 struct stat st; 1347 int ret; 1348 1349 ret = glfs_fstat(s->fd, &st); 1350 if (ret < 0) { 1351 return -errno; 1352 } else { 1353 return st.st_blocks * 512; 1354 } 1355 } 1356 1357 /* 1358 * Find allocation range in @bs around offset @start. 1359 * May change underlying file descriptor's file offset. 1360 * If @start is not in a hole, store @start in @data, and the 1361 * beginning of the next hole in @hole, and return 0. 1362 * If @start is in a non-trailing hole, store @start in @hole and the 1363 * beginning of the next non-hole in @data, and return 0. 1364 * If @start is in a trailing hole or beyond EOF, return -ENXIO. 1365 * If we can't find out, return a negative errno other than -ENXIO. 1366 * 1367 * (Shamefully copied from file-posix.c, only minuscule adaptions.) 1368 */ 1369 static int find_allocation(BlockDriverState *bs, off_t start, 1370 off_t *data, off_t *hole) 1371 { 1372 BDRVGlusterState *s = bs->opaque; 1373 1374 if (!s->supports_seek_data) { 1375 goto exit; 1376 } 1377 1378 #if defined SEEK_HOLE && defined SEEK_DATA 1379 off_t offs; 1380 1381 /* 1382 * SEEK_DATA cases: 1383 * D1. offs == start: start is in data 1384 * D2. offs > start: start is in a hole, next data at offs 1385 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole 1386 * or start is beyond EOF 1387 * If the latter happens, the file has been truncated behind 1388 * our back since we opened it. All bets are off then. 1389 * Treating like a trailing hole is simplest. 1390 * D4. offs < 0, errno != ENXIO: we learned nothing 1391 */ 1392 offs = glfs_lseek(s->fd, start, SEEK_DATA); 1393 if (offs < 0) { 1394 return -errno; /* D3 or D4 */ 1395 } 1396 1397 if (offs < start) { 1398 /* This is not a valid return by lseek(). We are safe to just return 1399 * -EIO in this case, and we'll treat it like D4. Unfortunately some 1400 * versions of gluster server will return offs < start, so an assert 1401 * here will unnecessarily abort QEMU. */ 1402 return -EIO; 1403 } 1404 1405 if (offs > start) { 1406 /* D2: in hole, next data at offs */ 1407 *hole = start; 1408 *data = offs; 1409 return 0; 1410 } 1411 1412 /* D1: in data, end not yet known */ 1413 1414 /* 1415 * SEEK_HOLE cases: 1416 * H1. offs == start: start is in a hole 1417 * If this happens here, a hole has been dug behind our back 1418 * since the previous lseek(). 1419 * H2. offs > start: either start is in data, next hole at offs, 1420 * or start is in trailing hole, EOF at offs 1421 * Linux treats trailing holes like any other hole: offs == 1422 * start. Solaris seeks to EOF instead: offs > start (blech). 1423 * If that happens here, a hole has been dug behind our back 1424 * since the previous lseek(). 1425 * H3. offs < 0, errno = ENXIO: start is beyond EOF 1426 * If this happens, the file has been truncated behind our 1427 * back since we opened it. Treat it like a trailing hole. 1428 * H4. offs < 0, errno != ENXIO: we learned nothing 1429 * Pretend we know nothing at all, i.e. "forget" about D1. 1430 */ 1431 offs = glfs_lseek(s->fd, start, SEEK_HOLE); 1432 if (offs < 0) { 1433 return -errno; /* D1 and (H3 or H4) */ 1434 } 1435 1436 if (offs < start) { 1437 /* This is not a valid return by lseek(). We are safe to just return 1438 * -EIO in this case, and we'll treat it like H4. Unfortunately some 1439 * versions of gluster server will return offs < start, so an assert 1440 * here will unnecessarily abort QEMU. */ 1441 return -EIO; 1442 } 1443 1444 if (offs > start) { 1445 /* 1446 * D1 and H2: either in data, next hole at offs, or it was in 1447 * data but is now in a trailing hole. In the latter case, 1448 * all bets are off. Treating it as if it there was data all 1449 * the way to EOF is safe, so simply do that. 1450 */ 1451 *data = start; 1452 *hole = offs; 1453 return 0; 1454 } 1455 1456 /* D1 and H1 */ 1457 return -EBUSY; 1458 #endif 1459 1460 exit: 1461 return -ENOTSUP; 1462 } 1463 1464 /* 1465 * Returns the allocation status of the specified offset. 1466 * 1467 * The block layer guarantees 'offset' and 'bytes' are within bounds. 1468 * 1469 * 'pnum' is set to the number of bytes (including and immediately following 1470 * the specified offset) that are known to be in the same 1471 * allocated/unallocated state. 1472 * 1473 * 'bytes' is the max value 'pnum' should be set to. 1474 * 1475 * (Based on raw_co_block_status() from file-posix.c.) 1476 */ 1477 static int coroutine_fn qemu_gluster_co_block_status(BlockDriverState *bs, 1478 bool want_zero, 1479 int64_t offset, 1480 int64_t bytes, 1481 int64_t *pnum, 1482 int64_t *map, 1483 BlockDriverState **file) 1484 { 1485 BDRVGlusterState *s = bs->opaque; 1486 off_t data = 0, hole = 0; 1487 int ret = -EINVAL; 1488 1489 if (!s->fd) { 1490 return ret; 1491 } 1492 1493 if (!want_zero) { 1494 *pnum = bytes; 1495 *map = offset; 1496 *file = bs; 1497 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 1498 } 1499 1500 ret = find_allocation(bs, offset, &data, &hole); 1501 if (ret == -ENXIO) { 1502 /* Trailing hole */ 1503 *pnum = bytes; 1504 ret = BDRV_BLOCK_ZERO; 1505 } else if (ret < 0) { 1506 /* No info available, so pretend there are no holes */ 1507 *pnum = bytes; 1508 ret = BDRV_BLOCK_DATA; 1509 } else if (data == offset) { 1510 /* On a data extent, compute bytes to the end of the extent, 1511 * possibly including a partial sector at EOF. */ 1512 *pnum = MIN(bytes, hole - offset); 1513 ret = BDRV_BLOCK_DATA; 1514 } else { 1515 /* On a hole, compute bytes to the beginning of the next extent. */ 1516 assert(hole == offset); 1517 *pnum = MIN(bytes, data - offset); 1518 ret = BDRV_BLOCK_ZERO; 1519 } 1520 1521 *map = offset; 1522 *file = bs; 1523 1524 return ret | BDRV_BLOCK_OFFSET_VALID; 1525 } 1526 1527 1528 static const char *const gluster_strong_open_opts[] = { 1529 GLUSTER_OPT_VOLUME, 1530 GLUSTER_OPT_PATH, 1531 GLUSTER_OPT_TYPE, 1532 GLUSTER_OPT_SERVER_PATTERN, 1533 GLUSTER_OPT_HOST, 1534 GLUSTER_OPT_PORT, 1535 GLUSTER_OPT_TO, 1536 GLUSTER_OPT_IPV4, 1537 GLUSTER_OPT_IPV6, 1538 GLUSTER_OPT_SOCKET, 1539 1540 NULL 1541 }; 1542 1543 static BlockDriver bdrv_gluster = { 1544 .format_name = "gluster", 1545 .protocol_name = "gluster", 1546 .instance_size = sizeof(BDRVGlusterState), 1547 .bdrv_needs_filename = false, 1548 .bdrv_file_open = qemu_gluster_open, 1549 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, 1550 .bdrv_reopen_commit = qemu_gluster_reopen_commit, 1551 .bdrv_reopen_abort = qemu_gluster_reopen_abort, 1552 .bdrv_close = qemu_gluster_close, 1553 .bdrv_co_create = qemu_gluster_co_create, 1554 .bdrv_co_create_opts = qemu_gluster_co_create_opts, 1555 .bdrv_getlength = qemu_gluster_getlength, 1556 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, 1557 .bdrv_co_truncate = qemu_gluster_co_truncate, 1558 .bdrv_co_readv = qemu_gluster_co_readv, 1559 .bdrv_co_writev = qemu_gluster_co_writev, 1560 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, 1561 #ifdef CONFIG_GLUSTERFS_DISCARD 1562 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, 1563 #endif 1564 #ifdef CONFIG_GLUSTERFS_ZEROFILL 1565 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, 1566 #endif 1567 .bdrv_co_block_status = qemu_gluster_co_block_status, 1568 .bdrv_refresh_limits = qemu_gluster_refresh_limits, 1569 .create_opts = &qemu_gluster_create_opts, 1570 .strong_runtime_opts = gluster_strong_open_opts, 1571 }; 1572 1573 static BlockDriver bdrv_gluster_tcp = { 1574 .format_name = "gluster", 1575 .protocol_name = "gluster+tcp", 1576 .instance_size = sizeof(BDRVGlusterState), 1577 .bdrv_needs_filename = false, 1578 .bdrv_file_open = qemu_gluster_open, 1579 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, 1580 .bdrv_reopen_commit = qemu_gluster_reopen_commit, 1581 .bdrv_reopen_abort = qemu_gluster_reopen_abort, 1582 .bdrv_close = qemu_gluster_close, 1583 .bdrv_co_create = qemu_gluster_co_create, 1584 .bdrv_co_create_opts = qemu_gluster_co_create_opts, 1585 .bdrv_getlength = qemu_gluster_getlength, 1586 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, 1587 .bdrv_co_truncate = qemu_gluster_co_truncate, 1588 .bdrv_co_readv = qemu_gluster_co_readv, 1589 .bdrv_co_writev = qemu_gluster_co_writev, 1590 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, 1591 #ifdef CONFIG_GLUSTERFS_DISCARD 1592 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, 1593 #endif 1594 #ifdef CONFIG_GLUSTERFS_ZEROFILL 1595 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, 1596 #endif 1597 .bdrv_co_block_status = qemu_gluster_co_block_status, 1598 .bdrv_refresh_limits = qemu_gluster_refresh_limits, 1599 .create_opts = &qemu_gluster_create_opts, 1600 .strong_runtime_opts = gluster_strong_open_opts, 1601 }; 1602 1603 static BlockDriver bdrv_gluster_unix = { 1604 .format_name = "gluster", 1605 .protocol_name = "gluster+unix", 1606 .instance_size = sizeof(BDRVGlusterState), 1607 .bdrv_needs_filename = true, 1608 .bdrv_file_open = qemu_gluster_open, 1609 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, 1610 .bdrv_reopen_commit = qemu_gluster_reopen_commit, 1611 .bdrv_reopen_abort = qemu_gluster_reopen_abort, 1612 .bdrv_close = qemu_gluster_close, 1613 .bdrv_co_create = qemu_gluster_co_create, 1614 .bdrv_co_create_opts = qemu_gluster_co_create_opts, 1615 .bdrv_getlength = qemu_gluster_getlength, 1616 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, 1617 .bdrv_co_truncate = qemu_gluster_co_truncate, 1618 .bdrv_co_readv = qemu_gluster_co_readv, 1619 .bdrv_co_writev = qemu_gluster_co_writev, 1620 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, 1621 #ifdef CONFIG_GLUSTERFS_DISCARD 1622 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, 1623 #endif 1624 #ifdef CONFIG_GLUSTERFS_ZEROFILL 1625 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, 1626 #endif 1627 .bdrv_co_block_status = qemu_gluster_co_block_status, 1628 .bdrv_refresh_limits = qemu_gluster_refresh_limits, 1629 .create_opts = &qemu_gluster_create_opts, 1630 .strong_runtime_opts = gluster_strong_open_opts, 1631 }; 1632 1633 /* rdma is deprecated (actually never supported for volfile fetch). 1634 * Let's maintain it for the protocol compatibility, to make sure things 1635 * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp 1636 * protocol with a warning. 1637 * TODO: remove gluster+rdma interface support 1638 */ 1639 static BlockDriver bdrv_gluster_rdma = { 1640 .format_name = "gluster", 1641 .protocol_name = "gluster+rdma", 1642 .instance_size = sizeof(BDRVGlusterState), 1643 .bdrv_needs_filename = true, 1644 .bdrv_file_open = qemu_gluster_open, 1645 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, 1646 .bdrv_reopen_commit = qemu_gluster_reopen_commit, 1647 .bdrv_reopen_abort = qemu_gluster_reopen_abort, 1648 .bdrv_close = qemu_gluster_close, 1649 .bdrv_co_create = qemu_gluster_co_create, 1650 .bdrv_co_create_opts = qemu_gluster_co_create_opts, 1651 .bdrv_getlength = qemu_gluster_getlength, 1652 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, 1653 .bdrv_co_truncate = qemu_gluster_co_truncate, 1654 .bdrv_co_readv = qemu_gluster_co_readv, 1655 .bdrv_co_writev = qemu_gluster_co_writev, 1656 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, 1657 #ifdef CONFIG_GLUSTERFS_DISCARD 1658 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, 1659 #endif 1660 #ifdef CONFIG_GLUSTERFS_ZEROFILL 1661 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, 1662 #endif 1663 .bdrv_co_block_status = qemu_gluster_co_block_status, 1664 .bdrv_refresh_limits = qemu_gluster_refresh_limits, 1665 .create_opts = &qemu_gluster_create_opts, 1666 .strong_runtime_opts = gluster_strong_open_opts, 1667 }; 1668 1669 static void bdrv_gluster_init(void) 1670 { 1671 bdrv_register(&bdrv_gluster_rdma); 1672 bdrv_register(&bdrv_gluster_unix); 1673 bdrv_register(&bdrv_gluster_tcp); 1674 bdrv_register(&bdrv_gluster); 1675 } 1676 1677 block_init(bdrv_gluster_init); 1678