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