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