[multipath] *bad bad nasty* map reload code path
[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 r = 0;
584
585         print_mp(mpp);
586
587         /*
588          * last chance to quit before touching the devmaps
589          */
590         if (conf->dry_run)
591                 return 0;
592
593         switch (mpp->action) {
594         case ACT_NOTHING:
595                 return 0;
596
597         case ACT_SWITCHPG:
598                 dm_switchgroup(mpp->alias, mpp->nextpg);
599                 /*
600                  * we may have avoided reinstating paths because there where in
601                  * active or disabled PG. Now that the topology has changed,
602                  * retry.
603                  */
604                 reinstate_paths(mpp);
605                 return 0;
606
607         case ACT_CREATE:
608                 r = dm_addmap(DM_DEVICE_CREATE, mpp->alias, DEFAULT_TARGET,
609                               mpp->params, mpp->size, mpp->wwid);
610                 break;
611
612         case ACT_RELOAD:
613                 r = dm_addmap(DM_DEVICE_RELOAD, mpp->alias, DEFAULT_TARGET,
614                               mpp->params, mpp->size, NULL);
615                 break;
616
617         default:
618                 break;
619         }
620
621         if (r) {
622                 /*
623                  * DM_DEVICE_CREATE or DM_DEVICE_RELOAD succeeded
624                  */
625                 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias);
626                 dm_switchgroup(mpp->alias, mpp->nextpg);
627         }
628
629         return r;
630 }
631
632 static int
633 coalesce_paths (vector curmp, vector pathvec)
634 {
635         int k, i;
636         char empty_buff[WWID_SIZE];
637         struct multipath * mpp;
638         struct path * pp1;
639         struct path * pp2;
640
641         memset(empty_buff, 0, WWID_SIZE);
642
643         vector_foreach_slot (pathvec, pp1, k) {
644                 /* skip this path for some reason */
645
646                 /* 1. if path has no unique id or wwid blacklisted */
647                 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
648                     blacklist(conf->blist, pp1->wwid))
649                         continue;
650
651                 /* 2. if path already coalesced */
652                 if (pp1->mpp)
653                         continue;
654
655                 /*
656                  * at this point, we know we really got a new mp
657                  */
658                 mpp = alloc_multipath();
659
660                 if (!mpp)
661                         return 1;
662
663                 mpp->mpe = find_mpe(pp1->wwid);
664                 mpp->hwe = pp1->hwe;
665                 select_alias(mpp);
666
667                 pp1->mpp = mpp;
668                 strcpy(mpp->wwid, pp1->wwid);
669                 mpp->size = pp1->size;
670                 mpp->paths = vector_alloc();
671
672                 if (pp1->priority < 0)
673                         mpp->action = ACT_NOTHING;
674
675                 if (!mpp->paths)
676                         return 1;
677                 
678                 if (store_path(mpp->paths, pp1))
679                         return 1;
680
681                 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
682                         pp2 = VECTOR_SLOT(pathvec, i);
683
684                         if (strcmp(pp1->wwid, pp2->wwid))
685                                 continue;
686                         
687                         pp2->mpp = mpp;
688
689                         if (pp2->size != mpp->size) {
690                                 /*
691                                  * ouch, avoid feeding that to the DM
692                                  */
693                                 condlog(3, "path size mismatch : discard %s",
694                                      mpp->wwid);
695                                 mpp->action = ACT_NOTHING;
696                         }
697                         if (pp2->priority < 0)
698                                 mpp->action = ACT_NOTHING;
699
700                         if (store_path(mpp->paths, pp2))
701                                 return 1;
702                 }
703                 if (setup_map(mpp)) {
704                         free_multipath(mpp, KEEP_PATHS);
705                         continue;
706                 }
707                 condlog(3, "action preset to %i", mpp->action);
708
709                 if (mpp->action == ACT_UNDEF)
710                         select_action(mpp, curmp);
711
712                 condlog(3, "action set to %i", mpp->action);
713
714                 domap(mpp);
715                 free_multipath(mpp, KEEP_PATHS);
716         }
717         return 0;
718 }
719
720 static void
721 usage (char * progname)
722 {
723         fprintf (stderr, VERSION_STRING);
724         fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
725                 progname);
726         fprintf (stderr,
727                 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
728                 "\t\t\t[device]\n" \
729                 "\n" \
730                 "\t-v level\tverbosty level\n" \
731                 "\t   0\t\t\tno output\n" \
732                 "\t   1\t\t\tprint created devmap names only\n" \
733                 "\t   2\t\t\tdefault verbosity\n" \
734                 "\t   3\t\t\tprint debug information\n" \
735                 "\t-d\t\tdry run, do not create or update devmaps\n" \
736                 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
737                 "\t-ll\t\tshow multipath topology (maximum info)\n" \
738                 "\t-F\t\tflush a multipath device map\n" \
739                 "\t-F\t\tflush all multipath device maps\n" \
740                 "\t-p policy\tforce all maps to specified policy :\n" \
741                 "\t   failover\t\t1 path per priority group\n" \
742                 "\t   multibus\t\tall paths in 1 priority group\n" \
743                 "\t   group_by_serial\t1 priority group per serial\n" \
744                 "\t   group_by_prio\t1 priority group per priority lvl\n" \
745                 "\t   group_by_node_name\t1 priority group per target node\n" \
746                 "\n" \
747                 "\tdevice\t\tlimit scope to the device's multipath\n" \
748                 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
749                 "\t\t\tor major:minor or a device map name)\n" \
750                 );
751
752         exit(1);
753 }
754
755 static int
756 update_paths (struct multipath * mpp)
757 {
758         int i, j;
759         struct pathgroup * pgp;
760         struct path * pp;
761
762         vector_foreach_slot (mpp->pg, pgp, i) {
763                 vector_foreach_slot (pgp->paths, pp, j) {
764                         if (!strlen(pp->dev)) {
765                                 if (devt2devname(pp->dev_t, pp->dev)) {
766                                         /*
767                                          * path is not in sysfs anymore
768                                          */
769                                         pp->state = PATH_DOWN;
770                                         continue;
771                                 }
772                                 pathinfo(pp, conf->hwtable,
773                                          DI_SYSFS | DI_CHECKER | \
774                                          DI_SERIAL | DI_PRIO);
775                                 continue;
776                         }
777                         if (pp->state == PATH_UNCHECKED)
778                                 pathinfo(pp, conf->hwtable, DI_CHECKER);
779
780                         if (!pp->priority)
781                                 pathinfo(pp, conf->hwtable, DI_PRIO);
782                 }
783         }
784         return 0;
785 }
786
787 static int
788 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
789 {
790         int i;
791         struct multipath * mpp;
792         char * wwid;
793
794         if (dm_get_maps(curmp, DEFAULT_TARGET))
795                 return 1;
796
797         vector_foreach_slot (curmp, mpp, i) {
798                 wwid = get_mpe_wwid(mpp->alias);
799
800                 if (wwid) {
801                         /* out of specified scope */
802                         if (refwwid && strncmp(wwid, refwwid, WWID_SIZE))
803                                 continue;
804                         wwid = NULL;
805                 }
806
807                 condlog(3, "params = %s", mpp->params);
808                 condlog(3, "status = %s", mpp->status);
809
810                 /* will set mpp->wwid */
811                 disassemble_map(pathvec, mpp->params, mpp);
812
813                 /*
814                  * disassemble_map may have added new paths to pathvec.
815                  * If not in "fast list mode", we need to fetch information
816                  * about them
817                  */
818                 if (conf->list != 1)
819                         update_paths(mpp);
820
821                 if (conf->list > 1)
822                         select_path_group(mpp);
823
824                 disassemble_status(mpp->status, mpp);
825
826                 if (conf->list)
827                         print_mp(mpp);
828
829                 if (!conf->dry_run)
830                         reinstate_paths(mpp);
831         }
832         return 0;
833 }
834
835 int
836 main (int argc, char *argv[])
837 {
838         vector curmp = NULL;
839         vector pathvec = NULL;
840         int i;
841         int di_flag = 0;
842         int arg;
843         extern char *optarg;
844         extern int optind;
845         char * refwwid = NULL;
846
847         if (getuid() != 0) {
848                 fprintf(stderr, "need to be root\n");
849                 exit(1);
850         }
851
852         if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
853                 exit(1);
854
855         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
856                 condlog(0, "multipath tools need sysfs mounted");
857                 exit(1);
858         }
859         if (load_config(DEFAULT_CONFIGFILE))
860                 exit(1);
861
862         while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
863                 switch(arg) {
864                 case 1: printf("optarg : %s\n",optarg);
865                         break;
866                 case 'v':
867                         if (sizeof(optarg) > sizeof(char *) ||
868                             !isdigit(optarg[0]))
869                                 usage (argv[0]);
870
871                         conf->verbosity = atoi(optarg);
872                         break;
873                 case 'd':
874                         conf->dry_run = 1;
875                         break;
876                 case 'f':
877                         conf->remove = 1;
878                         break;
879                 case 'F':
880                         conf->remove = 2;
881                         break;
882                 case 'l':
883                         conf->list = 1;
884                         conf->dry_run = 1;
885
886                         if (optarg && !strncmp(optarg, "l", 1))
887                                 conf->list++;
888
889                         break;
890                 case 'M':
891 #if _DEBUG_
892                         debug = atoi(optarg);
893 #endif
894                         break;
895                 case 'p':
896                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
897                         if (conf->pgpolicy_flag == -1) {
898                                 printf("'%s' is not a valid policy\n", optarg);
899                                 usage(argv[0]);
900                         }                
901                         break;
902                 case ':':
903                         fprintf(stderr, "Missing option arguement\n");
904                         usage(argv[0]);        
905                 case '?':
906                         fprintf(stderr, "Unknown switch: %s\n", optarg);
907                         usage(argv[0]);
908                 default:
909                         usage(argv[0]);
910                 }
911         }        
912         if (optind < argc) {
913                 conf->dev = MALLOC(FILE_NAME_SIZE);
914
915                 if (!conf->dev)
916                         goto out;
917
918                 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
919
920                 if (filepresent(conf->dev))
921                         conf->dev_type = DEV_DEVNODE;
922                 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
923                         conf->dev_type = DEV_DEVT;
924                 else
925                         conf->dev_type = DEV_DEVMAP;
926
927         }
928
929         if (conf->remove == FLUSH_ONE) {
930                 if (conf->dev_type == DEV_DEVMAP)
931                         dm_flush_map(conf->dev, DEFAULT_TARGET);
932                 else
933                         condlog(0, "must provide a map name to remove");
934
935                 goto out;
936         }
937         else if (conf->remove == FLUSH_ALL) {
938                 dm_flush_maps(DEFAULT_TARGET);
939                 goto out;
940         }
941
942         /*
943          * allocate core vectors to store paths and multipaths
944          */
945         curmp = vector_alloc();
946         pathvec = vector_alloc();
947
948         if (!curmp || !pathvec) {
949                 condlog(0, "can not allocate memory");
950                 goto out;
951         }
952
953         /*
954          * if we have a blacklisted device parameter, exit early
955          */
956         if (conf->dev && blacklist(conf->blist, conf->dev))
957                 goto out;
958         
959         condlog(3, "load path identifiers cache");
960         cache_load(pathvec);
961
962         if (conf->verbosity > 2) {
963                 fprintf(stdout, "#\n# all paths in cache :\n#\n");
964                 print_all_paths(pathvec);
965         }
966
967         /*
968          * get a path list
969          */
970         if (conf->dev)
971                 di_flag = DI_WWID;
972
973         if (conf->list > 1)
974                 /* extended path info '-ll' */
975                 di_flag |= DI_SYSFS | DI_CHECKER;
976         else if (conf->list)
977                 /* minimum path info '-l' */
978                 di_flag |= DI_SYSFS;
979         else
980                 /* maximum info */
981                 di_flag = DI_ALL;
982
983         if (path_discovery(pathvec, conf, di_flag))
984                 goto out;
985
986         if (conf->verbosity > 2) {
987                 fprintf(stdout, "#\n# all paths :\n#\n");
988                 print_all_paths(pathvec);
989         }
990
991         /*
992          * scope limiting must be translated into a wwid
993          * failing the translation is fatal (by policy)
994          */
995         if (conf->dev) {
996                 refwwid = get_refwwid(pathvec);
997
998                 if (!refwwid) {
999                         condlog(3, "scope is nul");
1000                         goto out;
1001                 }
1002         }
1003
1004         get_path_layout(&pl, pathvec);
1005
1006         if (get_dm_mpvec(curmp, pathvec, refwwid))
1007                 goto out;
1008
1009         filter_pathvec(pathvec, refwwid);
1010
1011         if (conf->list)
1012                 goto out;
1013
1014         /*
1015          * core logic entry point
1016          */
1017         coalesce_paths(curmp, pathvec);
1018
1019 out:
1020         if (refwwid)
1021                 FREE(refwwid);
1022
1023         free_multipathvec(curmp, KEEP_PATHS);
1024         free_pathvec(pathvec, FREE_PATHS);
1025         free_config(conf);
1026         dm_lib_release();
1027         dm_lib_exit();
1028 #ifdef _DEBUG_
1029         dbg_free_final(NULL);
1030 #endif
1031         exit(0);
1032 }