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