chiark / gitweb /
9402888c58a3c91872a11ab2b3796c54d4f1253b
[elogind.git] / extras / multipath / main.c
1 /*
2  * Soft:        Description here...
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 <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <linux/kdev_t.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <libsysfs.h>
29 #include "libdevmapper/libdevmapper.h"
30 #include "main.h"
31
32 static int
33 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
34        void *resp, int mx_resp_len, int noisy)
35 {
36         unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
37             { INQUIRY_CMD, 0, 0, 0, 0, 0 };
38         unsigned char sense_b[SENSE_BUFF_LEN];
39         struct sg_io_hdr io_hdr;
40
41         if (cmddt)
42                 inqCmdBlk[1] |= 2;
43         if (evpd)
44                 inqCmdBlk[1] |= 1;
45         inqCmdBlk[2] = (unsigned char) pg_op;
46         inqCmdBlk[4] = (unsigned char) mx_resp_len;
47         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
48         io_hdr.interface_id = 'S';
49         io_hdr.cmd_len = sizeof (inqCmdBlk);
50         io_hdr.mx_sb_len = sizeof (sense_b);
51         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
52         io_hdr.dxfer_len = mx_resp_len;
53         io_hdr.dxferp = resp;
54         io_hdr.cmdp = inqCmdBlk;
55         io_hdr.sbp = sense_b;
56         io_hdr.timeout = DEF_TIMEOUT;
57
58         if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
59                 perror("SG_IO (inquiry) error");
60                 return -1;
61         }
62
63         /* treat SG_ERR here to get rid of sg_err.[ch] */
64         io_hdr.status &= 0x7e;
65         if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
66             (0 == io_hdr.driver_status))
67                 return 0;
68         if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
69             (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
70             (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
71                 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
72                         int sense_key;
73                         unsigned char * sense_buffer = io_hdr.sbp;
74                         if (sense_buffer[0] & 0x2)
75                                 sense_key = sense_buffer[1] & 0xf;
76                         else
77                                 sense_key = sense_buffer[2] & 0xf;
78                         if(RECOVERED_ERROR == sense_key)
79                                 return 0;
80                 }
81         }
82         return -1;
83 }
84
85 static int
86 do_tur(int fd)
87 {
88         unsigned char turCmdBlk[TUR_CMD_LEN] = { 0x00, 0, 0, 0, 0, 0 };
89         struct sg_io_hdr io_hdr;
90         unsigned char sense_buffer[32];
91
92         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
93         io_hdr.interface_id = 'S';
94         io_hdr.cmd_len = sizeof (turCmdBlk);
95         io_hdr.mx_sb_len = sizeof (sense_buffer);
96         io_hdr.dxfer_direction = SG_DXFER_NONE;
97         io_hdr.cmdp = turCmdBlk;
98         io_hdr.sbp = sense_buffer;
99         io_hdr.timeout = 20000;
100         io_hdr.pack_id = 0;
101         if (ioctl(fd, SG_IO, &io_hdr) < 0) {
102                 close(fd);
103                 return 0;
104         }
105         if (io_hdr.info & SG_INFO_OK_MASK) {
106                 return 0;
107         }
108         return 1;
109 }
110
111 static void
112 sprint_wwid(char * buff, const char * str)
113 {
114         int i;
115         const char *p;
116         char *cursor;
117         unsigned char c;
118
119         p = str;
120         cursor = buff;
121         for (i = 0; i <= WWID_SIZE / 2 - 1; i++) {
122                 c = *p++;
123                 sprintf(cursor, "%.2x", (int) (unsigned char) c);
124                 cursor += 2;
125         }
126         buff[WWID_SIZE - 1] = '\0';
127 }
128
129 static int
130 get_lun_strings(int fd, struct path * mypath)
131 {
132         char buff[36];
133
134         if (0 == do_inq(fd, 0, 0, 0, buff, 36, 1)) {
135                 memcpy(mypath->vendor_id, &buff[8], 8);
136                 memcpy(mypath->product_id, &buff[16], 16);
137                 memcpy(mypath->rev, &buff[32], 4);
138                 return 1;
139         }
140         return 0;
141 }
142
143 /*
144 static int
145 get_serial (int fd, char * str)
146 {
147         char buff[MX_ALLOC_LEN + 1];
148         int len;
149
150         if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN, 0)) {
151                 len = buff[3];
152                 if (len > 0) {
153                         memcpy(str, buff + 4, len);
154                         buff[len] = '\0';
155                 }
156                 return 1;
157         }
158         return 0;
159 }
160 */
161
162 /* hardware vendor specifics : add support for new models below */
163
164 /* this one get EVPD page 0x83 off 8 */
165 /* tested ok with StorageWorks */
166 static int
167 get_evpd_wwid(int fd, char *str)
168 {
169         char buff[64];
170
171         if (0 == do_inq(fd, 0, 1, 0x83, buff, sizeof (buff), 1)) {
172                 sprint_wwid(str, &buff[8]);
173                 return 1;
174         }
175         return 0;
176 }
177
178 /* White list switch */
179 static int
180 get_unique_id(int fd, struct path * mypath)
181 {
182         int i;
183         static struct {
184                 char * vendor;
185                 char * product;
186                 int (*getuid) (int fd, char * wwid);
187         } wlist[] = {
188                 {"COMPAQ  ", "HSV110 (C)COMPAQ", &get_evpd_wwid},
189                 {"COMPAQ  ", "MSA1000         ", &get_evpd_wwid},
190                 {"COMPAQ  ", "MSA1000 VOLUME  ", &get_evpd_wwid},
191                 {"DEC     ", "HSG80           ", &get_evpd_wwid},
192                 {"HP      ", "HSV100          ", &get_evpd_wwid},
193                 {"HP      ", "A6189A          ", &get_evpd_wwid},
194                 {"HP      ", "OPEN-           ", &get_evpd_wwid},
195                 {"DDN     ", "SAN DataDirector", &get_evpd_wwid},
196                 {"FSC     ", "CentricStor     ", &get_evpd_wwid},
197                 {"HITACHI ", "DF400           ", &get_evpd_wwid},
198                 {"HITACHI ", "DF500           ", &get_evpd_wwid},
199                 {"HITACHI ", "DF600           ", &get_evpd_wwid},
200                 {"IBM     ", "ProFibre 4000R  ", &get_evpd_wwid},
201                 {"SGI     ", "TP9100          ", &get_evpd_wwid},
202                 {"SGI     ", "TP9300          ", &get_evpd_wwid},
203                 {"SGI     ", "TP9400          ", &get_evpd_wwid},
204                 {"SGI     ", "TP9500          ", &get_evpd_wwid},
205                 {NULL, NULL, NULL},
206         };
207
208         for (i = 0; wlist[i].vendor; i++) {
209                 if (strncmp(mypath->vendor_id, wlist[i].vendor, 8) == 0 &&
210                     strncmp(mypath->product_id, wlist[i].product, 16) == 0) {
211                         wlist[i].getuid(fd, mypath->wwid);
212                         return 0;
213                 }
214         }
215         return 1;
216 }
217
218 static void
219 basename(char * str1, char * str2)
220 {
221         char *p = str1 + (strlen(str1) - 1);
222
223         while (*--p != '/')
224                 continue;
225         strcpy(str2, ++p);
226 }
227
228 static int
229 blacklist (char * dev) {
230         int i;
231         static struct {
232                 char * headstr;
233                 int lengh;
234         } blist[] = {
235                 {"cciss", 5},
236                 {"hd", 2},
237                 {"md", 2},
238                 {"dm", 2},
239                 {"sr", 2},
240                 {"scd", 3},
241                 {"ram", 3},
242                 {"raw", 3},
243                 {NULL, 0},
244         };
245
246         for (i = 0; blist[i].lengh; i++) {
247                 if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
248                         return 1;
249         }
250         return 0;
251 }
252
253 static int
254 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
255 {
256         int k=0;
257         int sg_fd;
258         struct sysfs_directory * sdir;
259         struct sysfs_directory * devp;
260         struct sysfs_link * linkp;
261         char buff[FILE_NAME_SIZE];
262
263         char block_path[FILE_NAME_SIZE];
264
265         sprintf(block_path, "%s/block", conf->sysfs_path);
266         sdir = sysfs_open_directory(block_path);
267         sysfs_read_directory(sdir);
268         dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
269                 if (blacklist(devp->name))
270                         continue;
271                 sysfs_read_directory(devp);
272                 if(devp->links == NULL)
273                         continue;
274                 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
275                         if (!strncmp(linkp->name, "device", 6))
276                                 break;
277                 }
278                 if (linkp == NULL) {
279                         continue;
280                 }
281
282                 basename(devp->path, buff);
283                 sprintf(all_paths[k].sg_dev, "/dev/%s", buff);
284                 strcpy(all_paths[k].dev, all_paths[k].sg_dev);
285                 if ((sg_fd = open(all_paths[k].sg_dev, O_RDONLY)) < 0) {
286                         if (conf->verbose)
287                                 fprintf(stderr, "can't open %s. mknod ?",
288                                         all_paths[k].sg_dev); 
289                         continue;
290                 }
291                 get_lun_strings(sg_fd, &all_paths[k]);
292                 get_unique_id(sg_fd, &all_paths[k]);
293                 all_paths[k].state = do_tur(sg_fd);
294                 close(sg_fd);
295                 basename(linkp->target, buff);
296                 sscanf(buff, "%i:%i:%i:%i",
297                         &all_paths[k].sg_id.host_no,
298                         &all_paths[k].sg_id.channel,
299                         &all_paths[k].sg_id.scsi_id,
300                         &all_paths[k].sg_id.lun);
301                 k++;
302         }
303         sysfs_close_directory(sdir);
304
305         return 0;
306 }
307
308 static int
309 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
310                       struct scsi_dev * all_scsi_ids)
311 {
312         int k, i, sg_fd;
313         char buff[FILE_NAME_SIZE];
314         char file_name[FILE_NAME_SIZE];
315
316         for (k = 0; k < conf->max_devs; k++) {
317                 strcpy(file_name, "/dev/sg");
318                 sprintf(buff, "%d", k);
319                 strncat(file_name, buff, FILE_NAME_SIZE);
320                 strcpy(all_paths[k].sg_dev, file_name);
321                 if ((sg_fd = open(file_name, O_RDONLY)) < 0) {
322                         if (conf->verbose)
323                                 fprintf(stderr, "can't open %s. mknod ?",
324                                         file_name); 
325                         continue;
326                 }
327                 get_lun_strings(sg_fd, &all_paths[k]);
328                 get_unique_id(sg_fd, &all_paths[k]);
329                 all_paths[k].state = do_tur(sg_fd);
330                 if (0 > ioctl(sg_fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
331                         printf("device %s failed on sg ioctl, skip\n",
332                                file_name);
333
334                 close(sg_fd);
335
336                 for (i = 0; i < conf->max_devs; i++) {
337                         if ((all_paths[k].sg_id.host_no ==
338                              all_scsi_ids[i].host_no)
339                             && (all_paths[k].sg_id.scsi_id ==
340                                 (all_scsi_ids[i].scsi_id.dev_id & 0xff))
341                             && (all_paths[k].sg_id.lun ==
342                                 ((all_scsi_ids[i].scsi_id.dev_id >> 8) & 0xff))
343                             && (all_paths[k].sg_id.channel ==
344                                 ((all_scsi_ids[i].scsi_id.
345                                   dev_id >> 16) & 0xff))) {
346                                 strcpy(all_paths[k].dev, all_scsi_ids[i].dev);
347                                 break;
348                         }
349                 }
350         }
351         return 0;
352 }
353
354 static int
355 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
356 {
357         int k, big, little, res, host_no, fd;
358         char buff[64];
359         char fname[FILE_NAME_SIZE];
360         struct scsi_idlun my_scsi_id;
361
362         for (k = 0; k < conf->max_devs; k++) {
363                 strcpy(fname, "/dev/sd");
364                 if (k < 26) {
365                         buff[0] = 'a' + (char) k;
366                         buff[1] = '\0';
367                         strcat(fname, buff);
368                 } else if (k <= 255) {  /* assumes sequence goes x,y,z,aa,ab,ac etc */
369                         big = k / 26;
370                         little = k - (26 * big);
371                         big = big - 1;
372
373                         buff[0] = 'a' + (char) big;
374                         buff[1] = 'a' + (char) little;
375                         buff[2] = '\0';
376                         strcat(fname, buff);
377                 } else
378                         strcat(fname, "xxxx");
379
380                 if ((fd = open(fname, O_RDONLY)) < 0) {
381                         if (conf->verbose)
382                                 fprintf(stderr, "can't open %s. mknod ?",
383                                         fname); 
384                         continue;
385                 }
386
387                 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
388                 if (res < 0) {
389                         close(fd);
390                         printf("Could not get scsi idlun\n");
391                         continue;
392                 }
393
394                 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
395                 if (res < 0) {
396                         close(fd);
397                         printf("Could not get host_no\n");
398                         continue;
399                 }
400
401                 close(fd);
402
403                 strcpy(all_scsi_ids[k].dev, fname);
404                 all_scsi_ids[k].scsi_id = my_scsi_id;
405                 all_scsi_ids[k].host_no = host_no;
406         }
407         return 0;
408 }
409
410 /* print_path style */
411 #define ALL     0
412 #define NOWWID  1
413
414 static void
415 print_path(struct path * all_paths, int k, int style)
416 {
417         if (style != NOWWID)
418                 printf("%s ", all_paths[k].wwid);
419         else
420                 printf(" \\_");
421         printf("(%i %i %i %i) ",
422                all_paths[k].sg_id.host_no,
423                all_paths[k].sg_id.channel,
424                all_paths[k].sg_id.scsi_id, all_paths[k].sg_id.lun);
425         printf("%s ", all_paths[k].sg_dev);
426         printf("op:%i ", all_paths[k].state);
427         printf("%s ", all_paths[k].dev);
428         printf("[%.16s]\n", all_paths[k].product_id);
429 }
430
431 static void
432 print_all_path(struct env * conf, struct path * all_paths)
433 {
434         int k;
435         char empty_buff[WWID_SIZE];
436
437         memset(empty_buff, 0, WWID_SIZE);
438         for (k = 0; k < conf->max_devs; k++) {
439                 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
440                         continue;
441                 print_path(all_paths, k, ALL);
442         }
443 }
444
445 static void
446 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
447 {
448         int k, i;
449
450         for (k = 0; k <= nmp; k++) {
451                 printf("%s\n", mp[k].wwid);
452                 for (i = 0; i <= mp[k].npaths; i++)
453                         print_path(all_paths, PINDEX(k,i), NOWWID);
454         }
455 }
456
457 static int
458 coalesce_paths(struct env * conf, struct multipath * mp,
459                struct path * all_paths)
460 {
461         int k, i, nmp, np, already_done;
462         char empty_buff[WWID_SIZE];
463
464         nmp = -1;
465         already_done = 0;
466         memset(empty_buff, 0, WWID_SIZE);
467
468         for (k = 0; k < conf->max_devs - 1; k++) {
469                 /* skip this path if no unique id has been found */
470                 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
471                         continue;
472                 np = 0;
473
474                 for (i = 0; i <= nmp; i++) {
475                         if (0 == strcmp(mp[i].wwid, all_paths[k].wwid))
476                                 already_done = 1;
477                 }
478
479                 if (already_done) {
480                         already_done = 0;
481                         continue;
482                 }
483
484                 nmp++;
485                 strcpy(mp[nmp].wwid, all_paths[k].wwid);
486                 PINDEX(nmp,np) = k;
487
488                 for (i = k + 1; i < conf->max_devs; i++) {
489                         if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
490                                 np++;
491                                 PINDEX(nmp,np) = i;
492                                 mp[nmp].npaths = np;
493                         }
494                 }
495         }
496         return nmp;
497 }
498
499 static long
500 get_disk_size (struct env * conf, char * dev) {
501         long size;
502         int fd;
503         char attr_path[FILE_NAME_SIZE];
504         char buff[FILE_NAME_SIZE];
505         char basedev[FILE_NAME_SIZE];
506
507         if(conf->with_sysfs) {
508                 basename(dev, basedev);
509                 sprintf(attr_path, "%s/block/%s/size",
510                         conf->sysfs_path, basedev);
511                 if (0 > sysfs_read_attribute_value(attr_path, buff,
512                                          FILE_NAME_SIZE * sizeof(char)))
513                         return -1;
514                 size = atoi(buff);
515                 return size;
516         } else {
517                 if ((fd = open(dev, O_RDONLY)) < 0)
518                         return -1;
519                 if(!ioctl(fd, BLKGETSIZE, &size))
520                         return size;
521         }
522         return -1;
523 }
524
525 static int
526 make_dm_node(char * str)
527 {
528         int r = 0;
529         char buff[FILE_NAME_SIZE];
530         struct dm_names * names;
531         unsigned next = 0;
532         struct dm_task *dmt;
533
534         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
535                 return 0;
536
537         if (!dm_task_run(dmt))
538                 goto out;
539
540         if (!(names = dm_task_get_names(dmt)))
541                 goto out;
542
543         if (!names->dev) {
544                 r = 1;
545                 goto out;
546         }
547
548         do {
549                 if (0 == strcmp(names->name, str))
550                         break;
551                 next = names->next;
552                 names = (void *) names + next;
553         } while (next);
554
555         sprintf(buff, "/dev/mapper/%s", str);
556         unlink(buff);
557         mknod(buff, 0600 | S_IFBLK, names->dev);
558
559         out:
560         dm_task_destroy(dmt);
561         return r;
562
563 }
564
565 /* future use ?
566 static int
567 del_map(char * str) {
568         struct dm_task *dmt;
569
570         if (!(dmt = dm_task_create(DM_DEVICE_REMOVE)))
571                 return 0;
572         if (!dm_task_set_name(dmt, str))
573                 goto delout;
574         if (!dm_task_run(dmt))
575                 goto delout;
576
577         printf("Deleted device map : %s\n", str);
578
579         delout:
580         dm_task_destroy(dmt);
581         return 1;
582 }
583 */
584
585 static int
586 add_map(struct env * conf, struct path * all_paths,
587         struct multipath * mp, int index, int op)
588 {
589         char params[255];
590         char * params_p;
591         struct dm_task *dmt;
592         int i, np;
593         long size = -1;
594
595         /* defaults for multipath target */
596         int dm_nr_path_args         = 2;
597         int dm_path_test_int        = 10;
598         char * dm_ps_name           = "round-robin";
599         int dm_ps_nr_args           = 2;
600         int dm_path_failback_int    = 10;
601         int dm_path_nr_fail         = 2;
602         int dm_ps_prio              = 1;
603         int dm_ps_min_io            = 2;
604
605
606         if (!(dmt = dm_task_create(op)))
607                 return 0;
608
609         if (!dm_task_set_name(dmt, mp[index].wwid))
610                 goto addout;
611         params_p = &params[0];
612
613         np = 0;
614         for (i=0; i<=mp[index].npaths; i++) {
615                 if ((1 == all_paths[PINDEX(index,i)].state) &&
616                         (0 == all_paths[PINDEX(index,i)].sg_id.scsi_type))
617                         np++;
618         }
619         if (np == 0)
620                 goto addout;
621         /* temporarily disable creation of single path maps */
622         /* Sistina should modify the target limits */
623         if (np < 2)
624                 goto addout;
625
626         params_p += sprintf(params_p, "%i %i %i %s %i",
627                             np, dm_nr_path_args, dm_path_test_int,
628                             dm_ps_name, dm_ps_nr_args);
629         
630         for (i=0; i<=mp[index].npaths; i++) {
631                 if (( 0 == all_paths[PINDEX(index,i)].state) ||
632                         (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type))
633                         continue;
634                 if (size < 0)
635                         size = get_disk_size(conf, all_paths[PINDEX(index,0)].dev);
636                 params_p += sprintf(params_p, " %s %i %i %i %i",
637                                     all_paths[PINDEX(index,i)].dev,
638                                     dm_path_failback_int, dm_path_nr_fail,
639                                     dm_ps_prio, dm_ps_min_io);
640         }
641
642         if (size < 0)
643                 goto addout;
644
645         if (!conf->quiet) {
646                 if (op == DM_DEVICE_RELOAD)
647                         printf("U|");
648                 if (op == DM_DEVICE_CREATE)
649                         printf("N|");
650                 printf("%s : 0 %li %s %s\n",
651                         mp[index].wwid, size, DM_TARGET, params);
652         }
653
654         if (!dm_task_add_target(dmt, 0, size, DM_TARGET, params))
655                 goto addout;
656
657         if (!dm_task_run(dmt))
658                 goto addout;
659
660
661         make_dm_node(mp[index].wwid);
662
663         addout:
664         dm_task_destroy(dmt);
665         return 1;
666 }
667
668 /*
669 static int
670 get_table(const char * str)
671 {
672         int r = 0;
673         struct dm_task *dmt;
674         void *next = NULL;
675         uint64_t start, length;
676         char *target_type = NULL;
677         char *params;
678
679         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
680                 return 0;
681
682         if (!dm_task_set_name(dmt, str))
683                 goto out;
684
685         if (!dm_task_run(dmt))
686                 goto out;
687
688         do {
689                 next = dm_get_next_target(dmt, next, &start, &length,
690                                           &target_type, &params);
691                 if (target_type) {
692                         printf("%" PRIu64 " %" PRIu64 " %s %s\n",
693                                start, length, target_type, params);
694                 }
695         } while (next);
696
697         r = 1;
698
699       out:
700         dm_task_destroy(dmt);
701         return r;
702
703 }
704 */
705
706 static int
707 map_present(char * str)
708 {
709         int r = 0;
710         struct dm_task *dmt;
711         struct dm_names *names;
712         unsigned next = 0;
713
714         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
715                 return 0;
716
717         if (!dm_task_run(dmt))
718                 goto out;
719
720         if (!(names = dm_task_get_names(dmt)))
721                 goto out;
722
723         if (!names->dev) {
724                 goto out;
725         }
726
727         do {
728                 if (0 == strcmp(names->name, str))
729                         r = 1;
730                 next = names->next;
731                 names = (void *) names + next;
732         } while (next);
733
734         out:
735         dm_task_destroy(dmt);
736         return r;
737 }
738
739 static void
740 usage(char * progname)
741 {
742         fprintf(stderr, VERSION_STRING);
743         fprintf(stderr, "Usage: %s [-v|-q] [-d] [-m max_devs]\n", progname);
744         fprintf(stderr, "\t-v\t\tverbose, print all paths and multipaths\n");
745         fprintf(stderr, "\t-q\t\tquiet, no output at all\n");
746         fprintf(stderr, "\t-d\t\tdry run, do not create or update devmaps\n");
747         fprintf(stderr, "\t-m max_devs\tscan {max_devs} devices at most\n");
748         exit(1);
749 }
750
751 static int
752 filepresent(char * run) {
753         struct stat buf;
754
755         if(!stat(run, &buf))
756                 return 1;
757         return 0;
758 }
759
760 int
761 main(int argc, char *argv[])
762 {
763         char * run = "/var/run/multipath.run";
764         char * resched = "/var/run/multipath.reschedule";
765         struct multipath * mp;
766         struct path * all_paths;
767         struct scsi_dev * all_scsi_ids;
768         struct env conf;
769         int i, k, nmp;
770
771         /* Default behaviour */
772         conf.max_devs = MAX_DEVS;
773         conf.dry_run = 0;       /* 1 == Do not Create/Update devmaps */
774         conf.verbose = 0;       /* 1 == Print all_paths and mp */
775         conf.quiet = 0;         /* 1 == Do not even print devmaps */
776         conf.with_sysfs = 0;    /* Default to compat / suboptimal behaviour */
777
778         /* kindly provided by libsysfs */
779         if (0 == sysfs_get_mnt_path(conf.sysfs_path, FILE_NAME_SIZE))
780                 conf.with_sysfs = 1;
781
782         for (i = 1; i < argc; ++i) {
783                 if (0 == strcmp("-v", argv[i])) {
784                         if (conf.quiet == 1)
785                                 usage(argv[0]);
786                         conf.verbose = 1;
787                 } else if (0 == strcmp("-m", argv[i])) {
788                         conf.max_devs = atoi(argv[++i]);
789                         if (conf.max_devs < 2)
790                                 usage(argv[0]);
791                 } else if (0 == strcmp("-q", argv[i])) {
792                         if (conf.verbose == 1)
793                                 usage(argv[0]);
794                         conf.quiet = 1;
795                 } else if (0 == strcmp("-d", argv[i]))
796                         conf.dry_run = 1;
797                 else if (*argv[i] == '-') {
798                         fprintf(stderr, "Unknown switch: %s\n", argv[i]);
799                         usage(argv[0]);
800                 } else if (*argv[i] != '-') {
801                         fprintf(stderr, "Unknown argument\n");
802                         usage(argv[0]);
803                 }
804
805         }
806
807         if (filepresent(run)) {
808                 if (conf.verbose) {
809                         fprintf(stderr, "Already running.\n");
810                         fprintf(stderr, "If you know what you do, please ");
811                         fprintf(stderr, "remove %s\n", run);
812                 }
813                 /* leave a trace that we were called while already running */
814                 open(resched, O_CREAT);
815                 return 1;
816         }
817
818         /* dynamic allocations */
819         mp = malloc(conf.max_devs * sizeof(struct multipath));
820         all_paths = malloc(conf.max_devs * sizeof(struct path));
821         all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
822         if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL) {
823                 unlink(run);
824                 exit(1);
825         }
826 start:
827         if(!open(run, O_CREAT))
828                 exit(1);
829
830         if (!conf.with_sysfs) {
831                 get_all_scsi_ids(&conf, all_scsi_ids);
832                 get_all_paths_nosysfs(&conf, all_paths, all_scsi_ids);
833         } else {
834                 get_all_paths_sysfs(&conf, all_paths);
835         }
836         nmp = coalesce_paths(&conf, mp, all_paths);
837
838         if (conf.verbose) {
839                 print_all_path(&conf, all_paths);
840                 fprintf(stdout, "\n");
841                 print_all_mp(all_paths, mp, nmp);
842                 fprintf(stdout, "\n");
843         }
844
845         if (conf.dry_run) {
846                 unlink(run);
847                 exit(0);
848         }
849
850         for (k=0; k<=nmp; k++) {
851                 if (map_present(mp[k].wwid)) {
852                         add_map(&conf, all_paths, mp, k, DM_DEVICE_RELOAD);
853                 } else {
854                         add_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);
855                 }
856         }
857         unlink(run);
858
859         /* start again if we were ask to during this process run */
860         /* ie. do not loose an event-asked run */
861         if (filepresent(resched)) {
862                 unlink(resched);
863                 goto start;
864         }
865         exit(0);
866 }