2 * Soft: multipath device mapper target autoconfig
4 * Version: $Id: main.h,v 0.0.1 2003/09/18 15:13:38 cvaroqui Exp $
6 * Author: Copyright (C) 2003 Christophe Varoqui
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.
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.
23 #include <sys/types.h>
25 #include <linux/kdev_t.h>
27 #include <sys/ioctl.h>
29 #include "libdevmapper/libdevmapper.h"
33 /* White list switch */
35 get_unique_id(struct path * mypath)
42 int (*getuid) (char *, char *);
44 {"COMPAQ ", "HSV110 (C)COMPAQ", GROUP_BY_SERIAL, &get_evpd_wwid},
45 {"COMPAQ ", "MSA1000 ", GROUP_BY_SERIAL, &get_evpd_wwid},
46 {"COMPAQ ", "MSA1000 VOLUME ", GROUP_BY_SERIAL, &get_evpd_wwid},
47 {"DEC ", "HSG80 ", GROUP_BY_SERIAL, &get_evpd_wwid},
48 {"HP ", "HSV100 ", GROUP_BY_SERIAL, &get_evpd_wwid},
49 {"HP ", "A6189A ", MULTIBUS, &get_evpd_wwid},
50 {"HP ", "OPEN- ", MULTIBUS, &get_evpd_wwid},
51 {"DDN ", "SAN DataDirector", MULTIBUS, &get_evpd_wwid},
52 {"FSC ", "CentricStor ", MULTIBUS, &get_evpd_wwid},
53 {"HITACHI ", "DF400 ", MULTIBUS, &get_evpd_wwid},
54 {"HITACHI ", "DF500 ", MULTIBUS, &get_evpd_wwid},
55 {"HITACHI ", "DF600 ", MULTIBUS, &get_evpd_wwid},
56 {"IBM ", "ProFibre 4000R ", MULTIBUS, &get_evpd_wwid},
57 {"SGI ", "TP9100 ", MULTIBUS, &get_evpd_wwid},
58 {"SGI ", "TP9300 ", MULTIBUS, &get_evpd_wwid},
59 {"SGI ", "TP9400 ", MULTIBUS, &get_evpd_wwid},
60 {"SGI ", "TP9500 ", MULTIBUS, &get_evpd_wwid},
61 {NULL, NULL, 0, NULL},
64 for (i = 0; wlist[i].vendor; i++) {
65 if (strncmp(mypath->vendor_id, wlist[i].vendor, 8) == 0 &&
66 strncmp(mypath->product_id, wlist[i].product, 16) == 0) {
67 mypath->iopolicy = wlist[i].iopolicy;
68 if (!wlist[i].getuid(mypath->sg_dev, mypath->wwid))
76 blacklist (char * dev) {
93 for (i = 0; blist[i].lengh; i++) {
94 if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
101 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
104 struct sysfs_directory * sdir;
105 struct sysfs_directory * devp;
106 struct sysfs_link * linkp;
107 char refwwid[WWID_SIZE];
108 char empty_buff[WWID_SIZE];
109 char buff[FILE_NAME_SIZE];
110 char path[FILE_NAME_SIZE];
113 /* if called from hotplug, only consider the paths that relate to */
114 /* to the device pointed by conf.hotplugdev */
115 memset(empty_buff, 0, WWID_SIZE);
116 memset(refwwid, 0, WWID_SIZE);
117 if (strncmp("/devices", conf->hotplugdev, 8) == 0) {
118 sprintf(buff, "%s%s/block",
119 conf->sysfs_path, conf->hotplugdev);
120 memset(conf->hotplugdev, 0, FILE_NAME_SIZE);
122 /* if called from hotplug but with no block, leave */
123 if (0 > readlink(buff, conf->hotplugdev, FILE_NAME_SIZE))
126 basename(conf->hotplugdev, buff);
127 sprintf(curpath.sg_dev, "/dev/%s", buff);
129 get_lun_strings(curpath.vendor_id,
133 get_serial(curpath.serial, curpath.sg_dev);
134 if (!get_unique_id(&curpath))
136 strcpy(refwwid, curpath.wwid);
137 memset(&curpath, 0, sizeof(path));
140 sprintf(path, "%s/block", conf->sysfs_path);
141 sdir = sysfs_open_directory(path);
142 sysfs_read_directory(sdir);
144 dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
145 if (blacklist(devp->name))
148 sysfs_read_directory(devp);
150 if(devp->links == NULL)
153 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
154 if (!strncmp(linkp->name, "device", 6))
162 basename(devp->path, buff);
163 sprintf(curpath.sg_dev, "/dev/%s", buff);
165 get_lun_strings(curpath.vendor_id,
169 get_serial(curpath.serial, curpath.sg_dev);
170 if(!get_unique_id(&curpath)) {
171 memset(&curpath, 0, sizeof(path));
175 if (memcmp(empty_buff, refwwid, WWID_SIZE) != 0 &&
176 strncmp(curpath.wwid, refwwid, WWID_SIZE) != 0) {
177 memset(&curpath, 0, sizeof(path));
181 strcpy(all_paths[k].sg_dev, curpath.sg_dev);
182 strcpy(all_paths[k].dev, curpath.sg_dev);
183 strcpy(all_paths[k].wwid, curpath.wwid);
184 strcpy(all_paths[k].vendor_id, curpath.vendor_id);
185 strcpy(all_paths[k].product_id, curpath.product_id);
186 all_paths[k].iopolicy = curpath.iopolicy;
188 /* done with curpath, zero for reuse */
189 memset(&curpath, 0, sizeof(path));
191 basename(linkp->target, buff);
192 sscanf(buff, "%i:%i:%i:%i",
193 &all_paths[k].sg_id.host_no,
194 &all_paths[k].sg_id.channel,
195 &all_paths[k].sg_id.scsi_id,
196 &all_paths[k].sg_id.lun);
199 sysfs_close_directory(sdir);
204 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
205 struct scsi_dev * all_scsi_ids)
208 char buff[FILE_NAME_SIZE];
209 char file_name[FILE_NAME_SIZE];
211 for (k = 0; k < conf->max_devs; k++) {
212 strcpy(file_name, "/dev/sg");
213 sprintf(buff, "%d", k);
214 strncat(file_name, buff, FILE_NAME_SIZE);
215 strcpy(all_paths[k].sg_dev, file_name);
217 get_lun_strings(all_paths[k].vendor_id,
218 all_paths[k].product_id,
220 all_paths[k].sg_dev);
221 get_serial(all_paths[k].serial, all_paths[k].sg_dev);
222 if (!get_unique_id(&all_paths[k]))
225 if ((fd = open(all_paths[k].sg_dev, O_RDONLY)) < 0)
228 if (0 > ioctl(fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
229 printf("device %s failed on sg ioctl, skip\n",
234 for (i = 0; i < conf->max_devs; i++) {
235 if ((all_paths[k].sg_id.host_no ==
236 all_scsi_ids[i].host_no)
237 && (all_paths[k].sg_id.scsi_id ==
238 (all_scsi_ids[i].scsi_id.dev_id & 0xff))
239 && (all_paths[k].sg_id.lun ==
240 ((all_scsi_ids[i].scsi_id.dev_id >> 8) & 0xff))
241 && (all_paths[k].sg_id.channel ==
242 ((all_scsi_ids[i].scsi_id.
243 dev_id >> 16) & 0xff))) {
244 strcpy(all_paths[k].dev, all_scsi_ids[i].dev);
253 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
255 int k, big, little, res, host_no, fd;
257 char fname[FILE_NAME_SIZE];
258 struct scsi_idlun my_scsi_id;
260 for (k = 0; k < conf->max_devs; k++) {
261 strcpy(fname, "/dev/sd");
263 buff[0] = 'a' + (char) k;
266 } else if (k <= 255) {
267 /* assumes sequence goes x,y,z,aa,ab,ac etc */
269 little = k - (26 * big);
272 buff[0] = 'a' + (char) big;
273 buff[1] = 'a' + (char) little;
277 strcat(fname, "xxxx");
279 if ((fd = open(fname, O_RDONLY)) < 0) {
281 fprintf(stderr, "can't open %s. mknod ?",
286 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
289 printf("Could not get scsi idlun\n");
293 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
296 printf("Could not get host_no\n");
302 strcpy(all_scsi_ids[k].dev, fname);
303 all_scsi_ids[k].scsi_id = my_scsi_id;
304 all_scsi_ids[k].host_no = host_no;
309 /* print_path style */
314 print_path(struct path * all_paths, int k, int style)
317 printf("%s ", all_paths[k].wwid);
320 printf("(%i %i %i %i) ",
321 all_paths[k].sg_id.host_no,
322 all_paths[k].sg_id.channel,
323 all_paths[k].sg_id.scsi_id, all_paths[k].sg_id.lun);
324 if(0 != strcmp(all_paths[k].sg_dev, all_paths[k].dev))
325 printf("%s ", all_paths[k].sg_dev);
326 printf("%s ", all_paths[k].dev);
327 printf("[%.16s]\n", all_paths[k].product_id);
331 print_all_path(struct env * conf, struct path * all_paths)
334 char empty_buff[WWID_SIZE];
336 memset(empty_buff, 0, WWID_SIZE);
337 for (k = 0; k < conf->max_devs; k++) {
338 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
340 print_path(all_paths, k, ALL);
345 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
349 for (k = 0; k <= nmp; k++) {
350 printf("%s\n", mp[k].wwid);
351 for (i = 0; i <= mp[k].npaths; i++)
352 print_path(all_paths, PINDEX(k,i), NOWWID);
357 coalesce_paths(struct env * conf, struct multipath * mp,
358 struct path * all_paths)
360 int k, i, nmp, np, already_done;
361 char empty_buff[WWID_SIZE];
365 memset(empty_buff, 0, WWID_SIZE);
367 for (k = 0; k < conf->max_devs - 1; k++) {
368 /* skip this path for some reason */
370 /* 1. if path has no unique id */
371 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
374 /* 2. if mp with this uid already instanciated */
375 for (i = 0; i <= nmp; i++) {
376 if (0 == strcmp(mp[i].wwid, all_paths[k].wwid))
384 /* at this point, we know we really got a new mp */
387 strcpy(mp[nmp].wwid, all_paths[k].wwid);
390 if (mp[nmp].size == 0)
391 mp[nmp].size = get_disk_size(all_paths[k].dev);
393 for (i = k + 1; i < conf->max_devs; i++) {
394 if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
405 group_by_serial(struct multipath * mp, struct path * all_paths, char * str) {
406 int path_count, pg_count = 0;
409 char path_buff[FILE_NAME_SIZE];
410 char pg_buff[FILE_NAME_SIZE];
411 char * path_buff_p = &path_buff[0];
412 char * pg_buff_p = &pg_buff[0];
414 /* init the bitmap */
415 bitmap = malloc((mp->npaths + 1) * sizeof(int));
416 memset(bitmap, 0, (mp->npaths + 1) * sizeof(int));
418 for (i = 0; i <= mp->npaths; i++) {
422 /* here, we really got a new pg */
425 memset(&path_buff, 0, FILE_NAME_SIZE * sizeof(char));
426 path_buff_p = &path_buff[0];
428 path_buff_p += sprintf(path_buff_p, " %s", all_paths[mp->pindex[i]].dev);
431 for (k = i + 1; k <= mp->npaths; k++) {
434 if (0 == strcmp(all_paths[mp->pindex[i]].serial,
435 all_paths[mp->pindex[k]].serial)) {
436 path_buff_p += sprintf(path_buff_p, " %s", all_paths[mp->pindex[k]].dev);
441 pg_buff_p += sprintf(pg_buff_p, " 1 round-robin %i 0%s",
442 path_count, path_buff);
444 sprintf(str, " %i%s", pg_count, pg_buff);
448 dm_simplecmd(int task, const char *name) {
452 if (!(dmt = dm_task_create(task)))
455 if (!dm_task_set_name(dmt, name))
458 r = dm_task_run(dmt);
461 dm_task_destroy(dmt);
466 dm_addmap(int task, const char *name, const char *params, long size) {
469 if (!(dmt = dm_task_create(task)))
472 if (!dm_task_set_name(dmt, name))
475 if (!dm_task_add_target(dmt, 0, size, DM_TARGET, params))
478 if (!dm_task_run(dmt))
482 dm_task_destroy(dmt);
487 setup_map(struct env * conf, struct path * all_paths,
488 struct multipath * mp, int index, int op)
494 /* defaults for multipath target */
496 char * dm_ps_name = "round-robin";
497 int dm_ps_nr_args = 0;
499 params_p = ¶ms[0];
502 for (i=0; i<=mp[index].npaths; i++) {
503 if (0 == all_paths[PINDEX(index,i)].sg_id.scsi_type)
510 params_p += sprintf(params_p, "%i", conf->dm_path_test_int);
512 if ((all_paths[PINDEX(index,0)].iopolicy == MULTIBUS &&
513 conf->iopolicy == -1) || conf->iopolicy == MULTIBUS) {
514 params_p += sprintf(params_p, " 1 %i %s %i %i",
515 dm_pg_prio, dm_ps_name, np, dm_ps_nr_args);
517 for (i=0; i<=mp[index].npaths; i++) {
518 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
520 params_p += sprintf(params_p, " %s",
521 all_paths[PINDEX(index,i)].dev);
525 if ((all_paths[PINDEX(index,0)].iopolicy == FAILOVER &&
526 conf->iopolicy == -1) || conf->iopolicy == FAILOVER) {
527 params_p += sprintf(params_p, " %i", mp[index].npaths + 1);
528 for (i=0; i<=mp[index].npaths; i++) {
529 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
531 params_p += sprintf(params_p, " %i %s ",
532 dm_pg_prio, dm_ps_name);
533 params_p += sprintf(params_p, "1 %i",
535 params_p += sprintf(params_p, " %s",
536 all_paths[PINDEX(index,i)].dev);
540 if ((all_paths[PINDEX(index,0)].iopolicy == GROUP_BY_SERIAL &&
541 conf->iopolicy == -1) || conf->iopolicy == GROUP_BY_SERIAL) {
542 group_by_serial(&mp[index], all_paths, params_p);
545 if (mp[index].size < 0)
549 if (op == DM_DEVICE_RELOAD)
551 if (op == DM_DEVICE_CREATE)
553 printf("%s:0 %li %s %s\n",
554 mp[index].wwid, mp[index].size, DM_TARGET, params);
557 if (op == DM_DEVICE_RELOAD)
558 dm_simplecmd(DM_DEVICE_SUSPEND, mp[index].wwid);
560 dm_addmap(op, mp[index].wwid, params, mp[index].size);
562 if (op == DM_DEVICE_RELOAD)
563 dm_simplecmd(DM_DEVICE_RESUME, mp[index].wwid);
569 map_present(char * str)
573 struct dm_names *names;
576 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
579 if (!dm_task_run(dmt))
582 if (!(names = dm_task_get_names(dmt)))
590 if (0 == strcmp(names->name, str))
593 names = (void *) names + next;
597 dm_task_destroy(dmt);
602 usage(char * progname)
604 fprintf(stderr, VERSION_STRING);
605 fprintf(stderr, "Usage: %s [-v|-q] [-d] [-i int] [-m max_devs] ",
607 fprintf(stderr, "[-p failover|multibus|group_by_serial]\n");
608 fprintf(stderr, "\t-v\t\tverbose, print all paths and multipaths\n");
609 fprintf(stderr, "\t-q\t\tquiet, no output at all\n");
610 fprintf(stderr, "\t-d\t\tdry run, do not create or update devmaps\n");
611 fprintf(stderr, "\t-i\t\tmultipath target param : polling interval\n");
612 fprintf(stderr, "\t-m max_devs\tscan {max_devs} devices at most\n");
613 fprintf(stderr, "\t-p policy\tforce maps to specified policy :\n");
614 fprintf(stderr, "\t\t\tfailover\t1 path per priority group\n");
615 fprintf(stderr, "\t\t\tmultibus\tall paths in 1 priority group\n");
616 fprintf(stderr, "\t\t\tgroup_by_serial\t1 priority group per serial\n");
621 main(int argc, char *argv[])
623 struct multipath * mp;
624 struct path * all_paths;
625 struct scsi_dev * all_scsi_ids;
629 /* Default behaviour */
630 conf.max_devs = MAX_DEVS;
631 conf.dry_run = 0; /* 1 == Do not Create/Update devmaps */
632 conf.verbose = 0; /* 1 == Print all_paths and mp */
633 conf.quiet = 0; /* 1 == Do not even print devmaps */
634 conf.iopolicy = -1; /* Apply the defaults in get_unique_id() */
635 conf.dm_path_test_int = 10;
637 for (i = 1; i < argc; ++i) {
638 if (0 == strcmp("-v", argv[i])) {
642 } else if (0 == strcmp("-m", argv[i])) {
643 conf.max_devs = atoi(argv[++i]);
644 if (conf.max_devs < 2)
646 } else if (0 == strcmp("-q", argv[i])) {
647 if (conf.verbose == 1)
650 } else if (0 == strcmp("-d", argv[i]))
652 else if (0 == strcmp("-i", argv[i]))
653 conf.dm_path_test_int = atoi(argv[++i]);
654 else if (0 == strcmp("-p", argv[i++])) {
655 if (!strcmp(argv[i], "failover"))
656 conf.iopolicy = FAILOVER;
657 if (!strcmp(argv[i], "multibus"))
658 conf.iopolicy = MULTIBUS;
659 if (!strcmp(argv[i], "group_by_serial"))
660 conf.iopolicy = GROUP_BY_SERIAL;
661 } else if (0 == strcmp("scsi", argv[i]))
662 strcpy(conf.hotplugdev, argv[++i]);
663 else if (*argv[i] == '-') {
664 fprintf(stderr, "Unknown switch: %s\n", argv[i]);
666 } else if (*argv[i] != '-') {
667 fprintf(stderr, "Unknown argument\n");
673 /* dynamic allocations */
674 mp = malloc(conf.max_devs * sizeof(struct multipath));
675 all_paths = malloc(conf.max_devs * sizeof(struct path));
676 all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
677 if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL)
680 if (sysfs_get_mnt_path(conf.sysfs_path, FILE_NAME_SIZE)) {
681 get_all_scsi_ids(&conf, all_scsi_ids);
682 get_all_paths_nosysfs(&conf, all_paths, all_scsi_ids);
684 get_all_paths_sysfs(&conf, all_paths);
686 nmp = coalesce_paths(&conf, mp, all_paths);
689 print_all_path(&conf, all_paths);
690 fprintf(stdout, "\n");
691 print_all_mp(all_paths, mp, nmp);
692 fprintf(stdout, "\n");
698 for (k=0; k<=nmp; k++) {
699 if (map_present(mp[k].wwid)) {
700 setup_map(&conf, all_paths, mp, k, DM_DEVICE_RELOAD);
702 setup_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);