forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbd.c
1371 lines (1168 loc) · 34 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.
*
* 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 <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 "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
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;
};
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 tcmulib_cmd *tcmulib_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;
};
#ifdef LIBRADOS_SUPPORTS_SERVICES
#ifdef RBD_LOCK_ACQUIRE_SUPPORT
static void tcmu_rbd_service_status_update(struct tcmu_device *dev,
bool has_lock)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
char *status_buf = NULL;
int ret;
ret = asprintf(&status_buf, "%s%c%s%c", "lock_owner", '\0',
has_lock ? "true" : "false", '\0');
if (ret < 0) {
tcmu_dev_err(dev, "Could not allocate status buf. Service will not be updated.\n");
return;
}
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);
}
#endif /* RBD_LOCK_ACQUIRE_SUPPORT */
static int tcmu_rbd_service_register(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_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);
}
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 */
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 void tcmu_rbd_detect_device_class(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_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] = '\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);
/* request a list of crush rules associated to SSD device class */
ret = asprintf(&mon_cmd_bufs[0],
"{\"prefix\": \"osd crush rule ls-by-class\", "
"\"class\": \"ssd\", \"format\": \"json\"}");
if (ret < 0) {
tcmu_dev_warn(dev, "Could not allocate crush rule ls-by-class command.\n");
goto free_crush_rule;
}
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, "SSD not a registered device class.\n");
goto free_crush_rule;
} else if (ret < 0 || !mon_buf) {
tcmu_dev_warn(dev, "Could not retrieve pool crush rule ls-by-class (Err %d)\n",
ret);
goto free_crush_rule;
}
rados_buffer_free(mon_status_buf);
/* expected JSON output: ["<rule name>",["<rule name>"...]] */
mon_buf[mon_buf_len] = '\0';
if (strstr(mon_buf, crush_rule)) {
tcmu_dev_dbg(dev, "Pool %s associated to SSD device class.\n",
state->pool_name);
tcmu_set_dev_solid_state_media(dev, true);
}
rados_buffer_free(mon_buf);
free_crush_rule:
free(crush_rule);
}
static void tcmu_rbd_image_close(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_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 = tcmu_get_dev_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 = tcmu_get_dev_private(dev);
int ret;
ret = rados_create(&state->cluster, NULL);
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 = tcmu_get_dev_private(dev);
int ret, is_owner;
ret = rbd_is_exclusive_lock_owner(state->image, &is_owner);
if (ret == -ESHUTDOWN || ret == -ETIMEDOUT) {
return ret;
} else if (ret < 0) {
/* let initiator figure things out */
tcmu_dev_err(dev, "Could not check lock ownership. (Err %d).\n", ret);
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_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.
* @orig_owner: if non null, only break the lock if get owners matches
*
* If orig_owner is null and tcmu_rbd_lock_break fails to break the lock
* for a retryable error (-EAGAIN) the owner of the lock will be returned.
* The caller must free the string returned.
*
* Returns:
* 0 = lock has been broken.
* -EAGAIN = retryable error
* -ETIMEDOUT = could not complete operation in rados osd op timeout seconds.
* -EIO = hard failure.
*/
static int tcmu_rbd_lock_break(struct tcmu_device *dev, char **orig_owner)
{
struct tcmu_rbd_state *state = tcmu_get_dev_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 %d\n", ret);
if (ret == -ETIMEDOUT)
return ret;
else
return -EAGAIN;
}
if (lock_mode != RBD_LOCK_MODE_EXCLUSIVE) {
tcmu_dev_err(dev, "Invalid lock type (%d) found\n", lock_mode);
ret = -EIO;
goto free_owners;
}
if (*orig_owner && strcmp(*orig_owner, owners[0])) {
/* someone took the lock while we were retrying */
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);
if (ret == -ETIMEDOUT)
goto free_owners;
ret = -EAGAIN;
if (!*orig_owner) {
*orig_owner = strdup(owners[0]);
if (!*orig_owner)
ret = -EIO;
}
}
free_owners:
rbd_lock_get_owners_cleanup(owners, num_owners);
return ret;
}
static int tcmu_rbd_lock(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
int ret = 0, attempts = 0;
char *orig_owner = NULL;
/*
* TODO: Add retry/timeout settings to handle windows/ESX.
* Or, set to transitioning and grab the lock in the background.
*/
while (attempts++ < 5) {
ret = tcmu_rbd_has_lock(dev);
if (ret == 1) {
ret = 0;
break;
} else if (ret == -ETIMEDOUT || ret == -ESHUTDOWN) {
break;
} else if (ret < 0) {
sleep(1);
continue;
}
ret = tcmu_rbd_lock_break(dev, &orig_owner);
if (ret == -EIO || ret == -ETIMEDOUT) {
break;
} else if (ret == -EAGAIN) {
sleep(1);
continue;
}
ret = rbd_lock_acquire(state->image, RBD_LOCK_MODE_EXCLUSIVE);
if (!ret) {
tcmu_dev_warn(dev, "Acquired exclusive lock.\n");
break;
} else if (ret == -ETIMEDOUT) {
break;
}
tcmu_dev_err(dev, "Unknown error %d while trying to acquire lock.\n",
ret);
}
if (orig_owner)
free(orig_owner);
if (ret == -ETIMEDOUT || ret == -ESHUTDOWN)
ret = TCMUR_LOCK_NOTCONN;
else if (ret)
ret = TCMUR_LOCK_FAILED;
else
ret = TCMUR_LOCK_SUCCESS;
tcmu_rbd_service_status_update(dev, ret == TCMUR_LOCK_SUCCESS ?
true : false);
return ret;
}
static void tcmu_rbd_check_excl_lock_enabled(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_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);
free(state);
}
static int tcmu_rbd_check_image_size(struct tcmu_device *dev, uint64_t new_size)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
uint64_t rbd_size;
int ret;
ret = rbd_get_size(state->image, &rbd_size);
if (ret < 0) {
tcmu_dev_err(dev, "Could not get rbd size from cluster. Err %d.\n",
ret);
return ret;
}
if (new_size != rbd_size) {
tcmu_dev_err(dev, "Mismatched sizes. RBD image size %" PRIu64 ". Requested new size %" PRIu64 ".\n",
rbd_size, new_size);
return -EINVAL;
}
return 0;
}
static int tcmu_rbd_open(struct tcmu_device *dev, bool reopen)
{
rbd_image_info_t image_info;
char *pool, *name, *next_opt;
char *config, *dev_cfg_dup;
struct tcmu_rbd_state *state;
int ret;
state = calloc(1, sizeof(*state));
if (!state)
return -ENOMEM;
tcmu_set_dev_private(dev, state);
dev_cfg_dup = strdup(tcmu_get_dev_cfgstring(dev));
config = dev_cfg_dup;
if (!config) {
ret = -ENOMEM;
goto free_state;
}
tcmu_dev_dbg(dev, "tcmu_rbd_open config %s block size %u num lbas %" PRIu64 ".\n",
config, tcmu_get_dev_block_size(dev),
tcmu_get_dev_num_lbas(dev));
config = strchr(config, '/');
if (!config) {
tcmu_dev_err(dev, "no configuration found in cfgstring\n");
ret = -EINVAL;
goto free_config;
}
config += 1; /* get past '/' */
pool = strtok(config, "/");
if (!pool) {
tcmu_dev_err(dev, "Could not get pool name\n");
ret = -EINVAL;
goto free_config;
}
state->pool_name = strdup(pool);
if (!state->pool_name) {
ret = -ENOMEM;
tcmu_dev_err(dev, "Could not copy pool name\n");
goto free_config;
}
name = strtok(NULL, ";");
if (!name) {
tcmu_dev_err(dev, "Could not get image name\n");
ret = -EINVAL;
goto free_config;
}
state->image_name = strdup(name);
if (!state->image_name) {
ret = -ENOMEM;
tcmu_dev_err(dev, "Could not copy image name\n");
goto free_config;
}
/* The next options are optional */
next_opt = strtok(NULL, ";");
while (next_opt) {
if (!strncmp(next_opt, "osd_op_timeout=", 15)) {
state->osd_op_timeout = strdup(next_opt + 15);
if (!state->osd_op_timeout ||
!strlen(state->osd_op_timeout)) {
ret = -ENOMEM;
tcmu_dev_err(dev, "Could not copy osd op timeout.\n");
goto free_config;
}
} else if (!strncmp(next_opt, "conf=", 5)) {
state->conf_path = strdup(next_opt + 5);
if (!state->conf_path || !strlen(state->conf_path)) {
ret = -ENOMEM;
tcmu_dev_err(dev, "Could not copy conf path.\n");
goto free_config;
}
}
next_opt = strtok(NULL, ";");
}
ret = tcmu_rbd_image_open(dev);
if (ret < 0) {
goto free_config;
}
tcmu_rbd_check_excl_lock_enabled(dev);
ret = tcmu_rbd_check_image_size(dev, tcmu_get_dev_block_size(dev) *
tcmu_get_dev_num_lbas(dev));
if (ret) {
goto stop_image;
}
ret = rbd_stat(state->image, &image_info, sizeof(image_info));
if (ret < 0) {
tcmu_dev_err(dev, "Could not stat image.\n");
goto stop_image;
}
tcmu_set_dev_write_cache_enabled(dev, 0);
free(dev_cfg_dup);
return 0;
stop_image:
tcmu_rbd_image_close(dev);
free_config:
free(dev_cfg_dup);
free_state:
tcmu_rbd_state_free(state);
return ret;
}
static void tcmu_rbd_close(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
tcmu_rbd_image_close(dev);
tcmu_rbd_state_free(state);
}
static int tcmu_rbd_handle_blacklisted_cmd(struct tcmu_device *dev,
struct tcmulib_cmd *cmd)
{
tcmu_notify_lock_lost(dev);
/*
* This will happen during failback normally, because
* running IO is failed due to librbd's immediate blacklisting
* during lock acquisition on a higher priority path.
*/
return tcmu_set_sense_data(cmd->sense_buf, NOT_READY,
ASC_STATE_TRANSITION, NULL);
}
/*
* TODO: Check timers.
* The rados osd op timeout must be longer than the timeouts to detect
* unreachable OSDs (osd heartbeat grace + osd heartbeat interval) or
* we will end up failing the transport connection when we just needed
* to try a different OSD.
*/
static int tcmu_rbd_handle_timedout_cmd(struct tcmu_device *dev,
struct tcmulib_cmd *cmd)
{
tcmu_dev_err(dev, "Timing out cmd.\n");
tcmu_notify_conn_lost(dev);
/*
* TODO: For AA, we will want to kill the ceph tcp connections
* with LINGER on and set to 0, so there are no TCP retries,
* and we need something on the OSD side to drop requests
* that end up reaching it after the initiator's failover/recovery
* timeout. For implicit and explicit FO, we will just disable
* the iscsi port, and let the initiator switch paths which will
* result in us getting blacklisted, so fail with a retryable
* error.
*/
return SAM_STAT_BUSY;
}
#ifdef RBD_IOVEC_SUPPORT
static rbd_image_t tcmu_dev_to_image(struct tcmu_device *dev)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
return state->image;
}
#define tcmu_rbd_aio_read(dev, aio_cb, completion, iov, iov_cnt, length, offset) \
rbd_aio_readv(tcmu_dev_to_image(dev), iov, iov_cnt, offset, completion);
#define tcmu_rbd_aio_write(dev, aio_cb, completion, iov, iov_cnt, length, offset) \
rbd_aio_writev(tcmu_dev_to_image(dev), iov, iov_cnt, offset, completion);
#else
static int tcmu_rbd_aio_read(struct tcmu_device *dev, struct rbd_aio_cb *aio_cb,
rbd_completion_t completion, struct iovec *iov,
size_t iov_cnt, size_t length, off_t offset)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
int ret;
aio_cb->bounce_buffer = malloc(length);
if (!aio_cb->bounce_buffer) {
tcmu_dev_err(dev, "Could not allocate bounce buffer.\n");
return -ENOMEM;
}
ret = rbd_aio_read(state->image, offset, length, aio_cb->bounce_buffer,
completion);
if (ret < 0)
free(aio_cb->bounce_buffer);
return ret;
}
static int tcmu_rbd_aio_write(struct tcmu_device *dev, struct rbd_aio_cb *aio_cb,
rbd_completion_t completion, struct iovec *iov,
size_t iov_cnt, size_t length, off_t offset)
{
struct tcmu_rbd_state *state = tcmu_get_dev_private(dev);
int ret;
aio_cb->bounce_buffer = malloc(length);
if (!aio_cb->bounce_buffer) {
tcmu_dev_err(dev, "Failed to allocate bounce buffer.\n");
return -ENOMEM;;
}
tcmu_memcpy_from_iovec(aio_cb->bounce_buffer, length, iov, iov_cnt);
ret = rbd_aio_write(state->image, offset, length, aio_cb->bounce_buffer,
completion);
if (ret < 0)
free(aio_cb->bounce_buffer);
return ret;
}
#endif
/*
* NOTE: RBD async APIs almost always return 0 (success), except
* when allocation (via new) fails - which is not caught. So,
* the only errno we've to bother about as of now are memory
* allocation errors.
*/
static void rbd_finish_aio_generic(rbd_completion_t completion,
struct rbd_aio_cb *aio_cb)
{
struct tcmu_device *dev = aio_cb->dev;
struct tcmulib_cmd *tcmulib_cmd = aio_cb->tcmulib_cmd;
struct iovec *iov = aio_cb->iov;
size_t iov_cnt = aio_cb->iov_cnt;
uint32_t cmp_offset;
uint16_t asc_ascq;
int64_t ret;
int tcmu_r;
ret = rbd_aio_get_return_value(completion);
rbd_aio_release(completion);
if (ret == -ETIMEDOUT) {
tcmu_r = tcmu_rbd_handle_timedout_cmd(dev, tcmulib_cmd);
} else if (ret == -ESHUTDOWN) {
tcmu_r = tcmu_rbd_handle_blacklisted_cmd(dev, tcmulib_cmd);
} else if (ret == -EILSEQ && aio_cb->type == RBD_AIO_TYPE_CAW) {
cmp_offset = aio_cb->caw.miscompare_offset - aio_cb->caw.offset;
tcmu_dev_dbg(dev, "CAW miscompare at offset %u.\n", cmp_offset);
tcmu_r = tcmu_set_sense_data(tcmulib_cmd->sense_buf,
MISCOMPARE,
ASC_MISCOMPARE_DURING_VERIFY_OPERATION,
&cmp_offset);
} else if (ret == -EINVAL) {
tcmu_dev_err(dev, "Invalid IO request.\n");
tcmu_r = tcmu_set_sense_data(tcmulib_cmd->sense_buf,
ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
} else if (ret < 0) {
tcmu_dev_err(dev, "Got fatal IO error %d.\n", ret);
if (aio_cb->type == RBD_AIO_TYPE_READ)
asc_ascq = ASC_READ_ERROR;
else
asc_ascq = ASC_WRITE_ERROR;
tcmu_r = tcmu_set_sense_data(tcmulib_cmd->sense_buf,
MEDIUM_ERROR, asc_ascq, NULL);
} else {
tcmu_r = SAM_STAT_GOOD;
if (aio_cb->type == RBD_AIO_TYPE_READ &&
aio_cb->bounce_buffer) {
tcmu_memcpy_into_iovec(iov, iov_cnt,
aio_cb->bounce_buffer,
aio_cb->read.length);
}
}
tcmulib_cmd->done(dev, tcmulib_cmd, tcmu_r);
if (aio_cb->bounce_buffer)
free(aio_cb->bounce_buffer);
free(aio_cb);
}