forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1154 lines (970 loc) · 28.6 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 2014, Red Hat, 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.
*/
/*
* A daemon that supports a simplified interface for writing TCMU
* handlers.
*/
#define _GNU_SOURCE
#define _BITS_UIO_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 <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"
static char *handler_path = DEFAULT_HANDLER_PATH;
static struct tcmu_config *tcmu_cfg;
darray(struct tcmur_handler *) g_runner_handlers = darray_new();
int tcmur_register_handler(struct tcmur_handler *handler)
{
struct tcmur_handler *h;
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;
}
}
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);
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 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 sighandler(gpointer user_data)
{
g_main_loop_quit((GMainLoop*)user_data);
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)
{
return -1;
}
static void dbus_handler_close(struct tcmu_device *dev)
{
/* nop */
}
static int dbus_handler_handle_cmd(struct tcmu_device *dev,
struct tcmulib_cmd *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("/sys/module/target_core_user", &sb);
if (!ret) {
tcmu_dbg("Module target_core_user already loaded\n");
} else {
tcmu_err("stat() on '/sys/module/target_core_user' failed: %m\n");
}
}
} 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 == KMOD_PROBE_APPLY_BLACKLIST) {
tcmu_err("Module '%s' is blacklisted\n",
kmod_module_get_name(mod));
} else {
tcmu_err("Failed to insert '%s'\n",
kmod_module_get_name(mod));
}
ret = err;
}
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_get_daemon_dev_private(dev);
bool is_open = false;
pthread_mutex_lock(&rdev->state_lock);
/* check if this was already called due to thread cancelation */
if (rdev->flags & TCMUR_DEV_FLAG_STOPPED) {
pthread_mutex_unlock(&rdev->state_lock);
return;
}
rdev->flags |= TCMUR_DEV_FLAG_STOPPING;
pthread_mutex_unlock(&rdev->state_lock);
/*
* The lock thread can fire off the recovery thread, so make sure
* it is done first.
*/
tcmu_cancel_lock_thread(dev);
tcmu_cancel_recovery(dev);
pthread_mutex_lock(&rdev->state_lock);
if (rdev->flags & TCMUR_DEV_FLAG_IS_OPEN) {
rdev->flags &= ~TCMUR_DEV_FLAG_IS_OPEN;
is_open = true;
}
pthread_mutex_unlock(&rdev->state_lock);
if (is_open)
rhandler->close(dev);
pthread_mutex_lock(&rdev->state_lock);
rdev->flags |= TCMUR_DEV_FLAG_STOPPED;
pthread_mutex_unlock(&rdev->state_lock);
tcmu_dev_dbg(dev, "cmdproc cleanup done\n");
}
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_get_daemon_dev_private(dev);
struct pollfd pfd;
int ret;
bool dev_stopping = false;
pthread_cleanup_push(tcmur_stop_device, dev);
while (1) {
int completed = 0;
struct tcmulib_cmd *cmd;
tcmulib_processing_start(dev);
while (!dev_stopping && (cmd = tcmulib_get_next_command(dev)) != NULL) {
if (tcmu_get_log_level() == TCMU_LOG_DEBUG_SCSI_CMD)
tcmu_cdb_debug_info(dev, cmd);
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_NOT_HANDLED)
tcmu_dev_warn(dev, "Command 0x%x not supported\n", cmd->cdb[0]);
/*
* 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;
tcmur_command_complete(dev, cmd, ret);
}
}
if (completed)
tcmulib_processing_complete(dev);
pfd.fd = tcmu_get_dev_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. */
ret = ppoll(&pfd, 1, NULL, NULL);
if (ret == -1) {
tcmu_err("ppoll() returned %d\n", ret);
break;
}
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->state_lock);
if (rdev->flags & TCMUR_DEV_FLAG_STOPPING)
dev_stopping = true;
pthread_mutex_unlock(&rdev->state_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);
int ret;
if (tcmu_get_dev_num_lbas(dev) * tcmu_get_dev_block_size(dev) ==
cfg->data.dev_size)
return 0;
ret = rhandler->reconfig(dev, cfg);
if (ret)
return ret;
ret = tcmu_update_num_lbas(dev, cfg->data.dev_size);
if (!ret)
tcmur_set_pending_ua(dev, TCMUR_UA_DEV_SIZE_CHANGED);
return ret;
}
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 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;
uint32_t max_xfer_length;
int64_t dev_size;
int ret;
rdev = calloc(1, sizeof(*rdev));
if (!rdev)
return -ENOMEM;
tcmu_set_daemon_dev_private(dev, rdev);
list_node_init(&rdev->recovery_entry);
rdev->dev = dev;
ret = -EINVAL;
block_size = tcmu_get_attribute(dev, "hw_block_size");
if (block_size <= 0) {
tcmu_dev_err(dev, "Could not get hw_block_size\n");
goto free_rdev;
}
tcmu_set_dev_block_size(dev, block_size);
dev_size = tcmu_get_device_size(dev);
if (dev_size < 0) {
tcmu_dev_err(dev, "Could not get device size\n");
goto free_rdev;
}
tcmu_set_dev_num_lbas(dev, dev_size / block_size);
max_sectors = tcmu_get_attribute(dev, "hw_max_sectors");
if (max_sectors < 0)
goto free_rdev;
tcmu_set_dev_max_xfer_len(dev, max_sectors);
tcmu_dev_dbg(dev, "Got block_size %ld, size in bytes %lld\n",
block_size, dev_size);
ret = pthread_spin_init(&rdev->lock, 0);
if (ret != 0)
goto free_rdev;
ret = pthread_mutex_init(&rdev->caw_lock, NULL);
if (ret != 0)
goto cleanup_dev_lock;
ret = pthread_mutex_init(&rdev->format_lock, NULL);
if (ret != 0)
goto cleanup_caw_lock;
ret = pthread_mutex_init(&rdev->state_lock, NULL);
if (ret != 0)
goto cleanup_format_lock;
ret = setup_io_work_queue(dev);
if (ret < 0)
goto cleanup_state_lock;
ret = setup_aio_tracking(rdev);
if (ret < 0)
goto cleanup_io_work_queue;
ret = rhandler->open(dev);
if (ret)
goto cleanup_aio_tracking;
/*
* On the initial creation ALUA will probably not yet have been setup,
* but for reopens it will be so we need to sync our failover state.
*/
list_head_init(&group_list);
tcmu_get_alua_grps(dev, &group_list);
tcmu_release_alua_grps(&group_list);
rdev->flags |= TCMUR_DEV_FLAG_IS_OPEN;
ret = pthread_cond_init(&rdev->lock_cond, NULL);
if (ret < 0)
goto cleanup_lock_cond;
/*
* Set the optimal unmap granularity to max xfer len. Optimal unmap
* alignment starts at the begining of the device.
*/
max_xfer_length = tcmu_get_dev_max_xfer_len(dev);
tcmu_set_dev_opt_unmap_gran(dev, max_xfer_length);
tcmu_set_dev_unmap_gran_align(dev, 0);
ret = pthread_create(&rdev->cmdproc_thread, NULL, tcmur_cmdproc_thread,
dev);
if (ret < 0)
goto close_dev;
return 0;
close_dev:
rhandler->close(dev);
cleanup_lock_cond:
pthread_cond_destroy(&rdev->lock_cond);
cleanup_aio_tracking:
cleanup_aio_tracking(rdev);
cleanup_io_work_queue:
cleanup_io_work_queue(dev, true);
cleanup_state_lock:
pthread_mutex_destroy(&rdev->state_lock);
cleanup_format_lock:
pthread_mutex_destroy(&rdev->format_lock);
cleanup_caw_lock:
pthread_mutex_destroy(&rdev->caw_lock);
cleanup_dev_lock:
pthread_spin_destroy(&rdev->lock);
free_rdev:
free(rdev);
return ret;
}
void tcmu_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);
}
static void dev_removed(struct tcmu_device *dev)
{
struct tcmur_device *rdev = tcmu_get_daemon_dev_private(dev);
int ret;
pthread_mutex_lock(&rdev->state_lock);
rdev->flags |= TCMUR_DEV_FLAG_STOPPING;
pthread_mutex_unlock(&rdev->state_lock);
/*
* 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);
if (aio_wait_for_empty_queue(rdev))
tcmu_dev_err(dev, "could not flush queue.\n");
tcmu_cancel_thread(rdev->cmdproc_thread);
tcmur_stop_device(dev);
cleanup_io_work_queue(dev, false);
cleanup_aio_tracking(rdev);
ret = pthread_cond_destroy(&rdev->lock_cond);
if (ret != 0)
tcmu_err("could not cleanup lock cond %d\n", ret);
ret = pthread_mutex_destroy(&rdev->state_lock);
if (ret != 0)
tcmu_err("could not cleanup state lock %d\n", ret);
ret = pthread_mutex_destroy(&rdev->format_lock);
if (ret != 0)
tcmu_err("could not cleanup format lock %d\n", ret);
ret = pthread_mutex_destroy(&rdev->caw_lock);
if (ret != 0)
tcmu_err("could not cleanup caw lock %d\n", ret);
ret = pthread_spin_destroy(&rdev->lock);
if (ret != 0)
tcmu_err("could not cleanup mailbox lock %d\n", ret);
free(rdev);
tcmu_dev_dbg(dev, "removed from tcmu-runner\n");
}
#define TCMUR_MIN_OPEN_FD 65536
#define TCMUR_MAX_OPEN_FD 1048576
static int tcmu_set_max_fd_limit(const int nr_files)
{
struct rlimit old_rlim, new_rlim;
int ret;
ret = getrlimit(RLIMIT_NOFILE, &old_rlim);
if (ret == -1) {
tcmu_err("failed to get max open fd limit: %m\n");
return ret;
}
if (old_rlim.rlim_cur < nr_files) {
new_rlim.rlim_cur = nr_files;
if (old_rlim.rlim_max < nr_files) {
new_rlim.rlim_max = nr_files;
} else {
new_rlim.rlim_max = old_rlim.rlim_max;
}
ret = setrlimit(RLIMIT_NOFILE, &new_rlim);
if (ret == -1) {
tcmu_err("failed to set max open fd to [soft: %lld hard: %lld] %m\n",
(long long int)new_rlim.rlim_cur,
(long long int)new_rlim.rlim_max);
return ret;
}
tcmu_info("max open fd set to [soft: %lld hard: %lld]\n",
(long long int)new_rlim.rlim_cur,
(long long int)new_rlim.rlim_max);
return 0;
}
tcmu_info("max open fd remain [soft: %lld hard: %lld]\n",
(long long int)old_rlim.rlim_cur,
(long long int)old_rlim.rlim_max);
return 0;
}
static void usage(void) {
printf("\nusage:\n");
printf("\ttcmu-runner [options]\n");
printf("\noptions:\n");
printf("\t-h, --help: print this message and exit\n");
printf("\t-V, --version: print version and exit\n");
printf("\t-d, --debug: enable debug messages\n");
printf("\t-f, --nofile: set maximum file number could be opened\n");
printf("\t\tdefault will be as the systemd or the shell's limitation\n");
printf("\t--handler-path: set path to search for handler modules\n");
printf("\t\tdefault is %s\n", DEFAULT_HANDLER_PATH);
printf("\t-l, --tcmu-log-dir: tcmu log dir\n");
printf("\t\tdefault is %s\n", TCMU_LOG_DIR_DEFAULT);
printf("\n");
}
static struct option long_options[] = {
{"debug", no_argument, 0, 'd'},
{"handler-path", required_argument, 0, 0},
{"tcmu-log-dir", required_argument, 0, 'l'},
{"nofile", required_argument, 0, 'f'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{0, 0, 0, 0},
};
int main(int argc, char **argv)
{
darray(struct tcmulib_handler) handlers = darray_new();
struct tcmulib_context *tcmulib_context;
struct tcmur_handler **tmp_r_handler;
GMainLoop *loop;
GIOChannel *libtcmu_gio;
guint reg_id;
bool new_path = false;
int ret;
while (1) {
int option_index = 0;
int c, nr_files;
c = getopt_long(argc, argv, "df:hl:V",
long_options, &option_index);