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>
28 #include <sys/ioctl.h>
30 #include <sysfs/libsysfs.h>
31 #include "../libdevmapper/libdevmapper.h"
35 /* White list switch */
37 get_unique_id(struct path * mypath)
44 int (*getuid) (char *, char *);
46 {"COMPAQ ", "HSV110 (C)COMPAQ", GROUP_BY_TUR, &get_evpd_wwid},
47 {"COMPAQ ", "MSA1000 ", GROUP_BY_TUR, &get_evpd_wwid},
48 {"COMPAQ ", "MSA1000 VOLUME ", GROUP_BY_TUR, &get_evpd_wwid},
49 {"DEC ", "HSG80 ", GROUP_BY_TUR, &get_evpd_wwid},
50 {"HP ", "HSV100 ", GROUP_BY_TUR, &get_evpd_wwid},
51 {"HP ", "A6189A ", MULTIBUS, &get_evpd_wwid},
52 {"HP ", "OPEN- ", MULTIBUS, &get_evpd_wwid},
53 {"DDN ", "SAN DataDirector", MULTIBUS, &get_evpd_wwid},
54 {"FSC ", "CentricStor ", MULTIBUS, &get_evpd_wwid},
55 {"HITACHI ", "DF400 ", MULTIBUS, &get_evpd_wwid},
56 {"HITACHI ", "DF500 ", MULTIBUS, &get_evpd_wwid},
57 {"HITACHI ", "DF600 ", MULTIBUS, &get_evpd_wwid},
58 {"IBM ", "ProFibre 4000R ", MULTIBUS, &get_evpd_wwid},
59 {"SGI ", "TP9100 ", MULTIBUS, &get_evpd_wwid},
60 {"SGI ", "TP9300 ", MULTIBUS, &get_evpd_wwid},
61 {"SGI ", "TP9400 ", MULTIBUS, &get_evpd_wwid},
62 {"SGI ", "TP9500 ", MULTIBUS, &get_evpd_wwid},
63 {NULL, NULL, 0, NULL},
66 for (i = 0; wlist[i].vendor; i++) {
67 if (strncmp(mypath->vendor_id, wlist[i].vendor, 8) == 0 &&
68 strncmp(mypath->product_id, wlist[i].product, 16) == 0) {
69 mypath->iopolicy = wlist[i].iopolicy;
70 if (!wlist[i].getuid(mypath->sg_dev, mypath->wwid))
78 sysfsdevice2devname (char *devname, char *device)
80 char sysfs_path[FILE_NAME_SIZE];
81 char block_path[FILE_NAME_SIZE];
82 char link_path[FILE_NAME_SIZE];
85 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
86 fprintf(stderr, "[device] feature available with sysfs only\n");
90 sprintf(link_path, "%s%s/block", sysfs_path, device);
92 r = sysfs_get_link(link_path, block_path, FILE_NAME_SIZE);
97 sysfs_get_name_from_path(block_path, devname, FILE_NAME_SIZE);
104 devt2devname (char *devname, int major, int minor)
106 struct sysfs_directory * sdir;
107 struct sysfs_directory * devp;
108 char sysfs_path[FILE_NAME_SIZE];
109 char block_path[FILE_NAME_SIZE];
110 char attr_path[FILE_NAME_SIZE];
112 char attr_ref_value[16];
114 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
115 fprintf(stderr, "-D feature available with sysfs only\n");
119 sprintf(attr_ref_value, "%i:%i\n", major, minor);
120 sprintf(block_path, "%s/block", sysfs_path);
121 sdir = sysfs_open_directory(block_path);
122 sysfs_read_directory(sdir);
124 dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
125 sprintf(attr_path, "%s/%s/dev", block_path, devp->name);
126 sysfs_read_attribute_value(attr_path, attr_value, 16);
128 if (!strcmp(attr_value, attr_ref_value)) {
129 sprintf(attr_path, "%s/%s", block_path, devp->name);
130 sysfs_get_name_from_path(attr_path, devname, FILE_NAME_SIZE);
135 sysfs_close_directory(sdir);
141 blacklist (char * dev) {
158 for (i = 0; blist[i].lengh; i++) {
159 if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
166 devinfo (struct path *curpath)
168 get_lun_strings(curpath->vendor_id,
172 get_serial(curpath->serial, curpath->sg_dev);
173 curpath->tur = do_tur(curpath->sg_dev);
174 if (!get_unique_id(curpath))
182 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
185 struct sysfs_directory * sdir;
186 struct sysfs_directory * devp;
187 struct sysfs_link * linkp;
188 char refwwid[WWID_SIZE];
189 char empty_buff[WWID_SIZE];
190 char buff[FILE_NAME_SIZE];
191 char path[FILE_NAME_SIZE];
194 memset(empty_buff, 0, WWID_SIZE);
195 memset(refwwid, 0, WWID_SIZE);
197 /* if called from hotplug, only consider the paths that relate
198 to the device pointed by conf.hotplugdev */
200 if (strncmp("/devices", conf->hotplugdev, 8) == 0) {
201 if (sysfsdevice2devname (buff, conf->hotplugdev))
204 sprintf(curpath.sg_dev, "/dev/%s", buff);
206 if (devinfo(&curpath))
209 strcpy(refwwid, curpath.wwid);
210 memset(&curpath, 0, sizeof(path));
213 /* if major/minor specified on the cmd line,
214 only consider affiliated paths */
216 if (conf->major >= 0 && conf->minor >= 0) {
217 if (devt2devname(buff, conf->major, conf->minor))
220 sprintf(curpath.sg_dev, "/dev/%s", buff);
222 if (devinfo(&curpath))
225 strcpy(refwwid, curpath.wwid);
226 memset(&curpath, 0, sizeof(path));
230 sprintf(path, "%s/block", conf->sysfs_path);
231 sdir = sysfs_open_directory(path);
232 sysfs_read_directory(sdir);
234 dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
235 if (blacklist(devp->name))
238 sysfs_read_directory(devp);
240 if(devp->links == NULL)
243 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
244 if (!strncmp(linkp->name, "device", 6))
252 basename(devp->path, buff);
253 sprintf(curpath.sg_dev, "/dev/%s", buff);
255 if(devinfo(&curpath)) {
256 memset(&curpath, 0, sizeof(path));
260 if (memcmp(empty_buff, refwwid, WWID_SIZE) != 0 &&
261 strncmp(curpath.wwid, refwwid, WWID_SIZE) != 0) {
262 memset(&curpath, 0, sizeof(path));
266 strcpy(all_paths[k].sg_dev, curpath.sg_dev);
267 strcpy(all_paths[k].dev, curpath.sg_dev);
268 strcpy(all_paths[k].wwid, curpath.wwid);
269 strcpy(all_paths[k].vendor_id, curpath.vendor_id);
270 strcpy(all_paths[k].product_id, curpath.product_id);
271 all_paths[k].iopolicy = curpath.iopolicy;
272 all_paths[k].tur = curpath.tur;
274 /* done with curpath, zero for reuse */
275 memset(&curpath, 0, sizeof(path));
277 basename(linkp->target, buff);
278 sscanf(buff, "%i:%i:%i:%i",
279 &all_paths[k].sg_id.host_no,
280 &all_paths[k].sg_id.channel,
281 &all_paths[k].sg_id.scsi_id,
282 &all_paths[k].sg_id.lun);
285 sysfs_close_directory(sdir);
290 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
291 struct scsi_dev * all_scsi_ids)
294 char buff[FILE_NAME_SIZE];
295 char file_name[FILE_NAME_SIZE];
297 for (k = 0; k < conf->max_devs; k++) {
298 strcpy(file_name, "/dev/sg");
299 sprintf(buff, "%d", k);
300 strncat(file_name, buff, FILE_NAME_SIZE);
301 strcpy(all_paths[k].sg_dev, file_name);
303 get_lun_strings(all_paths[k].vendor_id,
304 all_paths[k].product_id,
306 all_paths[k].sg_dev);
307 get_serial(all_paths[k].serial, all_paths[k].sg_dev);
308 if (!get_unique_id(&all_paths[k]))
311 if ((fd = open(all_paths[k].sg_dev, O_RDONLY)) < 0)
314 if (0 > ioctl(fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
315 printf("device %s failed on sg ioctl, skip\n",
320 for (i = 0; i < conf->max_devs; i++) {
321 if ((all_paths[k].sg_id.host_no ==
322 all_scsi_ids[i].host_no)
323 && (all_paths[k].sg_id.scsi_id ==
324 (all_scsi_ids[i].scsi_id.dev_id & 0xff))
325 && (all_paths[k].sg_id.lun ==
326 ((all_scsi_ids[i].scsi_id.dev_id >> 8) & 0xff))
327 && (all_paths[k].sg_id.channel ==
328 ((all_scsi_ids[i].scsi_id.
329 dev_id >> 16) & 0xff))) {
330 strcpy(all_paths[k].dev, all_scsi_ids[i].dev);
339 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
341 int k, big, little, res, host_no, fd;
343 char fname[FILE_NAME_SIZE];
344 struct scsi_idlun my_scsi_id;
346 for (k = 0; k < conf->max_devs; k++) {
347 strcpy(fname, "/dev/sd");
349 buff[0] = 'a' + (char) k;
352 } else if (k <= 255) {
353 /* assumes sequence goes x,y,z,aa,ab,ac etc */
355 little = k - (26 * big);
358 buff[0] = 'a' + (char) big;
359 buff[1] = 'a' + (char) little;
363 strcat(fname, "xxxx");
365 if ((fd = open(fname, O_RDONLY)) < 0) {
367 fprintf(stderr, "can't open %s. mknod ?",
372 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
375 printf("Could not get scsi idlun\n");
379 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
382 printf("Could not get host_no\n");
388 strcpy(all_scsi_ids[k].dev, fname);
389 all_scsi_ids[k].scsi_id = my_scsi_id;
390 all_scsi_ids[k].host_no = host_no;
395 /* print_path style */
400 print_path(struct path * all_paths, int k, int style)
403 printf("%s ", all_paths[k].wwid);
406 printf("(%i %i %i %i) ",
407 all_paths[k].sg_id.host_no,
408 all_paths[k].sg_id.channel,
409 all_paths[k].sg_id.scsi_id, all_paths[k].sg_id.lun);
410 if(0 != strcmp(all_paths[k].sg_dev, all_paths[k].dev))
411 printf("%s ", all_paths[k].sg_dev);
412 printf("%s ", all_paths[k].dev);
413 printf("[%.16s]\n", all_paths[k].product_id);
417 print_all_path(struct env * conf, struct path * all_paths)
420 char empty_buff[WWID_SIZE];
422 memset(empty_buff, 0, WWID_SIZE);
423 for (k = 0; k < conf->max_devs; k++) {
424 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
426 print_path(all_paths, k, ALL);
431 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
435 for (k = 0; k <= nmp; k++) {
436 printf("%s\n", mp[k].wwid);
437 for (i = 0; i <= mp[k].npaths; i++)
438 print_path(all_paths, PINDEX(k,i), NOWWID);
443 coalesce_paths(struct env * conf, struct multipath * mp,
444 struct path * all_paths)
446 int k, i, nmp, np, already_done;
447 char empty_buff[WWID_SIZE];
451 memset(empty_buff, 0, WWID_SIZE);
453 for (k = 0; k < conf->max_devs - 1; k++) {
454 /* skip this path for some reason */
456 /* 1. if path has no unique id */
457 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
460 /* 2. if mp with this uid already instanciated */
461 for (i = 0; i <= nmp; i++) {
462 if (0 == strcmp(mp[i].wwid, all_paths[k].wwid))
470 /* at this point, we know we really got a new mp */
473 strcpy(mp[nmp].wwid, all_paths[k].wwid);
476 if (mp[nmp].size == 0)
477 mp[nmp].size = get_disk_size(all_paths[k].dev);
479 for (i = k + 1; i < conf->max_devs; i++) {
480 if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
491 group_by_tur(struct multipath * mp, struct path * all_paths, char * str) {
492 int left_path_count = 0;
493 int right_path_count = 0;
495 char left_path_buff[FILE_NAME_SIZE], right_path_buff[FILE_NAME_SIZE];
496 char * left_path_buff_p = &left_path_buff[0];
497 char * right_path_buff_p = &right_path_buff[0];
499 for (i = 0; i <= mp->npaths; i++) {
500 if (all_paths[mp->pindex[i]].tur) {
501 left_path_buff_p += sprintf(left_path_buff_p, " %s", all_paths[mp->pindex[i]].dev);
504 right_path_buff_p += sprintf(right_path_buff_p, " %s", all_paths[mp->pindex[i]].dev);
508 if (!left_path_count)
509 sprintf(str, " 1 round-robin %i 0 %s", right_path_count, right_path_buff);
510 else if (!right_path_count)
511 sprintf(str, " 1 round-robin %i 0 %s", left_path_count, left_path_buff);
513 sprintf(str, " 2 round-robin %i 0 %s round-robin %i 0 %s",
514 left_path_count, left_path_buff,
515 right_path_count, right_path_buff);
519 group_by_serial(struct multipath * mp, struct path * all_paths, char * str) {
520 int path_count, pg_count = 0;
523 char path_buff[FILE_NAME_SIZE];
524 char pg_buff[FILE_NAME_SIZE];
525 char * path_buff_p = &path_buff[0];
526 char * pg_buff_p = &pg_buff[0];
528 /* init the bitmap */
529 bitmap = malloc((mp->npaths + 1) * sizeof(int));
530 memset(bitmap, 0, (mp->npaths + 1) * sizeof(int));
532 for (i = 0; i <= mp->npaths; i++) {
536 /* here, we really got a new pg */
539 memset(&path_buff, 0, FILE_NAME_SIZE * sizeof(char));
540 path_buff_p = &path_buff[0];
542 path_buff_p += sprintf(path_buff_p, " %s", all_paths[mp->pindex[i]].dev);
545 for (k = i + 1; k <= mp->npaths; k++) {
548 if (0 == strcmp(all_paths[mp->pindex[i]].serial,
549 all_paths[mp->pindex[k]].serial)) {
550 path_buff_p += sprintf(path_buff_p, " %s", all_paths[mp->pindex[k]].dev);
555 pg_buff_p += sprintf(pg_buff_p, " round-robin %i 0%s",
556 path_count, path_buff);
558 sprintf(str, " %i%s", pg_count, pg_buff);
563 dm_simplecmd(int task, const char *name) {
567 if (!(dmt = dm_task_create(task)))
570 if (!dm_task_set_name(dmt, name))
573 r = dm_task_run(dmt);
576 dm_task_destroy(dmt);
581 dm_addmap(int task, const char *name, const char *params, long size) {
584 if (!(dmt = dm_task_create(task)))
587 if (!dm_task_set_name(dmt, name))
590 if (!dm_task_add_target(dmt, 0, size, DM_TARGET, params))
593 if (!dm_task_run(dmt))
597 dm_task_destroy(dmt);
602 setup_map(struct env * conf, struct path * all_paths,
603 struct multipath * mp, int index, int op)
609 /* defaults for multipath target */
610 char * dm_ps_name = "round-robin";
611 int dm_ps_nr_args = 0;
613 params_p = ¶ms[0];
616 for (i=0; i<=mp[index].npaths; i++) {
617 if (0 == all_paths[PINDEX(index,i)].sg_id.scsi_type)
624 if ((all_paths[PINDEX(index,0)].iopolicy == MULTIBUS &&
625 conf->iopolicy == -1) || conf->iopolicy == MULTIBUS) {
626 params_p += sprintf(params_p, " 1 %s %i %i",
627 dm_ps_name, np, dm_ps_nr_args);
629 for (i=0; i<=mp[index].npaths; i++) {
630 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
632 params_p += sprintf(params_p, " %s",
633 all_paths[PINDEX(index,i)].dev);
637 if ((all_paths[PINDEX(index,0)].iopolicy == FAILOVER &&
638 conf->iopolicy == -1) || conf->iopolicy == FAILOVER) {
639 params_p += sprintf(params_p, " %i", mp[index].npaths + 1);
640 for (i=0; i<=mp[index].npaths; i++) {
641 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
643 params_p += sprintf(params_p, " %s ",
645 params_p += sprintf(params_p, "1 %i",
647 params_p += sprintf(params_p, " %s",
648 all_paths[PINDEX(index,i)].dev);
652 if ((all_paths[PINDEX(index,0)].iopolicy == GROUP_BY_SERIAL &&
653 conf->iopolicy == -1) || conf->iopolicy == GROUP_BY_SERIAL) {
654 group_by_serial(&mp[index], all_paths, params_p);
657 if ((all_paths[PINDEX(index,0)].iopolicy == GROUP_BY_TUR &&
658 conf->iopolicy == -1) || conf->iopolicy == GROUP_BY_TUR) {
659 group_by_tur(&mp[index], all_paths, params_p);
662 if (mp[index].size < 0)
666 if (op == DM_DEVICE_RELOAD)
668 if (op == DM_DEVICE_CREATE)
670 printf("%s:0 %li %s %s\n",
671 mp[index].wwid, mp[index].size, DM_TARGET, params);
674 if (op == DM_DEVICE_RELOAD)
675 dm_simplecmd(DM_DEVICE_SUSPEND, mp[index].wwid);
677 dm_addmap(op, mp[index].wwid, params, mp[index].size);
679 if (op == DM_DEVICE_RELOAD)
680 dm_simplecmd(DM_DEVICE_RESUME, mp[index].wwid);
686 map_present(char * str)
690 struct dm_names *names;
693 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
696 if (!dm_task_run(dmt))
699 if (!(names = dm_task_get_names(dmt)))
707 if (0 == strcmp(names->name, str))
710 names = (void *) names + next;
714 dm_task_destroy(dmt);
727 file = fopen (PIDFILE, "r");
730 fprintf(stderr, "cannot signal daemon, pidfile not found\n");
734 buf = fgets (buf, 8, file);
737 pid = (pid_t) atol (buf);
744 filepresent(char * run) {
753 usage(char * progname)
755 fprintf(stderr, VERSION_STRING);
756 fprintf(stderr, "Usage: %s [-v|-q] [-d] [-m max_devs]\n",
758 fprintf(stderr, " [-p failover|multibus|group_by_serial]\n" \
761 "\t-v\t\tverbose, print all paths and multipaths\n" \
762 "\t-q\t\tquiet, no output at all\n" \
763 "\t-d\t\tdry run, do not create or update devmaps\n" \
764 "\t-m max_devs\tscan {max_devs} devices at most\n" \
766 "\t-p policy\tforce maps to specified policy :\n" \
767 "\t failover\t\t- 1 path per priority group\n" \
768 "\t multibus\t\t- all paths in 1 priority group\n" \
769 "\t group_by_serial\t- 1 priority group per serial\n" \
770 "\t group_by_tur\t\t- 1 priority group per TUR state\n" \
772 "\t-D maj min\tlimit scope to the device's multipath\n" \
773 "\t\t\t(major minor device reference)\n"
774 "\tdevice\t\tlimit scope to the device's multipath\n" \
775 "\t\t\t(hotplug-style $DEVPATH reference)\n"
781 main(int argc, char *argv[])
783 struct multipath * mp;
784 struct path * all_paths;
785 struct scsi_dev * all_scsi_ids;
790 /* Don't run in parallel */
791 while (filepresent(RUN) && try++ < MAXTRY)
794 if (filepresent(RUN)) {
795 fprintf(stderr, "waited for to long. exiting\n");
800 if (!open(RUN, O_CREAT)) {
801 fprintf(stderr, "can't create runfile\n");
805 /* Default behaviour */
806 conf.max_devs = MAX_DEVS;
807 conf.dry_run = 0; /* 1 == Do not Create/Update devmaps */
808 conf.verbose = 0; /* 1 == Print all_paths and mp */
809 conf.quiet = 0; /* 1 == Do not even print devmaps */
810 conf.iopolicy = -1; /* Apply the defaults in get_unique_id() */
814 for (i = 1; i < argc; ++i) {
815 if (0 == strcmp("-v", argv[i])) {
819 } else if (0 == strcmp("-m", argv[i])) {
820 conf.max_devs = atoi(argv[++i]);
821 if (conf.max_devs < 2)
823 } else if (0 == strcmp("-D", argv[i])) {
824 conf.major = atoi(argv[++i]);
825 conf.minor = atoi(argv[++i]);
826 } else if (0 == strcmp("-q", argv[i])) {
827 if (conf.verbose == 1)
830 } else if (0 == strcmp("-d", argv[i]))
832 else if (0 == strcmp("-p", argv[i])) {
834 if (!strcmp(argv[i], "failover"))
835 conf.iopolicy = FAILOVER;
836 if (!strcmp(argv[i], "multibus"))
837 conf.iopolicy = MULTIBUS;
838 if (!strcmp(argv[i], "group_by_serial"))
839 conf.iopolicy = GROUP_BY_SERIAL;
840 if (!strcmp(argv[i], "group_by_tur"))
841 conf.iopolicy = GROUP_BY_TUR;
842 } else if (*argv[i] == '-') {
843 fprintf(stderr, "Unknown switch: %s\n", argv[i]);
846 strncpy(conf.hotplugdev, argv[i], FILE_NAME_SIZE);
849 /* dynamic allocations */
850 mp = malloc(conf.max_devs * sizeof(struct multipath));
851 all_paths = malloc(conf.max_devs * sizeof(struct path));
852 all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
853 if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL)
856 if (sysfs_get_mnt_path(conf.sysfs_path, FILE_NAME_SIZE)) {
857 get_all_scsi_ids(&conf, all_scsi_ids);
858 get_all_paths_nosysfs(&conf, all_paths, all_scsi_ids);
860 get_all_paths_sysfs(&conf, all_paths);
863 nmp = coalesce_paths(&conf, mp, all_paths);
866 print_all_path(&conf, all_paths);
867 fprintf(stdout, "\n");
868 print_all_mp(all_paths, mp, nmp);
869 fprintf(stdout, "\n");
875 for (k=0; k<=nmp; k++) {
876 if (map_present(mp[k].wwid)) {
877 setup_map(&conf, all_paths, mp, k, DM_DEVICE_RELOAD);
879 setup_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);
883 /* signal multipathd that new devmaps may have come up */
891 /* release runfile */