-
Notifications
You must be signed in to change notification settings - Fork 49
/
main.c
4116 lines (3611 loc) · 97.8 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) 2004, 2005 Christophe Varoqui
* Copyright (c) 2005 Kiyoshi Ueda, NEC
* Copyright (c) 2005 Benjamin Marzinski, Redhat
* Copyright (c) 2005 Edward Goggin, EMC
*/
#include "autoconfig.h"
#include <unistd.h>
#include <sys/stat.h>
#include <libdevmapper.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <linux/oom.h>
#include <libudev.h>
#include <urcu.h>
#include "fpin.h"
#ifdef USE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
#include <semaphore.h>
#include <time.h>
#include <stdbool.h>
/*
* libmultipath
*/
#include "time-util.h"
/*
* libcheckers
*/
#include "checkers.h"
/*
* libmultipath
*/
#include "version.h"
#include "parser.h"
#include "vector.h"
#include "config.h"
#include "util.h"
#include "hwtable.h"
#include "defaults.h"
#include "structs.h"
#include "blacklist.h"
#include "structs_vec.h"
#include "dmparser.h"
#include "devmapper.h"
#include "sysfs.h"
#include "dict.h"
#include "discovery.h"
#include "debug.h"
#include "propsel.h"
#include "uevent.h"
#include "switchgroup.h"
#include "print.h"
#include "configure.h"
#include "prio.h"
#include "wwids.h"
#include "pgpolicies.h"
#include "log.h"
#include "uxsock.h"
#include "alias.h"
#include "mpath_cmd.h"
#include "mpath_persist.h"
#include "mpath_persist_int.h"
#include "prioritizers/alua_rtpg.h"
#include "main.h"
#include "pidfile.h"
#include "uxlsnr.h"
#include "uxclnt.h"
#include "cli.h"
#include "cli_handlers.h"
#include "lock.h"
#include "waiter.h"
#include "dmevents.h"
#include "io_err_stat.h"
#include "foreign.h"
#include "../third-party/valgrind/drd.h"
#include "init_unwinder.h"
#define CMDSIZE 160
#define MSG_SIZE 32
int mpath_pr_event_handle(struct path *pp);
void * mpath_pr_event_handler_fn (void * );
#define LOG_MSG(lvl, pp) \
do { \
if (pp->mpp && checker_selected(&pp->checker) && \
lvl <= libmp_verbosity) { \
if (pp->offline) \
condlog(lvl, "%s: %s - path offline", \
pp->mpp->alias, pp->dev); \
else { \
const char *__m = \
checker_message(&pp->checker); \
\
if (strlen(__m)) \
condlog(lvl, "%s: %s - %s checker%s", \
pp->mpp->alias, \
pp->dev, \
checker_name(&pp->checker), \
__m); \
} \
} \
} while(0)
struct mpath_event_param
{
char * devname;
struct multipath *mpp;
};
int uxsock_timeout;
static int verbosity;
static int bindings_read_only;
int ignore_new_devs;
#ifdef NO_DMEVENTS_POLL
static int poll_dmevents = 0;
#else
static int poll_dmevents = 1;
#endif
/* Don't access this variable without holding config_lock */
static enum daemon_status running_state = DAEMON_INIT;
/* Don't access this variable without holding config_lock */
static bool delayed_reconfig;
/* Don't access this variable without holding config_lock */
static enum force_reload_types reconfigure_pending = FORCE_RELOAD_NONE;
pid_t daemon_pid;
static pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t config_cond;
static pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr,
fpin_thr, fpin_consumer_thr;
static bool check_thr_started, uevent_thr_started, uxlsnr_thr_started,
uevq_thr_started, dmevent_thr_started, fpin_thr_started,
fpin_consumer_thr_started;
static int pid_fd = -1;
static inline enum daemon_status get_running_state(bool *pending_reconfig)
{
enum daemon_status st;
pthread_mutex_lock(&config_lock);
st = running_state;
if (pending_reconfig != NULL)
*pending_reconfig = (reconfigure_pending != FORCE_RELOAD_NONE);
pthread_mutex_unlock(&config_lock);
return st;
}
int should_exit(void)
{
return get_running_state(NULL) == DAEMON_SHUTDOWN;
}
/*
* global copy of vecs for use in sig handlers
*/
static struct vectors * gvecs;
struct config *multipath_conf;
/* Local variables */
static volatile sig_atomic_t exit_sig;
static volatile sig_atomic_t reconfig_sig;
static volatile sig_atomic_t log_reset_sig;
static const char *daemon_status_msg[DAEMON_STATUS_SIZE] = {
[DAEMON_INIT] = "init",
[DAEMON_START] = "startup",
[DAEMON_CONFIGURE] = "configure",
[DAEMON_IDLE] = "idle",
[DAEMON_RUNNING] = "running",
[DAEMON_SHUTDOWN] = "shutdown",
};
const char *
daemon_status(bool *pending_reconfig)
{
int status = get_running_state(pending_reconfig);
if (status < DAEMON_INIT || status >= DAEMON_STATUS_SIZE)
return NULL;
return daemon_status_msg[status];
}
/*
* I love you too, systemd ...
*/
#ifdef USE_SYSTEMD
static void do_sd_notify(enum daemon_status old_state,
enum daemon_status new_state)
{
char notify_msg[MSG_SIZE];
const char *msg;
static bool startup_done = false;
/*
* Checkerloop switches back and forth between idle and running state.
* No need to tell systemd each time.
* These notifications cause a lot of overhead on dbus.
*/
if ((new_state == DAEMON_IDLE || new_state == DAEMON_RUNNING) &&
(old_state == DAEMON_IDLE || old_state == DAEMON_RUNNING))
return;
if (new_state == DAEMON_IDLE || new_state == DAEMON_RUNNING)
msg = "up";
else
msg = daemon_status_msg[new_state];
if (msg && !safe_sprintf(notify_msg, "STATUS=%s", msg))
sd_notify(0, notify_msg);
if (new_state == DAEMON_SHUTDOWN) {
/* Tell systemd that we're not RELOADING any more */
if (old_state == DAEMON_CONFIGURE && startup_done)
sd_notify(0, "READY=1");
sd_notify(0, "STOPPING=1");
} else if (new_state == DAEMON_IDLE && old_state == DAEMON_CONFIGURE) {
sd_notify(0, "READY=1");
startup_done = true;
} else if (new_state == DAEMON_CONFIGURE && startup_done)
sd_notify(0, "RELOADING=1");
}
#else
static void do_sd_notify(__attribute__((unused)) enum daemon_status old_state,
__attribute__((unused)) enum daemon_status new_state)
{}
#endif
static void config_cleanup(__attribute__((unused)) void *arg)
{
pthread_mutex_unlock(&config_lock);
}
#define wait_for_state_change__(condition, ms) \
({ \
struct timespec tmo; \
int rc = 0; \
\
if (condition) { \
get_monotonic_time(&tmo); \
tmo.tv_nsec += (ms) * 1000 * 1000; \
normalize_timespec(&tmo); \
do \
rc = pthread_cond_timedwait( \
&config_cond, &config_lock, &tmo); \
while (rc == 0 && (condition)); \
} \
rc; \
})
/*
* If the current status is @oldstate, wait for at most @ms milliseconds
* for the state to change, and return the new state, which may still be
* @oldstate.
*/
enum daemon_status wait_for_state_change_if(enum daemon_status oldstate,
unsigned long ms)
{
enum daemon_status st;
if (oldstate == DAEMON_SHUTDOWN)
return DAEMON_SHUTDOWN;
pthread_mutex_lock(&config_lock);
pthread_cleanup_push(config_cleanup, NULL);
wait_for_state_change__(running_state == oldstate, ms);
st = running_state;
pthread_cleanup_pop(1);
return st;
}
/* must be called with config_lock held */
static void post_config_state__(enum daemon_status state)
{
if (state != running_state && running_state != DAEMON_SHUTDOWN) {
enum daemon_status old_state = running_state;
running_state = state;
pthread_cond_broadcast(&config_cond);
do_sd_notify(old_state, state);
condlog(4, "daemon state %s -> %s",
daemon_status_msg[old_state], daemon_status_msg[state]);
}
}
void post_config_state(enum daemon_status state)
{
pthread_mutex_lock(&config_lock);
pthread_cleanup_push(config_cleanup, NULL);
post_config_state__(state);
pthread_cleanup_pop(1);
}
static bool unblock_reconfigure(void)
{
bool was_delayed;
pthread_mutex_lock(&config_lock);
was_delayed = delayed_reconfig;
if (was_delayed) {
delayed_reconfig = false;
/*
* In IDLE state, make sure child() is woken up
* Otherwise it will wake up when state switches to IDLE
*/
if (running_state == DAEMON_IDLE)
post_config_state__(DAEMON_CONFIGURE);
}
pthread_mutex_unlock(&config_lock);
if (was_delayed)
condlog(3, "unblocked delayed reconfigure");
return was_delayed;
}
/*
* Make sure child() is woken up when a map is removed that multipathd
* is currently waiting for.
* Overrides libmultipath's weak symbol by the same name
*/
void remove_map_callback(struct multipath *mpp)
{
if (mpp->wait_for_udev > 0)
unblock_reconfigure();
}
void schedule_reconfigure(enum force_reload_types requested_type)
{
pthread_mutex_lock(&config_lock);
pthread_cleanup_push(config_cleanup, NULL);
enum force_reload_types type;
type = (reconfigure_pending == FORCE_RELOAD_YES ||
requested_type == FORCE_RELOAD_YES) ?
FORCE_RELOAD_YES : FORCE_RELOAD_WEAK;
switch (running_state)
{
case DAEMON_SHUTDOWN:
break;
case DAEMON_IDLE:
reconfigure_pending = type;
post_config_state__(DAEMON_CONFIGURE);
break;
case DAEMON_CONFIGURE:
case DAEMON_RUNNING:
reconfigure_pending = type;
break;
default:
break;
}
pthread_cleanup_pop(1);
}
static enum daemon_status set_config_state(enum daemon_status state)
{
int rc = 0;
enum daemon_status st;
pthread_cleanup_push(config_cleanup, NULL);
pthread_mutex_lock(&config_lock);
while (rc == 0 &&
running_state != state &&
running_state != DAEMON_SHUTDOWN &&
running_state != DAEMON_IDLE) {
rc = pthread_cond_wait(&config_cond, &config_lock);
}
if (rc == 0 && running_state == DAEMON_IDLE && state != DAEMON_IDLE)
post_config_state__(state);
st = running_state;
pthread_cleanup_pop(1);
return st;
}
struct config *get_multipath_config(void)
{
rcu_read_lock();
return rcu_dereference(multipath_conf);
}
void put_multipath_config(__attribute__((unused)) void *arg)
{
rcu_read_unlock();
}
/*
* The path group orderings that this function finds acceptable are different
* from now select_path_group determines the best pathgroup. The idea here is
* to only trigger a kernel reload when it is obvious that the pathgroups would
* be out of order, even if all the paths were usable. Thus pathgroups with
* PRIO_UNDEF are skipped, and the number of enabled paths doesn't matter here.
*/
bool path_groups_in_order(struct multipath *mpp)
{
int i;
struct pathgroup *pgp;
bool seen_marginal_pg = false;
int last_prio = INT_MAX;
if (VECTOR_SIZE(mpp->pg) < 2)
return true;
vector_foreach_slot(mpp->pg, pgp, i) {
if (seen_marginal_pg && !pgp->marginal)
return false;
/* skip pgs with PRIO_UNDEF, since this is likely temporary */
if (!pgp->paths || pgp->priority == PRIO_UNDEF)
continue;
if (pgp->marginal && !seen_marginal_pg) {
seen_marginal_pg = true;
last_prio = pgp->priority;
continue;
}
if (pgp->priority > last_prio)
return false;
last_prio = pgp->priority;
}
return true;
}
static int
need_switch_pathgroup (struct multipath * mpp, bool *need_reload)
{
int bestpg;
*need_reload = false;
if (!mpp)
return 0;
if (VECTOR_SIZE(mpp->pg) < 2)
return 0;
bestpg = select_path_group(mpp);
if (mpp->pgfailback == -FAILBACK_MANUAL)
return 0;
mpp->bestpg = bestpg;
*need_reload = !path_groups_in_order(mpp);
return (*need_reload || mpp->bestpg != mpp->nextpg);
}
static void
switch_pathgroup (struct multipath * mpp)
{
mpp->stat_switchgroup++;
dm_switchgroup(mpp->alias, mpp->bestpg);
condlog(2, "%s: switch to path group #%i",
mpp->alias, mpp->bestpg);
}
static int
wait_for_events(struct multipath *mpp, struct vectors *vecs)
{
if (poll_dmevents)
return watch_dmevents(mpp->alias);
else
return start_waiter_thread(mpp, vecs);
}
static void
remove_map_and_stop_waiter(struct multipath *mpp, struct vectors *vecs)
{
/* devices are automatically removed by the dmevent polling code,
* so they don't need to be manually removed here */
condlog(3, "%s: removing map from internal tables", mpp->alias);
if (!poll_dmevents)
stop_waiter_thread(mpp);
remove_map(mpp, vecs->pathvec, vecs->mpvec);
}
static void
remove_maps_and_stop_waiters(struct vectors *vecs)
{
int i;
struct multipath * mpp;
if (!vecs)
return;
if (!poll_dmevents) {
vector_foreach_slot(vecs->mpvec, mpp, i)
stop_waiter_thread(mpp);
}
else
unwatch_all_dmevents();
remove_maps(vecs);
}
int refresh_multipath(struct vectors *vecs, struct multipath *mpp)
{
if (dm_get_info(mpp->alias, &mpp->dmi) != DMP_OK) {
/* Error accessing table */
condlog(2, "%s: cannot access table", mpp->alias);
goto out;
}
if (update_multipath_strings(mpp, vecs->pathvec) != DMP_OK) {
condlog(0, "%s: failed to setup multipath", mpp->alias);
goto out;
}
return 0;
out:
remove_map_and_stop_waiter(mpp, vecs);
return 1;
}
int setup_multipath(struct vectors *vecs, struct multipath *mpp)
{
if (refresh_multipath(vecs, mpp) != 0)
return 1;
set_no_path_retry(mpp);
if (VECTOR_SIZE(mpp->paths) != 0)
dm_cancel_deferred_remove(mpp);
return 0;
}
int update_multipath (struct vectors *vecs, char *mapname)
{
struct multipath *mpp;
struct pathgroup *pgp;
struct path *pp;
int i, j;
mpp = find_mp_by_alias(vecs->mpvec, mapname);
if (!mpp) {
condlog(3, "%s: multipath map not found", mapname);
return 2;
}
if (setup_multipath(vecs, mpp))
return 1; /* mpp freed in setup_multipath */
/*
* compare checkers states with DM states
*/
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if (pp->dmstate != PSTATE_FAILED)
continue;
if (pp->state != PATH_DOWN) {
struct config *conf;
int oldstate = pp->state;
unsigned int checkint;
conf = get_multipath_config();
checkint = conf->checkint;
put_multipath_config(conf);
condlog(2, "%s: mark as failed", pp->dev);
mpp->stat_path_failures++;
pp->state = PATH_DOWN;
if (oldstate == PATH_UP ||
oldstate == PATH_GHOST)
update_queue_mode_del_path(mpp);
/*
* if opportune,
* schedule the next check earlier
*/
if (pp->tick > checkint)
pp->tick = checkint;
}
}
}
return 0;
}
static bool
flush_map_nopaths(struct multipath *mpp, struct vectors *vecs) {
int r;
bool is_queueing = true;
if (mpp->features)
is_queueing = strstr(mpp->features, "queue_if_no_path");
/* It's not safe to do a remove of a map that has "queue_if_no_path"
* set, since there could be outstanding IO which will cause
* multipathd to hang while attempting the remove */
if (mpp->flush_on_last_del == FLUSH_NEVER && is_queueing) {
condlog(2, "%s: map is queueing, can't remove", mpp->alias);
return false;
}
if (mpp->flush_on_last_del == FLUSH_UNUSED &&
mpath_in_use(mpp->alias) && is_queueing) {
condlog(2, "%s: map in use and queueing, can't remove",
mpp->alias);
return false;
}
/*
* This will flush FLUSH_NEVER devices and FLUSH_UNUSED devices
* that are in use, but only if they are already marked as not
* queueing. That is just to make absolutely certain that they
* really are not queueing, like they claim.
*/
condlog(is_queueing ? 2 : 3, "%s Last path deleted, disabling queueing",
mpp->alias);
mpp->retry_tick = 0;
mpp->no_path_retry = NO_PATH_RETRY_FAIL;
mpp->disable_queueing = 1;
mpp->stat_map_failures++;
if (dm_queue_if_no_path(mpp, 0) != 0) {
condlog(0, "%s: failed to disable queueing. Not removing",
mpp->alias);
return false;
}
r = dm_flush_map_nopaths(mpp->alias, mpp->deferred_remove);
if (r != DM_FLUSH_OK) {
if (r == DM_FLUSH_DEFERRED) {
condlog(2, "%s: devmap deferred remove", mpp->alias);
mpp->deferred_remove = DEFERRED_REMOVE_IN_PROGRESS;
}
else
condlog(0, "%s: can't flush", mpp->alias);
return false;
}
condlog(2, "%s: map flushed after removing all paths", mpp->alias);
remove_map_and_stop_waiter(mpp, vecs);
return true;
}
static void
pr_register_active_paths(struct multipath *mpp)
{
unsigned int i, j;
struct path *pp;
struct pathgroup *pgp;
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if ((pp->state == PATH_UP) || (pp->state == PATH_GHOST))
mpath_pr_event_handle(pp);
}
}
}
static int
update_map (struct multipath *mpp, struct vectors *vecs, int new_map)
{
int retries = 3;
char *params __attribute__((cleanup(cleanup_charp))) = NULL;
retry:
condlog(4, "%s: updating new map", mpp->alias);
if (adopt_paths(vecs->pathvec, mpp, NULL)) {
condlog(0, "%s: failed to adopt paths for new map update",
mpp->alias);
retries = -1;
goto fail;
}
verify_paths(mpp);
if (VECTOR_SIZE(mpp->paths) == 0 &&
flush_map_nopaths(mpp, vecs))
return 1;
mpp->action = ACT_RELOAD;
if (setup_map(mpp, ¶ms, vecs)) {
condlog(0, "%s: failed to setup new map in update", mpp->alias);
retries = -1;
goto fail;
}
if (domap(mpp, params, 1) == DOMAP_FAIL && retries-- > 0) {
condlog(0, "%s: map_udate sleep", mpp->alias);
free(params);
params = NULL;
sleep(1);
goto retry;
}
fail:
if (new_map && (retries < 0 || wait_for_events(mpp, vecs))) {
condlog(0, "%s: failed to create new map", mpp->alias);
remove_map(mpp, vecs->pathvec, vecs->mpvec);
return 1;
}
if (setup_multipath(vecs, mpp))
return 1;
sync_map_state(mpp);
if (mpp->prflag != PRFLAG_SET)
update_map_pr(mpp);
if (mpp->prflag == PRFLAG_SET)
pr_register_active_paths(mpp);
if (retries < 0)
condlog(0, "%s: failed reload in new map update", mpp->alias);
return 0;
}
static int add_map_without_path (struct vectors *vecs, const char *alias)
{
struct multipath __attribute__((cleanup(cleanup_multipath_and_paths)))
*mpp = alloc_multipath();
char __attribute__((cleanup(cleanup_charp))) *params = NULL;
char __attribute__((cleanup(cleanup_charp))) *status = NULL;
struct config *conf;
char uuid[DM_UUID_LEN];
int rc = DMP_ERR;
if (!mpp || !(mpp->alias = strdup(alias)))
return DMP_ERR;
if ((rc = libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_MPATH_ONLY | MAPINFO_CHECK_UUID,
(mapid_t) { .str = mpp->alias },
(mapinfo_t) {
.uuid = uuid,
.dmi = &mpp->dmi,
.size = &mpp->size,
.target = ¶ms,
.status = &status,
})) != DMP_OK)
return rc;
strlcpy(mpp->wwid, uuid + UUID_PREFIX_LEN, sizeof(mpp->wwid));
if (!strlen(mpp->wwid))
condlog(1, "%s: adding map with empty WWID", mpp->alias);
conf = get_multipath_config();
mpp->mpe = find_mpe(conf->mptable, mpp->wwid);
put_multipath_config(conf);
if ((rc = update_multipath_table__(mpp, vecs->pathvec, 0, params, status)) != DMP_OK)
return DMP_ERR;
if (!vector_alloc_slot(vecs->mpvec))
return DMP_ERR;
vector_set_slot(vecs->mpvec, steal_ptr(mpp));
/*
* We can't pass mpp here, steal_ptr() has just nullified it.
* vector_set_slot() just set the last slot, use that.
*/
if (update_map(VECTOR_LAST_SLOT(vecs->mpvec), vecs, 1) != 0) /* map removed */
return DMP_ERR;
return DMP_OK;
}
static int
coalesce_maps(struct vectors *vecs, vector nmpv)
{
struct multipath * ompp;
vector ompv = vecs->mpvec;
int i, reassign_maps;
struct config *conf;
conf = get_multipath_config();
reassign_maps = conf->reassign_maps;
put_multipath_config(conf);
vector_foreach_slot (ompv, ompp, i) {
condlog(3, "%s: coalesce map", ompp->alias);
if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
/*
* remove all current maps not allowed by the
* current configuration
*/
if (dm_flush_map(ompp->alias) != DM_FLUSH_OK) {
condlog(0, "%s: unable to flush devmap",
ompp->alias);
/*
* may be just because the device is open
*/
if (setup_multipath(vecs, ompp) != 0) {
i--;
continue;
}
if (!vector_alloc_slot(nmpv))
return 1;
vector_set_slot(nmpv, ompp);
vector_del_slot(ompv, i);
i--;
}
else
condlog(2, "%s devmap removed", ompp->alias);
} else if (reassign_maps) {
condlog(3, "%s: Reassign existing device-mapper"
" devices", ompp->alias);
dm_reassign(ompp->alias);
}
}
return 0;
}
static void
sync_maps_state(vector mpvec)
{
unsigned int i;
struct multipath *mpp;
vector_foreach_slot (mpvec, mpp, i)
sync_map_state(mpp);
}
int
flush_map(struct multipath * mpp, struct vectors * vecs)
{
int r = dm_suspend_and_flush_map(mpp->alias, 0);
if (r != DM_FLUSH_OK) {
if (r == DM_FLUSH_FAIL_CANT_RESTORE)
remove_feature(&mpp->features, "queue_if_no_path");
condlog(0, "%s: can't flush", mpp->alias);
return r;
}
condlog(2, "%s: map flushed", mpp->alias);
remove_map_and_stop_waiter(mpp, vecs);
return 0;
}
static int
uev_add_map (struct uevent * uev, struct vectors * vecs)
{
char *alias;
int major = -1, minor = -1, rc;
condlog(3, "%s: add map (uevent)", uev->kernel);
alias = uevent_get_dm_name(uev);
if (!alias) {
condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
major = uevent_get_major(uev);
minor = uevent_get_minor(uev);
alias = dm_mapname(major, minor);
if (!alias) {
condlog(2, "%s: mapname not found for %d:%d",
uev->kernel, major, minor);
return 1;
}
}
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
rc = ev_add_map(uev->kernel, alias, vecs);
lock_cleanup_pop(vecs->lock);
free(alias);
return rc;
}
/*
* ev_add_map expects that the multipath device already exists in kernel
* before it is called. It just adds a device to multipathd or updates an
* existing device.
*/
int
ev_add_map (char * dev, const char * alias, struct vectors * vecs)
{
struct multipath * mpp;
int reassign_maps, rc;
struct config *conf;
mpp = find_mp_by_alias(vecs->mpvec, alias);
if (mpp) {
if (mpp->wait_for_udev > 1) {
condlog(2, "%s: performing delayed actions",
mpp->alias);
if (update_map(mpp, vecs, 0))
/* setup multipathd removed the map */
return 1;
}
conf = get_multipath_config();
reassign_maps = conf->reassign_maps;
put_multipath_config(conf);
dm_get_info(mpp->alias, &mpp->dmi);
if (mpp->wait_for_udev) {
mpp->wait_for_udev = 0;
if (!need_to_delay_reconfig(vecs) &&
unblock_reconfigure())
return 0;
}
/*
* Not really an error -- we generate our own uevent
* if we create a multipath mapped device as a result
* of uev_add_path
*/
if (reassign_maps) {
condlog(3, "%s: Reassign existing device-mapper devices",
alias);
dm_reassign(alias);
}
return 0;
}
condlog(2, "%s: adding map", alias);
/*
* now we can register the map
*/
if ((rc = add_map_without_path(vecs, alias)) == DMP_OK) {
condlog(2, "%s: devmap %s registered", alias, dev);
return 0;
} else if (rc == DMP_NO_MATCH) {
condlog(4, "%s: not a multipath map", alias);
return 0;
} else {
condlog(2, "%s: ev_add_map failed", dev);
return 1;
}
}
static int
uev_remove_map (struct uevent * uev, struct vectors * vecs)
{
char *alias;
int minor;
struct multipath *mpp;
condlog(3, "%s: remove map (uevent)", uev->kernel);
alias = uevent_get_dm_name(uev);
if (!alias) {
condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
return 0;
}
minor = uevent_get_minor(uev);
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
mpp = find_mp_by_minor(vecs->mpvec, minor);
if (!mpp) {
condlog(2, "%s: devmap not registered, can't remove",
uev->kernel);
goto out;
}
if (strcmp(mpp->alias, alias)) {
condlog(2, "%s: map alias mismatch: have \"%s\", got \"%s\")",
uev->kernel, mpp->alias, alias);
goto out;
}
dm_queue_if_no_path(mpp, 0);
remove_map_and_stop_waiter(mpp, vecs);
out:
lock_cleanup_pop(vecs->lock);
free(alias);
return 0;
}
static void
rescan_path(struct udev_device *ud)
{
ud = udev_device_get_parent_with_subsystem_devtype(ud, "scsi",
"scsi_device");
if (ud) {
ssize_t ret =
sysfs_attr_set_value(ud, "rescan", "1", strlen("1"));
if (ret != strlen("1"))
log_sysfs_attr_set_value(1, ret,
"%s: failed to trigger rescan",
udev_device_get_syspath(ud));
}
}
/* Returns true if the path was removed */
bool
handle_path_wwid_change(struct path *pp, struct vectors *vecs)
{
struct udev_device *udd;
static const char add[] = "add";
ssize_t ret;
char dev[FILE_NAME_SIZE];
bool removed = false;
if (!pp || !pp->udev)
return removed;
strlcpy(dev, pp->dev, sizeof(dev));
udd = udev_device_ref(pp->udev);
if (ev_remove_path(pp, vecs, 1) & REMOVE_PATH_SUCCESS) {
removed = true;
} else if (pp->mpp) {
pp->dmstate = PSTATE_FAILED;
dm_fail_path(pp->mpp->alias, pp->dev_t);
}
rescan_path(udd);
ret = sysfs_attr_set_value(udd, "uevent", add, sizeof(add) - 1);