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