2 * Copyright (c) 2004, 2005, 2006 Christophe Varoqui
3 * Copyright (c) 2005 Stefan Bader, IBM
4 * Copyright (c) 2005 Mike Anderson
11 #include <sys/ioctl.h>
23 #include "blacklist.h"
27 #include "sg_include.h"
29 #include "discovery.h"
32 #include "unaligned.h"
33 #include "prioritizers/alua_rtpg.h"
35 #include "configure.h"
39 #define VPD_BUFLEN 4096
41 struct vpd_vendor_page vpd_vendor_pages[VPD_VP_ARRAY_SIZE] = {
42 [VPD_VP_UNDEF] = { 0x00, "undef" },
43 [VPD_VP_HP3PAR] = { 0xc0, "hp3par" },
47 alloc_path_with_pathinfo (struct config *conf, struct udev_device *udevice,
48 const char *wwid, int flag, struct path **pp_ptr)
50 int err = PATHINFO_FAILED;
57 devname = udev_device_get_sysname(udevice);
59 return PATHINFO_FAILED;
64 return PATHINFO_FAILED;
67 strlcpy(pp->wwid, wwid, sizeof(pp->wwid));
69 if (safe_sprintf(pp->dev, "%s", devname)) {
70 condlog(0, "pp->dev too small");
73 pp->udev = udev_device_ref(udevice);
74 err = pathinfo(pp, conf, flag | DI_BLACKLIST);
85 store_pathinfo (vector pathvec, struct config *conf,
86 struct udev_device *udevice, int flag, struct path **pp_ptr)
88 int err = PATHINFO_FAILED;
95 devname = udev_device_get_sysname(udevice);
97 return PATHINFO_FAILED;
102 return PATHINFO_FAILED;
104 if(safe_sprintf(pp->dev, "%s", devname)) {
105 condlog(0, "pp->dev too small");
108 pp->udev = udev_device_ref(udevice);
109 err = pathinfo(pp, conf, flag);
113 err = store_path(pathvec, pp);
116 pp->checkint = conf->checkint;
127 path_discover (vector pathvec, struct config * conf,
128 struct udev_device *udevice, int flag)
131 char devt[BLK_DEV_SIZE];
132 dev_t devnum = udev_device_get_devnum(udevice);
134 snprintf(devt, BLK_DEV_SIZE, "%d:%d",
135 major(devnum), minor(devnum));
136 pp = find_path_by_devt(pathvec, devt);
138 return store_pathinfo(pathvec, conf,
139 udevice, flag | DI_BLACKLIST,
143 * Don't use DI_BLACKLIST on paths already in pathvec. We rely
144 * on the caller to pre-populate the pathvec with valid paths
147 return pathinfo(pp, conf, flag);
150 static void cleanup_udev_enumerate_ptr(void *arg)
152 struct udev_enumerate *ue;
156 ue = *((struct udev_enumerate**) arg);
158 (void)udev_enumerate_unref(ue);
161 static void cleanup_udev_device_ptr(void *arg)
163 struct udev_device *ud;
167 ud = *((struct udev_device**) arg);
169 (void)udev_device_unref(ud);
173 path_discovery (vector pathvec, int flag)
175 struct udev_enumerate *udev_iter = NULL;
176 struct udev_list_entry *entry;
177 struct udev_device *udevice = NULL;
179 int num_paths = 0, total_paths = 0, ret;
181 pthread_cleanup_push(cleanup_udev_enumerate_ptr, &udev_iter);
182 pthread_cleanup_push(cleanup_udev_device_ptr, &udevice);
183 conf = get_multipath_config();
184 pthread_cleanup_push(put_multipath_config, conf);
186 udev_iter = udev_enumerate_new(udev);
192 if (udev_enumerate_add_match_subsystem(udev_iter, "block") < 0 ||
193 udev_enumerate_add_match_is_initialized(udev_iter) < 0 ||
194 udev_enumerate_scan_devices(udev_iter) < 0) {
195 condlog(1, "%s: error setting up udev_enumerate: %m", __func__);
200 udev_list_entry_foreach(entry,
201 udev_enumerate_get_list_entry(udev_iter)) {
208 devpath = udev_list_entry_get_name(entry);
209 condlog(4, "Discover device %s", devpath);
210 udevice = udev_device_new_from_syspath(udev, devpath);
212 condlog(4, "%s: no udev information", devpath);
215 devtype = udev_device_get_devtype(udevice);
216 if(devtype && !strncmp(devtype, "disk", 4)) {
218 if (path_discover(pathvec, conf,
219 udevice, flag) == PATHINFO_OK)
222 udevice = udev_device_unref(udevice);
224 ret = total_paths - num_paths;
225 condlog(4, "Discovered %d/%d paths", num_paths, total_paths);
227 pthread_cleanup_pop(1);
228 pthread_cleanup_pop(1);
229 pthread_cleanup_pop(1);
233 #define declare_sysfs_get_str(fname) \
235 sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len) \
239 const char * devname; \
244 devname = udev_device_get_sysname(udev); \
246 attr = udev_device_get_sysattr_value(udev, #fname); \
248 condlog(3, "%s: attribute %s not found in sysfs", \
252 for (l = strlen(attr); l >= 1 && isspace(attr[l-1]); l--); \
254 condlog(3, "%s: overflow in attribute %s", \
258 strlcpy(buff, attr, len); \
259 return strchop(buff); \
262 declare_sysfs_get_str(devtype);
263 declare_sysfs_get_str(vendor);
264 declare_sysfs_get_str(model);
265 declare_sysfs_get_str(rev);
268 sysfs_get_binary (struct udev_device * udev, const char *attrname,
269 unsigned char *buff, size_t len)
272 const char * devname;
275 condlog(3, "No udev device given\n");
279 devname = udev_device_get_sysname(udev);
280 attr_len = sysfs_bin_attr_get_value(udev, attrname, buff, len);
282 condlog(3, "%s: attribute %s not found in sysfs",
289 ssize_t sysfs_get_vpd(struct udev_device * udev, unsigned char pg,
290 unsigned char *buff, size_t len)
294 snprintf(attrname, sizeof(attrname), "vpd_pg%02x", pg);
295 return sysfs_get_binary(udev, attrname, buff, len);
298 ssize_t sysfs_get_inquiry(struct udev_device * udev,
299 unsigned char *buff, size_t len)
301 return sysfs_get_binary(udev, "inquiry", buff, len);
305 sysfs_get_timeout(const struct path *pp, unsigned int *timeout)
307 const char *attr = NULL;
309 struct udev_device *parent;
313 if (!pp->udev || pp->bus != SYSFS_BUS_SCSI)
318 subsys = udev_device_get_subsystem(parent);
319 attr = udev_device_get_sysattr_value(parent, "timeout");
322 parent = udev_device_get_parent(parent);
325 condlog(3, "%s: No timeout value in sysfs", pp->dev);
329 t = strtoul(attr, &eptr, 0);
330 if (attr == eptr || t == ULONG_MAX) {
331 condlog(3, "%s: Cannot parse timeout attribute '%s'",
336 condlog(3, "%s: Overflow in timeout value '%s'",
346 sysfs_get_tgt_nodename(struct path *pp, char *node)
348 const char *tgtname, *value;
349 struct udev_device *parent, *tgtdev;
350 int host, channel, tgtid = -1;
354 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev,
355 "scsi", "scsi_device");
359 value = udev_device_get_sysattr_value(parent, "sas_address");
361 tgtdev = udev_device_get_parent(parent);
365 tgtname = udev_device_get_sysname(tgtdev);
367 if (sscanf(tgtname, "end_device-%d:%d:%d%c",
368 &host, &channel, &tgtid, &c) == 3)
370 if (sscanf(tgtname, "end_device-%d:%d%c",
371 &host, &tgtid, &c) == 2)
374 tgtdev = udev_device_get_parent(tgtdev);
378 pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
379 pp->sg_id.transport_id = tgtid;
380 strlcpy(node, value, NODE_NAME_SIZE);
386 tgtdev = udev_device_get_parent(parent);
388 value = udev_device_get_subsystem(tgtdev);
389 if (value && !strcmp(value, "usb")) {
390 pp->sg_id.proto_id = SCSI_PROTOCOL_USB;
391 tgtname = udev_device_get_sysname(tgtdev);
393 strlcpy(node, tgtname, NODE_NAME_SIZE);
397 tgtdev = udev_device_get_parent(tgtdev);
399 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_target");
402 /* Check for FibreChannel */
403 tgtdev = udev_device_get_parent(parent);
404 value = udev_device_get_sysname(tgtdev);
405 if (value && sscanf(value, "rport-%d:%d-%d",
406 &host, &channel, &tgtid) == 3) {
407 tgtdev = udev_device_new_from_subsystem_sysname(udev,
408 "fc_remote_ports", value);
410 condlog(4, "SCSI target %d:%d:%d -> "
412 pp->sg_id.host_no, pp->sg_id.channel,
413 pp->sg_id.scsi_id, host, channel,
415 value = udev_device_get_sysattr_value(tgtdev,
418 pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
419 pp->sg_id.transport_id = tgtid;
420 strlcpy(node, value, NODE_NAME_SIZE);
421 udev_device_unref(tgtdev);
424 udev_device_unref(tgtdev);
428 /* Check for iSCSI */
432 tgtname = udev_device_get_sysname(parent);
433 if (tgtname && sscanf(tgtname , "session%d", &tgtid) == 1)
435 parent = udev_device_get_parent(parent);
439 if (parent && tgtname) {
440 tgtdev = udev_device_new_from_subsystem_sysname(udev,
441 "iscsi_session", tgtname);
445 value = udev_device_get_sysattr_value(tgtdev, "targetname");
447 pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
448 pp->sg_id.transport_id = tgtid;
449 strlcpy(node, value, NODE_NAME_SIZE);
450 udev_device_unref(tgtdev);
454 udev_device_unref(tgtdev);
457 /* Check for libata */
461 tgtname = udev_device_get_sysname(parent);
462 if (tgtname && sscanf(tgtname, "ata%d", &tgtid) == 1)
464 parent = udev_device_get_parent(parent);
468 pp->sg_id.proto_id = SCSI_PROTOCOL_ATA;
469 pp->sg_id.transport_id = tgtid;
470 snprintf(node, NODE_NAME_SIZE, "ata-%d.00", tgtid);
473 /* Unknown SCSI transport. Keep fingers crossed */
474 pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
478 static int sysfs_get_host_bus_id(const struct path *pp, char *bus_id)
480 struct udev_device *hostdev, *parent;
481 char host_name[HOST_NAME_LEN];
482 const char *driver_name, *subsystem_name, *value;
487 snprintf(host_name, sizeof(host_name), "host%d", pp->sg_id.host_no);
488 hostdev = udev_device_new_from_subsystem_sysname(udev,
489 "scsi_host", host_name);
493 for (parent = udev_device_get_parent(hostdev);
495 parent = udev_device_get_parent(parent)) {
496 driver_name = udev_device_get_driver(parent);
497 subsystem_name = udev_device_get_subsystem(parent);
498 if (driver_name && !strcmp(driver_name, "pcieport"))
500 if (subsystem_name && !strcmp(subsystem_name, "ccw"))
504 /* pci_device or ccw fcp device found
506 value = udev_device_get_sysname(parent);
509 udev_device_unref(hostdev);
513 strlcpy(bus_id, value, SLOT_NAME_SIZE);
514 udev_device_unref(hostdev);
517 udev_device_unref(hostdev);
521 int sysfs_get_host_adapter_name(const struct path *pp, char *adapter_name)
525 if (!pp || !adapter_name)
528 proto_id = pp->sg_id.proto_id;
530 if (proto_id != SCSI_PROTOCOL_FCP &&
531 proto_id != SCSI_PROTOCOL_SAS &&
532 proto_id != SCSI_PROTOCOL_ISCSI &&
533 proto_id != SCSI_PROTOCOL_SRP) {
536 /* iscsi doesn't have adapter info in sysfs
537 * get ip_address for grouping paths
539 if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
540 return sysfs_get_iscsi_ip_address(pp, adapter_name);
542 /* fetch adapter bus-ID for other protocols
544 return sysfs_get_host_bus_id(pp, adapter_name);
547 int sysfs_get_iscsi_ip_address(const struct path *pp, char *ip_address)
549 struct udev_device *hostdev;
550 char host_name[HOST_NAME_LEN];
553 sprintf(host_name, "host%d", pp->sg_id.host_no);
554 hostdev = udev_device_new_from_subsystem_sysname(udev,
555 "iscsi_host", host_name);
557 value = udev_device_get_sysattr_value(hostdev,
560 strncpy(ip_address, value, SLOT_NAME_SIZE);
561 udev_device_unref(hostdev);
564 udev_device_unref(hostdev);
570 sysfs_get_asymmetric_access_state(struct path *pp, char *buff, int buflen)
572 struct udev_device *parent = pp->udev;
573 char value[16], *eptr;
574 unsigned long preferred;
577 const char *subsys = udev_device_get_subsystem(parent);
578 if (subsys && !strncmp(subsys, "scsi", 4))
580 parent = udev_device_get_parent(parent);
586 if (sysfs_attr_get_value(parent, "access_state", buff, buflen) <= 0)
589 if (sysfs_attr_get_value(parent, "preferred_path", value, 16) <= 0)
592 preferred = strtoul(value, &eptr, 0);
593 if (value == eptr || preferred == ULONG_MAX) {
594 /* Parse error, ignore */
601 sysfs_set_eh_deadline(struct multipath *mpp, struct path *pp)
603 struct udev_device *hostdev;
604 char host_name[HOST_NAME_LEN], value[16];
607 if (mpp->eh_deadline == EH_DEADLINE_UNSET)
610 sprintf(host_name, "host%d", pp->sg_id.host_no);
611 hostdev = udev_device_new_from_subsystem_sysname(udev,
612 "scsi_host", host_name);
616 if (mpp->eh_deadline == EH_DEADLINE_OFF)
617 len = sprintf(value, "off");
618 else if (mpp->eh_deadline == EH_DEADLINE_ZERO)
619 len = sprintf(value, "0");
621 len = sprintf(value, "%d", mpp->eh_deadline);
623 ret = sysfs_attr_set_value(hostdev, "eh_deadline",
626 * not all scsi drivers support setting eh_deadline, so failing
627 * is totally reasonable
630 condlog(3, "%s: failed to set eh_deadline to %s, error %d", udev_device_get_sysname(hostdev), value, -ret);
632 udev_device_unref(hostdev);
637 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
639 struct udev_device *rport_dev = NULL;
640 char value[16], *eptr;
645 if (mpp->dev_loss == DEV_LOSS_TMO_UNSET &&
646 mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
649 sprintf(rport_id, "rport-%d:%d-%d",
650 pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
651 rport_dev = udev_device_new_from_subsystem_sysname(udev,
652 "fc_remote_ports", rport_id);
654 condlog(1, "%s: No fc_remote_port device for '%s'", pp->dev,
658 condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
659 pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
662 * read the current dev_loss_tmo value from sysfs
664 ret = sysfs_attr_get_value(rport_dev, "dev_loss_tmo", value, 16);
666 condlog(0, "%s: failed to read dev_loss_tmo value, "
667 "error %d", rport_id, -ret);
670 tmo = strtoul(value, &eptr, 0);
672 condlog(0, "%s: Cannot parse dev_loss_tmo "
673 "attribute '%s'", rport_id, value);
679 * dev_loss_tmo will be limited to 600 if fast_io_fail
681 * fast_io_fail will be limited by the current dev_loss_tmo
683 * So to get everything right we first need to increase
684 * dev_loss_tmo to the fast_io_fail setting (if present),
685 * then set fast_io_fail, and _then_ set dev_loss_tmo
686 * to the correct value.
688 if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET &&
689 mpp->fast_io_fail != MP_FAST_IO_FAIL_ZERO &&
690 mpp->fast_io_fail != MP_FAST_IO_FAIL_OFF) {
691 /* Check if we need to temporarily increase dev_loss_tmo */
692 if ((unsigned int)mpp->fast_io_fail >= tmo) {
693 /* Increase dev_loss_tmo temporarily */
694 snprintf(value, sizeof(value), "%u",
695 (unsigned int)mpp->fast_io_fail + 1);
696 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
697 value, strlen(value));
700 condlog(3, "%s: rport blocked",
703 condlog(0, "%s: failed to set "
704 "dev_loss_tmo to %s, error %d",
705 rport_id, value, -ret);
709 } else if (mpp->dev_loss > DEFAULT_DEV_LOSS_TMO &&
710 mpp->no_path_retry != NO_PATH_RETRY_QUEUE) {
711 condlog(2, "%s: limiting dev_loss_tmo to %d, since "
712 "fast_io_fail is not set",
713 rport_id, DEFAULT_DEV_LOSS_TMO);
714 mpp->dev_loss = DEFAULT_DEV_LOSS_TMO;
716 if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
717 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
718 sprintf(value, "off");
719 else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
722 snprintf(value, 16, "%u", mpp->fast_io_fail);
723 ret = sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
724 value, strlen(value));
727 condlog(3, "%s: rport blocked", rport_id);
729 condlog(0, "%s: failed to set fast_io_fail_tmo to %s, error %d",
730 rport_id, value, -ret);
733 if (mpp->dev_loss != DEV_LOSS_TMO_UNSET) {
734 snprintf(value, 16, "%u", mpp->dev_loss);
735 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
736 value, strlen(value));
739 condlog(3, "%s: rport blocked", rport_id);
741 condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
742 rport_id, value, -ret);
746 udev_device_unref(rport_dev);
750 sysfs_set_session_tmo(struct multipath *mpp, struct path *pp)
752 struct udev_device *session_dev = NULL;
756 if (mpp->dev_loss != DEV_LOSS_TMO_UNSET)
757 condlog(3, "%s: ignoring dev_loss_tmo on iSCSI", pp->dev);
758 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
761 sprintf(session_id, "session%d", pp->sg_id.transport_id);
762 session_dev = udev_device_new_from_subsystem_sysname(udev,
763 "iscsi_session", session_id);
765 condlog(1, "%s: No iscsi session for '%s'", pp->dev,
769 condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
770 pp->sg_id.channel, pp->sg_id.scsi_id, session_id);
772 if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
773 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF) {
774 condlog(3, "%s: can't switch off fast_io_fail_tmo "
775 "on iSCSI", pp->dev);
776 } else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO) {
777 condlog(3, "%s: can't set fast_io_fail_tmo to '0'"
778 "on iSCSI", pp->dev);
780 snprintf(value, 11, "%u", mpp->fast_io_fail);
781 if (sysfs_attr_set_value(session_dev, "recovery_tmo",
782 value, strlen(value)) <= 0) {
783 condlog(3, "%s: Failed to set recovery_tmo, "
784 " error %d", pp->dev, errno);
788 udev_device_unref(session_dev);
793 sysfs_set_nexus_loss_tmo(struct multipath *mpp, struct path *pp)
795 struct udev_device *parent, *sas_dev = NULL;
796 const char *end_dev_id = NULL;
798 static const char ed_str[] = "end_device-";
800 if (!pp->udev || mpp->dev_loss == DEV_LOSS_TMO_UNSET)
803 for (parent = udev_device_get_parent(pp->udev);
805 parent = udev_device_get_parent(parent)) {
806 const char *ed = udev_device_get_sysname(parent);
808 if (ed && !strncmp(ed, ed_str, sizeof(ed_str) - 1)) {
814 condlog(1, "%s: No SAS end device", pp->dev);
817 sas_dev = udev_device_new_from_subsystem_sysname(udev,
818 "sas_end_device", end_dev_id);
820 condlog(1, "%s: No SAS end device for '%s'", pp->dev,
824 condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
825 pp->sg_id.channel, pp->sg_id.scsi_id, end_dev_id);
827 if (mpp->dev_loss != DEV_LOSS_TMO_UNSET) {
828 snprintf(value, 11, "%u", mpp->dev_loss);
829 if (sysfs_attr_set_value(sas_dev, "I_T_nexus_loss_timeout",
830 value, strlen(value)) <= 0)
831 condlog(3, "%s: failed to update "
832 "I_T Nexus loss timeout, error %d",
835 udev_device_unref(sas_dev);
840 sysfs_set_scsi_tmo (struct multipath *mpp, unsigned int checkint)
844 unsigned int dev_loss_tmo = mpp->dev_loss;
845 struct path *err_path = NULL;
846 STATIC_BITFIELD(bf, LAST_BUS_PROTOCOL_ID + 1);
848 if (mpp->no_path_retry > 0) {
849 uint64_t no_path_retry_tmo =
850 (uint64_t)mpp->no_path_retry * checkint;
852 if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
853 no_path_retry_tmo = MAX_DEV_LOSS_TMO;
854 if (no_path_retry_tmo > dev_loss_tmo)
855 dev_loss_tmo = no_path_retry_tmo;
856 } else if (mpp->no_path_retry == NO_PATH_RETRY_QUEUE) {
857 dev_loss_tmo = MAX_DEV_LOSS_TMO;
859 if (mpp->dev_loss != DEV_LOSS_TMO_UNSET &&
860 mpp->dev_loss != dev_loss_tmo) {
861 condlog(2, "%s: Using dev_loss_tmo=%u instead of %u because of no_path_retry setting",
862 mpp->alias, dev_loss_tmo, mpp->dev_loss);
863 mpp->dev_loss = dev_loss_tmo;
865 if (mpp->dev_loss != DEV_LOSS_TMO_UNSET &&
866 mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET &&
867 (unsigned int)mpp->fast_io_fail >= mpp->dev_loss) {
868 condlog(3, "%s: turning off fast_io_fail (%d is not smaller than dev_loss_tmo)",
869 mpp->alias, mpp->fast_io_fail);
870 mpp->fast_io_fail = MP_FAST_IO_FAIL_OFF;
872 if (mpp->dev_loss == DEV_LOSS_TMO_UNSET &&
873 mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET &&
874 mpp->eh_deadline == EH_DEADLINE_UNSET)
877 vector_foreach_slot(mpp->paths, pp, i) {
878 if (pp->bus != SYSFS_BUS_SCSI) {
884 switch (pp->sg_id.proto_id) {
885 case SCSI_PROTOCOL_FCP:
886 sysfs_set_rport_tmo(mpp, pp);
888 case SCSI_PROTOCOL_ISCSI:
889 sysfs_set_session_tmo(mpp, pp);
891 case SCSI_PROTOCOL_SAS:
892 sysfs_set_nexus_loss_tmo(mpp, pp);
898 sysfs_set_eh_deadline(mpp, pp);
901 if (err_path && !is_bit_set_in_bitfield(bus_protocol_id(pp), bf)) {
902 STRBUF_ON_STACK(proto_buf);
904 snprint_path_protocol(&proto_buf, err_path);
905 condlog(2, "%s: setting dev_loss_tmo is unsupported for protocol %s",
906 mpp->alias, get_strbuf_str(&proto_buf));
907 set_bit_in_bitfield(bus_protocol_id(pp), bf);
913 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
914 void *resp, int mx_resp_len)
916 unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
917 { INQUIRY_CMD, 0, 0, 0, 0, 0 };
918 unsigned char sense_b[SENSE_BUFF_LEN];
919 struct sg_io_hdr io_hdr;
925 inqCmdBlk[2] = (unsigned char) pg_op;
926 inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
927 inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
928 memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
929 memset(sense_b, 0, SENSE_BUFF_LEN);
930 io_hdr.interface_id = 'S';
931 io_hdr.cmd_len = sizeof (inqCmdBlk);
932 io_hdr.mx_sb_len = sizeof (sense_b);
933 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
934 io_hdr.dxfer_len = mx_resp_len;
935 io_hdr.dxferp = resp;
936 io_hdr.cmdp = inqCmdBlk;
937 io_hdr.sbp = sense_b;
938 io_hdr.timeout = DEF_TIMEOUT * 1000;
940 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
943 /* treat SG_ERR here to get rid of sg_err.[ch] */
944 io_hdr.status &= 0x7e;
945 if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
946 (0 == io_hdr.driver_status))
948 if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
949 (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
950 (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
951 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
953 unsigned char * sense_buffer = io_hdr.sbp;
954 if (sense_buffer[0] & 0x2)
955 sense_key = sense_buffer[1] & 0xf;
957 sense_key = sense_buffer[2] & 0xf;
958 if(RECOVERED_ERROR == sense_key)
966 get_serial (char * str, int maxlen, int fd)
969 char buff[MX_ALLOC_LEN + 1] = {0};
974 if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN)) {
979 memcpy(str, buff + 4, len);
988 * Side effect: sets pp->tpgs if it could be determined.
989 * If ALUA calls fail because paths are unreachable, pp->tpgs remains unchanged.
992 detect_alua(struct path * pp)
996 unsigned int timeout;
999 if (pp->bus != SYSFS_BUS_SCSI) {
1000 pp->tpgs = TPGS_NONE;
1004 if (sysfs_get_timeout(pp, &timeout) <= 0)
1005 timeout = DEF_TIMEOUT;
1007 tpgs = get_target_port_group_support(pp, timeout);
1008 if (tpgs == -RTPG_INQUIRY_FAILED)
1010 else if (tpgs <= 0) {
1011 pp->tpgs = TPGS_NONE;
1015 if (pp->fd == -1 || pp->offline)
1018 ret = get_target_port_group(pp, timeout);
1019 if (ret < 0 || get_asymmetric_access_state(pp, ret, timeout) < 0) {
1022 if (ret == -RTPG_INQUIRY_FAILED)
1025 state = path_offline(pp);
1026 if (state == PATH_DOWN || state == PATH_PENDING)
1029 pp->tpgs = TPGS_NONE;
1035 int path_get_tpgs(struct path *pp)
1037 if (pp->tpgs == TPGS_UNDEF)
1042 #define DEFAULT_SGIO_LEN 254
1044 /* Query VPD page @pg. Returns number of INQUIRY bytes
1045 upon success and -1 upon failure. */
1047 sgio_get_vpd (unsigned char * buff, int maxlen, int fd, int pg)
1049 int len = DEFAULT_SGIO_LEN;
1057 if (0 == do_inq(fd, 0, 1, pg, buff, len)) {
1058 rlen = get_unaligned_be16(&buff[2]) + 4;
1059 if (rlen <= len || len >= maxlen)
1061 len = (rlen < maxlen)? rlen : maxlen;
1068 get_geometry(struct path *pp)
1073 if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
1074 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
1075 memset(&pp->geom, 0, sizeof(pp->geom));
1078 condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
1079 pp->dev, pp->geom.cylinders, pp->geom.heads,
1080 pp->geom.sectors, pp->geom.start);
1085 parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
1087 size_t len = get_unaligned_be16(&in[2]);
1092 if (len > WWID_SIZE)
1095 * Strip leading and trailing whitespace
1097 while (len > 0 && in[len + 3] == ' ')
1099 while (len > 0 && in[4] == ' ') {
1104 if (len >= out_len) {
1105 condlog(2, "vpd pg80 overflow, %zu/%zu bytes required",
1110 memcpy(out, in + 4, len);
1117 parse_vpd_pg83(const unsigned char *in, size_t in_len,
1118 char *out, size_t out_len)
1120 const unsigned char *d;
1121 const unsigned char *vpd = NULL;
1122 size_t len, vpd_len, i;
1123 int vpd_type, prio = -1;
1125 STRBUF_ON_STACK(buf);
1127 /* Need space at least for one digit */
1132 while (d <= in + in_len - 4) {
1133 bool invalid = false;
1136 /* Select 'association: LUN' */
1137 if ((d[1] & 0x30) == 0x30) {
1139 goto next_designator;
1140 } else if ((d[1] & 0x30) != 0x00)
1141 goto next_designator;
1143 switch (d[1] & 0xf) {
1144 unsigned char good_len;
1147 switch (d[4] >> 4) {
1149 /* IEEE Registered Extended: Prio 8 */
1154 /* IEEE Registered: Prio 7 */
1159 /* IEEE Extended: Prio 6 */
1164 /* IEEE Locally assigned: Prio 1 */
1169 /* Default: no priority */
1174 invalid = good_len == 0xff || good_len != d[3];
1177 /* EUI-64: Prio 4 */
1178 invalid = (d[3] != 8 && d[3] != 12 && d[3] != 16);
1182 /* SCSI Name: Prio 3 */
1183 invalid = (d[3] < 4 || (memcmp(d + 4, "eui.", 4) &&
1184 memcmp(d + 4, "naa.", 4) &&
1185 memcmp(d + 4, "iqn.", 4)));
1189 /* T-10 Vendor ID: Prio 2 */
1190 invalid = (d[3] < 8);
1194 condlog(2, "%s: UUID identifiers not yet supported",
1203 if (d + d[3] + 4 - in > (ssize_t)in_len) {
1204 condlog(2, "%s: device descriptor length overflow: %zd > %zu",
1205 __func__, d + d[3] + 4 - in, in_len);
1208 } else if (invalid) {
1209 condlog(2, "%s: invalid device designator at offset %zd: %02x%02x%02x%02x",
1210 __func__, d - in, d[0], d[1], d[2], d[3]);
1212 * We checked above that the next offset is within limits.
1213 * Proceed, fingers crossed.
1216 } else if (new_prio > prio) {
1226 if (d != in + in_len)
1227 /* Should this be fatal? (overflow covered above) */
1228 condlog(2, "%s: warning: last descriptor end %zd != VPD length %zu",
1229 __func__, d - in, in_len);
1232 vpd_type = vpd[1] & 0xf;
1235 /* untaint vpd_len for coverity */
1236 if (vpd_len > WWID_SIZE) {
1237 condlog(1, "%s: suspicious designator length %zu truncated to %u",
1238 __func__, vpd_len, WWID_SIZE);
1239 vpd_len = WWID_SIZE;
1241 if (vpd_type == 0x2 || vpd_type == 0x3) {
1244 if ((err = print_strbuf(&buf, "%d", vpd_type)) < 0)
1246 for (i = 0; i < vpd_len; i++)
1247 if ((err = print_strbuf(&buf, "%02x", vpd[i])) < 0)
1249 } else if (vpd_type == 0x8) {
1252 if (!memcmp("eui.", vpd, 4))
1254 else if (!memcmp("naa.", vpd, 4))
1258 if ((err = fill_strbuf(&buf, type, 1)) < 0)
1263 if ((err = __append_strbuf_str(&buf, (const char *)vpd, len)) < 0)
1266 /* The input is 0-padded, make sure the length is correct */
1267 truncate_strbuf(&buf, strlen(get_strbuf_str(&buf)));
1268 len = get_strbuf_len(&buf);
1270 char *buffer = __get_strbuf_buf(&buf);
1272 for (i = 0; i < len; ++i)
1273 buffer[i] = tolower(buffer[i]);
1276 } else if (vpd_type == 0x1) {
1277 const unsigned char *p;
1280 if ((err = fill_strbuf(&buf, '1', 1)) < 0)
1282 while (vpd && (p = memchr(vpd, ' ', vpd_len))) {
1284 if ((err = __append_strbuf_str(&buf, (const char *)vpd,
1289 while (vpd && vpd_len > 0 && *vpd == ' ') {
1293 if (vpd_len > 0 && (err = fill_strbuf(&buf, '_', 1)) < 0)
1297 if ((err = __append_strbuf_str(&buf, (const char *)vpd,
1303 len = get_strbuf_len(&buf);
1304 if (len >= out_len) {
1305 condlog(1, "%s: WWID overflow, type %d, %zu/%zu bytes required",
1306 __func__, vpd_type, len, out_len);
1307 if (vpd_type == 2 || vpd_type == 3)
1308 /* designator must have an even number of characters */
1309 len = 2 * (out_len / 2) - 1;
1313 strlcpy(out, get_strbuf_str(&buf), len + 1);
1318 parse_vpd_c0_hp3par(const unsigned char *in, size_t in_len,
1319 char *out, size_t out_len)
1323 memset(out, 0x0, out_len);
1324 if (in_len <= 4 || (in[4] > 3 && in_len < 44)) {
1325 condlog(3, "HP/3PAR vendor specific VPD page length too short: %zu", in_len);
1328 if (in[4] <= 3) /* revision must be > 3 to have Vomlume Name */
1330 len = get_unaligned_be32(&in[40]);
1331 if (len > out_len || len + 44 > in_len) {
1332 condlog(3, "HP/3PAR vendor specific Volume name too long: %zu",
1336 memcpy(out, &in[44], len);
1337 out[out_len - 1] = '\0';
1342 get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
1346 unsigned char buff[VPD_BUFLEN];
1348 memset(buff, 0x0, VPD_BUFLEN);
1349 if (!parent || sysfs_get_vpd(parent, pg, buff, VPD_BUFLEN) <= 0) {
1350 condlog(3, "failed to read sysfs vpd pg%02x", pg);
1354 if (buff[1] != pg) {
1355 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1359 buff_len = get_unaligned_be16(&buff[2]) + 4;
1360 if (buff_len > VPD_BUFLEN) {
1361 condlog(3, "vpd pg%02x page truncated", pg);
1362 buff_len = VPD_BUFLEN;
1366 len = parse_vpd_pg80(buff, str, maxlen);
1367 else if (pg == 0x83)
1368 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1376 fetch_vpd_page(int fd, int pg, unsigned char *buff, int maxlen)
1380 memset(buff, 0x0, maxlen);
1381 if (sgio_get_vpd(buff, maxlen, fd, pg) < 0) {
1382 int lvl = pg == 0x80 || pg == 0x83 ? 3 : 4;
1384 condlog(lvl, "failed to issue vpd inquiry for pg%02x",
1389 if (buff[1] != pg) {
1390 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1394 buff_len = get_unaligned_be16(&buff[2]) + 4;
1395 if (buff_len > maxlen) {
1396 condlog(3, "vpd pg%02x page truncated", pg);
1402 /* based on sg_inq.c from sg3_utils */
1404 is_vpd_page_supported(int fd, int pg)
1407 unsigned char buff[VPD_BUFLEN];
1409 len = fetch_vpd_page(fd, 0x00, buff, sizeof(buff));
1413 for (i = 4; i < len; ++i)
1420 get_vpd_sgio (int fd, int pg, int vend_id, char * str, int maxlen)
1423 unsigned char buff[VPD_BUFLEN];
1425 buff_len = fetch_vpd_page(fd, pg, buff, sizeof(buff));
1429 len = parse_vpd_pg80(buff, str, maxlen);
1430 else if (pg == 0x83)
1431 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1432 else if (pg == 0xc9 && maxlen >= 8) {
1436 len = (buff_len <= maxlen)? buff_len : maxlen;
1437 memcpy (str, buff, len);
1439 } else if (pg == 0xc0 && vend_id == VPD_VP_HP3PAR)
1440 len = parse_vpd_c0_hp3par(buff, buff_len, str, maxlen);
1448 scsi_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1450 struct udev_device *parent;
1451 const char *attr_path = NULL;
1455 const char *subsys = udev_device_get_subsystem(parent);
1456 if (subsys && !strncmp(subsys, "scsi", 4)) {
1457 attr_path = udev_device_get_sysname(parent);
1460 if (sscanf(attr_path, "%i:%i:%i:%" SCNu64,
1464 &pp->sg_id.lun) == 4)
1467 parent = udev_device_get_parent(parent);
1469 if (!attr_path || pp->sg_id.host_no == -1)
1470 return PATHINFO_FAILED;
1472 if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1473 return PATHINFO_FAILED;;
1475 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1477 if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1478 return PATHINFO_FAILED;;
1480 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1482 if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) < 0)
1483 return PATHINFO_FAILED;;
1485 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1488 * set the hwe configlet pointer
1490 find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1493 * host / bus / target / lun
1495 condlog(3, "%s: h:b:t:l = %i:%i:%i:%" PRIu64,
1505 if(sysfs_get_tgt_nodename(pp, pp->tgt_node_name))
1506 return PATHINFO_FAILED;
1508 condlog(3, "%s: tgt_node_name = %s",
1509 pp->dev, pp->tgt_node_name);
1515 nvme_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1517 struct udev_device *parent;
1518 const char *attr_path = NULL;
1522 attr_path = udev_device_get_sysname(pp->udev);
1524 return PATHINFO_FAILED;
1526 if (sscanf(attr_path, "nvme%dn%d",
1528 &pp->sg_id.scsi_id) != 2)
1529 return PATHINFO_FAILED;
1531 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev,
1534 return PATHINFO_SKIPPED;
1536 attr = udev_device_get_sysattr_value(pp->udev, "nsid");
1537 pp->sg_id.lun = attr ? atoi(attr) : 0;
1539 attr = udev_device_get_sysattr_value(parent, "cntlid");
1540 pp->sg_id.channel = attr ? atoi(attr) : 0;
1542 snprintf(pp->vendor_id, SCSI_VENDOR_SIZE, "NVME");
1543 snprintf(pp->product_id, PATH_PRODUCT_SIZE, "%s",
1544 udev_device_get_sysattr_value(parent, "model"));
1545 snprintf(pp->serial, SERIAL_SIZE, "%s",
1546 udev_device_get_sysattr_value(parent, "serial"));
1547 snprintf(pp->rev, PATH_REV_SIZE, "%s",
1548 udev_device_get_sysattr_value(parent, "firmware_rev"));
1550 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1551 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1552 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1553 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1555 find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1561 ccw_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1563 struct udev_device *parent;
1564 char attr_buff[NAME_SIZE];
1565 const char *attr_path;
1569 const char *subsys = udev_device_get_subsystem(parent);
1570 if (subsys && !strncmp(subsys, "ccw", 3))
1572 parent = udev_device_get_parent(parent);
1575 return PATHINFO_FAILED;
1577 sprintf(pp->vendor_id, "IBM");
1579 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1581 if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE) <= 0)
1582 return PATHINFO_FAILED;
1584 if (!strncmp(attr_buff, "3370", 4)) {
1585 sprintf(pp->product_id,"S/390 DASD FBA");
1586 } else if (!strncmp(attr_buff, "9336", 4)) {
1587 sprintf(pp->product_id,"S/390 DASD FBA");
1589 sprintf(pp->product_id,"S/390 DASD ECKD");
1592 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1595 * set the hwe configlet pointer
1597 find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1600 * host / bus / target / lun
1602 attr_path = udev_device_get_sysname(parent);
1604 return PATHINFO_FAILED;
1606 if (sscanf(attr_path, "%i.%i.%x",
1609 &pp->sg_id.scsi_id) == 3) {
1610 condlog(3, "%s: h:b:t:l = %i:%i:%i:%" PRIu64,
1622 cciss_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1624 const char * attr_path = NULL;
1625 struct udev_device *parent;
1629 const char *subsys = udev_device_get_subsystem(parent);
1630 if (subsys && !strncmp(subsys, "cciss", 5)) {
1631 attr_path = udev_device_get_sysname(parent);
1634 if (sscanf(attr_path, "c%id%i",
1636 &pp->sg_id.scsi_id) == 2)
1639 parent = udev_device_get_parent(parent);
1641 if (!attr_path || pp->sg_id.host_no == -1)
1642 return PATHINFO_FAILED;
1644 if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1645 return PATHINFO_FAILED;
1647 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1649 if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1650 return PATHINFO_FAILED;
1652 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1654 if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) <= 0)
1655 return PATHINFO_FAILED;
1657 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1660 * set the hwe configlet pointer
1662 find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1665 * host / bus / target / lun
1668 pp->sg_id.channel = 0;
1669 condlog(3, "%s: h:b:t:l = %i:%i:%i:%" PRIu64,
1680 common_sysfs_pathinfo (struct path * pp)
1685 return PATHINFO_FAILED;
1688 condlog(4, "%s: udev not initialised", pp->dev);
1689 return PATHINFO_FAILED;
1691 devt = udev_device_get_devnum(pp->udev);
1692 if (major(devt) == 0 && minor(devt) == 0)
1693 return PATHINFO_FAILED;
1695 snprintf(pp->dev_t, BLK_DEV_SIZE, "%d:%d", major(devt), minor(devt));
1697 condlog(4, "%s: dev_t = %s", pp->dev, pp->dev_t);
1699 if (sysfs_get_size(pp, &pp->size))
1700 return PATHINFO_FAILED;
1702 condlog(3, "%s: size = %llu", pp->dev, pp->size);
1708 path_offline (struct path * pp)
1710 struct udev_device * parent;
1711 char buff[SCSI_STATE_SIZE];
1713 const char *subsys_type;
1715 if (pp->bus == SYSFS_BUS_SCSI) {
1716 subsys_type = "scsi";
1718 else if (pp->bus == SYSFS_BUS_NVME) {
1719 subsys_type = "nvme";
1727 const char *subsys = udev_device_get_subsystem(parent);
1728 if (subsys && !strncmp(subsys, subsys_type, 4))
1730 parent = udev_device_get_parent(parent);
1734 condlog(1, "%s: failed to get sysfs information", pp->dev);
1735 return PATH_REMOVED;
1738 memset(buff, 0x0, SCSI_STATE_SIZE);
1739 err = sysfs_attr_get_value(parent, "state", buff, SCSI_STATE_SIZE);
1742 return PATH_REMOVED;
1748 condlog(4, "%s: path state = %s", pp->dev, buff);
1750 if (pp->bus == SYSFS_BUS_SCSI) {
1751 if (!strncmp(buff, "offline", 7)) {
1756 if (!strncmp(buff, "blocked", 7) ||
1757 !strncmp(buff, "quiesce", 7))
1758 return PATH_PENDING;
1759 else if (!strncmp(buff, "running", 7))
1763 else if (pp->bus == SYSFS_BUS_NVME) {
1764 if (!strncmp(buff, "dead", 4)) {
1769 if (!strncmp(buff, "new", 3) ||
1770 !strncmp(buff, "deleting", 8))
1771 return PATH_PENDING;
1772 else if (!strncmp(buff, "live", 4))
1780 sysfs_pathinfo(struct path *pp, const struct _vector *hwtable)
1782 int r = common_sysfs_pathinfo(pp);
1784 if (r != PATHINFO_OK)
1787 pp->bus = SYSFS_BUS_UNDEF;
1788 if (!strncmp(pp->dev,"cciss",5))
1789 pp->bus = SYSFS_BUS_CCISS;
1790 if (!strncmp(pp->dev,"dasd", 4))
1791 pp->bus = SYSFS_BUS_CCW;
1792 if (!strncmp(pp->dev,"sd", 2))
1793 pp->bus = SYSFS_BUS_SCSI;
1794 if (!strncmp(pp->dev,"nvme", 4))
1795 pp->bus = SYSFS_BUS_NVME;
1798 case SYSFS_BUS_SCSI:
1799 return scsi_sysfs_pathinfo(pp, hwtable);
1801 return ccw_sysfs_pathinfo(pp, hwtable);
1802 case SYSFS_BUS_CCISS:
1803 return cciss_sysfs_pathinfo(pp, hwtable);
1804 case SYSFS_BUS_NVME:
1805 return nvme_sysfs_pathinfo(pp, hwtable);
1806 case SYSFS_BUS_UNDEF:
1813 scsi_ioctl_pathinfo (struct path * pp, int mask)
1815 struct udev_device *parent;
1816 const char *attr_path = NULL;
1819 if (!(mask & DI_SERIAL))
1822 select_vpd_vendor_id(pp);
1823 vpd_id = pp->vpd_vendor_id;
1825 if (vpd_id != VPD_VP_UNDEF) {
1826 char vpd_data[VPD_DATA_SIZE] = {0};
1828 if (get_vpd_sgio(pp->fd, vpd_vendor_pages[vpd_id].pg, vpd_id,
1829 vpd_data, sizeof(vpd_data)) < 0)
1830 condlog(3, "%s: failed to get extra vpd data", pp->dev);
1832 vpd_data[VPD_DATA_SIZE - 1] = '\0';
1835 pp->vpd_data = strdup(vpd_data);
1837 condlog(0, "%s: failed to allocate space for vpd data", pp->dev);
1843 const char *subsys = udev_device_get_subsystem(parent);
1844 if (subsys && !strncmp(subsys, "scsi", 4)) {
1845 attr_path = udev_device_get_sysname(parent);
1848 if (sscanf(attr_path, "%i:%i:%i:%" SCNu64,
1852 &pp->sg_id.lun) == 4)
1855 parent = udev_device_get_parent(parent);
1857 if (!attr_path || pp->sg_id.host_no == -1)
1860 if (get_vpd_sysfs(parent, 0x80, pp->serial, SERIAL_SIZE) <= 0) {
1861 if (get_serial(pp->serial, SERIAL_SIZE, pp->fd)) {
1862 condlog(3, "%s: fail to get serial", pp->dev);
1867 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1872 cciss_ioctl_pathinfo(struct path *pp)
1874 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1875 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1879 get_state (struct path * pp, struct config *conf, int daemon, int oldstate)
1881 struct checker * c = &pp->checker;
1884 if (!checker_selected(c)) {
1886 if (pathinfo(pp, conf, DI_SYSFS) != PATHINFO_OK) {
1887 condlog(3, "%s: couldn't get sysfs pathinfo",
1889 return PATH_UNCHECKED;
1892 select_detect_checker(conf, pp);
1893 select_checker(conf, pp);
1894 if (!checker_selected(c)) {
1895 condlog(3, "%s: No checker selected", pp->dev);
1896 return PATH_UNCHECKED;
1898 checker_set_fd(c, pp->fd);
1899 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
1901 condlog(3, "%s: checker init failed", pp->dev);
1902 return PATH_UNCHECKED;
1905 if (pp->mpp && !c->mpcontext)
1906 checker_mp_init(c, &pp->mpp->mpcontext);
1907 checker_clear_message(c);
1908 if (conf->force_sync == 0)
1909 checker_set_async(c);
1911 checker_set_sync(c);
1912 if (!conf->checker_timeout &&
1913 sysfs_get_timeout(pp, &(c->timeout)) <= 0)
1914 c->timeout = DEF_TIMEOUT;
1915 state = checker_check(c, oldstate);
1916 condlog(3, "%s: %s state = %s", pp->dev,
1917 checker_name(c), checker_state_name(state));
1918 if (state != PATH_UP && state != PATH_GHOST &&
1919 strlen(checker_message(c)))
1920 condlog(3, "%s: %s checker%s",
1921 pp->dev, checker_name(c), checker_message(c));
1926 get_prio (struct path * pp, int timeout)
1929 struct config *conf;
1936 if (!prio_selected(p)) {
1937 conf = get_multipath_config();
1938 pthread_cleanup_push(put_multipath_config, conf);
1939 select_detect_prio(conf, pp);
1940 select_prio(conf, pp);
1941 pthread_cleanup_pop(1);
1942 if (!prio_selected(p)) {
1943 condlog(3, "%s: no prio selected", pp->dev);
1944 pp->priority = PRIO_UNDEF;
1948 old_prio = pp->priority;
1949 pp->priority = prio_getprio(p, pp, timeout);
1950 if (pp->priority < 0) {
1951 /* this changes pp->offline, but why not */
1952 int state = path_offline(pp);
1954 if (state == PATH_DOWN || state == PATH_PENDING) {
1955 pp->priority = old_prio;
1956 condlog(3, "%s: %s prio error in state %d, keeping prio = %d",
1957 pp->dev, prio_name(p), state, pp->priority);
1959 condlog(3, "%s: %s prio error in state %d",
1960 pp->dev, prio_name(p), state);
1961 pp->priority = PRIO_UNDEF;
1965 condlog((old_prio == pp->priority ? 4 : 3), "%s: %s prio = %u",
1966 pp->dev, prio_name(p), pp->priority);
1971 * Mangle string of length *len starting at start
1972 * by removing character sequence "00" (hex for a 0 byte),
1973 * starting at end, backwards.
1974 * Changes the value of *len if characters were removed.
1975 * Returns a pointer to the position where "end" was moved to.
1978 *skip_zeroes_backward(char* start, size_t *len, char *end)
1982 while (p >= start + 2 && *(p - 1) == '0' && *(p - 2) == '0')
1988 memmove(p, end, start + *len + 1 - end);
1995 * Fix for NVME wwids looking like this:
1996 * nvme.0000-3163653363666438366239656630386200-4c696e75780000000000000000000000000000000000000000000000000000000000000000000000-00000002
1997 * which are encountered in some combinations of Linux NVME host and target.
1998 * The '00' are hex-encoded 0-bytes which are forbidden in the serial (SN)
1999 * and model (MN) fields. Discard them.
2000 * If a WWID of the above type is found, sets pp->wwid and returns a value > 0.
2001 * Otherwise, returns 0.
2004 fix_broken_nvme_wwid(struct path *pp, const char *value, size_t size)
2006 static const char _nvme[] = "nvme.";
2011 len = strlen(value);
2012 if (len >= sizeof(mangled))
2015 /* Check that value starts with "nvme.%04x-" */
2016 if (memcmp(value, _nvme, sizeof(_nvme) - 1) || value[9] != '-')
2018 for (i = 5; i < 9; i++)
2019 if (!isxdigit(value[i]))
2022 memcpy(mangled, value, len + 1);
2024 /* search end of "model" part and strip trailing '00' */
2025 p = memrchr(mangled, '-', len);
2029 p = skip_zeroes_backward(mangled, &len, p);
2031 /* search end of "serial" part */
2032 p = memrchr(mangled, '-', p - mangled);
2033 if (p == NULL || memrchr(mangled, '-', p - mangled) != mangled + 9)
2034 /* We expect exactly 3 '-' in the value */
2037 p = skip_zeroes_backward(mangled, &len, p);
2041 memcpy(pp->wwid, mangled, len + 1);
2042 condlog(2, "%s: over-long WWID shortened to %s", pp->dev, pp->wwid);
2047 get_udev_uid(struct path * pp, char *uid_attribute, struct udev_device *udev)
2052 value = udev_device_get_property_value(udev, uid_attribute);
2053 if (!value || strlen(value) == 0)
2054 value = getenv(uid_attribute);
2055 if (value && strlen(value)) {
2056 len = strlcpy(pp->wwid, value, WWID_SIZE);
2057 if (len >= WWID_SIZE) {
2058 len = fix_broken_nvme_wwid(pp, value, WWID_SIZE);
2061 condlog(0, "%s: wwid overflow", pp->dev);
2065 condlog(3, "%s: no %s attribute", pp->dev,
2073 get_vpd_uid(struct path * pp)
2075 struct udev_device *parent = pp->udev;
2078 const char *subsys = udev_device_get_subsystem(parent);
2079 if (subsys && !strncmp(subsys, "scsi", 4))
2081 parent = udev_device_get_parent(parent);
2087 return get_vpd_sysfs(parent, 0x83, pp->wwid, WWID_SIZE);
2090 /* based on code from s390-tools/dasdinfo/dasdinfo.c */
2091 static ssize_t dasd_get_uid(struct path *pp)
2093 struct udev_device *parent;
2098 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "ccw",
2103 if (sysfs_attr_get_value(parent, "uid", value, 80) < 0)
2107 /* look for the 4th '.' and cut there */
2108 for (i = 0; i < 4; i++) {
2109 p = index(p + 1, '.');
2116 return strlcpy(pp->wwid, value, WWID_SIZE);
2119 static ssize_t uid_fallback(struct path *pp, int path_state,
2120 const char **origin)
2124 if (pp->bus == SYSFS_BUS_CCW) {
2125 len = dasd_get_uid(pp);
2127 } else if (pp->bus == SYSFS_BUS_SCSI) {
2128 len = get_vpd_uid(pp);
2130 if (len < 0 && path_state == PATH_UP) {
2131 condlog(1, "%s: failed to get sysfs uid: %s",
2132 pp->dev, strerror(-len));
2133 len = get_vpd_sgio(pp->fd, 0x83, 0, pp->wwid,
2137 } else if (pp->bus == SYSFS_BUS_NVME) {
2142 len = sysfs_attr_get_value(pp->udev, "wwid", value,
2146 len = strlcpy(pp->wwid, value, WWID_SIZE);
2147 if (len >= WWID_SIZE) {
2148 len = fix_broken_nvme_wwid(pp, value,
2152 condlog(0, "%s: wwid overflow", pp->dev);
2160 bool has_uid_fallback(struct path *pp)
2163 * Falling back to direct WWID determination is dangerous
2164 * if uid_attribute is set to something non-standard.
2165 * Allow it only if it's either the default, or if udev
2166 * has been disabled by setting 'uid_attribute ""'.
2168 if (!pp->uid_attribute)
2170 return ((pp->bus == SYSFS_BUS_SCSI &&
2171 (!strcmp(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE) ||
2172 !strcmp(pp->uid_attribute, ""))) ||
2173 (pp->bus == SYSFS_BUS_NVME &&
2174 (!strcmp(pp->uid_attribute, DEFAULT_NVME_UID_ATTRIBUTE) ||
2175 !strcmp(pp->uid_attribute, ""))) ||
2176 (pp->bus == SYSFS_BUS_CCW &&
2177 (!strcmp(pp->uid_attribute, DEFAULT_DASD_UID_ATTRIBUTE) ||
2178 !strcmp(pp->uid_attribute, ""))));
2182 get_uid (struct path * pp, int path_state, struct udev_device *udev,
2185 const char *origin = "unknown";
2187 struct config *conf;
2188 int used_fallback = 0;
2191 if (!pp->uid_attribute && !pp->getuid) {
2192 conf = get_multipath_config();
2193 pthread_cleanup_push(put_multipath_config, conf);
2194 select_getuid(conf, pp);
2195 select_recheck_wwid(conf, pp);
2196 pthread_cleanup_pop(1);
2199 memset(pp->wwid, 0, WWID_SIZE);
2201 char buff[CALLOUT_MAX_SIZE];
2203 /* Use 'getuid' callout, deprecated */
2204 condlog(1, "%s: using deprecated getuid callout", pp->dev);
2205 if (path_state != PATH_UP) {
2206 condlog(3, "%s: path inaccessible", pp->dev);
2208 } else if (apply_format(pp->getuid, &buff[0], pp)) {
2209 condlog(0, "error formatting uid callout command");
2211 } else if (execute_program(buff, pp->wwid, WWID_SIZE)) {
2212 condlog(3, "error calling out %s", buff);
2215 len = strlen(pp->wwid);
2217 } else if (pp->uid_attribute) {
2218 /* if the uid_attribute is an empty string skip udev checking */
2219 bool check_uid_attr = udev && *pp->uid_attribute;
2221 if (check_uid_attr) {
2222 len = get_udev_uid(pp, pp->uid_attribute, udev);
2225 condlog(1, "%s: empty udev uid", pp->dev);
2227 if ((!check_uid_attr || (len <= 0 && allow_fallback))
2228 && has_uid_fallback(pp)) {
2229 /* if udev wasn't set or we failed in get_udev_uid()
2230 * log at a higher priority */
2231 if (!udev || check_uid_attr)
2233 len = uid_fallback(pp, path_state, &origin);
2237 condlog(1, "%s: failed to get %s uid: %s",
2238 pp->dev, origin, strerror(-len));
2239 memset(pp->wwid, 0x0, WWID_SIZE);
2242 /* Strip any trailing blanks */
2243 for (i = strlen(pp->wwid); i > 0 && pp->wwid[i-1] == ' '; i--);
2247 condlog((used_fallback)? 1 : 3, "%s: uid = %s (%s)", pp->dev,
2248 *pp->wwid == '\0' ? "<empty>" : pp->wwid, origin);
2252 int pathinfo(struct path *pp, struct config *conf, int mask)
2257 return PATHINFO_FAILED;
2259 /* Treat removed paths as if they didn't exist */
2260 if (pp->initialized == INIT_REMOVED)
2261 return PATHINFO_FAILED;
2264 * For behavior backward-compatibility with multipathd,
2265 * the blacklisting by filter_property|devnode() is not
2266 * limited by DI_BLACKLIST and occurs before this debug
2267 * message with the mask value.
2270 const char *hidden =
2271 udev_device_get_sysattr_value(pp->udev, "hidden");
2273 if (hidden && !strcmp(hidden, "1")) {
2274 condlog(4, "%s: hidden", pp->dev);
2275 return PATHINFO_SKIPPED;
2278 if (is_claimed_by_foreign(pp->udev))
2279 return PATHINFO_SKIPPED;
2282 * uid_attribute is required for filter_property below,
2283 * and needs access to pp->hwe.
2285 if (!(mask & DI_SYSFS) && (mask & DI_BLACKLIST) &&
2286 !pp->uid_attribute && VECTOR_SIZE(pp->hwe) == 0)
2290 if (strlen(pp->dev) != 0 && filter_devnode(conf->blist_devnode,
2291 conf->elist_devnode,
2293 return PATHINFO_SKIPPED;
2295 condlog(4, "%s: mask = 0x%x", pp->dev, mask);
2298 * Sanity check: we need the device number to
2299 * avoid inconsistent information in
2300 * find_path_by_dev()/find_path_by_devt()
2302 if (!strlen(pp->dev_t) && !(mask & DI_SYSFS)) {
2303 condlog(1, "%s: empty device number", pp->dev);
2308 * fetch info available in sysfs
2310 if (mask & DI_SYSFS) {
2311 int rc = sysfs_pathinfo(pp, conf->hwtable);
2313 if (rc != PATHINFO_OK)
2316 if (pp->bus == SYSFS_BUS_SCSI &&
2317 pp->sg_id.proto_id == SCSI_PROTOCOL_USB &&
2318 !conf->allow_usb_devices) {
2319 condlog(3, "%s: skip USB device %s", pp->dev,
2321 return PATHINFO_SKIPPED;
2325 if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
2326 /* uid_attribute is required for filter_property() */
2327 if (pp->udev && !pp->uid_attribute) {
2328 select_getuid(conf, pp);
2329 select_recheck_wwid(conf, pp);
2332 if (filter_property(conf, pp->udev, 4, pp->uid_attribute) > 0 ||
2333 filter_device(conf->blist_device, conf->elist_device,
2334 pp->vendor_id, pp->product_id, pp->dev) > 0 ||
2335 filter_protocol(conf->blist_protocol, conf->elist_protocol,
2337 return PATHINFO_SKIPPED;
2340 path_state = path_offline(pp);
2341 if (path_state == PATH_REMOVED)
2343 else if (mask & DI_NOIO) {
2344 if (mask & DI_CHECKER)
2346 * Avoid any IO on the device itself.
2347 * simply use the path_offline() return as its state
2349 if (path_state != PATH_PENDING ||
2350 pp->state == PATH_UNCHECKED ||
2351 pp->state == PATH_WILD)
2352 pp->chkrstate = pp->state = path_state;
2357 * fetch info not available through sysfs
2360 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
2363 condlog(4, "Couldn't open device node for %s: %s",
2364 pp->dev, strerror(errno));
2368 if (mask & DI_SERIAL)
2371 if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI)
2372 scsi_ioctl_pathinfo(pp, mask);
2374 if (pp->bus == SYSFS_BUS_CCISS && mask & DI_SERIAL)
2375 cciss_ioctl_pathinfo(pp);
2377 if (mask & DI_CHECKER) {
2378 if (path_state == PATH_UP) {
2379 int newstate = get_state(pp, conf, 0, path_state);
2380 if (newstate != PATH_PENDING ||
2381 pp->state == PATH_UNCHECKED ||
2382 pp->state == PATH_WILD)
2383 pp->chkrstate = pp->state = newstate;
2384 if (pp->state == PATH_TIMEOUT)
2385 pp->state = PATH_DOWN;
2386 if (pp->state == PATH_UP && !pp->size) {
2387 condlog(3, "%s: device size is 0, "
2388 "path unusable", pp->dev);
2389 pp->state = PATH_GHOST;
2392 condlog(3, "%s: path inaccessible", pp->dev);
2393 pp->chkrstate = pp->state = path_state;
2397 if ((mask & DI_WWID) && !strlen(pp->wwid)) {
2398 int allow_fallback = ((mask & DI_NOFALLBACK) == 0 &&
2399 pp->retriggers >= conf->retrigger_tries);
2400 get_uid(pp, path_state, pp->udev, allow_fallback);
2401 if (!strlen(pp->wwid)) {
2402 if (pp->bus == SYSFS_BUS_UNDEF)
2403 return PATHINFO_SKIPPED;
2404 if (pp->initialized != INIT_FAILED) {
2405 pp->initialized = INIT_MISSING_UDEV;
2406 pp->tick = conf->retrigger_delay;
2407 } else if (allow_fallback &&
2408 (pp->state == PATH_UP || pp->state == PATH_GHOST)) {
2410 * We have failed to read udev info for this path
2411 * repeatedly. We used the fallback in get_uid()
2412 * if there was any, and still got no WWID,
2413 * although the path is allegedly up.
2414 * It's likely that this path is not fit for
2417 STRBUF_ON_STACK(buf);
2419 snprint_path(&buf, "%T", pp, 0);
2420 condlog(1, "%s: no WWID in state \"%s\", giving up",
2421 pp->dev, get_strbuf_str(&buf));
2422 return PATHINFO_SKIPPED;
2430 if (mask & DI_BLACKLIST && mask & DI_WWID) {
2431 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
2432 pp->wwid, pp->dev) > 0) {
2433 return PATHINFO_SKIPPED;
2438 * Retrieve path priority, even for PATH_DOWN paths if it has never
2439 * been successfully obtained before. If path is down don't try
2442 if ((mask & DI_PRIO) && path_state == PATH_UP && strlen(pp->wwid)) {
2443 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
2444 get_prio(pp, (pp->state != PATH_DOWN)?
2445 (conf->checker_timeout * 1000) : 10);
2449 if ((mask & DI_ALL) == DI_ALL)
2450 pp->initialized = INIT_OK;
2455 * Recoverable error, for example faulty or offline path
2457 pp->chkrstate = pp->state = PATH_DOWN;
2458 if (pp->initialized == INIT_NEW || pp->initialized == INIT_FAILED)
2459 memset(pp->wwid, 0, WWID_SIZE);