1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * compose persistent device path
5 * Logic based on Hannes Reinecke's shell script.
18 #include "alloc-util.h"
19 #include "dirent-util.h"
21 #include "libudev-private.h"
22 #include "string-util.h"
25 #include "udev-builtin.h"
28 static void path_prepend(char **path
, const char *fmt
, ...) {
30 _cleanup_free_
char *pre
= NULL
;
34 r
= vasprintf(&pre
, fmt
, va
);
44 new = strjoin(pre
, "-", *path
);
50 free_and_replace(*path
, new);
52 *path
= TAKE_PTR(pre
);
56 ** Linux only supports 32 bit luns.
57 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
59 static void format_lun_number(struct udev_device
*dev
, char **path
) {
60 unsigned long lun
= strtoul(udev_device_get_sysnum(dev
), NULL
, 10);
63 /* address method 0, peripheral device addressing with bus id of zero */
64 path_prepend(path
, "lun-%lu", lun
);
66 /* handle all other lun addressing methods by using a variant of the original lun format */
67 path_prepend(path
, "lun-0x%04lx%04lx00000000", lun
& 0xffff, (lun
>> 16) & 0xffff);
70 static struct udev_device
*skip_subsystem(struct udev_device
*dev
, const char *subsys
) {
71 struct udev_device
*parent
= dev
;
77 const char *subsystem
;
79 subsystem
= udev_device_get_subsystem(parent
);
80 if (!streq_ptr(subsystem
, subsys
))
84 parent
= udev_device_get_parent(parent
);
90 static struct udev_device
*handle_scsi_fibre_channel(struct udev_device
*parent
, char **path
) {
91 struct udev_device
*targetdev
;
92 _cleanup_(udev_device_unrefp
) struct udev_device
*fcdev
= NULL
;
94 _cleanup_free_
char *lun
= NULL
;
100 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
104 fcdev
= udev_device_new_from_subsystem_sysname(NULL
, "fc_transport", udev_device_get_sysname(targetdev
));
108 port
= udev_device_get_sysattr_value(fcdev
, "port_name");
112 format_lun_number(parent
, &lun
);
113 path_prepend(path
, "fc-%s-%s", port
, lun
);
117 static struct udev_device
*handle_scsi_sas_wide_port(struct udev_device
*parent
, char **path
) {
118 struct udev_device
*targetdev
, *target_parent
;
119 _cleanup_(udev_device_unrefp
) struct udev_device
*sasdev
= NULL
;
120 const char *sas_address
;
121 _cleanup_free_
char *lun
= NULL
;
127 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
131 target_parent
= udev_device_get_parent(targetdev
);
135 sasdev
= udev_device_new_from_subsystem_sysname(NULL
, "sas_device",
136 udev_device_get_sysname(target_parent
));
140 sas_address
= udev_device_get_sysattr_value(sasdev
, "sas_address");
144 format_lun_number(parent
, &lun
);
145 path_prepend(path
, "sas-%s-%s", sas_address
, lun
);
149 static struct udev_device
*handle_scsi_sas(struct udev_device
*parent
, char **path
)
151 struct udev_device
*targetdev
, *target_parent
, *port
, *expander
;
152 _cleanup_(udev_device_unrefp
) struct udev_device
153 *target_sasdev
= NULL
, *expander_sasdev
= NULL
, *port_sasdev
= NULL
;
154 const char *sas_address
= NULL
;
156 const char *phy_count
;
157 _cleanup_free_
char *lun
= NULL
;
163 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
167 target_parent
= udev_device_get_parent(targetdev
);
172 target_sasdev
= udev_device_new_from_subsystem_sysname(NULL
, "sas_device", udev_device_get_sysname(target_parent
));
176 /* The next parent is sas port */
177 port
= udev_device_get_parent(target_parent
);
181 /* Get port device */
182 port_sasdev
= udev_device_new_from_subsystem_sysname(NULL
, "sas_port", udev_device_get_sysname(port
));
184 phy_count
= udev_device_get_sysattr_value(port_sasdev
, "num_phys");
188 /* Check if we are simple disk */
189 if (strncmp(phy_count
, "1", 2) != 0)
190 return handle_scsi_sas_wide_port(parent
, path
);
192 /* Get connected phy */
193 phy_id
= udev_device_get_sysattr_value(target_sasdev
, "phy_identifier");
197 /* The port's parent is either hba or expander */
198 expander
= udev_device_get_parent(port
);
202 /* Get expander device */
203 expander_sasdev
= udev_device_new_from_subsystem_sysname(NULL
, "sas_device", udev_device_get_sysname(expander
));
204 if (expander_sasdev
) {
205 /* Get expander's address */
206 sas_address
= udev_device_get_sysattr_value(expander_sasdev
, "sas_address");
211 format_lun_number(parent
, &lun
);
213 path_prepend(path
, "sas-exp%s-phy%s-%s", sas_address
, phy_id
, lun
);
215 path_prepend(path
, "sas-phy%s-%s", phy_id
, lun
);
220 static struct udev_device
*handle_scsi_iscsi(struct udev_device
*parent
, char **path
) {
221 struct udev_device
*transportdev
;
222 _cleanup_(udev_device_unrefp
) struct udev_device
223 *sessiondev
= NULL
, *conndev
= NULL
;
224 const char *target
, *connname
, *addr
, *port
;
225 _cleanup_free_
char *lun
= NULL
;
231 /* find iscsi session */
232 transportdev
= parent
;
234 transportdev
= udev_device_get_parent(transportdev
);
237 if (startswith(udev_device_get_sysname(transportdev
), "session"))
241 /* find iscsi session device */
242 sessiondev
= udev_device_new_from_subsystem_sysname(NULL
, "iscsi_session", udev_device_get_sysname(transportdev
));
246 target
= udev_device_get_sysattr_value(sessiondev
, "targetname");
250 connname
= strjoina("connection", udev_device_get_sysnum(transportdev
), ":0");
251 conndev
= udev_device_new_from_subsystem_sysname(NULL
, "iscsi_connection", connname
);
255 addr
= udev_device_get_sysattr_value(conndev
, "persistent_address");
256 port
= udev_device_get_sysattr_value(conndev
, "persistent_port");
260 format_lun_number(parent
, &lun
);
261 path_prepend(path
, "ip-%s:%s-iscsi-%s-%s", addr
, port
, target
, lun
);
265 static struct udev_device
*handle_scsi_ata(struct udev_device
*parent
, char **path
) {
266 struct udev_device
*targetdev
, *target_parent
;
267 _cleanup_(udev_device_unrefp
) struct udev_device
*atadev
= NULL
;
274 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
278 target_parent
= udev_device_get_parent(targetdev
);
282 atadev
= udev_device_new_from_subsystem_sysname(NULL
, "ata_port", udev_device_get_sysname(target_parent
));
286 port_no
= udev_device_get_sysattr_value(atadev
, "port_no");
290 path_prepend(path
, "ata-%s", port_no
);
294 static struct udev_device
*handle_scsi_default(struct udev_device
*parent
, char **path
) {
295 struct udev_device
*hostdev
;
296 int host
, bus
, target
, lun
;
297 const char *name
, *base
, *pos
;
298 _cleanup_closedir_
DIR *dir
= NULL
;
305 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
309 name
= udev_device_get_sysname(parent
);
310 if (sscanf(name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4)
314 * Rebase host offset to get the local relative number
316 * Note: This is by definition racy, unreliable and too simple.
317 * Please do not copy this model anywhere. It's just a left-over
318 * from the time we had no idea how things should look like in
321 * Making assumptions about a global in-kernel counter and use
322 * that to calculate a local offset is a very broken concept. It
323 * can only work as long as things are in strict order.
325 * The kernel needs to export the instance/port number of a
326 * controller directly, without the need for rebase magic like
327 * this. Manual driver unbind/bind, parallel hotplug/unplug will
328 * get into the way of this "I hope it works" logic.
331 base
= udev_device_get_syspath(hostdev
);
332 pos
= strrchr(base
, '/');
336 base
= strndupa(base
, pos
- base
);
341 FOREACH_DIRENT_ALL(dent
, dir
, break) {
345 if (dent
->d_name
[0] == '.')
347 if (!IN_SET(dent
->d_type
, DT_DIR
, DT_LNK
))
349 if (!startswith(dent
->d_name
, "host"))
351 i
= strtoul(&dent
->d_name
[4], &rest
, 10);
355 * find the smallest number; the host really needs to export its
356 * own instance number per parent device; relying on the global host
357 * enumeration and plainly rebasing the numbers sounds unreliable
359 if (basenum
== -1 || i
< basenum
)
366 path_prepend(path
, "scsi-%u:%u:%u:%u", host
, bus
, target
, lun
);
370 static struct udev_device
*handle_scsi_hyperv(struct udev_device
*parent
, char **path
, size_t guid_str_len
) {
371 struct udev_device
*hostdev
;
372 struct udev_device
*vmbusdev
;
373 const char *guid_str
;
374 _cleanup_free_
char *lun
= NULL
;
380 assert(guid_str_len
< sizeof(guid
));
382 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
386 vmbusdev
= udev_device_get_parent(hostdev
);
390 guid_str
= udev_device_get_sysattr_value(vmbusdev
, "device_id");
394 if (strlen(guid_str
) < guid_str_len
|| guid_str
[0] != '{' || guid_str
[guid_str_len
-1] != '}')
397 for (i
= 1, k
= 0; i
< guid_str_len
-1; i
++) {
398 if (guid_str
[i
] == '-')
400 guid
[k
++] = guid_str
[i
];
404 format_lun_number(parent
, &lun
);
405 path_prepend(path
, "vmbus-%s-%s", guid
, lun
);
409 static struct udev_device
*handle_scsi(struct udev_device
*parent
, char **path
, bool *supported_parent
) {
410 const char *devtype
, *id
, *name
;
412 devtype
= udev_device_get_devtype(parent
);
413 if (!streq_ptr(devtype
, "scsi_device"))
417 id
= udev_device_get_sysattr_value(parent
, "ieee1394_id");
419 path_prepend(path
, "ieee1394-0x%s", id
);
420 *supported_parent
= true;
421 return skip_subsystem(parent
, "scsi");
424 /* scsi sysfs does not have a "subsystem" for the transport */
425 name
= udev_device_get_syspath(parent
);
427 if (strstr(name
, "/rport-")) {
428 *supported_parent
= true;
429 return handle_scsi_fibre_channel(parent
, path
);
432 if (strstr(name
, "/end_device-")) {
433 *supported_parent
= true;
434 return handle_scsi_sas(parent
, path
);
437 if (strstr(name
, "/session")) {
438 *supported_parent
= true;
439 return handle_scsi_iscsi(parent
, path
);
442 if (strstr(name
, "/ata"))
443 return handle_scsi_ata(parent
, path
);
445 if (strstr(name
, "/vmbus_"))
446 return handle_scsi_hyperv(parent
, path
, 37);
447 else if (strstr(name
, "/VMBUS"))
448 return handle_scsi_hyperv(parent
, path
, 38);
450 return handle_scsi_default(parent
, path
);
453 static struct udev_device
*handle_cciss(struct udev_device
*parent
, char **path
) {
455 unsigned controller
, disk
;
457 str
= udev_device_get_sysname(parent
);
458 if (sscanf(str
, "c%ud%u%*s", &controller
, &disk
) != 2)
461 path_prepend(path
, "cciss-disk%u", disk
);
462 return skip_subsystem(parent
, "cciss");
465 static void handle_scsi_tape(struct udev_device
*dev
, char **path
) {
468 /* must be the last device in the syspath */
472 name
= udev_device_get_sysname(dev
);
473 if (startswith(name
, "nst") && strchr("lma", name
[3]))
474 path_prepend(path
, "nst%c", name
[3]);
475 else if (startswith(name
, "st") && strchr("lma", name
[2]))
476 path_prepend(path
, "st%c", name
[2]);
479 static struct udev_device
*handle_usb(struct udev_device
*parent
, char **path
) {
480 const char *devtype
, *str
, *port
;
482 devtype
= udev_device_get_devtype(parent
);
485 if (!STR_IN_SET(devtype
, "usb_interface", "usb_device"))
488 str
= udev_device_get_sysname(parent
);
489 port
= strchr(str
, '-');
494 path_prepend(path
, "usb-0:%s", port
);
495 return skip_subsystem(parent
, "usb");
498 static struct udev_device
*handle_bcma(struct udev_device
*parent
, char **path
) {
502 sysname
= udev_device_get_sysname(parent
);
503 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
506 path_prepend(path
, "bcma-%u", core
);
510 /* Handle devices of AP bus in System z platform. */
511 static struct udev_device
*handle_ap(struct udev_device
*parent
, char **path
) {
512 const char *type
, *func
;
517 type
= udev_device_get_sysattr_value(parent
, "type");
518 func
= udev_device_get_sysattr_value(parent
, "ap_functions");
521 path_prepend(path
, "ap-%s-%s", type
, func
);
523 path_prepend(path
, "ap-%s", udev_device_get_sysname(parent
));
525 return skip_subsystem(parent
, "ap");
528 static int builtin_path_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
529 struct udev_device
*parent
;
530 _cleanup_free_
char *path
= NULL
;
531 bool supported_transport
= false;
532 bool supported_parent
= false;
536 /* walk up the chain of devices and compose path */
541 subsys
= udev_device_get_subsystem(parent
);
544 } else if (streq(subsys
, "scsi_tape")) {
545 handle_scsi_tape(parent
, &path
);
546 } else if (streq(subsys
, "scsi")) {
547 parent
= handle_scsi(parent
, &path
, &supported_parent
);
548 supported_transport
= true;
549 } else if (streq(subsys
, "cciss")) {
550 parent
= handle_cciss(parent
, &path
);
551 supported_transport
= true;
552 } else if (streq(subsys
, "usb")) {
553 parent
= handle_usb(parent
, &path
);
554 supported_transport
= true;
555 } else if (streq(subsys
, "bcma")) {
556 parent
= handle_bcma(parent
, &path
);
557 supported_transport
= true;
558 } else if (streq(subsys
, "serio")) {
559 path_prepend(&path
, "serio-%s", udev_device_get_sysnum(parent
));
560 parent
= skip_subsystem(parent
, "serio");
561 } else if (streq(subsys
, "pci")) {
562 path_prepend(&path
, "pci-%s", udev_device_get_sysname(parent
));
563 parent
= skip_subsystem(parent
, "pci");
564 supported_parent
= true;
565 } else if (streq(subsys
, "platform")) {
566 path_prepend(&path
, "platform-%s", udev_device_get_sysname(parent
));
567 parent
= skip_subsystem(parent
, "platform");
568 supported_transport
= true;
569 supported_parent
= true;
570 } else if (streq(subsys
, "acpi")) {
571 path_prepend(&path
, "acpi-%s", udev_device_get_sysname(parent
));
572 parent
= skip_subsystem(parent
, "acpi");
573 supported_parent
= true;
574 } else if (streq(subsys
, "xen")) {
575 path_prepend(&path
, "xen-%s", udev_device_get_sysname(parent
));
576 parent
= skip_subsystem(parent
, "xen");
577 supported_parent
= true;
578 } else if (streq(subsys
, "virtio")) {
579 parent
= skip_subsystem(parent
, "virtio");
580 supported_transport
= true;
581 } else if (streq(subsys
, "scm")) {
582 path_prepend(&path
, "scm-%s", udev_device_get_sysname(parent
));
583 parent
= skip_subsystem(parent
, "scm");
584 supported_transport
= true;
585 supported_parent
= true;
586 } else if (streq(subsys
, "ccw")) {
587 path_prepend(&path
, "ccw-%s", udev_device_get_sysname(parent
));
588 parent
= skip_subsystem(parent
, "ccw");
589 supported_transport
= true;
590 supported_parent
= true;
591 } else if (streq(subsys
, "ccwgroup")) {
592 path_prepend(&path
, "ccwgroup-%s", udev_device_get_sysname(parent
));
593 parent
= skip_subsystem(parent
, "ccwgroup");
594 supported_transport
= true;
595 supported_parent
= true;
596 } else if (streq(subsys
, "ap")) {
597 parent
= handle_ap(parent
, &path
);
598 supported_transport
= true;
599 supported_parent
= true;
600 } else if (streq(subsys
, "iucv")) {
601 path_prepend(&path
, "iucv-%s", udev_device_get_sysname(parent
));
602 parent
= skip_subsystem(parent
, "iucv");
603 supported_transport
= true;
604 supported_parent
= true;
605 } else if (streq(subsys
, "nvme")) {
606 const char *nsid
= udev_device_get_sysattr_value(dev
, "nsid");
609 path_prepend(&path
, "nvme-%s", nsid
);
610 parent
= skip_subsystem(parent
, "nvme");
611 supported_parent
= true;
612 supported_transport
= true;
617 parent
= udev_device_get_parent(parent
);
624 * Do not return devices with an unknown parent device type. They
625 * might produce conflicting IDs if the parent does not provide a
626 * unique and predictable name.
628 if (!supported_parent
)
632 * Do not return block devices without a well-known transport. Some
633 * devices do not expose their buses and do not provide a unique
634 * and predictable name that way.
636 if (streq_ptr(udev_device_get_subsystem(dev
), "block") && !supported_transport
)
640 char tag
[UTIL_NAME_SIZE
];
644 /* compose valid udev tag name */
645 for (p
= path
, i
= 0; *p
; p
++) {
646 if ((*p
>= '0' && *p
<= '9') ||
647 (*p
>= 'A' && *p
<= 'Z') ||
648 (*p
>= 'a' && *p
<= 'z') ||
654 /* skip all leading '_' */
658 /* avoid second '_' */
664 /* strip trailing '_' */
665 while (i
> 0 && tag
[i
-1] == '_')
669 udev_builtin_add_property(dev
, test
, "ID_PATH", path
);
670 udev_builtin_add_property(dev
, test
, "ID_PATH_TAG", tag
);
676 const struct udev_builtin udev_builtin_path_id
= {
678 .cmd
= builtin_path_id
,
679 .help
= "Compose persistent device path",