chiark / gitweb /
c57cd4fe7eb703873df47dbdbb1428637242921d
[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 void
86 sprint_wwid(char * buff, const char * str)
87 {
88         int i;
89         const char *p;
90         char *cursor;
91         unsigned char c;
92
93         p = str;
94         cursor = buff;
95         for (i = 0; i <= WWID_SIZE / 2 - 1; i++) {
96                 c = *p++;
97                 sprintf(cursor, "%.2x", (int) (unsigned char) c);
98                 cursor += 2;
99         }
100         buff[WWID_SIZE - 1] = '\0';
101 }
102
103 static int
104 get_lun_strings(int fd, struct path * mypath)
105 {
106         char buff[36];
107
108         if (0 == do_inq(fd, 0, 0, 0, buff, 36, 1)) {
109                 memcpy(mypath->vendor_id, &buff[8], 8);
110                 memcpy(mypath->product_id, &buff[16], 16);
111                 memcpy(mypath->rev, &buff[32], 4);
112                 return 1;
113         }
114         return 0;
115 }
116
117 /*
118 static int
119 get_serial (int fd, char * str)
120 {
121         char buff[MX_ALLOC_LEN + 1];
122         int len;
123
124         if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN, 0)) {
125                 len = buff[3];
126                 if (len > 0) {
127                         memcpy(str, buff + 4, len);
128                         buff[len] = '\0';
129                 }
130                 return 1;
131         }
132         return 0;
133 }
134 */
135
136 /* hardware vendor specifics : add support for new models below */
137
138 /* this one get EVPD page 0x83 off 8 */
139 /* tested ok with StorageWorks */
140 static int
141 get_evpd_wwid(int fd, char *str)
142 {
143         char buff[64];
144
145         if (0 == do_inq(fd, 0, 1, 0x83, buff, sizeof (buff), 1)) {
146                 sprint_wwid(str, &buff[8]);
147                 return 1;
148         }
149         return 0;
150 }
151
152 /* White list switch */
153 static int
154 get_unique_id(int fd, struct path * mypath)
155 {
156         int i;
157         static struct {
158                 char * vendor;
159                 char * product;
160                 int iopolicy;
161                 int (*getuid) (int fd, char * wwid);
162         } wlist[] = {
163                 {"COMPAQ  ", "HSV110 (C)COMPAQ", MULTIBUS, &get_evpd_wwid},
164                 {"COMPAQ  ", "MSA1000         ", MULTIBUS, &get_evpd_wwid},
165                 {"COMPAQ  ", "MSA1000 VOLUME  ", MULTIBUS, &get_evpd_wwid},
166                 {"DEC     ", "HSG80           ", MULTIBUS, &get_evpd_wwid},
167                 {"HP      ", "HSV100          ", MULTIBUS, &get_evpd_wwid},
168                 {"HP      ", "A6189A          ", MULTIBUS, &get_evpd_wwid},
169                 {"HP      ", "OPEN-           ", MULTIBUS, &get_evpd_wwid},
170                 {"DDN     ", "SAN DataDirector", MULTIBUS, &get_evpd_wwid},
171                 {"FSC     ", "CentricStor     ", MULTIBUS, &get_evpd_wwid},
172                 {"HITACHI ", "DF400           ", MULTIBUS, &get_evpd_wwid},
173                 {"HITACHI ", "DF500           ", MULTIBUS, &get_evpd_wwid},
174                 {"HITACHI ", "DF600           ", MULTIBUS, &get_evpd_wwid},
175                 {"IBM     ", "ProFibre 4000R  ", MULTIBUS, &get_evpd_wwid},
176                 {"SGI     ", "TP9100          ", MULTIBUS, &get_evpd_wwid},
177                 {"SGI     ", "TP9300          ", MULTIBUS, &get_evpd_wwid},
178                 {"SGI     ", "TP9400          ", MULTIBUS, &get_evpd_wwid},
179                 {"SGI     ", "TP9500          ", MULTIBUS, &get_evpd_wwid},
180                 {NULL, NULL, 0, NULL},
181         };
182
183         for (i = 0; wlist[i].vendor; i++) {
184                 if (strncmp(mypath->vendor_id, wlist[i].vendor, 8) == 0 &&
185                     strncmp(mypath->product_id, wlist[i].product, 16) == 0) {
186                         wlist[i].getuid(fd, mypath->wwid);
187                         mypath->iopolicy = wlist[i].iopolicy;
188                         return 0;
189                 }
190         }
191         return 1;
192 }
193
194 static void
195 basename(char * str1, char * str2)
196 {
197         char *p = str1 + (strlen(str1) - 1);
198
199         while (*--p != '/')
200                 continue;
201         strcpy(str2, ++p);
202 }
203
204 static int
205 blacklist (char * dev) {
206         int i;
207         static struct {
208                 char * headstr;
209                 int lengh;
210         } blist[] = {
211                 {"cciss", 5},
212                 {"hd", 2},
213                 {"md", 2},
214                 {"dm", 2},
215                 {"sr", 2},
216                 {"scd", 3},
217                 {"ram", 3},
218                 {"raw", 3},
219                 {NULL, 0},
220         };
221
222         for (i = 0; blist[i].lengh; i++) {
223                 if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
224                         return 1;
225         }
226         return 0;
227 }
228
229 static int
230 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
231 {
232         int k=0;
233         int sg_fd;
234         struct sysfs_directory * sdir;
235         struct sysfs_directory * devp;
236         struct sysfs_link * linkp;
237         char refwwid[WWID_SIZE];
238         char empty_buff[WWID_SIZE];
239         char buff[FILE_NAME_SIZE];
240         char path[FILE_NAME_SIZE];
241         struct path curpath;
242
243         /* if called from hotplug, only consider the paths that relate to */
244         /* to the device pointed by conf.hotplugdev */
245         memset(empty_buff, 0, WWID_SIZE);
246         memset(refwwid, 0, WWID_SIZE);
247         if (strncmp("/devices", conf->hotplugdev, 8) == 0) {
248                 sprintf(buff, "%s%s/block",
249                         conf->sysfs_path, conf->hotplugdev);
250                 memset(conf->hotplugdev, 0, FILE_NAME_SIZE);
251                 readlink(buff, conf->hotplugdev, FILE_NAME_SIZE);
252                 basename(conf->hotplugdev, buff);
253                 sprintf(curpath.sg_dev, "/dev/%s", buff);
254
255                 if ((sg_fd = open(curpath.sg_dev, O_RDONLY)) < 0)
256                         exit(1);
257
258                 get_lun_strings(sg_fd, &curpath);
259                 get_unique_id(sg_fd, &curpath);
260                 strcpy(refwwid, curpath.wwid);
261                 memset(&curpath, 0, sizeof(path));
262         }
263
264         sprintf(path, "%s/block", conf->sysfs_path);
265         sdir = sysfs_open_directory(path);
266         sysfs_read_directory(sdir);
267
268         dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
269                 if (blacklist(devp->name))
270                         continue;
271
272                 sysfs_read_directory(devp);
273
274                 if(devp->links == NULL)
275                         continue;
276
277                 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
278                         if (!strncmp(linkp->name, "device", 6))
279                                 break;
280                 }
281
282                 if (linkp == NULL) {
283                         continue;
284                 }
285
286                 basename(devp->path, buff);
287                 sprintf(curpath.sg_dev, "/dev/%s", buff);
288
289                 if ((sg_fd = open(curpath.sg_dev, O_RDONLY)) < 0) {
290                         if (conf->verbose)
291                                 fprintf(stderr, "can't open %s\n",
292                                         curpath.sg_dev); 
293                         continue;
294                 }
295
296                 get_lun_strings(sg_fd, &curpath);
297                 get_unique_id(sg_fd, &curpath);
298
299                 if (memcmp(empty_buff, refwwid, WWID_SIZE) != 0 && 
300                     strncmp(curpath.wwid, refwwid, WWID_SIZE) != 0) {
301                         memset(&curpath, 0, sizeof(path));
302                         continue;
303                 }
304
305                 strcpy(all_paths[k].sg_dev, curpath.sg_dev);
306                 strcpy(all_paths[k].dev, curpath.sg_dev);
307                 strcpy(all_paths[k].wwid, curpath.wwid);
308                 strcpy(all_paths[k].vendor_id, curpath.vendor_id);
309                 strcpy(all_paths[k].product_id, curpath.product_id);
310                 all_paths[k].iopolicy = curpath.iopolicy;
311
312                 /* done with curpath, zero for reuse */
313                 memset(&curpath, 0, sizeof(path));
314
315                 close(sg_fd);
316                 basename(linkp->target, buff);
317                 sscanf(buff, "%i:%i:%i:%i",
318                         &all_paths[k].sg_id.host_no,
319                         &all_paths[k].sg_id.channel,
320                         &all_paths[k].sg_id.scsi_id,
321                         &all_paths[k].sg_id.lun);
322                 k++;
323         }
324         sysfs_close_directory(sdir);
325         return 0;
326 }
327
328 static int
329 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
330                       struct scsi_dev * all_scsi_ids)
331 {
332         int k, i, sg_fd;
333         char buff[FILE_NAME_SIZE];
334         char file_name[FILE_NAME_SIZE];
335
336         for (k = 0; k < conf->max_devs; k++) {
337                 strcpy(file_name, "/dev/sg");
338                 sprintf(buff, "%d", k);
339                 strncat(file_name, buff, FILE_NAME_SIZE);
340                 strcpy(all_paths[k].sg_dev, file_name);
341                 if ((sg_fd = open(file_name, O_RDONLY)) < 0) {
342                         if (conf->verbose)
343                                 fprintf(stderr, "can't open %s. mknod ?",
344                                         file_name); 
345                         continue;
346                 }
347                 get_lun_strings(sg_fd, &all_paths[k]);
348                 get_unique_id(sg_fd, &all_paths[k]);
349                 if (0 > ioctl(sg_fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
350                         printf("device %s failed on sg ioctl, skip\n",
351                                file_name);
352
353                 close(sg_fd);
354
355                 for (i = 0; i < conf->max_devs; i++) {
356                         if ((all_paths[k].sg_id.host_no ==
357                              all_scsi_ids[i].host_no)
358                             && (all_paths[k].sg_id.scsi_id ==
359                                 (all_scsi_ids[i].scsi_id.dev_id & 0xff))
360                             && (all_paths[k].sg_id.lun ==
361                                 ((all_scsi_ids[i].scsi_id.dev_id >> 8) & 0xff))
362                             && (all_paths[k].sg_id.channel ==
363                                 ((all_scsi_ids[i].scsi_id.
364                                   dev_id >> 16) & 0xff))) {
365                                 strcpy(all_paths[k].dev, all_scsi_ids[i].dev);
366                                 break;
367                         }
368                 }
369         }
370         return 0;
371 }
372
373 static int
374 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
375 {
376         int k, big, little, res, host_no, fd;
377         char buff[64];
378         char fname[FILE_NAME_SIZE];
379         struct scsi_idlun my_scsi_id;
380
381         for (k = 0; k < conf->max_devs; k++) {
382                 strcpy(fname, "/dev/sd");
383                 if (k < 26) {
384                         buff[0] = 'a' + (char) k;
385                         buff[1] = '\0';
386                         strcat(fname, buff);
387                 } else if (k <= 255) {
388                         /* assumes sequence goes x,y,z,aa,ab,ac etc */
389                         big = k / 26;
390                         little = k - (26 * big);
391                         big = big - 1;
392
393                         buff[0] = 'a' + (char) big;
394                         buff[1] = 'a' + (char) little;
395                         buff[2] = '\0';
396                         strcat(fname, buff);
397                 } else
398                         strcat(fname, "xxxx");
399
400                 if ((fd = open(fname, O_RDONLY)) < 0) {
401                         if (conf->verbose)
402                                 fprintf(stderr, "can't open %s. mknod ?",
403                                         fname); 
404                         continue;
405                 }
406
407                 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
408                 if (res < 0) {
409                         close(fd);
410                         printf("Could not get scsi idlun\n");
411                         continue;
412                 }
413
414                 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
415                 if (res < 0) {
416                         close(fd);
417                         printf("Could not get host_no\n");
418                         continue;
419                 }
420
421                 close(fd);
422
423                 strcpy(all_scsi_ids[k].dev, fname);
424                 all_scsi_ids[k].scsi_id = my_scsi_id;
425                 all_scsi_ids[k].host_no = host_no;
426         }
427         return 0;
428 }
429
430 /* print_path style */
431 #define ALL     0
432 #define NOWWID  1
433
434 static void
435 print_path(struct path * all_paths, int k, int style)
436 {
437         if (style != NOWWID)
438                 printf("%s ", all_paths[k].wwid);
439         else
440                 printf(" \\_");
441         printf("(%i %i %i %i) ",
442                all_paths[k].sg_id.host_no,
443                all_paths[k].sg_id.channel,
444                all_paths[k].sg_id.scsi_id, all_paths[k].sg_id.lun);
445         if(0 != strcmp(all_paths[k].sg_dev, all_paths[k].dev))
446                 printf("%s ", all_paths[k].sg_dev);
447         printf("%s ", all_paths[k].dev);
448         printf("[%.16s]\n", all_paths[k].product_id);
449 }
450
451 static void
452 print_all_path(struct env * conf, struct path * all_paths)
453 {
454         int k;
455         char empty_buff[WWID_SIZE];
456
457         memset(empty_buff, 0, WWID_SIZE);
458         for (k = 0; k < conf->max_devs; k++) {
459                 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
460                         continue;
461                 print_path(all_paths, k, ALL);
462         }
463 }
464
465 static void
466 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
467 {
468         int k, i;
469
470         for (k = 0; k <= nmp; k++) {
471                 printf("%s\n", mp[k].wwid);
472                 for (i = 0; i <= mp[k].npaths; i++)
473                         print_path(all_paths, PINDEX(k,i), NOWWID);
474         }
475 }
476
477 static long
478 get_disk_size (struct env * conf, char * dev) {
479         long size;
480         int fd;
481         char attr_path[FILE_NAME_SIZE];
482         char buff[FILE_NAME_SIZE];
483         char basedev[FILE_NAME_SIZE];
484
485         if(conf->with_sysfs) {
486                 basename(dev, basedev);
487                 sprintf(attr_path, "%s/block/%s/size",
488                         conf->sysfs_path, basedev);
489                 if (0 > sysfs_read_attribute_value(attr_path, buff,
490                                          FILE_NAME_SIZE * sizeof(char)))
491                         return -1;
492                 size = atoi(buff);
493                 return size;
494         } else {
495                 if ((fd = open(dev, O_RDONLY)) < 0)
496                         return -1;
497                 if(!ioctl(fd, BLKGETSIZE, &size))
498                         return size;
499         }
500         return -1;
501 }
502
503 static int
504 coalesce_paths(struct env * conf, struct multipath * mp,
505                struct path * all_paths)
506 {
507         int k, i, nmp, np, already_done;
508         char empty_buff[WWID_SIZE];
509
510         nmp = -1;
511         already_done = 0;
512         memset(empty_buff, 0, WWID_SIZE);
513
514         for (k = 0; k < conf->max_devs - 1; k++) {
515                 /* skip this path for some reason */
516
517                 /* 1. if path has no unique id */
518                 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
519                         continue;
520
521                 /* 2. mp with this uid already instanciated */
522                 for (i = 0; i <= nmp; i++) {
523                         if (0 == strcmp(mp[i].wwid, all_paths[k].wwid))
524                                 already_done = 1;
525                 }
526                 if (already_done) {
527                         already_done = 0;
528                         continue;
529                 }
530
531                 /* at this point, we know we really got a new mp */
532                 np = 0;
533                 nmp++;
534                 strcpy(mp[nmp].wwid, all_paths[k].wwid);
535                 PINDEX(nmp,np) = k;
536
537                 if (mp[nmp].size == 0)
538                         mp[nmp].size = get_disk_size(conf, all_paths[k].dev);
539
540                 for (i = k + 1; i < conf->max_devs; i++) {
541                         if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
542                                 np++;
543                                 PINDEX(nmp,np) = i;
544                                 mp[nmp].npaths = np;
545                         }
546                 }
547         }
548         return nmp;
549 }
550
551 static int
552 make_dm_node(char * str)
553 {
554         int r = 0;
555         char buff[FILE_NAME_SIZE];
556         struct dm_names * names;
557         unsigned next = 0;
558         struct dm_task *dmt;
559
560         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
561                 return 0;
562
563         if (!dm_task_run(dmt))
564                 goto out;
565
566         if (!(names = dm_task_get_names(dmt)))
567                 goto out;
568
569         if (!names->dev) {
570                 r = 1;
571                 goto out;
572         }
573
574         do {
575                 if (0 == strcmp(names->name, str))
576                         break;
577                 next = names->next;
578                 names = (void *) names + next;
579         } while (next);
580
581         sprintf(buff, "/dev/mapper/%s", str);
582         unlink(buff);
583         mknod(buff, 0600 | S_IFBLK, names->dev);
584
585         out:
586         dm_task_destroy(dmt);
587         return r;
588
589 }
590
591 static int
592 add_map(struct env * conf, struct path * all_paths,
593         struct multipath * mp, int index, int op)
594 {
595         char params[255];
596         char * params_p;
597         struct dm_task *dmt;
598         int i, np;
599
600         /* defaults for multipath target */
601         int dm_pg_prio              = 1;
602         char * dm_ps_name           = "round-robin";
603         int dm_ps_nr_args           = 0;
604
605         if (!(dmt = dm_task_create(op)))
606                 return 0;
607
608         if (!dm_task_set_name(dmt, mp[index].wwid))
609                 goto addout;
610         params_p = &params[0];
611
612         np = 0;
613         for (i=0; i<=mp[index].npaths; i++) {
614                 if (0 == all_paths[PINDEX(index,i)].sg_id.scsi_type)
615                         np++;
616         }
617         if (np == 0)
618                 goto addout;
619
620         if (np < 1)
621                 goto addout;
622
623         params_p += sprintf(params_p, "%i", conf->dm_path_test_int);
624
625         if (all_paths[PINDEX(index,0)].iopolicy == MULTIBUS &&
626             !conf->forcedfailover ) {
627                 params_p += sprintf(params_p, " %i %s %i %i",
628                                     dm_pg_prio, dm_ps_name, np, dm_ps_nr_args);
629                 
630                 for (i=0; i<=mp[index].npaths; i++) {
631                         if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
632                                 continue;
633                         params_p += sprintf(params_p, " %s",
634                                             all_paths[PINDEX(index,i)].dev);
635                 }
636         }
637
638         if (all_paths[PINDEX(index,0)].iopolicy == FAILOVER ||
639             conf->forcedfailover) {
640                 for (i=0; i<=mp[index].npaths; i++) {
641                         if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
642                                 continue;
643                         params_p += sprintf(params_p, " %i %s ",
644                                             dm_pg_prio, dm_ps_name);
645                         params_p += sprintf(params_p, "1 %i",
646                                             dm_ps_nr_args);
647                         params_p += sprintf(params_p, " %s",
648                                             all_paths[PINDEX(index,i)].dev);
649                 }
650         }
651
652         if (mp[index].size < 0)
653                 goto addout;
654
655         if (!conf->quiet) {
656                 if (op == DM_DEVICE_RELOAD)
657                         printf("U:");
658                 if (op == DM_DEVICE_CREATE)
659                         printf("N:");
660                 printf("%s:0 %li %s %s\n",
661                         mp[index].wwid, mp[index].size, DM_TARGET, params);
662         }
663
664         if (!dm_task_add_target(dmt, 0, mp[index].size, DM_TARGET, params))
665                 goto addout;
666
667         if (!dm_task_run(dmt))
668                 goto addout;
669
670
671         make_dm_node(mp[index].wwid);
672
673         addout:
674         dm_task_destroy(dmt);
675         return 1;
676 }
677
678 static int
679 map_present(char * str)
680 {
681         int r = 0;
682         struct dm_task *dmt;
683         struct dm_names *names;
684         unsigned next = 0;
685
686         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
687                 return 0;
688
689         if (!dm_task_run(dmt))
690                 goto out;
691
692         if (!(names = dm_task_get_names(dmt)))
693                 goto out;
694
695         if (!names->dev) {
696                 goto out;
697         }
698
699         do {
700                 if (0 == strcmp(names->name, str))
701                         r = 1;
702                 next = names->next;
703                 names = (void *) names + next;
704         } while (next);
705
706         out:
707         dm_task_destroy(dmt);
708         return r;
709 }
710
711 static void
712 usage(char * progname)
713 {
714         fprintf(stderr, VERSION_STRING);
715         fprintf(stderr, "Usage: %s [-v|-q] [-d] [-i int] [-m max_devs]\n",
716                 progname);
717         fprintf(stderr, "\t-d\t\tdry run, do not create or update devmaps\n");
718         fprintf(stderr, "\t-f\t\tforce maps to failover mode (1 path/pg)\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("-f", argv[i]))
763                         conf.forcedfailover = 1;
764                 else if (0 == strcmp("-i", argv[i]))
765                         conf.dm_path_test_int = atoi(argv[++i]);
766                 else if (0 == strcmp("scsi", argv[i]))
767                         strcpy(conf.hotplugdev, argv[++i]);
768                 else if (*argv[i] == '-') {
769                         fprintf(stderr, "Unknown switch: %s\n", argv[i]);
770                         usage(argv[0]);
771                 } else if (*argv[i] != '-') {
772                         fprintf(stderr, "Unknown argument\n");
773                         usage(argv[0]);
774                 }
775
776         }
777
778         /* dynamic allocations */
779         mp = malloc(conf.max_devs * sizeof(struct multipath));
780         all_paths = malloc(conf.max_devs * sizeof(struct path));
781         all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
782         if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL)
783                 exit(1);
784
785         if (!conf.with_sysfs) {
786                 get_all_scsi_ids(&conf, all_scsi_ids);
787                 get_all_paths_nosysfs(&conf, all_paths, all_scsi_ids);
788         } else {
789                 get_all_paths_sysfs(&conf, all_paths);
790         }
791         nmp = coalesce_paths(&conf, mp, all_paths);
792
793         if (conf.verbose) {
794                 print_all_path(&conf, all_paths);
795                 fprintf(stdout, "\n");
796                 print_all_mp(all_paths, mp, nmp);
797                 fprintf(stdout, "\n");
798         }
799
800         if (conf.dry_run)
801                 exit(0);
802
803         for (k=0; k<=nmp; k++) {
804                 if (map_present(mp[k].wwid)) {
805                         add_map(&conf, all_paths, mp, k, DM_DEVICE_RELOAD);
806                 } else {
807                         add_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);
808                 }
809         }
810
811         /* free allocs */
812         free(mp);
813         free(all_paths);
814         free(all_scsi_ids);
815
816         exit(0);
817 }