[build] minor compilation issues
[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
50 #include "main.h"
51
52 static int
53 filter_pathvec (vector pathvec, char * refwwid)
54 {
55         int i;
56         struct path * pp;
57
58         if (!refwwid || !strlen(refwwid))
59                 return 0;
60
61         vector_foreach_slot (pathvec, pp, i) {
62                 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
63                         condlog(3, "skip path %s : out of scope", pp->dev);
64                         free_path(pp);
65                         vector_del_slot(pathvec, i);
66                         i--;
67                 }
68         }
69         return 0;
70 }
71
72 static void
73 usage (char * progname)
74 {
75         fprintf (stderr, VERSION_STRING);
76         fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
77                 progname);
78         fprintf (stderr,
79                 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
80                 "\t\t\t[device]\n" \
81                 "\n" \
82                 "\t-v level\tverbosity level\n" \
83                 "\t   0\t\t\tno output\n" \
84                 "\t   1\t\t\tprint created devmap names only\n" \
85                 "\t   2\t\t\tdefault verbosity\n" \
86                 "\t   3\t\t\tprint debug information\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                                 pathinfo(pp, conf->hwtable, DI_ALL);
132                                 continue;
133                         }
134                         if (pp->state == PATH_UNCHECKED)
135                                 pathinfo(pp, conf->hwtable, DI_CHECKER);
136
137                         if (!pp->priority)
138                                 pathinfo(pp, conf->hwtable, DI_PRIO);
139                 }
140         }
141         return 0;
142 }
143
144 static int
145 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
146 {
147         int i;
148         struct multipath * mpp;
149
150         if (dm_get_maps(curmp, DEFAULT_TARGET))
151                 return 1;
152
153         vector_foreach_slot (curmp, mpp, i) {
154                 /*
155                  * discard out of scope maps
156                  */
157                 if (mpp->wwid && refwwid &&
158                     strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
159                         condlog(3, "skip map %s: out of scope", mpp->alias);
160                         free_multipath(mpp, KEEP_PATHS);
161                         vector_del_slot(curmp, i);
162                         i--;
163                         continue;
164                 }
165
166                 condlog(3, "params = %s", mpp->params);
167                 condlog(3, "status = %s", mpp->status);
168
169                 disassemble_map(pathvec, mpp->params, mpp);
170
171                 /*
172                  * disassemble_map() can add new paths to pathvec.
173                  * If not in "fast list mode", we need to fetch information
174                  * about them
175                  */
176                 if (conf->list != 1)
177                         update_paths(mpp);
178
179                 if (conf->list > 1)
180                         mpp->bestpg = select_path_group(mpp);
181
182                 disassemble_status(mpp->status, mpp);
183
184                 if (conf->list)
185                         print_multipath_topology(mpp, conf->verbosity);
186
187                 if (!conf->dry_run)
188                         reinstate_paths(mpp);
189         }
190         return 0;
191 }
192
193
194 /*
195  * Return value:
196  *  -1: Retry
197  *   0: Success
198  *   1: Failure
199  */
200 static int
201 configure (void)
202 {
203         vector curmp = NULL;
204         vector pathvec = NULL;
205         struct vectors vecs;
206         int r = 1;
207         int di_flag = 0;
208         char * refwwid = NULL;
209         char * dev = NULL;
210
211         /*
212          * allocate core vectors to store paths and multipaths
213          */
214         curmp = vector_alloc();
215         pathvec = vector_alloc();
216
217         if (!curmp || !pathvec) {
218                 condlog(0, "can not allocate memory");
219                 goto out;
220         }
221         vecs.pathvec = pathvec;
222         vecs.mpvec = curmp;
223
224         /*
225          * if we have a blacklisted device parameter, exit early
226          */
227         if (conf->dev) {
228                 if (!strncmp(conf->dev, "/dev/", 5) &&
229                     strlen(conf->dev) > 5)
230                         dev = conf->dev + 5;
231                 else
232                         dev = conf->dev;
233         }
234         
235         if (dev && blacklist(conf->blist_devnode, dev))
236                 goto out;
237         
238         /*
239          * scope limiting must be translated into a wwid
240          * failing the translation is fatal (by policy)
241          */
242         if (conf->dev) {
243                 refwwid = get_refwwid(conf->dev, conf->dev_type, pathvec);
244
245                 if (!refwwid) {
246                         condlog(3, "scope is nul");
247                         goto out;
248                 }
249                 condlog(3, "scope limited to %s", refwwid);
250
251                 if (blacklist(conf->blist_wwid, refwwid))
252                         goto out;
253         }
254
255         /*
256          * get a path list
257          */
258         if (conf->dev)
259                 di_flag = DI_WWID;
260
261         if (conf->list > 1)
262                 /* extended path info '-ll' */
263                 di_flag |= DI_SYSFS | DI_CHECKER;
264         else if (conf->list)
265                 /* minimum path info '-l' */
266                 di_flag |= DI_SYSFS;
267         else
268                 /* maximum info */
269                 di_flag = DI_ALL;
270
271         if (path_discovery(pathvec, conf, di_flag))
272                 goto out;
273
274         if (conf->verbosity > 2)
275                 print_all_paths(pathvec, 1);
276
277         get_path_layout(pathvec);
278
279         if (get_dm_mpvec(curmp, pathvec, refwwid))
280                 goto out;
281
282         filter_pathvec(pathvec, refwwid);
283
284         if (conf->list)
285                 goto out;
286
287         /*
288          * core logic entry point
289          */
290         r = coalesce_paths(&vecs, NULL, NULL);
291
292 out:
293         if (refwwid)
294                 FREE(refwwid);
295
296         free_multipathvec(curmp, KEEP_PATHS);
297         free_pathvec(pathvec, FREE_PATHS);
298
299         return r;
300 }
301
302 int
303 main (int argc, char *argv[])
304 {
305         int arg;
306         extern char *optarg;
307         extern int optind;
308         int i, r = 1;
309
310         if (getuid() != 0) {
311                 fprintf(stderr, "need to be root\n");
312                 exit(1);
313         }
314
315         if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
316                 exit(1);
317
318         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
319                 condlog(0, "multipath tools need sysfs mounted");
320                 exit(1);
321         }
322         if (load_config(DEFAULT_CONFIGFILE))
323                 exit(1);
324
325         while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:b:")) != EOF ) {
326                 switch(arg) {
327                 case 1: printf("optarg : %s\n",optarg);
328                         break;
329                 case 'v':
330                         if (sizeof(optarg) > sizeof(char *) ||
331                             !isdigit(optarg[0]))
332                                 usage (argv[0]);
333
334                         conf->verbosity = atoi(optarg);
335                         break;
336                 case 'b':
337                         conf->bindings_file = optarg;
338                         break;
339                 case 'd':
340                         conf->dry_run = 1;
341                         break;
342                 case 'f':
343                         conf->remove = FLUSH_ONE;
344                         break;
345                 case 'F':
346                         conf->remove = FLUSH_ALL;
347                         break;
348                 case 'l':
349                         conf->list = 1;
350                         conf->dry_run = 1;
351
352                         if (optarg && !strncmp(optarg, "l", 1))
353                                 conf->list++;
354
355                         break;
356                 case 'M':
357 #if _DEBUG_
358                         debug = atoi(optarg);
359 #endif
360                         break;
361                 case 'p':
362                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
363                         if (conf->pgpolicy_flag == -1) {
364                                 printf("'%s' is not a valid policy\n", optarg);
365                                 usage(argv[0]);
366                         }                
367                         break;
368                 case ':':
369                         fprintf(stderr, "Missing option arguement\n");
370                         usage(argv[0]);        
371                 case '?':
372                         fprintf(stderr, "Unknown switch: %s\n", optarg);
373                         usage(argv[0]);
374                 default:
375                         usage(argv[0]);
376                 }
377         }        
378         if (optind < argc) {
379                 conf->dev = MALLOC(FILE_NAME_SIZE);
380
381                 if (!conf->dev)
382                         goto out;
383
384                 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
385
386                 if (filepresent(conf->dev))
387                         conf->dev_type = DEV_DEVNODE;
388                 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
389                         conf->dev_type = DEV_DEVT;
390                 else
391                         conf->dev_type = DEV_DEVMAP;
392
393         }
394
395         if (conf->remove == FLUSH_ONE) {
396                 if (conf->dev_type == DEV_DEVMAP)
397                         dm_flush_map(conf->dev, DEFAULT_TARGET);
398                 else
399                         condlog(0, "must provide a map name to remove");
400
401                 goto out;
402         }
403         else if (conf->remove == FLUSH_ALL) {
404                 dm_flush_maps(DEFAULT_TARGET);
405                 goto out;
406         }
407         while ((r = configure()) < 0)
408                 condlog(3, "restart multipath configuration process");
409         
410 out:
411         free_config(conf);
412         dm_lib_release();
413         dm_lib_exit();
414 #ifdef _DEBUG_
415         dbg_free_final(NULL);
416 #endif
417         return r;
418 }