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>
24 #include "blacklist.h"
28 #include "sg_include.h"
30 #include "discovery.h"
33 #include "unaligned.h"
34 #include "prioritizers/alua_rtpg.h"
37 struct vpd_vendor_page vpd_vendor_pages[VPD_VP_ARRAY_SIZE] = {
38 [VPD_VP_UNDEF] = { 0x00, "undef" },
39 [VPD_VP_HP3PAR] = { 0xc0, "hp3par" },
43 alloc_path_with_pathinfo (struct config *conf, struct udev_device *udevice,
44 const char *wwid, int flag, struct path **pp_ptr)
46 int err = PATHINFO_FAILED;
53 devname = udev_device_get_sysname(udevice);
55 return PATHINFO_FAILED;
60 return PATHINFO_FAILED;
63 strlcpy(pp->wwid, wwid, sizeof(pp->wwid));
65 if (safe_sprintf(pp->dev, "%s", devname)) {
66 condlog(0, "pp->dev too small");
68 pp->udev = udev_device_ref(udevice);
69 err = pathinfo(pp, conf, flag | DI_BLACKLIST);
80 store_pathinfo (vector pathvec, struct config *conf,
81 struct udev_device *udevice, int flag, struct path **pp_ptr)
83 int err = PATHINFO_FAILED;
90 devname = udev_device_get_sysname(udevice);
92 return PATHINFO_FAILED;
97 return PATHINFO_FAILED;
99 if(safe_sprintf(pp->dev, "%s", devname)) {
100 condlog(0, "pp->dev too small");
103 pp->udev = udev_device_ref(udevice);
104 err = pathinfo(pp, conf, flag);
108 err = store_path(pathvec, pp);
111 pp->checkint = conf->checkint;
122 path_discover (vector pathvec, struct config * conf,
123 struct udev_device *udevice, int flag)
126 const char * devname;
128 devname = udev_device_get_sysname(udevice);
130 return PATHINFO_FAILED;
132 pp = find_path_by_dev(pathvec, devname);
134 char devt[BLK_DEV_SIZE];
135 dev_t devnum = udev_device_get_devnum(udevice);
137 snprintf(devt, BLK_DEV_SIZE, "%d:%d",
138 major(devnum), minor(devnum));
139 pp = find_path_by_devt(pathvec, devt);
141 return store_pathinfo(pathvec, conf,
142 udevice, flag | DI_BLACKLIST,
145 return pathinfo(pp, conf, flag);
148 static void cleanup_udev_enumerate_ptr(void *arg)
150 struct udev_enumerate *ue;
154 ue = *((struct udev_enumerate**) arg);
156 (void)udev_enumerate_unref(ue);
159 static void cleanup_udev_device_ptr(void *arg)
161 struct udev_device *ud;
165 ud = *((struct udev_device**) arg);
167 (void)udev_device_unref(ud);
171 path_discovery (vector pathvec, int flag)
173 struct udev_enumerate *udev_iter = NULL;
174 struct udev_list_entry *entry;
175 struct udev_device *udevice = NULL;
177 int num_paths = 0, total_paths = 0, ret;
179 pthread_cleanup_push(cleanup_udev_enumerate_ptr, &udev_iter);
180 pthread_cleanup_push(cleanup_udev_device_ptr, &udevice);
181 conf = get_multipath_config();
182 pthread_cleanup_push(put_multipath_config, conf);
184 udev_iter = udev_enumerate_new(udev);
190 if (udev_enumerate_add_match_subsystem(udev_iter, "block") < 0 ||
191 udev_enumerate_add_match_is_initialized(udev_iter) < 0 ||
192 udev_enumerate_scan_devices(udev_iter) < 0) {
193 condlog(1, "%s: error setting up udev_enumerate: %m", __func__);
198 udev_list_entry_foreach(entry,
199 udev_enumerate_get_list_entry(udev_iter)) {
203 devpath = udev_list_entry_get_name(entry);
204 condlog(4, "Discover device %s", devpath);
205 udevice = udev_device_new_from_syspath(udev, devpath);
207 condlog(4, "%s: no udev information", devpath);
210 devtype = udev_device_get_devtype(udevice);
211 if(devtype && !strncmp(devtype, "disk", 4)) {
213 if (path_discover(pathvec, conf,
214 udevice, flag) == PATHINFO_OK)
217 udevice = udev_device_unref(udevice);
219 ret = total_paths - num_paths;
220 condlog(4, "Discovered %d/%d paths", num_paths, total_paths);
222 pthread_cleanup_pop(1);
223 pthread_cleanup_pop(1);
224 pthread_cleanup_pop(1);
228 #define declare_sysfs_get_str(fname) \
230 sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len) \
234 const char * devname; \
239 devname = udev_device_get_sysname(udev); \
241 attr = udev_device_get_sysattr_value(udev, #fname); \
243 condlog(3, "%s: attribute %s not found in sysfs", \
247 for (l = strlen(attr); l >= 1 && isspace(attr[l-1]); l--); \
249 condlog(3, "%s: overflow in attribute %s", \
253 strlcpy(buff, attr, len); \
254 return strchop(buff); \
257 declare_sysfs_get_str(devtype);
258 declare_sysfs_get_str(vendor);
259 declare_sysfs_get_str(model);
260 declare_sysfs_get_str(rev);
263 sysfs_get_binary (struct udev_device * udev, const char *attrname,
264 unsigned char *buff, size_t len)
267 const char * devname;
270 condlog(3, "No udev device given\n");
274 devname = udev_device_get_sysname(udev);
275 attr_len = sysfs_bin_attr_get_value(udev, attrname, buff, len);
277 condlog(3, "%s: attribute %s not found in sysfs",
284 ssize_t sysfs_get_vpd(struct udev_device * udev, unsigned char pg,
285 unsigned char *buff, size_t len)
289 snprintf(attrname, sizeof(attrname), "vpd_pg%02x", pg);
290 return sysfs_get_binary(udev, attrname, buff, len);
293 ssize_t sysfs_get_inquiry(struct udev_device * udev,
294 unsigned char *buff, size_t len)
296 return sysfs_get_binary(udev, "inquiry", buff, len);
300 sysfs_get_timeout(const struct path *pp, unsigned int *timeout)
302 const char *attr = NULL;
304 struct udev_device *parent;
308 if (!pp->udev || pp->bus != SYSFS_BUS_SCSI)
313 subsys = udev_device_get_subsystem(parent);
314 attr = udev_device_get_sysattr_value(parent, "timeout");
317 parent = udev_device_get_parent(parent);
320 condlog(3, "%s: No timeout value in sysfs", pp->dev);
324 t = strtoul(attr, &eptr, 0);
325 if (attr == eptr || t == ULONG_MAX) {
326 condlog(3, "%s: Cannot parse timeout attribute '%s'",
331 condlog(3, "%s: Overflow in timeout value '%s'",
341 sysfs_get_tgt_nodename(struct path *pp, char *node)
343 const char *tgtname, *value;
344 struct udev_device *parent, *tgtdev;
345 int host, channel, tgtid = -1;
347 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_device");
351 value = udev_device_get_sysattr_value(parent, "sas_address");
353 tgtdev = udev_device_get_parent(parent);
355 tgtname = udev_device_get_sysname(tgtdev);
356 if (sscanf(tgtname, "end_device-%d:%d",
359 tgtdev = udev_device_get_parent(tgtdev);
363 pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
364 pp->sg_id.transport_id = tgtid;
365 strlcpy(node, value, NODE_NAME_SIZE);
371 tgtdev = udev_device_get_parent(parent);
373 value = udev_device_get_subsystem(tgtdev);
374 if (value && !strcmp(value, "usb")) {
375 pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
376 tgtname = udev_device_get_sysname(tgtdev);
377 strlcpy(node, tgtname, NODE_NAME_SIZE);
378 condlog(3, "%s: skip USB device %s", pp->dev, node);
381 tgtdev = udev_device_get_parent(tgtdev);
383 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_target");
386 /* Check for FibreChannel */
387 tgtdev = udev_device_get_parent(parent);
388 value = udev_device_get_sysname(tgtdev);
389 if (sscanf(value, "rport-%d:%d-%d",
390 &host, &channel, &tgtid) == 3) {
391 tgtdev = udev_device_new_from_subsystem_sysname(udev,
392 "fc_remote_ports", value);
394 condlog(3, "SCSI target %d:%d:%d -> "
396 pp->sg_id.host_no, pp->sg_id.channel,
397 pp->sg_id.scsi_id, host, channel,
399 value = udev_device_get_sysattr_value(tgtdev,
402 pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
403 pp->sg_id.transport_id = tgtid;
404 strlcpy(node, value, NODE_NAME_SIZE);
405 udev_device_unref(tgtdev);
408 udev_device_unref(tgtdev);
412 /* Check for iSCSI */
416 tgtname = udev_device_get_sysname(parent);
417 if (tgtname && sscanf(tgtname , "session%d", &tgtid) == 1)
419 parent = udev_device_get_parent(parent);
423 if (parent && tgtname) {
424 tgtdev = udev_device_new_from_subsystem_sysname(udev,
425 "iscsi_session", tgtname);
429 value = udev_device_get_sysattr_value(tgtdev, "targetname");
431 pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
432 pp->sg_id.transport_id = tgtid;
433 strlcpy(node, value, NODE_NAME_SIZE);
434 udev_device_unref(tgtdev);
438 udev_device_unref(tgtdev);
441 /* Check for libata */
445 tgtname = udev_device_get_sysname(parent);
446 if (tgtname && sscanf(tgtname, "ata%d", &tgtid) == 1)
448 parent = udev_device_get_parent(parent);
452 pp->sg_id.proto_id = SCSI_PROTOCOL_ATA;
453 pp->sg_id.transport_id = tgtid;
454 snprintf(node, NODE_NAME_SIZE, "ata-%d.00", tgtid);
457 /* Unknown SCSI transport. Keep fingers crossed */
458 pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
462 int sysfs_get_host_adapter_name(const struct path *pp, char *adapter_name)
466 if (!pp || !adapter_name)
469 proto_id = pp->sg_id.proto_id;
471 if (proto_id != SCSI_PROTOCOL_FCP &&
472 proto_id != SCSI_PROTOCOL_SAS &&
473 proto_id != SCSI_PROTOCOL_ISCSI &&
474 proto_id != SCSI_PROTOCOL_SRP) {
477 /* iscsi doesn't have adapter info in sysfs
478 * get ip_address for grouping paths
480 if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
481 return sysfs_get_iscsi_ip_address(pp, adapter_name);
483 /* fetch adapter pci name for other protocols
485 return sysfs_get_host_pci_name(pp, adapter_name);
488 int sysfs_get_host_pci_name(const struct path *pp, char *pci_name)
490 struct udev_device *hostdev, *parent;
491 char host_name[HOST_NAME_LEN];
492 const char *driver_name, *value;
494 if (!pp || !pci_name)
497 sprintf(host_name, "host%d", pp->sg_id.host_no);
498 hostdev = udev_device_new_from_subsystem_sysname(udev,
499 "scsi_host", host_name);
503 parent = udev_device_get_parent(hostdev);
505 driver_name = udev_device_get_driver(parent);
507 parent = udev_device_get_parent(parent);
510 if (!strcmp(driver_name, "pcieport"))
512 parent = udev_device_get_parent(parent);
517 value = udev_device_get_sysname(parent);
519 strncpy(pci_name, value, SLOT_NAME_SIZE);
520 udev_device_unref(hostdev);
523 udev_device_unref(hostdev);
527 int sysfs_get_iscsi_ip_address(const struct path *pp, char *ip_address)
529 struct udev_device *hostdev;
530 char host_name[HOST_NAME_LEN];
533 sprintf(host_name, "host%d", pp->sg_id.host_no);
534 hostdev = udev_device_new_from_subsystem_sysname(udev,
535 "iscsi_host", host_name);
537 value = udev_device_get_sysattr_value(hostdev,
540 strncpy(ip_address, value, SLOT_NAME_SIZE);
541 udev_device_unref(hostdev);
544 udev_device_unref(hostdev);
550 sysfs_get_asymmetric_access_state(struct path *pp, char *buff, int buflen)
552 struct udev_device *parent = pp->udev;
553 char value[16], *eptr;
554 unsigned long preferred;
557 const char *subsys = udev_device_get_subsystem(parent);
558 if (subsys && !strncmp(subsys, "scsi", 4))
560 parent = udev_device_get_parent(parent);
566 if (sysfs_attr_get_value(parent, "access_state", buff, buflen) <= 0)
569 if (sysfs_attr_get_value(parent, "preferred_path", value, 16) <= 0)
572 preferred = strtoul(value, &eptr, 0);
573 if (value == eptr || preferred == ULONG_MAX) {
574 /* Parse error, ignore */
581 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
583 struct udev_device *rport_dev = NULL;
584 char value[16], *eptr;
586 unsigned long long tmo = 0;
589 sprintf(rport_id, "rport-%d:%d-%d",
590 pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
591 rport_dev = udev_device_new_from_subsystem_sysname(udev,
592 "fc_remote_ports", rport_id);
594 condlog(1, "%s: No fc_remote_port device for '%s'", pp->dev,
598 condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
599 pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
602 * read the current dev_loss_tmo value from sysfs
604 ret = sysfs_attr_get_value(rport_dev, "dev_loss_tmo", value, 16);
606 condlog(0, "%s: failed to read dev_loss_tmo value, "
607 "error %d", rport_id, -ret);
610 tmo = strtoull(value, &eptr, 0);
611 if (value == eptr || tmo == ULLONG_MAX) {
612 condlog(0, "%s: Cannot parse dev_loss_tmo "
613 "attribute '%s'", rport_id, value);
619 * dev_loss_tmo will be limited to 600 if fast_io_fail
621 * fast_io_fail will be limited by the current dev_loss_tmo
623 * So to get everything right we first need to increase
624 * dev_loss_tmo to the fast_io_fail setting (if present),
625 * then set fast_io_fail, and _then_ set dev_loss_tmo
626 * to the correct value.
628 if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET &&
629 mpp->fast_io_fail != MP_FAST_IO_FAIL_ZERO &&
630 mpp->fast_io_fail != MP_FAST_IO_FAIL_OFF) {
631 /* Check if we need to temporarily increase dev_loss_tmo */
632 if ((unsigned int)mpp->fast_io_fail >= tmo) {
633 /* Increase dev_loss_tmo temporarily */
634 snprintf(value, sizeof(value), "%u",
635 (unsigned int)mpp->fast_io_fail + 1);
636 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
637 value, strlen(value));
640 condlog(3, "%s: rport blocked",
643 condlog(0, "%s: failed to set "
644 "dev_loss_tmo to %s, error %d",
645 rport_id, value, -ret);
649 } else if (mpp->dev_loss > DEFAULT_DEV_LOSS_TMO &&
650 mpp->no_path_retry != NO_PATH_RETRY_QUEUE) {
651 condlog(3, "%s: limiting dev_loss_tmo to %d, since "
652 "fast_io_fail is not set",
653 rport_id, DEFAULT_DEV_LOSS_TMO);
654 mpp->dev_loss = DEFAULT_DEV_LOSS_TMO;
656 if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
657 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
658 sprintf(value, "off");
659 else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
662 snprintf(value, 16, "%u", mpp->fast_io_fail);
663 ret = sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
664 value, strlen(value));
667 condlog(3, "%s: rport blocked", rport_id);
669 condlog(0, "%s: failed to set fast_io_fail_tmo to %s, error %d",
670 rport_id, value, -ret);
673 if (mpp->dev_loss > 0) {
674 snprintf(value, 16, "%u", mpp->dev_loss);
675 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
676 value, strlen(value));
679 condlog(3, "%s: rport blocked", rport_id);
681 condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
682 rport_id, value, -ret);
686 udev_device_unref(rport_dev);
690 sysfs_set_session_tmo(struct multipath *mpp, struct path *pp)
692 struct udev_device *session_dev = NULL;
696 sprintf(session_id, "session%d", pp->sg_id.transport_id);
697 session_dev = udev_device_new_from_subsystem_sysname(udev,
698 "iscsi_session", session_id);
700 condlog(1, "%s: No iscsi session for '%s'", pp->dev,
704 condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
705 pp->sg_id.channel, pp->sg_id.scsi_id, session_id);
708 condlog(3, "%s: ignoring dev_loss_tmo on iSCSI", pp->dev);
710 if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
711 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF) {
712 condlog(3, "%s: can't switch off fast_io_fail_tmo "
713 "on iSCSI", pp->dev);
714 } else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO) {
715 condlog(3, "%s: can't set fast_io_fail_tmo to '0'"
716 "on iSCSI", pp->dev);
718 snprintf(value, 11, "%u", mpp->fast_io_fail);
719 if (sysfs_attr_set_value(session_dev, "recovery_tmo",
720 value, strlen(value)) <= 0) {
721 condlog(3, "%s: Failed to set recovery_tmo, "
722 " error %d", pp->dev, errno);
726 udev_device_unref(session_dev);
731 sysfs_set_nexus_loss_tmo(struct multipath *mpp, struct path *pp)
733 struct udev_device *sas_dev = NULL;
737 sprintf(end_dev_id, "end_device-%d:%d",
738 pp->sg_id.host_no, pp->sg_id.transport_id);
739 sas_dev = udev_device_new_from_subsystem_sysname(udev,
740 "sas_end_device", end_dev_id);
742 condlog(1, "%s: No SAS end device for '%s'", pp->dev,
746 condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
747 pp->sg_id.channel, pp->sg_id.scsi_id, end_dev_id);
750 snprintf(value, 11, "%u", mpp->dev_loss);
751 if (sysfs_attr_set_value(sas_dev, "I_T_nexus_loss_timeout",
752 value, strlen(value)) <= 0)
753 condlog(3, "%s: failed to update "
754 "I_T Nexus loss timeout, error %d",
757 udev_device_unref(sas_dev);
762 sysfs_set_scsi_tmo (struct multipath *mpp, unsigned int checkint)
766 unsigned int dev_loss_tmo = mpp->dev_loss;
768 if (mpp->no_path_retry > 0) {
769 uint64_t no_path_retry_tmo =
770 (uint64_t)mpp->no_path_retry * checkint;
772 if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
773 no_path_retry_tmo = MAX_DEV_LOSS_TMO;
774 if (no_path_retry_tmo > dev_loss_tmo)
775 dev_loss_tmo = no_path_retry_tmo;
776 condlog(3, "%s: update dev_loss_tmo to %u",
777 mpp->alias, dev_loss_tmo);
778 } else if (mpp->no_path_retry == NO_PATH_RETRY_QUEUE) {
779 dev_loss_tmo = MAX_DEV_LOSS_TMO;
780 condlog(3, "%s: update dev_loss_tmo to %u",
781 mpp->alias, dev_loss_tmo);
783 mpp->dev_loss = dev_loss_tmo;
784 if (mpp->dev_loss && mpp->fast_io_fail > 0 &&
785 (unsigned int)mpp->fast_io_fail >= mpp->dev_loss) {
786 condlog(3, "%s: turning off fast_io_fail (%d is not smaller than dev_loss_tmo)",
787 mpp->alias, mpp->fast_io_fail);
788 mpp->fast_io_fail = MP_FAST_IO_FAIL_OFF;
790 if (!mpp->dev_loss && mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
793 vector_foreach_slot(mpp->paths, pp, i) {
794 if (pp->sg_id.proto_id == SCSI_PROTOCOL_FCP)
795 sysfs_set_rport_tmo(mpp, pp);
796 if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
797 sysfs_set_session_tmo(mpp, pp);
798 if (pp->sg_id.proto_id == SCSI_PROTOCOL_SAS)
799 sysfs_set_nexus_loss_tmo(mpp, pp);
805 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
806 void *resp, int mx_resp_len)
808 unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
809 { INQUIRY_CMD, 0, 0, 0, 0, 0 };
810 unsigned char sense_b[SENSE_BUFF_LEN];
811 struct sg_io_hdr io_hdr;
817 inqCmdBlk[2] = (unsigned char) pg_op;
818 inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
819 inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
820 memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
821 memset(sense_b, 0, SENSE_BUFF_LEN);
822 io_hdr.interface_id = 'S';
823 io_hdr.cmd_len = sizeof (inqCmdBlk);
824 io_hdr.mx_sb_len = sizeof (sense_b);
825 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
826 io_hdr.dxfer_len = mx_resp_len;
827 io_hdr.dxferp = resp;
828 io_hdr.cmdp = inqCmdBlk;
829 io_hdr.sbp = sense_b;
830 io_hdr.timeout = DEF_TIMEOUT * 1000;
832 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
835 /* treat SG_ERR here to get rid of sg_err.[ch] */
836 io_hdr.status &= 0x7e;
837 if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
838 (0 == io_hdr.driver_status))
840 if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
841 (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
842 (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
843 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
845 unsigned char * sense_buffer = io_hdr.sbp;
846 if (sense_buffer[0] & 0x2)
847 sense_key = sense_buffer[1] & 0xf;
849 sense_key = sense_buffer[2] & 0xf;
850 if(RECOVERED_ERROR == sense_key)
858 get_serial (char * str, int maxlen, int fd)
861 char buff[MX_ALLOC_LEN + 1] = {0};
866 if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN)) {
871 memcpy(str, buff + 4, len);
880 detect_alua(struct path * pp)
884 unsigned int timeout;
886 if (sysfs_get_timeout(pp, &timeout) <= 0)
887 timeout = DEF_TIMEOUT;
889 if ((tpgs = get_target_port_group_support(pp, timeout)) <= 0) {
890 pp->tpgs = TPGS_NONE;
893 ret = get_target_port_group(pp, timeout);
894 if (ret < 0 || get_asymmetric_access_state(pp, ret, timeout) < 0) {
895 pp->tpgs = TPGS_NONE;
901 int path_get_tpgs(struct path *pp)
903 if (pp->tpgs == TPGS_UNDEF)
908 #define DEFAULT_SGIO_LEN 254
910 /* Query VPD page @pg. Returns number of INQUIRY bytes
911 upon success and -1 upon failure. */
913 sgio_get_vpd (unsigned char * buff, int maxlen, int fd, int pg)
915 int len = DEFAULT_SGIO_LEN;
923 if (0 == do_inq(fd, 0, 1, pg, buff, len)) {
924 rlen = get_unaligned_be16(&buff[2]) + 4;
925 if (rlen <= len || len >= maxlen)
927 len = (rlen < maxlen)? rlen : maxlen;
934 get_geometry(struct path *pp)
939 if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
940 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
941 memset(&pp->geom, 0, sizeof(pp->geom));
944 condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
945 pp->dev, pp->geom.cylinders, pp->geom.heads,
946 pp->geom.sectors, pp->geom.start);
951 parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
953 size_t len = get_unaligned_be16(&in[2]);
959 * Strip leading and trailing whitespace
961 while (len > 0 && in[len + 3] == ' ')
963 while (len > 0 && in[4] == ' ') {
968 if (len >= out_len) {
969 condlog(2, "vpd pg80 overflow, %lu/%lu bytes required",
974 memcpy(out, in + 4, len);
981 parse_vpd_pg83(const unsigned char *in, size_t in_len,
982 char *out, size_t out_len)
984 const unsigned char *d;
985 const unsigned char *vpd = NULL;
986 size_t len, vpd_len, i;
987 int vpd_type, prio = -1, naa_prio;
990 while (d < in + in_len) {
991 /* Select 'association: LUN' */
992 if ((d[1] & 0x30) != 0) {
996 switch (d[1] & 0xf) {
1001 /* IEEE Registered Extended: Prio 8 */
1005 /* IEEE Registered: Prio 7 */
1009 /* IEEE Extended: Prio 6 */
1013 /* IEEE Locally assigned: Prio 1 */
1017 /* Default: no priority */
1021 if (prio < naa_prio) {
1027 /* SCSI Name: Prio 4 */
1028 if (memcmp(d + 4, "eui.", 4) &&
1029 memcmp(d + 4, "naa.", 4) &&
1030 memcmp(d + 4, "iqn.", 4))
1038 /* EUI-64: Prio 3 */
1045 /* T-10 Vendor ID: Prio 2 */
1057 /* Need space at least for one digit */
1058 else if (out_len <= 1)
1062 vpd_type = vpd[1] & 0xf;
1065 if (vpd_type == 0x2 || vpd_type == 0x3) {
1068 len = sprintf(out, "%d", vpd_type);
1069 if (2 * vpd_len >= out_len - len) {
1070 condlog(1, "%s: WWID overflow, type %d, %lu/%lu bytes required",
1072 2 * vpd_len + len + 1, out_len);
1073 vpd_len = (out_len - len - 1) / 2;
1075 for (i = 0; i < vpd_len; i++)
1076 len += sprintf(out + len,
1078 } else if (vpd_type == 0x8 && vpd_len < 4) {
1079 condlog(1, "%s: VPD length %lu too small for designator type 8",
1082 } else if (vpd_type == 0x8) {
1083 if (!memcmp("eui.", vpd, 4))
1085 else if (!memcmp("naa.", vpd, 4))
1092 while (len > 2 && vpd[len - 2] == '\0')
1094 if (len > out_len - 1) {
1095 condlog(1, "%s: WWID overflow, type 8/%c, %lu/%lu bytes required",
1096 __func__, out[0], len + 1, out_len);
1101 for (i = 0; i < len; ++i)
1102 out[1 + i] = vpd[i];
1104 for (i = 0; i < len; ++i)
1105 out[1 + i] = tolower(vpd[i]);
1107 /* designator should be 0-terminated, but let's make sure */
1110 } else if (vpd_type == 0x1) {
1111 const unsigned char *p;
1116 while ((p = memchr(vpd, ' ', vpd_len))) {
1118 if (len + p_len > out_len - 1) {
1119 condlog(1, "%s: WWID overflow, type 1, %lu/%lu bytes required",
1120 __func__, len + p_len, out_len);
1121 p_len = out_len - len - 1;
1123 memcpy(out + len, vpd, p_len);
1125 if (len >= out_len - 1) {
1131 if (len >= out_len - 1) {
1137 while (vpd && *vpd == ' ') {
1143 if (p_len > 0 && len < out_len - 1) {
1144 if (len + p_len > out_len - 1) {
1145 condlog(1, "%s: WWID overflow, type 1, %lu/%lu bytes required",
1146 __func__, len + p_len + 1, out_len);
1147 p_len = out_len - len - 1;
1149 memcpy(out + len, vpd, p_len);
1153 if (len > 1 && out[len - 1] == '_') {
1154 out[len - 1] = '\0';
1162 parse_vpd_c0_hp3par(const unsigned char *in, size_t in_len,
1163 char *out, size_t out_len)
1167 memset(out, 0x0, out_len);
1168 if (in_len <= 4 || (in[4] > 3 && in_len < 44)) {
1169 condlog(3, "HP/3PAR vendor specific VPD page length too short: %lu", in_len);
1172 if (in[4] <= 3) /* revision must be > 3 to have Vomlume Name */
1174 len = get_unaligned_be32(&in[40]);
1175 if (len > out_len || len + 44 > in_len) {
1176 condlog(3, "HP/3PAR vendor specific Volume name too long: %lu",
1180 memcpy(out, &in[44], len);
1181 out[out_len - 1] = '\0';
1186 get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
1189 unsigned char buff[4096];
1191 memset(buff, 0x0, 4096);
1192 if (!parent || sysfs_get_vpd(parent, pg, buff, 4096) <= 0) {
1193 condlog(3, "failed to read sysfs vpd pg%02x", pg);
1197 if (buff[1] != pg) {
1198 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1202 buff_len = get_unaligned_be16(&buff[2]) + 4;
1203 if (buff_len > 4096)
1204 condlog(3, "vpd pg%02x page truncated", pg);
1207 len = parse_vpd_pg80(buff, str, maxlen);
1208 else if (pg == 0x83)
1209 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1217 get_vpd_sgio (int fd, int pg, int vend_id, char * str, int maxlen)
1220 unsigned char buff[4096];
1222 memset(buff, 0x0, 4096);
1223 if (sgio_get_vpd(buff, 4096, fd, pg) < 0) {
1224 int lvl = pg == 0x80 || pg == 0x83 ? 3 : 4;
1226 condlog(lvl, "failed to issue vpd inquiry for pg%02x",
1231 if (buff[1] != pg) {
1232 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1236 buff_len = get_unaligned_be16(&buff[2]) + 4;
1237 if (buff_len > 4096) {
1238 condlog(3, "vpd pg%02x page truncated", pg);
1242 len = parse_vpd_pg80(buff, str, maxlen);
1243 else if (pg == 0x83)
1244 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1245 else if (pg == 0xc9 && maxlen >= 8) {
1249 len = (buff_len <= maxlen)? buff_len : maxlen;
1250 memcpy (str, buff, len);
1252 } else if (pg == 0xc0 && vend_id == VPD_VP_HP3PAR)
1253 len = parse_vpd_c0_hp3par(buff, buff_len, str, maxlen);
1261 scsi_sysfs_pathinfo (struct path * pp, vector hwtable)
1263 struct udev_device *parent;
1264 const char *attr_path = NULL;
1268 const char *subsys = udev_device_get_subsystem(parent);
1269 if (subsys && !strncmp(subsys, "scsi", 4)) {
1270 attr_path = udev_device_get_sysname(parent);
1273 if (sscanf(attr_path, "%i:%i:%i:%i",
1277 &pp->sg_id.lun) == 4)
1280 parent = udev_device_get_parent(parent);
1282 if (!attr_path || pp->sg_id.host_no == -1)
1283 return PATHINFO_FAILED;
1285 if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1286 return PATHINFO_FAILED;;
1288 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1290 if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1291 return PATHINFO_FAILED;;
1293 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1295 if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) < 0)
1296 return PATHINFO_FAILED;;
1298 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1301 * set the hwe configlet pointer
1303 find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1306 * host / bus / target / lun
1308 condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1318 if(sysfs_get_tgt_nodename(pp, pp->tgt_node_name))
1319 return PATHINFO_FAILED;
1321 condlog(3, "%s: tgt_node_name = %s",
1322 pp->dev, pp->tgt_node_name);
1328 nvme_sysfs_pathinfo (struct path * pp, vector hwtable)
1330 struct udev_device *parent;
1331 const char *attr_path = NULL;
1334 attr_path = udev_device_get_sysname(pp->udev);
1336 return PATHINFO_FAILED;
1338 if (sscanf(attr_path, "nvme%dn%d",
1340 &pp->sg_id.scsi_id) != 2)
1341 return PATHINFO_FAILED;
1343 parent = udev_device_get_parent_with_subsystem_devtype(pp->udev,
1346 return PATHINFO_SKIPPED;
1348 attr = udev_device_get_sysattr_value(pp->udev, "nsid");
1349 pp->sg_id.lun = attr ? atoi(attr) : 0;
1351 attr = udev_device_get_sysattr_value(parent, "cntlid");
1352 pp->sg_id.channel = attr ? atoi(attr) : 0;
1354 snprintf(pp->vendor_id, SCSI_VENDOR_SIZE, "NVME");
1355 snprintf(pp->product_id, PATH_PRODUCT_SIZE, "%s",
1356 udev_device_get_sysattr_value(parent, "model"));
1357 snprintf(pp->serial, SERIAL_SIZE, "%s",
1358 udev_device_get_sysattr_value(parent, "serial"));
1359 snprintf(pp->rev, PATH_REV_SIZE, "%s",
1360 udev_device_get_sysattr_value(parent, "firmware_rev"));
1362 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1363 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1364 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1365 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1367 find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1373 ccw_sysfs_pathinfo (struct path * pp, vector hwtable)
1375 struct udev_device *parent;
1376 char attr_buff[NAME_SIZE];
1377 const char *attr_path;
1381 const char *subsys = udev_device_get_subsystem(parent);
1382 if (subsys && !strncmp(subsys, "ccw", 3))
1384 parent = udev_device_get_parent(parent);
1387 return PATHINFO_FAILED;
1389 sprintf(pp->vendor_id, "IBM");
1391 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1393 if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE) <= 0)
1394 return PATHINFO_FAILED;
1396 if (!strncmp(attr_buff, "3370", 4)) {
1397 sprintf(pp->product_id,"S/390 DASD FBA");
1398 } else if (!strncmp(attr_buff, "9336", 4)) {
1399 sprintf(pp->product_id,"S/390 DASD FBA");
1401 sprintf(pp->product_id,"S/390 DASD ECKD");
1404 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1407 * set the hwe configlet pointer
1409 find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1412 * host / bus / target / lun
1414 attr_path = udev_device_get_sysname(parent);
1416 if (sscanf(attr_path, "%i.%i.%x",
1419 &pp->sg_id.scsi_id) == 3) {
1420 condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1432 cciss_sysfs_pathinfo (struct path * pp, vector hwtable)
1434 const char * attr_path = NULL;
1435 struct udev_device *parent;
1439 const char *subsys = udev_device_get_subsystem(parent);
1440 if (subsys && !strncmp(subsys, "cciss", 5)) {
1441 attr_path = udev_device_get_sysname(parent);
1444 if (sscanf(attr_path, "c%id%i",
1446 &pp->sg_id.scsi_id) == 2)
1449 parent = udev_device_get_parent(parent);
1451 if (!attr_path || pp->sg_id.host_no == -1)
1452 return PATHINFO_FAILED;
1454 if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1455 return PATHINFO_FAILED;
1457 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1459 if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1460 return PATHINFO_FAILED;
1462 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1464 if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) <= 0)
1465 return PATHINFO_FAILED;
1467 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1470 * set the hwe configlet pointer
1472 find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1475 * host / bus / target / lun
1478 pp->sg_id.channel = 0;
1479 condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1490 common_sysfs_pathinfo (struct path * pp)
1495 return PATHINFO_FAILED;
1498 condlog(4, "%s: udev not initialised", pp->dev);
1499 return PATHINFO_FAILED;
1501 devt = udev_device_get_devnum(pp->udev);
1502 snprintf(pp->dev_t, BLK_DEV_SIZE, "%d:%d", major(devt), minor(devt));
1504 condlog(4, "%s: dev_t = %s", pp->dev, pp->dev_t);
1506 if (sysfs_get_size(pp, &pp->size))
1507 return PATHINFO_FAILED;
1509 condlog(3, "%s: size = %llu", pp->dev, pp->size);
1515 path_offline (struct path * pp)
1517 struct udev_device * parent;
1518 char buff[SCSI_STATE_SIZE];
1520 const char *subsys_type;
1522 if (pp->bus == SYSFS_BUS_SCSI) {
1523 subsys_type = "scsi";
1525 else if (pp->bus == SYSFS_BUS_NVME) {
1526 subsys_type = "nvme";
1534 const char *subsys = udev_device_get_subsystem(parent);
1535 if (subsys && !strncmp(subsys, subsys_type, 4))
1537 parent = udev_device_get_parent(parent);
1541 condlog(1, "%s: failed to get sysfs information", pp->dev);
1542 return PATH_REMOVED;
1545 memset(buff, 0x0, SCSI_STATE_SIZE);
1546 err = sysfs_attr_get_value(parent, "state", buff, SCSI_STATE_SIZE);
1549 return PATH_REMOVED;
1555 condlog(4, "%s: path state = %s", pp->dev, buff);
1557 if (pp->bus == SYSFS_BUS_SCSI) {
1558 if (!strncmp(buff, "offline", 7)) {
1563 if (!strncmp(buff, "blocked", 7) ||
1564 !strncmp(buff, "quiesce", 7))
1565 return PATH_PENDING;
1566 else if (!strncmp(buff, "running", 7))
1570 else if (pp->bus == SYSFS_BUS_NVME) {
1571 if (!strncmp(buff, "dead", 4)) {
1576 if (!strncmp(buff, "new", 3) ||
1577 !strncmp(buff, "deleting", 8))
1578 return PATH_PENDING;
1579 else if (!strncmp(buff, "live", 4))
1587 sysfs_pathinfo(struct path * pp, vector hwtable)
1589 int r = common_sysfs_pathinfo(pp);
1591 if (r != PATHINFO_OK)
1594 pp->bus = SYSFS_BUS_UNDEF;
1595 if (!strncmp(pp->dev,"cciss",5))
1596 pp->bus = SYSFS_BUS_CCISS;
1597 if (!strncmp(pp->dev,"dasd", 4))
1598 pp->bus = SYSFS_BUS_CCW;
1599 if (!strncmp(pp->dev,"sd", 2))
1600 pp->bus = SYSFS_BUS_SCSI;
1601 if (!strncmp(pp->dev,"nvme", 4))
1602 pp->bus = SYSFS_BUS_NVME;
1605 case SYSFS_BUS_SCSI:
1606 return scsi_sysfs_pathinfo(pp, hwtable);
1608 return ccw_sysfs_pathinfo(pp, hwtable);
1609 case SYSFS_BUS_CCISS:
1610 return cciss_sysfs_pathinfo(pp, hwtable);
1611 case SYSFS_BUS_NVME:
1612 return nvme_sysfs_pathinfo(pp, hwtable);
1613 case SYSFS_BUS_UNDEF:
1620 scsi_ioctl_pathinfo (struct path * pp, int mask)
1622 struct udev_device *parent;
1623 const char *attr_path = NULL;
1626 if (!(mask & DI_SERIAL))
1629 select_vpd_vendor_id(pp);
1630 vpd_id = pp->vpd_vendor_id;
1632 if (vpd_id != VPD_VP_UNDEF) {
1633 char vpd_data[VPD_DATA_SIZE] = {0};
1635 if (get_vpd_sgio(pp->fd, vpd_vendor_pages[vpd_id].pg, vpd_id,
1636 vpd_data, sizeof(vpd_data)) < 0)
1637 condlog(3, "%s: failed to get extra vpd data", pp->dev);
1639 vpd_data[VPD_DATA_SIZE - 1] = '\0';
1642 pp->vpd_data = strdup(vpd_data);
1644 condlog(0, "%s: failed to allocate space for vpd data", pp->dev);
1650 const char *subsys = udev_device_get_subsystem(parent);
1651 if (subsys && !strncmp(subsys, "scsi", 4)) {
1652 attr_path = udev_device_get_sysname(parent);
1655 if (sscanf(attr_path, "%i:%i:%i:%i",
1659 &pp->sg_id.lun) == 4)
1662 parent = udev_device_get_parent(parent);
1664 if (!attr_path || pp->sg_id.host_no == -1)
1667 if (get_vpd_sysfs(parent, 0x80, pp->serial, SERIAL_SIZE) <= 0) {
1668 if (get_serial(pp->serial, SERIAL_SIZE, pp->fd)) {
1669 condlog(3, "%s: fail to get serial", pp->dev);
1674 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1679 cciss_ioctl_pathinfo(struct path *pp)
1681 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1682 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1686 get_state (struct path * pp, struct config *conf, int daemon, int oldstate)
1688 struct checker * c = &pp->checker;
1691 if (!checker_selected(c)) {
1693 if (pathinfo(pp, conf, DI_SYSFS) != PATHINFO_OK) {
1694 condlog(3, "%s: couldn't get sysfs pathinfo",
1696 return PATH_UNCHECKED;
1699 select_detect_checker(conf, pp);
1700 select_checker(conf, pp);
1701 if (!checker_selected(c)) {
1702 condlog(3, "%s: No checker selected", pp->dev);
1703 return PATH_UNCHECKED;
1705 checker_set_fd(c, pp->fd);
1706 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
1708 condlog(3, "%s: checker init failed", pp->dev);
1709 return PATH_UNCHECKED;
1712 if (pp->mpp && !c->mpcontext)
1713 checker_mp_init(c, &pp->mpp->mpcontext);
1714 checker_clear_message(c);
1715 if (conf->force_sync == 0)
1716 checker_set_async(c);
1718 checker_set_sync(c);
1719 if (!conf->checker_timeout &&
1720 sysfs_get_timeout(pp, &(c->timeout)) <= 0)
1721 c->timeout = DEF_TIMEOUT;
1722 state = checker_check(c, oldstate);
1723 condlog(3, "%s: %s state = %s", pp->dev,
1724 checker_name(c), checker_state_name(state));
1725 if (state != PATH_UP && state != PATH_GHOST &&
1726 strlen(checker_message(c)))
1727 condlog(3, "%s: %s checker%s",
1728 pp->dev, checker_name(c), checker_message(c));
1733 get_prio (struct path * pp, int timeout)
1736 struct config *conf;
1743 if (!prio_selected(p)) {
1744 conf = get_multipath_config();
1745 pthread_cleanup_push(put_multipath_config, conf);
1746 select_detect_prio(conf, pp);
1747 select_prio(conf, pp);
1748 pthread_cleanup_pop(1);
1749 if (!prio_selected(p)) {
1750 condlog(3, "%s: no prio selected", pp->dev);
1751 pp->priority = PRIO_UNDEF;
1755 old_prio = pp->priority;
1756 pp->priority = prio_getprio(p, pp, timeout);
1757 if (pp->priority < 0) {
1758 /* this changes pp->offline, but why not */
1759 int state = path_offline(pp);
1761 if (state == PATH_DOWN || state == PATH_PENDING) {
1762 pp->priority = old_prio;
1763 condlog(3, "%s: %s prio error in state %d, keeping prio = %d",
1764 pp->dev, prio_name(p), state, pp->priority);
1766 condlog(3, "%s: %s prio error in state %d",
1767 pp->dev, prio_name(p), state);
1768 pp->priority = PRIO_UNDEF;
1772 condlog((old_prio == pp->priority ? 4 : 3), "%s: %s prio = %u",
1773 pp->dev, prio_name(p), pp->priority);
1778 * Mangle string of length *len starting at start
1779 * by removing character sequence "00" (hex for a 0 byte),
1780 * starting at end, backwards.
1781 * Changes the value of *len if characters were removed.
1782 * Returns a pointer to the position where "end" was moved to.
1785 *skip_zeroes_backward(char* start, size_t *len, char *end)
1789 while (p >= start + 2 && *(p - 1) == '0' && *(p - 2) == '0')
1795 memmove(p, end, start + *len + 1 - end);
1802 * Fix for NVME wwids looking like this:
1803 * nvme.0000-3163653363666438366239656630386200-4c696e75780000000000000000000000000000000000000000000000000000000000000000000000-00000002
1804 * which are encountered in some combinations of Linux NVME host and target.
1805 * The '00' are hex-encoded 0-bytes which are forbidden in the serial (SN)
1806 * and model (MN) fields. Discard them.
1807 * If a WWID of the above type is found, sets pp->wwid and returns a value > 0.
1808 * Otherwise, returns 0.
1811 fix_broken_nvme_wwid(struct path *pp, const char *value, size_t size)
1813 static const char _nvme[] = "nvme.";
1818 len = strlen(value);
1819 if (len >= sizeof(mangled))
1822 /* Check that value starts with "nvme.%04x-" */
1823 if (memcmp(value, _nvme, sizeof(_nvme) - 1) || value[9] != '-')
1825 for (i = 5; i < 9; i++)
1826 if (!isxdigit(value[i]))
1829 memcpy(mangled, value, len + 1);
1831 /* search end of "model" part and strip trailing '00' */
1832 p = memrchr(mangled, '-', len);
1836 p = skip_zeroes_backward(mangled, &len, p);
1838 /* search end of "serial" part */
1839 p = memrchr(mangled, '-', p - mangled);
1840 if (p == NULL || memrchr(mangled, '-', p - mangled) != mangled + 9)
1841 /* We expect exactly 3 '-' in the value */
1844 p = skip_zeroes_backward(mangled, &len, p);
1848 memcpy(pp->wwid, mangled, len + 1);
1849 condlog(2, "%s: over-long WWID shortened to %s", pp->dev, pp->wwid);
1854 get_udev_uid(struct path * pp, char *uid_attribute, struct udev_device *udev)
1859 value = udev_device_get_property_value(udev, uid_attribute);
1860 if (!value || strlen(value) == 0)
1861 value = getenv(uid_attribute);
1862 if (value && strlen(value)) {
1863 len = strlcpy(pp->wwid, value, WWID_SIZE);
1864 if (len >= WWID_SIZE) {
1865 len = fix_broken_nvme_wwid(pp, value, WWID_SIZE);
1868 condlog(0, "%s: wwid overflow", pp->dev);
1872 condlog(3, "%s: no %s attribute", pp->dev,
1880 get_vpd_uid(struct path * pp)
1882 struct udev_device *parent = pp->udev;
1885 const char *subsys = udev_device_get_subsystem(parent);
1886 if (subsys && !strncmp(subsys, "scsi", 4))
1888 parent = udev_device_get_parent(parent);
1894 return get_vpd_sysfs(parent, 0x83, pp->wwid, WWID_SIZE);
1897 static ssize_t uid_fallback(struct path *pp, int path_state,
1898 const char **origin)
1902 if (pp->bus == SYSFS_BUS_SCSI) {
1903 len = get_vpd_uid(pp);
1905 if (len < 0 && path_state == PATH_UP) {
1906 condlog(1, "%s: failed to get sysfs uid: %s",
1907 pp->dev, strerror(-len));
1908 len = get_vpd_sgio(pp->fd, 0x83, 0, pp->wwid,
1912 } else if (pp->bus == SYSFS_BUS_NVME) {
1914 len = sysfs_attr_get_value(pp->udev, "wwid", value,
1918 len = strlcpy(pp->wwid, value, WWID_SIZE);
1919 if (len >= WWID_SIZE) {
1920 len = fix_broken_nvme_wwid(pp, value,
1924 condlog(0, "%s: wwid overflow", pp->dev);
1932 static bool has_uid_fallback(struct path *pp)
1935 * Falling back to direct WWID determination is dangerous
1936 * if uid_attribute is set to something non-standard.
1937 * Allow it only if it's either the default, or if udev
1938 * has been disabled by setting 'uid_attribute ""'.
1940 if (!pp->uid_attribute)
1942 return ((pp->bus == SYSFS_BUS_SCSI &&
1943 (!strcmp(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE) ||
1944 !strcmp(pp->uid_attribute, ""))) ||
1945 (pp->bus == SYSFS_BUS_NVME &&
1946 (!strcmp(pp->uid_attribute, DEFAULT_NVME_UID_ATTRIBUTE) ||
1947 !strcmp(pp->uid_attribute, ""))));
1951 get_uid (struct path * pp, int path_state, struct udev_device *udev,
1955 const char *origin = "unknown";
1957 struct config *conf;
1958 int used_fallback = 0;
1960 if (!pp->uid_attribute && !pp->getuid) {
1961 conf = get_multipath_config();
1962 pthread_cleanup_push(put_multipath_config, conf);
1963 select_getuid(conf, pp);
1964 pthread_cleanup_pop(1);
1967 memset(pp->wwid, 0, WWID_SIZE);
1969 char buff[CALLOUT_MAX_SIZE];
1971 /* Use 'getuid' callout, deprecated */
1972 condlog(1, "%s: using deprecated getuid callout", pp->dev);
1973 if (path_state != PATH_UP) {
1974 condlog(3, "%s: path inaccessible", pp->dev);
1976 } else if (apply_format(pp->getuid, &buff[0], pp)) {
1977 condlog(0, "error formatting uid callout command");
1979 } else if (execute_program(buff, pp->wwid, WWID_SIZE)) {
1980 condlog(3, "error calling out %s", buff);
1983 len = strlen(pp->wwid);
1986 bool udev_available = udev && pp->uid_attribute
1987 && *pp->uid_attribute;
1989 if (udev_available) {
1990 len = get_udev_uid(pp, pp->uid_attribute, udev);
1993 "%s: failed to get udev uid: %s",
1994 pp->dev, strerror(-len));
1998 if ((!udev_available || (len <= 0 && allow_fallback))
1999 && has_uid_fallback(pp)) {
2001 len = uid_fallback(pp, path_state, &origin);
2005 condlog(1, "%s: failed to get %s uid: %s",
2006 pp->dev, origin, strerror(-len));
2007 memset(pp->wwid, 0x0, WWID_SIZE);
2010 /* Strip any trailing blanks */
2011 c = strchr(pp->wwid, '\0');
2013 while (c && c >= pp->wwid && *c == ' ') {
2018 condlog((used_fallback)? 1 : 3, "%s: uid = %s (%s)", pp->dev,
2019 *pp->wwid == '\0' ? "<empty>" : pp->wwid, origin);
2023 int pathinfo(struct path *pp, struct config *conf, int mask)
2028 return PATHINFO_FAILED;
2031 * For behavior backward-compatibility with multipathd,
2032 * the blacklisting by filter_property|devnode() is not
2033 * limited by DI_BLACKLIST and occurs before this debug
2034 * message with the mask value.
2037 const char *hidden =
2038 udev_device_get_sysattr_value(pp->udev, "hidden");
2040 if (hidden && !strcmp(hidden, "1")) {
2041 condlog(4, "%s: hidden", pp->dev);
2042 return PATHINFO_SKIPPED;
2044 if (is_claimed_by_foreign(pp->udev) ||
2045 filter_property(conf, pp->udev, 4, pp->uid_attribute) > 0)
2046 return PATHINFO_SKIPPED;
2049 if (filter_devnode(conf->blist_devnode,
2050 conf->elist_devnode,
2052 return PATHINFO_SKIPPED;
2054 condlog(4, "%s: mask = 0x%x", pp->dev, mask);
2057 * Sanity check: we need the device number to
2058 * avoid inconsistent information in
2059 * find_path_by_dev()/find_path_by_devt()
2061 if (!strlen(pp->dev_t) && !(mask & DI_SYSFS)) {
2062 condlog(1, "%s: empty device number", pp->dev);
2067 * fetch info available in sysfs
2069 if (mask & DI_SYSFS) {
2070 int rc = sysfs_pathinfo(pp, conf->hwtable);
2072 if (rc != PATHINFO_OK)
2076 if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
2077 if (filter_device(conf->blist_device, conf->elist_device,
2078 pp->vendor_id, pp->product_id, pp->dev) > 0 ||
2079 filter_protocol(conf->blist_protocol, conf->elist_protocol,
2081 return PATHINFO_SKIPPED;
2084 path_state = path_offline(pp);
2085 if (path_state == PATH_REMOVED)
2087 else if (mask & DI_NOIO) {
2088 if (mask & DI_CHECKER)
2090 * Avoid any IO on the device itself.
2091 * simply use the path_offline() return as its state
2093 pp->chkrstate = pp->state = path_state;
2098 * fetch info not available through sysfs
2101 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
2104 condlog(4, "Couldn't open node for %s: %s",
2105 pp->dev, strerror(errno));
2109 if (mask & DI_SERIAL)
2112 if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI)
2113 scsi_ioctl_pathinfo(pp, mask);
2115 if (pp->bus == SYSFS_BUS_CCISS && mask & DI_SERIAL)
2116 cciss_ioctl_pathinfo(pp);
2118 if (mask & DI_CHECKER) {
2119 if (path_state == PATH_UP) {
2120 int newstate = get_state(pp, conf, 0, path_state);
2121 if (newstate != PATH_PENDING ||
2122 pp->state == PATH_UNCHECKED ||
2123 pp->state == PATH_WILD)
2124 pp->chkrstate = pp->state = newstate;
2125 if (pp->state == PATH_TIMEOUT)
2126 pp->state = PATH_DOWN;
2127 if (pp->state == PATH_UP && !pp->size) {
2128 condlog(3, "%s: device size is 0, "
2129 "path unusable", pp->dev);
2130 pp->state = PATH_GHOST;
2133 condlog(3, "%s: path inaccessible", pp->dev);
2134 pp->chkrstate = pp->state = path_state;
2138 if ((mask & DI_WWID) && !strlen(pp->wwid)) {
2139 get_uid(pp, path_state, pp->udev,
2140 (pp->retriggers >= conf->retrigger_tries));
2141 if (!strlen(pp->wwid)) {
2142 if (pp->bus == SYSFS_BUS_UNDEF)
2143 return PATHINFO_SKIPPED;
2144 if (pp->initialized != INIT_FAILED) {
2145 pp->initialized = INIT_MISSING_UDEV;
2146 pp->tick = conf->retrigger_delay;
2154 if (mask & DI_BLACKLIST && mask & DI_WWID) {
2155 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
2156 pp->wwid, pp->dev) > 0) {
2157 return PATHINFO_SKIPPED;
2162 * Retrieve path priority, even for PATH_DOWN paths if it has never
2163 * been successfully obtained before. If path is down don't try
2166 if ((mask & DI_PRIO) && path_state == PATH_UP && strlen(pp->wwid)) {
2167 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
2168 get_prio(pp, (pp->state != PATH_DOWN)?
2169 (conf->checker_timeout * 1000) : 10);
2173 if ((mask & DI_ALL) == DI_ALL)
2174 pp->initialized = INIT_OK;
2179 * Recoverable error, for example faulty or offline path
2181 pp->chkrstate = pp->state = PATH_DOWN;
2182 if (pp->initialized == INIT_NEW || pp->initialized == INIT_FAILED)
2183 memset(pp->wwid, 0, WWID_SIZE);