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