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