1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
* storage_file_backend_gluster.c: storage file backend for Gluster handling
*
* Copyright (C) 2013-2018 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include <glusterfs/api/glfs.h>
#include "storage_file_backend.h"
#include "storage_file_backend_gluster.h"
#include "viralloc.h"
#include "virerror.h"
#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_STORAGE
VIR_LOG_INIT("storage.storage_file_gluster");
typedef struct _virStorageFileBackendGlusterPriv virStorageFileBackendGlusterPriv;
struct _virStorageFileBackendGlusterPriv {
glfs_t *vol;
};
static void
virStorageFileBackendGlusterDeinit(virStorageSource *src)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = drv->priv;
VIR_DEBUG("deinitializing gluster storage file %p (gluster://%s:%u/%s%s)",
src, src->hosts->name, src->hosts->port, src->volume, src->path);
if (priv->vol)
glfs_fini(priv->vol);
VIR_FREE(priv);
drv->priv = NULL;
}
static int
virStorageFileBackendGlusterInitServer(virStorageFileBackendGlusterPriv *priv,
virStorageNetHostDef *host)
{
const char *transport = virStorageNetHostTransportTypeToString(host->transport);
const char *hoststr = NULL;
int port = 0;
switch ((virStorageNetHostTransport) host->transport) {
case VIR_STORAGE_NET_HOST_TRANS_RDMA:
case VIR_STORAGE_NET_HOST_TRANS_TCP:
hoststr = host->name;
port = host->port;
break;
case VIR_STORAGE_NET_HOST_TRANS_UNIX:
hoststr = host->socket;
break;
case VIR_STORAGE_NET_HOST_TRANS_LAST:
break;
}
VIR_DEBUG("adding gluster host for %p: transport=%s host=%s port=%d",
priv, transport, hoststr, port);
if (glfs_set_volfile_server(priv->vol, transport, hoststr, port) < 0) {
virReportSystemError(errno,
_("failed to set gluster volfile server '%1$s'"),
hoststr);
return -1;
}
return 0;
}
static int
virStorageFileBackendGlusterInit(virStorageSource *src)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = NULL;
size_t i;
if (!src->volume) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("missing gluster volume name for path '%1$s'"),
src->path);
return -1;
}
priv = g_new0(virStorageFileBackendGlusterPriv, 1);
VIR_DEBUG("initializing gluster storage file %p "
"(priv='%p' volume='%s' path='%s') as [%u:%u]",
src, priv, src->volume, src->path,
(unsigned int)drv->uid, (unsigned int)drv->gid);
if (!(priv->vol = glfs_new(src->volume))) {
virReportError(VIR_ERR_OPERATION_FAILED,
_("failed to create glfs object for '%1$s'"), src->volume);
goto error;
}
for (i = 0; i < src->nhosts; i++) {
if (virStorageFileBackendGlusterInitServer(priv, src->hosts + i) < 0)
goto error;
}
if (glfs_init(priv->vol) < 0) {
virReportSystemError(errno,
_("failed to initialize gluster connection (src=%1$p priv=%2$p)"),
src, priv);
goto error;
}
drv->priv = priv;
return 0;
error:
if (priv->vol)
glfs_fini(priv->vol);
VIR_FREE(priv);
return -1;
}
static int
virStorageFileBackendGlusterCreate(virStorageSource *src)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = drv->priv;
glfs_fd_t *fd = NULL;
if (!(fd = glfs_creat(priv->vol, src->path,
O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR)))
return -1;
ignore_value(glfs_close(fd));
return 0;
}
static int
virStorageFileBackendGlusterUnlink(virStorageSource *src)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = drv->priv;
return glfs_unlink(priv->vol, src->path);
}
static int
virStorageFileBackendGlusterStat(virStorageSource *src,
struct stat *st)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = drv->priv;
return glfs_stat(priv->vol, src->path, st);
}
static ssize_t
virStorageFileBackendGlusterRead(virStorageSource *src,
size_t offset,
size_t len,
char **buf)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = drv->priv;
glfs_fd_t *fd = NULL;
ssize_t ret = -1;
char *s;
size_t nread = 0;
*buf = NULL;
if (!(fd = glfs_open(priv->vol, src->path, O_RDONLY))) {
virReportSystemError(errno, _("Failed to open file '%1$s'"),
src->path);
return -1;
}
if (offset > 0) {
if (glfs_lseek(fd, offset, SEEK_SET) == (off_t) -1) {
virReportSystemError(errno, _("cannot seek into '%1$s'"), src->path);
goto cleanup;
}
}
*buf = g_new0(char, len);
s = *buf;
while (len) {
ssize_t r = glfs_read(fd, s, len, 0);
if (r < 0 && errno == EINTR)
continue;
if (r < 0) {
VIR_FREE(*buf);
virReportSystemError(errno, _("unable to read '%1$s'"), src->path);
return r;
}
if (r == 0)
return nread;
s += r;
len -= r;
nread += r;
}
ret = nread;
cleanup:
if (fd)
glfs_close(fd);
return ret;
}
static int
virStorageFileBackendGlusterAccess(virStorageSource *src,
int mode)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = drv->priv;
return glfs_access(priv->vol, src->path, mode);
}
static int
virStorageFileBackendGlusterChown(const virStorageSource *src,
uid_t uid,
gid_t gid)
{
virStorageDriverData *drv = src->drv;
virStorageFileBackendGlusterPriv *priv = drv->priv;
return glfs_chown(priv->vol, src->path, uid, gid);
}
virStorageFileBackend virStorageFileBackendGluster = {
.type = VIR_STORAGE_TYPE_NETWORK,
.protocol = VIR_STORAGE_NET_PROTOCOL_GLUSTER,
.backendInit = virStorageFileBackendGlusterInit,
.backendDeinit = virStorageFileBackendGlusterDeinit,
.storageFileCreate = virStorageFileBackendGlusterCreate,
.storageFileUnlink = virStorageFileBackendGlusterUnlink,
.storageFileStat = virStorageFileBackendGlusterStat,
.storageFileRead = virStorageFileBackendGlusterRead,
.storageFileAccess = virStorageFileBackendGlusterAccess,
.storageFileChown = virStorageFileBackendGlusterChown,
};
int
virStorageFileGlusterRegister(void)
{
if (virStorageFileBackendRegister(&virStorageFileBackendGluster) < 0)
return -1;
return 0;
}
|