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