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