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