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 get_lun_strings(int fd, struct path * mypath)
108 if (0 == do_inq(fd, 0, 0, 0, buff, 36, 1)) {
109 memcpy(mypath->vendor_id, &buff[8], 8);
110 memcpy(mypath->product_id, &buff[16], 16);
111 memcpy(mypath->rev, &buff[32], 4);
119 get_serial (int fd, char * str)
121 char buff[MX_ALLOC_LEN + 1];
124 if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN, 0)) {
127 memcpy(str, buff + 4, len);
136 /* hardware vendor specifics : add support for new models below */
138 /* this one get EVPD page 0x83 off 8 */
139 /* tested ok with StorageWorks */
141 get_evpd_wwid(int fd, char *str)
145 if (0 == do_inq(fd, 0, 1, 0x83, buff, sizeof (buff), 1)) {
146 sprint_wwid(str, &buff[8]);
152 /* White list switch */
154 get_unique_id(int fd, struct path * mypath)
161 int (*getuid) (int fd, char * wwid);
163 {"COMPAQ ", "HSV110 (C)COMPAQ", MULTIBUS, &get_evpd_wwid},
164 {"COMPAQ ", "MSA1000 ", MULTIBUS, &get_evpd_wwid},
165 {"COMPAQ ", "MSA1000 VOLUME ", MULTIBUS, &get_evpd_wwid},
166 {"DEC ", "HSG80 ", MULTIBUS, &get_evpd_wwid},
167 {"HP ", "HSV100 ", MULTIBUS, &get_evpd_wwid},
168 {"HP ", "A6189A ", MULTIBUS, &get_evpd_wwid},
169 {"HP ", "OPEN- ", MULTIBUS, &get_evpd_wwid},
170 {"DDN ", "SAN DataDirector", MULTIBUS, &get_evpd_wwid},
171 {"FSC ", "CentricStor ", MULTIBUS, &get_evpd_wwid},
172 {"HITACHI ", "DF400 ", MULTIBUS, &get_evpd_wwid},
173 {"HITACHI ", "DF500 ", MULTIBUS, &get_evpd_wwid},
174 {"HITACHI ", "DF600 ", MULTIBUS, &get_evpd_wwid},
175 {"IBM ", "ProFibre 4000R ", MULTIBUS, &get_evpd_wwid},
176 {"SGI ", "TP9100 ", MULTIBUS, &get_evpd_wwid},
177 {"SGI ", "TP9300 ", MULTIBUS, &get_evpd_wwid},
178 {"SGI ", "TP9400 ", MULTIBUS, &get_evpd_wwid},
179 {"SGI ", "TP9500 ", MULTIBUS, &get_evpd_wwid},
180 {NULL, NULL, 0, NULL},
183 for (i = 0; wlist[i].vendor; i++) {
184 if (strncmp(mypath->vendor_id, wlist[i].vendor, 8) == 0 &&
185 strncmp(mypath->product_id, wlist[i].product, 16) == 0) {
186 wlist[i].getuid(fd, mypath->wwid);
187 mypath->iopolicy = wlist[i].iopolicy;
195 basename(char * str1, char * str2)
197 char *p = str1 + (strlen(str1) - 1);
205 blacklist (char * dev) {
222 for (i = 0; blist[i].lengh; i++) {
223 if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
230 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
234 struct sysfs_directory * sdir;
235 struct sysfs_directory * devp;
236 struct sysfs_link * linkp;
237 char refwwid[WWID_SIZE];
238 char empty_buff[WWID_SIZE];
239 char buff[FILE_NAME_SIZE];
240 char path[FILE_NAME_SIZE];
243 /* if called from hotplug, only consider the paths that relate to */
244 /* to the device pointed by conf.hotplugdev */
245 memset(empty_buff, 0, WWID_SIZE);
246 memset(refwwid, 0, WWID_SIZE);
247 if (strncmp("/devices", conf->hotplugdev, 8) == 0) {
248 sprintf(buff, "%s%s/block",
249 conf->sysfs_path, conf->hotplugdev);
250 memset(conf->hotplugdev, 0, FILE_NAME_SIZE);
251 readlink(buff, conf->hotplugdev, FILE_NAME_SIZE);
252 basename(conf->hotplugdev, buff);
253 sprintf(curpath.sg_dev, "/dev/%s", buff);
255 if ((sg_fd = open(curpath.sg_dev, O_RDONLY)) < 0)
258 get_lun_strings(sg_fd, &curpath);
259 get_unique_id(sg_fd, &curpath);
260 strcpy(refwwid, curpath.wwid);
261 memset(&curpath, 0, sizeof(path));
264 sprintf(path, "%s/block", conf->sysfs_path);
265 sdir = sysfs_open_directory(path);
266 sysfs_read_directory(sdir);
268 dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
269 if (blacklist(devp->name))
272 sysfs_read_directory(devp);
274 if(devp->links == NULL)
277 dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
278 if (!strncmp(linkp->name, "device", 6))
286 basename(devp->path, buff);
287 sprintf(curpath.sg_dev, "/dev/%s", buff);
289 if ((sg_fd = open(curpath.sg_dev, O_RDONLY)) < 0) {
291 fprintf(stderr, "can't open %s\n",
296 get_lun_strings(sg_fd, &curpath);
297 get_unique_id(sg_fd, &curpath);
299 if (memcmp(empty_buff, refwwid, WWID_SIZE) != 0 &&
300 strncmp(curpath.wwid, refwwid, WWID_SIZE) != 0) {
301 memset(&curpath, 0, sizeof(path));
305 strcpy(all_paths[k].sg_dev, curpath.sg_dev);
306 strcpy(all_paths[k].dev, curpath.sg_dev);
307 strcpy(all_paths[k].wwid, curpath.wwid);
308 strcpy(all_paths[k].vendor_id, curpath.vendor_id);
309 strcpy(all_paths[k].product_id, curpath.product_id);
310 all_paths[k].iopolicy = curpath.iopolicy;
312 /* done with curpath, zero for reuse */
313 memset(&curpath, 0, sizeof(path));
316 basename(linkp->target, buff);
317 sscanf(buff, "%i:%i:%i:%i",
318 &all_paths[k].sg_id.host_no,
319 &all_paths[k].sg_id.channel,
320 &all_paths[k].sg_id.scsi_id,
321 &all_paths[k].sg_id.lun);
324 sysfs_close_directory(sdir);
329 get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
330 struct scsi_dev * all_scsi_ids)
333 char buff[FILE_NAME_SIZE];
334 char file_name[FILE_NAME_SIZE];
336 for (k = 0; k < conf->max_devs; k++) {
337 strcpy(file_name, "/dev/sg");
338 sprintf(buff, "%d", k);
339 strncat(file_name, buff, FILE_NAME_SIZE);
340 strcpy(all_paths[k].sg_dev, file_name);
341 if ((sg_fd = open(file_name, O_RDONLY)) < 0) {
343 fprintf(stderr, "can't open %s. mknod ?",
347 get_lun_strings(sg_fd, &all_paths[k]);
348 get_unique_id(sg_fd, &all_paths[k]);
349 if (0 > ioctl(sg_fd, SG_GET_SCSI_ID, &(all_paths[k].sg_id)))
350 printf("device %s failed on sg ioctl, skip\n",
355 for (i = 0; i < conf->max_devs; i++) {
356 if ((all_paths[k].sg_id.host_no ==
357 all_scsi_ids[i].host_no)
358 && (all_paths[k].sg_id.scsi_id ==
359 (all_scsi_ids[i].scsi_id.dev_id & 0xff))
360 && (all_paths[k].sg_id.lun ==
361 ((all_scsi_ids[i].scsi_id.dev_id >> 8) & 0xff))
362 && (all_paths[k].sg_id.channel ==
363 ((all_scsi_ids[i].scsi_id.
364 dev_id >> 16) & 0xff))) {
365 strcpy(all_paths[k].dev, all_scsi_ids[i].dev);
374 get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
376 int k, big, little, res, host_no, fd;
378 char fname[FILE_NAME_SIZE];
379 struct scsi_idlun my_scsi_id;
381 for (k = 0; k < conf->max_devs; k++) {
382 strcpy(fname, "/dev/sd");
384 buff[0] = 'a' + (char) k;
387 } else if (k <= 255) {
388 /* assumes sequence goes x,y,z,aa,ab,ac etc */
390 little = k - (26 * big);
393 buff[0] = 'a' + (char) big;
394 buff[1] = 'a' + (char) little;
398 strcat(fname, "xxxx");
400 if ((fd = open(fname, O_RDONLY)) < 0) {
402 fprintf(stderr, "can't open %s. mknod ?",
407 res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
410 printf("Could not get scsi idlun\n");
414 res = ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &host_no);
417 printf("Could not get host_no\n");
423 strcpy(all_scsi_ids[k].dev, fname);
424 all_scsi_ids[k].scsi_id = my_scsi_id;
425 all_scsi_ids[k].host_no = host_no;
430 /* print_path style */
435 print_path(struct path * all_paths, int k, int style)
438 printf("%s ", all_paths[k].wwid);
441 printf("(%i %i %i %i) ",
442 all_paths[k].sg_id.host_no,
443 all_paths[k].sg_id.channel,
444 all_paths[k].sg_id.scsi_id, all_paths[k].sg_id.lun);
445 if(0 != strcmp(all_paths[k].sg_dev, all_paths[k].dev))
446 printf("%s ", all_paths[k].sg_dev);
447 printf("%s ", all_paths[k].dev);
448 printf("[%.16s]\n", all_paths[k].product_id);
452 print_all_path(struct env * conf, struct path * all_paths)
455 char empty_buff[WWID_SIZE];
457 memset(empty_buff, 0, WWID_SIZE);
458 for (k = 0; k < conf->max_devs; k++) {
459 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
461 print_path(all_paths, k, ALL);
466 print_all_mp(struct path * all_paths, struct multipath * mp, int nmp)
470 for (k = 0; k <= nmp; k++) {
471 printf("%s\n", mp[k].wwid);
472 for (i = 0; i <= mp[k].npaths; i++)
473 print_path(all_paths, PINDEX(k,i), NOWWID);
478 get_disk_size (struct env * conf, char * dev) {
481 char attr_path[FILE_NAME_SIZE];
482 char buff[FILE_NAME_SIZE];
483 char basedev[FILE_NAME_SIZE];
485 if(conf->with_sysfs) {
486 basename(dev, basedev);
487 sprintf(attr_path, "%s/block/%s/size",
488 conf->sysfs_path, basedev);
489 if (0 > sysfs_read_attribute_value(attr_path, buff,
490 FILE_NAME_SIZE * sizeof(char)))
495 if ((fd = open(dev, O_RDONLY)) < 0)
497 if(!ioctl(fd, BLKGETSIZE, &size))
504 coalesce_paths(struct env * conf, struct multipath * mp,
505 struct path * all_paths)
507 int k, i, nmp, np, already_done;
508 char empty_buff[WWID_SIZE];
512 memset(empty_buff, 0, WWID_SIZE);
514 for (k = 0; k < conf->max_devs - 1; k++) {
515 /* skip this path for some reason */
517 /* 1. if path has no unique id */
518 if (memcmp(empty_buff, all_paths[k].wwid, WWID_SIZE) == 0)
521 /* 2. mp with this uid already instanciated */
522 for (i = 0; i <= nmp; i++) {
523 if (0 == strcmp(mp[i].wwid, all_paths[k].wwid))
531 /* at this point, we know we really got a new mp */
534 strcpy(mp[nmp].wwid, all_paths[k].wwid);
537 if (mp[nmp].size == 0)
538 mp[nmp].size = get_disk_size(conf, all_paths[k].dev);
540 for (i = k + 1; i < conf->max_devs; i++) {
541 if (0 == strcmp(all_paths[k].wwid, all_paths[i].wwid)) {
552 make_dm_node(char * str)
555 char buff[FILE_NAME_SIZE];
556 struct dm_names * names;
560 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
563 if (!dm_task_run(dmt))
566 if (!(names = dm_task_get_names(dmt)))
575 if (0 == strcmp(names->name, str))
578 names = (void *) names + next;
581 sprintf(buff, "/dev/mapper/%s", str);
583 mknod(buff, 0600 | S_IFBLK, names->dev);
586 dm_task_destroy(dmt);
592 add_map(struct env * conf, struct path * all_paths,
593 struct multipath * mp, int index, int op)
600 /* defaults for multipath target */
602 char * dm_ps_name = "round-robin";
603 int dm_ps_nr_args = 0;
605 if (!(dmt = dm_task_create(op)))
608 if (!dm_task_set_name(dmt, mp[index].wwid))
610 params_p = ¶ms[0];
613 for (i=0; i<=mp[index].npaths; i++) {
614 if (0 == all_paths[PINDEX(index,i)].sg_id.scsi_type)
623 params_p += sprintf(params_p, "%i", conf->dm_path_test_int);
625 if (all_paths[PINDEX(index,0)].iopolicy == MULTIBUS &&
626 !conf->forcedfailover ) {
627 params_p += sprintf(params_p, " %i %s %i %i",
628 dm_pg_prio, dm_ps_name, np, dm_ps_nr_args);
630 for (i=0; i<=mp[index].npaths; i++) {
631 if (0 != all_paths[PINDEX(index,i)].sg_id.scsi_type)
633 params_p += sprintf(params_p, " %s",
634 all_paths[PINDEX(index,i)].dev);
638 if (all_paths[PINDEX(index,0)].iopolicy == FAILOVER ||
639 conf->forcedfailover) {
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, " %i %s ",
644 dm_pg_prio, dm_ps_name);
645 params_p += sprintf(params_p, "1 %i",
647 params_p += sprintf(params_p, " %s",
648 all_paths[PINDEX(index,i)].dev);
652 if (mp[index].size < 0)
656 if (op == DM_DEVICE_RELOAD)
658 if (op == DM_DEVICE_CREATE)
660 printf("%s:0 %li %s %s\n",
661 mp[index].wwid, mp[index].size, DM_TARGET, params);
664 if (!dm_task_add_target(dmt, 0, mp[index].size, DM_TARGET, params))
667 if (!dm_task_run(dmt))
671 make_dm_node(mp[index].wwid);
674 dm_task_destroy(dmt);
679 map_present(char * str)
683 struct dm_names *names;
686 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
689 if (!dm_task_run(dmt))
692 if (!(names = dm_task_get_names(dmt)))
700 if (0 == strcmp(names->name, str))
703 names = (void *) names + next;
707 dm_task_destroy(dmt);
712 usage(char * progname)
714 fprintf(stderr, VERSION_STRING);
715 fprintf(stderr, "Usage: %s [-v|-q] [-d] [-i int] [-m max_devs]\n",
717 fprintf(stderr, "\t-d\t\tdry run, do not create or update devmaps\n");
718 fprintf(stderr, "\t-f\t\tforce maps to failover mode (1 path/pg)\n");
719 fprintf(stderr, "\t-i\t\tmultipath target param : polling interval\n");
720 fprintf(stderr, "\t-m max_devs\tscan {max_devs} devices at most\n");
721 fprintf(stderr, "\t-q\t\tquiet, no output at all\n");
722 fprintf(stderr, "\t-v\t\tverbose, print all paths and multipaths\n");
727 main(int argc, char *argv[])
729 struct multipath * mp;
730 struct path * all_paths;
731 struct scsi_dev * all_scsi_ids;
735 /* Default behaviour */
736 conf.max_devs = MAX_DEVS;
737 conf.dry_run = 0; /* 1 == Do not Create/Update devmaps */
738 conf.verbose = 0; /* 1 == Print all_paths and mp */
739 conf.quiet = 0; /* 1 == Do not even print devmaps */
740 conf.with_sysfs = 0; /* Default to compat / suboptimal behaviour */
741 conf.dm_path_test_int = 10;
743 /* kindly provided by libsysfs */
744 if (0 == sysfs_get_mnt_path(conf.sysfs_path, FILE_NAME_SIZE))
747 for (i = 1; i < argc; ++i) {
748 if (0 == strcmp("-v", argv[i])) {
752 } else if (0 == strcmp("-m", argv[i])) {
753 conf.max_devs = atoi(argv[++i]);
754 if (conf.max_devs < 2)
756 } else if (0 == strcmp("-q", argv[i])) {
757 if (conf.verbose == 1)
760 } else if (0 == strcmp("-d", argv[i]))
762 else if (0 == strcmp("-f", argv[i]))
763 conf.forcedfailover = 1;
764 else if (0 == strcmp("-i", argv[i]))
765 conf.dm_path_test_int = atoi(argv[++i]);
766 else if (0 == strcmp("scsi", argv[i]))
767 strcpy(conf.hotplugdev, argv[++i]);
768 else if (*argv[i] == '-') {
769 fprintf(stderr, "Unknown switch: %s\n", argv[i]);
771 } else if (*argv[i] != '-') {
772 fprintf(stderr, "Unknown argument\n");
778 /* dynamic allocations */
779 mp = malloc(conf.max_devs * sizeof(struct multipath));
780 all_paths = malloc(conf.max_devs * sizeof(struct path));
781 all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
782 if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL)
785 if (!conf.with_sysfs) {
786 get_all_scsi_ids(&conf, all_scsi_ids);
787 get_all_paths_nosysfs(&conf, all_paths, all_scsi_ids);
789 get_all_paths_sysfs(&conf, all_paths);
791 nmp = coalesce_paths(&conf, mp, all_paths);
794 print_all_path(&conf, all_paths);
795 fprintf(stdout, "\n");
796 print_all_mp(all_paths, mp, nmp);
797 fprintf(stdout, "\n");
803 for (k=0; k<=nmp; k++) {
804 if (map_present(mp[k].wwid)) {
805 add_map(&conf, all_paths, mp, k, DM_DEVICE_RELOAD);
807 add_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);