forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glfs.c
720 lines (600 loc) · 16 KB
/
glfs.c
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
* Copyright 2015, Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
#include <scsi/scsi.h>
#include <pthread.h>
#include <glusterfs/api/glfs.h>
#include "darray.h"
#include "tcmu-runner.h"
#include "libtcmu.h"
#define ALLOWED_BSOFLAGS (O_DIRECT | O_RDWR | O_LARGEFILE)
#define GLUSTER_PORT "24007"
#define TCMU_GLFS_LOG_FILENAME "tcmu-runner-glfs.log" /* MAX 32 CHAR */
#define TCMU_GLFS_DEBUG_LEVEL 4
/* cache protection */
pthread_mutex_t glfs_lock;
typedef enum gluster_transport {
GLUSTER_TRANSPORT_TCP,
GLUSTER_TRANSPORT_UNIX,
GLUSTER_TRANSPORT_RDMA,
GLUSTER_TRANSPORT__MAX,
} gluster_transport;
typedef struct unix_sockaddr {
char *socket;
} unix_sockaddr;
typedef struct inet_sockaddr {
char *addr;
char *port;
} inet_sockaddr;
typedef struct gluster_hostdef {
gluster_transport type;
union { /* union tag is @type */
unix_sockaddr uds;
inet_sockaddr inet;
} u;
} gluster_hostdef;
typedef struct gluster_server {
char *volname; /* volume name*/
char *path; /* path of file in the volume */
gluster_hostdef *server; /* gluster server definition */
} gluster_server;
struct glfs_state {
glfs_t *fs;
glfs_fd_t *gfd;
gluster_server *hosts;
/*
* Current tcmu helper API reports WCE=1, but doesn't
* implement inquiry VPD 0xb2, so clients will not know UNMAP
* or WRITE_SAME are supported. TODO: fix this
*/
};
typedef struct glfs_cbk_cookie {
struct tcmu_device *dev;
struct tcmulib_cmd *cmd;
size_t length;
enum {
TCMU_GLFS_READ = 1,
TCMU_GLFS_WRITE = 2,
TCMU_GLFS_FLUSH = 3
} op;
} glfs_cbk_cookie;
struct gluster_cacheconn {
char *volname;
gluster_hostdef *server;
glfs_t *fs;
darray(char *) cfgstring;
} gluster_cacheconn;
static darray(struct gluster_cacheconn *) glfs_cache = darray_new();
const char *const gluster_transport_lookup[] = {
[GLUSTER_TRANSPORT_TCP] = "tcp",
[GLUSTER_TRANSPORT_UNIX] = "unix",
[GLUSTER_TRANSPORT_RDMA] = "rdma",
[GLUSTER_TRANSPORT__MAX] = NULL,
};
static void gluster_free_host(gluster_hostdef *host)
{
if(!host)
return;
switch (host->type) {
case GLUSTER_TRANSPORT_UNIX:
free(host->u.uds.socket);
break;
case GLUSTER_TRANSPORT_TCP:
case GLUSTER_TRANSPORT_RDMA:
free(host->u.inet.addr);
free(host->u.inet.port);
break;
case GLUSTER_TRANSPORT__MAX:
break;
}
}
static bool
gluster_compare_hosts(gluster_hostdef *src_server, gluster_hostdef *dst_server)
{
if (src_server->type != dst_server->type)
return false;
switch (src_server->type) {
case GLUSTER_TRANSPORT_UNIX:
if (!strcmp(src_server->u.uds.socket, dst_server->u.uds.socket))
return true;
break;
case GLUSTER_TRANSPORT_TCP:
case GLUSTER_TRANSPORT_RDMA:
if (!strcmp(src_server->u.inet.addr, dst_server->u.inet.addr)
&&
!strcmp(src_server->u.inet.port, dst_server->u.inet.port))
return true;
break;
case GLUSTER_TRANSPORT__MAX:
break;
}
return false;
}
static int gluster_cache_add(gluster_server *dst, glfs_t *fs, char* cfgstring)
{
struct gluster_cacheconn *entry;
char* cfg_copy = NULL;
entry = calloc(1, sizeof(gluster_cacheconn));
if (!entry)
goto error;
entry->volname = strdup(dst->volname);
entry->server = calloc(1, sizeof(gluster_hostdef));
if (!entry->server)
goto free_entry;
entry->server->type = dst->server->type;
if (entry->server->type == GLUSTER_TRANSPORT_UNIX) {
entry->server->u.uds.socket = strdup(dst->server->u.uds.socket);
} else {
entry->server->u.inet.addr = strdup(dst->server->u.inet.addr);
entry->server->u.inet.port = strdup(dst->server->u.inet.port);
}
entry->fs = fs;
cfg_copy = strdup(cfgstring);
darray_init(entry->cfgstring);
darray_append(entry->cfgstring, cfg_copy);
darray_append(glfs_cache, entry);
return 0;
free_entry:
if (entry->volname)
free(entry->volname);
free(entry);
error:
return -1;
}
static glfs_t* gluster_cache_query(gluster_server *dst, char *cfgstring)
{
struct gluster_cacheconn **entry;
char** config;
char* cfg_copy = NULL;
bool cfgmatch = false;
darray_foreach(entry, glfs_cache) {
if (strcmp((*entry)->volname, dst->volname))
continue;
if (gluster_compare_hosts((*entry)->server, dst->server)) {
darray_foreach(config, (*entry)->cfgstring) {
if (!strcmp(*config, cfgstring)) {
cfgmatch = true;
break;
}
}
if (!cfgmatch) {
cfg_copy = strdup(cfgstring);
darray_append((*entry)->cfgstring, cfg_copy);
}
return (*entry)->fs;
}
}
return NULL;
}
static void gluster_cache_refresh(glfs_t *fs, const char *cfgstring)
{
struct gluster_cacheconn **entry;
char** config;
size_t i = 0;
size_t j = 0;
if (!fs)
return;
darray_foreach(entry, glfs_cache) {
if ((*entry)->fs == fs) {
if (cfgstring) {
darray_foreach(config, (*entry)->cfgstring) {
if (!strcmp(*config, cfgstring)) {
free(*config);
darray_remove((*entry)->cfgstring, j);
break;
}
j++;
}
}
if (darray_size((*entry)->cfgstring))
return;
free((*entry)->volname);
glfs_fini((*entry)->fs);
(*entry)->fs = NULL;
gluster_free_host((*entry)->server);
free((*entry)->server);
(*entry)->server = NULL;
free((*entry));
darray_remove(glfs_cache, i);
return;
} else {
i++;
}
}
}
static void gluster_thread_cleanup(void *arg)
{
pthread_mutex_unlock(arg);
}
static int gluster_cache_query_or_add(struct tcmu_device *dev,
glfs_t **fs, gluster_server *entry,
char *config, bool *init)
{
int ret = -1;
pthread_cleanup_push(gluster_thread_cleanup, &glfs_lock);
pthread_mutex_lock(&glfs_lock);
*fs = gluster_cache_query(entry, config);
if (*fs) {
*init = false;
ret = 0;
goto out;
}
*fs = glfs_new(entry->volname);
if (!*fs) {
tcmu_dev_err(dev, "glfs_new(vol=%s) failed: %m\n", entry->volname);
goto out;
}
ret = gluster_cache_add(entry, *fs, config);
if (ret) {
tcmu_dev_err(dev, "gluster_cache_add(vol=%s, config=%s) failed: %m\n",
entry->volname, config);
glfs_fini(*fs);
*fs = NULL;
goto out;
}
out:
pthread_mutex_unlock(&glfs_lock);
pthread_cleanup_pop(0);
return ret;
}
static void gluster_free_server(gluster_server **hosts)
{
if (!*hosts)
return;
free((*hosts)->volname);
free((*hosts)->path);
gluster_free_host((*hosts)->server);
free((*hosts)->server);
(*hosts)->server = NULL;
free(*hosts);
*hosts = NULL;
}
/*
* Break image string into server, volume, and path components.
* Returns -1 on failure.
*/
static int parse_imagepath(char *cfgstring, gluster_server **hosts)
{
gluster_server *entry = NULL;
char *origp = strdup(cfgstring);
char *p, *sep;
if (!origp)
goto fail;
/* part before '@' is the volume name */
p = origp;
sep = strchr(p, '@');
if (!sep)
goto fail;
*hosts = calloc(1, sizeof(gluster_server));
if (!hosts)
goto fail;
entry = *hosts;
entry->server = calloc(1, sizeof(gluster_hostdef));
if (!entry->server)
goto fail;
*sep = '\0';
entry->volname = strdup(p);
if (!entry->volname)
goto fail;
/* part between '@' and 1st '/' is the server name */
p = sep + 1;
sep = strchr(p, '/');
if (!sep)
goto fail;
*sep = '\0';
entry->server->type = GLUSTER_TRANSPORT_TCP; /* FIXME: Get type dynamically */
entry->server->u.inet.addr = strdup(p);
if (!entry->server->u.inet.addr)
goto fail;
entry->server->u.inet.port = strdup(GLUSTER_PORT); /* FIXME: Get port dynamically */
/* The rest is the path name */
p = sep + 1;
entry->path = strdup(p);
if (!entry->path)
goto fail;
if (entry->server->type == GLUSTER_TRANSPORT_UNIX) {
if (!strlen(entry->server->u.uds.socket) ||
!strlen(entry->volname) || !strlen(entry->path))
goto fail;
} else {
if (!strlen(entry->server->u.inet.addr) ||
!strlen(entry->volname) || !strlen(entry->path))
goto fail;
}
free(origp);
return 0;
fail:
gluster_free_server(hosts);
free(origp);
return -1;
}
static glfs_t* tcmu_create_glfs_object(struct tcmu_device *dev,
char *config, gluster_server **hosts)
{
gluster_server *entry = NULL;
char logfilepath[PATH_MAX];
glfs_t *fs = NULL;
int ret = -1;
bool init = true;
if (parse_imagepath(config, hosts) == -1) {
tcmu_dev_err(dev, "hostaddr, volname, or path missing in %s\n",
config);
goto fail;
}
entry = *hosts;
ret = gluster_cache_query_or_add(dev, &fs, entry, config, &init);
if (ret) {
tcmu_dev_err(dev, "gluster_cache_query_or_add(vol=%s, config=%s) failed\n",
entry->volname, config);
goto fail;
}
if (!init) {
return fs;
}
ret = glfs_set_volfile_server(fs,
gluster_transport_lookup[entry->server->type],
entry->server->u.inet.addr,
atoi(entry->server->u.inet.port));
if (ret) {
tcmu_dev_err(dev, "glfs_set_volfile_server(vol=%s, addr=%s) failed: %m\n",
entry->volname, entry->server->u.inet.addr);
goto unref;
}
ret = tcmu_make_absolute_logfile(logfilepath, TCMU_GLFS_LOG_FILENAME);
if (ret < 0) {
tcmu_dev_err(dev, "tcmu_make_absolute_logfile failed: %d\n", ret);
goto unref;
}
ret = glfs_set_logging(fs, logfilepath, TCMU_GLFS_DEBUG_LEVEL);
if (ret < 0) {
tcmu_dev_err(dev, "glfs_set_logging(vol=%s, path=%s) failed: %m\n",
entry->volname, logfilepath);
goto unref;
}
ret = glfs_init(fs);
if (ret) {
tcmu_dev_err(dev, "glfs_init(vol=%s) failed: %m\n", entry->volname);
goto unref;
}
return fs;
unref:
gluster_cache_refresh(fs, config);
fail:
gluster_free_server(hosts);
return NULL;
}
static char* tcmu_get_path( struct tcmu_device *dev)
{
char *config;
config = strchr(tcmu_get_dev_cfgstring(dev), '/');
if (!config) {
tcmu_dev_err(dev, "no configuration found in cfgstring\n");
return NULL;
}
config += 1; /* get past '/' */
return config;
}
static int tcmu_glfs_open(struct tcmu_device *dev)
{
struct glfs_state *gfsp;
char *config;
gfsp = calloc(1, sizeof(*gfsp));
if (!gfsp)
return -ENOMEM;
tcmu_set_dev_private(dev, gfsp);
tcmu_set_dev_write_cache_enabled(dev, 1);
config = tcmu_get_path(dev);
if (!config) {
goto fail;
}
gfsp->fs = tcmu_create_glfs_object(dev, config, &gfsp->hosts);
if (!gfsp->fs) {
tcmu_dev_err(dev, "tcmu_create_glfs_object(config=%s) failed\n", config);
goto fail;
}
gfsp->gfd = glfs_open(gfsp->fs, gfsp->hosts->path, ALLOWED_BSOFLAGS);
if (!gfsp->gfd) {
tcmu_dev_err(dev, "glfs_open(vol=%s, file=%s) failed: %m\n",
gfsp->hosts->volname, gfsp->hosts->path);
goto unref;
}
return 0;
unref:
gluster_cache_refresh(gfsp->fs, tcmu_get_path(dev));
gluster_free_server(&gfsp->hosts);
fail:
free(gfsp);
return -EIO;
}
static void tcmu_glfs_close(struct tcmu_device *dev)
{
struct glfs_state *gfsp = tcmu_get_dev_private(dev);
glfs_close(gfsp->gfd);
gluster_cache_refresh(gfsp->fs, tcmu_get_path(dev));
gluster_free_server(&gfsp->hosts);
free(gfsp);
}
static void glfs_async_cbk(glfs_fd_t *fd, ssize_t ret, void *data)
{
glfs_cbk_cookie *cookie = data;
struct tcmu_device *dev = cookie->dev;
struct tcmulib_cmd *cmd = cookie->cmd;
size_t length = cookie->length;
if (ret < 0 || ret != length) {
/* Read/write/flush failed */
switch (cookie->op) {
case TCMU_GLFS_READ:
ret = tcmu_set_sense_data(cmd->sense_buf, MEDIUM_ERROR,
ASC_READ_ERROR, NULL);
break;
case TCMU_GLFS_WRITE:
case TCMU_GLFS_FLUSH:
ret = tcmu_set_sense_data(cmd->sense_buf, MEDIUM_ERROR,
ASC_WRITE_ERROR, NULL);
break;
}
} else {
ret = SAM_STAT_GOOD;
}
cmd->done(dev, cmd, ret);
free(cookie);
}
static int tcmu_glfs_read(struct tcmu_device *dev,
struct tcmulib_cmd *cmd,
struct iovec *iov, size_t iov_cnt,
size_t length, off_t offset)
{
struct glfs_state *state = tcmu_get_dev_private(dev);
glfs_cbk_cookie *cookie;
cookie = calloc(1, sizeof(*cookie));
if (!cookie) {
tcmu_dev_err(dev, "Could not allocate cookie: %m\n");
goto out;
}
cookie->dev = dev;
cookie->cmd = cmd;
cookie->length = length;
cookie->op = TCMU_GLFS_READ;
if (glfs_preadv_async(state->gfd, iov, iov_cnt, offset, SEEK_SET,
glfs_async_cbk, cookie) < 0) {
tcmu_dev_err(dev, "glfs_preadv_async(vol=%s, file=%s) failed: %m\n",
state->hosts->volname, state->hosts->path);
goto out;
}
return 0;
out:
free(cookie);
return SAM_STAT_TASK_SET_FULL;
}
static int tcmu_glfs_write(struct tcmu_device *dev,
struct tcmulib_cmd *cmd,
struct iovec *iov, size_t iov_cnt,
size_t length, off_t offset)
{
struct glfs_state *state = tcmu_get_dev_private(dev);
glfs_cbk_cookie *cookie;
cookie = calloc(1, sizeof(*cookie));
if (!cookie) {
tcmu_dev_err(dev, "Could not allocate cookie: %m\n");
goto out;
}
cookie->dev = dev;
cookie->cmd = cmd;
cookie->length = length;
cookie->op = TCMU_GLFS_WRITE;
if (glfs_pwritev_async(state->gfd, iov, iov_cnt, offset,
ALLOWED_BSOFLAGS, glfs_async_cbk, cookie) < 0) {
tcmu_dev_err(dev, "glfs_pwritev_async(vol=%s, file=%s) failed: %m\n",
state->hosts->volname, state->hosts->path);
goto out;
}
return 0;
out:
free(cookie);
return SAM_STAT_TASK_SET_FULL;
}
static int tcmu_glfs_reconfig(struct tcmu_device *dev,
struct tcmulib_cfg_info *cfg)
{
switch (cfg->type) {
case TCMULIB_CFG_DEV_SIZE:
/*
* Let glusterfs tools handle size checks
*/
return 0;
case TCMULIB_CFG_DEV_CFGSTR:
case TCMULIB_CFG_WRITE_CACHE:
default:
return -EOPNOTSUPP;
}
}
static int tcmu_glfs_flush(struct tcmu_device *dev,
struct tcmulib_cmd *cmd)
{
struct glfs_state *state = tcmu_get_dev_private(dev);
glfs_cbk_cookie *cookie;
cookie = calloc(1, sizeof(*cookie));
if (!cookie) {
tcmu_dev_err(dev, "Could not allocate cookie: %m\n");
goto out;
}
cookie->dev = dev;
cookie->cmd = cmd;
cookie->length = 0;
cookie->op = TCMU_GLFS_FLUSH;
if (glfs_fdatasync_async(state->gfd, glfs_async_cbk, cookie) < 0) {
tcmu_dev_err(dev, "glfs_fdatasync_async(vol=%s, file=%s) failed: %m\n",
state->hosts->volname, state->hosts->path);
goto out;
}
return 0;
out:
free(cookie);
return SAM_STAT_TASK_SET_FULL;
}
/*
* For backstore creation
*
* Specify volume, hostname and filepath e.g,
*
* $ targetcli /backstores/user:glfs create $blockname $size \
* $volume@$hostname/$filepath
*
* volume: must be the name of an existing Gluster volume.
* hostname: the hostname or the IP address
* filepath: is the path of the backing file in Gluster volume.
*/
static const char glfs_cfg_desc[] =
"glfs config string is of the form:\n"
"\"$volume@$hostname/$filepath\"\n"
"where:\n"
" volume: The volume on the Gluster server\n"
" hostname: The server's hostname\n"
" filepath: The path of the backing file";
struct tcmur_handler glfs_handler = {
.name = "Gluster glfs handler",
.subtype = "glfs",
.cfg_desc = glfs_cfg_desc,
.open = tcmu_glfs_open,
.close = tcmu_glfs_close,
.read = tcmu_glfs_read,
.write = tcmu_glfs_write,
.reconfig = tcmu_glfs_reconfig,
.flush = tcmu_glfs_flush,
};
/* Entry point must be named "handler_init". */
int handler_init(void)
{
int ret;
ret = pthread_mutex_init(&glfs_lock, NULL);
if (ret != 0) {
return -1;
}
ret = tcmur_register_handler(&glfs_handler);
if (ret != 0) {
pthread_mutex_destroy(&glfs_lock);
}
return ret;
}