f48664a0445a286bc223dacf93409a23f3d70cdd
[multipath-tools/.git] / libmultipath / configure.c
1 /*
2  * Copyright (c) 2003, 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Benjamin Marzinski, Redhat
4  * Copyright (c) 2005 Kiyoshi Ueda, NEC
5  * Copyright (c) 2005 Patrick Caulfield, Redhat
6  * Copyright (c) 2005 Edward Goggin, EMC
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <sys/file.h>
14 #include <errno.h>
15 #include <ctype.h>
16 #include <libdevmapper.h>
17 #include <libudev.h>
18 #include "mpath_cmd.h"
19
20 #include "checkers.h"
21 #include "vector.h"
22 #include "memory.h"
23 #include "devmapper.h"
24 #include "defaults.h"
25 #include "structs.h"
26 #include "structs_vec.h"
27 #include "dmparser.h"
28 #include "config.h"
29 #include "blacklist.h"
30 #include "propsel.h"
31 #include "discovery.h"
32 #include "debug.h"
33 #include "switchgroup.h"
34 #include "dm-generic.h"
35 #include "print.h"
36 #include "configure.h"
37 #include "pgpolicies.h"
38 #include "dict.h"
39 #include "alias.h"
40 #include "prio.h"
41 #include "util.h"
42 #include "uxsock.h"
43 #include "wwids.h"
44 #include "sysfs.h"
45 #include "io_err_stat.h"
46
47 /* group paths in pg by host adapter
48  */
49 int group_by_host_adapter(struct pathgroup *pgp, vector adapters)
50 {
51         struct adapter_group *agp;
52         struct host_group *hgp;
53         struct path *pp, *pp1;
54         char adapter_name1[SLOT_NAME_SIZE];
55         char adapter_name2[SLOT_NAME_SIZE];
56         int i, j;
57         int found_hostgroup = 0;
58
59         while (VECTOR_SIZE(pgp->paths) > 0) {
60
61                 pp = VECTOR_SLOT(pgp->paths, 0);
62
63                 if (sysfs_get_host_adapter_name(pp, adapter_name1))
64                         goto out;
65                 /* create a new host adapter group
66                  */
67                 agp = alloc_adaptergroup();
68                 if (!agp)
69                         goto out;
70                 agp->pgp = pgp;
71
72                 strncpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE - 1);
73                 store_adaptergroup(adapters, agp);
74
75                 /* create a new host port group
76                  */
77                 hgp = alloc_hostgroup();
78                 if (!hgp)
79                         goto out;
80                 if (store_hostgroup(agp->host_groups, hgp))
81                         goto out;
82
83                 hgp->host_no = pp->sg_id.host_no;
84                 agp->num_hosts++;
85                 if (store_path(hgp->paths, pp))
86                         goto out;
87
88                 hgp->num_paths++;
89                 /* delete path from path group
90                  */
91                 vector_del_slot(pgp->paths, 0);
92
93                 /* add all paths belonging to same host adapter
94                  */
95                 vector_foreach_slot(pgp->paths, pp1, i) {
96                         if (sysfs_get_host_adapter_name(pp1, adapter_name2))
97                                 goto out;
98                         if (strcmp(adapter_name1, adapter_name2) == 0) {
99                                 found_hostgroup = 0;
100                                 vector_foreach_slot(agp->host_groups, hgp, j) {
101                                         if (hgp->host_no == pp1->sg_id.host_no) {
102                                                 if (store_path(hgp->paths, pp1))
103                                                         goto out;
104                                                 hgp->num_paths++;
105                                                 found_hostgroup = 1;
106                                                 break;
107                                         }
108                                 }
109                                 if (!found_hostgroup) {
110                                         /* this path belongs to new host port
111                                          * within this adapter
112                                          */
113                                         hgp = alloc_hostgroup();
114                                         if (!hgp)
115                                                 goto out;
116
117                                         if (store_hostgroup(agp->host_groups, hgp))
118                                                 goto out;
119
120                                         agp->num_hosts++;
121                                         if (store_path(hgp->paths, pp1))
122                                                 goto out;
123
124                                         hgp->host_no = pp1->sg_id.host_no;
125                                         hgp->num_paths++;
126                                 }
127                                 /* delete paths from original path_group
128                                  * as they are added into adapter group now
129                                  */
130                                 vector_del_slot(pgp->paths, i);
131                                 i--;
132                         }
133                 }
134         }
135         return 0;
136
137 out:    /* add back paths into pg as re-ordering failed
138          */
139         vector_foreach_slot(adapters, agp, i) {
140                         vector_foreach_slot(agp->host_groups, hgp, j) {
141                                 while (VECTOR_SIZE(hgp->paths) > 0) {
142                                         pp = VECTOR_SLOT(hgp->paths, 0);
143                                         if (store_path(pgp->paths, pp))
144                                                 condlog(3, "failed to restore "
145                                                 "path %s into path group",
146                                                  pp->dev);
147                                         vector_del_slot(hgp->paths, 0);
148                                 }
149                         }
150                 }
151         free_adaptergroup(adapters);
152         return 1;
153 }
154
155 /* re-order paths in pg by alternating adapters and host ports
156  * for optimized selection
157  */
158 int order_paths_in_pg_by_alt_adapters(struct pathgroup *pgp, vector adapters,
159                  int total_paths)
160 {
161         int next_adapter_index = 0;
162         struct adapter_group *agp;
163         struct host_group *hgp;
164         struct path *pp;
165
166         while (total_paths > 0) {
167                 agp = VECTOR_SLOT(adapters, next_adapter_index);
168                 if (!agp) {
169                         condlog(0, "can't get adapter group %d", next_adapter_index);
170                         return 1;
171                 }
172
173                 hgp = VECTOR_SLOT(agp->host_groups, agp->next_host_index);
174                 if (!hgp) {
175                         condlog(0, "can't get host group %d of adapter group %d", next_adapter_index, agp->next_host_index);
176                         return 1;
177                 }
178
179                 if (!hgp->num_paths) {
180                         agp->next_host_index++;
181                         agp->next_host_index %= agp->num_hosts;
182                         next_adapter_index++;
183                         next_adapter_index %= VECTOR_SIZE(adapters);
184                         continue;
185                 }
186
187                 pp  = VECTOR_SLOT(hgp->paths, 0);
188
189                 if (store_path(pgp->paths, pp))
190                         return 1;
191
192                 total_paths--;
193
194                 vector_del_slot(hgp->paths, 0);
195
196                 hgp->num_paths--;
197
198                 agp->next_host_index++;
199                 agp->next_host_index %= agp->num_hosts;
200                 next_adapter_index++;
201                 next_adapter_index %= VECTOR_SIZE(adapters);
202         }
203
204         /* all paths are added into path_group
205          * in crafted child order
206          */
207         return 0;
208 }
209
210 /* round-robin: order paths in path group to alternate
211  * between all host adapters
212  */
213 int rr_optimize_path_order(struct pathgroup *pgp)
214 {
215         vector adapters;
216         struct path *pp;
217         int total_paths;
218         int i;
219
220         total_paths = VECTOR_SIZE(pgp->paths);
221         vector_foreach_slot(pgp->paths, pp, i) {
222                 if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP &&
223                         pp->sg_id.proto_id != SCSI_PROTOCOL_SAS &&
224                         pp->sg_id.proto_id != SCSI_PROTOCOL_ISCSI &&
225                         pp->sg_id.proto_id != SCSI_PROTOCOL_SRP) {
226                         /* return success as default path order
227                          * is maintained in path group
228                          */
229                         return 0;
230                 }
231         }
232         adapters = vector_alloc();
233         if (!adapters)
234                 return 0;
235
236         /* group paths in path group by host adapters
237          */
238         if (group_by_host_adapter(pgp, adapters)) {
239                 /* already freed adapters */
240                 condlog(3, "Failed to group paths by adapters");
241                 return 0;
242         }
243
244         /* re-order paths in pg to alternate between adapters and host ports
245          */
246         if (order_paths_in_pg_by_alt_adapters(pgp, adapters, total_paths)) {
247                 condlog(3, "Failed to re-order paths in pg by adapters "
248                         "and host ports");
249                 free_adaptergroup(adapters);
250                 /* return failure as original paths are
251                  * removed form pgp
252                  */
253                 return 1;
254         }
255
256         free_adaptergroup(adapters);
257         return 0;
258 }
259
260 int setup_map(struct multipath *mpp, char *params, int params_size,
261               struct vectors *vecs)
262 {
263         struct pathgroup * pgp;
264         struct config *conf;
265         int i;
266
267         /*
268          * don't bother if devmap size is unknown
269          */
270         if (mpp->size <= 0) {
271                 condlog(3, "%s: devmap size is unknown", mpp->alias);
272                 return 1;
273         }
274
275         /*
276          * free features, selector, and hwhandler properties if they are being reused
277          */
278         free_multipath_attributes(mpp);
279         if (mpp->disable_queueing && VECTOR_SIZE(mpp->paths) != 0)
280                 mpp->disable_queueing = 0;
281
282         /*
283          * properties selectors
284          *
285          * Ordering matters for some properties:
286          * - features after no_path_retry and retain_hwhandler
287          * - hwhandler after retain_hwhandler
288          * No guarantee that this list is complete, check code in
289          * propsel.c if in doubt.
290          */
291         conf = get_multipath_config();
292         pthread_cleanup_push(put_multipath_config, conf);
293
294         select_pgfailback(conf, mpp);
295         select_pgpolicy(conf, mpp);
296         select_selector(conf, mpp);
297         select_no_path_retry(conf, mpp);
298         select_retain_hwhandler(conf, mpp);
299         select_features(conf, mpp);
300         select_hwhandler(conf, mpp);
301         select_rr_weight(conf, mpp);
302         select_minio(conf, mpp);
303         select_mode(conf, mpp);
304         select_uid(conf, mpp);
305         select_gid(conf, mpp);
306         select_fast_io_fail(conf, mpp);
307         select_dev_loss(conf, mpp);
308         select_reservation_key(conf, mpp);
309         select_deferred_remove(conf, mpp);
310         select_delay_watch_checks(conf, mpp);
311         select_delay_wait_checks(conf, mpp);
312         select_marginal_path_err_sample_time(conf, mpp);
313         select_marginal_path_err_rate_threshold(conf, mpp);
314         select_marginal_path_err_recheck_gap_time(conf, mpp);
315         select_marginal_path_double_failed_time(conf, mpp);
316         select_skip_kpartx(conf, mpp);
317         select_max_sectors_kb(conf, mpp);
318         select_ghost_delay(conf, mpp);
319         select_flush_on_last_del(conf, mpp);
320
321         sysfs_set_scsi_tmo(mpp, conf->checkint);
322         pthread_cleanup_pop(1);
323
324         if (mpp->marginal_path_double_failed_time > 0 &&
325             mpp->marginal_path_err_sample_time > 0 &&
326             mpp->marginal_path_err_recheck_gap_time > 0 &&
327             mpp->marginal_path_err_rate_threshold >= 0)
328                 start_io_err_stat_thread(vecs);
329         /*
330          * assign paths to path groups -- start with no groups and all paths
331          * in mpp->paths
332          */
333         if (mpp->pg) {
334                 vector_foreach_slot (mpp->pg, pgp, i)
335                         free_pathgroup(pgp, KEEP_PATHS);
336
337                 vector_free(mpp->pg);
338                 mpp->pg = NULL;
339         }
340         if (mpp->pgpolicyfn && mpp->pgpolicyfn(mpp))
341                 return 1;
342
343         mpp->nr_active = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
344
345         /*
346          * ponders each path group and determine highest prio pg
347          * to switch over (default to first)
348          */
349         mpp->bestpg = select_path_group(mpp);
350
351         /* re-order paths in all path groups in an optimized way
352          * for round-robin path selectors to get maximum throughput.
353          */
354         if (!strncmp(mpp->selector, "round-robin", 11)) {
355                 vector_foreach_slot(mpp->pg, pgp, i) {
356                         if (VECTOR_SIZE(pgp->paths) <= 2)
357                                 continue;
358                         if (rr_optimize_path_order(pgp)) {
359                                 condlog(2, "cannot re-order paths for "
360                                         "optimization: %s",
361                                         mpp->alias);
362                                 return 1;
363                         }
364                 }
365         }
366
367         /*
368          * transform the mp->pg vector of vectors of paths
369          * into a mp->params strings to feed the device-mapper
370          */
371         if (assemble_map(mpp, params, params_size)) {
372                 condlog(0, "%s: problem assembing map", mpp->alias);
373                 return 1;
374         }
375         return 0;
376 }
377
378 static void
379 compute_pgid(struct pathgroup * pgp)
380 {
381         struct path * pp;
382         int i;
383
384         vector_foreach_slot (pgp->paths, pp, i)
385                 pgp->id ^= (long)pp;
386 }
387
388 static int
389 pgcmp (struct multipath * mpp, struct multipath * cmpp)
390 {
391         int i, j;
392         struct pathgroup * pgp;
393         struct pathgroup * cpgp;
394         int r = 0;
395
396         if (!mpp)
397                 return 0;
398
399         vector_foreach_slot (mpp->pg, pgp, i) {
400                 compute_pgid(pgp);
401
402                 vector_foreach_slot (cmpp->pg, cpgp, j) {
403                         if (pgp->id == cpgp->id &&
404                             !pathcmp(pgp, cpgp)) {
405                                 r = 0;
406                                 break;
407                         }
408                         r++;
409                 }
410                 if (r)
411                         return r;
412         }
413         return r;
414 }
415
416 static struct udev_device *
417 get_udev_for_mpp(const struct multipath *mpp)
418 {
419         dev_t devnum;
420         struct udev_device *udd;
421
422         if (!mpp || !mpp->dmi) {
423                 condlog(1, "%s called with empty mpp", __func__);
424                 return NULL;
425         }
426
427         devnum = makedev(mpp->dmi->major, mpp->dmi->minor);
428         udd = udev_device_new_from_devnum(udev, 'b', devnum);
429         if (!udd) {
430                 condlog(1, "failed to get udev device for %s", mpp->alias);
431                 return NULL;
432         }
433         return udd;
434 }
435
436 static void
437 trigger_udev_change(const struct multipath *mpp)
438 {
439         static const char change[] = "change";
440         struct udev_device *udd = get_udev_for_mpp(mpp);
441         if (!udd)
442                 return;
443         condlog(3, "triggering %s uevent for %s", change, mpp->alias);
444         sysfs_attr_set_value(udd, "uevent", change, sizeof(change)-1);
445         udev_device_unref(udd);
446 }
447
448 void
449 trigger_paths_udev_change(struct multipath *mpp, bool is_mpath)
450 {
451         struct pathgroup *pgp;
452         struct path *pp;
453         int i, j;
454         /*
455          * If a path changes from multipath to non-multipath, we must
456          * synthesize an artificial "add" event, otherwise the LVM2 rules
457          * (69-lvm2-lvmetad.rules) won't pick it up. Otherwise, we'd just
458          * irritate ourselves with an "add", so use "change".
459          */
460         const char *action = is_mpath ? "change" : "add";
461
462         if (!mpp || !mpp->pg)
463                 return;
464
465         vector_foreach_slot (mpp->pg, pgp, i) {
466                 if (!pgp->paths)
467                         continue;
468                 vector_foreach_slot(pgp->paths, pp, j) {
469                         const char *env;
470
471                         if (!pp->udev)
472                                 continue;
473                         /*
474                          * Paths that are already classified as multipath
475                          * members don't need another uevent.
476                          */
477                         env = udev_device_get_property_value(
478                                 pp->udev, "DM_MULTIPATH_DEVICE_PATH");
479
480                         if (is_mpath && env != NULL && !strcmp(env, "1")) {
481                                 /*
482                                  * If FIND_MULTIPATHS_WAIT_UNTIL is not "0",
483                                  * path is in "maybe" state and timer is running
484                                  * Send uevent now (see multipath.rules).
485                                  */
486                                 env = udev_device_get_property_value(
487                                         pp->udev, "FIND_MULTIPATHS_WAIT_UNTIL");
488                                 if (env == NULL || !strcmp(env, "0"))
489                                         continue;
490                         } else if (!is_mpath &&
491                                    (env == NULL || !strcmp(env, "0")))
492                                 continue;
493
494                         condlog(3, "triggering %s uevent for %s (is %smultipath member)",
495                                 action, pp->dev, is_mpath ? "" : "no ");
496                         sysfs_attr_set_value(pp->udev, "uevent",
497                                              action, strlen(action));
498                 }
499         }
500
501         mpp->needs_paths_uevent = 0;
502 }
503
504 static int
505 is_mpp_known_to_udev(const struct multipath *mpp)
506 {
507         struct udev_device *udd = get_udev_for_mpp(mpp);
508         int ret = (udd != NULL);
509         udev_device_unref(udd);
510         return ret;
511 }
512
513 static int
514 sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload)
515 {
516         struct pathgroup * pgp;
517         struct path *pp;
518         char buff[11];
519         int i, j, ret, err = 0;
520         struct udev_device *udd;
521         int max_sectors_kb;
522
523         if (mpp->max_sectors_kb == MAX_SECTORS_KB_UNDEF)
524                 return 0;
525         max_sectors_kb = mpp->max_sectors_kb;
526         if (is_reload) {
527                 if (!mpp->dmi && dm_get_info(mpp->alias, &mpp->dmi) != 0) {
528                         condlog(1, "failed to get dm info for %s", mpp->alias);
529                         return 1;
530                 }
531                 udd = get_udev_for_mpp(mpp);
532                 if (!udd) {
533                         condlog(1, "failed to get udev device to set max_sectors_kb for %s", mpp->alias);
534                         return 1;
535                 }
536                 ret = sysfs_attr_get_value(udd, "queue/max_sectors_kb", buff,
537                                            sizeof(buff));
538                 udev_device_unref(udd);
539                 if (ret <= 0) {
540                         condlog(1, "failed to get current max_sectors_kb from %s", mpp->alias);
541                         return 1;
542                 }
543                 if (sscanf(buff, "%u\n", &max_sectors_kb) != 1) {
544                         condlog(1, "can't parse current max_sectors_kb from %s",
545                                 mpp->alias);
546                         return 1;
547                 }
548         }
549         snprintf(buff, 11, "%d", max_sectors_kb);
550
551         vector_foreach_slot (mpp->pg, pgp, i) {
552                 vector_foreach_slot(pgp->paths, pp, j) {
553                         ret = sysfs_attr_set_value(pp->udev,
554                                                    "queue/max_sectors_kb",
555                                                    buff, strlen(buff));
556                         if (ret < 0) {
557                                 condlog(1, "failed setting max_sectors_kb on %s : %s", pp->dev, strerror(-ret));
558                                 err = 1;
559                         }
560                 }
561         }
562         return err;
563 }
564
565 static void
566 select_action (struct multipath * mpp, vector curmp, int force_reload)
567 {
568         struct multipath * cmpp;
569         struct multipath * cmpp_by_name;
570         char * mpp_feat, * cmpp_feat;
571
572         cmpp = find_mp_by_wwid(curmp, mpp->wwid);
573         cmpp_by_name = find_mp_by_alias(curmp, mpp->alias);
574
575         if (!cmpp_by_name) {
576                 if (cmpp) {
577                         condlog(2, "%s: rename %s to %s", mpp->wwid,
578                                 cmpp->alias, mpp->alias);
579                         strncpy(mpp->alias_old, cmpp->alias, WWID_SIZE - 1);
580                         mpp->action = ACT_RENAME;
581                         if (force_reload) {
582                                 mpp->force_udev_reload = 1;
583                                 mpp->action = ACT_FORCERENAME;
584                         }
585                         return;
586                 }
587                 mpp->action = ACT_CREATE;
588                 condlog(3, "%s: set ACT_CREATE (map does not exist)",
589                         mpp->alias);
590                 return;
591         }
592
593         if (!cmpp) {
594                 condlog(2, "%s: remove (wwid changed)", mpp->alias);
595                 dm_flush_map(mpp->alias);
596                 strncpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE - 1);
597                 drop_multipath(curmp, cmpp_by_name->wwid, KEEP_PATHS);
598                 mpp->action = ACT_CREATE;
599                 condlog(3, "%s: set ACT_CREATE (map wwid change)",
600                         mpp->alias);
601                 return;
602         }
603
604         if (cmpp != cmpp_by_name) {
605                 condlog(2, "%s: unable to rename %s to %s (%s is used by %s)",
606                         mpp->wwid, cmpp->alias, mpp->alias,
607                         mpp->alias, cmpp_by_name->wwid);
608                 /* reset alias to existing alias */
609                 FREE(mpp->alias);
610                 mpp->alias = STRDUP(cmpp->alias);
611                 mpp->action = ACT_IMPOSSIBLE;
612                 return;
613         }
614
615         if (pathcount(mpp, PATH_UP) == 0) {
616                 mpp->action = ACT_IMPOSSIBLE;
617                 condlog(3, "%s: set ACT_IMPOSSIBLE (no usable path)",
618                         mpp->alias);
619                 return;
620         }
621         if (force_reload) {
622                 mpp->force_udev_reload = 1;
623                 mpp->action = ACT_RELOAD;
624                 condlog(3, "%s: set ACT_RELOAD (forced by user)",
625                         mpp->alias);
626                 return;
627         }
628         if (cmpp->size != mpp->size) {
629                 mpp->force_udev_reload = 1;
630                 mpp->action = ACT_RESIZE;
631                 condlog(3, "%s: set ACT_RESIZE (size change)",
632                         mpp->alias);
633                 return;
634         }
635
636         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF &&
637             !!strstr(mpp->features, "queue_if_no_path") !=
638             !!strstr(cmpp->features, "queue_if_no_path")) {
639                 mpp->action =  ACT_RELOAD;
640                 condlog(3, "%s: set ACT_RELOAD (no_path_retry change)",
641                         mpp->alias);
642                 return;
643         }
644         if ((mpp->retain_hwhandler != RETAIN_HWHANDLER_ON ||
645              strcmp(cmpp->hwhandler, "0") == 0) &&
646             (strlen(cmpp->hwhandler) != strlen(mpp->hwhandler) ||
647              strncmp(cmpp->hwhandler, mpp->hwhandler,
648                     strlen(mpp->hwhandler)))) {
649                 mpp->action = ACT_RELOAD;
650                 condlog(3, "%s: set ACT_RELOAD (hwhandler change)",
651                         mpp->alias);
652                 return;
653         }
654
655         if (mpp->retain_hwhandler != RETAIN_HWHANDLER_UNDEF &&
656             !!strstr(mpp->features, "retain_attached_hw_handler") !=
657             !!strstr(cmpp->features, "retain_attached_hw_handler") &&
658             get_linux_version_code() < KERNEL_VERSION(4, 3, 0)) {
659                 mpp->action = ACT_RELOAD;
660                 condlog(3, "%s: set ACT_RELOAD (retain_hwhandler change)",
661                         mpp->alias);
662                 return;
663         }
664
665         cmpp_feat = STRDUP(cmpp->features);
666         mpp_feat = STRDUP(mpp->features);
667         if (cmpp_feat && mpp_feat) {
668                 remove_feature(&mpp_feat, "queue_if_no_path");
669                 remove_feature(&mpp_feat, "retain_attached_hw_handler");
670                 remove_feature(&cmpp_feat, "queue_if_no_path");
671                 remove_feature(&cmpp_feat, "retain_attached_hw_handler");
672                 if (strncmp(mpp_feat, cmpp_feat, PARAMS_SIZE)) {
673                         mpp->action =  ACT_RELOAD;
674                         condlog(3, "%s: set ACT_RELOAD (features change)",
675                                 mpp->alias);
676                 }
677         }
678         FREE(cmpp_feat);
679         FREE(mpp_feat);
680
681         if (!cmpp->selector || strncmp(cmpp->selector, mpp->selector,
682                     strlen(mpp->selector))) {
683                 mpp->action = ACT_RELOAD;
684                 condlog(3, "%s: set ACT_RELOAD (selector change)",
685                         mpp->alias);
686                 return;
687         }
688         if (cmpp->minio != mpp->minio) {
689                 mpp->action = ACT_RELOAD;
690                 condlog(3, "%s: set ACT_RELOAD (minio change, %u->%u)",
691                         mpp->alias, cmpp->minio, mpp->minio);
692                 return;
693         }
694         if (!cmpp->pg || VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
695                 mpp->action = ACT_RELOAD;
696                 condlog(3, "%s: set ACT_RELOAD (path group number change)",
697                         mpp->alias);
698                 return;
699         }
700         if (pgcmp(mpp, cmpp)) {
701                 mpp->action = ACT_RELOAD;
702                 condlog(3, "%s: set ACT_RELOAD (path group topology change)",
703                         mpp->alias);
704                 return;
705         }
706         if (cmpp->nextpg != mpp->bestpg) {
707                 mpp->action = ACT_SWITCHPG;
708                 condlog(3, "%s: set ACT_SWITCHPG (next path group change)",
709                         mpp->alias);
710                 return;
711         }
712         if (!is_mpp_known_to_udev(cmpp)) {
713                 mpp->action = ACT_RELOAD;
714                 condlog(3, "%s: set ACT_RELOAD (udev device not initialized)",
715                         mpp->alias);
716                 return;
717         }
718         mpp->action = ACT_NOTHING;
719         condlog(3, "%s: set ACT_NOTHING (map unchanged)",
720                 mpp->alias);
721         return;
722 }
723
724 int reinstate_paths(struct multipath *mpp)
725 {
726         int i, j;
727         struct pathgroup * pgp;
728         struct path * pp;
729
730         if (!mpp->pg)
731                 return 0;
732
733         vector_foreach_slot (mpp->pg, pgp, i) {
734                 if (!pgp->paths)
735                         continue;
736
737                 vector_foreach_slot (pgp->paths, pp, j) {
738                         if (pp->state != PATH_UP &&
739                             (pgp->status == PGSTATE_DISABLED ||
740                              pgp->status == PGSTATE_ACTIVE))
741                                 continue;
742
743                         if (pp->dmstate == PSTATE_FAILED) {
744                                 if (dm_reinstate_path(mpp->alias, pp->dev_t))
745                                         condlog(0, "%s: error reinstating",
746                                                 pp->dev);
747                         }
748                 }
749         }
750         return 0;
751 }
752
753 static int
754 lock_multipath (struct multipath * mpp, int lock)
755 {
756         struct pathgroup * pgp;
757         struct path * pp;
758         int i, j;
759         int x, y;
760
761         if (!mpp || !mpp->pg)
762                 return 0;
763
764         vector_foreach_slot (mpp->pg, pgp, i) {
765                 if (!pgp->paths)
766                         continue;
767                 vector_foreach_slot(pgp->paths, pp, j) {
768                         if (lock && flock(pp->fd, LOCK_SH | LOCK_NB) &&
769                             errno == EWOULDBLOCK)
770                                 goto fail;
771                         else if (!lock)
772                                 flock(pp->fd, LOCK_UN);
773                 }
774         }
775         return 0;
776 fail:
777         vector_foreach_slot (mpp->pg, pgp, x) {
778                 if (x > i)
779                         return 1;
780                 if (!pgp->paths)
781                         continue;
782                 vector_foreach_slot(pgp->paths, pp, y) {
783                         if (x == i && y >= j)
784                                 return 1;
785                         flock(pp->fd, LOCK_UN);
786                 }
787         }
788         return 1;
789 }
790
791 /*
792  * Return value:
793  */
794 #define DOMAP_RETRY     -1
795 #define DOMAP_FAIL      0
796 #define DOMAP_OK        1
797 #define DOMAP_EXIST     2
798 #define DOMAP_DRY       3
799
800 int domap(struct multipath *mpp, char *params, int is_daemon)
801 {
802         int r = DOMAP_FAIL;
803         struct config *conf;
804         int verbosity;
805
806         /*
807          * last chance to quit before touching the devmaps
808          */
809         if (mpp->action == ACT_DRY_RUN) {
810                 conf = get_multipath_config();
811                 verbosity = conf->verbosity;
812                 put_multipath_config(conf);
813                 print_multipath_topology(mpp, verbosity);
814                 return DOMAP_DRY;
815         }
816
817         if (mpp->action == ACT_CREATE &&
818             dm_map_present(mpp->alias)) {
819                 condlog(3, "%s: map already present", mpp->alias);
820                 mpp->action = ACT_RELOAD;
821         }
822
823         switch (mpp->action) {
824         case ACT_REJECT:
825         case ACT_NOTHING:
826         case ACT_IMPOSSIBLE:
827                 return DOMAP_EXIST;
828
829         case ACT_SWITCHPG:
830                 dm_switchgroup(mpp->alias, mpp->bestpg);
831                 /*
832                  * we may have avoided reinstating paths because there where in
833                  * active or disabled PG. Now that the topology has changed,
834                  * retry.
835                  */
836                 reinstate_paths(mpp);
837                 return DOMAP_EXIST;
838
839         case ACT_CREATE:
840                 if (lock_multipath(mpp, 1)) {
841                         condlog(3, "%s: failed to create map (in use)",
842                                 mpp->alias);
843                         return DOMAP_RETRY;
844                 }
845
846                 sysfs_set_max_sectors_kb(mpp, 0);
847                 if (is_daemon && mpp->ghost_delay > 0 && mpp->nr_active &&
848                     pathcount(mpp, PATH_GHOST) == mpp->nr_active)
849                         mpp->ghost_delay_tick = mpp->ghost_delay;
850                 r = dm_addmap_create(mpp, params);
851
852                 lock_multipath(mpp, 0);
853                 break;
854
855         case ACT_RELOAD:
856                 sysfs_set_max_sectors_kb(mpp, 1);
857                 if (mpp->ghost_delay_tick > 0 && pathcount(mpp, PATH_UP))
858                         mpp->ghost_delay_tick = 0;
859                 r = dm_addmap_reload(mpp, params, 0);
860                 break;
861
862         case ACT_RESIZE:
863                 sysfs_set_max_sectors_kb(mpp, 1);
864                 if (mpp->ghost_delay_tick > 0 && pathcount(mpp, PATH_UP))
865                         mpp->ghost_delay_tick = 0;
866                 r = dm_addmap_reload(mpp, params, 1);
867                 break;
868
869         case ACT_RENAME:
870                 conf = get_multipath_config();
871                 pthread_cleanup_push(put_multipath_config, conf);
872                 r = dm_rename(mpp->alias_old, mpp->alias,
873                               conf->partition_delim, mpp->skip_kpartx);
874                 pthread_cleanup_pop(1);
875                 break;
876
877         case ACT_FORCERENAME:
878                 conf = get_multipath_config();
879                 pthread_cleanup_push(put_multipath_config, conf);
880                 r = dm_rename(mpp->alias_old, mpp->alias,
881                               conf->partition_delim, mpp->skip_kpartx);
882                 pthread_cleanup_pop(1);
883                 if (r) {
884                         sysfs_set_max_sectors_kb(mpp, 1);
885                         if (mpp->ghost_delay_tick > 0 &&
886                             pathcount(mpp, PATH_UP))
887                                 mpp->ghost_delay_tick = 0;
888                         r = dm_addmap_reload(mpp, params, 0);
889                 }
890                 break;
891
892         default:
893                 break;
894         }
895
896         if (r == DOMAP_OK) {
897                 /*
898                  * DM_DEVICE_CREATE, DM_DEVICE_RENAME, or DM_DEVICE_RELOAD
899                  * succeeded
900                  */
901                 mpp->force_udev_reload = 0;
902                 if (mpp->action == ACT_CREATE &&
903                     (remember_wwid(mpp->wwid) == 1 ||
904                      mpp->needs_paths_uevent))
905                         trigger_paths_udev_change(mpp, true);
906                 if (!is_daemon) {
907                         /* multipath client mode */
908                         dm_switchgroup(mpp->alias, mpp->bestpg);
909                 } else  {
910                         /* multipath daemon mode */
911                         mpp->stat_map_loads++;
912                         condlog(2, "%s: load table [0 %llu %s %s]", mpp->alias,
913                                 mpp->size, TGT_MPATH, params);
914                         /*
915                          * Required action is over, reset for the stateful daemon.
916                          * But don't do it for creation as we use in the caller the
917                          * mpp->action to figure out whether to start the watievent checker.
918                          */
919                         if (mpp->action != ACT_CREATE)
920                                 mpp->action = ACT_NOTHING;
921                         else {
922                                 conf = get_multipath_config();
923                                 mpp->wait_for_udev = 1;
924                                 mpp->uev_wait_tick = conf->uev_wait_timeout;
925                                 put_multipath_config(conf);
926                         }
927                 }
928                 dm_setgeometry(mpp);
929                 return DOMAP_OK;
930         } else if (r == DOMAP_FAIL && mpp->action == ACT_CREATE &&
931                    mpp->needs_paths_uevent)
932                 trigger_paths_udev_change(mpp, false);
933
934         return DOMAP_FAIL;
935 }
936
937 static int
938 deadmap (struct multipath * mpp)
939 {
940         int i, j;
941         struct pathgroup * pgp;
942         struct path * pp;
943
944         if (!mpp->pg)
945                 return 1;
946
947         vector_foreach_slot (mpp->pg, pgp, i) {
948                 if (!pgp->paths)
949                         continue;
950
951                 vector_foreach_slot (pgp->paths, pp, j)
952                         if (strlen(pp->dev))
953                                 return 0; /* alive */
954         }
955
956         return 1; /* dead */
957 }
958
959 int check_daemon(void)
960 {
961         int fd;
962         char *reply;
963         int ret = 0;
964         unsigned int timeout;
965         struct config *conf;
966
967         fd = mpath_connect();
968         if (fd == -1)
969                 return 0;
970
971         if (send_packet(fd, "show daemon") != 0)
972                 goto out;
973         conf = get_multipath_config();
974         timeout = conf->uxsock_timeout;
975         put_multipath_config(conf);
976         if (recv_packet(fd, &reply, timeout) != 0)
977                 goto out;
978
979         if (strstr(reply, "shutdown"))
980                 goto out_free;
981
982         ret = 1;
983
984 out_free:
985         FREE(reply);
986 out:
987         mpath_disconnect(fd);
988         return ret;
989 }
990
991 /*
992  * The force_reload parameter determines how coalesce_paths treats existing maps.
993  * FORCE_RELOAD_NONE: existing maps aren't touched at all
994  * FORCE_RELOAD_YES: all maps are rebuilt from scratch and (re)loaded in DM
995  * FORCE_RELOAD_WEAK: existing maps are compared to the current conf and only
996  * reloaded in DM if there's a difference. This is useful during startup.
997  */
998 int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
999                     int force_reload, enum mpath_cmds cmd)
1000 {
1001         int r = 1;
1002         int k, i;
1003         int is_daemon = (cmd == CMD_NONE) ? 1 : 0;
1004         char params[PARAMS_SIZE];
1005         struct multipath * mpp;
1006         struct path * pp1;
1007         struct path * pp2;
1008         vector curmp = vecs->mpvec;
1009         vector pathvec = vecs->pathvec;
1010         struct config *conf;
1011         int allow_queueing;
1012         uint64_t *size_mismatch_seen;
1013
1014         /* ignore refwwid if it's empty */
1015         if (refwwid && !strlen(refwwid))
1016                 refwwid = NULL;
1017
1018         if (force_reload != FORCE_RELOAD_NONE) {
1019                 vector_foreach_slot (pathvec, pp1, k) {
1020                         pp1->mpp = NULL;
1021                 }
1022         }
1023
1024         if (VECTOR_SIZE(pathvec) == 0)
1025                 return 0;
1026         size_mismatch_seen = calloc((VECTOR_SIZE(pathvec) - 1) / 64 + 1,
1027                                     sizeof(uint64_t));
1028         if (size_mismatch_seen == NULL)
1029                 return 1;
1030
1031         vector_foreach_slot (pathvec, pp1, k) {
1032                 int invalid;
1033                 /* skip this path for some reason */
1034
1035                 /* 1. if path has no unique id or wwid blacklisted */
1036                 if (strlen(pp1->wwid) == 0) {
1037                         orphan_path(pp1, "no WWID");
1038                         continue;
1039                 }
1040
1041                 conf = get_multipath_config();
1042                 pthread_cleanup_push(put_multipath_config, conf);
1043                 invalid = (filter_path(conf, pp1) > 0);
1044                 pthread_cleanup_pop(1);
1045                 if (invalid) {
1046                         orphan_path(pp1, "blacklisted");
1047                         continue;
1048                 }
1049
1050                 /* 2. if path already coalesced, or seen and discarded */
1051                 if (pp1->mpp || is_bit_set_in_array(k, size_mismatch_seen))
1052                         continue;
1053
1054                 /* 3. if path has disappeared */
1055                 if (pp1->state == PATH_REMOVED) {
1056                         orphan_path(pp1, "path removed");
1057                         continue;
1058                 }
1059
1060                 /* 4. path is out of scope */
1061                 if (refwwid && strncmp(pp1->wwid, refwwid, WWID_SIZE - 1))
1062                         continue;
1063
1064                 /* If find_multipaths was selected check if the path is valid */
1065                 if (!refwwid && !should_multipath(pp1, pathvec, curmp)) {
1066                         orphan_path(pp1, "only one path");
1067                         continue;
1068                 }
1069
1070                 /*
1071                  * at this point, we know we really got a new mp
1072                  */
1073                 mpp = add_map_with_path(vecs, pp1, 0);
1074                 if (!mpp) {
1075                         orphan_path(pp1, "failed to create multipath device");
1076                         continue;
1077                 }
1078
1079                 if (!mpp->paths) {
1080                         condlog(0, "%s: skip coalesce (no paths)", mpp->alias);
1081                         remove_map(mpp, vecs, 0);
1082                         continue;
1083                 }
1084
1085                 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
1086                         pp2 = VECTOR_SLOT(pathvec, i);
1087
1088                         if (strcmp(pp1->wwid, pp2->wwid))
1089                                 continue;
1090
1091                         if (!mpp->size && pp2->size)
1092                                 mpp->size = pp2->size;
1093
1094                         if (mpp->size && pp2->size &&
1095                             pp2->size != mpp->size) {
1096                                 /*
1097                                  * ouch, avoid feeding that to the DM
1098                                  */
1099                                 condlog(0, "%s: size %llu, expected %llu. "
1100                                         "Discard", pp2->dev, pp2->size,
1101                                         mpp->size);
1102                                 mpp->action = ACT_REJECT;
1103                                 set_bit_in_array(i, size_mismatch_seen);
1104                         }
1105                 }
1106                 verify_paths(mpp, vecs);
1107
1108                 params[0] = '\0';
1109                 if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1110                         remove_map(mpp, vecs, 0);
1111                         continue;
1112                 }
1113
1114                 if (cmd == CMD_DRY_RUN)
1115                         mpp->action = ACT_DRY_RUN;
1116                 if (mpp->action == ACT_UNDEF)
1117                         select_action(mpp, curmp,
1118                                       force_reload == FORCE_RELOAD_YES ? 1 : 0);
1119
1120                 r = domap(mpp, params, is_daemon);
1121
1122                 if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
1123                         condlog(3, "%s: domap (%u) failure "
1124                                    "for create/reload map",
1125                                 mpp->alias, r);
1126                         if (r == DOMAP_FAIL || is_daemon) {
1127                                 condlog(2, "%s: %s map",
1128                                         mpp->alias, (mpp->action == ACT_CREATE)?
1129                                         "ignoring" : "removing");
1130                                 remove_map(mpp, vecs, 0);
1131                                 continue;
1132                         } else /* if (r == DOMAP_RETRY && !is_daemon) */ {
1133                                 goto out;
1134                         }
1135                 }
1136                 if (r == DOMAP_DRY)
1137                         continue;
1138
1139                 if (r == DOMAP_EXIST && mpp->action == ACT_NOTHING &&
1140                     force_reload == FORCE_RELOAD_WEAK)
1141                         /*
1142                          * First time we're called, and no changes applied.
1143                          * domap() was a noop. But we can't be sure that
1144                          * udev has already finished setting up this device
1145                          * (udev in initrd may have been shut down while
1146                          * processing this device or its children).
1147                          * Trigger a change event, just in case.
1148                          */
1149                         trigger_udev_change(find_mp_by_wwid(curmp, mpp->wwid));
1150
1151                 conf = get_multipath_config();
1152                 allow_queueing = conf->allow_queueing;
1153                 put_multipath_config(conf);
1154                 if (!is_daemon && !allow_queueing && !check_daemon()) {
1155                         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF &&
1156                             mpp->no_path_retry != NO_PATH_RETRY_FAIL)
1157                                 condlog(3, "%s: multipathd not running, unset "
1158                                         "queue_if_no_path feature", mpp->alias);
1159                         if (!dm_queue_if_no_path(mpp->alias, 0))
1160                                 remove_feature(&mpp->features,
1161                                                "queue_if_no_path");
1162                 }
1163
1164                 if (!is_daemon && mpp->action != ACT_NOTHING) {
1165                         int verbosity;
1166
1167                         conf = get_multipath_config();
1168                         verbosity = conf->verbosity;
1169                         put_multipath_config(conf);
1170                         print_multipath_topology(mpp, verbosity);
1171                 }
1172
1173                 if (newmp) {
1174                         if (mpp->action != ACT_REJECT) {
1175                                 if (!vector_alloc_slot(newmp)) {
1176                                         r = 1;
1177                                         goto out;
1178                                 }
1179                                 vector_set_slot(newmp, mpp);
1180                         }
1181                         else
1182                                 remove_map(mpp, vecs, 0);
1183                 }
1184         }
1185         /*
1186          * Flush maps with only dead paths (ie not in sysfs)
1187          * Keep maps with only failed paths
1188          */
1189         if (newmp) {
1190                 vector_foreach_slot (newmp, mpp, i) {
1191                         char alias[WWID_SIZE];
1192
1193                         if (!deadmap(mpp))
1194                                 continue;
1195
1196                         strncpy(alias, mpp->alias, WWID_SIZE - 1);
1197
1198                         vector_del_slot(newmp, i);
1199                         i--;
1200                         remove_map(mpp, vecs, 0);
1201
1202                         if (dm_flush_map(alias))
1203                                 condlog(2, "%s: remove failed (dead)",
1204                                         alias);
1205                         else
1206                                 condlog(2, "%s: remove (dead)", alias);
1207                 }
1208         }
1209         r = 0;
1210 out:
1211         free(size_mismatch_seen);
1212         return r;
1213 }
1214
1215 struct udev_device *get_udev_device(const char *dev, enum devtypes dev_type)
1216 {
1217         struct udev_device *ud = NULL;
1218         const char *base;
1219
1220         if (dev == NULL || *dev == '\0')
1221                 return NULL;
1222
1223         switch (dev_type) {
1224         case DEV_DEVNODE:
1225         case DEV_DEVMAP:
1226                 /* This should be GNU basename, compiler will warn if not */
1227                 base = basename(dev);
1228                 if (*base == '\0')
1229                         break;
1230                 ud = udev_device_new_from_subsystem_sysname(udev, "block",
1231                                                             base);
1232                 break;
1233         case DEV_DEVT:
1234                 ud = udev_device_new_from_devnum(udev, 'b', parse_devt(dev));
1235                 break;
1236         case DEV_UEVENT:
1237                 ud = udev_device_new_from_environment(udev);
1238                 break;
1239         default:
1240                 condlog(0, "Internal error: get_udev_device called with invalid type %d\n",
1241                         dev_type);
1242                 break;
1243         }
1244         if (ud == NULL)
1245                 condlog(2, "get_udev_device: failed to look up %s with type %d",
1246                         dev, dev_type);
1247         return ud;
1248 }
1249
1250 /*
1251  * returns:
1252  * 0 - success
1253  * 1 - failure
1254  * 2 - blacklist
1255  */
1256 int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
1257                 vector pathvec, char **wwid)
1258 {
1259         int ret = 1;
1260         struct path * pp;
1261         char buff[FILE_NAME_SIZE];
1262         char * refwwid = NULL, tmpwwid[WWID_SIZE];
1263         int flags = DI_SYSFS | DI_WWID;
1264         struct config *conf;
1265         int invalid = 0;
1266
1267         if (!wwid)
1268                 return 1;
1269         *wwid = NULL;
1270
1271         if (dev_type == DEV_NONE)
1272                 return 1;
1273
1274         if (cmd != CMD_REMOVE_WWID)
1275                 flags |= DI_BLACKLIST;
1276
1277         if (dev_type == DEV_DEVNODE) {
1278                 if (basenamecpy(dev, buff, FILE_NAME_SIZE) == 0) {
1279                         condlog(1, "basename failed for '%s' (%s)",
1280                                 dev, buff);
1281                         return 1;
1282                 }
1283
1284                 pp = find_path_by_dev(pathvec, buff);
1285                 if (!pp) {
1286                         struct udev_device *udevice =
1287                                 get_udev_device(buff, dev_type);
1288
1289                         if (!udevice)
1290                                 return 1;
1291
1292                         conf = get_multipath_config();
1293                         pthread_cleanup_push(put_multipath_config, conf);
1294                         ret = store_pathinfo(pathvec, conf, udevice,
1295                                              flags, &pp);
1296                         pthread_cleanup_pop(1);
1297                         udev_device_unref(udevice);
1298                         if (!pp) {
1299                                 if (ret == 1)
1300                                         condlog(0, "%s: can't store path info",
1301                                                 dev);
1302                                 return ret;
1303                         }
1304                 }
1305                 conf = get_multipath_config();
1306                 pthread_cleanup_push(put_multipath_config, conf);
1307                 if (pp->udev && pp->uid_attribute &&
1308                     filter_property(conf, pp->udev, 3) > 0)
1309                         invalid = 1;
1310                 pthread_cleanup_pop(1);
1311                 if (invalid)
1312                         return 2;
1313
1314                 refwwid = pp->wwid;
1315                 goto out;
1316         }
1317
1318         if (dev_type == DEV_DEVT) {
1319                 strchop(dev);
1320                 if (devt2devname(buff, FILE_NAME_SIZE, dev)) {
1321                         condlog(0, "%s: cannot find block device\n", dev);
1322                         return 1;
1323                 }
1324                 pp = find_path_by_dev(pathvec, buff);
1325                 if (!pp) {
1326                         struct udev_device *udevice =
1327                                 get_udev_device(dev, dev_type);
1328
1329                         if (!udevice)
1330                                 return 1;
1331
1332                         conf = get_multipath_config();
1333                         pthread_cleanup_push(put_multipath_config, conf);
1334                         ret = store_pathinfo(pathvec, conf, udevice,
1335                                              flags, &pp);
1336                         pthread_cleanup_pop(1);
1337                         udev_device_unref(udevice);
1338                         if (!pp) {
1339                                 if (ret == 1)
1340                                         condlog(0, "%s can't store path info",
1341                                                 buff);
1342                                 return ret;
1343                         }
1344                 }
1345                 conf = get_multipath_config();
1346                 pthread_cleanup_push(put_multipath_config, conf);
1347                 if (pp->udev && pp->uid_attribute &&
1348                     filter_property(conf, pp->udev, 3) > 0)
1349                         invalid = 1;
1350                 pthread_cleanup_pop(1);
1351                 if (invalid)
1352                         return 2;
1353                 refwwid = pp->wwid;
1354                 goto out;
1355         }
1356
1357         if (dev_type == DEV_UEVENT) {
1358                 struct udev_device *udevice = get_udev_device(dev, dev_type);
1359
1360                 if (!udevice)
1361                         return 1;
1362
1363                 conf = get_multipath_config();
1364                 pthread_cleanup_push(put_multipath_config, conf);
1365                 ret = store_pathinfo(pathvec, conf, udevice,
1366                                      flags, &pp);
1367                 pthread_cleanup_pop(1);
1368                 udev_device_unref(udevice);
1369                 if (!pp) {
1370                         if (ret == 1)
1371                                 condlog(0, "%s: can't store path info", dev);
1372                         return ret;
1373                 }
1374                 conf = get_multipath_config();
1375                 pthread_cleanup_push(put_multipath_config, conf);
1376                 if (pp->udev && pp->uid_attribute &&
1377                     filter_property(conf, pp->udev, 3) > 0)
1378                         invalid = 1;
1379                 pthread_cleanup_pop(1);
1380                 if (invalid)
1381                         return 2;
1382                 refwwid = pp->wwid;
1383                 goto out;
1384         }
1385
1386         if (dev_type == DEV_DEVMAP) {
1387
1388                 conf = get_multipath_config();
1389                 pthread_cleanup_push(put_multipath_config, conf);
1390                 if (((dm_get_uuid(dev, tmpwwid)) == 0) && (strlen(tmpwwid))) {
1391                         refwwid = tmpwwid;
1392                         goto check;
1393                 }
1394
1395                 /*
1396                  * may be a binding
1397                  */
1398                 if (get_user_friendly_wwid(dev, tmpwwid,
1399                                            conf->bindings_file) == 0) {
1400                         refwwid = tmpwwid;
1401                         goto check;
1402                 }
1403
1404                 /*
1405                  * or may be an alias
1406                  */
1407                 refwwid = get_mpe_wwid(conf->mptable, dev);
1408
1409                 /*
1410                  * or directly a wwid
1411                  */
1412                 if (!refwwid)
1413                         refwwid = dev;
1414
1415 check:
1416                 if (refwwid && strlen(refwwid) &&
1417                     filter_wwid(conf->blist_wwid, conf->elist_wwid, refwwid,
1418                                 NULL) > 0)
1419                         invalid = 1;
1420                 pthread_cleanup_pop(1);
1421                 if (invalid)
1422                         return 2;
1423         }
1424 out:
1425         if (refwwid && strlen(refwwid)) {
1426                 *wwid = STRDUP(refwwid);
1427                 return 0;
1428         }
1429
1430         return 1;
1431 }
1432
1433 int reload_map(struct vectors *vecs, struct multipath *mpp, int refresh,
1434                int is_daemon)
1435 {
1436         char params[PARAMS_SIZE] = {0};
1437         struct path *pp;
1438         int i, r;
1439
1440         update_mpp_paths(mpp, vecs->pathvec);
1441         if (refresh) {
1442                 vector_foreach_slot (mpp->paths, pp, i) {
1443                         struct config *conf = get_multipath_config();
1444                         pthread_cleanup_push(put_multipath_config, conf);
1445                         r = pathinfo(pp, conf, DI_PRIO);
1446                         pthread_cleanup_pop(1);
1447                         if (r) {
1448                                 condlog(2, "%s: failed to refresh pathinfo",
1449                                         mpp->alias);
1450                                 return 1;
1451                         }
1452                 }
1453         }
1454         if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1455                 condlog(0, "%s: failed to setup map", mpp->alias);
1456                 return 1;
1457         }
1458         select_action(mpp, vecs->mpvec, 1);
1459
1460         r = domap(mpp, params, is_daemon);
1461         if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
1462                 condlog(3, "%s: domap (%u) failure "
1463                         "for reload map", mpp->alias, r);
1464                 return 1;
1465         }
1466
1467         return 0;
1468 }