2 * compose persistent device path
4 * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
6 * Logic based on Hannes Reinecke's shell script.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "string-util.h"
37 static int path_prepend(char **path
, const char *fmt
, ...) {
43 err
= vasprintf(&pre
, fmt
, va
);
51 err
= asprintf(&new, "%s-%s", pre
, *path
);
65 ** Linux only supports 32 bit luns.
66 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
68 static int format_lun_number(struct udev_device
*dev
, char **path
) {
69 unsigned long lun
= strtoul(udev_device_get_sysnum(dev
), NULL
, 10);
71 /* address method 0, peripheral device addressing with bus id of zero */
73 return path_prepend(path
, "lun-%lu", lun
);
74 /* handle all other lun addressing methods by using a variant of the original lun format */
75 return path_prepend(path
, "lun-0x%04lx%04lx00000000", lun
& 0xffff, (lun
>> 16) & 0xffff);
78 static struct udev_device
*skip_subsystem(struct udev_device
*dev
, const char *subsys
) {
79 struct udev_device
*parent
= dev
;
84 while (parent
!= NULL
) {
85 const char *subsystem
;
87 subsystem
= udev_device_get_subsystem(parent
);
88 if (subsystem
== NULL
|| !streq(subsystem
, subsys
))
91 parent
= udev_device_get_parent(parent
);
96 static struct udev_device
*handle_scsi_fibre_channel(struct udev_device
*parent
, char **path
) {
97 struct udev
*udev
= udev_device_get_udev(parent
);
98 struct udev_device
*targetdev
;
99 struct udev_device
*fcdev
= NULL
;
106 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
107 if (targetdev
== NULL
)
110 fcdev
= udev_device_new_from_subsystem_sysname(udev
, "fc_transport", udev_device_get_sysname(targetdev
));
113 port
= udev_device_get_sysattr_value(fcdev
, "port_name");
119 format_lun_number(parent
, &lun
);
120 path_prepend(path
, "fc-%s-%s", port
, lun
);
123 udev_device_unref(fcdev
);
127 static struct udev_device
*handle_scsi_sas_wide_port(struct udev_device
*parent
, char **path
) {
128 struct udev
*udev
= udev_device_get_udev(parent
);
129 struct udev_device
*targetdev
;
130 struct udev_device
*target_parent
;
131 struct udev_device
*sasdev
;
132 const char *sas_address
;
138 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
139 if (targetdev
== NULL
)
142 target_parent
= udev_device_get_parent(targetdev
);
143 if (target_parent
== NULL
)
146 sasdev
= udev_device_new_from_subsystem_sysname(udev
, "sas_device",
147 udev_device_get_sysname(target_parent
));
151 sas_address
= udev_device_get_sysattr_value(sasdev
, "sas_address");
152 if (sas_address
== NULL
) {
157 format_lun_number(parent
, &lun
);
158 path_prepend(path
, "sas-%s-%s", sas_address
, lun
);
161 udev_device_unref(sasdev
);
165 static struct udev_device
*handle_scsi_sas(struct udev_device
*parent
, char **path
)
167 struct udev
*udev
= udev_device_get_udev(parent
);
168 struct udev_device
*targetdev
;
169 struct udev_device
*target_parent
;
170 struct udev_device
*port
;
171 struct udev_device
*expander
;
172 struct udev_device
*target_sasdev
= NULL
;
173 struct udev_device
*expander_sasdev
= NULL
;
174 struct udev_device
*port_sasdev
= NULL
;
175 const char *sas_address
= NULL
;
177 const char *phy_count
;
183 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
184 if (targetdev
== NULL
)
187 target_parent
= udev_device_get_parent(targetdev
);
188 if (target_parent
== NULL
)
192 target_sasdev
= udev_device_new_from_subsystem_sysname(udev
,
193 "sas_device", udev_device_get_sysname(target_parent
));
194 if (target_sasdev
== NULL
)
197 /* The next parent is sas port */
198 port
= udev_device_get_parent(target_parent
);
204 /* Get port device */
205 port_sasdev
= udev_device_new_from_subsystem_sysname(udev
,
206 "sas_port", udev_device_get_sysname(port
));
208 phy_count
= udev_device_get_sysattr_value(port_sasdev
, "num_phys");
209 if (phy_count
== NULL
) {
214 /* Check if we are simple disk */
215 if (strncmp(phy_count
, "1", 2) != 0) {
216 parent
= handle_scsi_sas_wide_port(parent
, path
);
220 /* Get connected phy */
221 phy_id
= udev_device_get_sysattr_value(target_sasdev
, "phy_identifier");
222 if (phy_id
== NULL
) {
227 /* The port's parent is either hba or expander */
228 expander
= udev_device_get_parent(port
);
229 if (expander
== NULL
) {
234 /* Get expander device */
235 expander_sasdev
= udev_device_new_from_subsystem_sysname(udev
,
236 "sas_device", udev_device_get_sysname(expander
));
237 if (expander_sasdev
!= NULL
) {
238 /* Get expander's address */
239 sas_address
= udev_device_get_sysattr_value(expander_sasdev
,
241 if (sas_address
== NULL
) {
247 format_lun_number(parent
, &lun
);
249 path_prepend(path
, "sas-exp%s-phy%s-%s", sas_address
, phy_id
, lun
);
251 path_prepend(path
, "sas-phy%s-%s", phy_id
, lun
);
255 udev_device_unref(target_sasdev
);
256 udev_device_unref(expander_sasdev
);
257 udev_device_unref(port_sasdev
);
261 static struct udev_device
*handle_scsi_iscsi(struct udev_device
*parent
, char **path
) {
262 struct udev
*udev
= udev_device_get_udev(parent
);
263 struct udev_device
*transportdev
;
264 struct udev_device
*sessiondev
= NULL
;
267 struct udev_device
*conndev
= NULL
;
275 /* find iscsi session */
276 transportdev
= parent
;
278 transportdev
= udev_device_get_parent(transportdev
);
279 if (transportdev
== NULL
)
281 if (startswith(udev_device_get_sysname(transportdev
), "session"))
285 /* find iscsi session device */
286 sessiondev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_session", udev_device_get_sysname(transportdev
));
287 if (sessiondev
== NULL
)
289 target
= udev_device_get_sysattr_value(sessiondev
, "targetname");
290 if (target
== NULL
) {
295 if (asprintf(&connname
, "connection%s:0", udev_device_get_sysnum(transportdev
)) < 0) {
299 conndev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_connection", connname
);
301 if (conndev
== NULL
) {
305 addr
= udev_device_get_sysattr_value(conndev
, "persistent_address");
306 port
= udev_device_get_sysattr_value(conndev
, "persistent_port");
307 if (addr
== NULL
|| port
== NULL
) {
312 format_lun_number(parent
, &lun
);
313 path_prepend(path
, "ip-%s:%s-iscsi-%s-%s", addr
, port
, target
, lun
);
316 udev_device_unref(sessiondev
);
317 udev_device_unref(conndev
);
321 static struct udev_device
*handle_scsi_ata(struct udev_device
*parent
, char **path
) {
322 struct udev
*udev
= udev_device_get_udev(parent
);
323 struct udev_device
*targetdev
;
324 struct udev_device
*target_parent
;
325 struct udev_device
*atadev
;
331 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
335 target_parent
= udev_device_get_parent(targetdev
);
339 atadev
= udev_device_new_from_subsystem_sysname(udev
, "ata_port", udev_device_get_sysname(target_parent
));
343 port_no
= udev_device_get_sysattr_value(atadev
, "port_no");
348 path_prepend(path
, "ata-%s", port_no
);
350 udev_device_unref(atadev
);
354 static struct udev_device
*handle_scsi_default(struct udev_device
*parent
, char **path
) {
355 struct udev_device
*hostdev
;
356 int host
, bus
, target
, lun
;
367 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
371 name
= udev_device_get_sysname(parent
);
372 if (sscanf(name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4)
376 * Rebase host offset to get the local relative number
378 * Note: This is by definition racy, unreliable and too simple.
379 * Please do not copy this model anywhere. It's just a left-over
380 * from the time we had no idea how things should look like in
383 * Making assumptions about a global in-kernel counter and use
384 * that to calculate a local offset is a very broken concept. It
385 * can only work as long as things are in strict order.
387 * The kernel needs to export the instance/port number of a
388 * controller directly, without the need for rebase magic like
389 * this. Manual driver unbind/bind, parallel hotplug/unplug will
390 * get into the way of this "I hope it works" logic.
393 base
= strdup(udev_device_get_syspath(hostdev
));
396 pos
= strrchr(base
, '/');
407 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
411 if (dent
->d_name
[0] == '.')
413 if (dent
->d_type
!= DT_DIR
&& dent
->d_type
!= DT_LNK
)
415 if (!startswith(dent
->d_name
, "host"))
417 i
= strtoul(&dent
->d_name
[4], &rest
, 10);
421 * find the smallest number; the host really needs to export its
422 * own instance number per parent device; relying on the global host
423 * enumeration and plainly rebasing the numbers sounds unreliable
425 if (basenum
== -1 || i
< basenum
)
435 path_prepend(path
, "scsi-%u:%u:%u:%u", host
, bus
, target
, lun
);
441 static struct udev_device
*handle_scsi_hyperv(struct udev_device
*parent
, char **path
) {
442 struct udev_device
*hostdev
;
443 struct udev_device
*vmbusdev
;
444 const char *guid_str
;
452 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
456 vmbusdev
= udev_device_get_parent(hostdev
);
460 guid_str
= udev_device_get_sysattr_value(vmbusdev
, "device_id");
464 if (strlen(guid_str
) < 37 || guid_str
[0] != '{' || guid_str
[36] != '}')
467 for (i
= 1, k
= 0; i
< 36; i
++) {
468 if (guid_str
[i
] == '-')
470 guid
[k
++] = guid_str
[i
];
474 format_lun_number(parent
, &lun
);
475 path_prepend(path
, "vmbus-%s-%s", guid
, lun
);
480 static struct udev_device
*handle_scsi(struct udev_device
*parent
, char **path
, bool *supported_parent
) {
485 devtype
= udev_device_get_devtype(parent
);
486 if (devtype
== NULL
|| !streq(devtype
, "scsi_device"))
490 id
= udev_device_get_sysattr_value(parent
, "ieee1394_id");
492 parent
= skip_subsystem(parent
, "scsi");
493 path_prepend(path
, "ieee1394-0x%s", id
);
494 *supported_parent
= true;
498 /* scsi sysfs does not have a "subsystem" for the transport */
499 name
= udev_device_get_syspath(parent
);
501 if (strstr(name
, "/rport-") != NULL
) {
502 parent
= handle_scsi_fibre_channel(parent
, path
);
503 *supported_parent
= true;
507 if (strstr(name
, "/end_device-") != NULL
) {
508 parent
= handle_scsi_sas(parent
, path
);
509 *supported_parent
= true;
513 if (strstr(name
, "/session") != NULL
) {
514 parent
= handle_scsi_iscsi(parent
, path
);
515 *supported_parent
= true;
519 if (strstr(name
, "/ata") != NULL
) {
520 parent
= handle_scsi_ata(parent
, path
);
524 if (strstr(name
, "/vmbus_") != NULL
) {
525 parent
= handle_scsi_hyperv(parent
, path
);
529 parent
= handle_scsi_default(parent
, path
);
534 static struct udev_device
*handle_cciss(struct udev_device
*parent
, char **path
) {
536 unsigned int controller
, disk
;
538 str
= udev_device_get_sysname(parent
);
539 if (sscanf(str
, "c%ud%u%*s", &controller
, &disk
) != 2)
542 path_prepend(path
, "cciss-disk%u", disk
);
543 parent
= skip_subsystem(parent
, "cciss");
547 static void handle_scsi_tape(struct udev_device
*dev
, char **path
) {
550 /* must be the last device in the syspath */
554 name
= udev_device_get_sysname(dev
);
555 if (startswith(name
, "nst") && strchr("lma", name
[3]) != NULL
)
556 path_prepend(path
, "nst%c", name
[3]);
557 else if (startswith(name
, "st") && strchr("lma", name
[2]) != NULL
)
558 path_prepend(path
, "st%c", name
[2]);
561 static struct udev_device
*handle_usb(struct udev_device
*parent
, char **path
) {
566 devtype
= udev_device_get_devtype(parent
);
569 if (!streq(devtype
, "usb_interface") && !streq(devtype
, "usb_device"))
572 str
= udev_device_get_sysname(parent
);
573 port
= strchr(str
, '-');
578 parent
= skip_subsystem(parent
, "usb");
579 path_prepend(path
, "usb-0:%s", port
);
583 static struct udev_device
*handle_bcma(struct udev_device
*parent
, char **path
) {
587 sysname
= udev_device_get_sysname(parent
);
588 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
591 path_prepend(path
, "bcma-%u", core
);
595 static struct udev_device
*handle_ccw(struct udev_device
*parent
, struct udev_device
*dev
, char **path
) {
596 struct udev_device
*scsi_dev
;
602 scsi_dev
= udev_device_get_parent_with_subsystem_devtype(dev
, "scsi", "scsi_device");
603 if (scsi_dev
!= NULL
) {
608 hba_id
= udev_device_get_sysattr_value(scsi_dev
, "hba_id");
609 wwpn
= udev_device_get_sysattr_value(scsi_dev
, "wwpn");
610 lun
= udev_device_get_sysattr_value(scsi_dev
, "fcp_lun");
611 if (hba_id
!= NULL
&& lun
!= NULL
&& wwpn
!= NULL
) {
612 path_prepend(path
, "ccw-%s-zfcp-%s:%s", hba_id
, wwpn
, lun
);
617 path_prepend(path
, "ccw-%s", udev_device_get_sysname(parent
));
619 parent
= skip_subsystem(parent
, "ccw");
623 static int builtin_path_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
624 struct udev_device
*parent
;
626 bool supported_transport
= false;
627 bool supported_parent
= false;
632 parent
= udev_device_get_parent_with_subsystem_devtype(dev
, "ccw", NULL
);
633 if (parent
!= NULL
) {
634 handle_ccw(parent
, dev
, &path
);
638 /* walk up the chain of devices and compose path */
640 while (parent
!= NULL
) {
643 subsys
= udev_device_get_subsystem(parent
);
644 if (subsys
== NULL
) {
646 } else if (streq(subsys
, "scsi_tape")) {
647 handle_scsi_tape(parent
, &path
);
648 } else if (streq(subsys
, "scsi")) {
649 parent
= handle_scsi(parent
, &path
, &supported_parent
);
650 supported_transport
= true;
651 } else if (streq(subsys
, "cciss")) {
652 parent
= handle_cciss(parent
, &path
);
653 supported_transport
= true;
654 } else if (streq(subsys
, "usb")) {
655 parent
= handle_usb(parent
, &path
);
656 supported_transport
= true;
657 } else if (streq(subsys
, "bcma")) {
658 parent
= handle_bcma(parent
, &path
);
659 supported_transport
= true;
660 } else if (streq(subsys
, "serio")) {
661 path_prepend(&path
, "serio-%s", udev_device_get_sysnum(parent
));
662 parent
= skip_subsystem(parent
, "serio");
663 } else if (streq(subsys
, "pci")) {
664 path_prepend(&path
, "pci-%s", udev_device_get_sysname(parent
));
665 parent
= skip_subsystem(parent
, "pci");
666 supported_parent
= true;
667 } else if (streq(subsys
, "platform")) {
668 path_prepend(&path
, "platform-%s", udev_device_get_sysname(parent
));
669 parent
= skip_subsystem(parent
, "platform");
670 supported_transport
= true;
671 supported_parent
= true;
672 } else if (streq(subsys
, "acpi")) {
673 path_prepend(&path
, "acpi-%s", udev_device_get_sysname(parent
));
674 parent
= skip_subsystem(parent
, "acpi");
675 supported_parent
= true;
676 } else if (streq(subsys
, "xen")) {
677 path_prepend(&path
, "xen-%s", udev_device_get_sysname(parent
));
678 parent
= skip_subsystem(parent
, "xen");
679 supported_parent
= true;
680 } else if (streq(subsys
, "scm")) {
681 path_prepend(&path
, "scm-%s", udev_device_get_sysname(parent
));
682 parent
= skip_subsystem(parent
, "scm");
683 supported_transport
= true;
684 supported_parent
= true;
688 parent
= udev_device_get_parent(parent
);
692 * Do not return devices with an unknown parent device type. They
693 * might produce conflicting IDs if the parent does not provide a
694 * unique and predictable name.
696 if (!supported_parent
)
700 * Do not return block devices without a well-known transport. Some
701 * devices do not expose their buses and do not provide a unique
702 * and predictable name that way.
704 if (streq(udev_device_get_subsystem(dev
), "block") && !supported_transport
)
709 char tag
[UTIL_NAME_SIZE
];
713 /* compose valid udev tag name */
714 for (p
= path
, i
= 0; *p
; p
++) {
715 if ((*p
>= '0' && *p
<= '9') ||
716 (*p
>= 'A' && *p
<= 'Z') ||
717 (*p
>= 'a' && *p
<= 'z') ||
723 /* skip all leading '_' */
727 /* avoid second '_' */
733 /* strip trailing '_' */
734 while (i
> 0 && tag
[i
-1] == '_')
738 udev_builtin_add_property(dev
, test
, "ID_PATH", path
);
739 udev_builtin_add_property(dev
, test
, "ID_PATH_TAG", tag
);
746 const struct udev_builtin udev_builtin_path_id
= {
748 .cmd
= builtin_path_id
,
749 .help
= "Compose persistent device path",