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