-
Notifications
You must be signed in to change notification settings - Fork 148
/
main.c
1470 lines (1238 loc) · 36.4 KB
/
main.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) 2014 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.
*/
/*
* A daemon that supports a simplified interface for writing TCMU
* handlers.
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <assert.h>
#include <dlfcn.h>
#include <pthread.h>
#include <signal.h>
#include <glib.h>
#include <glib-unix.h>
#include <gio/gio.h>
#include <getopt.h>
#include <poll.h>
#include <time.h>
#include <scsi/scsi.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <libkmod.h>
#include <sys/utsname.h>
#include "target_core_user_local.h"
#include "darray.h"
#include "tcmu-runner.h"
#include "tcmur_aio.h"
#include "tcmur_device.h"
#include "tcmur_cmd_handler.h"
#include "libtcmu.h"
#include "tcmuhandler-generated.h"
#include "version.h"
#include "libtcmu_config.h"
#include "libtcmu_log.h"
#include "tcmur_work.h"
#define TCMU_LOCK_FILE "/run/tcmu.lock"
static char *handler_path = DEFAULT_HANDLER_PATH;
static struct tcmu_config *tcmu_cfg;
darray(struct tcmur_handler *) g_runner_handlers = darray_new();
struct tcmur_handler *tcmu_get_runner_handler(struct tcmu_device *dev)
{
struct tcmulib_handler *handler = tcmu_dev_get_handler(dev);
return handler->hm_private;
}
int tcmur_register_handler(struct tcmur_handler *handler)
{
struct tcmur_handler *h;
int ret;
int i;
for (i = 0; i < darray_size(g_runner_handlers); i++) {
h = darray_item(g_runner_handlers, i);
if (!strcmp(h->subtype, handler->subtype)) {
tcmu_err("Handler %s has already been registered\n",
handler->subtype);
return -1;
}
}
if (handler->init) {
ret = handler->init();
if (ret) {
tcmu_err("Failed to init handler %s, ret = %d\n",
handler->subtype, ret);
return ret;
}
}
tcmu_info("Handler %s is registered\n", handler->subtype);
darray_append(g_runner_handlers, handler);
return 0;
}
static int tcmur_register_dbus_handler(struct tcmur_handler *handler)
{
assert(handler->_is_dbus_handler == true);
return tcmur_register_handler(handler);
}
bool tcmur_unregister_handler(struct tcmur_handler *handler)
{
int i;
for (i = 0; i < darray_size(g_runner_handlers); i++) {
if (darray_item(g_runner_handlers, i) == handler) {
darray_remove(g_runner_handlers, i);
return true;
}
}
return false;
}
static void free_dbus_handler(struct tcmur_handler *handler)
{
g_free((char*)handler->opaque);
g_free((char*)handler->subtype);
g_free((char*)handler->cfg_desc);
if (handler->destroy)
handler->destroy();
g_free(handler);
}
static bool tcmur_unregister_dbus_handler(struct tcmur_handler *handler)
{
bool ret = false;
assert(handler->_is_dbus_handler == true);
ret = tcmur_unregister_handler(handler);
if (ret == true) {
free_dbus_handler(handler);
}
return ret;
}
static void tcmur_unregister_all_dbus_handlers(void)
{
struct tcmur_handler *handler;
int i;
for (i = 0; i < darray_size(g_runner_handlers); i++) {
handler = darray_item(g_runner_handlers, i);
if (handler->_is_dbus_handler == true) {
if (tcmur_unregister_handler(handler))
free_dbus_handler(handler);
}
}
darray_free(g_runner_handlers);
}
static int is_handler(const struct dirent *dirent)
{
if (strncmp(dirent->d_name, "handler_", 8))
return 0;
return 1;
}
static int open_handlers(void)
{
struct dirent **dirent_list;
int num_handlers;
int num_good = 0;
char *error;
int i;
num_handlers = scandir(handler_path, &dirent_list, is_handler, alphasort);
if (num_handlers == -1)
return -1;
for (i = 0; i < num_handlers; i++) {
char *path;
void *handle;
int (*handler_init)(void);
int ret;
ret = asprintf(&path, "%s/%s", handler_path, dirent_list[i]->d_name);
if (ret == -1) {
tcmu_err("ENOMEM\n");
continue;
}
handle = dlopen(path, RTLD_NOW|RTLD_LOCAL);
if (!handle) {
tcmu_err("Could not open handler at %s: %s\n", path, dlerror());
free(path);
continue;
}
dlerror();
handler_init = dlsym(handle, "handler_init");
if ((error = dlerror())) {
tcmu_err("dlsym failure on %s: (%s)\n", path, error);
free(path);
continue;
}
ret = handler_init();
if (ret) {
tcmu_err("handler init failed on path %s\n", path);
free(path);
continue;
}
free(path);
if (ret == 0)
num_good++;
}
for (i = 0; i < num_handlers; i++)
free(dirent_list[i]);
free(dirent_list);
return num_good;
}
static gboolean handle_sig(gpointer user_data)
{
tcmu_dbg("Have received signal!\n");
g_main_loop_quit((GMainLoop*)user_data);
return G_SOURCE_CONTINUE;
}
static gboolean handle_sighup(gpointer user_data)
{
tcmu_resetup_log_file(NULL, NULL);
return G_SOURCE_CONTINUE;
}
gboolean tcmulib_callback(GIOChannel *source,
GIOCondition condition,
gpointer data)
{
struct tcmulib_context *ctx = data;
tcmulib_master_fd_ready(ctx);
return TRUE;
}
static GDBusObjectManagerServer *manager = NULL;
static gboolean
on_check_config(TCMUService1 *interface,
GDBusMethodInvocation *invocation,
gchar *cfgstring,
gpointer user_data)
{
struct tcmur_handler *handler = user_data;
char *reason = NULL;
bool str_ok = true;
if (handler->check_config)
str_ok = handler->check_config(cfgstring, &reason);
if (str_ok)
reason = "success";
g_dbus_method_invocation_return_value(invocation,
g_variant_new("(bs)", str_ok, reason ? : "unknown"));
if (!str_ok)
free(reason);
return TRUE;
}
static void
dbus_export_handler(struct tcmur_handler *handler, GCallback check_config)
{
GDBusObjectSkeleton *object;
char obj_name[128];
TCMUService1 *interface;
snprintf(obj_name, sizeof(obj_name), "/org/kernel/TCMUService1/%s",
handler->subtype);
object = g_dbus_object_skeleton_new(obj_name);
interface = tcmuservice1_skeleton_new();
g_dbus_object_skeleton_add_interface(object, G_DBUS_INTERFACE_SKELETON(interface));
g_signal_connect(interface,
"handle-check-config",
check_config,
handler); /* user_data */
tcmuservice1_set_config_desc(interface, handler->cfg_desc);
g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
g_object_unref(object);
}
static bool
dbus_unexport_handler(struct tcmur_handler *handler)
{
char obj_name[128];
snprintf(obj_name, sizeof(obj_name), "/org/kernel/TCMUService1/%s",
handler->subtype);
return g_dbus_object_manager_server_unexport(manager, obj_name) == TRUE;
}
struct dbus_info {
guint watcher_id;
/* The RegisterHandler invocation on
* org.kernel.TCMUService1.HandlerManager1 interface. */
GDBusMethodInvocation *register_invocation;
/* Connection to the handler's bus_name. */
GDBusConnection *connection;
};
static int dbus_handler_open(struct tcmu_device *dev, bool reopen)
{
return -1;
}
static void dbus_handler_close(struct tcmu_device *dev)
{
/* nop */
}
static int dbus_handler_handle_cmd(struct tcmu_device *dev,
struct tcmur_cmd *tcmu_cmd)
{
abort();
}
static gboolean
on_dbus_check_config(TCMUService1 *interface,
GDBusMethodInvocation *invocation,
gchar *cfgstring,
gpointer user_data)
{
char *bus_name, *obj_name;
struct tcmur_handler *handler = user_data;
GDBusConnection *connection;
GError *error = NULL;
GVariant *result;
bus_name = g_strdup_printf("org.kernel.TCMUService1.HandlerManager1.%s",
handler->subtype);
obj_name = g_strdup_printf("/org/kernel/TCMUService1/HandlerManager1/%s",
handler->subtype);
connection = g_dbus_method_invocation_get_connection(invocation);
result = g_dbus_connection_call_sync(connection,
bus_name,
obj_name,
"org.kernel.TCMUService1",
"CheckConfig",
g_variant_new("(s)", cfgstring),
NULL, G_DBUS_CALL_FLAGS_NONE, -1,
NULL, &error);
if (result)
g_dbus_method_invocation_return_value(invocation, result);
else
g_dbus_method_invocation_return_value(invocation,
g_variant_new("(bs)", FALSE, error->message));
g_free(bus_name);
g_free(obj_name);
return TRUE;
}
static void
on_handler_appeared(GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
{
struct tcmur_handler *handler = user_data;
struct dbus_info *info = handler->opaque;
if (info->register_invocation) {
info->connection = connection;
tcmur_register_dbus_handler(handler);
dbus_export_handler(handler, G_CALLBACK(on_dbus_check_config));
g_dbus_method_invocation_return_value(info->register_invocation,
g_variant_new("(bs)", TRUE, "succeeded"));
info->register_invocation = NULL;
}
}
static void
on_handler_vanished(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
struct tcmur_handler *handler = user_data;
struct dbus_info *info = handler->opaque;
if (info->register_invocation) {
char *reason;
reason = g_strdup_printf("Cannot find handler bus name: "
"org.kernel.TCMUService1.HandlerManager1.%s",
handler->subtype);
g_dbus_method_invocation_return_value(info->register_invocation,
g_variant_new("(bs)", FALSE, reason));
g_free(reason);
}
dbus_unexport_handler(handler);
g_bus_unwatch_name(info->watcher_id);
tcmur_unregister_dbus_handler(handler);
}
static gboolean
on_register_handler(TCMUService1HandlerManager1 *interface,
GDBusMethodInvocation *invocation,
gchar *subtype,
gchar *cfg_desc,
gpointer user_data)
{
struct tcmur_handler *handler;
struct dbus_info *info;
char *bus_name;
bus_name = g_strdup_printf("org.kernel.TCMUService1.HandlerManager1.%s",
subtype);
handler = g_new0(struct tcmur_handler, 1);
handler->subtype = g_strdup(subtype);
handler->cfg_desc = g_strdup(cfg_desc);
handler->open = dbus_handler_open;
handler->close = dbus_handler_close;
handler->handle_cmd = dbus_handler_handle_cmd;
info = g_new0(struct dbus_info, 1);
handler->opaque = info;
handler->_is_dbus_handler = 1;
info->register_invocation = invocation;
info->watcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
bus_name,
G_BUS_NAME_WATCHER_FLAGS_NONE,
on_handler_appeared,
on_handler_vanished,
handler,
NULL);
if (info->watcher_id == 0) {
// probably an invalid name, roll back and report an error
free_dbus_handler(handler);
g_dbus_method_invocation_return_value(invocation,
g_variant_new("(bs)", FALSE,
"failed to watch for DBus handler name"));
}
g_free(bus_name);
return TRUE;
}
void dbus_handler_manager1_init(GDBusConnection *connection)
{
GError *error = NULL;
TCMUService1HandlerManager1 *interface;
gboolean ret;
interface = tcmuservice1_handler_manager1_skeleton_new();
ret = g_dbus_interface_skeleton_export(
G_DBUS_INTERFACE_SKELETON(interface),
connection,
"/org/kernel/TCMUService1/HandlerManager1",
&error);
g_signal_connect(interface,
"handle-register-handler",
G_CALLBACK (on_register_handler),
NULL);
if (!ret)
tcmu_err("Handler manager export failed: %s\n",
error ? error->message : "unknown error");
if (error)
g_error_free(error);
}
static void dbus_bus_acquired(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
struct tcmur_handler **handler;
tcmu_dbg("bus %s acquired\n", name);
manager = g_dbus_object_manager_server_new("/org/kernel/TCMUService1");
darray_foreach(handler, g_runner_handlers) {
dbus_export_handler(*handler, G_CALLBACK(on_check_config));
}
dbus_handler_manager1_init(connection);
g_dbus_object_manager_server_set_connection(manager, connection);
}
static void dbus_name_acquired(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
tcmu_dbg("name %s acquired\n", name);
}
static void dbus_name_lost(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
tcmu_dbg("name lost\n");
}
static int load_our_module(void)
{
struct kmod_list *list = NULL, *itr;
struct kmod_ctx *ctx;
struct stat sb;
struct utsname u;
int ret;
ctx = kmod_new(NULL, NULL);
if (!ctx) {
tcmu_err("kmod_new() failed: %m\n");
return -1;
}
ret = kmod_module_new_from_lookup(ctx, "target_core_user", &list);
if (ret < 0) {
/* In some environments like containers, /lib/modules/`uname -r`
* will not exist, in such cases the load module job be taken
* care by admin, either by manual load or makesure it's builtin
*/
if (ENOENT == errno) {
if (uname(&u) < 0) {
tcmu_err("uname() failed: %m\n");
} else {
tcmu_info("no modules directory '/lib/modules/%s', checking module target_core_user entry in '/sys/modules/'\n",
u.release);
ret = stat(CFGFS_TARGET_MOD, &sb);
if (!ret) {
tcmu_dbg("Module target_core_user already loaded\n");
} else {
tcmu_err("stat() on '%s' failed: %m\n",
CFGFS_TARGET_MOD);
}
}
} else {
tcmu_err("kmod_module_new_from_lookup() failed to lookup alias target_core_use %m\n");
}
kmod_unref(ctx);
return ret;
}
if (!list) {
tcmu_err("kmod_module_new_from_lookup() failed to find module target_core_user\n");
kmod_unref(ctx);
return -ENOENT;
}
kmod_list_foreach(itr, list) {
int state, err;
struct kmod_module *mod = kmod_module_get_module(itr);
state = kmod_module_get_initstate(mod);
switch (state) {
case KMOD_MODULE_BUILTIN:
tcmu_info("Module '%s' is builtin\n",
kmod_module_get_name(mod));
break;
case KMOD_MODULE_LIVE:
tcmu_dbg("Module '%s' is already loaded\n",
kmod_module_get_name(mod));
break;
default:
err = kmod_module_probe_insert_module(mod,
KMOD_PROBE_APPLY_BLACKLIST,
NULL, NULL, NULL, NULL);
if (err == 0) {
tcmu_info("Inserted module '%s'\n",
kmod_module_get_name(mod));
} else if (err < 0) {
tcmu_err("Failed to insert '%s': %s\n",
kmod_module_get_name(mod), strerror(-err));
ret = err;
} else {
switch (err) {
case KMOD_PROBE_APPLY_BLACKLIST:
tcmu_err("Module '%s' is blacklisted\n",
kmod_module_get_name(mod));
break;
default:
tcmu_err("Module '%s' is stopped by a reason: 0x%x\n",
kmod_module_get_name(mod), err);
break;
}
ret = -EIO;
}
}
kmod_module_unref(mod);
}
kmod_module_unref_list(list);
kmod_unref(ctx);
return ret;
}
/*
* tcmur_stop_device - stop device for removal
* @arg: tcmu_device to stop
*
* Stop internal tcmur device operations like lock and recovery and close
* the device. Running IO must be stopped before calling this.
*/
static void tcmur_stop_device(void *arg)
{
struct tcmu_device *dev = arg;
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
bool is_open = false;
pthread_mutex_lock(&rdev->rdev_lock);
/* check if this was already called due to thread cancelation */
if (rdev->flags & TCMUR_DEV_FLAG_STOPPED) {
pthread_mutex_unlock(&rdev->rdev_lock);
return;
}
rdev->flags |= TCMUR_DEV_FLAG_STOPPING;
pthread_mutex_unlock(&rdev->rdev_lock);
/*
* The lock thread can fire off the recovery thread, so make sure
* it is done first.
*/
tcmur_flush_work(rdev->event_work);
tcmu_cancel_recovery(dev);
tcmu_release_dev_lock(dev);
pthread_mutex_lock(&rdev->rdev_lock);
if (rdev->flags & TCMUR_DEV_FLAG_IS_OPEN) {
rdev->flags &= ~TCMUR_DEV_FLAG_IS_OPEN;
is_open = true;
}
pthread_mutex_unlock(&rdev->rdev_lock);
if (is_open)
rhandler->close(dev);
pthread_mutex_lock(&rdev->rdev_lock);
rdev->flags |= TCMUR_DEV_FLAG_STOPPED;
pthread_mutex_unlock(&rdev->rdev_lock);
tcmu_dev_dbg(dev, "cmdproc cleanup done\n");
}
int tcmur_get_time(struct tcmu_device *dev, struct timespec *time)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
int ret;
ret = clock_gettime(CLOCK_MONOTONIC_COARSE, time);
if (!ret) {
tcmu_dev_dbg(dev, "Current time %"PRIdMAX" secs.\n", (intmax_t)time->tv_sec);
return 0;
}
tcmu_dev_err(dev, "Could not get time. Error %d. Command timeout feature disabled.\n",
ret);
rdev->cmd_time_out = 0;
return ret;
}
static bool get_next_cmd_timeout(struct tcmu_device *dev,
struct timespec *curr_time,
struct timespec *tmo)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
int run_time, cmd_tmo = rdev->cmd_time_out;
struct tcmur_cmd *tcmur_cmd;
bool has_timeout = false;
if (!cmd_tmo)
return false;
memset(tmo, 0, sizeof(*tmo));
pthread_spin_lock(&rdev->cmds_list_lock);
list_for_each(&rdev->cmds_list, tcmur_cmd, cmds_list_entry) {
if (tcmur_cmd->timed_out)
continue;
has_timeout = true;
run_time = difftime(curr_time->tv_sec,
tcmur_cmd->start_time.tv_sec);
if (cmd_tmo > run_time) {
tmo->tv_sec = cmd_tmo - run_time;
} else {
/*
* We do not do a clock call for every command, so
* cmds can time out while we were processing new
* cmds. Force a recheck.
*/
tmo->tv_sec = 0;
}
tcmu_dev_dbg(dev, "Next cmd id %hu timeout in %"PRIdMAX" secs. Current time %"PRIdMAX". Start time %"PRIdMAX"\n",
tcmur_cmd->lib_cmd->cmd_id, (intmax_t)tmo->tv_sec,
(intmax_t)curr_time->tv_sec, (intmax_t)tcmur_cmd->start_time.tv_sec);
break;
}
pthread_spin_unlock(&rdev->cmds_list_lock);
return has_timeout;
}
static void check_for_timed_out_cmds(struct tcmu_device *dev)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
int cmd_tmo = rdev->cmd_time_out;
struct tcmur_cmd *tcmur_cmd;
struct timespec curr_time;
struct tcmulib_cmd *cmd;
int run_time;
uint8_t *cdb;
if (!cmd_tmo)
return;
memset(&curr_time, 0, sizeof(curr_time));
if (tcmur_get_time(dev, &curr_time))
return;
pthread_spin_lock(&rdev->cmds_list_lock);
list_for_each(&rdev->cmds_list, tcmur_cmd, cmds_list_entry) {
if (tcmur_cmd->timed_out)
continue;
run_time = difftime(curr_time.tv_sec,
tcmur_cmd->start_time.tv_sec);
if (run_time < cmd_tmo)
continue;
cmd = tcmur_cmd->lib_cmd;
if (tcmu_get_log_level() == TCMU_LOG_DEBUG_SCSI_CMD) {
tcmu_cdb_print_info(dev, cmd, "timed out.");
} else {
cdb = cmd->cdb;
tcmu_dev_info(dev, "Command %hu SCSI CDB 0x%x at LBA %"PRIu64" for %u blocks timed out.\n",
cmd->cmd_id, cdb[0],
tcmu_cdb_get_lba(cdb),
tcmu_cdb_get_xfer_length(cdb));
}
tcmur_cmd->timed_out = true;
/*
* These time outs are only currently used for diagnostic
* purposes right now, so we do not want to escalate the
* error handler and just return true here.
*/
tcmu_notify_cmd_timed_out(dev);
}
pthread_spin_unlock(&rdev->cmds_list_lock);
}
static void tcmur_tcmulib_cmd_start(struct tcmu_device *dev,
struct tcmulib_cmd *cmd,
struct timespec *curr_time)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
memset(tcmur_cmd, 0, sizeof(*tcmur_cmd));
tcmur_cmd->lib_cmd = cmd;
list_node_init(&tcmur_cmd->cmds_list_entry);
if (rdev->cmd_time_out) {
tcmur_cmd->start_time.tv_sec = curr_time->tv_sec;
pthread_spin_lock(&rdev->cmds_list_lock);
list_add_tail(&rdev->cmds_list, &tcmur_cmd->cmds_list_entry);
pthread_spin_unlock(&rdev->cmds_list_lock);
}
}
static void *tcmur_cmdproc_thread(void *arg)
{
struct tcmu_device *dev = arg;
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
struct pollfd pfd;
int ret;
bool dev_stopping = false;
tcmu_set_thread_name("cmdproc", dev);
pthread_cleanup_push(tcmur_stop_device, dev);
while (1) {
int completed = 0;
struct tcmulib_cmd *cmd;
struct timespec tmo, curr_time;
bool set_tmo;
tcmulib_processing_start(dev);
if (rdev->cmd_time_out)
tcmur_get_time(dev, &curr_time);
while (!dev_stopping &&
(cmd = tcmulib_get_next_command(dev,
sizeof(struct tcmur_cmd))) != NULL) {
tcmur_tcmulib_cmd_start(dev, cmd, &curr_time);
if (tcmu_get_log_level() == TCMU_LOG_DEBUG_SCSI_CMD)
tcmu_cdb_print_info(dev, cmd, NULL);
if (tcmur_handler_is_passthrough_only(rhandler))
ret = tcmur_cmd_passthrough_handler(dev, cmd);
else
ret = tcmur_generic_handle_cmd(dev, cmd);
if (ret == TCMU_STS_NOT_HANDLED)
tcmu_cdb_print_info(dev, cmd, "is not supported");
/*
* 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_STS_ASYNC_HANDLED) {
completed = 1;
tcmur_tcmulib_cmd_complete(dev, cmd, ret);
}
}
if (completed)
tcmulib_processing_complete(dev);
set_tmo = get_next_cmd_timeout(dev, &curr_time, &tmo);
pfd.fd = tcmu_dev_get_fd(dev);
pfd.events = POLLIN;
pfd.revents = 0;
/* Use ppoll instead poll to avoid poll call reschedules during signal
* handling. If we were removing a device, then the uio device's memory
* could be freed, but the poll would be rescheduled and end up accessing
* the released device. */
if (set_tmo) {
ret = ppoll(&pfd, 1, &tmo, NULL);
} else {
ret = ppoll(&pfd, 1, NULL, NULL);
}
if (ret == -1) {
tcmu_err("ppoll() returned %d\n", ret);
break;
}
if (!ret) {
check_for_timed_out_cmds(dev);
} else if (pfd.revents != POLLIN) {
tcmu_err("ppoll received unexpected revent: 0x%x\n", pfd.revents);
break;
}
/*
* LIO will wait for outstanding requests and prevent new ones
* from being sent to runner during device removal, but if the
* tcmu cmd_time_out has fired tcmu-runner may still be executing
* requests that LIO has completed. We only need to wait for replies
* for outstanding requests so throttle the cmdproc thread now.
*/
pthread_mutex_lock(&rdev->rdev_lock);
if (rdev->flags & TCMUR_DEV_FLAG_STOPPING)
dev_stopping = true;
pthread_mutex_unlock(&rdev->rdev_lock);
}
/*
* If we are doing a clean shutdown via dev_removed the
* removing thread will call the cleanup function when
* it has stopped and flushed the device.
*/
pthread_cleanup_pop(0);
return NULL;
}
static int dev_resize(struct tcmu_device *dev, struct tcmulib_cfg_info *cfg)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
uint64_t new_lbas = tcmu_byte_to_lba(dev, cfg->data.dev_size);
int ret;
if (tcmu_dev_get_num_lbas(dev) == new_lbas)
return 0;
ret = rhandler->reconfig(dev, cfg);
if (ret)
return ret;
tcmu_dev_set_num_lbas(dev, new_lbas);
tcmur_set_pending_ua(dev, TCMUR_UA_DEV_SIZE_CHANGED);
return 0;
}
static int dev_reconfig(struct tcmu_device *dev, struct tcmulib_cfg_info *cfg)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
if (!rhandler->reconfig)
return -EOPNOTSUPP;
switch (cfg->type) {
case TCMULIB_CFG_DEV_SIZE:
return dev_resize(dev, cfg);
default:
return rhandler->reconfig(dev, cfg);
}
}
static void parse_tcmu_runner_args(struct tcmu_device *dev)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
char *arg, *cfg_str, *arg_end, *cfg_end;
bool found;
cfg_str = tcmu_dev_get_cfgstring(dev);
/* count ending null in string */
cfg_end = cfg_str + strlen(cfg_str) + 1;
while ((arg = strstr(cfg_str, ";"))) {
found = false;
arg++;
if (!strncmp(arg, "tcmur_cmd_time_out=", 19)) {
rdev->cmd_time_out = atoi(arg + 19);
tcmu_dev_dbg(dev, "Using tcmur_cmd_timeout %d\n",
rdev->cmd_time_out);
found = true;
}
arg_end = strstr(arg, ";");
if (!arg_end) {
arg_end = cfg_end;
} else {
arg_end++;
}
if (found) {
memmove(arg - 1, arg_end, cfg_end - arg_end + 1);
} else {
cfg_str = arg;
}
}
tcmu_dev_dbg(dev, "Updated cfgstring: %s.\n",
tcmu_dev_get_cfgstring(dev));
}
static int dev_added(struct tcmu_device *dev)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct list_head group_list;
struct tcmur_device *rdev;
int32_t block_size, max_sectors;
int64_t dev_size;
int ret;
rdev = calloc(1, sizeof(*rdev));
if (!rdev)
return -ENOMEM;
tcmu_dev_set_private(dev, rdev);
list_node_init(&rdev->recovery_entry);
list_head_init(&rdev->cmds_list);
rdev->dev = dev;
parse_tcmu_runner_args(dev);
ret = -EINVAL;
block_size = tcmu_cfgfs_dev_get_attr_int(dev, "hw_block_size");
if (block_size <= 0) {
tcmu_dev_err(dev, "Could not get hw_block_size\n");
goto free_rdev;
}
tcmu_dev_set_block_size(dev, block_size);
dev_size = tcmu_cfgfs_dev_get_info_u64(dev, "Size", &ret);
if (ret < 0) {
tcmu_dev_err(dev, "Could not get device size\n");
goto free_rdev;
}
tcmu_dev_set_num_lbas(dev, tcmu_byte_to_lba(dev, dev_size));
max_sectors = tcmu_cfgfs_dev_get_attr_int(dev, "hw_max_sectors");
if (max_sectors < 0)
goto free_rdev;