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