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