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