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