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