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);
265 /* if called from hotplug but with no block, leave */
266 if (0 > readlink(buff, conf->hotplugdev, FILE_NAME_SIZE))
269 basename(conf->hotplugdev, buff);
270 sprintf(curpath.sg_dev, "/dev/%s", buff);
272 get_lun_strings(conf, &curpath);
273 if (!get_unique_id(&curpath))
275 strcpy(refwwid, curpath.wwid);
276 memset(&curpath, 0, sizeof(path));
279 sprintf(path, "%s/block", conf->sysfs_path);
280 sdir = sysfs_open_directory(path);
281 sysfs_read_directory(sdir);
283 dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
284 if (blacklist(devp->name))
287 sysfs_read_directory(devp);
289 if(devp->links == NULL)
292 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
293 if (!strncmp(linkp->name, "device", 6))
301 basename(devp->path, buff);
302 sprintf(curpath.sg_dev, "/dev/%s", buff);
304 get_lun_strings(conf, &curpath);
305 if(!get_unique_id(&curpath)) {
306 memset(&curpath, 0, sizeof(path));
310 if (memcmp(empty_buff, refwwid, WWID_SIZE) != 0 &&
311 strncmp(curpath.wwid, refwwid, WWID_SIZE) != 0) {
312 memset(&curpath, 0, sizeof(path));
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;
323 /* done with curpath, zero for reuse */
324 memset(&curpath, 0, sizeof(path));
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);
334 sysfs_close_directory(sdir);
339 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
340 struct scsi_dev * all_scsi_ids)
343 char buff[FILE_NAME_SIZE];
344 char file_name[FILE_NAME_SIZE];
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);
352 get_lun_strings(conf, &all_paths[k]);
353 if (!get_unique_id(&all_paths[k]))
356 if ((fd = open(all_paths[k].sg_dev, O_RDONLY)) < 0)
359 if (0 > ioctl(fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
360 printf("device %s failed on sg ioctl, skip\n",
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);
384 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
386 int k, big, little, res, host_no, fd;
388 char fname[FILE_NAME_SIZE];
389 struct scsi_idlun my_scsi_id;
391 for (k = 0; k < conf->max_devs; k++) {
392 strcpy(fname, "/dev/sd");
394 buff[0] = 'a' + (char) k;
397 } else if (k <= 255) {
398 /* assumes sequence goes x,y,z,aa,ab,ac etc */
400 little = k - (26 * big);
403 buff[0] = 'a' + (char) big;
404 buff[1] = 'a' + (char) little;
408 strcat(fname, "xxxx");
410 if ((fd = open(fname, O_RDONLY)) < 0) {
412 fprintf(stderr, "can't open %s. mknod ?",
417 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
420 printf("Could not get scsi idlun\n");
424 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
427 printf("Could not get host_no\n");
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;
440 /* print_path style */
445 print_path(struct path * all_paths, int k, int style)
448 printf("%s ", all_paths[k].wwid);
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);
462 print_all_path(struct env * conf, struct path * all_paths)
465 char empty_buff[WWID_SIZE];
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)
471 print_path(all_paths, k, ALL);
476 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
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);
488 get_disk_size (struct env * conf, char * dev) {
491 char attr_path[FILE_NAME_SIZE];
492 char buff[FILE_NAME_SIZE];
493 char basedev[FILE_NAME_SIZE];
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)))
505 if ((fd = open(dev, O_RDONLY)) < 0)
507 if(!ioctl(fd, BLKGETSIZE, &size))
514 coalesce_paths(struct env * conf, struct multipath * mp,
515 struct path * all_paths)
517 int k, i, nmp, np, already_done;
518 char empty_buff[WWID_SIZE];
522 memset(empty_buff, 0, WWID_SIZE);
524 for (k = 0; k < conf->max_devs - 1; k++) {
525 /* skip this path for some reason */
527 /* 1. if path has no unique id */
528 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
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))
541 /* at this point, we know we really got a new mp */
544 strcpy(mp[nmp].wwid, all_paths[k].wwid);
547 if (mp[nmp].size == 0)
548 mp[nmp].size = get_disk_size(conf, all_paths[k].dev);
550 for (i = k + 1; i < conf->max_devs; i++) {
551 if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
562 dm_simplecmd(int task, const char *name) {
566 if (!(dmt = dm_task_create(task)))
569 if (!dm_task_set_name(dmt, name))
572 r = dm_task_run(dmt);
575 dm_task_destroy(dmt);
580 dm_addmap(int task, const char *name, const char *params, long size) {
583 if (!(dmt = dm_task_create(task)))
586 if (!dm_task_set_name(dmt, name))
589 if (!dm_task_add_target(dmt, 0, size, DM_TARGET, params))
592 if (!dm_task_run(dmt))
596 dm_task_destroy(dmt);
601 setup_map(struct env * conf, struct path * all_paths,
602 struct multipath * mp, int index, int op)
608 /* 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 params_p += sprintf(params_p, "%i", conf->dm_path_test_int);
626 if (all_paths[PINDEX(index,0)].iopolicy == MULTIBUS &&
627 !conf->forcedfailover ) {
628 params_p += sprintf(params_p, " 1 %i %s %i %i",
629 dm_pg_prio, dm_ps_name, np, dm_ps_nr_args);
631 for (i=0; i<=mp[index].npaths; i++) {
632 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
634 params_p += sprintf(params_p, " %s",
635 all_paths[PINDEX(index,i)].dev);
639 if (all_paths[PINDEX(index,0)].iopolicy == FAILOVER ||
640 conf->forcedfailover) {
641 params_p += sprintf(params_p, " %i", mp[index].npaths + 1);
642 for (i=0; i<=mp[index].npaths; i++) {
643 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
645 params_p += sprintf(params_p, " %i %s ",
646 dm_pg_prio, dm_ps_name);
647 params_p += sprintf(params_p, "1 %i",
649 params_p += sprintf(params_p, " %s",
650 all_paths[PINDEX(index,i)].dev);
654 if (mp[index].size < 0)
658 if (op == DM_DEVICE_RELOAD)
660 if (op == DM_DEVICE_CREATE)
662 printf("%s:0 %li %s %s\n",
663 mp[index].wwid, mp[index].size, DM_TARGET, params);
666 if (op == DM_DEVICE_RELOAD)
667 dm_simplecmd(DM_DEVICE_SUSPEND, mp[index].wwid);
669 dm_addmap(op, mp[index].wwid, params, mp[index].size);
671 if (op == DM_DEVICE_RELOAD)
672 dm_simplecmd(DM_DEVICE_RESUME, mp[index].wwid);
678 map_present(char * str)
682 struct dm_names *names;
685 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
688 if (!dm_task_run(dmt))
691 if (!(names = dm_task_get_names(dmt)))
699 if (0 == strcmp(names->name, str))
702 names = (void *) names + next;
706 dm_task_destroy(dmt);
711 usage(char * progname)
713 fprintf(stderr, VERSION_STRING);
714 fprintf(stderr, "Usage: %s [-v|-q] [-d] [-i int] [-m max_devs]\n",
716 fprintf(stderr, "\t-d\t\tdry run, do not create or update devmaps\n");
717 fprintf(stderr, "\t-f\t\tforce maps to failover mode (1 path/pg)\n");
718 fprintf(stderr, "\t-i\t\tmultipath target param : polling interval\n");
719 fprintf(stderr, "\t-m max_devs\tscan {max_devs} devices at most\n");
720 fprintf(stderr, "\t-q\t\tquiet, no output at all\n");
721 fprintf(stderr, "\t-v\t\tverbose, print all paths and multipaths\n");
726 main(int argc, char *argv[])
728 struct multipath * mp;
729 struct path * all_paths;
730 struct scsi_dev * all_scsi_ids;
734 /* Default behaviour */
735 conf.max_devs = MAX_DEVS;
736 conf.dry_run = 0; /* 1 == Do not Create/Update devmaps */
737 conf.verbose = 0; /* 1 == Print all_paths and mp */
738 conf.quiet = 0; /* 1 == Do not even print devmaps */
739 conf.with_sysfs = 0; /* Default to compat / suboptimal behaviour */
740 conf.dm_path_test_int = 10;
742 /* kindly provided by libsysfs */
743 if (0 == sysfs_get_mnt_path(conf.sysfs_path, FILE_NAME_SIZE))
746 for (i = 1; i < argc; ++i) {
747 if (0 == strcmp("-v", argv[i])) {
751 } else if (0 == strcmp("-m", argv[i])) {
752 conf.max_devs = atoi(argv[++i]);
753 if (conf.max_devs < 2)
755 } else if (0 == strcmp("-q", argv[i])) {
756 if (conf.verbose == 1)
759 } else if (0 == strcmp("-d", argv[i]))
761 else if (0 == strcmp("-f", argv[i]))
762 conf.forcedfailover = 1;
763 else if (0 == strcmp("-i", argv[i]))
764 conf.dm_path_test_int = atoi(argv[++i]);
765 else if (0 == strcmp("scsi", argv[i]))
766 strcpy(conf.hotplugdev, argv[++i]);
767 else if (*argv[i] == '-') {
768 fprintf(stderr, "Unknown switch: %s\n", argv[i]);
770 } else if (*argv[i] != '-') {
771 fprintf(stderr, "Unknown argument\n");
777 /* dynamic allocations */
778 mp = malloc(conf.max_devs * sizeof(struct multipath));
779 all_paths = malloc(conf.max_devs * sizeof(struct path));
780 all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
781 if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL)
784 if (!conf.with_sysfs) {
785 get_all_scsi_ids(&conf, all_scsi_ids);
786 get_all_paths_nosysfs(&conf, all_paths, all_scsi_ids);
788 get_all_paths_sysfs(&conf, all_paths);
790 nmp = coalesce_paths(&conf, mp, all_paths);
793 print_all_path(&conf, all_paths);
794 fprintf(stdout, "\n");
795 print_all_mp(all_paths, mp, nmp);
796 fprintf(stdout, "\n");
802 for (k=0; k<=nmp; k++) {
803 if (map_present(mp[k].wwid)) {
804 setup_map(&conf, all_paths, mp, k, DM_DEVICE_RELOAD);
806 setup_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);