chiark / gitweb /
[PATCH] get part of callout return string
[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 static int
164 get_storageworks_wwid(int fd, char *str)
165 {
166         char buff[64];
167
168         if (0 == do_inq(fd, 0, 1, 0x83, buff, sizeof (buff), 1)) {
169                 sprint_wwid(str, &buff[8]);
170                 return 1;
171         }
172         return 0;
173 }
174
175 /* White list switch */
176 static int
177 get_unique_id(int fd, struct path * mypath)
178 {
179         if (strncmp(mypath->product_id, "HSV110 (C)COMPAQ", 16) == 0 ||
180             strncmp(mypath->product_id, "HSG80           ", 16) == 0) {
181                 get_storageworks_wwid(fd, mypath->wwid);
182                 return 0;
183         }
184
185         return 1;
186 }
187
188 static void
189 basename(char * str1, char * str2)
190 {
191         char *p = str1 + (strlen(str1) - 1);
192
193         while (*--p != '/')
194                 continue;
195         strcpy(str2, ++p);
196 }
197
198 static int
199 blacklist (char * dev) {
200         int i;
201         static struct {
202                 char * headstr;
203                 int lengh;
204         } blist[] = {
205                 {"cciss", 5},
206                 {"hd", 2},
207                 {"md", 2},
208                 {"dm", 2},
209                 {"sr", 2},
210                 {"scd", 3},
211                 {"ram", 3},
212                 {"raw", 3},
213                 {NULL, 0},
214         };
215
216         for (i = 0; blist[i].lengh; i++) {
217                 if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
218                         return 1;
219         }
220         return 0;
221 }
222
223 static int
224 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
225 {
226         int k=0;
227         int sg_fd;
228         struct sysfs_directory * sdir;
229         struct sysfs_directory * devp;
230         struct sysfs_link * linkp;
231         char buff[FILE_NAME_SIZE];
232
233         char block_path[FILE_NAME_SIZE];
234
235         sprintf(block_path, "%s/block", conf->sysfs_path);
236         sdir = sysfs_open_directory(block_path);
237         sysfs_read_directory(sdir);
238         dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
239                 if (blacklist(devp->name))
240                         continue;
241                 sysfs_read_directory(devp);
242                 if(devp->links == NULL)
243                         continue;
244                 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
245                         if (!strncmp(linkp->name, "device", 6))
246                                 break;
247                 }
248                 if (linkp == NULL) {
249                         continue;
250                 }
251
252                 basename(devp->path, buff);
253                 sprintf(all_paths[k].sg_dev, "/dev/%s", buff);
254                 strcpy(all_paths[k].dev, all_paths[k].sg_dev);
255                 if ((sg_fd = open(all_paths[k].sg_dev, O_RDONLY)) < 0) {
256                         if (conf->verbose)
257                                 fprintf(stderr, "can't open %s. mknod ?",
258                                         all_paths[k].sg_dev); 
259                         continue;
260                 }
261                 get_lun_strings(sg_fd, &all_paths[k]);
262                 get_unique_id(sg_fd, &all_paths[k]);
263                 all_paths[k].state = do_tur(sg_fd);
264                 close(sg_fd);
265                 basename(linkp->target, buff);
266                 sscanf(buff, "%i:%i:%i:%i",
267                         &all_paths[k].sg_id.host_no,
268                         &all_paths[k].sg_id.channel,
269                         &all_paths[k].sg_id.scsi_id,
270                         &all_paths[k].sg_id.lun);
271                 k++;
272         }
273         sysfs_close_directory(sdir);
274
275         return 0;
276 }
277
278 static int
279 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
280                       struct scsi_dev * all_scsi_ids)
281 {
282         int k, i, sg_fd;
283         char buff[FILE_NAME_SIZE];
284         char file_name[FILE_NAME_SIZE];
285
286         for (k = 0; k < conf->max_devs; k++) {
287                 strcpy(file_name, "/dev/sg");
288                 sprintf(buff, "%d", k);
289                 strncat(file_name, buff, FILE_NAME_SIZE);
290                 strcpy(all_paths[k].sg_dev, file_name);
291                 if ((sg_fd = open(file_name, O_RDONLY)) < 0) {
292                         if (conf->verbose)
293                                 fprintf(stderr, "can't open %s. mknod ?",
294                                         file_name); 
295                         continue;
296                 }
297                 get_lun_strings(sg_fd, &all_paths[k]);
298                 get_unique_id(sg_fd, &all_paths[k]);
299                 all_paths[k].state = do_tur(sg_fd);
300                 if (0 > ioctl(sg_fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
301                         printf("device %s failed on sg ioctl, skip\n",
302                                file_name);
303
304                 close(sg_fd);
305
306                 for (i = 0; i < conf->max_devs; i++) {
307                         if ((all_paths[k].sg_id.host_no ==
308                              all_scsi_ids[i].host_no)
309                             && (all_paths[k].sg_id.scsi_id ==
310                                 (all_scsi_ids[i].scsi_id.dev_id & 0xff))
311                             && (all_paths[k].sg_id.lun ==
312                                 ((all_scsi_ids[i].scsi_id.dev_id >> 8) & 0xff))
313                             && (all_paths[k].sg_id.channel ==
314                                 ((all_scsi_ids[i].scsi_id.
315                                   dev_id >> 16) & 0xff))) {
316                                 strcpy(all_paths[k].dev, all_scsi_ids[i].dev);
317                                 break;
318                         }
319                 }
320         }
321         return 0;
322 }
323
324 static int
325 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
326 {
327         int k, big, little, res, host_no, fd;
328         char buff[64];
329         char fname[FILE_NAME_SIZE];
330         struct scsi_idlun my_scsi_id;
331
332         for (k = 0; k < conf->max_devs; k++) {
333                 strcpy(fname, "/dev/sd");
334                 if (k < 26) {
335                         buff[0] = 'a' + (char) k;
336                         buff[1] = '\0';
337                         strcat(fname, buff);
338                 } else if (k <= 255) {  /* assumes sequence goes x,y,z,aa,ab,ac etc */
339                         big = k / 26;
340                         little = k - (26 * big);
341                         big = big - 1;
342
343                         buff[0] = 'a' + (char) big;
344                         buff[1] = 'a' + (char) little;
345                         buff[2] = '\0';
346                         strcat(fname, buff);
347                 } else
348                         strcat(fname, "xxxx");
349
350                 if ((fd = open(fname, O_RDONLY)) < 0) {
351                         if (conf->verbose)
352                                 fprintf(stderr, "can't open %s. mknod ?",
353                                         fname); 
354                         continue;
355                 }
356
357                 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
358                 if (res < 0) {
359                         close(fd);
360                         printf("Could not get scsi idlun\n");
361                         continue;
362                 }
363
364                 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
365                 if (res < 0) {
366                         close(fd);
367                         printf("Could not get host_no\n");
368                         continue;
369                 }
370
371                 close(fd);
372
373                 strcpy(all_scsi_ids[k].dev, fname);
374                 all_scsi_ids[k].scsi_id = my_scsi_id;
375                 all_scsi_ids[k].host_no = host_no;
376         }
377         return 0;
378 }
379
380 /* print_path style */
381 #define ALL     0
382 #define NOWWID  1
383
384 static void
385 print_path(struct path * all_paths, int k, int style)
386 {
387         if (style != NOWWID)
388                 printf("%s ", all_paths[k].wwid);
389         else
390                 printf(" \\_");
391         printf("(%i %i %i %i) ",
392                all_paths[k].sg_id.host_no,
393                all_paths[k].sg_id.channel,
394                all_paths[k].sg_id.scsi_id, all_paths[k].sg_id.lun);
395         printf("%s ", all_paths[k].sg_dev);
396         printf("op:%i ", all_paths[k].state);
397         printf("%s ", all_paths[k].dev);
398         printf("[%.16s]\n", all_paths[k].product_id);
399 }
400
401 static void
402 print_all_path(struct env * conf, struct path * all_paths)
403 {
404         int k;
405         char empty_buff[WWID_SIZE];
406
407         memset(empty_buff, 0, WWID_SIZE);
408         for (k = 0; k < conf->max_devs; k++) {
409                 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
410                         continue;
411                 print_path(all_paths, k, ALL);
412         }
413 }
414
415 static void
416 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
417 {
418         int k, i;
419
420         for (k = 0; k <= nmp; k++) {
421                 printf("%s\n", mp[k].wwid);
422                 for (i = 0; i <= mp[k].npaths; i++)
423                         print_path(all_paths, PINDEX(k,i), NOWWID);
424         }
425 }
426
427 static int
428 coalesce_paths(struct env * conf, struct multipath * mp,
429                struct path * all_paths)
430 {
431         int k, i, nmp, np, already_done;
432         char empty_buff[WWID_SIZE];
433
434         nmp = -1;
435         already_done = 0;
436         memset(empty_buff, 0, WWID_SIZE);
437
438         for (k = 0; k < conf->max_devs - 1; k++) {
439                 /* skip this path if no unique id has been found */
440                 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
441                         continue;
442                 np = 0;
443
444                 for (i = 0; i <= nmp; i++) {
445                         if (0 == strcmp(mp[i].wwid, all_paths[k].wwid))
446                                 already_done = 1;
447                 }
448
449                 if (already_done) {
450                         already_done = 0;
451                         continue;
452                 }
453
454                 nmp++;
455                 strcpy(mp[nmp].wwid, all_paths[k].wwid);
456                 PINDEX(nmp,np) = k;
457
458                 for (i = k + 1; i < conf->max_devs; i++) {
459                         if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
460                                 np++;
461                                 PINDEX(nmp,np) = i;
462                                 mp[nmp].npaths = np;
463                         }
464                 }
465         }
466         return nmp;
467 }
468
469 static long
470 get_disk_size (struct env * conf, char * dev) {
471         long size;
472         int fd;
473         char attr_path[FILE_NAME_SIZE];
474         char buff[FILE_NAME_SIZE];
475         char basedev[FILE_NAME_SIZE];
476
477         if(conf->with_sysfs) {
478                 basename(dev, basedev);
479                 sprintf(attr_path, "%s/block/%s/size",
480                         conf->sysfs_path, basedev);
481                 if (0 > sysfs_read_attribute_value(attr_path, buff,
482                                          FILE_NAME_SIZE * sizeof(char)))
483                         return -1;
484                 size = atoi(buff);
485                 return size;
486         } else {
487                 if ((fd = open(dev, O_RDONLY)) < 0)
488                         return -1;
489                 if(!ioctl(fd, BLKGETSIZE, &size))
490                         return size;
491         }
492         return -1;
493 }
494
495 static int
496 make_dm_node(char * str)
497 {
498         int r = 0;
499         char buff[FILE_NAME_SIZE];
500         struct dm_names * names;
501         unsigned next = 0;
502         struct dm_task *dmt;
503
504         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
505                 return 0;
506
507         if (!dm_task_run(dmt))
508                 goto out;
509
510         if (!(names = dm_task_get_names(dmt)))
511                 goto out;
512
513         if (!names->dev) {
514                 r = 1;
515                 goto out;
516         }
517
518         do {
519                 if (0 == strcmp(names->name, str))
520                         break;
521                 next = names->next;
522                 names = (void *) names + next;
523         } while (next);
524
525         sprintf(buff, "/dev/mapper/%s", str);
526         unlink(buff);
527         mknod(buff, 0600 | S_IFBLK, names->dev);
528
529         out:
530         dm_task_destroy(dmt);
531         return r;
532
533 }
534
535 /* future use ?
536 static int
537 del_map(char * str) {
538         struct dm_task *dmt;
539
540         if (!(dmt = dm_task_create(DM_DEVICE_REMOVE)))
541                 return 0;
542         if (!dm_task_set_name(dmt, str))
543                 goto delout;
544         if (!dm_task_run(dmt))
545                 goto delout;
546
547         printf("Deleted device map : %s\n", str);
548
549         delout:
550         dm_task_destroy(dmt);
551         return 1;
552 }
553 */
554
555 static int
556 add_map(struct env * conf, struct path * all_paths,
557         struct multipath * mp, int index, int op)
558 {
559         char params[255];
560         char * params_p;
561         struct dm_task *dmt;
562         int i, np;
563         long size = -1;
564
565         /* defaults for multipath target */
566         int dm_nr_path_args         = 2;
567         int dm_path_test_int        = 10;
568         char * dm_ps_name           = "round-robin";
569         int dm_ps_nr_args           = 2;
570         int dm_path_failback_int    = 10;
571         int dm_path_nr_fail         = 2;
572         int dm_ps_prio              = 1;
573         int dm_ps_min_io            = 2;
574
575
576         if (!(dmt = dm_task_create(op)))
577                 return 0;
578
579         if (!dm_task_set_name(dmt, mp[index].wwid))
580                 goto addout;
581         params_p = &params[0];
582
583         np = 0;
584         for (i=0; i<=mp[index].npaths; i++) {
585                 if ((1 == all_paths[PINDEX(index,i)].state) &&
586                         (0 == all_paths[PINDEX(index,i)].sg_id.scsi_type))
587                         np++;
588         }
589         if (np == 0)
590                 goto addout;
591         /* temporarily disable creation of single path maps */
592         /* Sistina should modify the target limits */
593         if (np < 2)
594                 goto addout;
595
596         params_p += sprintf(params_p, "%i %i %i %s %i",
597                             np, dm_nr_path_args, dm_path_test_int,
598                             dm_ps_name, dm_ps_nr_args);
599         
600         for (i=0; i<=mp[index].npaths; i++) {
601                 if (( 0 == all_paths[PINDEX(index,i)].state) ||
602                         (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type))
603                         continue;
604                 if (size < 0)
605                         size = get_disk_size(conf, all_paths[PINDEX(index,0)].dev);
606                 params_p += sprintf(params_p, " %s %i %i %i %i",
607                                     all_paths[PINDEX(index,i)].dev,
608                                     dm_path_failback_int, dm_path_nr_fail,
609                                     dm_ps_prio, dm_ps_min_io);
610         }
611
612         if (size < 0)
613                 goto addout;
614
615         if (!conf->quiet) {
616                 if (op == DM_DEVICE_RELOAD)
617                         printf("U|");
618                 if (op == DM_DEVICE_CREATE)
619                         printf("N|");
620                 printf("%s : 0 %li %s %s\n",
621                         mp[index].wwid, size, DM_TARGET, params);
622         }
623
624         if (!dm_task_add_target(dmt, 0, size, DM_TARGET, params))
625                 goto addout;
626
627         if (!dm_task_run(dmt))
628                 goto addout;
629
630
631         make_dm_node(mp[index].wwid);
632
633         addout:
634         dm_task_destroy(dmt);
635         return 1;
636 }
637
638 /*
639 static int
640 get_table(const char * str)
641 {
642         int r = 0;
643         struct dm_task *dmt;
644         void *next = NULL;
645         uint64_t start, length;
646         char *target_type = NULL;
647         char *params;
648
649         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
650                 return 0;
651
652         if (!dm_task_set_name(dmt, str))
653                 goto out;
654
655         if (!dm_task_run(dmt))
656                 goto out;
657
658         do {
659                 next = dm_get_next_target(dmt, next, &start, &length,
660                                           &target_type, &params);
661                 if (target_type) {
662                         printf("%" PRIu64 " %" PRIu64 " %s %s\n",
663                                start, length, target_type, params);
664                 }
665         } while (next);
666
667         r = 1;
668
669       out:
670         dm_task_destroy(dmt);
671         return r;
672
673 }
674 */
675
676 static int
677 map_present(char * str)
678 {
679         int r = 0;
680         struct dm_task *dmt;
681         struct dm_names *names;
682         unsigned next = 0;
683
684         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
685                 return 0;
686
687         if (!dm_task_run(dmt))
688                 goto out;
689
690         if (!(names = dm_task_get_names(dmt)))
691                 goto out;
692
693         if (!names->dev) {
694                 goto out;
695         }
696
697         do {
698                 if (0 == strcmp(names->name, str))
699                         r = 1;
700                 next = names->next;
701                 names = (void *) names + next;
702         } while (next);
703
704         out:
705         dm_task_destroy(dmt);
706         return r;
707 }
708
709 static void
710 usage(char * progname)
711 {
712         fprintf(stderr, VERSION_STRING);
713         fprintf(stderr, "Usage: %s [-v|-q] [-d] [-m max_devs]\n", progname);
714         fprintf(stderr, "\t-v\t\tverbose, print all paths and multipaths\n");
715         fprintf(stderr, "\t-q\t\tquiet, no output at all\n");
716         fprintf(stderr, "\t-d\t\tdry run, do not create or update devmaps\n");
717         fprintf(stderr, "\t-m max_devs\tscan {max_devs} devices at most\n");
718         exit(1);
719 }
720
721 static int
722 filepresent(char * run) {
723         struct stat buf;
724
725         if(!stat(run, &buf))
726                 return 1;
727         return 0;
728 }
729
730 int
731 main(int argc, char *argv[])
732 {
733         char * run = "/var/run/multipath.run";
734         char * resched = "/var/run/multipath.reschedule";
735         struct multipath * mp;
736         struct path * all_paths;
737         struct scsi_dev * all_scsi_ids;
738         struct env conf;
739         int i, k, nmp;
740
741         /* Default behaviour */
742         conf.max_devs = MAX_DEVS;
743         conf.dry_run = 0;       /* 1 == Do not Create/Update devmaps */
744         conf.verbose = 0;       /* 1 == Print all_paths and mp */
745         conf.quiet = 0;         /* 1 == Do not even print devmaps */
746         conf.with_sysfs = 0;    /* Default to compat / suboptimal behaviour */
747
748         /* kindly provided by libsysfs */
749         if (0 == sysfs_get_mnt_path(conf.sysfs_path, FILE_NAME_SIZE))
750                 conf.with_sysfs = 1;
751
752         for (i = 1; i < argc; ++i) {
753                 if (0 == strcmp("-v", argv[i])) {
754                         if (conf.quiet == 1)
755                                 usage(argv[0]);
756                         conf.verbose = 1;
757                 } else if (0 == strcmp("-m", argv[i])) {
758                         conf.max_devs = atoi(argv[++i]);
759                         if (conf.max_devs < 2)
760                                 usage(argv[0]);
761                 } else if (0 == strcmp("-q", argv[i])) {
762                         if (conf.verbose == 1)
763                                 usage(argv[0]);
764                         conf.quiet = 1;
765                 } else if (0 == strcmp("-d", argv[i]))
766                         conf.dry_run = 1;
767                 else if (*argv[i] == '-') {
768                         fprintf(stderr, "Unknown switch: %s\n", argv[i]);
769                         usage(argv[0]);
770                 } else if (*argv[i] != '-') {
771                         fprintf(stderr, "Unknown argument\n");
772                         usage(argv[0]);
773                 }
774
775         }
776
777         if (filepresent(run)) {
778                 if (conf.verbose) {
779                         fprintf(stderr, "Already running.\n");
780                         fprintf(stderr, "If you know what you do, please ");
781                         fprintf(stderr, "remove %s\n", run);
782                 }
783                 /* leave a trace that we were called while already running */
784                 open(resched, O_CREAT);
785                 return 1;
786         }
787
788         /* dynamic allocations */
789         mp = malloc(conf.max_devs * sizeof(struct multipath));
790         all_paths = malloc(conf.max_devs * sizeof(struct path));
791         all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
792         if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL) {
793                 unlink(run);
794                 exit(1);
795         }
796 start:
797         if(!open(run, O_CREAT))
798                 exit(1);
799
800         if (!conf.with_sysfs) {
801                 get_all_scsi_ids(&conf, all_scsi_ids);
802                 get_all_paths_nosysfs(&conf, all_paths, all_scsi_ids);
803         } else {
804                 get_all_paths_sysfs(&conf, all_paths);
805         }
806         nmp = coalesce_paths(&conf, mp, all_paths);
807
808         if (conf.verbose) {
809                 print_all_path(&conf, all_paths);
810                 fprintf(stdout, "\n");
811                 print_all_mp(all_paths, mp, nmp);
812                 fprintf(stdout, "\n");
813         }
814
815         if (conf.dry_run) {
816                 unlink(run);
817                 exit(0);
818         }
819
820         for (k=0; k<=nmp; k++) {
821                 if (map_present(mp[k].wwid)) {
822                         add_map(&conf, all_paths, mp, k, DM_DEVICE_RELOAD);
823                 } else {
824                         add_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);
825                 }
826         }
827         unlink(run);
828
829         /* start again if we were ask to during this process run */
830         /* ie. do not loose an event-asked run */
831         if (filepresent(resched)) {
832                 unlink(resched);
833                 goto start;
834         }
835         exit(0);
836 }