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