forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbd.c
1745 lines (1489 loc) · 43.8 KB
/
rbd.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 2016, China Mobile, 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 <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <endian.h>
#include <errno.h>
#include <pthread.h>
#include <scsi/scsi.h>
#include "darray.h"
#include "tcmu-runner.h"
#include "tcmur_cmd_handler.h"
#include "libtcmu.h"
#include "tcmur_device.h"
#include <rbd/librbd.h>
#include <rados/librados.h>
/*
* rbd_lock_acquire exclusive lock support was added in librbd 0.1.11
*/
#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 11)
#define RBD_LOCK_ACQUIRE_SUPPORT
#endif
/* rbd_aio_discard added in 0.1.2 */
#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
#define RBD_DISCARD_SUPPORT
#endif
/*
* rbd_aio_writesame support was added in librbd 1.12.0
*/
#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(1, 12, 0) || LIBRBD_SUPPORTS_WRITESAME
#define RBD_WRITE_SAME_SUPPORT
#endif
/* defined in librbd.h if supported */
#ifdef LIBRBD_SUPPORTS_IOVEC
#if LIBRBD_SUPPORTS_IOVEC
#define RBD_IOVEC_SUPPORT
#endif
#endif
/* defined in librbd.h if supported */
#ifdef LIBRBD_SUPPORTS_COMPARE_AND_WRITE
#if LIBRBD_SUPPORTS_COMPARE_AND_WRITE
#define RBD_COMPARE_AND_WRITE_SUPPORT
#endif
#endif
#define TCMU_RBD_LOCKER_TAG_KEY "tcmu_rbd_locker_tag"
#define TCMU_RBD_LOCKER_TAG_FMT "tcmu_tag=%hu,rbd_client=%s"
#define TCMU_RBD_LOCKER_BUF_LEN 256
struct tcmu_rbd_state {
rados_t cluster;
rados_ioctx_t io_ctx;
rbd_image_t image;
char *image_name;
char *pool_name;
char *osd_op_timeout;
char *conf_path;
char *id;
char *addrs;
};
enum rbd_aio_type {
RBD_AIO_TYPE_WRITE = 0,
RBD_AIO_TYPE_READ,
RBD_AIO_TYPE_CAW
};
struct rbd_aio_cb {
struct tcmu_device *dev;
struct tcmur_cmd *tcmur_cmd;
enum rbd_aio_type type;
union {
struct {
int64_t length;
} read;
struct {
uint64_t offset;
uint64_t miscompare_offset;
} caw;
};
char *bounce_buffer;
struct iovec *iov;
size_t iov_cnt;
};
static pthread_mutex_t blacklist_caches_lock = PTHREAD_MUTEX_INITIALIZER;
static darray(char *) blacklist_caches;
#ifdef LIBRADOS_SUPPORTS_SERVICES
#ifdef RBD_LOCK_ACQUIRE_SUPPORT
static int tcmu_rbd_service_status_update(struct tcmu_device *dev,
bool has_lock)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
char *status_buf = NULL;
int ret;
ret = asprintf(&status_buf,
"%s%c%s%c%s%c%"PRIu64"%c%s%c%"PRIu64"%c%s%c%"PRIu64"%c",
"lock_owner", '\0', has_lock ? "true" : "false", '\0',
"lock_lost_cnt", '\0', rdev->lock_lost_cnt, '\0',
"conn_lost_cnt", '\0', rdev->conn_lost_cnt, '\0',
"cmd_timed_out_cnt", '\0', rdev->cmd_timed_out_cnt, '\0');
if (ret < 0) {
tcmu_dev_err(dev, "Could not allocate status buf. Service will not be updated.\n");
return ret;
}
ret = rados_service_update_status(state->cluster, status_buf);
if (ret < 0) {
tcmu_dev_err(dev, "Could not update service status. (Err %d)\n",
ret);
}
free(status_buf);
return ret;
}
#endif /* RBD_LOCK_ACQUIRE_SUPPORT */
static int tcmu_rbd_report_event(struct tcmu_device *dev)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
/*
* We ignore the specific event and report all the current counter
* values, because tools like gwcli/dashboard may not see every
* update, and we do not want one event to overwrite the info.
*/
return tcmu_rbd_service_status_update(dev,
rdev->lock_state == TCMUR_DEV_LOCK_WRITE_LOCKED ? true : false);
}
static int tcmu_rbd_service_register(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
struct utsname u;
char *daemon_buf = NULL;
char *metadata_buf = NULL;
char *image_id_buf = NULL;
int ret;
ret = uname(&u);
if (ret < 0) {
ret = -errno;
tcmu_dev_err(dev, "Could not query uname. (Err %d)\n", ret);
return ret;
}
image_id_buf = malloc(RBD_MAX_BLOCK_NAME_SIZE);
if (image_id_buf == NULL) {
tcmu_dev_err(dev, "Could not allocate image id buf.\n");
return -ENOMEM;
}
ret = rbd_get_id(state->image, image_id_buf, RBD_MAX_BLOCK_NAME_SIZE);
if (ret < 0) {
tcmu_dev_err(dev, "Could not retrieve image id.\n");
goto free_image_id_buf;
}
ret = asprintf(&daemon_buf, "%s:%s/%s",
u.nodename, state->pool_name, state->image_name);
if (ret < 0) {
tcmu_dev_err(dev, "Could not allocate daemon buf.\n");
ret = -ENOMEM;
goto free_image_id_buf;
}
ret = asprintf(&metadata_buf, "%s%c%s%c%s%c%s%c%s%c%s%c",
"pool_name", '\0', state->pool_name, '\0',
"image_name", '\0', state->image_name, '\0',
"image_id", '\0', image_id_buf, '\0');
if (ret < 0) {
tcmu_dev_err(dev, "Could not allocate metadata buf.\n");
ret = -ENOMEM;
goto free_daemon_buf;
}
ret = rados_service_register(state->cluster, "tcmu-runner",
daemon_buf, metadata_buf);
if (ret < 0) {
tcmu_dev_err(dev, "Could not register service to cluster. (Err %d)\n",
ret);
goto free_meta_buf;
}
ret = tcmu_rbd_report_event(dev);
if (ret < 0)
tcmu_dev_err(dev, "Could not update status. (Err %d)\n", ret);
free_meta_buf:
free(metadata_buf);
free_daemon_buf:
free(daemon_buf);
free_image_id_buf:
free(image_id_buf);
return ret;
}
#else /* LIBRADOS_SUPPORTS_SERVICES */
static int tcmu_rbd_service_register(struct tcmu_device *dev)
{
/* Ignorable. Just log in dbg mode just in case. */
tcmu_dev_dbg(dev, "Ceph service registration not supported.\n");
return 0;
}
#ifdef RBD_LOCK_ACQUIRE_SUPPORT
static void tcmu_rbd_service_status_update(struct tcmu_device *dev,
bool has_lock)
{
}
#endif /* RBD_LOCK_ACQUIRE_SUPPORT */
#endif /* LIBRADOS_SUPPORTS_SERVICES */
#if defined LIBRADOS_SUPPORTS_GETADDRS || defined RBD_LOCK_ACQUIRE_SUPPORT
static void tcmu_rbd_rm_stale_entry_from_blacklist(struct tcmu_device *dev, char *addrs)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
const char *p, *q, *end;
char *cmd, *addr;
int ret;
/*
* Just skip extra chars before '[' if there has
*/
p = strchr(addrs, '[');
if (!p)
p = addrs;
/*
* The addrs will a string like:
* "[192.168.195.172:0/2203456141,192.168.195.172:0/4908756432]"
* Or
* "192.168.195.172:0/2203456141"
*/
while (1) {
if (p == NULL || *p == ']') {
return; /* we are done here */
} else if (*p == '[' || *p == ',') {
/* Skip "[" and white spaces */
while (*p != '\0' && !isalnum(*p)) p++;
if (*p == '\0') {
tcmu_dev_warn(dev, "Get an invalid address '%s'!\n", addrs);
return;
}
end = strchr(p, ',');
if (!end)
end = strchr(p, ']');
if (!end) {
tcmu_dev_warn(dev, "Get an invalid address '%s'!\n", addrs);
return;
}
q = end; /* The *end should be ',' or ']' */
while (*q != '\0' && !isalnum(*q)) q--;
if (*q == '\0') {
tcmu_dev_warn(dev, "Get an invalid address '%s'!\n", addrs);
return;
}
addr = strndup(p, q - p + 1);
p = end;
} else {
/* In case of "192.168.195.172:0/2203456141" */
addr = strdup(p);
p = NULL;
}
ret = asprintf(&cmd,
"{\"prefix\": \"osd blacklist\","
"\"blacklistop\": \"rm\","
"\"addr\": \"%s\"}",
addr);
free(addr);
if (ret < 0) {
tcmu_dev_warn(dev, "Could not allocate command. (Err %d)\n",
ret);
return;
}
ret = rados_mon_command(state->cluster, (const char**)&cmd, 1, NULL, 0,
NULL, NULL, NULL, NULL);
free(cmd);
if (ret < 0) {
tcmu_dev_err(dev, "Could not rm blacklist entry '%s'. (Err %d)\n",
addr, ret);
return;
}
}
}
static int tcmu_rbd_rm_stale_entries_from_blacklist(struct tcmu_device *dev)
{
char **entry, *tmp_entry;
int ret = 0;
int i;
pthread_mutex_lock(&blacklist_caches_lock);
if (darray_empty(blacklist_caches))
goto unlock;
/* Try to remove all the stale blacklist entities */
darray_foreach(entry, blacklist_caches) {
tcmu_dev_info(dev, "removing addrs: {%s}\n", *entry);
tcmu_rbd_rm_stale_entry_from_blacklist(dev, *entry);
}
unlock:
for (i = darray_size(blacklist_caches) - 1; i >= 0; i--) {
tmp_entry = darray_item(blacklist_caches, i);
darray_remove(blacklist_caches, i);
free(tmp_entry);
}
pthread_mutex_unlock(&blacklist_caches_lock);
return ret;
}
#endif // LIBRADOS_SUPPORTS_GETADDRS || RBD_LOCK_ACQUIRE_SUPPORT
static char *tcmu_rbd_find_quote(char *string)
{
/* ignore escaped quotes */
while (true) {
string = strpbrk(string, "\"\\");
if (!string) {
break;
}
if (*string == '"') {
break;
}
if (*++string == '\0') {
break;
}
/* skip past the escaped character */
++string;
}
return string;
}
static bool tcmu_rbd_match_device_class(struct tcmu_device *dev,
const char *crush_rule,
const char *device_class)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
char *mon_cmd_bufs[2] = {NULL, NULL};
char *mon_buf = NULL, *mon_status_buf = NULL;
size_t mon_buf_len = 0, mon_status_buf_len = 0;
int ret;
bool match = false;
/* request a list of crush rules associated to the device class */
ret = asprintf(&mon_cmd_bufs[0],
"{\"prefix\": \"osd crush rule ls-by-class\", "
"\"class\": \"%s\", \"format\": \"json\"}",
device_class);
if (ret < 0) {
tcmu_dev_warn(dev, "Could not allocate crush rule ls-by-class command.\n");
return false;
}
ret = rados_mon_command(state->cluster, (const char **)mon_cmd_bufs, 1,
"", 0, &mon_buf, &mon_buf_len,
&mon_status_buf, &mon_status_buf_len);
free(mon_cmd_bufs[0]);
if (ret == -ENOENT) {
tcmu_dev_dbg(dev, "%s not a registered device class.\n", device_class);
return false;
} else if (ret < 0 || !mon_buf) {
tcmu_dev_warn(dev, "Could not retrieve pool crush rule ls-by-class (Err %d)\n",
ret);
return false;
}
rados_buffer_free(mon_status_buf);
/* expected JSON output: ["<rule name>",["<rule name>"...]] */
mon_buf[mon_buf_len - 1] = '\0';
match = (strstr(mon_buf, crush_rule) != NULL);
rados_buffer_free(mon_buf);
return match;
}
static void tcmu_rbd_detect_device_class(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
char *mon_cmd_bufs[2] = {NULL, NULL};
char *mon_buf = NULL, *mon_status_buf = NULL;
size_t mon_buf_len = 0, mon_status_buf_len = 0;
char *crush_rule = NULL, *crush_rule_end = NULL;
int ret;
/* request the crush rule name for the image's pool */
ret = asprintf(&mon_cmd_bufs[0],
"{\"prefix\": \"osd pool get\", "
"\"pool\": \"%s\", "
"\"var\": \"crush_rule\", "
"\"format\": \"json\"}", state->pool_name);
if (ret < 0) {
tcmu_dev_warn(dev, "Could not allocate crush rule command.\n");
return;
}
ret = rados_mon_command(state->cluster, (const char **)mon_cmd_bufs, 1,
"", 0, &mon_buf, &mon_buf_len,
&mon_status_buf, &mon_status_buf_len);
free(mon_cmd_bufs[0]);
if (ret < 0 || !mon_buf) {
tcmu_dev_warn(dev, "Could not retrieve pool crush rule (Err %d)\n",
ret);
return;
}
rados_buffer_free(mon_status_buf);
/* expected JSON output: "{..."crush_rule":"<rule name>"...}" */
mon_buf[mon_buf_len - 1] = '\0';
crush_rule = strstr(mon_buf, "\"crush_rule\":\"");
if (!crush_rule) {
tcmu_dev_warn(dev, "Could not locate crush rule key\n");
rados_buffer_free(mon_buf);
return;
}
/* skip past key to the start of the quoted rule name */
crush_rule += 13;
crush_rule_end = tcmu_rbd_find_quote(crush_rule + 1);
if (!crush_rule_end) {
tcmu_dev_warn(dev, "Could not extract crush rule\n");
rados_buffer_free(mon_buf);
return;
}
*(crush_rule_end + 1) = '\0';
crush_rule = strdup(crush_rule);
rados_buffer_free(mon_buf);
tcmu_dev_dbg(dev, "Pool %s using crush rule %s\n", state->pool_name,
crush_rule);
if (tcmu_rbd_match_device_class(dev, crush_rule, "ssd") ||
tcmu_rbd_match_device_class(dev, crush_rule, "nvme")) {
tcmu_dev_dbg(dev, "Pool %s associated to solid state device class.\n",
state->pool_name);
tcmu_dev_set_solid_state_media(dev, true);
}
free(crush_rule);
}
static void tcmu_rbd_image_close(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
rbd_close(state->image);
rados_ioctx_destroy(state->io_ctx);
rados_shutdown(state->cluster);
state->cluster = NULL;
state->io_ctx = NULL;
state->image = NULL;
}
static int timer_check_and_set_def(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
char buf[128];
int grace, interval, ret, len;
float timeout;
ret = rados_conf_get(state->cluster, "osd_heartbeat_grace",
buf, 128);
if (ret) {
tcmu_dev_err(dev, "Failed to get cluster's default osd_heartbeat_grace\n");
return ret;
}
grace = atoi(buf);
ret = rados_conf_get(state->cluster, "osd_heartbeat_interval",
buf, 128);
if (ret) {
tcmu_dev_err(dev, "Failed to get cluster's default osd_heartbeat_interval\n");
return ret;
}
interval = atoi(buf);
ret = rados_conf_get(state->cluster, "rados_osd_op_timeout",
buf, 128);
if (ret) {
tcmu_dev_err(dev, "Failed to get cluster's default rados_osd_op_timeout\n");
return ret;
}
timeout = atof(buf);
tcmu_dev_dbg(dev, "The cluster's default osd op timeout(%f), osd heartbeat grace(%d) interval(%d)\n",
timeout, grace, interval);
/* Frist: Try to use new osd op timeout value */
if (state->osd_op_timeout && atof(state->osd_op_timeout) > grace + interval)
goto set;
/* Second: Try to use the default osd op timeout value as read from the cluster */
if (timeout > grace + interval) {
tcmu_dev_dbg(dev, "The osd op timeout will remain the default value: %f\n", timeout);
return 0;
}
tcmu_dev_warn(dev, "osd op timeout (%s) must be larger than osd heartbeat grace (%d) + interval (%d)!\n",
state->osd_op_timeout, grace, interval);
/*
* At last: Set the default rados_osd_op_timeout to grace + interval + 5
* to make sure rados_osd_op_timeout > grace + interval.
*/
len = sprintf(buf, "%d", grace + interval + 5);
buf[len] = '\0';
if (state->osd_op_timeout)
free(state->osd_op_timeout);
state->osd_op_timeout = strdup(buf);
if (!state->osd_op_timeout) {
tcmu_dev_err(dev, "Failed to alloc memory for ->osd_op_timeout\n");
return -ENOMEM;
}
tcmu_dev_warn(dev, "Will set the osd op timeout to %s instead!\n",
state->osd_op_timeout);
set:
return rados_conf_set(state->cluster, "rados_osd_op_timeout",
state->osd_op_timeout);
}
static int tcmu_rbd_image_open(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
int ret;
ret = rados_create(&state->cluster, state->id);
if (ret < 0) {
tcmu_dev_err(dev, "Could not create cluster. (Err %d)\n", ret);
return ret;
}
/* Try default location when conf_path=NULL, but ignore failure */
ret = rados_conf_read_file(state->cluster, state->conf_path);
if (state->conf_path && ret < 0) {
tcmu_dev_err(dev, "Could not read config %s (Err %d)",
state->conf_path, ret);
goto rados_shutdown;
}
rados_conf_set(state->cluster, "rbd_cache", "false");
ret = timer_check_and_set_def(dev);
if (ret)
tcmu_dev_warn(dev,
"Could not set rados osd op timeout to %s (Err %d. Failover may be delayed.)\n",
state->osd_op_timeout, ret);
ret = rados_connect(state->cluster);
if (ret < 0) {
tcmu_dev_err(dev, "Could not connect to cluster. (Err %d)\n",
ret);
goto rados_shutdown;
}
tcmu_rbd_detect_device_class(dev);
ret = rados_ioctx_create(state->cluster, state->pool_name,
&state->io_ctx);
if (ret < 0) {
tcmu_dev_err(dev, "Could not create ioctx for pool %s. (Err %d)\n",
state->pool_name, ret);
goto rados_shutdown;
}
ret = rbd_open(state->io_ctx, state->image_name, &state->image, NULL);
if (ret < 0) {
tcmu_dev_err(dev, "Could not open image %s. (Err %d)\n",
state->image_name, ret);
goto rados_destroy;
}
ret = tcmu_rbd_service_register(dev);
if (ret < 0)
goto rbd_close;
return 0;
rbd_close:
rbd_close(state->image);
state->image = NULL;
rados_destroy:
rados_ioctx_destroy(state->io_ctx);
state->io_ctx = NULL;
rados_shutdown:
rados_shutdown(state->cluster);
state->cluster = NULL;
return ret;
}
#ifdef RBD_LOCK_ACQUIRE_SUPPORT
/*
* Returns:
* 0 = client is not owner.
* 1 = client is owner.
* -ESHUTDOWN/-EBLACKLISTED(-108) = client is blacklisted.
* -ETIMEDOUT = rados osd op timeout has expired.
* -EIO = misc error.
*/
static int tcmu_rbd_has_lock(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
int ret, is_owner;
ret = rbd_is_exclusive_lock_owner(state->image, &is_owner);
if (ret < 0) {
if (ret == -ESHUTDOWN) {
tcmu_dev_dbg(dev, "Client is blacklisted. Could not check lock ownership.\n");
} else {
tcmu_dev_err(dev, "Could not check lock ownership. Error: %s.\n",
strerror(-ret));
}
if (ret == -ESHUTDOWN || ret == -ETIMEDOUT)
return ret;
/* let initiator figure things out */
return -EIO;
} else if (is_owner) {
tcmu_dev_dbg(dev, "Is owner\n");
return 1;
}
tcmu_dev_dbg(dev, "Not owner\n");
return 0;
}
static int tcmu_rbd_get_lock_state(struct tcmu_device *dev)
{
int ret;
ret = tcmu_rbd_has_lock(dev);
if (ret == 1)
return TCMUR_DEV_LOCK_WRITE_LOCKED;
else if (ret == 0 || ret == -ESHUTDOWN)
return TCMUR_DEV_LOCK_UNLOCKED;
else
return TCMUR_DEV_LOCK_UNKNOWN;
}
/**
* tcmu_rbd_lock_break - break rbd exclusive lock if needed
* @dev: device to break the lock for.
*
* Returns:
* 0 = lock has been broken.
* -ETIMEDOUT = could not complete operation in rados osd op timeout seconds.
* -Ezyx = misc failure.
*/
static int tcmu_rbd_lock_break(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
rbd_lock_mode_t lock_mode;
char *owners[1];
size_t num_owners = 1;
int ret;
ret = rbd_lock_get_owners(state->image, &lock_mode, owners,
&num_owners);
if (ret == -ENOENT || (!ret && !num_owners))
return 0;
if (ret < 0) {
tcmu_dev_err(dev, "Could not get lock owners to break lock %d\n",
ret);
return ret;
}
if (lock_mode != RBD_LOCK_MODE_EXCLUSIVE) {
tcmu_dev_err(dev, "Invalid lock type (%d) found\n", lock_mode);
ret = -EIO;
goto free_owners;
}
tcmu_dev_dbg(dev, "Attempting to break lock from %s.\n", owners[0]);
ret = rbd_lock_break(state->image, lock_mode, owners[0]);
if (ret < 0)
tcmu_dev_err(dev, "Could not break lock from %s. (Err %d)\n",
owners[0], ret);
free_owners:
rbd_lock_get_owners_cleanup(owners, num_owners);
return ret;
}
static int tcmu_rbd_to_sts(int rc)
{
switch (rc) {
case 0:
return TCMU_STS_OK;
case -ESHUTDOWN:
return TCMU_STS_FENCED;
case -ENOENT:
return TCMU_STS_NO_LOCK_HOLDERS;
case -ETIMEDOUT:
return TCMU_STS_TIMEOUT;
case -ENOMEM:
return TCMU_STS_NO_RESOURCE;
default:
return TCMU_STS_HW_ERR;
}
}
static int tcmu_rbd_get_lock_tag(struct tcmu_device *dev, uint16_t *tag)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
char *metadata_owner, *owners[1];
size_t num_owners = 1;
rbd_lock_mode_t lock_mode;
char buf[TCMU_RBD_LOCKER_BUF_LEN];
size_t buf_len = TCMU_RBD_LOCKER_BUF_LEN;
int ret;
memset(buf, 0, buf_len);
ret = rbd_metadata_get(state->image, TCMU_RBD_LOCKER_TAG_KEY,
buf, &buf_len);
tcmu_dev_dbg(dev, "get meta got %d %s\n", ret, buf);
if (ret)
goto done;
ret = rbd_lock_get_owners(state->image, &lock_mode, owners,
&num_owners);
tcmu_dev_dbg(dev, "get lockowner got %d\n", ret);
if (ret)
goto done;
if (!num_owners) {
/* there would be stale metadata due to a crash */
ret = -ENOENT;
goto done;
}
metadata_owner = strstr(buf, "rbd_client=");
if (!metadata_owner) {
tcmu_dev_err(dev, "Invalid lock tag %s.\n", buf);
/* Force initiator to retry STPG */
ret = -ENOENT;
goto free_owners;
}
metadata_owner += 11;
if (strcmp(metadata_owner, owners[0])) {
tcmu_dev_dbg(dev, "owner mismatch %s %s\n", metadata_owner,
owners[0]);
/*
* A node could be in the middle of grabbing the lock or it
* failed midway. Force tcmu to report all standby so the
* initiator retries the STPG for the failure case.
*/
ret = -ENOENT;
goto free_owners;
}
ret = sscanf(buf, "tcmu_tag=%hu,%*s", tag);
if (ret != 1) {
tcmu_dev_err(dev, "Invalid lock tag %s.\n", buf);
/* Force initiator to retry STPG */
ret = -ENOENT;
goto free_owners;
}
ret = 0;
free_owners:
if (num_owners)
rbd_lock_get_owners_cleanup(owners, num_owners);
done:
return tcmu_rbd_to_sts(ret);
}
static int tcmu_rbd_set_lock_tag(struct tcmu_device *dev, uint16_t tcmu_tag)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
rbd_lock_mode_t lock_mode;
char *owners[1];
size_t num_owners = 1;
char *tcmu_rbd_tag;
int ret;
/*
* We cannot take the lock and set the tag atomically. In case
* we fail here and are in an inconsistent state, we attach the rbd
* client lock info along with the tcmu locker tag so remote nodes
* can check the rbd info against rbd_lock_get_owners to determine if
* the tcmu locker tag is current/valid.
*/
ret = rbd_lock_get_owners(state->image, &lock_mode, owners,
&num_owners);
tcmu_dev_dbg(dev, "set tag get lockowner got %d %zd\n", ret, num_owners);
if (ret)
return ret;
if (!num_owners)
return -ENOENT;
ret = asprintf(&tcmu_rbd_tag, TCMU_RBD_LOCKER_TAG_FMT, tcmu_tag,
owners[0]);
if (ret < 0) {
ret = -ENOMEM;
goto free_owners;
}
ret = rbd_metadata_set(state->image, TCMU_RBD_LOCKER_TAG_KEY,
tcmu_rbd_tag);
free(tcmu_rbd_tag);
if (ret)
tcmu_dev_err(dev, "Could not store lock tag. Err %d.\n", ret);
free_owners:
if (num_owners)
rbd_lock_get_owners_cleanup(owners, num_owners);
return ret;
}
static int tcmu_rbd_unlock(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
int ret;
ret = tcmu_rbd_has_lock(dev);
if (ret == 0)
return TCMU_STS_OK;
else if (ret < 0)
return tcmu_rbd_to_sts(ret);
ret = rbd_lock_release(state->image);
if (!ret)
return TCMU_STS_OK;
tcmu_dev_err(dev, "Could not release lock. Err %d.\n", ret);
return tcmu_rbd_to_sts(ret);
}
static int tcmu_rbd_lock(struct tcmu_device *dev, uint16_t tag)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
#if !defined LIBRADOS_SUPPORTS_GETADDRS && defined RBD_LOCK_ACQUIRE_SUPPORT
rbd_lock_mode_t lock_mode;
char *owners1[1], *owners2[1];
size_t num_owners1 = 1, num_owners2 = 1;
#endif
int ret;
ret = tcmu_rbd_has_lock(dev);
if (ret == 1) {
ret = 0;
/*
* We might have failed after getting the lock, but
* before we set the meta data.
*/
goto set_lock_tag;
} else if (ret)
goto done;
ret = tcmu_rbd_lock_break(dev);
if (ret)
goto done;
ret = rbd_lock_acquire(state->image, RBD_LOCK_MODE_EXCLUSIVE);
if (ret)
goto done;
#if !defined LIBRADOS_SUPPORTS_GETADDRS && defined RBD_LOCK_ACQUIRE_SUPPORT
ret = rbd_lock_get_owners(state->image, &lock_mode, owners1,
&num_owners1);
if ((!ret && !num_owners1) || ret < 0) {
tcmu_dev_warn(dev, "Could not get lock owners to store blacklist entry %d!\n",
ret);
} else {
int is_owner;
/* To check whether we are still the lock owner */
ret = rbd_is_exclusive_lock_owner(state->image, &is_owner);
if (ret) {
rbd_lock_get_owners_cleanup(owners1, num_owners1);
tcmu_dev_warn(dev, "Could not check lock owners to store blacklist entry %d!\n",
ret);
goto no_owner;
}
/* To get the lock owner again */
ret = rbd_lock_get_owners(state->image, &lock_mode, owners2,
&num_owners2);
if ((!ret && !num_owners2) || ret < 0) {
tcmu_dev_warn(dev, "Could not get lock owners to store blacklist entry %d!\n",
ret);
/* Only we didn't lose the lock during the above check will we store the blacklist list */
} else if (!strcmp(owners1[0], owners2[0]) && is_owner) {
state->addrs = strdup(owners1[0]); // ignore the errors
}
rbd_lock_get_owners_cleanup(owners1, num_owners1);
rbd_lock_get_owners_cleanup(owners2, num_owners2);
}
no_owner:
#endif
set_lock_tag:
tcmu_dev_warn(dev, "Acquired exclusive lock.\n");
if (tag != TCMU_INVALID_LOCK_TAG)
ret = tcmu_rbd_set_lock_tag(dev, tag);
done:
tcmu_rbd_service_status_update(dev, ret == 0 ? true : false);
return tcmu_rbd_to_sts(ret);
}
static void tcmu_rbd_check_excl_lock_enabled(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
uint64_t features = 0;
int ret;
ret = rbd_get_features(state->image, &features);
if (ret) {
tcmu_dev_warn(dev, "Could not get rbd features. HA may not be supported. Err %d.\n", ret);
return;
}
if (!(features & RBD_FEATURE_EXCLUSIVE_LOCK)) {
tcmu_dev_warn(dev, "exclusive-lock not enabled for image. HA not supported.\n");
}
}
#else /* RBD_LOCK_ACQUIRE_SUPPORT */
static void tcmu_rbd_check_excl_lock_enabled(struct tcmu_device *dev)
{
tcmu_dev_warn(dev, "HA not supported.\n");
}
#endif /* RBD_LOCK_ACQUIRE_SUPPORT */
static void tcmu_rbd_state_free(struct tcmu_rbd_state *state)
{
if (state->conf_path)
free(state->conf_path);
if (state->osd_op_timeout)
free(state->osd_op_timeout);
if (state->image_name)
free(state->image_name);
if (state->pool_name)
free(state->pool_name);
if (state->id)
free(state->id);
if (state->addrs)
free(state->addrs);
free(state);
}
static int tcmu_rbd_check_image_size(struct tcmu_device *dev, uint64_t new_size)
{
struct tcmu_rbd_state *state = tcmur_dev_get_private(dev);
uint64_t rbd_size;