Return error when receiving CLI packet
[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
18 #include "checkers.h"
19 #include "vector.h"
20 #include "memory.h"
21 #include "devmapper.h"
22 #include "defaults.h"
23 #include "structs.h"
24 #include "structs_vec.h"
25 #include "dmparser.h"
26 #include "config.h"
27 #include "blacklist.h"
28 #include "propsel.h"
29 #include "discovery.h"
30 #include "debug.h"
31 #include "switchgroup.h"
32 #include "print.h"
33 #include "configure.h"
34 #include "pgpolicies.h"
35 #include "dict.h"
36 #include "alias.h"
37 #include "prio.h"
38 #include "util.h"
39 #include "uxsock.h"
40 #include "wwids.h"
41
42 /* group paths in pg by host adapter
43  */
44 int group_by_host_adapter(struct pathgroup *pgp, vector adapters)
45 {
46         struct adapter_group *agp;
47         struct host_group *hgp;
48         struct path *pp, *pp1;
49         char adapter_name1[SLOT_NAME_SIZE];
50         char adapter_name2[SLOT_NAME_SIZE];
51         int i, j;
52         int found_hostgroup = 0;
53
54         while (VECTOR_SIZE(pgp->paths) > 0) {
55
56                 pp = VECTOR_SLOT(pgp->paths, 0);
57
58                 if (sysfs_get_host_adapter_name(pp, adapter_name1))
59                         goto out;
60                 /* create a new host adapter group
61                  */
62                 agp = alloc_adaptergroup();
63                 if (!agp)
64                         goto out;
65                 agp->pgp = pgp;
66
67                 strncpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE);
68                 store_adaptergroup(adapters, agp);
69
70                 /* create a new host port group
71                  */
72                 hgp = alloc_hostgroup();
73                 if (!hgp)
74                         goto out;
75                 if (store_hostgroup(agp->host_groups, hgp))
76                         goto out;
77
78                 hgp->host_no = pp->sg_id.host_no;
79                 agp->num_hosts++;
80                 if (store_path(hgp->paths, pp))
81                         goto out;
82
83                 hgp->num_paths++;
84                 /* delete path from path group
85                  */
86                 vector_del_slot(pgp->paths, 0);
87
88                 /* add all paths belonging to same host adapter
89                  */
90                 vector_foreach_slot(pgp->paths, pp1, i) {
91                         if (sysfs_get_host_adapter_name(pp1, adapter_name2))
92                                 goto out;
93                         if (strcmp(adapter_name1, adapter_name2) == 0) {
94                                 found_hostgroup = 0;
95                                 vector_foreach_slot(agp->host_groups, hgp, j) {
96                                         if (hgp->host_no == pp1->sg_id.host_no) {
97                                                 if (store_path(hgp->paths, pp1))
98                                                         goto out;
99                                                 hgp->num_paths++;
100                                                 found_hostgroup = 1;
101                                                 break;
102                                         }
103                                 }
104                                 if (!found_hostgroup) {
105                                         /* this path belongs to new host port
106                                          * within this adapter
107                                          */
108                                         hgp = alloc_hostgroup();
109                                         if (!hgp)
110                                                 goto out;
111
112                                         if (store_hostgroup(agp->host_groups, hgp))
113                                                 goto out;
114
115                                         agp->num_hosts++;
116                                         if (store_path(hgp->paths, pp1))
117                                                 goto out;
118
119                                         hgp->host_no = pp1->sg_id.host_no;
120                                         hgp->num_paths++;
121                                 }
122                                 /* delete paths from original path_group
123                                  * as they are added into adapter group now
124                                  */
125                                 vector_del_slot(pgp->paths, i);
126                                 i--;
127                         }
128                 }
129         }
130         return 0;
131
132 out:    /* add back paths into pg as re-ordering failed
133          */
134         vector_foreach_slot(adapters, agp, i) {
135                         vector_foreach_slot(agp->host_groups, hgp, j) {
136                                 while (VECTOR_SIZE(hgp->paths) > 0) {
137                                         pp = VECTOR_SLOT(hgp->paths, 0);
138                                         if (store_path(pgp->paths, pp))
139                                                 condlog(3, "failed to restore "
140                                                 "path %s into path group",
141                                                  pp->dev);
142                                         vector_del_slot(hgp->paths, 0);
143                                 }
144                         }
145                 }
146         free_adaptergroup(adapters);
147         return 1;
148 }
149
150 /* re-order paths in pg by alternating adapters and host ports
151  * for optimized selection
152  */
153 int order_paths_in_pg_by_alt_adapters(struct pathgroup *pgp, vector adapters,
154                  int total_paths)
155 {
156         int next_adapter_index = 0;
157         struct adapter_group *agp;
158         struct host_group *hgp;
159         struct path *pp;
160
161         while (total_paths > 0) {
162                 agp = VECTOR_SLOT(adapters, next_adapter_index);
163                 if (!agp) {
164                         condlog(0, "can't get adapter group %d", next_adapter_index);
165                         return 1;
166                 }
167
168                 hgp = VECTOR_SLOT(agp->host_groups, agp->next_host_index);
169                 if (!hgp) {
170                         condlog(0, "can't get host group %d of adapter group %d", next_adapter_index, agp->next_host_index);
171                         return 1;
172                 }
173
174                 if (!hgp->num_paths) {
175                         agp->next_host_index++;
176                         agp->next_host_index %= agp->num_hosts;
177                         next_adapter_index++;
178                         next_adapter_index %= VECTOR_SIZE(adapters);
179                         continue;
180                 }
181
182                 pp  = VECTOR_SLOT(hgp->paths, 0);
183
184                 if (store_path(pgp->paths, pp))
185                         return 1;
186
187                 total_paths--;
188
189                 vector_del_slot(hgp->paths, 0);
190
191                 hgp->num_paths--;
192
193                 agp->next_host_index++;
194                 agp->next_host_index %= agp->num_hosts;
195                 next_adapter_index++;
196                 next_adapter_index %= VECTOR_SIZE(adapters);
197         }
198
199         /* all paths are added into path_group
200          * in crafted child order
201          */
202         return 0;
203 }
204
205 /* round-robin: order paths in path group to alternate
206  * between all host adapters
207  */
208 int rr_optimize_path_order(struct pathgroup *pgp)
209 {
210         vector adapters;
211         struct path *pp;
212         int total_paths;
213         int i;
214
215         total_paths = VECTOR_SIZE(pgp->paths);
216         vector_foreach_slot(pgp->paths, pp, i) {
217                 if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP &&
218                         pp->sg_id.proto_id != SCSI_PROTOCOL_SAS &&
219                         pp->sg_id.proto_id != SCSI_PROTOCOL_ISCSI &&
220                         pp->sg_id.proto_id != SCSI_PROTOCOL_SRP) {
221                         /* return success as default path order
222                          * is maintained in path group
223                          */
224                         return 0;
225                 }
226         }
227         adapters = vector_alloc();
228         if (!adapters)
229                 return 0;
230
231         /* group paths in path group by host adapters
232          */
233         if (group_by_host_adapter(pgp, adapters)) {
234                 /* already freed adapters */
235                 condlog(3, "Failed to group paths by adapters");
236                 return 0;
237         }
238
239         /* re-order paths in pg to alternate between adapters and host ports
240          */
241         if (order_paths_in_pg_by_alt_adapters(pgp, adapters, total_paths)) {
242                 condlog(3, "Failed to re-order paths in pg by adapters "
243                         "and host ports");
244                 free_adaptergroup(adapters);
245                 /* return failure as original paths are
246                  * removed form pgp
247                  */
248                 return 1;
249         }
250
251         free_adaptergroup(adapters);
252         return 0;
253 }
254
255 extern int
256 setup_map (struct multipath * mpp, char * params, int params_size)
257 {
258         struct pathgroup * pgp;
259         int i;
260
261         /*
262          * don't bother if devmap size is unknown
263          */
264         if (mpp->size <= 0) {
265                 condlog(3, "%s: devmap size is unknown", mpp->alias);
266                 return 1;
267         }
268
269         /*
270          * free features, selector, and hwhandler properties if they are being reused
271          */
272         free_multipath_attributes(mpp);
273
274         /*
275          * properties selectors
276          */
277         select_pgfailback(mpp);
278         select_pgpolicy(mpp);
279         select_selector(mpp);
280         select_features(mpp);
281         select_hwhandler(mpp);
282         select_rr_weight(mpp);
283         select_minio(mpp);
284         select_no_path_retry(mpp);
285         select_mode(mpp);
286         select_uid(mpp);
287         select_gid(mpp);
288         select_fast_io_fail(mpp);
289         select_dev_loss(mpp);
290         select_reservation_key(mpp);
291         select_retain_hwhandler(mpp);
292         select_deferred_remove(mpp);
293         select_delay_watch_checks(mpp);
294         select_delay_wait_checks(mpp);
295
296         sysfs_set_scsi_tmo(mpp);
297         /*
298          * assign paths to path groups -- start with no groups and all paths
299          * in mpp->paths
300          */
301         if (mpp->pg) {
302                 vector_foreach_slot (mpp->pg, pgp, i)
303                         free_pathgroup(pgp, KEEP_PATHS);
304
305                 vector_free(mpp->pg);
306                 mpp->pg = NULL;
307         }
308         if (mpp->pgpolicyfn && mpp->pgpolicyfn(mpp))
309                 return 1;
310
311         mpp->nr_active = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
312
313         /*
314          * ponders each path group and determine highest prio pg
315          * to switch over (default to first)
316          */
317         mpp->bestpg = select_path_group(mpp);
318
319         /* re-order paths in all path groups in an optimized way
320          * for round-robin path selectors to get maximum throughput.
321          */
322         if (!strncmp(mpp->selector, "round-robin", 11)) {
323                 vector_foreach_slot(mpp->pg, pgp, i) {
324                         if (VECTOR_SIZE(pgp->paths) <= 2)
325                                 continue;
326                         if (rr_optimize_path_order(pgp)) {
327                                 condlog(2, "cannot re-order paths for "
328                                         "optimization: %s",
329                                         mpp->alias);
330                                 return 1;
331                         }
332                 }
333         }
334
335         /*
336          * transform the mp->pg vector of vectors of paths
337          * into a mp->params strings to feed the device-mapper
338          */
339         if (assemble_map(mpp, params, params_size)) {
340                 condlog(0, "%s: problem assembing map", mpp->alias);
341                 return 1;
342         }
343         return 0;
344 }
345
346 static void
347 compute_pgid(struct pathgroup * pgp)
348 {
349         struct path * pp;
350         int i;
351
352         vector_foreach_slot (pgp->paths, pp, i)
353                 pgp->id ^= (long)pp;
354 }
355
356 static int
357 pgcmp (struct multipath * mpp, struct multipath * cmpp)
358 {
359         int i, j;
360         struct pathgroup * pgp;
361         struct pathgroup * cpgp;
362         int r = 0;
363
364         if (!mpp)
365                 return 0;
366
367         vector_foreach_slot (mpp->pg, pgp, i) {
368                 compute_pgid(pgp);
369
370                 vector_foreach_slot (cmpp->pg, cpgp, j) {
371                         if (pgp->id == cpgp->id &&
372                             !pathcmp(pgp, cpgp)) {
373                                 r = 0;
374                                 break;
375                         }
376                         r++;
377                 }
378                 if (r)
379                         return r;
380         }
381         return r;
382 }
383
384 static void
385 select_action (struct multipath * mpp, vector curmp, int force_reload)
386 {
387         struct multipath * cmpp;
388         struct multipath * cmpp_by_name;
389
390         cmpp = find_mp_by_wwid(curmp, mpp->wwid);
391         cmpp_by_name = find_mp_by_alias(curmp, mpp->alias);
392
393         if (!cmpp_by_name) {
394                 if (cmpp) {
395                         condlog(2, "%s: rename %s to %s", mpp->wwid,
396                                 cmpp->alias, mpp->alias);
397                         strncpy(mpp->alias_old, cmpp->alias, WWID_SIZE);
398                         mpp->action = ACT_RENAME;
399                         if (force_reload)
400                                 mpp->action = ACT_RENAME2;
401                         return;
402                 }
403                 mpp->action = ACT_CREATE;
404                 condlog(3, "%s: set ACT_CREATE (map does not exist)",
405                         mpp->alias);
406                 return;
407         }
408
409         if (!cmpp) {
410                 condlog(2, "%s: remove (wwid changed)", mpp->alias);
411                 dm_flush_map(mpp->alias);
412                 strncpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE);
413                 drop_multipath(curmp, cmpp_by_name->wwid, KEEP_PATHS);
414                 mpp->action = ACT_CREATE;
415                 condlog(3, "%s: set ACT_CREATE (map wwid change)",
416                         mpp->alias);
417                 return;
418         }
419
420         if (cmpp != cmpp_by_name) {
421                 condlog(2, "%s: unable to rename %s to %s (%s is used by %s)",
422                         mpp->wwid, cmpp->alias, mpp->alias,
423                         mpp->alias, cmpp_by_name->wwid);
424                 mpp->action = ACT_NOTHING;
425                 return;
426         }
427
428         if (pathcount(mpp, PATH_UP) == 0) {
429                 mpp->action = ACT_NOTHING;
430                 condlog(3, "%s: set ACT_NOTHING (no usable path)",
431                         mpp->alias);
432                 return;
433         }
434         if (force_reload) {
435                 mpp->action = ACT_RELOAD;
436                 condlog(3, "%s: set ACT_RELOAD (forced by user)",
437                         mpp->alias);
438                 return;
439         }
440         if (cmpp->size != mpp->size) {
441                 mpp->action = ACT_RESIZE;
442                 condlog(3, "%s: set ACT_RESIZE (size change)",
443                         mpp->alias);
444                 return;
445         }
446         if (!mpp->no_path_retry &&
447             (strlen(cmpp->features) != strlen(mpp->features) ||
448              strcmp(cmpp->features, mpp->features))) {
449                 mpp->action =  ACT_RELOAD;
450                 condlog(3, "%s: set ACT_RELOAD (features change)",
451                         mpp->alias);
452                 return;
453         }
454         if (mpp->retain_hwhandler != RETAIN_HWHANDLER_ON &&
455             (strlen(cmpp->hwhandler) != strlen(mpp->hwhandler) ||
456              strncmp(cmpp->hwhandler, mpp->hwhandler,
457                     strlen(mpp->hwhandler)))) {
458                 mpp->action = ACT_RELOAD;
459                 condlog(3, "%s: set ACT_RELOAD (hwhandler change)",
460                         mpp->alias);
461                 return;
462         }
463         if (!cmpp->selector || strncmp(cmpp->selector, mpp->selector,
464                     strlen(mpp->selector))) {
465                 mpp->action = ACT_RELOAD;
466                 condlog(3, "%s: set ACT_RELOAD (selector change)",
467                         mpp->alias);
468                 return;
469         }
470         if (cmpp->minio != mpp->minio) {
471                 mpp->action = ACT_RELOAD;
472                 condlog(3, "%s: set ACT_RELOAD (minio change, %u->%u)",
473                         mpp->alias, cmpp->minio, mpp->minio);
474                 return;
475         }
476         if (!cmpp->pg || VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
477                 mpp->action = ACT_RELOAD;
478                 condlog(3, "%s: set ACT_RELOAD (path group number change)",
479                         mpp->alias);
480                 return;
481         }
482         if (pgcmp(mpp, cmpp)) {
483                 mpp->action = ACT_RELOAD;
484                 condlog(3, "%s: set ACT_RELOAD (path group topology change)",
485                         mpp->alias);
486                 return;
487         }
488         if (cmpp->nextpg != mpp->bestpg) {
489                 mpp->action = ACT_SWITCHPG;
490                 condlog(3, "%s: set ACT_SWITCHPG (next path group change)",
491                         mpp->alias);
492                 return;
493         }
494         mpp->action = ACT_NOTHING;
495         condlog(3, "%s: set ACT_NOTHING (map unchanged)",
496                 mpp->alias);
497         return;
498 }
499
500 extern int
501 reinstate_paths (struct multipath * mpp)
502 {
503         int i, j;
504         struct pathgroup * pgp;
505         struct path * pp;
506
507         if (!mpp->pg)
508                 return 0;
509
510         vector_foreach_slot (mpp->pg, pgp, i) {
511                 if (!pgp->paths)
512                         continue;
513
514                 vector_foreach_slot (pgp->paths, pp, j) {
515                         if (pp->state != PATH_UP &&
516                             (pgp->status == PGSTATE_DISABLED ||
517                              pgp->status == PGSTATE_ACTIVE))
518                                 continue;
519
520                         if (pp->dmstate == PSTATE_FAILED) {
521                                 if (dm_reinstate_path(mpp->alias, pp->dev_t))
522                                         condlog(0, "%s: error reinstating",
523                                                 pp->dev);
524                         }
525                 }
526         }
527         return 0;
528 }
529
530 static int
531 lock_multipath (struct multipath * mpp, int lock)
532 {
533         struct pathgroup * pgp;
534         struct path * pp;
535         int i, j;
536         int x, y;
537
538         if (!mpp || !mpp->pg)
539                 return 0;
540
541         vector_foreach_slot (mpp->pg, pgp, i) {
542                 if (!pgp->paths)
543                         continue;
544                 vector_foreach_slot(pgp->paths, pp, j) {
545                         if (lock && flock(pp->fd, LOCK_EX | LOCK_NB) &&
546                             errno == EWOULDBLOCK)
547                                 goto fail;
548                         else if (!lock)
549                                 flock(pp->fd, LOCK_UN);
550                 }
551         }
552         return 0;
553 fail:
554         vector_foreach_slot (mpp->pg, pgp, x) {
555                 if (x > i)
556                         return 1;
557                 if (!pgp->paths)
558                         continue;
559                 vector_foreach_slot(pgp->paths, pp, y) {
560                         if (x == i && y >= j)
561                                 return 1;
562                         flock(pp->fd, LOCK_UN);
563                 }
564         }
565         return 1;
566 }
567
568 /*
569  * Return value:
570  */
571 #define DOMAP_RETRY     -1
572 #define DOMAP_FAIL      0
573 #define DOMAP_OK        1
574 #define DOMAP_EXIST     2
575 #define DOMAP_DRY       3
576
577 extern int
578 domap (struct multipath * mpp, char * params)
579 {
580         int r = 0;
581
582         /*
583          * last chance to quit before touching the devmaps
584          */
585         if (conf->cmd == CMD_DRY_RUN && mpp->action != ACT_NOTHING) {
586                 print_multipath_topology(mpp, conf->verbosity);
587                 return DOMAP_DRY;
588         }
589
590         switch (mpp->action) {
591         case ACT_REJECT:
592         case ACT_NOTHING:
593                 return DOMAP_EXIST;
594
595         case ACT_SWITCHPG:
596                 dm_switchgroup(mpp->alias, mpp->bestpg);
597                 /*
598                  * we may have avoided reinstating paths because there where in
599                  * active or disabled PG. Now that the topology has changed,
600                  * retry.
601                  */
602                 reinstate_paths(mpp);
603                 return DOMAP_EXIST;
604
605         case ACT_CREATE:
606                 if (lock_multipath(mpp, 1)) {
607                         condlog(3, "%s: failed to create map (in use)",
608                                 mpp->alias);
609                         return DOMAP_RETRY;
610                 }
611
612                 if (dm_map_present(mpp->alias)) {
613                         condlog(3, "%s: map already present", mpp->alias);
614                         lock_multipath(mpp, 0);
615                         break;
616                 }
617
618                 r = dm_addmap_create(mpp, params);
619
620                 lock_multipath(mpp, 0);
621                 break;
622
623         case ACT_RELOAD:
624                 r = dm_addmap_reload(mpp, params);
625                 if (r)
626                         r = dm_simplecmd_noflush(DM_DEVICE_RESUME, mpp->alias,
627                                                  0, MPATH_UDEV_RELOAD_FLAG);
628                 break;
629
630         case ACT_RESIZE:
631                 r = dm_addmap_reload(mpp, params);
632                 if (r)
633                         r = dm_simplecmd_flush(DM_DEVICE_RESUME, mpp->alias, 1, 0);
634                 break;
635
636         case ACT_RENAME:
637                 r = dm_rename(mpp->alias_old, mpp->alias);
638                 break;
639
640         case ACT_RENAME2:
641                 r = dm_rename(mpp->alias_old, mpp->alias);
642                 if (r) {
643                         r = dm_addmap_reload(mpp, params);
644                         if (r)
645                                 r = dm_simplecmd_noflush(DM_DEVICE_RESUME, mpp->alias, 0, MPATH_UDEV_RELOAD_FLAG);
646                 }
647                 break;
648
649         default:
650                 break;
651         }
652
653         if (r) {
654                 /*
655                  * DM_DEVICE_CREATE, DM_DEVICE_RENAME, or DM_DEVICE_RELOAD
656                  * succeeded
657                  */
658                 if (mpp->action == ACT_CREATE)
659                         remember_wwid(mpp->wwid);
660                 if (!conf->daemon) {
661                         /* multipath client mode */
662                         dm_switchgroup(mpp->alias, mpp->bestpg);
663                 } else  {
664                         /* multipath daemon mode */
665                         mpp->stat_map_loads++;
666                         condlog(2, "%s: load table [0 %llu %s %s]", mpp->alias,
667                                 mpp->size, TGT_MPATH, params);
668                         /*
669                          * Required action is over, reset for the stateful daemon.
670                          * But don't do it for creation as we use in the caller the
671                          * mpp->action to figure out whether to start the watievent checker.
672                          */
673                         if (mpp->action != ACT_CREATE)
674                                 mpp->action = ACT_NOTHING;
675                 }
676                 dm_setgeometry(mpp);
677                 return DOMAP_OK;
678         }
679         return DOMAP_FAIL;
680 }
681
682 static int
683 deadmap (struct multipath * mpp)
684 {
685         int i, j;
686         struct pathgroup * pgp;
687         struct path * pp;
688
689         if (!mpp->pg)
690                 return 1;
691
692         vector_foreach_slot (mpp->pg, pgp, i) {
693                 if (!pgp->paths)
694                         continue;
695
696                 vector_foreach_slot (pgp->paths, pp, j)
697                         if (strlen(pp->dev))
698                                 return 0; /* alive */
699         }
700
701         return 1; /* dead */
702 }
703
704 int check_daemon(void)
705 {
706         int fd;
707         char *reply;
708         size_t len;
709         int ret = 0;
710
711         fd = ux_socket_connect(DEFAULT_SOCKET);
712         if (fd == -1)
713                 return 0;
714
715         if (send_packet(fd, "show daemon", 12) != 0)
716                 goto out;
717         if (recv_packet(fd, &reply, &len, DEFAULT_UXSOCK_TIMEOUT) != 0)
718                 goto out;
719
720         if (strstr(reply, "shutdown"))
721                 goto out_free;
722
723         ret = 1;
724
725 out_free:
726         FREE(reply);
727 out:
728         close(fd);
729         return ret;
730 }
731
732 extern int
733 coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid, int force_reload)
734 {
735         int r = 1;
736         int k, i;
737         char empty_buff[WWID_SIZE];
738         char params[PARAMS_SIZE];
739         struct multipath * mpp;
740         struct path * pp1;
741         struct path * pp2;
742         vector curmp = vecs->mpvec;
743         vector pathvec = vecs->pathvec;
744
745         memset(empty_buff, 0, WWID_SIZE);
746
747         /* ignore refwwid if it's empty */
748         if (refwwid && !strlen(refwwid))
749                 refwwid = NULL;
750
751         if (force_reload) {
752                 vector_foreach_slot (pathvec, pp1, k) {
753                         pp1->mpp = NULL;
754                 }
755         }
756         vector_foreach_slot (pathvec, pp1, k) {
757                 /* skip this path for some reason */
758
759                 /* 1. if path has no unique id or wwid blacklisted */
760                 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
761                     filter_path(conf, pp1) > 0) {
762                         orphan_path(pp1, "wwid blacklisted");
763                         continue;
764                 }
765
766                 /* 2. if path already coalesced */
767                 if (pp1->mpp)
768                         continue;
769
770                 /* 3. if path has disappeared */
771                 if (!pp1->size) {
772                         orphan_path(pp1, "invalid size");
773                         continue;
774                 }
775
776                 /* 4. path is out of scope */
777                 if (refwwid && strncmp(pp1->wwid, refwwid, WWID_SIZE))
778                         continue;
779
780                 /* If find_multipaths was selected check if the path is valid */
781                 if (conf->find_multipaths && !refwwid &&
782                     !should_multipath(pp1, pathvec)) {
783                         orphan_path(pp1, "only one path");
784                         continue;
785                 }
786
787                 /*
788                  * at this point, we know we really got a new mp
789                  */
790                 mpp = add_map_with_path(vecs, pp1, 0);
791                 if (!mpp)
792                         return 1;
793
794                 if (pp1->priority == PRIO_UNDEF)
795                         mpp->action = ACT_REJECT;
796
797                 if (!mpp->paths) {
798                         condlog(0, "%s: skip coalesce (no paths)", mpp->alias);
799                         remove_map(mpp, vecs, 0);
800                         continue;
801                 }
802
803                 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
804                         pp2 = VECTOR_SLOT(pathvec, i);
805
806                         if (strcmp(pp1->wwid, pp2->wwid))
807                                 continue;
808
809                         if (!pp2->size)
810                                 continue;
811
812                         if (pp2->size != mpp->size) {
813                                 /*
814                                  * ouch, avoid feeding that to the DM
815                                  */
816                                 condlog(0, "%s: size %llu, expected %llu. "
817                                         "Discard", pp2->dev_t, pp2->size,
818                                         mpp->size);
819                                 mpp->action = ACT_REJECT;
820                         }
821                         if (pp2->priority == PRIO_UNDEF)
822                                 mpp->action = ACT_REJECT;
823                 }
824                 verify_paths(mpp, vecs);
825
826                 params[0] = '\0';
827                 if (setup_map(mpp, params, PARAMS_SIZE)) {
828                         remove_map(mpp, vecs, 0);
829                         continue;
830                 }
831
832                 if (mpp->action == ACT_UNDEF)
833                         select_action(mpp, curmp, force_reload);
834
835                 r = domap(mpp, params);
836
837                 if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
838                         condlog(3, "%s: domap (%u) failure "
839                                    "for create/reload map",
840                                 mpp->alias, r);
841                         if (r == DOMAP_FAIL) {
842                                 condlog(2, "%s: %s map",
843                                         mpp->alias, (mpp->action == ACT_CREATE)?
844                                         "ignoring" : "removing");
845                                 remove_map(mpp, vecs, 0);
846                                 continue;
847                         } else /* if (r == DOMAP_RETRY) */
848                                 return r;
849                 }
850                 if (r == DOMAP_DRY)
851                         continue;
852
853                 if (!conf->daemon && !conf->allow_queueing && !check_daemon()) {
854                         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF &&
855                             mpp->no_path_retry != NO_PATH_RETRY_FAIL)
856                                 condlog(3, "%s: multipathd not running, unset "
857                                         "queue_if_no_path feature", mpp->alias);
858                         if (!dm_queue_if_no_path(mpp->alias, 0))
859                                 remove_feature(&mpp->features,
860                                                "queue_if_no_path");
861                 }
862                 else if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF) {
863                         if (mpp->no_path_retry == NO_PATH_RETRY_FAIL) {
864                                 condlog(3, "%s: unset queue_if_no_path feature",
865                                         mpp->alias);
866                                 if (!dm_queue_if_no_path(mpp->alias, 0))
867                                         remove_feature(&mpp->features,
868                                                        "queue_if_no_path");
869                         } else {
870                                 condlog(3, "%s: set queue_if_no_path feature",
871                                         mpp->alias);
872                                 if (!dm_queue_if_no_path(mpp->alias, 1))
873                                         add_feature(&mpp->features,
874                                                     "queue_if_no_path");
875                         }
876                 }
877
878                 if (!conf->daemon && mpp->action != ACT_NOTHING)
879                         print_multipath_topology(mpp, conf->verbosity);
880
881                 if (newmp) {
882                         if (mpp->action != ACT_REJECT) {
883                                 if (!vector_alloc_slot(newmp))
884                                         return 1;
885                                 vector_set_slot(newmp, mpp);
886                         }
887                         else
888                                 remove_map(mpp, vecs, 0);
889                 }
890         }
891         /*
892          * Flush maps with only dead paths (ie not in sysfs)
893          * Keep maps with only failed paths
894          */
895         if (newmp) {
896                 vector_foreach_slot (newmp, mpp, i) {
897                         char alias[WWID_SIZE];
898                         int j;
899
900                         if (!deadmap(mpp))
901                                 continue;
902
903                         strncpy(alias, mpp->alias, WWID_SIZE);
904
905                         if ((j = find_slot(newmp, (void *)mpp)) != -1)
906                                 vector_del_slot(newmp, j);
907
908                         remove_map(mpp, vecs, 0);
909
910                         if (dm_flush_map(alias))
911                                 condlog(2, "%s: remove failed (dead)",
912                                         alias);
913                         else
914                                 condlog(2, "%s: remove (dead)", alias);
915                 }
916         }
917         return 0;
918 }
919
920 /*
921  * returns:
922  * 0 - success
923  * 1 - failure
924  * 2 - blacklist
925  */
926 extern int
927 get_refwwid (char * dev, enum devtypes dev_type, vector pathvec, char **wwid)
928 {
929         int ret = 1;
930         struct path * pp;
931         char buff[FILE_NAME_SIZE];
932         char * refwwid = NULL, tmpwwid[WWID_SIZE];
933
934         if (!wwid)
935                 return 1;
936         *wwid = NULL;
937
938         if (dev_type == DEV_NONE)
939                 return 1;
940
941         if (dev_type == DEV_DEVNODE) {
942                 if (basenamecpy(dev, buff, FILE_NAME_SIZE) == 0) {
943                         condlog(1, "basename failed for '%s' (%s)",
944                                 dev, buff);
945                         return 1;
946                 }
947
948                 pp = find_path_by_dev(pathvec, buff);
949                 if (!pp) {
950                         struct udev_device *udevice = udev_device_new_from_subsystem_sysname(conf->udev, "block", buff);
951
952                         if (!udevice) {
953                                 condlog(2, "%s: can't get udev device", buff);
954                                 return 1;
955                         }
956                         ret = store_pathinfo(pathvec, conf->hwtable, udevice,
957                                              DI_SYSFS | DI_WWID, &pp);
958                         udev_device_unref(udevice);
959                         if (!pp) {
960                                 if (ret == 1)
961                                         condlog(0, "%s: can't store path info",
962                                                 dev);
963                                 return ret;
964                         }
965                 }
966                 if (pp->udev && filter_property(conf, pp->udev) > 0)
967                         return 2;
968
969                 refwwid = pp->wwid;
970                 goto out;
971         }
972
973         if (dev_type == DEV_DEVT) {
974                 strchop(dev);
975                 pp = find_path_by_devt(pathvec, dev);
976                 if (!pp) {
977                         struct udev_device *udevice = udev_device_new_from_devnum(conf->udev, 'b', parse_devt(dev));
978
979                         if (!udevice) {
980                                 condlog(2, "%s: can't get udev device", dev);
981                                 return 1;
982                         }
983                         ret = store_pathinfo(pathvec, conf->hwtable, udevice,
984                                              DI_SYSFS | DI_WWID, &pp);
985                         udev_device_unref(udevice);
986                         if (!pp) {
987                                 if (ret == 1)
988                                         condlog(0, "%s can't store path info",
989                                                 buff);
990                                 return ret;
991                         }
992                 }
993                 if (pp->udev && filter_property(conf, pp->udev) > 0)
994                         return 2;
995
996                 refwwid = pp->wwid;
997                 goto out;
998         }
999
1000         if (dev_type == DEV_UEVENT) {
1001                 struct udev_device *udevice = udev_device_new_from_environment(conf->udev);
1002
1003                 if (!udevice) {
1004                         condlog(2, "%s: can't get udev device", dev);
1005                         return 1;
1006                 }
1007                 ret = store_pathinfo(pathvec, conf->hwtable, udevice,
1008                                      DI_SYSFS | DI_WWID, &pp);
1009                 udev_device_unref(udevice);
1010                 if (!pp) {
1011                         if (ret == 1)
1012                                 condlog(0, "%s: can't store path info",
1013                                         dev);
1014                         return ret;
1015                 }
1016                 if (pp->udev && filter_property(conf, pp->udev) > 0)
1017                         return 2;
1018
1019                 refwwid = pp->wwid;
1020                 goto out;
1021         }
1022
1023         if (dev_type == DEV_DEVMAP) {
1024
1025                 if (((dm_get_uuid(dev, tmpwwid)) == 0) && (strlen(tmpwwid))) {
1026                         refwwid = tmpwwid;
1027                         goto check;
1028                 }
1029
1030                 /*
1031                  * may be a binding
1032                  */
1033                 if (get_user_friendly_wwid(dev, tmpwwid,
1034                                            conf->bindings_file) == 0) {
1035                         refwwid = tmpwwid;
1036                         goto check;
1037                 }
1038
1039                 /*
1040                  * or may be an alias
1041                  */
1042                 refwwid = get_mpe_wwid(dev);
1043
1044                 /*
1045                  * or directly a wwid
1046                  */
1047                 if (!refwwid)
1048                         refwwid = dev;
1049
1050 check:
1051                 if (refwwid && strlen(refwwid)) {
1052                         if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
1053                                         refwwid, NULL) > 0)
1054                         return 2;
1055                 }
1056         }
1057 out:
1058         if (refwwid && strlen(refwwid)) {
1059                 *wwid = STRDUP(refwwid);
1060                 return 0;
1061         }
1062
1063         return 1;
1064 }
1065
1066 extern int reload_map(struct vectors *vecs, struct multipath *mpp, int refresh)
1067 {
1068         char params[PARAMS_SIZE] = {0};
1069         struct path *pp;
1070         int i, r;
1071
1072         update_mpp_paths(mpp, vecs->pathvec);
1073         if (refresh) {
1074                 vector_foreach_slot (mpp->paths, pp, i) {
1075                         r = pathinfo(pp, conf->hwtable, DI_PRIO);
1076                         if (r) {
1077                                 condlog(2, "%s: failed to refresh pathinfo",
1078                                         mpp->alias);
1079                                 return 1;
1080                         }
1081                 }
1082         }
1083         if (setup_map(mpp, params, PARAMS_SIZE)) {
1084                 condlog(0, "%s: failed to setup map", mpp->alias);
1085                 return 1;
1086         }
1087         select_action(mpp, vecs->mpvec, 1);
1088
1089         r = domap(mpp, params);
1090         if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
1091                 condlog(3, "%s: domap (%u) failure "
1092                         "for reload map", mpp->alias, r);
1093                 return 1;
1094         }
1095         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF) {
1096                 if (mpp->no_path_retry == NO_PATH_RETRY_FAIL)
1097                         dm_queue_if_no_path(mpp->alias, 0);
1098                 else
1099                         dm_queue_if_no_path(mpp->alias, 1);
1100         }
1101
1102         return 0;
1103 }