[multipath] set devmap UUID to multipath WWID
[platform/upstream/multipath-tools.git] / multipath / main.c
1 /*
2  * Soft:        multipath device mapper target autoconfig
3  *
4  * Version:     $Id: main.h,v 0.0.1 2003/09/18 15:13:38 cvaroqui Exp $
5  *
6  * Author:      Copyright (C) 2003 Christophe Varoqui
7  *
8  *              This program is distributed in the hope that it will be useful,
9  *              but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *              See the GNU General Public License for more details.
12  *
13  *              This program is free software; you can redistribute it and/or
14  *              modify it under the terms of the GNU General Public License
15  *              as published by the Free Software Foundation; either version
16  *              2 of the License, or (at your option) any later version.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23
24 #include <parser.h>
25 #include <vector.h>
26 #include <memory.h>
27 #include <libdevmapper.h>
28 #include <devmapper.h>
29 #include <checkers.h>
30 #include <path_state.h>
31 #include <blacklist.h>
32 #include <hwtable.h>
33 #include <util.h>
34 #include <defaults.h>
35 #include <structs.h>
36 #include <dmparser.h>
37 #include <cache.h>
38 #include <config.h>
39 #include <propsel.h>
40 #include <discovery.h>
41 #include <debug.h>
42 #include <switchgroup.h>
43 #include <sysfs/libsysfs.h>
44 #include <print.h>
45
46 #include "main.h"
47 #include "pgpolicies.h"
48 #include "dict.h"
49
50 /* for column aligned output */
51 struct path_layout pl;
52
53 static char *
54 get_refwwid (vector pathvec)
55 {
56         struct path * pp;
57         char buff[FILE_NAME_SIZE];
58         char * refwwid;
59
60         if (conf->dev_type == DEV_NONE)
61                 return NULL;
62
63         if (conf->dev_type == DEV_DEVNODE) {
64                 condlog(3, "limited scope = %s", conf->dev);
65                 basename(conf->dev, buff);
66                 pp = find_path_by_dev(pathvec, buff);
67                 
68                 if (!pp) {
69                         pp = alloc_path();
70
71                         if (!pp)
72                                 return NULL;
73
74                         strncpy(pp->dev, buff, FILE_NAME_SIZE);
75
76                         if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
77                                 return NULL;
78
79                         if (store_path(pathvec, pp)) {
80                                 free_path(pp);
81                                 return NULL;
82                         }
83                 }
84
85                 refwwid = MALLOC(WWID_SIZE);
86
87                 if (!refwwid)
88                         return NULL;
89
90                 memcpy(refwwid, pp->wwid, WWID_SIZE);
91                 return refwwid;
92         }
93
94         if (conf->dev_type == DEV_DEVT) {
95                 condlog(3, "limited scope = %s", conf->dev);
96                 pp = find_path_by_devt(pathvec, conf->dev);
97                 
98                 if (!pp) {
99                         if (devt2devname(conf->dev, buff))
100                                 return NULL;
101
102                         pp = alloc_path();
103
104                         if (!pp)
105                                 return NULL;
106
107                         if(safe_sprintf(pp->dev, "%s", buff)) {
108                                 fprintf(stderr, "pp->dev too small\n");
109                                 exit(1);
110                         }
111                         if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
112                                 return NULL;
113                         
114                         if (store_path(pathvec, pp)) {
115                                 free_path(pp);
116                                 return NULL;
117                         }
118                 }
119
120                 refwwid = MALLOC(WWID_SIZE);
121
122                 if (!refwwid)
123                         return NULL;
124                 
125                 memcpy(refwwid, pp->wwid, WWID_SIZE);
126                 return refwwid;
127         }
128         if (conf->dev_type == DEV_DEVMAP) {
129                 condlog(3, "limited scope = %s", conf->dev);
130                 /*
131                  * may be an alias
132                  */
133                 refwwid = get_mpe_wwid(conf->dev);
134
135                 if (refwwid)
136                         return refwwid;
137                 
138                 /*
139                  * or directly a wwid
140                  */
141                 refwwid = MALLOC(WWID_SIZE);
142
143                 if (!refwwid)
144                         return NULL;
145
146                 strncpy(refwwid, conf->dev, WWID_SIZE);
147                 return refwwid;
148         }
149         return NULL;
150 }
151
152 static void
153 print_path (struct path * pp, char * style)
154 {
155         char buff[MAX_LINE_LEN];
156
157         snprint_path(&buff[0], MAX_LINE_LEN, style, pp, &pl);
158         printf("%s", buff);
159 }
160
161 static void
162 print_map (struct multipath * mpp)
163 {
164         if (mpp->size && mpp->params)
165                 printf("0 %llu %s %s\n",
166                          mpp->size, DEFAULT_TARGET, mpp->params);
167         return;
168 }
169
170 static void
171 print_all_paths (vector pathvec)
172 {
173         int i;
174         struct path * pp;
175
176         vector_foreach_slot (pathvec, pp, i)
177                 print_path(pp, PRINT_PATH_LONG);
178 }
179
180 static void
181 print_mp (struct multipath * mpp)
182 {
183         int j, i;
184         struct path * pp = NULL;
185         struct pathgroup * pgp = NULL;
186
187         if (mpp->action == ACT_NOTHING || conf->verbosity == 0)
188                 return;
189
190         if (conf->verbosity > 1) {
191                 switch (mpp->action) {
192                 case ACT_RELOAD:
193                         printf("%s: ", ACT_RELOAD_STR);
194                         break;
195
196                 case ACT_CREATE:
197                         printf("%s: ", ACT_CREATE_STR);
198                         break;
199
200                 case ACT_SWITCHPG:
201                         printf("%s: ", ACT_SWITCHPG_STR);
202                         break;
203
204                 default:
205                         break;
206                 }
207         }
208
209         if (mpp->alias)
210                 printf("%s", mpp->alias);
211
212         if (conf->verbosity == 1) {
213                 printf("\n");
214                 return;
215         }
216         if (strncmp(mpp->alias, mpp->wwid, WWID_SIZE))
217                 printf(" (%s)", mpp->wwid);
218
219         printf("\n");
220
221         if (mpp->size < (1 << 11))
222                 printf("[size=%llu kB]", mpp->size >> 1);
223         else if (mpp->size < (1 << 21))
224                 printf("[size=%llu MB]", mpp->size >> 11);
225         else if (mpp->size < (1 << 31))
226                 printf("[size=%llu GB]", mpp->size >> 21);
227         else
228                 printf("[size=%llu TB]", mpp->size >> 31);
229
230         if (mpp->features)
231                 printf("[features=\"%s\"]", mpp->features);
232
233         if (mpp->hwhandler)
234                 printf("[hwhandler=\"%s\"]", mpp->hwhandler);
235
236         fprintf(stdout, "\n");
237
238         if (!mpp->pg)
239                 return;
240
241         vector_foreach_slot (mpp->pg, pgp, j) {
242                 printf("\\_ ");
243
244                 if (mpp->selector) {
245                         printf("%s ", mpp->selector);
246 #if 0
247                         /* align to path status info */
248                         for (i = pl.hbtl_len + pl.dev_len + pl.dev_t_len + 4;
249                              i > strlen(mpp->selector); i--)
250                                 printf(" ");
251 #endif
252                 }
253                 if (pgp->priority)
254                         printf("[prio=%i]", pgp->priority);
255
256                 switch (pgp->status) {
257                 case PGSTATE_ENABLED:
258                         printf("[enabled]");
259                         break;
260                 case PGSTATE_DISABLED:
261                         printf("[disabled]");
262                         break;
263                 case PGSTATE_ACTIVE:
264                         printf("[active]");
265                         break;
266                 default:
267                         break;
268                 }
269                 printf("\n");
270
271                 vector_foreach_slot (pgp->paths, pp, i)
272                         print_path(pp, PRINT_PATH_INDENT);
273         }
274         printf("\n");
275 }
276
277 static int
278 filter_pathvec (vector pathvec, char * refwwid)
279 {
280         int i;
281         struct path * pp;
282
283         if (!refwwid || !strlen(refwwid))
284                 return 0;
285
286         vector_foreach_slot (pathvec, pp, i) {
287                 if (memcmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
288                         condlog(3, "skip path %s : out of scope", pp->dev);
289                         free_path(pp);
290                         vector_del_slot(pathvec, i);
291                         i--;
292                 }
293         }
294         return 0;
295 }
296
297 /*
298  * Transforms the path group vector into a proper device map string
299  */
300 int
301 assemble_map (struct multipath * mp)
302 {
303         int i, j;
304         int shift, freechar;
305         int minio;
306         char * p;
307         struct pathgroup * pgp;
308         struct path * pp;
309
310         p = mp->params;
311         freechar = sizeof(mp->params);
312         
313         shift = snprintf(p, freechar, "%s %s %i %i",
314                          mp->features, mp->hwhandler,
315                          VECTOR_SIZE(mp->pg), mp->nextpg);
316
317         if (shift >= freechar) {
318                 fprintf(stderr, "mp->params too small\n");
319                 return 1;
320         }
321         p += shift;
322         freechar -= shift;
323         
324         vector_foreach_slot (mp->pg, pgp, i) {
325                 pgp = VECTOR_SLOT(mp->pg, i);
326                 shift = snprintf(p, freechar, " %s %i 1", mp->selector,
327                                  VECTOR_SIZE(pgp->paths));
328                 if (shift >= freechar) {
329                         fprintf(stderr, "mp->params too small\n");
330                         return 1;
331                 }
332                 p += shift;
333                 freechar -= shift;
334
335                 vector_foreach_slot (pgp->paths, pp, j) {
336                         minio = conf->minio;
337                         
338                         if (mp->rr_weight == RR_WEIGHT_PRIO && pp->priority)
339                                 minio *= pp->priority;
340
341                         shift = snprintf(p, freechar, " %s %d",
342                                          pp->dev_t, minio);
343                         if (shift >= freechar) {
344                                 fprintf(stderr, "mp->params too small\n");
345                                 return 1;
346                         }
347                         p += shift;
348                         freechar -= shift;
349                 }
350         }
351         if (freechar < 1) {
352                 fprintf(stderr, "mp->params too small\n");
353                 return 1;
354         }
355         snprintf(p, 1, "\n");
356
357         if (conf->verbosity > 2)
358                 print_map(mp);
359
360         return 0;
361 }
362
363 static int
364 setup_map (struct multipath * mpp)
365 {
366         struct path * pp;
367         int i;
368
369         /*
370          * don't bother if devmap size is unknown
371          */
372         if (mpp->size <= 0) {
373                 condlog(3, "%s devmap size is unknown", mpp->alias);
374                 return 1;
375         }
376
377         /*
378          * don't bother if a constituant path is claimed
379          * (not by the device mapper driver)
380          */
381         vector_foreach_slot (mpp->paths, pp, i) {
382                 if (pp->claimed && pp->dmstate == PSTATE_UNDEF) {
383                         condlog(3, "%s claimed", pp->dev);
384                         return 1;
385                 }
386         }
387
388         /*
389          * properties selectors
390          */
391         select_pgpolicy(mpp);
392         select_selector(mpp);
393         select_features(mpp);
394         select_hwhandler(mpp);
395         select_rr_weight(mpp);
396
397         /*
398          * apply selected grouping policy to valid paths
399          */
400         switch (mpp->pgpolicy) {
401         case MULTIBUS:
402                 one_group(mpp);
403                 break;
404         case FAILOVER:
405                 one_path_per_group(mpp);
406                 break;
407         case GROUP_BY_SERIAL:
408                 group_by_serial(mpp);
409                 break;
410         case GROUP_BY_PRIO:
411                 group_by_prio(mpp);
412                 break;
413         case GROUP_BY_NODE_NAME:
414                 group_by_node_name(mpp);
415                 break;
416         default:
417                 break;
418         }
419
420         if (mpp->pg == NULL) {
421                 condlog(3, "pgpolicy failed to produce a pg vector");
422                 return 1;
423         }
424
425         /*
426          * ponders each path group and determine highest prio pg
427          * to switch over (default to first)
428          */
429         select_path_group(mpp);
430
431         /*
432          * transform the mp->pg vector of vectors of paths
433          * into a mp->params strings to feed the device-mapper
434          */
435         if (assemble_map(mpp)) {
436                 condlog(3, "problem assembing map");
437                 return 1;
438         }
439         return 0;
440 }
441
442 static int
443 pathcount (struct multipath * mpp, int state)
444 {
445         struct pathgroup *pgp;
446         struct path *pp;
447         int i, j;
448         int count = 0;
449
450         vector_foreach_slot (mpp->pg, pgp, i)
451                 vector_foreach_slot (pgp->paths, pp, j)
452                         if (pp->state == state)
453                                 count++;
454         return count;
455 }
456
457 static void
458 compute_pgid(struct pathgroup * pgp)
459 {
460         struct path * pp;
461         int i;
462
463         vector_foreach_slot (pgp->paths, pp, i)
464                 pgp->id ^= (long)pp;
465 }
466
467 static int
468 pgcmp (struct multipath * mpp, struct multipath * cmpp)
469 {
470         int i, j;
471         struct pathgroup * pgp;
472         struct pathgroup * cpgp;
473         int r = 0;
474
475         vector_foreach_slot (mpp->pg, pgp, i) {
476                 compute_pgid(pgp);
477
478                 vector_foreach_slot (cmpp->pg, cpgp, j) {
479                         if (pgp->id == cpgp->id) {
480                                 r = 0;
481                                 break;
482                         }
483                         r++;
484                 }
485                 if (r)
486                         return r;
487         }
488         return r;
489 }
490
491 static void
492 select_action (struct multipath * mpp, vector curmp)
493 {
494         struct multipath * cmpp;
495
496         cmpp = find_mp(curmp, mpp->alias);
497
498         if (!cmpp) {
499                 cmpp = find_mp_by_wwid(curmp, mpp->wwid);
500
501                 if (cmpp && !conf->dry_run) {
502                         condlog(2, "remove: %s (dup of %s)",
503                                 cmpp->alias, mpp->alias);
504                         dm_flush_map(cmpp->alias, DEFAULT_TARGET);
505                 }
506                 mpp->action = ACT_CREATE;
507                 return;
508         }
509         if (pathcount(mpp, PATH_UP) == 0) {
510                 condlog(3, "no good path");
511                 mpp->action = ACT_NOTHING;
512                 return;
513         }
514         if (cmpp->size != mpp->size) {
515                 condlog(3, "size different than current");
516                 mpp->action = ACT_RELOAD;
517                 return;
518         }
519         if (strncmp(cmpp->features, mpp->features,
520                     strlen(mpp->features))) {
521                 condlog(3, "features different than current");
522                 mpp->action =  ACT_RELOAD;
523                 return;
524         }
525         if (strncmp(cmpp->hwhandler, mpp->hwhandler,
526                     strlen(mpp->hwhandler))) {
527                 condlog(3, "hwhandler different than current");
528                 mpp->action = ACT_RELOAD;
529                 return;
530         }
531         if (strncmp(cmpp->selector, mpp->selector,
532                     strlen(mpp->selector))) {
533                 condlog(3, "selector different than current");
534                 mpp->action = ACT_RELOAD;
535                 return;
536         }
537         if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
538                 condlog(3, "different number of PG");
539                 mpp->action = ACT_RELOAD;
540                 return;
541         }
542         if (pgcmp(mpp, cmpp)) {
543                 condlog(3, "different path group topology");
544                 mpp->action = ACT_RELOAD;
545                 return;
546         }
547         if (cmpp->nextpg != mpp->nextpg) {
548                 condlog(3, "nextpg different than current");
549                 mpp->action = ACT_SWITCHPG;
550                 return;
551         }
552         mpp->action = ACT_NOTHING;
553         return;
554 }
555
556 static int
557 reinstate_paths (struct multipath * mpp)
558 {
559         int i, j;
560         struct pathgroup * pgp;
561         struct path * pp;
562
563         vector_foreach_slot (mpp->pg, pgp, i) {
564                 vector_foreach_slot (pgp->paths, pp, j) {
565                         if (pp->state != PATH_UP &&
566                             (pgp->status == PGSTATE_DISABLED ||
567                              pgp->status == PGSTATE_ACTIVE))
568                                 continue;
569
570                         if (pp->dmstate == PSTATE_FAILED) {
571                                 if (dm_reinstate(mpp->alias, pp->dev_t))
572                                         condlog(0, "error reinstating %s",
573                                                 pp->dev);
574                         }
575                 }
576         }
577         return 0;
578 }
579
580 static int
581 domap (struct multipath * mpp)
582 {
583         int op = ACT_NOTHING;
584         int r = 0;
585
586         print_mp(mpp);
587
588         /*
589          * last chance to quit before touching the devmaps
590          */
591         if (conf->dry_run)
592                 return 0;
593
594         switch (mpp->action) {
595         case ACT_NOTHING:
596                 return 0;
597
598         case ACT_SWITCHPG:
599                 dm_switchgroup(mpp->alias, mpp->nextpg);
600                 /*
601                  * we may have avoided reinstating paths because there where in
602                  * active or disabled PG. Now that the topology has changed,
603                  * retry.
604                  */
605                 reinstate_paths(mpp);
606                 return 0;
607
608         case ACT_CREATE:
609                 op = DM_DEVICE_CREATE;
610                 break;
611
612         case ACT_RELOAD:
613                 op = DM_DEVICE_RELOAD;
614                 break;
615
616         default:
617                 break;
618         }
619
620                 
621         /*
622          * device mapper creation or updating
623          * here we know we'll have garbage on stderr from
624          * libdevmapper. so shut it down temporarily.
625          */
626         dm_log_init_verbose(0);
627
628         r = dm_addmap(op, mpp->alias, DEFAULT_TARGET, mpp->params, mpp->size, mpp->wwid);
629
630         if (r == 0)
631                 dm_simplecmd(DM_DEVICE_REMOVE, mpp->alias);
632         else if (op == DM_DEVICE_RELOAD)
633                 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias);
634
635         /*
636          * PG order is random, so we need to set the primary one
637          * upon create or reload
638          */
639         dm_switchgroup(mpp->alias, mpp->nextpg);
640
641         dm_log_init_verbose(1);
642
643         return r;
644 }
645
646 static int
647 coalesce_paths (vector curmp, vector pathvec)
648 {
649         int k, i;
650         char empty_buff[WWID_SIZE];
651         struct multipath * mpp;
652         struct path * pp1;
653         struct path * pp2;
654
655         memset(empty_buff, 0, WWID_SIZE);
656
657         vector_foreach_slot (pathvec, pp1, k) {
658                 /* skip this path for some reason */
659
660                 /* 1. if path has no unique id or wwid blacklisted */
661                 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
662                     blacklist(conf->blist, pp1->wwid))
663                         continue;
664
665                 /* 2. if path already coalesced */
666                 if (pp1->mpp)
667                         continue;
668
669                 /*
670                  * at this point, we know we really got a new mp
671                  */
672                 mpp = alloc_multipath();
673
674                 if (!mpp)
675                         return 1;
676
677                 mpp->mpe = find_mpe(pp1->wwid);
678                 mpp->hwe = pp1->hwe;
679                 select_alias(mpp);
680
681                 pp1->mpp = mpp;
682                 strcpy(mpp->wwid, pp1->wwid);
683                 mpp->size = pp1->size;
684                 mpp->paths = vector_alloc();
685
686                 if (pp1->priority < 0)
687                         mpp->action = ACT_NOTHING;
688
689                 if (!mpp->paths)
690                         return 1;
691                 
692                 if (store_path(mpp->paths, pp1))
693                         return 1;
694
695                 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
696                         pp2 = VECTOR_SLOT(pathvec, i);
697
698                         if (strcmp(pp1->wwid, pp2->wwid))
699                                 continue;
700                         
701                         pp2->mpp = mpp;
702
703                         if (pp2->size != mpp->size) {
704                                 /*
705                                  * ouch, avoid feeding that to the DM
706                                  */
707                                 condlog(3, "path size mismatch : discard %s",
708                                      mpp->wwid);
709                                 mpp->action = ACT_NOTHING;
710                         }
711                         if (pp2->priority < 0)
712                                 mpp->action = ACT_NOTHING;
713
714                         if (store_path(mpp->paths, pp2))
715                                 return 1;
716                 }
717                 if (setup_map(mpp)) {
718                         free_multipath(mpp, KEEP_PATHS);
719                         continue;
720                 }
721                 condlog(3, "action preset to %i", mpp->action);
722
723                 if (mpp->action == ACT_UNDEF)
724                         select_action(mpp, curmp);
725
726                 condlog(3, "action set to %i", mpp->action);
727
728                 domap(mpp);
729                 free_multipath(mpp, KEEP_PATHS);
730         }
731         return 0;
732 }
733
734 static void
735 usage (char * progname)
736 {
737         fprintf (stderr, VERSION_STRING);
738         fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
739                 progname);
740         fprintf (stderr,
741                 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
742                 "\t\t\t[device]\n" \
743                 "\n" \
744                 "\t-v level\tverbosty level\n" \
745                 "\t   0\t\t\tno output\n" \
746                 "\t   1\t\t\tprint created devmap names only\n" \
747                 "\t   2\t\t\tdefault verbosity\n" \
748                 "\t   3\t\t\tprint debug information\n" \
749                 "\t-d\t\tdry run, do not create or update devmaps\n" \
750                 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
751                 "\t-ll\t\tshow multipath topology (maximum info)\n" \
752                 "\t-F\t\tflush a multipath device map\n" \
753                 "\t-F\t\tflush all multipath device maps\n" \
754                 "\t-p policy\tforce all maps to specified policy :\n" \
755                 "\t   failover\t\t1 path per priority group\n" \
756                 "\t   multibus\t\tall paths in 1 priority group\n" \
757                 "\t   group_by_serial\t1 priority group per serial\n" \
758                 "\t   group_by_prio\t1 priority group per priority lvl\n" \
759                 "\t   group_by_node_name\t1 priority group per target node\n" \
760                 "\n" \
761                 "\tdevice\t\tlimit scope to the device's multipath\n" \
762                 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
763                 "\t\t\tor major:minor or a device map name)\n" \
764                 );
765
766         exit(1);
767 }
768
769 static int
770 update_paths (struct multipath * mpp)
771 {
772         int i, j;
773         struct pathgroup * pgp;
774         struct path * pp;
775
776         vector_foreach_slot (mpp->pg, pgp, i) {
777                 vector_foreach_slot (pgp->paths, pp, j) {
778                         if (!strlen(pp->dev)) {
779                                 if (devt2devname(pp->dev_t, pp->dev)) {
780                                         /*
781                                          * path is not in sysfs anymore
782                                          */
783                                         pp->state = PATH_DOWN;
784                                         continue;
785                                 }
786                                 pathinfo(pp, conf->hwtable,
787                                          DI_SYSFS | DI_CHECKER | \
788                                          DI_SERIAL | DI_PRIO);
789                                 continue;
790                         }
791                         if (pp->state == PATH_UNCHECKED)
792                                 pathinfo(pp, conf->hwtable, DI_CHECKER);
793
794                         if (!pp->priority)
795                                 pathinfo(pp, conf->hwtable, DI_PRIO);
796                 }
797         }
798         return 0;
799 }
800
801 static int
802 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
803 {
804         int i;
805         struct multipath * mpp;
806         char * wwid;
807
808         if (dm_get_maps(curmp, DEFAULT_TARGET))
809                 return 1;
810
811         vector_foreach_slot (curmp, mpp, i) {
812                 wwid = get_mpe_wwid(mpp->alias);
813
814                 if (wwid) {
815                         /* out of specified scope */
816                         if (refwwid && strncmp(wwid, refwwid, WWID_SIZE))
817                                 continue;
818                         wwid = NULL;
819                 }
820
821                 condlog(3, "params = %s", mpp->params);
822                 condlog(3, "status = %s", mpp->status);
823
824                 /* will set mpp->wwid */
825                 disassemble_map(pathvec, mpp->params, mpp);
826
827                 /*
828                  * disassemble_map may have added new paths to pathvec.
829                  * If not in "fast list mode", we need to fetch information
830                  * about them
831                  */
832                 if (conf->list != 1)
833                         update_paths(mpp);
834
835                 if (conf->list > 1)
836                         select_path_group(mpp);
837
838                 disassemble_status(mpp->status, mpp);
839
840                 if (conf->list)
841                         print_mp(mpp);
842
843                 if (!conf->dry_run)
844                         reinstate_paths(mpp);
845         }
846         return 0;
847 }
848
849 int
850 main (int argc, char *argv[])
851 {
852         vector curmp = NULL;
853         vector pathvec = NULL;
854         int i;
855         int di_flag = 0;
856         int arg;
857         extern char *optarg;
858         extern int optind;
859         char * refwwid = NULL;
860
861         if (getuid() != 0) {
862                 fprintf(stderr, "need to be root\n");
863                 exit(1);
864         }
865
866         if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
867                 exit(1);
868
869         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
870                 condlog(0, "multipath tools need sysfs mounted");
871                 exit(1);
872         }
873         if (load_config(DEFAULT_CONFIGFILE))
874                 exit(1);
875
876         while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
877                 switch(arg) {
878                 case 1: printf("optarg : %s\n",optarg);
879                         break;
880                 case 'v':
881                         if (sizeof(optarg) > sizeof(char *) ||
882                             !isdigit(optarg[0]))
883                                 usage (argv[0]);
884
885                         conf->verbosity = atoi(optarg);
886                         break;
887                 case 'd':
888                         conf->dry_run = 1;
889                         break;
890                 case 'f':
891                         conf->remove = 1;
892                         break;
893                 case 'F':
894                         conf->remove = 2;
895                         break;
896                 case 'l':
897                         conf->list = 1;
898                         conf->dry_run = 1;
899
900                         if (optarg && !strncmp(optarg, "l", 1))
901                                 conf->list++;
902
903                         break;
904                 case 'M':
905 #if _DEBUG_
906                         debug = atoi(optarg);
907 #endif
908                         break;
909                 case 'p':
910                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
911                         if (conf->pgpolicy_flag == -1) {
912                                 printf("'%s' is not a valid policy\n", optarg);
913                                 usage(argv[0]);
914                         }                
915                         break;
916                 case ':':
917                         fprintf(stderr, "Missing option arguement\n");
918                         usage(argv[0]);        
919                 case '?':
920                         fprintf(stderr, "Unknown switch: %s\n", optarg);
921                         usage(argv[0]);
922                 default:
923                         usage(argv[0]);
924                 }
925         }        
926         if (optind < argc) {
927                 conf->dev = MALLOC(FILE_NAME_SIZE);
928
929                 if (!conf->dev)
930                         goto out;
931
932                 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
933
934                 if (filepresent(conf->dev))
935                         conf->dev_type = DEV_DEVNODE;
936                 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
937                         conf->dev_type = DEV_DEVT;
938                 else
939                         conf->dev_type = DEV_DEVMAP;
940
941         }
942
943         if (conf->remove == FLUSH_ONE) {
944                 if (conf->dev_type == DEV_DEVMAP)
945                         dm_flush_map(conf->dev, DEFAULT_TARGET);
946                 else
947                         condlog(0, "must provide a map name to remove");
948
949                 goto out;
950         }
951         else if (conf->remove == FLUSH_ALL) {
952                 dm_flush_maps(DEFAULT_TARGET);
953                 goto out;
954         }
955
956         /*
957          * allocate core vectors to store paths and multipaths
958          */
959         curmp = vector_alloc();
960         pathvec = vector_alloc();
961
962         if (!curmp || !pathvec) {
963                 condlog(0, "can not allocate memory");
964                 goto out;
965         }
966
967         /*
968          * if we have a blacklisted device parameter, exit early
969          */
970         if (conf->dev && blacklist(conf->blist, conf->dev))
971                 goto out;
972         
973         condlog(3, "load path identifiers cache");
974         cache_load(pathvec);
975
976         if (conf->verbosity > 2) {
977                 fprintf(stdout, "#\n# all paths in cache :\n#\n");
978                 print_all_paths(pathvec);
979         }
980
981         /*
982          * get a path list
983          */
984         if (conf->dev)
985                 di_flag = DI_WWID;
986
987         if (conf->list > 1)
988                 /* extended path info '-ll' */
989                 di_flag |= DI_SYSFS | DI_CHECKER;
990         else if (conf->list)
991                 /* minimum path info '-l' */
992                 di_flag |= DI_SYSFS;
993         else
994                 /* maximum info */
995                 di_flag = DI_ALL;
996
997         if (path_discovery(pathvec, conf, di_flag) || VECTOR_SIZE(pathvec) == 0)
998                 goto out;
999
1000         if (conf->verbosity > 2) {
1001                 fprintf(stdout, "#\n# all paths :\n#\n");
1002                 print_all_paths(pathvec);
1003         }
1004
1005         /*
1006          * scope limiting must be translated into a wwid
1007          * failing the translation is fatal (by policy)
1008          */
1009         if (conf->dev) {
1010                 refwwid = get_refwwid(pathvec);
1011
1012                 if (!refwwid) {
1013                         condlog(3, "scope is nul");
1014                         goto out;
1015                 }
1016         }
1017
1018         get_path_layout(&pl, pathvec);
1019
1020         if (get_dm_mpvec(curmp, pathvec, refwwid))
1021                 goto out;
1022
1023         filter_pathvec(pathvec, refwwid);
1024
1025         if (conf->list)
1026                 goto out;
1027
1028         /*
1029          * core logic entry point
1030          */
1031         coalesce_paths(curmp, pathvec);
1032
1033 out:
1034         if (refwwid)
1035                 FREE(refwwid);
1036
1037         free_multipathvec(curmp, KEEP_PATHS);
1038         free_pathvec(pathvec, FREE_PATHS);
1039         free_config(conf);
1040         dm_lib_release();
1041         dm_lib_exit();
1042 #ifdef _DEBUG_
1043         dbg_free_final(NULL);
1044 #endif
1045         exit(0);
1046 }