[libmultipath] drop the path cache
[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 <vector.h>
31 #include <memory.h>
32 #include <libdevmapper.h>
33 #include <devmapper.h>
34 #include <path_state.h>
35 #include <blacklist.h>
36 #include <util.h>
37 #include <defaults.h>
38 #include <structs.h>
39 #include <structs_vec.h>
40 #include <dmparser.h>
41 #include <config.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_mp(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, 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
252         /*
253          * get a path list
254          */
255         if (conf->dev)
256                 di_flag = DI_WWID;
257
258         if (conf->list > 1)
259                 /* extended path info '-ll' */
260                 di_flag |= DI_SYSFS | DI_CHECKER;
261         else if (conf->list)
262                 /* minimum path info '-l' */
263                 di_flag |= DI_SYSFS;
264         else
265                 /* maximum info */
266                 di_flag = DI_ALL;
267
268         if (path_discovery(pathvec, conf, di_flag))
269                 goto out;
270
271         if (conf->verbosity > 2)
272                 print_all_paths(pathvec, 1);
273
274         get_path_layout(pathvec);
275
276         if (get_dm_mpvec(curmp, pathvec, refwwid))
277                 goto out;
278
279         filter_pathvec(pathvec, refwwid);
280
281         if (conf->list)
282                 goto out;
283
284         /*
285          * core logic entry point
286          */
287         r = coalesce_paths(&vecs, NULL, NULL);
288
289 out:
290         if (refwwid)
291                 FREE(refwwid);
292
293         free_multipathvec(curmp, KEEP_PATHS);
294         free_pathvec(pathvec, FREE_PATHS);
295
296         return r;
297 }
298
299 int
300 main (int argc, char *argv[])
301 {
302         int arg;
303         extern char *optarg;
304         extern int optind;
305         int i, r;
306
307         if (getuid() != 0) {
308                 fprintf(stderr, "need to be root\n");
309                 exit(1);
310         }
311
312         if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
313                 exit(1);
314
315         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
316                 condlog(0, "multipath tools need sysfs mounted");
317                 exit(1);
318         }
319         if (load_config(DEFAULT_CONFIGFILE))
320                 exit(1);
321
322         while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:b:")) != EOF ) {
323                 switch(arg) {
324                 case 1: printf("optarg : %s\n",optarg);
325                         break;
326                 case 'v':
327                         if (sizeof(optarg) > sizeof(char *) ||
328                             !isdigit(optarg[0]))
329                                 usage (argv[0]);
330
331                         conf->verbosity = atoi(optarg);
332                         break;
333                 case 'b':
334                         conf->bindings_file = optarg;
335                         break;
336                 case 'd':
337                         conf->dry_run = 1;
338                         break;
339                 case 'f':
340                         conf->remove = FLUSH_ONE;
341                         break;
342                 case 'F':
343                         conf->remove = FLUSH_ALL;
344                         break;
345                 case 'l':
346                         conf->list = 1;
347                         conf->dry_run = 1;
348
349                         if (optarg && !strncmp(optarg, "l", 1))
350                                 conf->list++;
351
352                         break;
353                 case 'M':
354 #if _DEBUG_
355                         debug = atoi(optarg);
356 #endif
357                         break;
358                 case 'p':
359                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
360                         if (conf->pgpolicy_flag == -1) {
361                                 printf("'%s' is not a valid policy\n", optarg);
362                                 usage(argv[0]);
363                         }                
364                         break;
365                 case ':':
366                         fprintf(stderr, "Missing option arguement\n");
367                         usage(argv[0]);        
368                 case '?':
369                         fprintf(stderr, "Unknown switch: %s\n", optarg);
370                         usage(argv[0]);
371                 default:
372                         usage(argv[0]);
373                 }
374         }        
375         if (optind < argc) {
376                 conf->dev = MALLOC(FILE_NAME_SIZE);
377
378                 if (!conf->dev)
379                         goto out;
380
381                 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
382
383                 if (filepresent(conf->dev))
384                         conf->dev_type = DEV_DEVNODE;
385                 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
386                         conf->dev_type = DEV_DEVT;
387                 else
388                         conf->dev_type = DEV_DEVMAP;
389
390         }
391
392         if (conf->remove == FLUSH_ONE) {
393                 if (conf->dev_type == DEV_DEVMAP)
394                         dm_flush_map(conf->dev, DEFAULT_TARGET);
395                 else
396                         condlog(0, "must provide a map name to remove");
397
398                 goto out;
399         }
400         else if (conf->remove == FLUSH_ALL) {
401                 dm_flush_maps(DEFAULT_TARGET);
402                 goto out;
403         }
404         while ((r = configure()) < 0)
405                 condlog(3, "restart multipath configuration process");
406         
407 out:
408         free_config(conf);
409         dm_lib_release();
410         dm_lib_exit();
411 #ifdef _DEBUG_
412         dbg_free_final(NULL);
413 #endif
414         return r;
415 }