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