-
Notifications
You must be signed in to change notification settings - Fork 148
/
glfs.c
1014 lines (855 loc) · 22.9 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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015 Red Hat, Inc.
*
* This file is licensed to you under your choice of the GNU Lesser
* General Public License, version 2.1 or any later version (LGPLv2.1 or
* later), or the Apache License 2.0.
*/
#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"
#include "tcmur_device.h"
#include "tcmur_cmd_handler.h"
#include "version.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 7
/* cache protection */
pthread_mutex_t glfs_lock;
#if GFAPI_VERSION766
#define GF_ENFORCE_MANDATORY_LOCK "trusted.glusterfs.enforce-mandatory-lock"
#endif
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;
bool no_fencing;
/*
* 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 tcmur_cmd *tcmur_cmd;
size_t length;
enum {
TCMU_GLFS_READ = 1,
TCMU_GLFS_WRITE = 2,
TCMU_GLFS_FLUSH = 3,
TCMU_GLFS_DISCARD = 4,
TCMU_GLFS_WRITESAME = 5
} 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;
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 bool tcmu_glfs_update_logdir(void)
{
struct gluster_cacheconn **entry;
char logfilepath[PATH_MAX];
int ret;
darray_foreach(entry, glfs_cache) {
ret = tcmu_make_absolute_logfile(logfilepath, TCMU_GLFS_LOG_FILENAME);
if (ret < 0) {
tcmu_err("tcmu_make_absolute_logfile failed: %d\n", ret);
return false;
}
if (glfs_set_logging((*entry)->fs, logfilepath, TCMU_GLFS_DEBUG_LEVEL)) {
tcmu_err("glfs_set_logging() on %s failed[%s]",
(*entry)->volname, strerror(errno));
return false;
}
}
return true;
}
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_dev_get_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, bool reopen)
{
struct glfs_state *gfsp;
char *config;
struct stat st;
int ret = -EIO;
long long dev_size;
uint32_t block_size = tcmu_dev_get_block_size(dev);
gfsp = calloc(1, sizeof(*gfsp));
if (!gfsp)
return -ENOMEM;
tcmur_dev_set_private(dev, gfsp);
tcmu_dev_set_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;
}
#if GFAPI_VERSION766
ret = glfs_fsetxattr(gfsp->gfd, GF_ENFORCE_MANDATORY_LOCK, "set", 4, 0);
if (ret) {
if (errno == EINVAL) {
gfsp->no_fencing = true;
} else {
tcmu_dev_err(dev,"glfs_fsetxattr failed: %m\n");
goto close;
}
}
#endif
ret = glfs_lstat(gfsp->fs, gfsp->hosts->path, &st);
if (ret) {
tcmu_dev_warn(dev, "glfs_lstat failed: %m\n");
goto close;
}
dev_size = tcmu_dev_get_num_lbas(dev) * block_size;
if (st.st_size != dev_size) {
/*
* The glfs allows the backend file size not to align
* to the block_size. But the dev_size here in tcmu-runner
* will round down and align it to the block_size.
*/
if (round_down(st.st_size, block_size) == dev_size)
goto out;
if (reopen)
goto out;
tcmu_dev_warn(dev,
"device size (%lld) and backing file size (%lld) not matching, updating it to kernel\n",
(long long)dev_size, (long long) st.st_size);
/* Update the device size in kernel. */
ret = tcmur_dev_update_size(dev, st.st_size);
if (ret)
goto close;
tcmu_dev_info(dev, "loaded with size (%lld)\n", (long long) st.st_size);
}
out:
return 0;
close:
glfs_close(gfsp->gfd);
unref:
gluster_cache_refresh(gfsp->fs, tcmu_get_path(dev));
gluster_free_server(&gfsp->hosts);
fail:
free(gfsp);
return ret;
}
static void tcmu_glfs_close(struct tcmu_device *dev)
{
struct glfs_state *gfsp = tcmur_dev_get_private(dev);
glfs_close(gfsp->gfd);
gluster_cache_refresh(gfsp->fs, tcmu_get_path(dev));
gluster_free_server(&gfsp->hosts);
free(gfsp);
}
#if GFAPI_VERSION760
static void glfs_async_cbk(glfs_fd_t *fd, ssize_t ret,
struct glfs_stat *prestat,
struct glfs_stat *poststat,
void *data)
#else
static void glfs_async_cbk(glfs_fd_t *fd, ssize_t ret, void *data)
#endif
{
glfs_cbk_cookie *cookie = data;
struct tcmu_device *dev = cookie->dev;
struct tcmur_cmd *tcmur_cmd = cookie->tcmur_cmd;
#if GFAPI_VERSION766
struct glfs_state *gfsp = tcmur_dev_get_private(dev);
#endif
size_t length = cookie->length;
int err = -errno;
if (ret < 0) {
switch (err) {
case -ETIMEDOUT:
/*
* TIMEDOUT is a scenario where the fop can
* not reach the server for 30 minutes.
*/
tcmu_dev_err(dev, "Timing out cmd after 30 minutes.\n");
tcmu_notify_cmd_timed_out(dev);
ret = TCMU_STS_TIMEOUT;
break;
#if GFAPI_VERSION766
case -EAGAIN:
case -EBUSY:
case -ENOTCONN:
/*
* The lock maybe preemted and then any IO to
* land on the file without a lock will be
* rejected with EBUSY.
*
* And if local is disconnected, we should get
* ENOTCONN for further requests. But if the
* connection is reestablished and at the same
* time another client has taken the lock we
* will get EBUSY too.
*/
if (!gfsp->no_fencing) {
tcmu_dev_dbg(dev, "failed with errno %d.\n", err);
tcmu_notify_lock_lost(dev);
ret = TCMU_STS_BUSY;
break;
}
#endif
default:
tcmu_dev_dbg(dev, "failed with errno %d.\n", err);
ret = TCMU_STS_HW_ERR;
}
} else if (ret != length) {
tcmu_dev_dbg(dev, "ret(%zu) != length(%zu).\n", ret, length);
/* Read/write/flush failed */
switch (cookie->op) {
case TCMU_GLFS_READ:
/* ENOENT for READ operation means EOF,
* see glusterfs commit 9fe5c6d3
*/
if (err == -ENOENT) {
ret = TCMU_STS_OK;
break;
}
ret = TCMU_STS_RD_ERR;
break;
case TCMU_GLFS_WRITE:
case TCMU_GLFS_FLUSH:
case TCMU_GLFS_DISCARD:
case TCMU_GLFS_WRITESAME:
ret = TCMU_STS_WR_ERR;
break;
}
} else {
ret = TCMU_STS_OK;
}
tcmur_cmd_complete(dev, tcmur_cmd, ret);
free(cookie);
}
static int tcmu_glfs_read(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd,
struct iovec *iov, size_t iov_cnt,
size_t length, off_t offset)
{
struct glfs_state *state = tcmur_dev_get_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->tcmur_cmd = tcmur_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 TCMU_STS_OK;
out:
free(cookie);
return TCMU_STS_NO_RESOURCE;
}
static int tcmu_glfs_write(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd,
struct iovec *iov, size_t iov_cnt,
size_t length, off_t offset)
{
struct glfs_state *state = tcmur_dev_get_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->tcmur_cmd = tcmur_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 TCMU_STS_OK;
out:
free(cookie);
return TCMU_STS_NO_RESOURCE;
}
static int tcmu_glfs_reconfig(struct tcmu_device *dev,
struct tcmulib_cfg_info *cfg)
{
struct glfs_state *gfsp = tcmur_dev_get_private(dev);
struct stat st;
int ret = -EIO;
switch (cfg->type) {
case TCMULIB_CFG_DEV_SIZE:
ret = glfs_lstat(gfsp->fs, gfsp->hosts->path, &st);
if (ret) {
tcmu_dev_warn(dev, "glfs_lstat failed: %m\n");
tcmu_notify_conn_lost(dev);
/* Let the targetcli command return success */
ret = 0;
} else if (st.st_size != cfg->data.dev_size) {
/*
* Currently we cannot update the size to kernel here,
* because it will be overrided by kernel with the old
* value after it gets the genl reply.
*/
tcmu_dev_warn(dev,
"device size (%lld) and backing file size (%lld) not matching, and ignoring it\n",
(long long)cfg->data.dev_size, (long long) st.st_size);
return -EINVAL;
}
return ret;
case TCMULIB_CFG_DEV_CFGSTR:
case TCMULIB_CFG_WRITE_CACHE:
default:
return -EOPNOTSUPP;
}
}
static int tcmu_glfs_flush(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd)
{
struct glfs_state *state = tcmur_dev_get_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->tcmur_cmd = tcmur_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 TCMU_STS_OK;
out:
free(cookie);
return TCMU_STS_NO_RESOURCE;
}
static int tcmu_glfs_discard(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd,
uint64_t offset, uint64_t length)
{
struct glfs_state *state = tcmur_dev_get_private(dev);
glfs_cbk_cookie *cookie;
ssize_t ret;
cookie = calloc(1, sizeof(*cookie));
if (!cookie) {
tcmu_dev_err(dev, "Could not allocate cookie: %m\n");
goto out;
}
cookie->dev = dev;
cookie->tcmur_cmd = tcmur_cmd;
cookie->length = 0;
cookie->op = TCMU_GLFS_DISCARD;
ret = glfs_discard_async(state->gfd, offset, length, glfs_async_cbk, cookie);
if (ret < 0) {
tcmu_dev_err(dev, "glfs_discard_async(vol=%s, file=%s) failed: %m\n",
state->hosts->volname, state->hosts->path);
goto out;
}
return TCMU_STS_OK;
out:
free(cookie);
return TCMU_STS_NO_RESOURCE;
}
static int tcmu_glfs_writesame(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd,
uint64_t offset, uint64_t length,
struct iovec *iov, size_t iov_cnt)
{
struct glfs_state *state = tcmur_dev_get_private(dev);
glfs_cbk_cookie *cookie;
ssize_t ret;
if (!tcmu_iovec_zeroed(iov, iov_cnt)) {
tcmu_dev_warn(dev,
"Received none zeroed data, will fall back to writesame emulator instead.\n");
return TCMU_STS_NOT_HANDLED;
}
cookie = calloc(1, sizeof(*cookie));
if (!cookie) {
tcmu_dev_err(dev, "Could not allocate cookie: %m\n");
goto out;
}
cookie->dev = dev;
cookie->tcmur_cmd = tcmur_cmd;
cookie->length = 0;
cookie->op = TCMU_GLFS_WRITESAME;
ret = glfs_zerofill_async(state->gfd, offset, length, glfs_async_cbk, cookie);
if (ret < 0) {
tcmu_dev_err(dev, "glfs_zerofill_async(vol=%s, file=%s) failed: %m\n",
state->hosts->volname, state->hosts->path);
goto out;
}
return TCMU_STS_OK;
out:
free(cookie);
return TCMU_STS_NO_RESOURCE;
}
#if GFAPI_VERSION766
static int tcmu_glfs_to_sts(int rc)
{
switch (rc) {
case 0:
return TCMU_STS_OK;
case -ENOTCONN:
return TCMU_STS_FENCED;
default:
return TCMU_STS_HW_ERR;
}
}
static int tcmu_glfs_lock(struct tcmu_device *dev, uint16_t tag)
{
struct glfs_state *state = tcmur_dev_get_private(dev);
struct flock lock;
int ret;
if (state->no_fencing)
return 0;
lock.l_type = F_WRLCK | F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
ret = glfs_file_lock(state->gfd, F_SETLK, &lock, GLFS_LK_MANDATORY);
if (ret)
tcmu_dev_err(dev, "glfs_file_lock failed: %m\n");
return tcmu_glfs_to_sts(ret);
}
static int tcmu_glfs_unlock(struct tcmu_device *dev)
{
struct glfs_state *state = tcmur_dev_get_private(dev);
struct flock lock;
int ret;
if (state->no_fencing)
return 0;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
ret = glfs_file_lock(state->gfd, F_SETLK, &lock, GLFS_LK_MANDATORY);
if (ret)
tcmu_dev_err(dev, "glfs_file_lock failed: %m\n");
return tcmu_glfs_to_sts(ret);
}
#endif
static int tcmu_glfs_init(void)
{
darray_init(glfs_cache);
return 0;
}
static void tcmu_glfs_destroy(void)
{
darray_free(glfs_cache);
}
/*
* 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,
.unmap = tcmu_glfs_discard,
.writesame = tcmu_glfs_writesame,
.update_logdir = tcmu_glfs_update_logdir,
#if GFAPI_VERSION766
.lock = tcmu_glfs_lock,
.unlock = tcmu_glfs_unlock,
#endif
.init = tcmu_glfs_init,
.destroy = tcmu_glfs_destroy,
};
/* Entry point must be named "handler_init". */