1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * compose persistent device path
5 * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
7 * Logic based on Hannes Reinecke's shell script.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "alloc-util.h"
34 #include "dirent-util.h"
36 #include "string-util.h"
39 #include "udev-util.h"
42 static void path_prepend(char **path
, const char *fmt
, ...) {
44 _cleanup_free_
char *pre
= NULL
;
48 r
= vasprintf(&pre
, fmt
, va
);
58 new = strjoin(pre
, "-", *path
);
64 free_and_replace(*path
, new);
66 *path
= TAKE_PTR(pre
);
70 ** Linux only supports 32 bit luns.
71 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
73 static void format_lun_number(struct udev_device
*dev
, char **path
) {
74 unsigned long lun
= strtoul(udev_device_get_sysnum(dev
), NULL
, 10);
77 /* address method 0, peripheral device addressing with bus id of zero */
78 path_prepend(path
, "lun-%lu", lun
);
80 /* handle all other lun addressing methods by using a variant of the original lun format */
81 path_prepend(path
, "lun-0x%04lx%04lx00000000", lun
& 0xffff, (lun
>> 16) & 0xffff);
84 static struct udev_device
*skip_subsystem(struct udev_device
*dev
, const char *subsys
) {
85 struct udev_device
*parent
= dev
;
91 const char *subsystem
;
93 subsystem
= udev_device_get_subsystem(parent
);
94 if (!streq_ptr(subsystem
, subsys
))
98 parent
= udev_device_get_parent(parent
);
104 static struct udev_device
*handle_scsi_fibre_channel(struct udev_device
*parent
, char **path
) {
106 struct udev_device
*targetdev
;
107 _cleanup_(udev_device_unrefp
) struct udev_device
*fcdev
= NULL
;
109 _cleanup_free_
char *lun
= NULL
;
114 udev
= udev_device_get_udev(parent
);
116 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
120 fcdev
= udev_device_new_from_subsystem_sysname(udev
, "fc_transport", udev_device_get_sysname(targetdev
));
124 port
= udev_device_get_sysattr_value(fcdev
, "port_name");
128 format_lun_number(parent
, &lun
);
129 path_prepend(path
, "fc-%s-%s", port
, lun
);
133 static struct udev_device
*handle_scsi_sas_wide_port(struct udev_device
*parent
, char **path
) {
135 struct udev_device
*targetdev
, *target_parent
;
136 _cleanup_(udev_device_unrefp
) struct udev_device
*sasdev
= NULL
;
137 const char *sas_address
;
138 _cleanup_free_
char *lun
= NULL
;
143 udev
= udev_device_get_udev(parent
);
145 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
149 target_parent
= udev_device_get_parent(targetdev
);
153 sasdev
= udev_device_new_from_subsystem_sysname(udev
, "sas_device",
154 udev_device_get_sysname(target_parent
));
158 sas_address
= udev_device_get_sysattr_value(sasdev
, "sas_address");
162 format_lun_number(parent
, &lun
);
163 path_prepend(path
, "sas-%s-%s", sas_address
, lun
);
167 static struct udev_device
*handle_scsi_sas(struct udev_device
*parent
, char **path
)
170 struct udev_device
*targetdev
, *target_parent
, *port
, *expander
;
171 _cleanup_(udev_device_unrefp
) struct udev_device
172 *target_sasdev
= NULL
, *expander_sasdev
= NULL
, *port_sasdev
= NULL
;
173 const char *sas_address
= NULL
;
175 const char *phy_count
;
176 _cleanup_free_
char *lun
= NULL
;
181 udev
= udev_device_get_udev(parent
);
183 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
187 target_parent
= udev_device_get_parent(targetdev
);
192 target_sasdev
= udev_device_new_from_subsystem_sysname(
193 udev
, "sas_device", udev_device_get_sysname(target_parent
));
197 /* The next parent is sas port */
198 port
= udev_device_get_parent(target_parent
);
202 /* Get port device */
203 port_sasdev
= udev_device_new_from_subsystem_sysname(
204 udev
, "sas_port", udev_device_get_sysname(port
));
206 phy_count
= udev_device_get_sysattr_value(port_sasdev
, "num_phys");
210 /* Check if we are simple disk */
211 if (strncmp(phy_count
, "1", 2) != 0)
212 return handle_scsi_sas_wide_port(parent
, path
);
214 /* Get connected phy */
215 phy_id
= udev_device_get_sysattr_value(target_sasdev
, "phy_identifier");
219 /* The port's parent is either hba or expander */
220 expander
= udev_device_get_parent(port
);
224 /* Get expander device */
225 expander_sasdev
= udev_device_new_from_subsystem_sysname(
226 udev
, "sas_device", udev_device_get_sysname(expander
));
227 if (expander_sasdev
) {
228 /* Get expander's address */
229 sas_address
= udev_device_get_sysattr_value(expander_sasdev
,
235 format_lun_number(parent
, &lun
);
237 path_prepend(path
, "sas-exp%s-phy%s-%s", sas_address
, phy_id
, lun
);
239 path_prepend(path
, "sas-phy%s-%s", phy_id
, lun
);
244 static struct udev_device
*handle_scsi_iscsi(struct udev_device
*parent
, char **path
) {
246 struct udev_device
*transportdev
;
247 _cleanup_(udev_device_unrefp
) struct udev_device
248 *sessiondev
= NULL
, *conndev
= NULL
;
249 const char *target
, *connname
, *addr
, *port
;
250 _cleanup_free_
char *lun
= NULL
;
255 udev
= udev_device_get_udev(parent
);
257 /* find iscsi session */
258 transportdev
= parent
;
260 transportdev
= udev_device_get_parent(transportdev
);
263 if (startswith(udev_device_get_sysname(transportdev
), "session"))
267 /* find iscsi session device */
268 sessiondev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_session", udev_device_get_sysname(transportdev
));
272 target
= udev_device_get_sysattr_value(sessiondev
, "targetname");
276 connname
= strjoina("connection", udev_device_get_sysnum(transportdev
), ":0");
277 conndev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_connection", connname
);
281 addr
= udev_device_get_sysattr_value(conndev
, "persistent_address");
282 port
= udev_device_get_sysattr_value(conndev
, "persistent_port");
286 format_lun_number(parent
, &lun
);
287 path_prepend(path
, "ip-%s:%s-iscsi-%s-%s", addr
, port
, target
, lun
);
291 static struct udev_device
*handle_scsi_ata(struct udev_device
*parent
, char **path
) {
293 struct udev_device
*targetdev
, *target_parent
;
294 _cleanup_(udev_device_unrefp
) struct udev_device
*atadev
= NULL
;
300 udev
= udev_device_get_udev(parent
);
302 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
306 target_parent
= udev_device_get_parent(targetdev
);
310 atadev
= udev_device_new_from_subsystem_sysname(udev
, "ata_port", udev_device_get_sysname(target_parent
));
314 port_no
= udev_device_get_sysattr_value(atadev
, "port_no");
318 path_prepend(path
, "ata-%s", port_no
);
322 static struct udev_device
*handle_scsi_default(struct udev_device
*parent
, char **path
) {
323 struct udev_device
*hostdev
;
324 int host
, bus
, target
, lun
;
325 const char *name
, *base
, *pos
;
326 _cleanup_closedir_
DIR *dir
= NULL
;
333 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
337 name
= udev_device_get_sysname(parent
);
338 if (sscanf(name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4)
342 * Rebase host offset to get the local relative number
344 * Note: This is by definition racy, unreliable and too simple.
345 * Please do not copy this model anywhere. It's just a left-over
346 * from the time we had no idea how things should look like in
349 * Making assumptions about a global in-kernel counter and use
350 * that to calculate a local offset is a very broken concept. It
351 * can only work as long as things are in strict order.
353 * The kernel needs to export the instance/port number of a
354 * controller directly, without the need for rebase magic like
355 * this. Manual driver unbind/bind, parallel hotplug/unplug will
356 * get into the way of this "I hope it works" logic.
359 base
= udev_device_get_syspath(hostdev
);
360 pos
= strrchr(base
, '/');
364 base
= strndupa(base
, pos
- base
);
369 FOREACH_DIRENT_ALL(dent
, dir
, break) {
373 if (dent
->d_name
[0] == '.')
375 if (!IN_SET(dent
->d_type
, DT_DIR
, DT_LNK
))
377 if (!startswith(dent
->d_name
, "host"))
379 i
= strtoul(&dent
->d_name
[4], &rest
, 10);
383 * find the smallest number; the host really needs to export its
384 * own instance number per parent device; relying on the global host
385 * enumeration and plainly rebasing the numbers sounds unreliable
387 if (basenum
== -1 || i
< basenum
)
394 path_prepend(path
, "scsi-%u:%u:%u:%u", host
, bus
, target
, lun
);
398 static struct udev_device
*handle_scsi_hyperv(struct udev_device
*parent
, char **path
, size_t guid_str_len
) {
399 struct udev_device
*hostdev
;
400 struct udev_device
*vmbusdev
;
401 const char *guid_str
;
402 _cleanup_free_
char *lun
= NULL
;
408 assert(guid_str_len
< sizeof(guid
));
410 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
414 vmbusdev
= udev_device_get_parent(hostdev
);
418 guid_str
= udev_device_get_sysattr_value(vmbusdev
, "device_id");
422 if (strlen(guid_str
) < guid_str_len
|| guid_str
[0] != '{' || guid_str
[guid_str_len
-1] != '}')
425 for (i
= 1, k
= 0; i
< guid_str_len
-1; i
++) {
426 if (guid_str
[i
] == '-')
428 guid
[k
++] = guid_str
[i
];
432 format_lun_number(parent
, &lun
);
433 path_prepend(path
, "vmbus-%s-%s", guid
, lun
);
437 static struct udev_device
*handle_scsi(struct udev_device
*parent
, char **path
, bool *supported_parent
) {
438 const char *devtype
, *id
, *name
;
440 devtype
= udev_device_get_devtype(parent
);
441 if (!streq_ptr(devtype
, "scsi_device"))
445 id
= udev_device_get_sysattr_value(parent
, "ieee1394_id");
447 path_prepend(path
, "ieee1394-0x%s", id
);
448 *supported_parent
= true;
449 return skip_subsystem(parent
, "scsi");
452 /* scsi sysfs does not have a "subsystem" for the transport */
453 name
= udev_device_get_syspath(parent
);
455 if (strstr(name
, "/rport-")) {
456 *supported_parent
= true;
457 return handle_scsi_fibre_channel(parent
, path
);
460 if (strstr(name
, "/end_device-")) {
461 *supported_parent
= true;
462 return handle_scsi_sas(parent
, path
);
465 if (strstr(name
, "/session")) {
466 *supported_parent
= true;
467 return handle_scsi_iscsi(parent
, path
);
470 if (strstr(name
, "/ata"))
471 return handle_scsi_ata(parent
, path
);
473 if (strstr(name
, "/vmbus_"))
474 return handle_scsi_hyperv(parent
, path
, 37);
475 else if (strstr(name
, "/VMBUS"))
476 return handle_scsi_hyperv(parent
, path
, 38);
478 return handle_scsi_default(parent
, path
);
481 static struct udev_device
*handle_cciss(struct udev_device
*parent
, char **path
) {
483 unsigned int controller
, disk
;
485 str
= udev_device_get_sysname(parent
);
486 if (sscanf(str
, "c%ud%u%*s", &controller
, &disk
) != 2)
489 path_prepend(path
, "cciss-disk%u", disk
);
490 return skip_subsystem(parent
, "cciss");
493 static void handle_scsi_tape(struct udev_device
*dev
, char **path
) {
496 /* must be the last device in the syspath */
500 name
= udev_device_get_sysname(dev
);
501 if (startswith(name
, "nst") && strchr("lma", name
[3]))
502 path_prepend(path
, "nst%c", name
[3]);
503 else if (startswith(name
, "st") && strchr("lma", name
[2]))
504 path_prepend(path
, "st%c", name
[2]);
507 static struct udev_device
*handle_usb(struct udev_device
*parent
, char **path
) {
508 const char *devtype
, *str
, *port
;
510 devtype
= udev_device_get_devtype(parent
);
513 if (!STR_IN_SET(devtype
, "usb_interface", "usb_device"))
516 str
= udev_device_get_sysname(parent
);
517 port
= strchr(str
, '-');
522 path_prepend(path
, "usb-0:%s", port
);
523 return skip_subsystem(parent
, "usb");
526 static struct udev_device
*handle_bcma(struct udev_device
*parent
, char **path
) {
530 sysname
= udev_device_get_sysname(parent
);
531 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
534 path_prepend(path
, "bcma-%u", core
);
538 /* Handle devices of AP bus in System z platform. */
539 static struct udev_device
*handle_ap(struct udev_device
*parent
, char **path
) {
540 const char *type
, *func
;
545 type
= udev_device_get_sysattr_value(parent
, "type");
546 func
= udev_device_get_sysattr_value(parent
, "ap_functions");
549 path_prepend(path
, "ap-%s-%s", type
, func
);
551 path_prepend(path
, "ap-%s", udev_device_get_sysname(parent
));
553 return skip_subsystem(parent
, "ap");
556 static int builtin_path_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
557 struct udev_device
*parent
;
558 _cleanup_free_
char *path
= NULL
;
559 bool supported_transport
= false;
560 bool supported_parent
= false;
564 /* walk up the chain of devices and compose path */
569 subsys
= udev_device_get_subsystem(parent
);
572 } else if (streq(subsys
, "scsi_tape")) {
573 handle_scsi_tape(parent
, &path
);
574 } else if (streq(subsys
, "scsi")) {
575 parent
= handle_scsi(parent
, &path
, &supported_parent
);
576 supported_transport
= true;
577 } else if (streq(subsys
, "cciss")) {
578 parent
= handle_cciss(parent
, &path
);
579 supported_transport
= true;
580 } else if (streq(subsys
, "usb")) {
581 parent
= handle_usb(parent
, &path
);
582 supported_transport
= true;
583 } else if (streq(subsys
, "bcma")) {
584 parent
= handle_bcma(parent
, &path
);
585 supported_transport
= true;
586 } else if (streq(subsys
, "serio")) {
587 path_prepend(&path
, "serio-%s", udev_device_get_sysnum(parent
));
588 parent
= skip_subsystem(parent
, "serio");
589 } else if (streq(subsys
, "pci")) {
590 path_prepend(&path
, "pci-%s", udev_device_get_sysname(parent
));
591 parent
= skip_subsystem(parent
, "pci");
592 supported_parent
= true;
593 } else if (streq(subsys
, "platform")) {
594 path_prepend(&path
, "platform-%s", udev_device_get_sysname(parent
));
595 parent
= skip_subsystem(parent
, "platform");
596 supported_transport
= true;
597 supported_parent
= true;
598 } else if (streq(subsys
, "acpi")) {
599 path_prepend(&path
, "acpi-%s", udev_device_get_sysname(parent
));
600 parent
= skip_subsystem(parent
, "acpi");
601 supported_parent
= true;
602 } else if (streq(subsys
, "xen")) {
603 path_prepend(&path
, "xen-%s", udev_device_get_sysname(parent
));
604 parent
= skip_subsystem(parent
, "xen");
605 supported_parent
= true;
606 } else if (streq(subsys
, "virtio")) {
607 parent
= skip_subsystem(parent
, "virtio");
608 supported_transport
= true;
609 } else if (streq(subsys
, "scm")) {
610 path_prepend(&path
, "scm-%s", udev_device_get_sysname(parent
));
611 parent
= skip_subsystem(parent
, "scm");
612 supported_transport
= true;
613 supported_parent
= true;
614 } else if (streq(subsys
, "ccw")) {
615 path_prepend(&path
, "ccw-%s", udev_device_get_sysname(parent
));
616 parent
= skip_subsystem(parent
, "ccw");
617 supported_transport
= true;
618 supported_parent
= true;
619 } else if (streq(subsys
, "ccwgroup")) {
620 path_prepend(&path
, "ccwgroup-%s", udev_device_get_sysname(parent
));
621 parent
= skip_subsystem(parent
, "ccwgroup");
622 supported_transport
= true;
623 supported_parent
= true;
624 } else if (streq(subsys
, "ap")) {
625 parent
= handle_ap(parent
, &path
);
626 supported_transport
= true;
627 supported_parent
= true;
628 } else if (streq(subsys
, "iucv")) {
629 path_prepend(&path
, "iucv-%s", udev_device_get_sysname(parent
));
630 parent
= skip_subsystem(parent
, "iucv");
631 supported_transport
= true;
632 supported_parent
= true;
633 } else if (streq(subsys
, "nvme")) {
634 const char *nsid
= udev_device_get_sysattr_value(dev
, "nsid");
637 path_prepend(&path
, "nvme-%s", nsid
);
638 parent
= skip_subsystem(parent
, "nvme");
639 supported_parent
= true;
640 supported_transport
= true;
645 parent
= udev_device_get_parent(parent
);
652 * Do not return devices with an unknown parent device type. They
653 * might produce conflicting IDs if the parent does not provide a
654 * unique and predictable name.
656 if (!supported_parent
)
660 * Do not return block devices without a well-known transport. Some
661 * devices do not expose their buses and do not provide a unique
662 * and predictable name that way.
664 if (streq_ptr(udev_device_get_subsystem(dev
), "block") && !supported_transport
)
668 char tag
[UTIL_NAME_SIZE
];
672 /* compose valid udev tag name */
673 for (p
= path
, i
= 0; *p
; p
++) {
674 if ((*p
>= '0' && *p
<= '9') ||
675 (*p
>= 'A' && *p
<= 'Z') ||
676 (*p
>= 'a' && *p
<= 'z') ||
682 /* skip all leading '_' */
686 /* avoid second '_' */
692 /* strip trailing '_' */
693 while (i
> 0 && tag
[i
-1] == '_')
697 udev_builtin_add_property(dev
, test
, "ID_PATH", path
);
698 udev_builtin_add_property(dev
, test
, "ID_PATH_TAG", tag
);
704 const struct udev_builtin udev_builtin_path_id
= {
706 .cmd
= builtin_path_id
,
707 .help
= "Compose persistent device path",