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