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