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