2 * Copyright (c) 2004, 2005, 2006 Christophe Varoqui
3 * Copyright (c) 2005 Stefan Bader, IBM
4 * Copyright (c) 2005 Mike Anderson
22 #include "blacklist.h"
25 #include "sg_include.h"
27 #include "discovery.h"
32 store_pathinfo (vector pathvec, vector hwtable, struct udev_device *udevice,
33 int flag, struct path **pp_ptr)
42 devname = udev_device_get_sysname(udevice);
51 if(safe_sprintf(pp->dev, "%s", devname)) {
52 condlog(0, "pp->dev too small");
55 pp->udev = udev_device_ref(udevice);
56 err = pathinfo(pp, hwtable, flag | DI_BLACKLIST);
60 err = store_path(pathvec, pp);
73 path_discover (vector pathvec, struct config * conf,
74 struct udev_device *udevice, int flag)
79 devname = udev_device_get_sysname(udevice);
83 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
87 pp = find_path_by_dev(pathvec, (char *)devname);
89 if (store_pathinfo(pathvec, conf->hwtable,
90 udevice, flag, NULL) != 1)
95 return pathinfo(pp, conf->hwtable, flag);
99 path_discovery (vector pathvec, struct config * conf, int flag)
101 struct udev_enumerate *udev_iter;
102 struct udev_list_entry *entry;
103 struct udev_device *udevice;
107 udev_iter = udev_enumerate_new(conf->udev);
111 udev_enumerate_add_match_subsystem(udev_iter, "block");
112 udev_enumerate_scan_devices(udev_iter);
114 udev_list_entry_foreach(entry,
115 udev_enumerate_get_list_entry(udev_iter)) {
117 devpath = udev_list_entry_get_name(entry);
118 condlog(4, "Discover device %s", devpath);
119 udevice = udev_device_new_from_syspath(conf->udev, devpath);
121 condlog(4, "%s: no udev information", devpath);
125 devtype = udev_device_get_devtype(udevice);
126 if(devtype && !strncmp(devtype, "disk", 4))
127 r += path_discover(pathvec, conf, udevice, flag);
128 udev_device_unref(udevice);
130 udev_enumerate_unref(udev_iter);
131 condlog(4, "Discovery status %d", r);
135 #define declare_sysfs_get_str(fname) \
137 sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len) \
140 const char * devname; \
142 devname = udev_device_get_sysname(udev); \
144 attr = udev_device_get_sysattr_value(udev, #fname); \
146 condlog(3, "%s: attribute %s not found in sysfs", \
150 if (strlen(attr) > len) { \
151 condlog(3, "%s: overflow in attribute %s", \
155 strlcpy(buff, attr, len); \
159 declare_sysfs_get_str(devtype);
160 declare_sysfs_get_str(cutype);
161 declare_sysfs_get_str(vendor);
162 declare_sysfs_get_str(model);
163 declare_sysfs_get_str(rev);
164 declare_sysfs_get_str(state);
165 declare_sysfs_get_str(dev);
168 sysfs_get_timeout(struct path *pp, unsigned int *timeout)
170 const char *attr = NULL;
172 struct udev_device *parent;
176 if (!pp->udev || pp->bus != SYSFS_BUS_SCSI)
181 subsys = udev_device_get_subsystem(parent);
182 attr = udev_device_get_sysattr_value(parent, "timeout");
185 parent = udev_device_get_parent(parent);
188 condlog(3, "%s: No timeout value in sysfs", pp->dev);
192 r = sscanf(attr, "%u\n", &t);
195 condlog(3, "%s: Cannot parse timeout attribute '%s'",
206 sysfs_get_tgt_nodename (struct path *pp, char * node)
208 const char *targetid, *value;
209 struct udev_device *parent, *tgtdev;
210 int host, channel, rport_id = -1;
212 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_device");
216 value = udev_device_get_sysattr_value(parent, "sas_address");
218 pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
219 strncpy(node, value, NODE_NAME_SIZE);
223 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_target");
226 /* Check for FibreChannel */
227 tgtdev = udev_device_get_parent(parent);
228 value = udev_device_get_sysname(tgtdev);
229 if (sscanf(value, "rport-%d:%d-%d",
230 &host, &channel, &rport_id) == 3) {
231 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev,
232 "fc_remote_ports", value);
234 condlog(3, "SCSI target %d:%d:%d -> "
236 pp->sg_id.host_no, pp->sg_id.channel,
237 pp->sg_id.scsi_id, host, channel,
239 value = udev_device_get_sysattr_value(tgtdev,
242 pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
243 pp->sg_id.transport_id = rport_id;
244 strncpy(node, value, NODE_NAME_SIZE);
245 udev_device_unref(tgtdev);
248 udev_device_unref(tgtdev);
252 /* Check for iSCSI */
256 targetid = udev_device_get_sysname(parent);
257 if (!strncmp(targetid , "session", 6))
259 parent = udev_device_get_parent(parent);
263 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev, "iscsi_session", targetid);
267 value = udev_device_get_sysattr_value(tgtdev, "targetname");
269 pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
270 strncpy(node, value, NODE_NAME_SIZE);
271 udev_device_unref(tgtdev);
275 udev_device_unref(tgtdev);
282 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
284 struct udev_device *rport_dev = NULL;
288 if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP) {
289 condlog(3, "%s: Not a FCP device", pp->dev);
292 sprintf(rport_id, "rport-%d:%d-%d",
293 pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
294 rport_dev = udev_device_new_from_subsystem_sysname(conf->udev,
295 "fc_remote_ports", rport_id);
297 condlog(1, "%s: No fc_remote_port device for '%s'", pp->dev,
301 condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
302 pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
304 snprintf(value, 11, "%u", mpp->dev_loss);
306 sysfs_attr_set_value(rport_dev, "dev_loss_tmo", value, 11) <= 0) {
307 if ((mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET ||
308 mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
309 && mpp->dev_loss > 600) {
310 condlog(3, "%s: limiting dev_loss_tmo to 600, since "
311 "fast_io_fail is not set", mpp->alias);
312 snprintf(value, 11, "%u", 600);
313 if (sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
315 condlog(0, "%s failed to set dev_loss_tmo",
320 if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET){
321 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
322 sprintf(value, "off");
323 else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
326 snprintf(value, 11, "%u", mpp->fast_io_fail);
327 if (sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
329 condlog(0, "%s failed to set fast_io_fail_tmo",
334 udev_device_unref(rport_dev);
338 sysfs_set_scsi_tmo (struct multipath *mpp)
342 int dev_loss_tmo = mpp->dev_loss;
344 if (mpp->no_path_retry > 0) {
345 int no_path_retry_tmo = mpp->no_path_retry * conf->checkint;
347 if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
348 no_path_retry_tmo = MAX_DEV_LOSS_TMO;
349 if (no_path_retry_tmo > dev_loss_tmo)
350 dev_loss_tmo = no_path_retry_tmo;
351 condlog(3, "%s: update dev_loss_tmo to %d",
352 mpp->alias, dev_loss_tmo);
353 } else if (mpp->no_path_retry == NO_PATH_RETRY_QUEUE) {
354 dev_loss_tmo = MAX_DEV_LOSS_TMO;
355 condlog(3, "%s: update dev_loss_tmo to %d",
356 mpp->alias, dev_loss_tmo);
358 mpp->dev_loss = dev_loss_tmo;
359 if (mpp->dev_loss && mpp->fast_io_fail >= (int)mpp->dev_loss) {
360 condlog(3, "%s: turning off fast_io_fail (%d is not smaller than dev_loss_tmo)",
361 mpp->alias, mpp->fast_io_fail);
362 mpp->fast_io_fail = MP_FAST_IO_FAIL_OFF;
364 if (!mpp->dev_loss && mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
367 vector_foreach_slot(mpp->paths, pp, i) {
368 sysfs_set_rport_tmo(mpp, pp);
374 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
375 void *resp, int mx_resp_len)
377 unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
378 { INQUIRY_CMD, 0, 0, 0, 0, 0 };
379 unsigned char sense_b[SENSE_BUFF_LEN];
380 struct sg_io_hdr io_hdr;
386 inqCmdBlk[2] = (unsigned char) pg_op;
387 inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
388 inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
389 memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
390 memset(sense_b, 0, SENSE_BUFF_LEN);
391 io_hdr.interface_id = 'S';
392 io_hdr.cmd_len = sizeof (inqCmdBlk);
393 io_hdr.mx_sb_len = sizeof (sense_b);
394 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
395 io_hdr.dxfer_len = mx_resp_len;
396 io_hdr.dxferp = resp;
397 io_hdr.cmdp = inqCmdBlk;
398 io_hdr.sbp = sense_b;
399 io_hdr.timeout = DEF_TIMEOUT;
401 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
404 /* treat SG_ERR here to get rid of sg_err.[ch] */
405 io_hdr.status &= 0x7e;
406 if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
407 (0 == io_hdr.driver_status))
409 if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
410 (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
411 (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
412 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
414 unsigned char * sense_buffer = io_hdr.sbp;
415 if (sense_buffer[0] & 0x2)
416 sense_key = sense_buffer[1] & 0xf;
418 sense_key = sense_buffer[2] & 0xf;
419 if(RECOVERED_ERROR == sense_key)
427 get_serial (char * str, int maxlen, int fd)
430 char buff[MX_ALLOC_LEN + 1] = {0};
435 if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN)) {
440 memcpy(str, buff + 4, len);
449 get_geometry(struct path *pp)
454 if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
455 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
456 memset(&pp->geom, 0, sizeof(pp->geom));
459 condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
460 pp->dev, pp->geom.cylinders, pp->geom.heads,
461 pp->geom.sectors, pp->geom.start);
466 scsi_sysfs_pathinfo (struct path * pp)
468 struct udev_device *parent;
469 const char *attr_path = NULL;
473 const char *subsys = udev_device_get_subsystem(parent);
474 if (subsys && !strncmp(subsys, "scsi", 4)) {
475 attr_path = udev_device_get_sysname(parent);
478 if (sscanf(attr_path, "%i:%i:%i:%i",
482 &pp->sg_id.lun) == 4)
485 parent = udev_device_get_parent(parent);
487 if (!attr_path || pp->sg_id.host_no == -1)
490 if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE))
493 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
495 if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE))
498 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
500 if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE))
503 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
506 * set the hwe configlet pointer
508 pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
511 * host / bus / target / lun
513 condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
523 if(!sysfs_get_tgt_nodename(pp, pp->tgt_node_name)) {
524 condlog(3, "%s: tgt_node_name = %s",
525 pp->dev, pp->tgt_node_name);
532 ccw_sysfs_pathinfo (struct path * pp)
534 struct udev_device *parent;
535 char attr_buff[NAME_SIZE];
536 const char *attr_path;
540 const char *subsys = udev_device_get_subsystem(parent);
541 if (subsys && !strncmp(subsys, "ccw", 3))
543 parent = udev_device_get_parent(parent);
548 sprintf(pp->vendor_id, "IBM");
550 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
552 if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE))
555 if (!strncmp(attr_buff, "3370", 4)) {
556 sprintf(pp->product_id,"S/390 DASD FBA");
557 } else if (!strncmp(attr_buff, "9336", 4)) {
558 sprintf(pp->product_id,"S/390 DASD FBA");
560 sprintf(pp->product_id,"S/390 DASD ECKD");
563 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
566 * set the hwe configlet pointer
568 pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, NULL);
571 * host / bus / target / lun
573 attr_path = udev_device_get_sysname(parent);
575 sscanf(attr_path, "%i.%i.%x",
579 condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
590 cciss_sysfs_pathinfo (struct path * pp)
592 const char * attr_path = NULL;
593 struct udev_device *parent;
597 const char *subsys = udev_device_get_subsystem(parent);
598 if (subsys && !strncmp(subsys, "cciss", 5)) {
599 attr_path = udev_device_get_sysname(parent);
602 if (sscanf(attr_path, "c%id%i",
604 &pp->sg_id.scsi_id) == 2)
607 parent = udev_device_get_parent(parent);
609 if (!attr_path || pp->sg_id.host_no == -1)
612 if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE))
615 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
617 if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE))
620 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
622 if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE))
625 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
628 * set the hwe configlet pointer
630 pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
633 * host / bus / target / lun
636 pp->sg_id.channel = 0;
637 condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
647 common_sysfs_pathinfo (struct path * pp)
650 condlog(4, "%s: udev not initialised", pp->dev);
653 if (sysfs_get_dev(pp->udev, pp->dev_t, BLK_DEV_SIZE)) {
654 condlog(3, "%s: no 'dev' attribute in sysfs", pp->dev);
658 condlog(3, "%s: dev_t = %s", pp->dev, pp->dev_t);
660 if (sysfs_get_size(pp, &pp->size))
663 condlog(3, "%s: size = %llu", pp->dev, pp->size);
669 path_offline (struct path * pp)
671 struct udev_device * parent;
672 char buff[SCSI_STATE_SIZE];
674 if (pp->bus != SYSFS_BUS_SCSI)
679 const char *subsys = udev_device_get_subsystem(parent);
680 if (subsys && !strncmp(subsys, "scsi", 4))
682 parent = udev_device_get_parent(parent);
686 condlog(1, "%s: failed to get sysfs information", pp->dev);
690 if (sysfs_get_state(parent, buff, SCSI_STATE_SIZE))
693 condlog(3, "%s: path state = %s", pp->dev, buff);
695 if (!strncmp(buff, "offline", 7)) {
700 if (!strncmp(buff, "blocked", 7))
702 else if (!strncmp(buff, "running", 7))
709 sysfs_pathinfo(struct path * pp)
711 if (common_sysfs_pathinfo(pp))
714 pp->bus = SYSFS_BUS_UNDEF;
715 if (!strncmp(pp->dev,"cciss",5))
716 pp->bus = SYSFS_BUS_CCISS;
717 if (!strncmp(pp->dev,"dasd", 4))
718 pp->bus = SYSFS_BUS_CCW;
719 if (!strncmp(pp->dev,"sd", 2))
720 pp->bus = SYSFS_BUS_SCSI;
722 if (pp->bus == SYSFS_BUS_UNDEF)
724 else if (pp->bus == SYSFS_BUS_SCSI) {
725 if (scsi_sysfs_pathinfo(pp))
727 } else if (pp->bus == SYSFS_BUS_CCW) {
728 if (ccw_sysfs_pathinfo(pp))
730 } else if (pp->bus == SYSFS_BUS_CCISS) {
731 if (cciss_sysfs_pathinfo(pp))
738 scsi_ioctl_pathinfo (struct path * pp, int mask)
740 if (mask & DI_SERIAL) {
741 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
742 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
749 cciss_ioctl_pathinfo (struct path * pp, int mask)
751 if (mask & DI_SERIAL) {
752 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
753 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
759 get_state (struct path * pp, int daemon)
761 struct checker * c = &pp->checker;
764 condlog(3, "%s: get_state", pp->dev);
766 if (!checker_selected(c)) {
768 if (pathinfo(pp, conf->hwtable, DI_SYSFS) != 0) {
769 condlog(3, "%s: couldn't get sysfs pathinfo",
771 return PATH_UNCHECKED;
775 if (!checker_selected(c)) {
776 condlog(3, "%s: No checker selected", pp->dev);
777 return PATH_UNCHECKED;
779 checker_set_fd(c, pp->fd);
780 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
781 memset(c, 0x0, sizeof(struct checker));
782 condlog(3, "%s: checker init failed", pp->dev);
783 return PATH_UNCHECKED;
786 checker_clear_message(c);
788 checker_set_async(c);
789 if (!conf->checker_timeout &&
790 (pp->bus != SYSFS_BUS_SCSI ||
791 sysfs_get_timeout(pp, &(c->timeout))))
792 c->timeout = DEF_TIMEOUT;
793 state = checker_check(c);
794 condlog(3, "%s: state = %s", pp->dev, checker_state_name(state));
795 if (state != PATH_UP && strlen(checker_message(c)))
796 condlog(3, "%s: checker msg is \"%s\"",
797 pp->dev, checker_message(c));
802 get_prio (struct path * pp)
807 struct prio * p = &pp->prio;
809 if (!prio_selected(p)) {
810 select_detect_prio(pp);
812 if (!prio_selected(p)) {
813 condlog(3, "%s: no prio selected", pp->dev);
817 pp->priority = prio_getprio(p, pp);
818 if (pp->priority < 0) {
819 condlog(3, "%s: %s prio error", pp->dev, prio_name(p));
820 pp->priority = PRIO_UNDEF;
823 condlog(3, "%s: %s prio = %u",
824 pp->dev, prio_name(p), pp->priority);
829 get_uid (struct path * pp)
834 if (!pp->uid_attribute)
838 condlog(1, "%s: no udev information", pp->dev);
842 memset(pp->wwid, 0, WWID_SIZE);
843 value = udev_device_get_property_value(pp->udev, pp->uid_attribute);
844 if ((!value || strlen(value) == 0) && conf->dry_run == 2)
845 value = getenv(pp->uid_attribute);
846 if (value && strlen(value)) {
847 size_t len = WWID_SIZE;
849 if (strlen(value) + 1 > WWID_SIZE) {
850 condlog(0, "%s: wwid overflow", pp->dev);
854 strncpy(pp->wwid, value, len);
856 condlog(3, "%s: no %s attribute", pp->dev,
860 /* Strip any trailing blanks */
861 c = strchr(pp->wwid, '\0');
863 while (c && c >= pp->wwid && *c == ' ') {
867 condlog(3, "%s: uid = %s (udev)", pp->dev,
868 *pp->wwid == '\0' ? "<empty>" : pp->wwid);
873 pathinfo (struct path *pp, vector hwtable, int mask)
877 condlog(3, "%s: mask = 0x%x", pp->dev, mask);
880 * fetch info available in sysfs
882 if (mask & DI_SYSFS && sysfs_pathinfo(pp))
885 if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
886 if (filter_device(conf->blist_device, conf->elist_device,
887 pp->vendor_id, pp->product_id) > 0) {
892 path_state = path_offline(pp);
895 * fetch info not available through sysfs
898 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
901 condlog(4, "Couldn't open node for %s: %s",
902 pp->dev, strerror(errno));
906 if (mask & DI_SERIAL)
909 if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI &&
910 scsi_ioctl_pathinfo(pp, mask))
913 if (pp->bus == SYSFS_BUS_CCISS &&
914 cciss_ioctl_pathinfo(pp, mask))
917 if (mask & DI_CHECKER) {
918 if (path_state == PATH_UP) {
919 pp->chkrstate = pp->state = get_state(pp, 0);
920 if (pp->state == PATH_UNCHECKED ||
921 pp->state == PATH_WILD)
924 condlog(3, "%s: path inaccessible", pp->dev);
925 pp->chkrstate = pp->state = path_state;
929 if (path_state == PATH_UP && (mask & DI_WWID) && !strlen(pp->wwid))
931 if (mask & DI_BLACKLIST && mask & DI_WWID) {
932 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
939 * Retrieve path priority, even for PATH_DOWN paths if it has never
940 * been successfully obtained before.
942 if ((mask & DI_PRIO) && path_state == PATH_UP) {
943 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
944 if (!strlen(pp->wwid))
948 pp->priority = PRIO_UNDEF;
956 * Recoverable error, for example faulty or offline path
958 memset(pp->wwid, 0, WWID_SIZE);
959 pp->chkrstate = pp->state = PATH_DOWN;