multipath-tools: Fix more strncpy(X, Y, size - 1) calls
[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 (mpp->pgpolicyfn && mpp->pgpolicyfn(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 void
523 trigger_paths_udev_change(struct multipath *mpp, bool is_mpath)
524 {
525         struct pathgroup *pgp;
526         struct path *pp;
527         int i, j;
528         /*
529          * If a path changes from multipath to non-multipath, we must
530          * synthesize an artificial "add" event, otherwise the LVM2 rules
531          * (69-lvm2-lvmetad.rules) won't pick it up. Otherwise, we'd just
532          * irritate ourselves with an "add", so use "change".
533          */
534         const char *action = is_mpath ? "change" : "add";
535
536         if (!mpp || !mpp->pg)
537                 return;
538
539         vector_foreach_slot (mpp->pg, pgp, i) {
540                 if (!pgp->paths)
541                         continue;
542                 vector_foreach_slot(pgp->paths, pp, j) {
543                         const char *env;
544
545                         if (!pp->udev)
546                                 continue;
547                         /*
548                          * Paths that are already classified as multipath
549                          * members don't need another uevent.
550                          */
551                         env = udev_device_get_property_value(
552                                 pp->udev, "DM_MULTIPATH_DEVICE_PATH");
553
554                         if (is_mpath && env != NULL && !strcmp(env, "1")) {
555                                 /*
556                                  * If FIND_MULTIPATHS_WAIT_UNTIL is not "0",
557                                  * path is in "maybe" state and timer is running
558                                  * Send uevent now (see multipath.rules).
559                                  */
560                                 env = udev_device_get_property_value(
561                                         pp->udev, "FIND_MULTIPATHS_WAIT_UNTIL");
562                                 if (env == NULL || !strcmp(env, "0"))
563                                         continue;
564                         } else if (!is_mpath &&
565                                    (env == NULL || !strcmp(env, "0")))
566                                 continue;
567
568                         condlog(3, "triggering %s uevent for %s (is %smultipath member)",
569                                 action, pp->dev, is_mpath ? "" : "no ");
570                         sysfs_attr_set_value(pp->udev, "uevent",
571                                              action, strlen(action));
572                 }
573         }
574
575         mpp->needs_paths_uevent = 0;
576 }
577
578 static int
579 is_mpp_known_to_udev(const struct multipath *mpp)
580 {
581         struct udev_device *udd = get_udev_for_mpp(mpp);
582         int ret = (udd != NULL);
583         udev_device_unref(udd);
584         return ret;
585 }
586
587 static int
588 sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload)
589 {
590         struct pathgroup * pgp;
591         struct path *pp;
592         char buff[11];
593         int i, j, ret, err = 0;
594         struct udev_device *udd;
595         int max_sectors_kb;
596
597         if (mpp->max_sectors_kb == MAX_SECTORS_KB_UNDEF)
598                 return 0;
599         max_sectors_kb = mpp->max_sectors_kb;
600         if (is_reload) {
601                 if (!mpp->dmi && dm_get_info(mpp->alias, &mpp->dmi) != 0) {
602                         condlog(1, "failed to get dm info for %s", mpp->alias);
603                         return 1;
604                 }
605                 udd = get_udev_for_mpp(mpp);
606                 if (!udd) {
607                         condlog(1, "failed to get udev device to set max_sectors_kb for %s", mpp->alias);
608                         return 1;
609                 }
610                 ret = sysfs_attr_get_value(udd, "queue/max_sectors_kb", buff,
611                                            sizeof(buff));
612                 udev_device_unref(udd);
613                 if (ret <= 0) {
614                         condlog(1, "failed to get current max_sectors_kb from %s", mpp->alias);
615                         return 1;
616                 }
617                 if (sscanf(buff, "%u\n", &max_sectors_kb) != 1) {
618                         condlog(1, "can't parse current max_sectors_kb from %s",
619                                 mpp->alias);
620                         return 1;
621                 }
622         }
623         snprintf(buff, 11, "%d", max_sectors_kb);
624
625         vector_foreach_slot (mpp->pg, pgp, i) {
626                 vector_foreach_slot(pgp->paths, pp, j) {
627                         ret = sysfs_attr_set_value(pp->udev,
628                                                    "queue/max_sectors_kb",
629                                                    buff, strlen(buff));
630                         if (ret < 0) {
631                                 condlog(1, "failed setting max_sectors_kb on %s : %s", pp->dev, strerror(-ret));
632                                 err = 1;
633                         }
634                 }
635         }
636         return err;
637 }
638
639 static void
640 select_action (struct multipath * mpp, vector curmp, int force_reload)
641 {
642         struct multipath * cmpp;
643         struct multipath * cmpp_by_name;
644         char * mpp_feat, * cmpp_feat;
645
646         cmpp = find_mp_by_wwid(curmp, mpp->wwid);
647         cmpp_by_name = find_mp_by_alias(curmp, mpp->alias);
648
649         if (!cmpp_by_name) {
650                 if (cmpp) {
651                         condlog(2, "%s: rename %s to %s", mpp->wwid,
652                                 cmpp->alias, mpp->alias);
653                         strlcpy(mpp->alias_old, cmpp->alias, WWID_SIZE);
654                         mpp->action = ACT_RENAME;
655                         if (force_reload) {
656                                 mpp->force_udev_reload = 1;
657                                 mpp->action = ACT_FORCERENAME;
658                         }
659                         return;
660                 }
661                 mpp->action = ACT_CREATE;
662                 condlog(3, "%s: set ACT_CREATE (map does not exist)",
663                         mpp->alias);
664                 return;
665         }
666
667         if (!cmpp) {
668                 condlog(2, "%s: remove (wwid changed)", mpp->alias);
669                 dm_flush_map(mpp->alias);
670                 strlcpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE);
671                 drop_multipath(curmp, cmpp_by_name->wwid, KEEP_PATHS);
672                 mpp->action = ACT_CREATE;
673                 condlog(3, "%s: set ACT_CREATE (map wwid change)",
674                         mpp->alias);
675                 return;
676         }
677
678         if (cmpp != cmpp_by_name) {
679                 condlog(2, "%s: unable to rename %s to %s (%s is used by %s)",
680                         mpp->wwid, cmpp->alias, mpp->alias,
681                         mpp->alias, cmpp_by_name->wwid);
682                 /* reset alias to existing alias */
683                 FREE(mpp->alias);
684                 mpp->alias = STRDUP(cmpp->alias);
685                 mpp->action = ACT_IMPOSSIBLE;
686                 return;
687         }
688
689         if (pathcount(mpp, PATH_UP) == 0) {
690                 mpp->action = ACT_IMPOSSIBLE;
691                 condlog(3, "%s: set ACT_IMPOSSIBLE (no usable path)",
692                         mpp->alias);
693                 return;
694         }
695         if (force_reload) {
696                 mpp->force_udev_reload = 1;
697                 mpp->action = ACT_RELOAD;
698                 condlog(3, "%s: set ACT_RELOAD (forced by user)",
699                         mpp->alias);
700                 return;
701         }
702         if (cmpp->size != mpp->size) {
703                 mpp->force_udev_reload = 1;
704                 mpp->action = ACT_RESIZE;
705                 condlog(3, "%s: set ACT_RESIZE (size change)",
706                         mpp->alias);
707                 return;
708         }
709
710         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF &&
711             !!strstr(mpp->features, "queue_if_no_path") !=
712             !!strstr(cmpp->features, "queue_if_no_path")) {
713                 mpp->action =  ACT_RELOAD;
714                 condlog(3, "%s: set ACT_RELOAD (no_path_retry change)",
715                         mpp->alias);
716                 return;
717         }
718         if ((mpp->retain_hwhandler != RETAIN_HWHANDLER_ON ||
719              strcmp(cmpp->hwhandler, "0") == 0) &&
720             (strlen(cmpp->hwhandler) != strlen(mpp->hwhandler) ||
721              strncmp(cmpp->hwhandler, mpp->hwhandler,
722                     strlen(mpp->hwhandler)))) {
723                 mpp->action = ACT_RELOAD;
724                 condlog(3, "%s: set ACT_RELOAD (hwhandler change)",
725                         mpp->alias);
726                 return;
727         }
728
729         if (mpp->retain_hwhandler != RETAIN_HWHANDLER_UNDEF &&
730             !!strstr(mpp->features, "retain_attached_hw_handler") !=
731             !!strstr(cmpp->features, "retain_attached_hw_handler") &&
732             get_linux_version_code() < KERNEL_VERSION(4, 3, 0)) {
733                 mpp->action = ACT_RELOAD;
734                 condlog(3, "%s: set ACT_RELOAD (retain_hwhandler change)",
735                         mpp->alias);
736                 return;
737         }
738
739         cmpp_feat = STRDUP(cmpp->features);
740         mpp_feat = STRDUP(mpp->features);
741         if (cmpp_feat && mpp_feat) {
742                 remove_feature(&mpp_feat, "queue_if_no_path");
743                 remove_feature(&mpp_feat, "retain_attached_hw_handler");
744                 remove_feature(&cmpp_feat, "queue_if_no_path");
745                 remove_feature(&cmpp_feat, "retain_attached_hw_handler");
746                 if (strncmp(mpp_feat, cmpp_feat, PARAMS_SIZE)) {
747                         mpp->action =  ACT_RELOAD;
748                         condlog(3, "%s: set ACT_RELOAD (features change)",
749                                 mpp->alias);
750                 }
751         }
752         FREE(cmpp_feat);
753         FREE(mpp_feat);
754
755         if (!cmpp->selector || strncmp(cmpp->selector, mpp->selector,
756                     strlen(mpp->selector))) {
757                 mpp->action = ACT_RELOAD;
758                 condlog(3, "%s: set ACT_RELOAD (selector change)",
759                         mpp->alias);
760                 return;
761         }
762         if (cmpp->minio != mpp->minio) {
763                 mpp->action = ACT_RELOAD;
764                 condlog(3, "%s: set ACT_RELOAD (minio change, %u->%u)",
765                         mpp->alias, cmpp->minio, mpp->minio);
766                 return;
767         }
768         if (!cmpp->pg || VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
769                 mpp->action = ACT_RELOAD;
770                 condlog(3, "%s: set ACT_RELOAD (path group number change)",
771                         mpp->alias);
772                 return;
773         }
774         if (pgcmp(mpp, cmpp)) {
775                 mpp->action = ACT_RELOAD;
776                 condlog(3, "%s: set ACT_RELOAD (path group topology change)",
777                         mpp->alias);
778                 return;
779         }
780         if (cmpp->nextpg != mpp->bestpg) {
781                 mpp->action = ACT_SWITCHPG;
782                 condlog(3, "%s: set ACT_SWITCHPG (next path group change)",
783                         mpp->alias);
784                 return;
785         }
786         if (!is_mpp_known_to_udev(cmpp)) {
787                 mpp->action = ACT_RELOAD;
788                 condlog(3, "%s: set ACT_RELOAD (udev device not initialized)",
789                         mpp->alias);
790                 return;
791         }
792         mpp->action = ACT_NOTHING;
793         condlog(3, "%s: set ACT_NOTHING (map unchanged)",
794                 mpp->alias);
795         return;
796 }
797
798 int reinstate_paths(struct multipath *mpp)
799 {
800         int i, j;
801         struct pathgroup * pgp;
802         struct path * pp;
803
804         if (!mpp->pg)
805                 return 0;
806
807         vector_foreach_slot (mpp->pg, pgp, i) {
808                 if (!pgp->paths)
809                         continue;
810
811                 vector_foreach_slot (pgp->paths, pp, j) {
812                         if (pp->state != PATH_UP &&
813                             (pgp->status == PGSTATE_DISABLED ||
814                              pgp->status == PGSTATE_ACTIVE))
815                                 continue;
816
817                         if (pp->dmstate == PSTATE_FAILED) {
818                                 if (dm_reinstate_path(mpp->alias, pp->dev_t))
819                                         condlog(0, "%s: error reinstating",
820                                                 pp->dev);
821                         }
822                 }
823         }
824         return 0;
825 }
826
827 static int
828 lock_multipath (struct multipath * mpp, int lock)
829 {
830         struct pathgroup * pgp;
831         struct path * pp;
832         int i, j;
833         int x, y;
834
835         if (!mpp || !mpp->pg)
836                 return 0;
837
838         vector_foreach_slot (mpp->pg, pgp, i) {
839                 if (!pgp->paths)
840                         continue;
841                 vector_foreach_slot(pgp->paths, pp, j) {
842                         if (lock && flock(pp->fd, LOCK_SH | LOCK_NB) &&
843                             errno == EWOULDBLOCK)
844                                 goto fail;
845                         else if (!lock)
846                                 flock(pp->fd, LOCK_UN);
847                 }
848         }
849         return 0;
850 fail:
851         vector_foreach_slot (mpp->pg, pgp, x) {
852                 if (x > i)
853                         return 1;
854                 if (!pgp->paths)
855                         continue;
856                 vector_foreach_slot(pgp->paths, pp, y) {
857                         if (x == i && y >= j)
858                                 return 1;
859                         flock(pp->fd, LOCK_UN);
860                 }
861         }
862         return 1;
863 }
864
865 int domap(struct multipath *mpp, char *params, int is_daemon)
866 {
867         int r = DOMAP_FAIL;
868         struct config *conf;
869         int verbosity;
870
871         /*
872          * last chance to quit before touching the devmaps
873          */
874         if (mpp->action == ACT_DRY_RUN) {
875                 conf = get_multipath_config();
876                 verbosity = conf->verbosity;
877                 put_multipath_config(conf);
878                 print_multipath_topology(mpp, verbosity);
879                 return DOMAP_DRY;
880         }
881
882         if (mpp->action == ACT_CREATE &&
883             dm_map_present(mpp->alias)) {
884                 condlog(3, "%s: map already present", mpp->alias);
885                 mpp->action = ACT_RELOAD;
886         }
887
888         switch (mpp->action) {
889         case ACT_REJECT:
890         case ACT_NOTHING:
891         case ACT_IMPOSSIBLE:
892                 return DOMAP_EXIST;
893
894         case ACT_SWITCHPG:
895                 dm_switchgroup(mpp->alias, mpp->bestpg);
896                 /*
897                  * we may have avoided reinstating paths because there where in
898                  * active or disabled PG. Now that the topology has changed,
899                  * retry.
900                  */
901                 reinstate_paths(mpp);
902                 return DOMAP_EXIST;
903
904         case ACT_CREATE:
905                 if (lock_multipath(mpp, 1)) {
906                         condlog(3, "%s: failed to create map (in use)",
907                                 mpp->alias);
908                         return DOMAP_RETRY;
909                 }
910
911                 sysfs_set_max_sectors_kb(mpp, 0);
912                 if (is_daemon && mpp->ghost_delay > 0 && mpp->nr_active &&
913                     pathcount(mpp, PATH_GHOST) == mpp->nr_active)
914                         mpp->ghost_delay_tick = mpp->ghost_delay;
915                 r = dm_addmap_create(mpp, params);
916
917                 lock_multipath(mpp, 0);
918                 break;
919
920         case ACT_RELOAD:
921                 sysfs_set_max_sectors_kb(mpp, 1);
922                 if (mpp->ghost_delay_tick > 0 && pathcount(mpp, PATH_UP))
923                         mpp->ghost_delay_tick = 0;
924                 r = dm_addmap_reload(mpp, params, 0);
925                 break;
926
927         case ACT_RESIZE:
928                 sysfs_set_max_sectors_kb(mpp, 1);
929                 if (mpp->ghost_delay_tick > 0 && pathcount(mpp, PATH_UP))
930                         mpp->ghost_delay_tick = 0;
931                 r = dm_addmap_reload(mpp, params, 1);
932                 break;
933
934         case ACT_RENAME:
935                 conf = get_multipath_config();
936                 pthread_cleanup_push(put_multipath_config, conf);
937                 r = dm_rename(mpp->alias_old, mpp->alias,
938                               conf->partition_delim, mpp->skip_kpartx);
939                 pthread_cleanup_pop(1);
940                 break;
941
942         case ACT_FORCERENAME:
943                 conf = get_multipath_config();
944                 pthread_cleanup_push(put_multipath_config, conf);
945                 r = dm_rename(mpp->alias_old, mpp->alias,
946                               conf->partition_delim, mpp->skip_kpartx);
947                 pthread_cleanup_pop(1);
948                 if (r) {
949                         sysfs_set_max_sectors_kb(mpp, 1);
950                         if (mpp->ghost_delay_tick > 0 &&
951                             pathcount(mpp, PATH_UP))
952                                 mpp->ghost_delay_tick = 0;
953                         r = dm_addmap_reload(mpp, params, 0);
954                 }
955                 break;
956
957         default:
958                 break;
959         }
960
961         if (r == DOMAP_OK) {
962                 /*
963                  * DM_DEVICE_CREATE, DM_DEVICE_RENAME, or DM_DEVICE_RELOAD
964                  * succeeded
965                  */
966                 mpp->force_udev_reload = 0;
967                 if (mpp->action == ACT_CREATE &&
968                     (remember_wwid(mpp->wwid) == 1 ||
969                      mpp->needs_paths_uevent))
970                         trigger_paths_udev_change(mpp, true);
971                 if (!is_daemon) {
972                         /* multipath client mode */
973                         dm_switchgroup(mpp->alias, mpp->bestpg);
974                 } else  {
975                         /* multipath daemon mode */
976                         mpp->stat_map_loads++;
977                         condlog(2, "%s: load table [0 %llu %s %s]", mpp->alias,
978                                 mpp->size, TGT_MPATH, params);
979                         /*
980                          * Required action is over, reset for the stateful daemon.
981                          * But don't do it for creation as we use in the caller the
982                          * mpp->action to figure out whether to start the watievent checker.
983                          */
984                         if (mpp->action != ACT_CREATE)
985                                 mpp->action = ACT_NOTHING;
986                         else {
987                                 conf = get_multipath_config();
988                                 mpp->wait_for_udev = 1;
989                                 mpp->uev_wait_tick = conf->uev_wait_timeout;
990                                 put_multipath_config(conf);
991                         }
992                 }
993                 dm_setgeometry(mpp);
994                 return DOMAP_OK;
995         } else if (r == DOMAP_FAIL && mpp->action == ACT_CREATE &&
996                    mpp->needs_paths_uevent)
997                 trigger_paths_udev_change(mpp, false);
998
999         return DOMAP_FAIL;
1000 }
1001
1002 static int
1003 deadmap (struct multipath * mpp)
1004 {
1005         int i, j;
1006         struct pathgroup * pgp;
1007         struct path * pp;
1008
1009         if (!mpp->pg)
1010                 return 1;
1011
1012         vector_foreach_slot (mpp->pg, pgp, i) {
1013                 if (!pgp->paths)
1014                         continue;
1015
1016                 vector_foreach_slot (pgp->paths, pp, j)
1017                         if (strlen(pp->dev))
1018                                 return 0; /* alive */
1019         }
1020
1021         return 1; /* dead */
1022 }
1023
1024 int check_daemon(void)
1025 {
1026         int fd;
1027         char *reply;
1028         int ret = 0;
1029         unsigned int timeout;
1030         struct config *conf;
1031
1032         fd = mpath_connect();
1033         if (fd == -1)
1034                 return 0;
1035
1036         if (send_packet(fd, "show daemon") != 0)
1037                 goto out;
1038         conf = get_multipath_config();
1039         timeout = conf->uxsock_timeout;
1040         put_multipath_config(conf);
1041         if (recv_packet(fd, &reply, timeout) != 0)
1042                 goto out;
1043
1044         if (reply && strstr(reply, "shutdown"))
1045                 goto out_free;
1046
1047         ret = 1;
1048
1049 out_free:
1050         FREE(reply);
1051 out:
1052         mpath_disconnect(fd);
1053         return ret;
1054 }
1055
1056 /*
1057  * The force_reload parameter determines how coalesce_paths treats existing maps.
1058  * FORCE_RELOAD_NONE: existing maps aren't touched at all
1059  * FORCE_RELOAD_YES: all maps are rebuilt from scratch and (re)loaded in DM
1060  * FORCE_RELOAD_WEAK: existing maps are compared to the current conf and only
1061  * reloaded in DM if there's a difference. This is useful during startup.
1062  */
1063 int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
1064                     int force_reload, enum mpath_cmds cmd)
1065 {
1066         int ret = CP_FAIL;
1067         int k, i, r;
1068         int is_daemon = (cmd == CMD_NONE) ? 1 : 0;
1069         char params[PARAMS_SIZE];
1070         struct multipath * mpp;
1071         struct path * pp1;
1072         struct path * pp2;
1073         vector curmp = vecs->mpvec;
1074         vector pathvec = vecs->pathvec;
1075         struct config *conf;
1076         int allow_queueing;
1077         uint64_t *size_mismatch_seen;
1078
1079         /* ignore refwwid if it's empty */
1080         if (refwwid && !strlen(refwwid))
1081                 refwwid = NULL;
1082
1083         if (force_reload != FORCE_RELOAD_NONE) {
1084                 vector_foreach_slot (pathvec, pp1, k) {
1085                         pp1->mpp = NULL;
1086                 }
1087         }
1088
1089         if (VECTOR_SIZE(pathvec) == 0)
1090                 return CP_OK;
1091         size_mismatch_seen = calloc((VECTOR_SIZE(pathvec) - 1) / 64 + 1,
1092                                     sizeof(uint64_t));
1093         if (size_mismatch_seen == NULL)
1094                 return CP_FAIL;
1095
1096         vector_foreach_slot (pathvec, pp1, k) {
1097                 int invalid;
1098                 /* skip this path for some reason */
1099
1100                 /* 1. if path has no unique id or wwid blacklisted */
1101                 if (strlen(pp1->wwid) == 0) {
1102                         orphan_path(pp1, "no WWID");
1103                         continue;
1104                 }
1105
1106                 conf = get_multipath_config();
1107                 pthread_cleanup_push(put_multipath_config, conf);
1108                 invalid = (filter_path(conf, pp1) > 0);
1109                 pthread_cleanup_pop(1);
1110                 if (invalid) {
1111                         orphan_path(pp1, "blacklisted");
1112                         continue;
1113                 }
1114
1115                 /* 2. if path already coalesced, or seen and discarded */
1116                 if (pp1->mpp || is_bit_set_in_array(k, size_mismatch_seen))
1117                         continue;
1118
1119                 /* 3. if path has disappeared */
1120                 if (pp1->state == PATH_REMOVED) {
1121                         orphan_path(pp1, "path removed");
1122                         continue;
1123                 }
1124
1125                 /* 4. path is out of scope */
1126                 if (refwwid && strncmp(pp1->wwid, refwwid, WWID_SIZE - 1))
1127                         continue;
1128
1129                 /* If find_multipaths was selected check if the path is valid */
1130                 if (!refwwid && !should_multipath(pp1, pathvec, curmp)) {
1131                         orphan_path(pp1, "only one path");
1132                         continue;
1133                 }
1134
1135                 /*
1136                  * at this point, we know we really got a new mp
1137                  */
1138                 mpp = add_map_with_path(vecs, pp1, 0);
1139                 if (!mpp) {
1140                         orphan_path(pp1, "failed to create multipath device");
1141                         continue;
1142                 }
1143
1144                 if (!mpp->paths) {
1145                         condlog(0, "%s: skip coalesce (no paths)", mpp->alias);
1146                         remove_map(mpp, vecs, 0);
1147                         continue;
1148                 }
1149
1150                 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
1151                         pp2 = VECTOR_SLOT(pathvec, i);
1152
1153                         if (strcmp(pp1->wwid, pp2->wwid))
1154                                 continue;
1155
1156                         if (!mpp->size && pp2->size)
1157                                 mpp->size = pp2->size;
1158
1159                         if (mpp->size && pp2->size &&
1160                             pp2->size != mpp->size) {
1161                                 /*
1162                                  * ouch, avoid feeding that to the DM
1163                                  */
1164                                 condlog(0, "%s: size %llu, expected %llu. "
1165                                         "Discard", pp2->dev, pp2->size,
1166                                         mpp->size);
1167                                 mpp->action = ACT_REJECT;
1168                                 set_bit_in_array(i, size_mismatch_seen);
1169                         }
1170                 }
1171                 verify_paths(mpp, vecs);
1172
1173                 params[0] = '\0';
1174                 if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1175                         remove_map(mpp, vecs, 0);
1176                         continue;
1177                 }
1178
1179                 if (cmd == CMD_DRY_RUN)
1180                         mpp->action = ACT_DRY_RUN;
1181                 if (mpp->action == ACT_UNDEF)
1182                         select_action(mpp, curmp,
1183                                       force_reload == FORCE_RELOAD_YES ? 1 : 0);
1184
1185                 r = domap(mpp, params, is_daemon);
1186
1187                 if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
1188                         condlog(3, "%s: domap (%u) failure "
1189                                    "for create/reload map",
1190                                 mpp->alias, r);
1191                         if (r == DOMAP_FAIL || is_daemon) {
1192                                 condlog(2, "%s: %s map",
1193                                         mpp->alias, (mpp->action == ACT_CREATE)?
1194                                         "ignoring" : "removing");
1195                                 remove_map(mpp, vecs, 0);
1196                                 continue;
1197                         } else /* if (r == DOMAP_RETRY && !is_daemon) */ {
1198                                 ret = CP_RETRY;
1199                                 goto out;
1200                         }
1201                 }
1202                 if (r == DOMAP_DRY)
1203                         continue;
1204
1205                 if (r == DOMAP_EXIST && mpp->action == ACT_NOTHING &&
1206                     force_reload == FORCE_RELOAD_WEAK)
1207                         /*
1208                          * First time we're called, and no changes applied.
1209                          * domap() was a noop. But we can't be sure that
1210                          * udev has already finished setting up this device
1211                          * (udev in initrd may have been shut down while
1212                          * processing this device or its children).
1213                          * Trigger a change event, just in case.
1214                          */
1215                         trigger_udev_change(find_mp_by_wwid(curmp, mpp->wwid));
1216
1217                 conf = get_multipath_config();
1218                 allow_queueing = conf->allow_queueing;
1219                 put_multipath_config(conf);
1220                 if (!is_daemon && !allow_queueing && !check_daemon()) {
1221                         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF &&
1222                             mpp->no_path_retry != NO_PATH_RETRY_FAIL)
1223                                 condlog(3, "%s: multipathd not running, unset "
1224                                         "queue_if_no_path feature", mpp->alias);
1225                         if (!dm_queue_if_no_path(mpp->alias, 0))
1226                                 remove_feature(&mpp->features,
1227                                                "queue_if_no_path");
1228                 }
1229
1230                 if (!is_daemon && mpp->action != ACT_NOTHING) {
1231                         int verbosity;
1232
1233                         conf = get_multipath_config();
1234                         verbosity = conf->verbosity;
1235                         put_multipath_config(conf);
1236                         print_multipath_topology(mpp, verbosity);
1237                 }
1238
1239                 if (newmp) {
1240                         if (mpp->action != ACT_REJECT) {
1241                                 if (!vector_alloc_slot(newmp))
1242                                         goto out;
1243                                 vector_set_slot(newmp, mpp);
1244                         }
1245                         else
1246                                 remove_map(mpp, vecs, 0);
1247                 }
1248         }
1249         /*
1250          * Flush maps with only dead paths (ie not in sysfs)
1251          * Keep maps with only failed paths
1252          */
1253         if (newmp) {
1254                 vector_foreach_slot (newmp, mpp, i) {
1255                         char alias[WWID_SIZE];
1256
1257                         if (!deadmap(mpp))
1258                                 continue;
1259
1260                         strlcpy(alias, mpp->alias, WWID_SIZE);
1261
1262                         vector_del_slot(newmp, i);
1263                         i--;
1264                         remove_map(mpp, vecs, 0);
1265
1266                         if (dm_flush_map(alias))
1267                                 condlog(2, "%s: remove failed (dead)",
1268                                         alias);
1269                         else
1270                                 condlog(2, "%s: remove (dead)", alias);
1271                 }
1272         }
1273         ret = CP_OK;
1274 out:
1275         free(size_mismatch_seen);
1276         return ret;
1277 }
1278
1279 struct udev_device *get_udev_device(const char *dev, enum devtypes dev_type)
1280 {
1281         struct udev_device *ud = NULL;
1282         const char *base;
1283
1284         if (dev == NULL || *dev == '\0')
1285                 return NULL;
1286
1287         switch (dev_type) {
1288         case DEV_DEVNODE:
1289         case DEV_DEVMAP:
1290                 /* This should be GNU basename, compiler will warn if not */
1291                 base = basename(dev);
1292                 if (*base == '\0')
1293                         break;
1294                 ud = udev_device_new_from_subsystem_sysname(udev, "block",
1295                                                             base);
1296                 break;
1297         case DEV_DEVT:
1298                 ud = udev_device_new_from_devnum(udev, 'b', parse_devt(dev));
1299                 break;
1300         case DEV_UEVENT:
1301                 ud = udev_device_new_from_environment(udev);
1302                 break;
1303         default:
1304                 condlog(0, "Internal error: get_udev_device called with invalid type %d\n",
1305                         dev_type);
1306                 break;
1307         }
1308         if (ud == NULL)
1309                 condlog(2, "get_udev_device: failed to look up %s with type %d",
1310                         dev, dev_type);
1311         return ud;
1312 }
1313
1314 /*
1315  * returns:
1316  * 0 - success
1317  * 1 - failure
1318  * 2 - blacklist
1319  */
1320 int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
1321                 vector pathvec, char **wwid)
1322 {
1323         int ret = 1;
1324         struct path * pp;
1325         char buff[FILE_NAME_SIZE];
1326         char * refwwid = NULL, tmpwwid[WWID_SIZE];
1327         int flags = DI_SYSFS | DI_WWID;
1328         struct config *conf;
1329         int invalid = 0;
1330
1331         if (!wwid)
1332                 return 1;
1333         *wwid = NULL;
1334
1335         if (dev_type == DEV_NONE)
1336                 return 1;
1337
1338         if (cmd != CMD_REMOVE_WWID)
1339                 flags |= DI_BLACKLIST;
1340
1341         if (dev_type == DEV_DEVNODE) {
1342                 if (basenamecpy(dev, buff, FILE_NAME_SIZE) == 0) {
1343                         condlog(1, "basename failed for '%s' (%s)",
1344                                 dev, buff);
1345                         return 1;
1346                 }
1347
1348                 pp = find_path_by_dev(pathvec, buff);
1349                 if (!pp) {
1350                         struct udev_device *udevice =
1351                                 get_udev_device(buff, dev_type);
1352
1353                         if (!udevice)
1354                                 return 1;
1355
1356                         conf = get_multipath_config();
1357                         pthread_cleanup_push(put_multipath_config, conf);
1358                         ret = store_pathinfo(pathvec, conf, udevice,
1359                                              flags, &pp);
1360                         pthread_cleanup_pop(1);
1361                         udev_device_unref(udevice);
1362                         if (!pp) {
1363                                 if (ret == 1)
1364                                         condlog(0, "%s: can't store path info",
1365                                                 dev);
1366                                 return ret;
1367                         }
1368                 }
1369                 conf = get_multipath_config();
1370                 pthread_cleanup_push(put_multipath_config, conf);
1371                 if (pp->udev && pp->uid_attribute &&
1372                     filter_property(conf, pp->udev, 3) > 0)
1373                         invalid = 1;
1374                 pthread_cleanup_pop(1);
1375                 if (invalid)
1376                         return 2;
1377
1378                 refwwid = pp->wwid;
1379                 goto out;
1380         }
1381
1382         if (dev_type == DEV_DEVT) {
1383                 strchop(dev);
1384                 if (devt2devname(buff, FILE_NAME_SIZE, dev)) {
1385                         condlog(0, "%s: cannot find block device\n", dev);
1386                         return 1;
1387                 }
1388                 pp = find_path_by_dev(pathvec, buff);
1389                 if (!pp) {
1390                         struct udev_device *udevice =
1391                                 get_udev_device(dev, dev_type);
1392
1393                         if (!udevice)
1394                                 return 1;
1395
1396                         conf = get_multipath_config();
1397                         pthread_cleanup_push(put_multipath_config, conf);
1398                         ret = store_pathinfo(pathvec, conf, udevice,
1399                                              flags, &pp);
1400                         pthread_cleanup_pop(1);
1401                         udev_device_unref(udevice);
1402                         if (!pp) {
1403                                 if (ret == 1)
1404                                         condlog(0, "%s can't store path info",
1405                                                 buff);
1406                                 return ret;
1407                         }
1408                 }
1409                 conf = get_multipath_config();
1410                 pthread_cleanup_push(put_multipath_config, conf);
1411                 if (pp->udev && pp->uid_attribute &&
1412                     filter_property(conf, pp->udev, 3) > 0)
1413                         invalid = 1;
1414                 pthread_cleanup_pop(1);
1415                 if (invalid)
1416                         return 2;
1417                 refwwid = pp->wwid;
1418                 goto out;
1419         }
1420
1421         if (dev_type == DEV_UEVENT) {
1422                 struct udev_device *udevice = get_udev_device(dev, dev_type);
1423
1424                 if (!udevice)
1425                         return 1;
1426
1427                 conf = get_multipath_config();
1428                 pthread_cleanup_push(put_multipath_config, conf);
1429                 ret = store_pathinfo(pathvec, conf, udevice,
1430                                      flags, &pp);
1431                 pthread_cleanup_pop(1);
1432                 udev_device_unref(udevice);
1433                 if (!pp) {
1434                         if (ret == 1)
1435                                 condlog(0, "%s: can't store path info", dev);
1436                         return ret;
1437                 }
1438                 conf = get_multipath_config();
1439                 pthread_cleanup_push(put_multipath_config, conf);
1440                 if (pp->udev && pp->uid_attribute &&
1441                     filter_property(conf, pp->udev, 3) > 0)
1442                         invalid = 1;
1443                 pthread_cleanup_pop(1);
1444                 if (invalid)
1445                         return 2;
1446                 refwwid = pp->wwid;
1447                 goto out;
1448         }
1449
1450         if (dev_type == DEV_DEVMAP) {
1451
1452                 conf = get_multipath_config();
1453                 pthread_cleanup_push(put_multipath_config, conf);
1454                 if (((dm_get_uuid(dev, tmpwwid)) == 0)
1455                     && (strlen(tmpwwid))) {
1456                         refwwid = tmpwwid;
1457                         goto check;
1458                 }
1459
1460                 /*
1461                  * may be a binding
1462                  */
1463                 if (get_user_friendly_wwid(dev, tmpwwid,
1464                                            conf->bindings_file) == 0) {
1465                         refwwid = tmpwwid;
1466                         goto check;
1467                 }
1468
1469                 /*
1470                  * or may be an alias
1471                  */
1472                 refwwid = get_mpe_wwid(conf->mptable, dev);
1473
1474                 /*
1475                  * or directly a wwid
1476                  */
1477                 if (!refwwid)
1478                         refwwid = dev;
1479
1480 check:
1481                 if (refwwid && strlen(refwwid) &&
1482                     filter_wwid(conf->blist_wwid, conf->elist_wwid, refwwid,
1483                                 NULL) > 0)
1484                         invalid = 1;
1485                 pthread_cleanup_pop(1);
1486                 if (invalid)
1487                         return 2;
1488         }
1489 out:
1490         if (refwwid && strlen(refwwid)) {
1491                 *wwid = STRDUP(refwwid);
1492                 return 0;
1493         }
1494
1495         return 1;
1496 }
1497
1498 int reload_map(struct vectors *vecs, struct multipath *mpp, int refresh,
1499                int is_daemon)
1500 {
1501         char params[PARAMS_SIZE] = {0};
1502         struct path *pp;
1503         int i, r;
1504
1505         update_mpp_paths(mpp, vecs->pathvec);
1506         if (refresh) {
1507                 vector_foreach_slot (mpp->paths, pp, i) {
1508                         struct config *conf = get_multipath_config();
1509                         pthread_cleanup_push(put_multipath_config, conf);
1510                         r = pathinfo(pp, conf, DI_PRIO);
1511                         pthread_cleanup_pop(1);
1512                         if (r) {
1513                                 condlog(2, "%s: failed to refresh pathinfo",
1514                                         mpp->alias);
1515                                 return 1;
1516                         }
1517                 }
1518         }
1519         if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1520                 condlog(0, "%s: failed to setup map", mpp->alias);
1521                 return 1;
1522         }
1523         select_action(mpp, vecs->mpvec, 1);
1524
1525         r = domap(mpp, params, is_daemon);
1526         if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
1527                 condlog(3, "%s: domap (%u) failure "
1528                         "for reload map", mpp->alias, r);
1529                 return 1;
1530         }
1531
1532         return 0;
1533 }