forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibtcmu.c
1023 lines (840 loc) · 22.7 KB
/
libtcmu.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
/*
* 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
#define _BITS_UIO_H
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <errno.h>
#include <dirent.h>
#include <scsi/scsi.h>
#include <linux/target_core_user.h>
#include <libnl3/netlink/genl/genl.h>
#include <libnl3/netlink/genl/mngt.h>
#include <libnl3/netlink/genl/ctrl.h>
#include "libtcmu.h"
#include "libtcmu_log.h"
#include "libtcmu_priv.h"
#include "tcmur_aio.h"
#include "tcmur_cmd_handler.h"
#include "tcmu-runner.h"
#define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
[TCMU_ATTR_DEVICE] = { .type = NLA_STRING },
[TCMU_ATTR_MINOR] = { .type = NLA_U32 },
};
static darray(struct tcmu_thread) g_threads = darray_new();
static int add_device(struct tcmulib_context *ctx, char *dev_name, char *cfgstring);
static void remove_device(struct tcmulib_context *ctx, char *dev_name, char *cfgstring);
static int handle_netlink(struct nl_cache_ops *unused, struct genl_cmd *cmd,
struct genl_info *info, void *arg)
{
struct tcmulib_context *ctx = arg;
char buf[32];
if (!info->attrs[TCMU_ATTR_MINOR] || !info->attrs[TCMU_ATTR_DEVICE]) {
tcmu_err("TCMU_ATTR_MINOR or TCMU_ATTR_DEVICE not set, doing nothing\n");
return 0;
}
snprintf(buf, sizeof(buf), "uio%d", nla_get_u32(info->attrs[TCMU_ATTR_MINOR]));
switch (cmd->c_id) {
case TCMU_CMD_ADDED_DEVICE:
add_device(ctx, buf, nla_get_string(info->attrs[TCMU_ATTR_DEVICE]));
break;
case TCMU_CMD_REMOVED_DEVICE:
remove_device(ctx, buf, nla_get_string(info->attrs[TCMU_ATTR_DEVICE]));
break;
default:
tcmu_err("Unknown notification %d\n", cmd->c_id);
}
return 0;
}
static struct genl_cmd tcmu_cmds[] = {
{
.c_id = TCMU_CMD_ADDED_DEVICE,
.c_name = "ADDED DEVICE",
.c_msg_parser = handle_netlink,
.c_maxattr = TCMU_ATTR_MAX,
.c_attr_policy = tcmu_attr_policy,
},
{
.c_id = TCMU_CMD_REMOVED_DEVICE,
.c_name = "REMOVED DEVICE",
.c_msg_parser = handle_netlink,
.c_maxattr = TCMU_ATTR_MAX,
.c_attr_policy = tcmu_attr_policy,
},
};
static struct genl_ops tcmu_ops = {
.o_name = "TCM-USER",
.o_cmds = tcmu_cmds,
.o_ncmds = ARRAY_SIZE(tcmu_cmds),
};
static struct nl_sock *setup_netlink(struct tcmulib_context *ctx)
{
struct nl_sock *sock;
int ret;
sock = nl_socket_alloc();
if (!sock) {
tcmu_err("couldn't alloc socket\n");
return NULL;
}
nl_socket_disable_seq_check(sock);
nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, genl_handle_msg, ctx);
ret = genl_connect(sock);
if (ret < 0) {
tcmu_err("couldn't connect\n");
goto err_free;
}
ret = genl_register_family(&tcmu_ops);
if (ret < 0) {
tcmu_err("couldn't register family\n");
goto err_close;
}
ret = genl_ops_resolve(sock, &tcmu_ops);
if (ret < 0) {
tcmu_err("couldn't resolve ops, is target_core_user.ko loaded?\n");
goto err_close;
}
ret = genl_ctrl_resolve_grp(sock, "TCM-USER", "config");
ret = nl_socket_add_membership(sock, ret);
if (ret < 0) {
tcmu_err("couldn't add membership\n");
goto err_close;
}
return sock;
err_close:
nl_close(sock);
err_free:
nl_socket_free(sock);
return NULL;
}
static void teardown_netlink(struct nl_sock *sock)
{
nl_close(sock);
nl_socket_free(sock);
}
static struct tcmulib_handler *find_handler(struct tcmulib_context *ctx,
char *cfgstring)
{
struct tcmulib_handler *handler;
size_t len;
char *found_at;
found_at = strchrnul(cfgstring, '/');
len = found_at - cfgstring;
darray_foreach(handler, ctx->handlers) {
if (!strncmp(cfgstring, handler->subtype, len))
return handler;
}
return NULL;
}
static void cmdproc_thread_cleanup(void *arg)
{
struct tcmu_device *dev = arg;
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
rhandler->close(dev);
}
static int generic_cmd(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
uint8_t *cdb = cmd->cdb;
struct iovec *iovec = cmd->iovec;
size_t iov_cnt = cmd->iov_cnt;
uint8_t *sense = cmd->sense_buf;
uint32_t block_size = tcmu_get_dev_block_size(dev);
uint64_t num_lbas = tcmu_get_dev_num_lbas(dev);
switch (cdb[0]) {
case INQUIRY:
return tcmu_emulate_inquiry(dev, cdb, iovec, iov_cnt, sense);
case TEST_UNIT_READY:
return tcmu_emulate_test_unit_ready(cdb, iovec, iov_cnt, sense);
case SERVICE_ACTION_IN_16:
if (cdb[1] == READ_CAPACITY_16)
return tcmu_emulate_read_capacity_16(num_lbas,
block_size,
cdb, iovec,
iov_cnt, sense);
else
return TCMU_NOT_HANDLED;
case READ_CAPACITY:
if ((cdb[1] & 0x01) || (cdb[8] & 0x01))
/* Reserved bits for MM logical units */
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB,
NULL);
else
return tcmu_emulate_read_capacity_10(num_lbas,
block_size,
cdb, iovec,
iov_cnt, sense);
case MODE_SENSE:
case MODE_SENSE_10:
return tcmu_emulate_mode_sense(cdb, iovec, iov_cnt, sense);
case START_STOP:
return tcmu_emulate_start_stop(dev, cdb, sense);
case MODE_SELECT:
case MODE_SELECT_10:
return tcmu_emulate_mode_select(cdb, iovec, iov_cnt, sense);
default:
tcmu_err("unknown command %x\n", cdb[0]);
return TCMU_NOT_HANDLED;
}
}
#define CDB_TO_BUF_SIZE(bytes) ((bytes) * 3 + 1)
#define CDB_FIX_BYTES 64 /* 64 bytes for default */
#define CDB_FIX_SIZE CDB_TO_BUF_SIZE(CDB_FIX_BYTES)
static void tcmu_cdb_debug_info(const struct tcmulib_cmd *cmd)
{
int i, n, bytes;
char fix[CDB_FIX_SIZE], *buf;
buf = fix;
bytes = tcmu_get_cdb_length(cmd->cdb);
if (bytes > CDB_FIX_SIZE) {
buf = malloc(CDB_TO_BUF_SIZE(bytes));
if (!buf) {
tcmu_err("out of memory\n");
return;
}
}
for (i = 0, n = 0; i < bytes; i++) {
n += sprintf(buf + n, "%x ", cmd->cdb[i]);
}
sprintf(buf + n, "\n");
tcmu_dbg_scsi_cmd(buf);
if (bytes > CDB_FIX_SIZE)
free(buf);
}
static bool command_is_generic(struct tcmulib_cmd *tcmulib_cmd)
{
uint8_t *cdb = tcmulib_cmd->cdb;
switch(cdb[0]) {
case INQUIRY:
case TEST_UNIT_READY:
case MODE_SENSE:
case MODE_SENSE_10:
case START_STOP:
case MODE_SELECT:
case MODE_SELECT_10:
case READ_CAPACITY:
return true;
case SERVICE_ACTION_IN_16:
if (cdb[1] == READ_CAPACITY_16)
return true;
/* fall through */
default:
return false;
}
}
static int generic_handle_cmd(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
if (command_is_generic(cmd))
return generic_cmd(dev, cmd);
else
return tcmur_cmd_handler(dev, cmd);
}
static void *tcmu_cmdproc_thread(void *arg)
{
int ret;
struct tcmu_device *dev = arg;
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct pollfd pfd;
pthread_cleanup_push(cmdproc_thread_cleanup, dev);
while (1) {
int completed = 0;
struct tcmulib_cmd *cmd;
tcmulib_processing_start(dev);
while ((cmd = tcmulib_get_next_command(dev)) != NULL) {
if (tcmu_get_log_level() == TCMU_LOG_DEBUG_SCSI_CMD)
tcmu_cdb_debug_info(cmd);
if (tcmur_handler_is_passthrough_only(rhandler))
ret = tcmur_cmd_passthrough_handler(dev, cmd);
else
ret = generic_handle_cmd(dev, cmd);
/*
* command (processing) completion is called in the following
* scenarios:
* - handle_cmd: synchronous handlers
* - generic_handle_cmd: non tcmur handler calls (see generic_cmd())
* and on errors when calling tcmur handler.
*/
if (ret != TCMU_ASYNC_HANDLED) {
completed = 1;
tcmulib_command_complete(dev, cmd, ret);
}
}
if (completed)
tcmulib_processing_complete(dev);
pfd.fd = tcmu_get_dev_fd(dev);
pfd.events = POLLIN;
pfd.revents = 0;
poll(&pfd, 1, -1);
if (pfd.revents != POLLIN) {
tcmu_err("poll received unexpected revent: 0x%x\n", pfd.revents);
break;
}
}
tcmu_err("thread terminating, should never happen\n");
pthread_cleanup_pop(1);
return NULL;
}
static int add_device(struct tcmulib_context *ctx,
char *dev_name, char *cfgstring)
{
struct tcmu_device *dev;
struct tcmu_mailbox *mb;
char str_buf[256];
int fd;
int ret;
char *ptr, *oldptr;
char *reason = NULL;
int len;
dev = calloc(1, sizeof(*dev));
if (!dev) {
tcmu_err("calloc failed in add_device\n");
return -ENOMEM;
}
snprintf(dev->dev_name, sizeof(dev->dev_name), "%s", dev_name);
oldptr = cfgstring;
ptr = strchr(oldptr, '/');
if (!ptr) {
tcmu_err("invalid cfgstring\n");
goto err_free;
}
if (strncmp(cfgstring, "tcm-user", ptr-oldptr)) {
tcmu_err("invalid cfgstring\n");
goto err_free;
}
/* Get HBA name */
oldptr = ptr+1;
ptr = strchr(oldptr, '/');
if (!ptr) {
tcmu_err("invalid cfgstring\n");
goto err_free;
}
len = ptr-oldptr;
snprintf(dev->tcm_hba_name, sizeof(dev->tcm_hba_name), "user_%.*s", len, oldptr);
/* Get device name */
oldptr = ptr+1;
ptr = strchr(oldptr, '/');
if (!ptr) {
tcmu_err("invalid cfgstring\n");
goto err_free;
}
len = ptr-oldptr;
snprintf(dev->tcm_dev_name, sizeof(dev->tcm_dev_name), "%.*s", len, oldptr);
/* The rest is the handler-specific cfgstring */
oldptr = ptr+1;
ptr = strchr(oldptr, '/');
snprintf(dev->cfgstring, sizeof(dev->cfgstring), "%s", oldptr);
dev->handler = find_handler(ctx, dev->cfgstring);
if (!dev->handler) {
tcmu_err("could not find handler for %s\n", dev->dev_name);
goto err_free;
}
if (dev->handler->check_config &&
!dev->handler->check_config(dev->cfgstring, &reason)) {
/* It may be handled by other handlers */
tcmu_err("check_config failed for %s because of %s\n", dev->dev_name, reason);
free(reason);
goto err_free;
}
snprintf(str_buf, sizeof(str_buf), "/dev/%s", dev_name);
dev->fd = open(str_buf, O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (dev->fd == -1) {
tcmu_err("could not open %s\n", str_buf);
goto err_free;
}
snprintf(str_buf, sizeof(str_buf), "/sys/class/uio/%s/maps/map0/size", dev->dev_name);
fd = open(str_buf, O_RDONLY);
if (fd == -1) {
tcmu_err("could not open %s\n", str_buf);
goto err_fd_close;
}
ret = read(fd, str_buf, sizeof(str_buf));
close(fd);
if (ret <= 0) {
tcmu_err("could not read size of map0\n");
goto err_fd_close;
}
str_buf[ret-1] = '\0'; /* null-terminate and chop off the \n */
dev->map_len = strtoull(str_buf, NULL, 0);
if (dev->map_len == ULLONG_MAX) {
tcmu_err("could not get map length\n");
goto err_fd_close;
}
dev->map = mmap(NULL, dev->map_len, PROT_READ|PROT_WRITE, MAP_SHARED, dev->fd, 0);
if (dev->map == MAP_FAILED) {
tcmu_err("could not mmap: %m\n");
goto err_fd_close;
}
mb = dev->map;
if (mb->version != KERN_IFACE_VER) {
tcmu_err("Kernel interface version mismatch: wanted %d got %d\n",
KERN_IFACE_VER, mb->version);
goto err_munmap;
}
ret = pthread_spin_init(&dev->lock, 0);
if (ret < 0) {
tcmu_err("failed to initialize mailbox lock\n");
goto err_munmap;
}
ret = pthread_mutex_init(&dev->caw_lock, NULL);
if (ret < 0) {
tcmu_err("failed to initialize caw lock\n");
goto cleanup_lock;
}
ret = setup_io_work_queue(dev);
if (ret < 0) {
goto cleanup_caw_lock;
}
ret = setup_aio_tracking(dev);
if (ret < 0) {
goto cleanup_io_work_queue;
}
dev->cmd_tail = mb->cmd_tail;
dev->ctx = ctx;
darray_append(ctx->devices, dev);
ret = dev->handler->added(dev);
if (ret < 0) {
tcmu_err("handler open failed for %s\n", dev->dev_name);
goto cleanup_aio_tracking;
}
return 0;
cleanup_aio_tracking:
cleanup_aio_tracking(dev);
cleanup_io_work_queue:
cleanup_io_work_queue(dev, true);
cleanup_caw_lock:
pthread_mutex_destroy(&dev->caw_lock);
cleanup_lock:
pthread_spin_destroy(&dev->lock);
err_munmap:
munmap(dev->map, dev->map_len);
err_fd_close:
close(dev->fd);
err_free:
free(dev);
return -ENOENT;
}
static void close_devices(struct tcmulib_context *ctx)
{
struct tcmu_device **dev_ptr;
struct tcmu_device *dev;
char *cfgstring = "";
darray_foreach(dev_ptr, ctx->devices) {
dev = *dev_ptr;
remove_device(ctx, dev->dev_name, cfgstring);
}
}
static void remove_device(struct tcmulib_context *ctx,
char *dev_name, char *cfgstring)
{
struct tcmu_device **dev_ptr;
struct tcmu_device *dev;
int i = 0, ret;
bool found = false;
darray_foreach(dev_ptr, ctx->devices) {
dev = *dev_ptr;
size_t len = strnlen(dev->dev_name, sizeof(dev->dev_name));
if (strncmp(dev->dev_name, dev_name, len)) {
i++;
} else {
found = true;
break;
}
}
if (!found) {
tcmu_err("could not remove device %s: not found\n", dev_name);
return;
}
darray_remove(ctx->devices, i);
/*
* The order of cleaning up worker threads and calling ->removed()
* is important: for sync handlers, the worker thread needs to be
* terminated before removing the handler (i.e., calling handlers
* ->close() callout) in order to ensure that no handler callouts
* are getting invoked when shutting down the handler.
*/
cleanup_io_work_queue_threads(dev);
dev->handler->removed(dev);
cleanup_io_work_queue(dev, false);
cleanup_aio_tracking(dev);
ret = close(dev->fd);
if (ret != 0) {
tcmu_err("could not close device fd %s: %d\n", dev_name, errno);
}
ret = munmap(dev->map, dev->map_len);
if (ret != 0) {
tcmu_err("could not unmap device %s: %d\n", dev_name, errno);
}
ret = pthread_spin_destroy(&dev->lock);
if (ret < 0) {
tcmu_err("could not cleanup mailbox lock %s: %d\n", dev_name, errno);
}
ret = pthread_mutex_destroy(&dev->caw_lock);
if (ret < 0)
tcmu_err("could not cleanup caw lock %s: %d\n", dev_name, errno);
free(dev);
}
static int is_uio(const struct dirent *dirent)
{
int fd;
char tmp_path[64];
char buf[256];
ssize_t ret;
if (strncmp(dirent->d_name, "uio", 3))
return 0;
snprintf(tmp_path, sizeof(tmp_path), "/sys/class/uio/%s/name", dirent->d_name);
fd = open(tmp_path, O_RDONLY);
if (fd == -1)
return 0;
ret = read(fd, buf, sizeof(buf));
if (ret <= 0 || ret >= sizeof(buf))
goto not_uio;
buf[ret-1] = '\0'; /* null-terminate and chop off the \n */
/* we only want uio devices whose name is a format we expect */
if (strncmp(buf, "tcm-user", 8))
goto not_uio;
close(fd);
return 1;
not_uio:
close(fd);
return 0;
}
static int open_devices(struct tcmulib_context *ctx)
{
struct dirent **dirent_list;
int num_devs;
int num_good_devs = 0;
int i;
num_devs = scandir("/dev", &dirent_list, is_uio, alphasort);
if (num_devs == -1)
return -1;
for (i = 0; i < num_devs; i++) {
char tmp_path[64];
char buf[256];
int fd;
int ret;
snprintf(tmp_path, sizeof(tmp_path), "/sys/class/uio/%s/name",
dirent_list[i]->d_name);
fd = open(tmp_path, O_RDONLY);
if (fd == -1) {
tcmu_err("could not open %s!\n", tmp_path);
continue;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret <= 0 || ret >= sizeof(buf)) {
tcmu_err("read of %s had issues\n", tmp_path);
continue;
}
buf[ret-1] = '\0'; /* null-terminate and chop off the \n */
ret = add_device(ctx, dirent_list[i]->d_name, buf);
if (ret < 0)
continue;
num_good_devs++;
}
for (i = 0; i < num_devs; i++)
free(dirent_list[i]);
free(dirent_list);
return num_good_devs;
}
struct tcmulib_context *tcmulib_initialize(
struct tcmulib_handler *handlers,
size_t handler_count)
{
struct tcmulib_context *ctx;
int ret;
int i;
ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return NULL;
ctx->nl_sock = setup_netlink(ctx);
if (!ctx->nl_sock) {
free(ctx);
return NULL;
}
darray_init(ctx->handlers);
darray_init(ctx->devices);
for (i = 0; i < handler_count; i++) {
struct tcmulib_handler handler = handlers[i];
handler.ctx = ctx;
darray_append(ctx->handlers, handler);
}
ret = open_devices(ctx);
if (ret < 0) {
teardown_netlink(ctx->nl_sock);
darray_free(ctx->handlers);
darray_free(ctx->devices);
return NULL;
}
return ctx;
}
void tcmulib_close(struct tcmulib_context *ctx)
{
int ret;
close_devices(ctx);
teardown_netlink(ctx->nl_sock);
darray_free(ctx->handlers);
darray_free(ctx->devices);
ret = genl_unregister_family(&tcmu_ops);
if (ret != 0) {
tcmu_err("genl_unregister_family failed, %d\n", ret);
}
free(ctx);
}
int tcmulib_get_master_fd(struct tcmulib_context *ctx)
{
return nl_socket_get_fd(ctx->nl_sock);
}
int tcmulib_master_fd_ready(struct tcmulib_context *ctx)
{
return nl_recvmsgs_default(ctx->nl_sock);
}
void *tcmu_get_dev_private(struct tcmu_device *dev)
{
return dev->hm_private;
}
void tcmu_set_dev_private(struct tcmu_device *dev, void *private)
{
dev->hm_private = private;
}
void tcmu_set_dev_num_lbas(struct tcmu_device *dev, uint64_t num_lbas)
{
dev->num_lbas = num_lbas;
}
uint64_t tcmu_get_dev_num_lbas(struct tcmu_device *dev)
{
return dev->num_lbas;
}
void tcmu_set_dev_block_size(struct tcmu_device *dev, uint32_t block_size)
{
dev->block_size = block_size;
}
uint32_t tcmu_get_dev_block_size(struct tcmu_device *dev)
{
return dev->block_size;
}
int tcmu_get_dev_fd(struct tcmu_device *dev)
{
return dev->fd;
}
char *tcmu_get_dev_cfgstring(struct tcmu_device *dev)
{
return dev->cfgstring;
}
struct tcmulib_handler *tcmu_get_dev_handler(struct tcmu_device *dev)
{
return dev->handler;
}
struct tcmur_handler *tcmu_get_runner_handler(struct tcmu_device *dev)
{
struct tcmulib_handler *handler = tcmu_get_dev_handler(dev);
return handler->hm_private;
}
static inline struct tcmu_cmd_entry *
device_cmd_head(struct tcmu_device *dev)
{
struct tcmu_mailbox *mb = dev->map;
return (struct tcmu_cmd_entry *) ((char *) mb + mb->cmdr_off + mb->cmd_head);
}
static inline struct tcmu_cmd_entry *
device_cmd_tail(struct tcmu_device *dev)
{
struct tcmu_mailbox *mb = dev->map;
return (struct tcmu_cmd_entry *) ((char *) mb + mb->cmdr_off + dev->cmd_tail);
}
/* update the tcmu_device's tail */
#define TCMU_UPDATE_DEV_TAIL(dev, mb, ent) \
do { \
dev->cmd_tail = (dev->cmd_tail + tcmu_hdr_get_len((ent)->hdr.len_op)) % mb->cmdr_size; \
} while (0);
struct tcmulib_cmd *tcmulib_get_next_command(struct tcmu_device *dev)
{
struct tcmu_mailbox *mb = dev->map;
struct tcmu_cmd_entry *ent;
while ((ent = device_cmd_tail(dev)) != device_cmd_head(dev)) {
switch (tcmu_hdr_get_op(ent->hdr.len_op)) {
case TCMU_OP_PAD:
/* do nothing */
break;
case TCMU_OP_CMD: {
int i;
struct tcmulib_cmd *cmd;
uint8_t *cdb = (uint8_t *) mb + ent->req.cdb_off;
unsigned cdb_len = tcmu_get_cdb_length(cdb);
/* Alloc memory for cmd itself, iovec and cdb */
cmd = malloc(sizeof(*cmd) + sizeof(*cmd->iovec) * ent->req.iov_cnt + cdb_len);
if (!cmd)
return NULL;
cmd->cmd_id = ent->hdr.cmd_id;
/* Convert iovec addrs in-place to not be offsets */
cmd->iov_cnt = ent->req.iov_cnt;
cmd->iovec = (struct iovec *) (cmd + 1);
for (i = 0; i < ent->req.iov_cnt; i++) {
cmd->iovec[i].iov_base = (void *) mb +
(size_t) ent->req.iov[i].iov_base;
cmd->iovec[i].iov_len = ent->req.iov[i].iov_len;
}
/* Copy cdb that currently points to the command ring */
cmd->cdb = (uint8_t *) (cmd->iovec + cmd->iov_cnt);
memcpy(cmd->cdb, (void *) mb + ent->req.cdb_off, cdb_len);
TCMU_UPDATE_DEV_TAIL(dev, mb, ent);
return cmd;
}
default:
/* We don't even know how to handle this TCMU opcode. */
ent->hdr.uflags |= TCMU_UFLAG_UNKNOWN_OP;
}
TCMU_UPDATE_DEV_TAIL(dev, mb, ent);
}
return NULL;
}
/* update the ring buffer's tail */
#define TCMU_UPDATE_RB_TAIL(mb, ent) \
do { \
mb->cmd_tail = (mb->cmd_tail + tcmu_hdr_get_len((ent)->hdr.len_op)) % mb->cmdr_size; \
} while (0);
void tcmulib_command_complete(
struct tcmu_device *dev,
struct tcmulib_cmd *cmd,
int result)
{
struct tcmu_mailbox *mb = dev->map;
pthread_cleanup_push(_cleanup_spin_lock, (void *)&dev->lock);
pthread_spin_lock(&dev->lock);
struct tcmu_cmd_entry *ent = (void *) mb + mb->cmdr_off + mb->cmd_tail;
/* current command could be PAD in async case */
while (ent != (void *) mb + mb->cmdr_off + mb->cmd_head) {
if (tcmu_hdr_get_op(ent->hdr.len_op) == TCMU_OP_CMD)
break;
TCMU_UPDATE_RB_TAIL(mb, ent);
ent = (void *) mb + mb->cmdr_off + mb->cmd_tail;
}
/* cmd_id could be different in async case */
if (cmd->cmd_id != ent->hdr.cmd_id) {
ent->hdr.cmd_id = cmd->cmd_id;
}
if (result == TCMU_NOT_HANDLED) {
/* Tell the kernel we didn't handle it */
char *buf = ent->rsp.sense_buffer;
ent->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
buf[0] = 0x70; /* fixed, current */
buf[2] = 0x5; /* illegal request */
buf[7] = 0xa;
buf[12] = 0x20; /* ASC: invalid command operation code */
buf[13] = 0x0; /* ASCQ: (none) */
} else {
if (result != SAM_STAT_GOOD) {
memcpy(ent->rsp.sense_buffer, cmd->sense_buf,
TCMU_SENSE_BUFFERSIZE);
}
ent->rsp.scsi_status = result;
}
TCMU_UPDATE_RB_TAIL(mb, ent);
pthread_spin_unlock(&dev->lock);
pthread_cleanup_pop(0);
free(cmd);
}
void tcmulib_processing_start(struct tcmu_device *dev)
{
int r;
uint32_t buf;
/* Clear the event on the fd */
do {
r = read(dev->fd, &buf, 4);
} while (r == -1 && errno == EINTR);
if (r == -1 && errno != EAGAIN)
perror("read");
}
void tcmulib_processing_complete(struct tcmu_device *dev)
{
int r;
uint32_t buf = 0;
/* Tell the kernel there are completed commands */
do {
r = write(dev->fd, &buf, 4);
} while (r == -1 && errno == EINTR);
if (r == -1 && errno != EAGAIN)
perror("write");
}
int tcmulib_start_cmdproc_thread(struct tcmu_device *dev)
{
int ret;
struct tcmu_thread thread;
thread.dev = dev;
ret = pthread_create(&thread.thread_id, NULL, tcmu_cmdproc_thread, dev);
if (ret) {
return -1;
}
darray_append(g_threads, thread);
return 0;
}
void _cleanup_mutex_lock(void *arg)
{
pthread_mutex_unlock(arg);
}
void _cleanup_spin_lock(void *arg)
{
pthread_spin_unlock(arg);
}
void cancel_thread(pthread_t thread)
{
void *join_retval;
int ret;
ret = pthread_cancel(thread);
if (ret) {
tcmu_err("pthread_cancel failed with value %d\n", ret);
return;
}
ret = pthread_join(thread, &join_retval);
if (ret) {
tcmu_err("pthread_join failed with value %d\n", ret);
return;
}
if (join_retval != PTHREAD_CANCELED)
tcmu_err("unexpected join retval: %p\n", join_retval);
}
void tcmulib_cleanup_cmdproc_thread(struct tcmu_device *dev)
{
struct tcmu_thread *thread;
int i = 0;
bool found = false;
darray_foreach(thread, g_threads) {
if (thread->dev == dev) {