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