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