[libmultipath] devmapper prereq detection overhault
[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:      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  * Copyright (c) 2003, 2004, 2005 Christophe Varoqui
19  * Copyright (c) 2005 Benjamin Marzinski, Redhat
20  * Copyright (c) 2005 Kiyoshi Ueda, NEC
21  * Copyright (c) 2005 Patrick Caulfield, Redhat
22  * Copyright (c) 2005 Edward Goggin, EMC
23  */
24
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <ctype.h>
28 #include <sysfs/libsysfs.h>
29
30 #include <checkers.h>
31 #include <vector.h>
32 #include <memory.h>
33 #include <libdevmapper.h>
34 #include <devmapper.h>
35 #include <util.h>
36 #include <defaults.h>
37 #include <structs.h>
38 #include <structs_vec.h>
39 #include <dmparser.h>
40 #include <config.h>
41 #include <blacklist.h>
42 #include <discovery.h>
43 #include <debug.h>
44 #include <switchgroup.h>
45 #include <print.h>
46 #include <alias.h>
47 #include <configure.h>
48 #include <pgpolicies.h>
49 #include <version.h>
50
51 static int
52 filter_pathvec (vector pathvec, char * refwwid)
53 {
54         int i;
55         struct path * pp;
56
57         if (!refwwid || !strlen(refwwid))
58                 return 0;
59
60         vector_foreach_slot (pathvec, pp, i) {
61                 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
62                         condlog(3, "skip path %s : out of scope", pp->dev);
63                         free_path(pp);
64                         vector_del_slot(pathvec, i);
65                         i--;
66                 }
67         }
68         return 0;
69 }
70
71 static void
72 usage (char * progname)
73 {
74         fprintf (stderr, VERSION_STRING);
75         fprintf (stderr, "Usage: %s\t[-v level] [-d] [-h|-l|-ll|-f|-F]\n",
76                 progname);
77         fprintf (stderr,
78                 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
79                 "\t\t\t[device]\n" \
80                 "\n" \
81                 "\t-v level\tverbosity level\n" \
82                 "\t   0\t\t\tno output\n" \
83                 "\t   1\t\t\tprint created devmap names only\n" \
84                 "\t   2\t\t\tdefault verbosity\n" \
85                 "\t   3\t\t\tprint debug information\n" \
86                 "\t-h\t\tprint this usage text\n" \
87                 "\t-b file\t\tbindings file location\n" \
88                 "\t-d\t\tdry run, do not create or update devmaps\n" \
89                 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
90                 "\t-ll\t\tshow multipath topology (maximum info)\n" \
91                 "\t-f\t\tflush a multipath device map\n" \
92                 "\t-F\t\tflush all multipath device maps\n" \
93                 "\t-p policy\tforce all maps to specified policy :\n" \
94                 "\t   failover\t\t1 path per priority group\n" \
95                 "\t   multibus\t\tall paths in 1 priority group\n" \
96                 "\t   group_by_serial\t1 priority group per serial\n" \
97                 "\t   group_by_prio\t1 priority group per priority lvl\n" \
98                 "\t   group_by_node_name\t1 priority group per target node\n" \
99                 "\n" \
100                 "\tdevice\t\tlimit scope to the device's multipath\n" \
101                 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
102                 "\t\t\tor major:minor or a device map name)\n" \
103                 );
104
105         exit(1);
106 }
107
108 static int
109 update_paths (struct multipath * mpp)
110 {
111         int i, j;
112         struct pathgroup * pgp;
113         struct path * pp;
114
115         if (!mpp->pg)
116                 return 0;
117
118         vector_foreach_slot (mpp->pg, pgp, i) {
119                 if (!pgp->paths)
120                         continue;
121
122                 vector_foreach_slot (pgp->paths, pp, j) {
123                         if (!strlen(pp->dev)) {
124                                 if (devt2devname(pp->dev, pp->dev_t)) {
125                                         /*
126                                          * path is not in sysfs anymore
127                                          */
128                                         pp->state = PATH_DOWN;
129                                         continue;
130                                 }
131                                 pp->mpp = mpp;
132                                 pathinfo(pp, conf->hwtable, DI_ALL);
133                                 continue;
134                         }
135                         pp->mpp = mpp;
136                         if (pp->state == PATH_UNCHECKED)
137                                 pathinfo(pp, conf->hwtable, DI_CHECKER);
138
139                         if (pp->priority == PRIO_UNDEF)
140                                 pathinfo(pp, conf->hwtable, DI_PRIO);
141                 }
142         }
143         return 0;
144 }
145
146 static int
147 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
148 {
149         int i;
150         struct multipath * mpp;
151
152         if (dm_get_maps(curmp, DEFAULT_TARGET))
153                 return 1;
154
155         vector_foreach_slot (curmp, mpp, i) {
156                 /*
157                  * discard out of scope maps
158                  */
159                 if (mpp->wwid && refwwid &&
160                     strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
161                         condlog(3, "skip map %s: out of scope", mpp->alias);
162                         free_multipath(mpp, KEEP_PATHS);
163                         vector_del_slot(curmp, i);
164                         i--;
165                         continue;
166                 }
167
168                 condlog(3, "params = %s", mpp->params);
169                 condlog(3, "status = %s", mpp->status);
170
171                 disassemble_map(pathvec, mpp->params, mpp);
172
173                 /*
174                  * disassemble_map() can add new paths to pathvec.
175                  * If not in "fast list mode", we need to fetch information
176                  * about them
177                  */
178                 if (conf->list != 1)
179                         update_paths(mpp);
180
181                 if (conf->list > 1)
182                         mpp->bestpg = select_path_group(mpp);
183
184                 disassemble_status(mpp->status, mpp);
185
186                 if (conf->list)
187                         print_multipath_topology(mpp, conf->verbosity);
188
189                 if (!conf->dry_run)
190                         reinstate_paths(mpp);
191         }
192         return 0;
193 }
194
195
196 /*
197  * Return value:
198  *  -1: Retry
199  *   0: Success
200  *   1: Failure
201  */
202 static int
203 configure (void)
204 {
205         vector curmp = NULL;
206         vector pathvec = NULL;
207         struct vectors vecs;
208         int r = 1;
209         int di_flag = 0;
210         char * refwwid = NULL;
211         char * dev = NULL;
212
213         /*
214          * allocate core vectors to store paths and multipaths
215          */
216         curmp = vector_alloc();
217         pathvec = vector_alloc();
218
219         if (!curmp || !pathvec) {
220                 condlog(0, "can not allocate memory");
221                 goto out;
222         }
223         vecs.pathvec = pathvec;
224         vecs.mpvec = curmp;
225
226         /*
227          * if we have a blacklisted device parameter, exit early
228          */
229         if (conf->dev) {
230                 if (!strncmp(conf->dev, "/dev/", 5) &&
231                     strlen(conf->dev) > 5)
232                         dev = conf->dev + 5;
233                 else
234                         dev = conf->dev;
235         }
236         
237         if (dev && blacklist(conf->blist_devnode, dev))
238                 goto out;
239         
240         /*
241          * scope limiting must be translated into a wwid
242          * failing the translation is fatal (by policy)
243          */
244         if (conf->dev) {
245                 refwwid = get_refwwid(conf->dev, conf->dev_type, pathvec);
246
247                 if (!refwwid) {
248                         condlog(3, "scope is nul");
249                         goto out;
250                 }
251                 condlog(3, "scope limited to %s", refwwid);
252
253                 if (blacklist(conf->blist_wwid, refwwid))
254                         goto out;
255         }
256
257         /*
258          * get a path list
259          */
260         if (conf->dev)
261                 di_flag = DI_WWID;
262
263         if (conf->list > 1)
264                 /* extended path info '-ll' */
265                 di_flag |= DI_SYSFS | DI_CHECKER;
266         else if (conf->list)
267                 /* minimum path info '-l' */
268                 di_flag |= DI_SYSFS;
269         else
270                 /* maximum info */
271                 di_flag = DI_ALL;
272
273         if (path_discovery(pathvec, conf, di_flag))
274                 goto out;
275
276         if (conf->verbosity > 2)
277                 print_all_paths(pathvec, 1);
278
279         get_path_layout(pathvec);
280
281         if (get_dm_mpvec(curmp, pathvec, refwwid))
282                 goto out;
283
284         filter_pathvec(pathvec, refwwid);
285
286         if (conf->list) {
287                 r = 0;
288                 goto out;
289         }
290
291         /*
292          * core logic entry point
293          */
294         r = coalesce_paths(&vecs, NULL, NULL);
295
296 out:
297         if (refwwid)
298                 FREE(refwwid);
299
300         free_multipathvec(curmp, KEEP_PATHS);
301         free_pathvec(pathvec, FREE_PATHS);
302
303         return r;
304 }
305
306 int
307 main (int argc, char *argv[])
308 {
309         int arg;
310         extern char *optarg;
311         extern int optind;
312         int i, r = 1;
313
314         if (getuid() != 0) {
315                 fprintf(stderr, "need to be root\n");
316                 exit(1);
317         }
318
319         if (dm_prereq(DEFAULT_TARGET))
320                 exit(1);
321
322         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
323                 condlog(0, "multipath tools need sysfs mounted");
324                 exit(1);
325         }
326         if (load_config(DEFAULT_CONFIGFILE))
327                 exit(1);
328
329         while ((arg = getopt(argc, argv, ":dhl::FfM:v:p:b:")) != EOF ) {
330                 switch(arg) {
331                 case 1: printf("optarg : %s\n",optarg);
332                         break;
333                 case 'v':
334                         if (sizeof(optarg) > sizeof(char *) ||
335                             !isdigit(optarg[0]))
336                                 usage (argv[0]);
337
338                         conf->verbosity = atoi(optarg);
339                         break;
340                 case 'b':
341                         conf->bindings_file = optarg;
342                         break;
343                 case 'd':
344                         conf->dry_run = 1;
345                         break;
346                 case 'f':
347                         conf->remove = FLUSH_ONE;
348                         break;
349                 case 'F':
350                         conf->remove = FLUSH_ALL;
351                         break;
352                 case 'l':
353                         conf->list = 1;
354                         conf->dry_run = 1;
355
356                         if (optarg && !strncmp(optarg, "l", 1))
357                                 conf->list++;
358
359                         break;
360                 case 'M':
361 #if _DEBUG_
362                         debug = atoi(optarg);
363 #endif
364                         break;
365                 case 'p':
366                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
367                         if (conf->pgpolicy_flag == -1) {
368                                 printf("'%s' is not a valid policy\n", optarg);
369                                 usage(argv[0]);
370                         }                
371                         break;
372                 case 'h':
373                         usage(argv[0]);
374                 case ':':
375                         fprintf(stderr, "Missing option arguement\n");
376                         usage(argv[0]);        
377                 case '?':
378                         fprintf(stderr, "Unknown switch: %s\n", optarg);
379                         usage(argv[0]);
380                 default:
381                         usage(argv[0]);
382                 }
383         }        
384         if (optind < argc) {
385                 conf->dev = MALLOC(FILE_NAME_SIZE);
386
387                 if (!conf->dev)
388                         goto out;
389
390                 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
391
392                 if (filepresent(conf->dev))
393                         conf->dev_type = DEV_DEVNODE;
394                 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
395                         conf->dev_type = DEV_DEVT;
396                 else
397                         conf->dev_type = DEV_DEVMAP;
398
399         }
400
401         if (conf->remove == FLUSH_ONE) {
402                 if (conf->dev_type == DEV_DEVMAP)
403                         dm_flush_map(conf->dev, DEFAULT_TARGET);
404                 else
405                         condlog(0, "must provide a map name to remove");
406
407                 goto out;
408         }
409         else if (conf->remove == FLUSH_ALL) {
410                 dm_flush_maps(DEFAULT_TARGET);
411                 goto out;
412         }
413         while ((r = configure()) < 0)
414                 condlog(3, "restart multipath configuration process");
415         
416 out:
417         free_config(conf);
418         dm_lib_release();
419         dm_lib_exit();
420 #ifdef _DEBUG_
421         dbg_free_final(NULL);
422 #endif
423         return r;
424 }