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 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
34 void *resp, int mx_resp_len, int noisy)
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;
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;
54 io_hdr.cmdp = inqCmdBlk;
56 io_hdr.timeout = DEF_TIMEOUT;
58 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
59 perror("SG_IO (inquiry) error");
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))
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)) {
73 unsigned char * sense_buffer = io_hdr.sbp;
74 if (sense_buffer[0] & 0x2)
75 sense_key = sense_buffer[1] & 0xf;
77 sense_key = sense_buffer[2] & 0xf;
78 if(RECOVERED_ERROR == sense_key)
86 sprint_wwid(char * buff, const char * str)
95 for (i = 0; i <= WWID_SIZE / 2 - 1; i++) {
97 sprintf(cursor, "%.2x", (int) (unsigned char) c);
100 buff[WWID_SIZE - 1] = '\0';
104 basename(char * str1, char * str2)
106 char *p = str1 + (strlen(str1) - 1);
114 get_lun_strings(struct env * conf, struct path * mypath)
118 char attr_path[FILE_NAME_SIZE];
119 char basedev[FILE_NAME_SIZE];
121 if(conf->with_sysfs) {
123 basename(mypath->sg_dev, basedev);
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;
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;
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;
141 if ((fd = open(mypath->sg_dev, O_RDONLY)) < 0)
143 if (0 != do_inq(fd, 0, 0, 0, buff, 36, 1))
145 memcpy(mypath->vendor_id, &buff[8], 8);
146 memcpy(mypath->product_id, &buff[16], 16);
147 memcpy(mypath->rev, &buff[32], 4);
154 /* hardware vendor specifics : add support for new models below */
156 /* this one get EVPD page 0x83 off 8 */
157 /* tested ok with StorageWorks */
159 get_evpd_wwid(struct path * mypath)
164 if ((fd = open(mypath->sg_dev, O_RDONLY)) < 0)
167 if (0 == do_inq(fd, 0, 1, 0x83, buff, sizeof (buff), 1)) {
168 sprint_wwid(mypath->wwid, &buff[8]);
170 return 1; /* success */
173 return 0; /* not good */
176 /* White list switch */
178 get_unique_id(struct path * mypath)
185 int (*getuid) (struct path *);
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},
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))
219 blacklist (char * dev) {
236 for (i = 0; blist[i].lengh; i++) {
237 if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
244 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
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];
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);
268 get_lun_strings(conf, &curpath);
269 if (!get_unique_id(&curpath))
271 strcpy(refwwid, curpath.wwid);
272 memset(&curpath, 0, sizeof(path));
275 sprintf(path, "%s/block", conf->sysfs_path);
276 sdir = sysfs_open_directory(path);
277 sysfs_read_directory(sdir);
279 dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
280 if (blacklist(devp->name))
283 sysfs_read_directory(devp);
285 if(devp->links == NULL)
288 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
289 if (!strncmp(linkp->name, "device", 6))
297 basename(devp->path, buff);
298 sprintf(curpath.sg_dev, "/dev/%s", buff);
300 get_lun_strings(conf, &curpath);
301 if(!get_unique_id(&curpath)) {
302 memset(&curpath, 0, sizeof(path));
306 if (memcmp(empty_buff, refwwid, WWID_SIZE) != 0 &&
307 strncmp(curpath.wwid, refwwid, WWID_SIZE) != 0) {
308 memset(&curpath, 0, sizeof(path));
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;
319 /* done with curpath, zero for reuse */
320 memset(&curpath, 0, sizeof(path));
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);
330 sysfs_close_directory(sdir);
335 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
336 struct scsi_dev * all_scsi_ids)
339 char buff[FILE_NAME_SIZE];
340 char file_name[FILE_NAME_SIZE];
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);
348 get_lun_strings(conf, &all_paths[k]);
349 if (!get_unique_id(&all_paths[k]))
352 if ((fd = open(all_paths[k].sg_dev, O_RDONLY)) < 0)
355 if (0 > ioctl(fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
356 printf("device %s failed on sg ioctl, skip\n",
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);
380 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
382 int k, big, little, res, host_no, fd;
384 char fname[FILE_NAME_SIZE];
385 struct scsi_idlun my_scsi_id;
387 for (k = 0; k < conf->max_devs; k++) {
388 strcpy(fname, "/dev/sd");
390 buff[0] = 'a' + (char) k;
393 } else if (k <= 255) {
394 /* assumes sequence goes x,y,z,aa,ab,ac etc */
396 little = k - (26 * big);
399 buff[0] = 'a' + (char) big;
400 buff[1] = 'a' + (char) little;
404 strcat(fname, "xxxx");
406 if ((fd = open(fname, O_RDONLY)) < 0) {
408 fprintf(stderr, "can't open %s. mknod ?",
413 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
416 printf("Could not get scsi idlun\n");
420 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
423 printf("Could not get host_no\n");
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;
436 /* print_path style */
441 print_path(struct path * all_paths, int k, int style)
444 printf("%s ", all_paths[k].wwid);
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);
458 print_all_path(struct env * conf, struct path * all_paths)
461 char empty_buff[WWID_SIZE];
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)
467 print_path(all_paths, k, ALL);
472 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
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);
484 get_disk_size (struct env * conf, char * dev) {
487 char attr_path[FILE_NAME_SIZE];
488 char buff[FILE_NAME_SIZE];
489 char basedev[FILE_NAME_SIZE];
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)))
501 if ((fd = open(dev, O_RDONLY)) < 0)
503 if(!ioctl(fd, BLKGETSIZE, &size))
510 coalesce_paths(struct env * conf, struct multipath * mp,
511 struct path * all_paths)
513 int k, i, nmp, np, already_done;
514 char empty_buff[WWID_SIZE];
518 memset(empty_buff, 0, WWID_SIZE);
520 for (k = 0; k < conf->max_devs - 1; k++) {
521 /* skip this path for some reason */
523 /* 1. if path has no unique id */
524 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
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))
537 /* at this point, we know we really got a new mp */
540 strcpy(mp[nmp].wwid, all_paths[k].wwid);
543 if (mp[nmp].size == 0)
544 mp[nmp].size = get_disk_size(conf, all_paths[k].dev);
546 for (i = k + 1; i < conf->max_devs; i++) {
547 if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
558 make_dm_node(char * str)
561 char buff[FILE_NAME_SIZE];
562 struct dm_names * names;
566 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
569 if (!dm_task_run(dmt))
572 if (!(names = dm_task_get_names(dmt)))
581 if (0 == strcmp(names->name, str))
584 names = (void *) names + next;
587 sprintf(buff, "/dev/mapper/%s", str);
589 mknod(buff, 0600 | S_IFBLK, names->dev);
592 dm_task_destroy(dmt);
598 dm_simplecmd(int task, const char *name) {
602 if (!(dmt = dm_task_create(task)))
605 if (!dm_task_set_name(dmt, name))
608 r = dm_task_run(dmt);
611 dm_task_destroy(dmt);
616 dm_addmap(int task, const char *name, const char *params, long size) {
619 if (!(dmt = dm_task_create(task)))
622 if (!dm_task_set_name(dmt, name))
625 if (!dm_task_add_target(dmt, 0, size, DM_TARGET, params))
628 if (!dm_task_run(dmt))
632 dm_task_destroy(dmt);
637 setup_map(struct env * conf, struct path * all_paths,
638 struct multipath * mp, int index, int op)
644 /* defaults for multipath target */
646 char * dm_ps_name = "round-robin";
647 int dm_ps_nr_args = 0;
649 params_p = ¶ms[0];
652 for (i=0; i<=mp[index].npaths; i++) {
653 if (0 == all_paths[PINDEX(index,i)].sg_id.scsi_type)
660 params_p += sprintf(params_p, "%i", conf->dm_path_test_int);
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);
667 for (i=0; i<=mp[index].npaths; i++) {
668 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
670 params_p += sprintf(params_p, " %s",
671 all_paths[PINDEX(index,i)].dev);
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)
680 params_p += sprintf(params_p, " %i %s ",
681 dm_pg_prio, dm_ps_name);
682 params_p += sprintf(params_p, "1 %i",
684 params_p += sprintf(params_p, " %s",
685 all_paths[PINDEX(index,i)].dev);
689 if (mp[index].size < 0)
693 if (op == DM_DEVICE_RELOAD)
695 if (op == DM_DEVICE_CREATE)
697 printf("%s:0 %li %s %s\n",
698 mp[index].wwid, mp[index].size, DM_TARGET, params);
701 if (op == DM_DEVICE_RELOAD)
702 dm_simplecmd(DM_DEVICE_SUSPEND, mp[index].wwid);
704 dm_addmap(op, mp[index].wwid, params, mp[index].size);
706 if (op == DM_DEVICE_RELOAD)
707 dm_simplecmd(DM_DEVICE_RESUME, mp[index].wwid);
709 make_dm_node(mp[index].wwid);
714 map_present(char * str)
718 struct dm_names *names;
721 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
724 if (!dm_task_run(dmt))
727 if (!(names = dm_task_get_names(dmt)))
735 if (0 == strcmp(names->name, str))
738 names = (void *) names + next;
742 dm_task_destroy(dmt);
747 usage(char * progname)
749 fprintf(stderr, VERSION_STRING);
750 fprintf(stderr, "Usage: %s [-v|-q] [-d] [-i int] [-m max_devs]\n",
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");
762 main(int argc, char *argv[])
764 struct multipath * mp;
765 struct path * all_paths;
766 struct scsi_dev * all_scsi_ids;
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;
778 /* kindly provided by libsysfs */
779 if (0 == sysfs_get_mnt_path(conf.sysfs_path, FILE_NAME_SIZE))
782 for (i = 1; i < argc; ++i) {
783 if (0 == strcmp("-v", argv[i])) {
787 } else if (0 == strcmp("-m", argv[i])) {
788 conf.max_devs = atoi(argv[++i]);
789 if (conf.max_devs < 2)
791 } else if (0 == strcmp("-q", argv[i])) {
792 if (conf.verbose == 1)
795 } else if (0 == strcmp("-d", argv[i]))
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]);
806 } else if (*argv[i] != '-') {
807 fprintf(stderr, "Unknown argument\n");
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)
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);
824 get_all_paths_sysfs(&conf, all_paths);
826 nmp = coalesce_paths(&conf, mp, all_paths);
829 print_all_path(&conf, all_paths);
830 fprintf(stdout, "\n");
831 print_all_mp(all_paths, mp, nmp);
832 fprintf(stdout, "\n");
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);
842 setup_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);