blob: ad5fadbe793a217c782b4f66986380e31fb88d1e [file] [log] [blame]
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301/*
2 * GlusterFS backend for QEMU
3 *
4 * Copyright (C) 2012 Bharata B Rao <bharata@linux.vnet.ibm.com>
5 *
Bharata B Rao85c09bc2014-01-29 19:59:55 +05306 * 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.
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05308 *
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05309 */
Markus Armbruster922a01a2018-02-01 12:18:46 +010010
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
Stefano Garzarellade23e722019-03-28 11:52:27 +010012#include "qemu/units.h"
Bharata B Rao8d6d89c2012-09-27 19:30:32 +053013#include <glusterfs/api/glfs.h>
Markus Armbrustere2c1c342022-12-21 14:35:49 +010014#include "block/block-io.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010015#include "block/block_int.h"
Max Reitz609f45e2018-06-14 21:14:28 +020016#include "block/qdict.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010018#include "qapi/qmp/qdict.h"
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +053019#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010020#include "qemu/uri.h"
Prasanna Kumar Kalever0552ff22016-07-19 22:27:31 +053021#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020022#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010023#include "qemu/option.h"
Prasanna Kumar Kaleverc56ac332016-08-09 14:20:09 +053024#include "qemu/cutils.h"
Bharata B Rao8d6d89c2012-09-27 19:30:32 +053025
Prasanna Kumar Kalevere014dbe2019-03-05 16:46:33 +010026#ifdef CONFIG_GLUSTERFS_FTRUNCATE_HAS_STAT
27# define glfs_ftruncate(fd, offset) glfs_ftruncate(fd, offset, NULL, NULL)
28#endif
29
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053030#define GLUSTER_OPT_FILENAME "filename"
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +053031#define GLUSTER_OPT_VOLUME "volume"
32#define GLUSTER_OPT_PATH "path"
33#define GLUSTER_OPT_TYPE "type"
34#define GLUSTER_OPT_SERVER_PATTERN "server."
35#define GLUSTER_OPT_HOST "host"
36#define GLUSTER_OPT_PORT "port"
37#define GLUSTER_OPT_TO "to"
38#define GLUSTER_OPT_IPV4 "ipv4"
39#define GLUSTER_OPT_IPV6 "ipv6"
40#define GLUSTER_OPT_SOCKET "socket"
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053041#define GLUSTER_OPT_DEBUG "debug"
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +053042#define GLUSTER_DEFAULT_PORT 24007
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053043#define GLUSTER_DEBUG_DEFAULT 4
44#define GLUSTER_DEBUG_MAX 9
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +053045#define GLUSTER_OPT_LOGFILE "logfile"
46#define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */
Stefano Garzarellade23e722019-03-28 11:52:27 +010047/*
48 * Several versions of GlusterFS (3.12? -> 6.0.1) fail when the transfer size
49 * is greater or equal to 1024 MiB, so we are limiting the transfer size to 512
50 * MiB to avoid this rare issue.
51 */
52#define GLUSTER_MAX_TRANSFER (512 * MiB)
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053053
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +053054#define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n"
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053055
Bharata B Rao8d6d89c2012-09-27 19:30:32 +053056typedef struct GlusterAIOCB {
Bharata B Rao8d6d89c2012-09-27 19:30:32 +053057 int64_t size;
58 int ret;
Bharata B Rao15744b02013-12-21 14:51:24 +053059 Coroutine *coroutine;
Stefan Hajnoczi6ee50af2014-05-08 16:34:41 +020060 AioContext *aio_context;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +053061} GlusterAIOCB;
62
63typedef struct BDRVGlusterState {
64 struct glfs *glfs;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +053065 struct glfs_fd *fd;
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +053066 char *logfile;
Niels de Vos947eb202016-03-10 19:38:00 +010067 bool supports_seek_data;
Prasanna Kumar Kalever1a417e42016-11-02 22:20:36 +053068 int debug;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +053069} BDRVGlusterState;
70
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053071typedef struct BDRVGlusterReopenState {
72 struct glfs *glfs;
73 struct glfs_fd *fd;
74} BDRVGlusterReopenState;
75
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053076
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +053077typedef struct GlfsPreopened {
78 char *volume;
79 glfs_t *fs;
80 int ref;
81} GlfsPreopened;
82
83typedef struct ListElement {
84 QLIST_ENTRY(ListElement) list;
85 GlfsPreopened saved;
86} ListElement;
87
Paolo Bonzinib58deb32018-12-06 11:58:10 +010088static QLIST_HEAD(, ListElement) glfs_list;
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +053089
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +053090static QemuOptsList qemu_gluster_create_opts = {
91 .name = "qemu-gluster-create-opts",
92 .head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head),
93 .desc = {
94 {
95 .name = BLOCK_OPT_SIZE,
96 .type = QEMU_OPT_SIZE,
97 .help = "Virtual disk size"
98 },
99 {
100 .name = BLOCK_OPT_PREALLOC,
101 .type = QEMU_OPT_STRING,
Stefano Garzarella2ea8e962019-05-24 09:58:48 +0200102 .help = "Preallocation mode (allowed values: off"
103#ifdef CONFIG_GLUSTERFS_FALLOCATE
104 ", falloc"
105#endif
106#ifdef CONFIG_GLUSTERFS_ZEROFILL
107 ", full"
108#endif
109 ")"
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +0530110 },
111 {
112 .name = GLUSTER_OPT_DEBUG,
113 .type = QEMU_OPT_NUMBER,
114 .help = "Gluster log level, valid range is 0-9",
115 },
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530116 {
117 .name = GLUSTER_OPT_LOGFILE,
118 .type = QEMU_OPT_STRING,
119 .help = "Logfile path of libgfapi",
120 },
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +0530121 { /* end of list */ }
122 }
123};
124
125static QemuOptsList runtime_opts = {
126 .name = "gluster",
127 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
128 .desc = {
129 {
130 .name = GLUSTER_OPT_FILENAME,
131 .type = QEMU_OPT_STRING,
132 .help = "URL to the gluster image",
133 },
134 {
135 .name = GLUSTER_OPT_DEBUG,
136 .type = QEMU_OPT_NUMBER,
137 .help = "Gluster log level, valid range is 0-9",
138 },
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530139 {
140 .name = GLUSTER_OPT_LOGFILE,
141 .type = QEMU_OPT_STRING,
142 .help = "Logfile path of libgfapi",
143 },
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +0530144 { /* end of list */ }
145 },
146};
147
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530148static QemuOptsList runtime_json_opts = {
149 .name = "gluster_json",
150 .head = QTAILQ_HEAD_INITIALIZER(runtime_json_opts.head),
151 .desc = {
152 {
153 .name = GLUSTER_OPT_VOLUME,
154 .type = QEMU_OPT_STRING,
155 .help = "name of gluster volume where VM image resides",
156 },
157 {
158 .name = GLUSTER_OPT_PATH,
159 .type = QEMU_OPT_STRING,
160 .help = "absolute path to image file in gluster volume",
161 },
162 {
163 .name = GLUSTER_OPT_DEBUG,
164 .type = QEMU_OPT_NUMBER,
165 .help = "Gluster log level, valid range is 0-9",
166 },
167 { /* end of list */ }
168 },
169};
170
171static QemuOptsList runtime_type_opts = {
172 .name = "gluster_type",
173 .head = QTAILQ_HEAD_INITIALIZER(runtime_type_opts.head),
174 .desc = {
175 {
176 .name = GLUSTER_OPT_TYPE,
177 .type = QEMU_OPT_STRING,
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100178 .help = "inet|unix",
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530179 },
180 { /* end of list */ }
181 },
182};
183
184static QemuOptsList runtime_unix_opts = {
185 .name = "gluster_unix",
186 .head = QTAILQ_HEAD_INITIALIZER(runtime_unix_opts.head),
187 .desc = {
188 {
189 .name = GLUSTER_OPT_SOCKET,
190 .type = QEMU_OPT_STRING,
Kevin Wolf9dae6352018-04-03 13:08:10 +0200191 .help = "socket file path (legacy)",
192 },
193 {
194 .name = GLUSTER_OPT_PATH,
195 .type = QEMU_OPT_STRING,
196 .help = "socket file path (QAPI)",
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530197 },
198 { /* end of list */ }
199 },
200};
201
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100202static QemuOptsList runtime_inet_opts = {
203 .name = "gluster_inet",
204 .head = QTAILQ_HEAD_INITIALIZER(runtime_inet_opts.head),
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530205 .desc = {
206 {
207 .name = GLUSTER_OPT_TYPE,
208 .type = QEMU_OPT_STRING,
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100209 .help = "inet|unix",
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530210 },
211 {
212 .name = GLUSTER_OPT_HOST,
213 .type = QEMU_OPT_STRING,
214 .help = "host address (hostname/ipv4/ipv6 addresses)",
215 },
216 {
217 .name = GLUSTER_OPT_PORT,
Prasanna Kumar Kalever53d98372016-08-09 15:18:14 +0530218 .type = QEMU_OPT_STRING,
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530219 .help = "port number on which glusterd is listening (default 24007)",
220 },
221 {
222 .name = "to",
223 .type = QEMU_OPT_NUMBER,
224 .help = "max port number, not supported by gluster",
225 },
226 {
227 .name = "ipv4",
228 .type = QEMU_OPT_BOOL,
229 .help = "ipv4 bool value, not supported by gluster",
230 },
231 {
232 .name = "ipv6",
233 .type = QEMU_OPT_BOOL,
234 .help = "ipv6 bool value, not supported by gluster",
235 },
236 { /* end of list */ }
237 },
238};
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +0530239
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530240static void glfs_set_preopened(const char *volume, glfs_t *fs)
241{
242 ListElement *entry = NULL;
243
244 entry = g_new(ListElement, 1);
245
246 entry->saved.volume = g_strdup(volume);
247
248 entry->saved.fs = fs;
249 entry->saved.ref = 1;
250
251 QLIST_INSERT_HEAD(&glfs_list, entry, list);
252}
253
254static glfs_t *glfs_find_preopened(const char *volume)
255{
256 ListElement *entry = NULL;
257
258 QLIST_FOREACH(entry, &glfs_list, list) {
259 if (strcmp(entry->saved.volume, volume) == 0) {
260 entry->saved.ref++;
261 return entry->saved.fs;
262 }
263 }
264
265 return NULL;
266}
267
268static void glfs_clear_preopened(glfs_t *fs)
269{
270 ListElement *entry = NULL;
Kevin Wolf668c0e42016-11-17 11:30:08 +0100271 ListElement *next;
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530272
273 if (fs == NULL) {
274 return;
275 }
276
Kevin Wolf668c0e42016-11-17 11:30:08 +0100277 QLIST_FOREACH_SAFE(entry, &glfs_list, list, next) {
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530278 if (entry->saved.fs == fs) {
279 if (--entry->saved.ref) {
280 return;
281 }
282
283 QLIST_REMOVE(entry, list);
284
285 glfs_fini(entry->saved.fs);
286 g_free(entry->saved.volume);
287 g_free(entry);
288 }
289 }
290}
291
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530292static int parse_volume_options(BlockdevOptionsGluster *gconf, char *path)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530293{
294 char *p, *q;
295
296 if (!path) {
297 return -EINVAL;
298 }
299
300 /* volume */
301 p = q = path + strspn(path, "/");
302 p += strcspn(p, "/");
303 if (*p == '\0') {
304 return -EINVAL;
305 }
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530306 gconf->volume = g_strndup(q, p - q);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530307
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530308 /* path */
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530309 p += strspn(p, "/");
310 if (*p == '\0') {
311 return -EINVAL;
312 }
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530313 gconf->path = g_strdup(p);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530314 return 0;
315}
316
317/*
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530318 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...]
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530319 *
320 * 'gluster' is the protocol.
321 *
322 * 'transport' specifies the transport type used to connect to gluster
323 * management daemon (glusterd). Valid transport types are
Prasanna Kumar Kalever0552ff22016-07-19 22:27:31 +0530324 * tcp or unix. If a transport type isn't specified, then tcp type is assumed.
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530325 *
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530326 * 'host' specifies the host where the volume file specification for
Prasanna Kumar Kalever0552ff22016-07-19 22:27:31 +0530327 * the given volume resides. This can be either hostname or ipv4 address.
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530328 * If transport type is 'unix', then 'host' field should not be specified.
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530329 * The 'socket' field needs to be populated with the path to unix domain
330 * socket.
331 *
332 * 'port' is the port number on which glusterd is listening. This is optional
333 * and if not specified, QEMU will send 0 which will make gluster to use the
334 * default port. If the transport type is unix, then 'port' should not be
335 * specified.
336 *
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530337 * 'volume' is the name of the gluster volume which contains the VM image.
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530338 *
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530339 * 'path' is the path to the actual VM image that resides on gluster volume.
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530340 *
341 * Examples:
342 *
343 * file=gluster://1.2.3.4/testvol/a.img
344 * file=gluster+tcp://1.2.3.4/testvol/a.img
345 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530346 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530347 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530348 */
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530349static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf,
350 const char *filename)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530351{
Markus Armbruster62cf3962017-04-26 09:36:40 +0200352 SocketAddress *gsconf;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530353 URI *uri;
354 QueryParams *qp = NULL;
355 bool is_unix = false;
356 int ret = 0;
357
358 uri = uri_parse(filename);
359 if (!uri) {
360 return -EINVAL;
361 }
362
Eric Blake54aa3de2020-11-12 19:13:37 -0600363 gsconf = g_new0(SocketAddress, 1);
364 QAPI_LIST_PREPEND(gconf->server, gsconf);
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530365
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530366 /* transport */
Paolo Bonzini24897a72014-02-17 14:43:54 +0100367 if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
Markus Armbruster62cf3962017-04-26 09:36:40 +0200368 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530369 } else if (!strcmp(uri->scheme, "gluster+tcp")) {
Markus Armbruster62cf3962017-04-26 09:36:40 +0200370 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530371 } else if (!strcmp(uri->scheme, "gluster+unix")) {
Markus Armbruster62cf3962017-04-26 09:36:40 +0200372 gsconf->type = SOCKET_ADDRESS_TYPE_UNIX;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530373 is_unix = true;
374 } else if (!strcmp(uri->scheme, "gluster+rdma")) {
Markus Armbruster62cf3962017-04-26 09:36:40 +0200375 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
Alistair Francis3dc6f862017-07-12 06:57:41 -0700376 warn_report("rdma feature is not supported, falling back to tcp");
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530377 } else {
378 ret = -EINVAL;
379 goto out;
380 }
381
382 ret = parse_volume_options(gconf, uri->path);
383 if (ret < 0) {
384 goto out;
385 }
386
387 qp = query_params_parse(uri->query);
388 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
389 ret = -EINVAL;
390 goto out;
391 }
392
393 if (is_unix) {
394 if (uri->server || uri->port) {
395 ret = -EINVAL;
396 goto out;
397 }
398 if (strcmp(qp->p[0].name, "socket")) {
399 ret = -EINVAL;
400 goto out;
401 }
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530402 gsconf->u.q_unix.path = g_strdup(qp->p[0].value);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530403 } else {
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100404 gsconf->u.inet.host = g_strdup(uri->server ? uri->server : "localhost");
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530405 if (uri->port) {
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100406 gsconf->u.inet.port = g_strdup_printf("%d", uri->port);
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530407 } else {
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100408 gsconf->u.inet.port = g_strdup_printf("%d", GLUSTER_DEFAULT_PORT);
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530409 }
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530410 }
411
412out:
413 if (qp) {
414 query_params_free(qp);
415 }
416 uri_free(uri);
417 return ret;
418}
419
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530420static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
421 Error **errp)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530422{
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530423 struct glfs *glfs;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530424 int ret;
425 int old_errno;
Markus Armbruster62cf3962017-04-26 09:36:40 +0200426 SocketAddressList *server;
Eric Blakebd1386c2023-05-22 14:04:29 -0500427 uint64_t port;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530428
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530429 glfs = glfs_find_preopened(gconf->volume);
430 if (glfs) {
431 return glfs;
432 }
433
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530434 glfs = glfs_new(gconf->volume);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530435 if (!glfs) {
436 goto out;
437 }
438
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530439 glfs_set_preopened(gconf->volume, glfs);
440
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530441 for (server = gconf->server; server; server = server->next) {
Markus Armbrusterfce5d532017-03-30 19:43:13 +0200442 switch (server->value->type) {
Markus Armbruster62cf3962017-04-26 09:36:40 +0200443 case SOCKET_ADDRESS_TYPE_UNIX:
Markus Armbrusterfc294582017-03-06 20:00:44 +0100444 ret = glfs_set_volfile_server(glfs, "unix",
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530445 server->value->u.q_unix.path, 0);
Markus Armbrusterfce5d532017-03-30 19:43:13 +0200446 break;
Markus Armbruster62cf3962017-04-26 09:36:40 +0200447 case SOCKET_ADDRESS_TYPE_INET:
Eric Blakebd1386c2023-05-22 14:04:29 -0500448 if (parse_uint_full(server->value->u.inet.port, 10, &port) < 0 ||
Prasanna Kumar Kaleverc56ac332016-08-09 14:20:09 +0530449 port > 65535) {
450 error_setg(errp, "'%s' is not a valid port number",
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100451 server->value->u.inet.port);
Prasanna Kumar Kaleverc56ac332016-08-09 14:20:09 +0530452 errno = EINVAL;
453 goto out;
454 }
Markus Armbrusterfc294582017-03-06 20:00:44 +0100455 ret = glfs_set_volfile_server(glfs, "tcp",
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100456 server->value->u.inet.host,
Prasanna Kumar Kaleverc56ac332016-08-09 14:20:09 +0530457 (int)port);
Markus Armbrusterfce5d532017-03-30 19:43:13 +0200458 break;
Markus Armbruster62cf3962017-04-26 09:36:40 +0200459 case SOCKET_ADDRESS_TYPE_VSOCK:
460 case SOCKET_ADDRESS_TYPE_FD:
Markus Armbrusterfce5d532017-03-30 19:43:13 +0200461 default:
462 abort();
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530463 }
464
465 if (ret < 0) {
466 goto out;
467 }
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530468 }
469
Prasanna Kumar Kalever1a417e42016-11-02 22:20:36 +0530470 ret = glfs_set_logging(glfs, gconf->logfile, gconf->debug);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530471 if (ret < 0) {
472 goto out;
473 }
474
475 ret = glfs_init(glfs);
476 if (ret) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530477 error_setg(errp, "Gluster connection for volume %s, path %s failed"
478 " to connect", gconf->volume, gconf->path);
479 for (server = gconf->server; server; server = server->next) {
Markus Armbruster62cf3962017-04-26 09:36:40 +0200480 if (server->value->type == SOCKET_ADDRESS_TYPE_UNIX) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530481 error_append_hint(errp, "hint: failed on socket %s ",
482 server->value->u.q_unix.path);
483 } else {
484 error_append_hint(errp, "hint: failed on host %s and port %s ",
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100485 server->value->u.inet.host,
486 server->value->u.inet.port);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530487 }
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530488 }
Peter Krempa45571172014-05-09 12:08:10 +0200489
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530490 error_append_hint(errp, "Please refer to gluster logs for more info\n");
491
Peter Krempa45571172014-05-09 12:08:10 +0200492 /* glfs_init sometimes doesn't set errno although docs suggest that */
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530493 if (errno == 0) {
Peter Krempa45571172014-05-09 12:08:10 +0200494 errno = EINVAL;
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530495 }
Peter Krempa45571172014-05-09 12:08:10 +0200496
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530497 goto out;
498 }
499 return glfs;
500
501out:
502 if (glfs) {
503 old_errno = errno;
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530504 glfs_clear_preopened(glfs);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530505 errno = old_errno;
506 }
507 return NULL;
508}
509
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530510/*
511 * Convert the json formatted command line into qapi.
512*/
513static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
514 QDict *options, Error **errp)
515{
516 QemuOpts *opts;
Markus Armbruster62cf3962017-04-26 09:36:40 +0200517 SocketAddress *gsconf = NULL;
Eric Blake95b3a8c2021-01-13 16:10:13 -0600518 SocketAddressList **tail;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530519 QDict *backing_options = NULL;
520 Error *local_err = NULL;
521 char *str = NULL;
522 const char *ptr;
Peter Maydell56faeb92017-06-05 18:01:38 +0100523 int i, type, num_servers;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530524
525 /* create opts info from runtime_json_opts list */
526 opts = qemu_opts_create(&runtime_json_opts, NULL, 0, &error_abort);
Markus Armbrustera5f9b9d2020-07-07 18:06:05 +0200527 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530528 goto out;
529 }
530
531 num_servers = qdict_array_entries(options, GLUSTER_OPT_SERVER_PATTERN);
532 if (num_servers < 1) {
533 error_setg(&local_err, QERR_MISSING_PARAMETER, "server");
534 goto out;
535 }
536
537 ptr = qemu_opt_get(opts, GLUSTER_OPT_VOLUME);
538 if (!ptr) {
539 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_VOLUME);
540 goto out;
541 }
542 gconf->volume = g_strdup(ptr);
543
544 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
545 if (!ptr) {
546 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_PATH);
547 goto out;
548 }
549 gconf->path = g_strdup(ptr);
550 qemu_opts_del(opts);
Eric Blake95b3a8c2021-01-13 16:10:13 -0600551 tail = &gconf->server;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530552
553 for (i = 0; i < num_servers; i++) {
554 str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
555 qdict_extract_subqdict(options, &backing_options, str);
556
557 /* create opts info from runtime_type_opts list */
558 opts = qemu_opts_create(&runtime_type_opts, NULL, 0, &error_abort);
Markus Armbrustera5f9b9d2020-07-07 18:06:05 +0200559 if (!qemu_opts_absorb_qdict(opts, backing_options, errp)) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530560 goto out;
561 }
562
563 ptr = qemu_opt_get(opts, GLUSTER_OPT_TYPE);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530564 if (!ptr) {
565 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_TYPE);
566 error_append_hint(&local_err, GERR_INDEX_HINT, i);
567 goto out;
568
569 }
Markus Armbruster62cf3962017-04-26 09:36:40 +0200570 gsconf = g_new0(SocketAddress, 1);
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100571 if (!strcmp(ptr, "tcp")) {
572 ptr = "inet"; /* accept legacy "tcp" */
573 }
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200574 type = qapi_enum_parse(&SocketAddressType_lookup, ptr, -1, NULL);
Markus Armbruster62cf3962017-04-26 09:36:40 +0200575 if (type != SOCKET_ADDRESS_TYPE_INET
576 && type != SOCKET_ADDRESS_TYPE_UNIX) {
Markus Armbrusterfce5d532017-03-30 19:43:13 +0200577 error_setg(&local_err,
578 "Parameter '%s' may be 'inet' or 'unix'",
579 GLUSTER_OPT_TYPE);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530580 error_append_hint(&local_err, GERR_INDEX_HINT, i);
581 goto out;
582 }
Markus Armbrusterfce5d532017-03-30 19:43:13 +0200583 gsconf->type = type;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530584 qemu_opts_del(opts);
585
Markus Armbruster62cf3962017-04-26 09:36:40 +0200586 if (gsconf->type == SOCKET_ADDRESS_TYPE_INET) {
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100587 /* create opts info from runtime_inet_opts list */
588 opts = qemu_opts_create(&runtime_inet_opts, NULL, 0, &error_abort);
Markus Armbrustera5f9b9d2020-07-07 18:06:05 +0200589 if (!qemu_opts_absorb_qdict(opts, backing_options, errp)) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530590 goto out;
591 }
592
593 ptr = qemu_opt_get(opts, GLUSTER_OPT_HOST);
594 if (!ptr) {
595 error_setg(&local_err, QERR_MISSING_PARAMETER,
596 GLUSTER_OPT_HOST);
597 error_append_hint(&local_err, GERR_INDEX_HINT, i);
598 goto out;
599 }
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100600 gsconf->u.inet.host = g_strdup(ptr);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530601 ptr = qemu_opt_get(opts, GLUSTER_OPT_PORT);
602 if (!ptr) {
603 error_setg(&local_err, QERR_MISSING_PARAMETER,
604 GLUSTER_OPT_PORT);
605 error_append_hint(&local_err, GERR_INDEX_HINT, i);
606 goto out;
607 }
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100608 gsconf->u.inet.port = g_strdup(ptr);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530609
610 /* defend for unsupported fields in InetSocketAddress,
611 * i.e. @ipv4, @ipv6 and @to
612 */
613 ptr = qemu_opt_get(opts, GLUSTER_OPT_TO);
614 if (ptr) {
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100615 gsconf->u.inet.has_to = true;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530616 }
617 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV4);
618 if (ptr) {
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100619 gsconf->u.inet.has_ipv4 = true;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530620 }
621 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV6);
622 if (ptr) {
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100623 gsconf->u.inet.has_ipv6 = true;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530624 }
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100625 if (gsconf->u.inet.has_to) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530626 error_setg(&local_err, "Parameter 'to' not supported");
627 goto out;
628 }
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100629 if (gsconf->u.inet.has_ipv4 || gsconf->u.inet.has_ipv6) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530630 error_setg(&local_err, "Parameters 'ipv4/ipv6' not supported");
631 goto out;
632 }
633 qemu_opts_del(opts);
634 } else {
635 /* create opts info from runtime_unix_opts list */
636 opts = qemu_opts_create(&runtime_unix_opts, NULL, 0, &error_abort);
Markus Armbrustera5f9b9d2020-07-07 18:06:05 +0200637 if (!qemu_opts_absorb_qdict(opts, backing_options, errp)) {
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530638 goto out;
639 }
640
Kevin Wolf9dae6352018-04-03 13:08:10 +0200641 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
642 if (!ptr) {
643 ptr = qemu_opt_get(opts, GLUSTER_OPT_SOCKET);
644 } else if (qemu_opt_get(opts, GLUSTER_OPT_SOCKET)) {
645 error_setg(&local_err,
646 "Conflicting parameters 'path' and 'socket'");
647 error_append_hint(&local_err, GERR_INDEX_HINT, i);
648 goto out;
649 }
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530650 if (!ptr) {
651 error_setg(&local_err, QERR_MISSING_PARAMETER,
Kevin Wolf9dae6352018-04-03 13:08:10 +0200652 GLUSTER_OPT_PATH);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530653 error_append_hint(&local_err, GERR_INDEX_HINT, i);
654 goto out;
655 }
656 gsconf->u.q_unix.path = g_strdup(ptr);
657 qemu_opts_del(opts);
658 }
659
Eric Blake95b3a8c2021-01-13 16:10:13 -0600660 QAPI_LIST_APPEND(tail, gsconf);
Markus Armbruster85a82e82017-03-06 20:00:46 +0100661 gsconf = NULL;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530662
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200663 qobject_unref(backing_options);
Markus Armbruster85a82e82017-03-06 20:00:46 +0100664 backing_options = NULL;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530665 g_free(str);
666 str = NULL;
667 }
668
669 return 0;
670
671out:
672 error_propagate(errp, local_err);
Markus Armbruster62cf3962017-04-26 09:36:40 +0200673 qapi_free_SocketAddress(gsconf);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530674 qemu_opts_del(opts);
Markus Armbruster85a82e82017-03-06 20:00:46 +0100675 g_free(str);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200676 qobject_unref(backing_options);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530677 errno = EINVAL;
678 return -errno;
679}
680
Kevin Wolfab8bda72018-01-31 16:27:38 +0100681/* Converts options given in @filename and the @options QDict into the QAPI
682 * object @gconf. */
683static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
684 const char *filename,
685 QDict *options, Error **errp)
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530686{
687 int ret;
688 if (filename) {
689 ret = qemu_gluster_parse_uri(gconf, filename);
690 if (ret < 0) {
Daniel P. Berrangé44acd462018-02-06 10:52:04 +0000691 error_setg(errp, "invalid URI %s", filename);
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530692 error_append_hint(errp, "Usage: file=gluster[+transport]://"
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530693 "[host[:port]]volume/path[?socket=...]"
694 "[,file.debug=N]"
695 "[,file.logfile=/path/filename.log]\n");
Kevin Wolfab8bda72018-01-31 16:27:38 +0100696 return ret;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530697 }
698 } else {
699 ret = qemu_gluster_parse_json(gconf, options, errp);
700 if (ret < 0) {
701 error_append_hint(errp, "Usage: "
702 "-drive driver=qcow2,file.driver=gluster,"
703 "file.volume=testvol,file.path=/path/a.qcow2"
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530704 "[,file.debug=9]"
705 "[,file.logfile=/path/filename.log],"
Markus Armbrusterc5f1ae32017-03-06 20:00:48 +0100706 "file.server.0.type=inet,"
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530707 "file.server.0.host=1.2.3.4,"
708 "file.server.0.port=24007,"
709 "file.server.1.transport=unix,"
Kevin Wolf9dae6352018-04-03 13:08:10 +0200710 "file.server.1.path=/var/run/glusterd.socket ..."
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530711 "\n");
Kevin Wolfab8bda72018-01-31 16:27:38 +0100712 return ret;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530713 }
Kevin Wolfab8bda72018-01-31 16:27:38 +0100714 }
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530715
Kevin Wolfab8bda72018-01-31 16:27:38 +0100716 return 0;
717}
718
719static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf,
720 const char *filename,
721 QDict *options, Error **errp)
722{
723 int ret;
724
725 ret = qemu_gluster_parse(gconf, filename, options, errp);
726 if (ret < 0) {
727 errno = -ret;
728 return NULL;
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530729 }
730
731 return qemu_gluster_glfs_init(gconf, errp);
732}
733
Bharata B Rao7c815372013-12-21 14:51:25 +0530734/*
735 * AIO callback routine called from GlusterFS thread.
736 */
Niels de Vos0e3b8912019-03-05 16:46:34 +0100737static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret,
738#ifdef CONFIG_GLUSTERFS_IOCB_HAS_STAT
739 struct glfs_stat *pre, struct glfs_stat *post,
740#endif
741 void *arg)
Bharata B Rao7c815372013-12-21 14:51:25 +0530742{
743 GlusterAIOCB *acb = (GlusterAIOCB *)arg;
744
745 if (!ret || ret == acb->size) {
746 acb->ret = 0; /* Success */
747 } else if (ret < 0) {
Jeff Codya8827452016-04-05 23:11:34 -0400748 acb->ret = -errno; /* Read/Write failed */
Bharata B Rao7c815372013-12-21 14:51:25 +0530749 } else {
750 acb->ret = -EIO; /* Partial read/write - fail it */
751 }
752
Paolo Bonzini19196312017-02-13 14:52:31 +0100753 aio_co_schedule(acb->aio_context, acb->coroutine);
Bharata B Rao7c815372013-12-21 14:51:25 +0530754}
755
Jeff Cody1b37b342014-02-17 11:11:11 -0500756static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags)
757{
758 assert(open_flags != NULL);
759
760 *open_flags |= O_BINARY;
761
762 if (bdrv_flags & BDRV_O_RDWR) {
763 *open_flags |= O_RDWR;
764 } else {
765 *open_flags |= O_RDONLY;
766 }
767
768 if ((bdrv_flags & BDRV_O_NOCACHE)) {
769 *open_flags |= O_DIRECT;
770 }
771}
772
Niels de Vos947eb202016-03-10 19:38:00 +0100773/*
774 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of
775 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used.
776 * - Corrected versions return -1 and set errno to EINVAL.
777 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set
778 * errno to ENXIO when SEEK_DATA is called with a position of EOF.
779 */
780static bool qemu_gluster_test_seek(struct glfs_fd *fd)
781{
Jeff Codyd9b78972016-10-07 17:48:12 -0400782 off_t ret = 0;
783
784#if defined SEEK_HOLE && defined SEEK_DATA
785 off_t eof;
Niels de Vos947eb202016-03-10 19:38:00 +0100786
787 eof = glfs_lseek(fd, 0, SEEK_END);
788 if (eof < 0) {
789 /* this should never occur */
790 return false;
791 }
792
793 /* this should always fail with ENXIO if SEEK_DATA is supported */
794 ret = glfs_lseek(fd, eof, SEEK_DATA);
Jeff Codyd9b78972016-10-07 17:48:12 -0400795#endif
796
Niels de Vos947eb202016-03-10 19:38:00 +0100797 return (ret < 0) && (errno == ENXIO);
798}
799
Kevin Wolf56d1b4d2013-04-12 20:02:37 +0200800static int qemu_gluster_open(BlockDriverState *bs, QDict *options,
Max Reitz015a1032013-09-05 14:22:29 +0200801 int bdrv_flags, Error **errp)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530802{
803 BDRVGlusterState *s = bs->opaque;
Jeff Cody1b37b342014-02-17 11:11:11 -0500804 int open_flags = 0;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530805 int ret = 0;
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530806 BlockdevOptionsGluster *gconf = NULL;
Kevin Wolfb4894772013-04-12 17:50:16 +0200807 QemuOpts *opts;
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530808 const char *filename, *logfile;
Kevin Wolfb4894772013-04-12 17:50:16 +0200809
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800810 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Markus Armbrusteraf175e82020-07-07 18:06:03 +0200811 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
Kevin Wolfb4894772013-04-12 17:50:16 +0200812 ret = -EINVAL;
813 goto out;
814 }
815
Jeff Cody7eac8682016-04-07 17:24:19 -0400816 filename = qemu_opt_get(opts, GLUSTER_OPT_FILENAME);
Kevin Wolfb4894772013-04-12 17:50:16 +0200817
Prasanna Kumar Kalever1a417e42016-11-02 22:20:36 +0530818 s->debug = qemu_opt_get_number(opts, GLUSTER_OPT_DEBUG,
819 GLUSTER_DEBUG_DEFAULT);
820 if (s->debug < 0) {
821 s->debug = 0;
822 } else if (s->debug > GLUSTER_DEBUG_MAX) {
823 s->debug = GLUSTER_DEBUG_MAX;
Jeff Cody7eac8682016-04-07 17:24:19 -0400824 }
825
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530826 gconf = g_new0(BlockdevOptionsGluster, 1);
Prasanna Kumar Kalever1a417e42016-11-02 22:20:36 +0530827 gconf->debug = s->debug;
828 gconf->has_debug = true;
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530829
830 logfile = qemu_opt_get(opts, GLUSTER_OPT_LOGFILE);
831 s->logfile = g_strdup(logfile ? logfile : GLUSTER_LOGFILE_DEFAULT);
832
833 gconf->logfile = g_strdup(s->logfile);
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530834
Prasanna Kumar Kalever6c7189b2016-07-19 22:27:33 +0530835 s->glfs = qemu_gluster_init(gconf, filename, options, errp);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530836 if (!s->glfs) {
837 ret = -errno;
838 goto out;
839 }
840
Jeff Codyd85fa9e2016-04-05 10:40:09 -0400841#ifdef CONFIG_GLUSTERFS_XLATOR_OPT
842 /* Without this, if fsync fails for a recoverable reason (for instance,
843 * ENOSPC), gluster will dump its cache, preventing retries. This means
844 * almost certain data loss. Not all gluster versions support the
845 * 'resync-failed-syncs-after-fsync' key value, but there is no way to
846 * discover during runtime if it is supported (this api returns success for
847 * unknown key/value pairs) */
848 ret = glfs_set_xlator_option(s->glfs, "*-write-behind",
849 "resync-failed-syncs-after-fsync",
850 "on");
851 if (ret < 0) {
852 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
853 ret = -errno;
854 goto out;
855 }
856#endif
857
Jeff Cody1b37b342014-02-17 11:11:11 -0500858 qemu_gluster_parse_flags(bdrv_flags, &open_flags);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530859
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530860 s->fd = glfs_open(s->glfs, gconf->path, open_flags);
Kevin Wolf54ea21b2018-10-08 17:27:18 +0200861 ret = s->fd ? 0 : -errno;
862
863 if (ret == -EACCES || ret == -EROFS) {
864 /* Try to degrade to read-only, but if it doesn't work, still use the
865 * normal error message. */
866 if (bdrv_apply_auto_read_only(bs, NULL, NULL) == 0) {
867 open_flags = (open_flags & ~O_RDWR) | O_RDONLY;
868 s->fd = glfs_open(s->glfs, gconf->path, open_flags);
869 ret = s->fd ? 0 : -errno;
870 }
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530871 }
872
Niels de Vos947eb202016-03-10 19:38:00 +0100873 s->supports_seek_data = qemu_gluster_test_seek(s->fd);
874
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530875out:
Kevin Wolfb4894772013-04-12 17:50:16 +0200876 qemu_opts_del(opts);
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530877 qapi_free_BlockdevOptionsGluster(gconf);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530878 if (!ret) {
879 return ret;
880 }
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530881 g_free(s->logfile);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530882 if (s->fd) {
883 glfs_close(s->fd);
884 }
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530885
886 glfs_clear_preopened(s->glfs);
887
Bharata B Rao8d6d89c2012-09-27 19:30:32 +0530888 return ret;
889}
890
Stefano Garzarellade23e722019-03-28 11:52:27 +0100891static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
892{
893 bs->bl.max_transfer = GLUSTER_MAX_TRANSFER;
Fabian Ebner9b38fc52022-05-20 09:59:22 +0200894 bs->bl.max_pdiscard = MIN(SIZE_MAX, INT64_MAX);
Stefano Garzarellade23e722019-03-28 11:52:27 +0100895}
896
Jeff Codyadccfbc2014-02-17 11:11:12 -0500897static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
898 BlockReopenQueue *queue, Error **errp)
899{
900 int ret = 0;
Jeff Cody7eac8682016-04-07 17:24:19 -0400901 BDRVGlusterState *s;
Jeff Codyadccfbc2014-02-17 11:11:12 -0500902 BDRVGlusterReopenState *reop_s;
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530903 BlockdevOptionsGluster *gconf;
Jeff Codyadccfbc2014-02-17 11:11:12 -0500904 int open_flags = 0;
905
906 assert(state != NULL);
907 assert(state->bs != NULL);
908
Jeff Cody7eac8682016-04-07 17:24:19 -0400909 s = state->bs->opaque;
910
Markus Armbruster5839e532014-08-19 10:31:08 +0200911 state->opaque = g_new0(BDRVGlusterReopenState, 1);
Jeff Codyadccfbc2014-02-17 11:11:12 -0500912 reop_s = state->opaque;
913
914 qemu_gluster_parse_flags(state->flags, &open_flags);
915
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530916 gconf = g_new0(BlockdevOptionsGluster, 1);
Prasanna Kumar Kalever1a417e42016-11-02 22:20:36 +0530917 gconf->debug = s->debug;
918 gconf->has_debug = true;
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +0530919 gconf->logfile = g_strdup(s->logfile);
Stefano Garzarella0b1847b2019-07-15 15:28:44 +0200920
921 /*
922 * If 'state->bs->exact_filename' is empty, 'state->options' should contain
923 * the JSON parameters already parsed.
924 */
925 if (state->bs->exact_filename[0] != '\0') {
926 reop_s->glfs = qemu_gluster_init(gconf, state->bs->exact_filename, NULL,
927 errp);
928 } else {
929 reop_s->glfs = qemu_gluster_init(gconf, NULL, state->options, errp);
930 }
Jeff Codyadccfbc2014-02-17 11:11:12 -0500931 if (reop_s->glfs == NULL) {
932 ret = -errno;
933 goto exit;
934 }
935
Jeff Codyd85fa9e2016-04-05 10:40:09 -0400936#ifdef CONFIG_GLUSTERFS_XLATOR_OPT
937 ret = glfs_set_xlator_option(reop_s->glfs, "*-write-behind",
938 "resync-failed-syncs-after-fsync", "on");
939 if (ret < 0) {
940 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
941 ret = -errno;
942 goto exit;
943 }
944#endif
945
Prasanna Kumar Kaleverd5cf4072016-07-19 22:27:29 +0530946 reop_s->fd = glfs_open(reop_s->glfs, gconf->path, open_flags);
Jeff Codyadccfbc2014-02-17 11:11:12 -0500947 if (reop_s->fd == NULL) {
948 /* reops->glfs will be cleaned up in _abort */
949 ret = -errno;
950 goto exit;
951 }
952
953exit:
954 /* state->opaque will be freed in either the _abort or _commit */
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +0530955 qapi_free_BlockdevOptionsGluster(gconf);
Jeff Codyadccfbc2014-02-17 11:11:12 -0500956 return ret;
957}
958
959static void qemu_gluster_reopen_commit(BDRVReopenState *state)
960{
961 BDRVGlusterReopenState *reop_s = state->opaque;
962 BDRVGlusterState *s = state->bs->opaque;
963
964
965 /* close the old */
966 if (s->fd) {
967 glfs_close(s->fd);
968 }
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530969
970 glfs_clear_preopened(s->glfs);
Jeff Codyadccfbc2014-02-17 11:11:12 -0500971
972 /* use the newly opened image / connection */
973 s->fd = reop_s->fd;
974 s->glfs = reop_s->glfs;
975
976 g_free(state->opaque);
977 state->opaque = NULL;
978
979 return;
980}
981
982
983static void qemu_gluster_reopen_abort(BDRVReopenState *state)
984{
985 BDRVGlusterReopenState *reop_s = state->opaque;
986
987 if (reop_s == NULL) {
988 return;
989 }
990
991 if (reop_s->fd) {
992 glfs_close(reop_s->fd);
993 }
994
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +0530995 glfs_clear_preopened(reop_s->glfs);
Jeff Codyadccfbc2014-02-17 11:11:12 -0500996
997 g_free(state->opaque);
998 state->opaque = NULL;
999
1000 return;
1001}
1002
Bharata B Rao7c815372013-12-21 14:51:25 +05301003#ifdef CONFIG_GLUSTERFS_ZEROFILL
Eric Blakee88a36e2016-06-01 15:10:08 -06001004static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs,
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +05301005 int64_t offset,
Vladimir Sementsov-Ogievskiyf34b2bc2021-09-03 13:28:03 +03001006 int64_t bytes,
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +05301007 BdrvRequestFlags flags)
Bharata B Rao7c815372013-12-21 14:51:25 +05301008{
1009 int ret;
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001010 GlusterAIOCB acb;
Bharata B Rao7c815372013-12-21 14:51:25 +05301011 BDRVGlusterState *s = bs->opaque;
Bharata B Rao7c815372013-12-21 14:51:25 +05301012
Vladimir Sementsov-Ogievskiyf34b2bc2021-09-03 13:28:03 +03001013 acb.size = bytes;
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001014 acb.ret = 0;
1015 acb.coroutine = qemu_coroutine_self();
1016 acb.aio_context = bdrv_get_aio_context(bs);
Bharata B Rao7c815372013-12-21 14:51:25 +05301017
Vladimir Sementsov-Ogievskiyf34b2bc2021-09-03 13:28:03 +03001018 ret = glfs_zerofill_async(s->fd, offset, bytes, gluster_finish_aiocb, &acb);
Bharata B Rao7c815372013-12-21 14:51:25 +05301019 if (ret < 0) {
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001020 return -errno;
Bharata B Rao7c815372013-12-21 14:51:25 +05301021 }
1022
1023 qemu_coroutine_yield();
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001024 return acb.ret;
Bharata B Rao7c815372013-12-21 14:51:25 +05301025}
1026#endif
1027
Max Reitz36e87902018-02-13 14:03:51 +01001028static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
1029 PreallocMode prealloc, Error **errp)
1030{
Max Reitzf3a33f72018-02-13 14:03:52 +01001031 int64_t current_length;
1032
1033 current_length = glfs_lseek(fd, 0, SEEK_END);
1034 if (current_length < 0) {
1035 error_setg_errno(errp, errno, "Failed to determine current size");
1036 return -errno;
1037 }
1038
1039 if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
1040 error_setg(errp, "Cannot use preallocation for shrinking files");
1041 return -ENOTSUP;
1042 }
1043
1044 if (current_length == offset) {
1045 return 0;
1046 }
1047
Max Reitz36e87902018-02-13 14:03:51 +01001048 switch (prealloc) {
1049#ifdef CONFIG_GLUSTERFS_FALLOCATE
1050 case PREALLOC_MODE_FALLOC:
Max Reitzf3a33f72018-02-13 14:03:52 +01001051 if (glfs_fallocate(fd, 0, current_length, offset - current_length)) {
Max Reitz36e87902018-02-13 14:03:51 +01001052 error_setg_errno(errp, errno, "Could not preallocate data");
1053 return -errno;
1054 }
1055 break;
1056#endif /* CONFIG_GLUSTERFS_FALLOCATE */
1057#ifdef CONFIG_GLUSTERFS_ZEROFILL
1058 case PREALLOC_MODE_FULL:
1059 if (glfs_ftruncate(fd, offset)) {
1060 error_setg_errno(errp, errno, "Could not resize file");
1061 return -errno;
1062 }
Max Reitzf3a33f72018-02-13 14:03:52 +01001063 if (glfs_zerofill(fd, current_length, offset - current_length)) {
Max Reitz36e87902018-02-13 14:03:51 +01001064 error_setg_errno(errp, errno, "Could not zerofill the new area");
1065 return -errno;
1066 }
1067 break;
1068#endif /* CONFIG_GLUSTERFS_ZEROFILL */
1069 case PREALLOC_MODE_OFF:
1070 if (glfs_ftruncate(fd, offset)) {
1071 error_setg_errno(errp, errno, "Could not resize file");
1072 return -errno;
1073 }
1074 break;
1075 default:
1076 error_setg(errp, "Unsupported preallocation mode: %s",
1077 PreallocMode_str(prealloc));
1078 return -EINVAL;
1079 }
1080
1081 return 0;
1082}
1083
Kevin Wolfab8bda72018-01-31 16:27:38 +01001084static int qemu_gluster_co_create(BlockdevCreateOptions *options,
1085 Error **errp)
1086{
1087 BlockdevCreateOptionsGluster *opts = &options->u.gluster;
1088 struct glfs *glfs;
1089 struct glfs_fd *fd = NULL;
1090 int ret = 0;
1091
1092 assert(options->driver == BLOCKDEV_DRIVER_GLUSTER);
1093
1094 glfs = qemu_gluster_glfs_init(opts->location, errp);
1095 if (!glfs) {
1096 ret = -errno;
1097 goto out;
1098 }
1099
1100 fd = glfs_creat(glfs, opts->location->path,
1101 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
1102 if (!fd) {
1103 ret = -errno;
1104 goto out;
1105 }
1106
1107 ret = qemu_gluster_do_truncate(fd, opts->size, opts->preallocation, errp);
1108
1109out:
1110 if (fd) {
1111 if (glfs_close(fd) != 0 && ret == 0) {
1112 ret = -errno;
1113 }
1114 }
1115 glfs_clear_preopened(glfs);
1116 return ret;
1117}
1118
Maxim Levitskyb92902d2020-03-26 03:12:17 +02001119static int coroutine_fn qemu_gluster_co_create_opts(BlockDriver *drv,
1120 const char *filename,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001121 QemuOpts *opts,
1122 Error **errp)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301123{
Kevin Wolfab8bda72018-01-31 16:27:38 +01001124 BlockdevCreateOptions *options;
1125 BlockdevCreateOptionsGluster *gopts;
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +05301126 BlockdevOptionsGluster *gconf;
Chunyan Liu90c772d2014-06-05 17:20:54 +08001127 char *tmp = NULL;
Niels de Vosdf3a4292017-05-28 12:01:14 +05301128 Error *local_err = NULL;
Kevin Wolfab8bda72018-01-31 16:27:38 +01001129 int ret;
1130
1131 options = g_new0(BlockdevCreateOptions, 1);
1132 options->driver = BLOCKDEV_DRIVER_GLUSTER;
1133 gopts = &options->u.gluster;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301134
Prasanna Kumar Kalever7edac2d2016-07-19 22:27:32 +05301135 gconf = g_new0(BlockdevOptionsGluster, 1);
Kevin Wolfab8bda72018-01-31 16:27:38 +01001136 gopts->location = gconf;
1137
1138 gopts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1139 BDRV_SECTOR_SIZE);
1140
1141 tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1142 gopts->preallocation = qapi_enum_parse(&PreallocMode_lookup, tmp,
1143 PREALLOC_MODE_OFF, &local_err);
1144 g_free(tmp);
1145 if (local_err) {
1146 error_propagate(errp, local_err);
1147 ret = -EINVAL;
1148 goto fail;
1149 }
1150
Prasanna Kumar Kalever1a417e42016-11-02 22:20:36 +05301151 gconf->debug = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG,
1152 GLUSTER_DEBUG_DEFAULT);
1153 if (gconf->debug < 0) {
1154 gconf->debug = 0;
1155 } else if (gconf->debug > GLUSTER_DEBUG_MAX) {
1156 gconf->debug = GLUSTER_DEBUG_MAX;
Jeff Cody7eac8682016-04-07 17:24:19 -04001157 }
Prasanna Kumar Kalever1a417e42016-11-02 22:20:36 +05301158 gconf->has_debug = true;
Jeff Cody7eac8682016-04-07 17:24:19 -04001159
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +05301160 gconf->logfile = qemu_opt_get_del(opts, GLUSTER_OPT_LOGFILE);
1161 if (!gconf->logfile) {
1162 gconf->logfile = g_strdup(GLUSTER_LOGFILE_DEFAULT);
1163 }
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +05301164
Kevin Wolfab8bda72018-01-31 16:27:38 +01001165 ret = qemu_gluster_parse(gconf, filename, NULL, errp);
1166 if (ret < 0) {
1167 goto fail;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301168 }
1169
Kevin Wolfab8bda72018-01-31 16:27:38 +01001170 ret = qemu_gluster_co_create(options, errp);
1171 if (ret < 0) {
1172 goto fail;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301173 }
1174
Kevin Wolfab8bda72018-01-31 16:27:38 +01001175 ret = 0;
1176fail:
1177 qapi_free_BlockdevCreateOptions(options);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301178 return ret;
1179}
1180
Bharata B Rao15744b02013-12-21 14:51:24 +05301181static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs,
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +05301182 int64_t sector_num, int nb_sectors,
1183 QEMUIOVector *qiov, int write)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301184{
1185 int ret;
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001186 GlusterAIOCB acb;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301187 BDRVGlusterState *s = bs->opaque;
Bharata B Rao15744b02013-12-21 14:51:24 +05301188 size_t size = nb_sectors * BDRV_SECTOR_SIZE;
1189 off_t offset = sector_num * BDRV_SECTOR_SIZE;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301190
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001191 acb.size = size;
1192 acb.ret = 0;
1193 acb.coroutine = qemu_coroutine_self();
1194 acb.aio_context = bdrv_get_aio_context(bs);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301195
1196 if (write) {
1197 ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0,
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +05301198 gluster_finish_aiocb, &acb);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301199 } else {
1200 ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0,
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +05301201 gluster_finish_aiocb, &acb);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301202 }
1203
1204 if (ret < 0) {
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001205 return -errno;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301206 }
Bharata B Rao15744b02013-12-21 14:51:24 +05301207
1208 qemu_coroutine_yield();
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001209 return acb.ret;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301210}
1211
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001212static coroutine_fn int qemu_gluster_co_truncate(BlockDriverState *bs,
1213 int64_t offset,
Max Reitzc80d8b02019-09-18 11:51:40 +02001214 bool exact,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001215 PreallocMode prealloc,
Kevin Wolf92b92792020-04-24 14:54:39 +02001216 BdrvRequestFlags flags,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001217 Error **errp)
Paolo Bonzini42ec24e2013-07-19 19:51:33 +05301218{
Paolo Bonzini42ec24e2013-07-19 19:51:33 +05301219 BDRVGlusterState *s = bs->opaque;
Max Reitzc3132af2018-02-13 14:03:53 +01001220 return qemu_gluster_do_truncate(s->fd, offset, prealloc, errp);
Paolo Bonzini42ec24e2013-07-19 19:51:33 +05301221}
1222
Bharata B Rao15744b02013-12-21 14:51:24 +05301223static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +05301224 int64_t sector_num,
1225 int nb_sectors,
1226 QEMUIOVector *qiov)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301227{
Bharata B Rao15744b02013-12-21 14:51:24 +05301228 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301229}
1230
Bharata B Rao15744b02013-12-21 14:51:24 +05301231static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs,
Prasanna Kumar Kaleverf70c50c2016-07-19 22:27:30 +05301232 int64_t sector_num,
1233 int nb_sectors,
Eric Blakee18a58b2018-04-24 17:01:57 -05001234 QEMUIOVector *qiov,
1235 int flags)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301236{
Bharata B Rao15744b02013-12-21 14:51:24 +05301237 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301238}
1239
Jeff Cody5d4343e2016-04-15 16:29:06 -04001240static void qemu_gluster_close(BlockDriverState *bs)
1241{
1242 BDRVGlusterState *s = bs->opaque;
1243
Prasanna Kumar Kalevere9db8ff2016-07-22 20:26:48 +05301244 g_free(s->logfile);
Jeff Cody5d4343e2016-04-15 16:29:06 -04001245 if (s->fd) {
1246 glfs_close(s->fd);
1247 s->fd = NULL;
1248 }
Prasanna Kumar Kalever6349c152016-10-27 20:54:50 +05301249 glfs_clear_preopened(s->glfs);
Jeff Cody5d4343e2016-04-15 16:29:06 -04001250}
1251
Bharata B Rao15744b02013-12-21 14:51:24 +05301252static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301253{
1254 int ret;
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001255 GlusterAIOCB acb;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301256 BDRVGlusterState *s = bs->opaque;
1257
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001258 acb.size = 0;
1259 acb.ret = 0;
1260 acb.coroutine = qemu_coroutine_self();
1261 acb.aio_context = bdrv_get_aio_context(bs);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301262
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001263 ret = glfs_fsync_async(s->fd, gluster_finish_aiocb, &acb);
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301264 if (ret < 0) {
Jeff Codyd85fa9e2016-04-05 10:40:09 -04001265 ret = -errno;
1266 goto error;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301267 }
Bharata B Rao15744b02013-12-21 14:51:24 +05301268
1269 qemu_coroutine_yield();
Jeff Codyd85fa9e2016-04-05 10:40:09 -04001270 if (acb.ret < 0) {
1271 ret = acb.ret;
1272 goto error;
1273 }
1274
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001275 return acb.ret;
Jeff Codyd85fa9e2016-04-05 10:40:09 -04001276
1277error:
1278 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache
1279 * after a fsync failure, so we have no way of allowing the guest to safely
1280 * continue. Gluster versions prior to 3.5.6 don't retain the cache
1281 * either, but will invalidate the fd on error, so this is again our only
1282 * option.
1283 *
1284 * The 'resync-failed-syncs-after-fsync' xlator option for the
1285 * write-behind cache will cause later gluster versions to retain its
1286 * cache after error, so long as the fd remains open. However, we
1287 * currently have no way of knowing if this option is supported.
1288 *
1289 * TODO: Once gluster provides a way for us to determine if the option
1290 * is supported, bypass the closure and setting drv to NULL. */
1291 qemu_gluster_close(bs);
1292 bs->drv = NULL;
1293 return ret;
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301294}
1295
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301296#ifdef CONFIG_GLUSTERFS_DISCARD
Eric Blake10141702016-07-15 17:23:00 -06001297static coroutine_fn int qemu_gluster_co_pdiscard(BlockDriverState *bs,
Vladimir Sementsov-Ogievskiy0c802282021-09-03 13:28:06 +03001298 int64_t offset, int64_t bytes)
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301299{
1300 int ret;
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001301 GlusterAIOCB acb;
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301302 BDRVGlusterState *s = bs->opaque;
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301303
Vladimir Sementsov-Ogievskiy0c802282021-09-03 13:28:06 +03001304 assert(bytes <= SIZE_MAX); /* rely on max_pdiscard */
1305
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001306 acb.size = 0;
1307 acb.ret = 0;
1308 acb.coroutine = qemu_coroutine_self();
1309 acb.aio_context = bdrv_get_aio_context(bs);
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301310
Vladimir Sementsov-Ogievskiy0c802282021-09-03 13:28:06 +03001311 ret = glfs_discard_async(s->fd, offset, bytes, gluster_finish_aiocb, &acb);
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301312 if (ret < 0) {
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001313 return -errno;
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301314 }
Bharata B Rao15744b02013-12-21 14:51:24 +05301315
1316 qemu_coroutine_yield();
Paolo Bonzinic833d1e2015-10-01 13:04:38 +02001317 return acb.ret;
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301318}
1319#endif
1320
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001321static int64_t coroutine_fn qemu_gluster_co_getlength(BlockDriverState *bs)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301322{
1323 BDRVGlusterState *s = bs->opaque;
1324 int64_t ret;
1325
1326 ret = glfs_lseek(s->fd, 0, SEEK_END);
1327 if (ret < 0) {
1328 return -errno;
1329 } else {
1330 return ret;
1331 }
1332}
1333
Emanuele Giuseppe Esposito82618d72023-01-13 21:42:07 +01001334static int64_t coroutine_fn
1335qemu_gluster_co_get_allocated_file_size(BlockDriverState *bs)
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301336{
1337 BDRVGlusterState *s = bs->opaque;
1338 struct stat st;
1339 int ret;
1340
1341 ret = glfs_fstat(s->fd, &st);
1342 if (ret < 0) {
1343 return -errno;
1344 } else {
1345 return st.st_blocks * 512;
1346 }
1347}
1348
Niels de Vos947eb202016-03-10 19:38:00 +01001349/*
1350 * Find allocation range in @bs around offset @start.
1351 * May change underlying file descriptor's file offset.
1352 * If @start is not in a hole, store @start in @data, and the
1353 * beginning of the next hole in @hole, and return 0.
1354 * If @start is in a non-trailing hole, store @start in @hole and the
1355 * beginning of the next non-hole in @data, and return 0.
1356 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1357 * If we can't find out, return a negative errno other than -ENXIO.
1358 *
Stefan Weil50d6a8a2018-07-12 21:51:20 +02001359 * (Shamefully copied from file-posix.c, only minuscule adaptions.)
Niels de Vos947eb202016-03-10 19:38:00 +01001360 */
1361static int find_allocation(BlockDriverState *bs, off_t start,
1362 off_t *data, off_t *hole)
1363{
1364 BDRVGlusterState *s = bs->opaque;
Niels de Vos947eb202016-03-10 19:38:00 +01001365
1366 if (!s->supports_seek_data) {
Jeff Codyd9b78972016-10-07 17:48:12 -04001367 goto exit;
Niels de Vos947eb202016-03-10 19:38:00 +01001368 }
1369
Jeff Codyd9b78972016-10-07 17:48:12 -04001370#if defined SEEK_HOLE && defined SEEK_DATA
1371 off_t offs;
1372
Niels de Vos947eb202016-03-10 19:38:00 +01001373 /*
1374 * SEEK_DATA cases:
1375 * D1. offs == start: start is in data
1376 * D2. offs > start: start is in a hole, next data at offs
1377 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1378 * or start is beyond EOF
1379 * If the latter happens, the file has been truncated behind
1380 * our back since we opened it. All bets are off then.
1381 * Treating like a trailing hole is simplest.
1382 * D4. offs < 0, errno != ENXIO: we learned nothing
1383 */
1384 offs = glfs_lseek(s->fd, start, SEEK_DATA);
1385 if (offs < 0) {
1386 return -errno; /* D3 or D4 */
1387 }
Jeff Cody223a23c2017-05-23 13:27:50 -04001388
1389 if (offs < start) {
1390 /* This is not a valid return by lseek(). We are safe to just return
1391 * -EIO in this case, and we'll treat it like D4. Unfortunately some
1392 * versions of gluster server will return offs < start, so an assert
1393 * here will unnecessarily abort QEMU. */
1394 return -EIO;
1395 }
Niels de Vos947eb202016-03-10 19:38:00 +01001396
1397 if (offs > start) {
1398 /* D2: in hole, next data at offs */
1399 *hole = start;
1400 *data = offs;
1401 return 0;
1402 }
1403
1404 /* D1: in data, end not yet known */
1405
1406 /*
1407 * SEEK_HOLE cases:
1408 * H1. offs == start: start is in a hole
1409 * If this happens here, a hole has been dug behind our back
1410 * since the previous lseek().
1411 * H2. offs > start: either start is in data, next hole at offs,
1412 * or start is in trailing hole, EOF at offs
1413 * Linux treats trailing holes like any other hole: offs ==
1414 * start. Solaris seeks to EOF instead: offs > start (blech).
1415 * If that happens here, a hole has been dug behind our back
1416 * since the previous lseek().
1417 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1418 * If this happens, the file has been truncated behind our
1419 * back since we opened it. Treat it like a trailing hole.
1420 * H4. offs < 0, errno != ENXIO: we learned nothing
1421 * Pretend we know nothing at all, i.e. "forget" about D1.
1422 */
1423 offs = glfs_lseek(s->fd, start, SEEK_HOLE);
1424 if (offs < 0) {
1425 return -errno; /* D1 and (H3 or H4) */
1426 }
Jeff Cody223a23c2017-05-23 13:27:50 -04001427
1428 if (offs < start) {
1429 /* This is not a valid return by lseek(). We are safe to just return
1430 * -EIO in this case, and we'll treat it like H4. Unfortunately some
1431 * versions of gluster server will return offs < start, so an assert
1432 * here will unnecessarily abort QEMU. */
1433 return -EIO;
1434 }
Niels de Vos947eb202016-03-10 19:38:00 +01001435
1436 if (offs > start) {
1437 /*
1438 * D1 and H2: either in data, next hole at offs, or it was in
1439 * data but is now in a trailing hole. In the latter case,
1440 * all bets are off. Treating it as if it there was data all
1441 * the way to EOF is safe, so simply do that.
1442 */
1443 *data = start;
1444 *hole = offs;
1445 return 0;
1446 }
1447
1448 /* D1 and H1 */
1449 return -EBUSY;
Jeff Codyd9b78972016-10-07 17:48:12 -04001450#endif
1451
1452exit:
1453 return -ENOTSUP;
Niels de Vos947eb202016-03-10 19:38:00 +01001454}
1455
1456/*
Eric Blake08c9e772018-02-13 14:26:45 -06001457 * Returns the allocation status of the specified offset.
Niels de Vos947eb202016-03-10 19:38:00 +01001458 *
Eric Blake08c9e772018-02-13 14:26:45 -06001459 * The block layer guarantees 'offset' and 'bytes' are within bounds.
Niels de Vos947eb202016-03-10 19:38:00 +01001460 *
Eric Blake08c9e772018-02-13 14:26:45 -06001461 * 'pnum' is set to the number of bytes (including and immediately following
1462 * the specified offset) that are known to be in the same
Niels de Vos947eb202016-03-10 19:38:00 +01001463 * allocated/unallocated state.
1464 *
Hanna Reitz72b4cab2021-08-12 10:41:47 +02001465 * 'bytes' is a soft cap for 'pnum'. If the information is free, 'pnum' may
1466 * well exceed it.
Niels de Vos947eb202016-03-10 19:38:00 +01001467 *
Eric Blake08c9e772018-02-13 14:26:45 -06001468 * (Based on raw_co_block_status() from file-posix.c.)
Niels de Vos947eb202016-03-10 19:38:00 +01001469 */
Eric Blake08c9e772018-02-13 14:26:45 -06001470static int coroutine_fn qemu_gluster_co_block_status(BlockDriverState *bs,
1471 bool want_zero,
1472 int64_t offset,
1473 int64_t bytes,
1474 int64_t *pnum,
1475 int64_t *map,
1476 BlockDriverState **file)
Niels de Vos947eb202016-03-10 19:38:00 +01001477{
1478 BDRVGlusterState *s = bs->opaque;
Eric Blake08c9e772018-02-13 14:26:45 -06001479 off_t data = 0, hole = 0;
Niels de Vos947eb202016-03-10 19:38:00 +01001480 int ret = -EINVAL;
1481
Max Reitze24154d2021-08-05 16:36:03 +02001482 assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
1483
Niels de Vos947eb202016-03-10 19:38:00 +01001484 if (!s->fd) {
1485 return ret;
1486 }
1487
Eric Blake08c9e772018-02-13 14:26:45 -06001488 if (!want_zero) {
1489 *pnum = bytes;
1490 *map = offset;
1491 *file = bs;
1492 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
Niels de Vos947eb202016-03-10 19:38:00 +01001493 }
1494
Eric Blake08c9e772018-02-13 14:26:45 -06001495 ret = find_allocation(bs, offset, &data, &hole);
Niels de Vos947eb202016-03-10 19:38:00 +01001496 if (ret == -ENXIO) {
1497 /* Trailing hole */
Eric Blake08c9e772018-02-13 14:26:45 -06001498 *pnum = bytes;
Niels de Vos947eb202016-03-10 19:38:00 +01001499 ret = BDRV_BLOCK_ZERO;
1500 } else if (ret < 0) {
1501 /* No info available, so pretend there are no holes */
Eric Blake08c9e772018-02-13 14:26:45 -06001502 *pnum = bytes;
Niels de Vos947eb202016-03-10 19:38:00 +01001503 ret = BDRV_BLOCK_DATA;
Eric Blake08c9e772018-02-13 14:26:45 -06001504 } else if (data == offset) {
1505 /* On a data extent, compute bytes to the end of the extent,
Niels de Vos947eb202016-03-10 19:38:00 +01001506 * possibly including a partial sector at EOF. */
Hanna Reitz72b4cab2021-08-12 10:41:47 +02001507 *pnum = hole - offset;
Max Reitze24154d2021-08-05 16:36:03 +02001508
1509 /*
1510 * We are not allowed to return partial sectors, though, so
1511 * round up if necessary.
1512 */
1513 if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) {
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001514 int64_t file_length = qemu_gluster_co_getlength(bs);
Max Reitze24154d2021-08-05 16:36:03 +02001515 if (file_length > 0) {
1516 /* Ignore errors, this is just a safeguard */
1517 assert(hole == file_length);
1518 }
1519 *pnum = ROUND_UP(*pnum, bs->bl.request_alignment);
1520 }
1521
Niels de Vos947eb202016-03-10 19:38:00 +01001522 ret = BDRV_BLOCK_DATA;
1523 } else {
Eric Blake08c9e772018-02-13 14:26:45 -06001524 /* On a hole, compute bytes to the beginning of the next extent. */
1525 assert(hole == offset);
Hanna Reitz72b4cab2021-08-12 10:41:47 +02001526 *pnum = data - offset;
Niels de Vos947eb202016-03-10 19:38:00 +01001527 ret = BDRV_BLOCK_ZERO;
1528 }
1529
Eric Blake08c9e772018-02-13 14:26:45 -06001530 *map = offset;
Niels de Vos947eb202016-03-10 19:38:00 +01001531 *file = bs;
1532
Eric Blake08c9e772018-02-13 14:26:45 -06001533 return ret | BDRV_BLOCK_OFFSET_VALID;
Niels de Vos947eb202016-03-10 19:38:00 +01001534}
1535
1536
Max Reitz26542672019-02-01 20:29:25 +01001537static const char *const gluster_strong_open_opts[] = {
1538 GLUSTER_OPT_VOLUME,
1539 GLUSTER_OPT_PATH,
1540 GLUSTER_OPT_TYPE,
1541 GLUSTER_OPT_SERVER_PATTERN,
1542 GLUSTER_OPT_HOST,
1543 GLUSTER_OPT_PORT,
1544 GLUSTER_OPT_TO,
1545 GLUSTER_OPT_IPV4,
1546 GLUSTER_OPT_IPV6,
1547 GLUSTER_OPT_SOCKET,
1548
1549 NULL
1550};
1551
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301552static BlockDriver bdrv_gluster = {
1553 .format_name = "gluster",
1554 .protocol_name = "gluster",
1555 .instance_size = sizeof(BDRVGlusterState),
1556 .bdrv_file_open = qemu_gluster_open,
Jeff Codyadccfbc2014-02-17 11:11:12 -05001557 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1558 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1559 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301560 .bdrv_close = qemu_gluster_close,
Kevin Wolfab8bda72018-01-31 16:27:38 +01001561 .bdrv_co_create = qemu_gluster_co_create,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001562 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001563 .bdrv_co_getlength = qemu_gluster_co_getlength,
Emanuele Giuseppe Esposito82618d72023-01-13 21:42:07 +01001564 .bdrv_co_get_allocated_file_size = qemu_gluster_co_get_allocated_file_size,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001565 .bdrv_co_truncate = qemu_gluster_co_truncate,
Bharata B Rao15744b02013-12-21 14:51:24 +05301566 .bdrv_co_readv = qemu_gluster_co_readv,
1567 .bdrv_co_writev = qemu_gluster_co_writev,
1568 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301569#ifdef CONFIG_GLUSTERFS_DISCARD
Eric Blake10141702016-07-15 17:23:00 -06001570 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301571#endif
Bharata B Rao7c815372013-12-21 14:51:25 +05301572#ifdef CONFIG_GLUSTERFS_ZEROFILL
Eric Blakee88a36e2016-06-01 15:10:08 -06001573 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
Bharata B Rao7c815372013-12-21 14:51:25 +05301574#endif
Eric Blake08c9e772018-02-13 14:26:45 -06001575 .bdrv_co_block_status = qemu_gluster_co_block_status,
Stefano Garzarellade23e722019-03-28 11:52:27 +01001576 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
Chunyan Liu90c772d2014-06-05 17:20:54 +08001577 .create_opts = &qemu_gluster_create_opts,
Max Reitz26542672019-02-01 20:29:25 +01001578 .strong_runtime_opts = gluster_strong_open_opts,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301579};
1580
1581static BlockDriver bdrv_gluster_tcp = {
1582 .format_name = "gluster",
1583 .protocol_name = "gluster+tcp",
1584 .instance_size = sizeof(BDRVGlusterState),
1585 .bdrv_file_open = qemu_gluster_open,
Jeff Codyadccfbc2014-02-17 11:11:12 -05001586 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1587 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1588 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301589 .bdrv_close = qemu_gluster_close,
Kevin Wolfab8bda72018-01-31 16:27:38 +01001590 .bdrv_co_create = qemu_gluster_co_create,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001591 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001592 .bdrv_co_getlength = qemu_gluster_co_getlength,
Emanuele Giuseppe Esposito82618d72023-01-13 21:42:07 +01001593 .bdrv_co_get_allocated_file_size = qemu_gluster_co_get_allocated_file_size,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001594 .bdrv_co_truncate = qemu_gluster_co_truncate,
Bharata B Rao15744b02013-12-21 14:51:24 +05301595 .bdrv_co_readv = qemu_gluster_co_readv,
1596 .bdrv_co_writev = qemu_gluster_co_writev,
1597 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301598#ifdef CONFIG_GLUSTERFS_DISCARD
Eric Blake10141702016-07-15 17:23:00 -06001599 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301600#endif
Bharata B Rao7c815372013-12-21 14:51:25 +05301601#ifdef CONFIG_GLUSTERFS_ZEROFILL
Eric Blakee88a36e2016-06-01 15:10:08 -06001602 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
Bharata B Rao7c815372013-12-21 14:51:25 +05301603#endif
Eric Blake08c9e772018-02-13 14:26:45 -06001604 .bdrv_co_block_status = qemu_gluster_co_block_status,
Stefano Garzarellade23e722019-03-28 11:52:27 +01001605 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
Chunyan Liu90c772d2014-06-05 17:20:54 +08001606 .create_opts = &qemu_gluster_create_opts,
Max Reitz26542672019-02-01 20:29:25 +01001607 .strong_runtime_opts = gluster_strong_open_opts,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301608};
1609
1610static BlockDriver bdrv_gluster_unix = {
1611 .format_name = "gluster",
1612 .protocol_name = "gluster+unix",
1613 .instance_size = sizeof(BDRVGlusterState),
1614 .bdrv_file_open = qemu_gluster_open,
Jeff Codyadccfbc2014-02-17 11:11:12 -05001615 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1616 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1617 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301618 .bdrv_close = qemu_gluster_close,
Kevin Wolfab8bda72018-01-31 16:27:38 +01001619 .bdrv_co_create = qemu_gluster_co_create,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001620 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001621 .bdrv_co_getlength = qemu_gluster_co_getlength,
Emanuele Giuseppe Esposito82618d72023-01-13 21:42:07 +01001622 .bdrv_co_get_allocated_file_size = qemu_gluster_co_get_allocated_file_size,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001623 .bdrv_co_truncate = qemu_gluster_co_truncate,
Bharata B Rao15744b02013-12-21 14:51:24 +05301624 .bdrv_co_readv = qemu_gluster_co_readv,
1625 .bdrv_co_writev = qemu_gluster_co_writev,
1626 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301627#ifdef CONFIG_GLUSTERFS_DISCARD
Eric Blake10141702016-07-15 17:23:00 -06001628 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301629#endif
Bharata B Rao7c815372013-12-21 14:51:25 +05301630#ifdef CONFIG_GLUSTERFS_ZEROFILL
Eric Blakee88a36e2016-06-01 15:10:08 -06001631 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
Bharata B Rao7c815372013-12-21 14:51:25 +05301632#endif
Eric Blake08c9e772018-02-13 14:26:45 -06001633 .bdrv_co_block_status = qemu_gluster_co_block_status,
Stefano Garzarellade23e722019-03-28 11:52:27 +01001634 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
Chunyan Liu90c772d2014-06-05 17:20:54 +08001635 .create_opts = &qemu_gluster_create_opts,
Max Reitz26542672019-02-01 20:29:25 +01001636 .strong_runtime_opts = gluster_strong_open_opts,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301637};
1638
Prasanna Kumar Kalever0552ff22016-07-19 22:27:31 +05301639/* rdma is deprecated (actually never supported for volfile fetch).
1640 * Let's maintain it for the protocol compatibility, to make sure things
1641 * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp
1642 * protocol with a warning.
1643 * TODO: remove gluster+rdma interface support
1644 */
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301645static BlockDriver bdrv_gluster_rdma = {
1646 .format_name = "gluster",
1647 .protocol_name = "gluster+rdma",
1648 .instance_size = sizeof(BDRVGlusterState),
1649 .bdrv_file_open = qemu_gluster_open,
Jeff Codyadccfbc2014-02-17 11:11:12 -05001650 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1651 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1652 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301653 .bdrv_close = qemu_gluster_close,
Kevin Wolfab8bda72018-01-31 16:27:38 +01001654 .bdrv_co_create = qemu_gluster_co_create,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001655 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001656 .bdrv_co_getlength = qemu_gluster_co_getlength,
Emanuele Giuseppe Esposito82618d72023-01-13 21:42:07 +01001657 .bdrv_co_get_allocated_file_size = qemu_gluster_co_get_allocated_file_size,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001658 .bdrv_co_truncate = qemu_gluster_co_truncate,
Bharata B Rao15744b02013-12-21 14:51:24 +05301659 .bdrv_co_readv = qemu_gluster_co_readv,
1660 .bdrv_co_writev = qemu_gluster_co_writev,
1661 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301662#ifdef CONFIG_GLUSTERFS_DISCARD
Eric Blake10141702016-07-15 17:23:00 -06001663 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
Bharata B Rao0c14fb42013-07-16 21:47:42 +05301664#endif
Bharata B Rao7c815372013-12-21 14:51:25 +05301665#ifdef CONFIG_GLUSTERFS_ZEROFILL
Eric Blakee88a36e2016-06-01 15:10:08 -06001666 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
Bharata B Rao7c815372013-12-21 14:51:25 +05301667#endif
Eric Blake08c9e772018-02-13 14:26:45 -06001668 .bdrv_co_block_status = qemu_gluster_co_block_status,
Stefano Garzarellade23e722019-03-28 11:52:27 +01001669 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
Chunyan Liu90c772d2014-06-05 17:20:54 +08001670 .create_opts = &qemu_gluster_create_opts,
Max Reitz26542672019-02-01 20:29:25 +01001671 .strong_runtime_opts = gluster_strong_open_opts,
Bharata B Rao8d6d89c2012-09-27 19:30:32 +05301672};
1673
1674static void bdrv_gluster_init(void)
1675{
1676 bdrv_register(&bdrv_gluster_rdma);
1677 bdrv_register(&bdrv_gluster_unix);
1678 bdrv_register(&bdrv_gluster_tcp);
1679 bdrv_register(&bdrv_gluster);
1680}
1681
1682block_init(bdrv_gluster_init);