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);
72 ** Linux only supports 32 bit luns.
73 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
75 static void format_lun_number(struct udev_device
*dev
, char **path
) {
76 unsigned long lun
= strtoul(udev_device_get_sysnum(dev
), NULL
, 10);
79 /* address method 0, peripheral device addressing with bus id of zero */
80 path_prepend(path
, "lun-%lu", lun
);
82 /* handle all other lun addressing methods by using a variant of the original lun format */
83 path_prepend(path
, "lun-0x%04lx%04lx00000000", lun
& 0xffff, (lun
>> 16) & 0xffff);
86 static struct udev_device
*skip_subsystem(struct udev_device
*dev
, const char *subsys
) {
87 struct udev_device
*parent
= dev
;
93 const char *subsystem
;
95 subsystem
= udev_device_get_subsystem(parent
);
96 if (!streq_ptr(subsystem
, subsys
))
100 parent
= udev_device_get_parent(parent
);
106 static struct udev_device
*handle_scsi_fibre_channel(struct udev_device
*parent
, char **path
) {
108 struct udev_device
*targetdev
;
109 _cleanup_udev_device_unref_
struct udev_device
*fcdev
= NULL
;
111 _cleanup_free_
char *lun
= NULL
;
116 udev
= udev_device_get_udev(parent
);
118 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
122 fcdev
= udev_device_new_from_subsystem_sysname(udev
, "fc_transport", udev_device_get_sysname(targetdev
));
126 port
= udev_device_get_sysattr_value(fcdev
, "port_name");
130 format_lun_number(parent
, &lun
);
131 path_prepend(path
, "fc-%s-%s", port
, lun
);
135 static struct udev_device
*handle_scsi_sas_wide_port(struct udev_device
*parent
, char **path
) {
137 struct udev_device
*targetdev
, *target_parent
;
138 _cleanup_udev_device_unref_
struct udev_device
*sasdev
= NULL
;
139 const char *sas_address
;
140 _cleanup_free_
char *lun
= NULL
;
145 udev
= udev_device_get_udev(parent
);
147 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
151 target_parent
= udev_device_get_parent(targetdev
);
155 sasdev
= udev_device_new_from_subsystem_sysname(udev
, "sas_device",
156 udev_device_get_sysname(target_parent
));
160 sas_address
= udev_device_get_sysattr_value(sasdev
, "sas_address");
164 format_lun_number(parent
, &lun
);
165 path_prepend(path
, "sas-%s-%s", sas_address
, lun
);
169 static struct udev_device
*handle_scsi_sas(struct udev_device
*parent
, char **path
)
172 struct udev_device
*targetdev
, *target_parent
, *port
, *expander
;
173 _cleanup_udev_device_unref_
struct udev_device
174 *target_sasdev
= NULL
, *expander_sasdev
= NULL
, *port_sasdev
= NULL
;
175 const char *sas_address
= NULL
;
177 const char *phy_count
;
178 _cleanup_free_
char *lun
= NULL
;
183 udev
= udev_device_get_udev(parent
);
185 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
189 target_parent
= udev_device_get_parent(targetdev
);
194 target_sasdev
= udev_device_new_from_subsystem_sysname(
195 udev
, "sas_device", udev_device_get_sysname(target_parent
));
199 /* The next parent is sas port */
200 port
= udev_device_get_parent(target_parent
);
204 /* Get port device */
205 port_sasdev
= udev_device_new_from_subsystem_sysname(
206 udev
, "sas_port", udev_device_get_sysname(port
));
208 phy_count
= udev_device_get_sysattr_value(port_sasdev
, "num_phys");
212 /* Check if we are simple disk */
213 if (strncmp(phy_count
, "1", 2) != 0)
214 return handle_scsi_sas_wide_port(parent
, path
);
216 /* Get connected phy */
217 phy_id
= udev_device_get_sysattr_value(target_sasdev
, "phy_identifier");
221 /* The port's parent is either hba or expander */
222 expander
= udev_device_get_parent(port
);
226 /* Get expander device */
227 expander_sasdev
= udev_device_new_from_subsystem_sysname(
228 udev
, "sas_device", udev_device_get_sysname(expander
));
229 if (expander_sasdev
) {
230 /* Get expander's address */
231 sas_address
= udev_device_get_sysattr_value(expander_sasdev
,
237 format_lun_number(parent
, &lun
);
239 path_prepend(path
, "sas-exp%s-phy%s-%s", sas_address
, phy_id
, lun
);
241 path_prepend(path
, "sas-phy%s-%s", phy_id
, lun
);
246 static struct udev_device
*handle_scsi_iscsi(struct udev_device
*parent
, char **path
) {
248 struct udev_device
*transportdev
;
249 _cleanup_udev_device_unref_
struct udev_device
250 *sessiondev
= NULL
, *conndev
= NULL
;
251 const char *target
, *connname
, *addr
, *port
;
252 _cleanup_free_
char *lun
= NULL
;
257 udev
= udev_device_get_udev(parent
);
259 /* find iscsi session */
260 transportdev
= parent
;
262 transportdev
= udev_device_get_parent(transportdev
);
265 if (startswith(udev_device_get_sysname(transportdev
), "session"))
269 /* find iscsi session device */
270 sessiondev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_session", udev_device_get_sysname(transportdev
));
274 target
= udev_device_get_sysattr_value(sessiondev
, "targetname");
278 connname
= strjoina("connection", udev_device_get_sysnum(transportdev
), ":0");
279 conndev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_connection", connname
);
283 addr
= udev_device_get_sysattr_value(conndev
, "persistent_address");
284 port
= udev_device_get_sysattr_value(conndev
, "persistent_port");
288 format_lun_number(parent
, &lun
);
289 path_prepend(path
, "ip-%s:%s-iscsi-%s-%s", addr
, port
, target
, lun
);
293 static struct udev_device
*handle_scsi_ata(struct udev_device
*parent
, char **path
) {
295 struct udev_device
*targetdev
, *target_parent
;
296 _cleanup_udev_device_unref_
struct udev_device
*atadev
= NULL
;
302 udev
= udev_device_get_udev(parent
);
304 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
308 target_parent
= udev_device_get_parent(targetdev
);
312 atadev
= udev_device_new_from_subsystem_sysname(udev
, "ata_port", udev_device_get_sysname(target_parent
));
316 port_no
= udev_device_get_sysattr_value(atadev
, "port_no");
320 path_prepend(path
, "ata-%s", port_no
);
324 static struct udev_device
*handle_scsi_default(struct udev_device
*parent
, char **path
) {
325 struct udev_device
*hostdev
;
326 int host
, bus
, target
, lun
;
327 const char *name
, *base
, *pos
;
328 _cleanup_closedir_
DIR *dir
= NULL
;
335 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
339 name
= udev_device_get_sysname(parent
);
340 if (sscanf(name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4)
344 * Rebase host offset to get the local relative number
346 * Note: This is by definition racy, unreliable and too simple.
347 * Please do not copy this model anywhere. It's just a left-over
348 * from the time we had no idea how things should look like in
351 * Making assumptions about a global in-kernel counter and use
352 * that to calculate a local offset is a very broken concept. It
353 * can only work as long as things are in strict order.
355 * The kernel needs to export the instance/port number of a
356 * controller directly, without the need for rebase magic like
357 * this. Manual driver unbind/bind, parallel hotplug/unplug will
358 * get into the way of this "I hope it works" logic.
361 base
= udev_device_get_syspath(hostdev
);
362 pos
= strrchr(base
, '/');
366 base
= strndupa(base
, pos
- base
);
371 FOREACH_DIRENT_ALL(dent
, dir
, break) {
375 if (dent
->d_name
[0] == '.')
377 if (!IN_SET(dent
->d_type
, DT_DIR
, DT_LNK
))
379 if (!startswith(dent
->d_name
, "host"))
381 i
= strtoul(&dent
->d_name
[4], &rest
, 10);
385 * find the smallest number; the host really needs to export its
386 * own instance number per parent device; relying on the global host
387 * enumeration and plainly rebasing the numbers sounds unreliable
389 if (basenum
== -1 || i
< basenum
)
396 path_prepend(path
, "scsi-%u:%u:%u:%u", host
, bus
, target
, lun
);
400 static struct udev_device
*handle_scsi_hyperv(struct udev_device
*parent
, char **path
, size_t guid_str_len
) {
401 struct udev_device
*hostdev
;
402 struct udev_device
*vmbusdev
;
403 const char *guid_str
;
404 _cleanup_free_
char *lun
= NULL
;
410 assert(guid_str_len
< sizeof(guid
));
412 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
416 vmbusdev
= udev_device_get_parent(hostdev
);
420 guid_str
= udev_device_get_sysattr_value(vmbusdev
, "device_id");
424 if (strlen(guid_str
) < guid_str_len
|| guid_str
[0] != '{' || guid_str
[guid_str_len
-1] != '}')
427 for (i
= 1, k
= 0; i
< guid_str_len
-1; i
++) {
428 if (guid_str
[i
] == '-')
430 guid
[k
++] = guid_str
[i
];
434 format_lun_number(parent
, &lun
);
435 path_prepend(path
, "vmbus-%s-%s", guid
, lun
);
439 static struct udev_device
*handle_scsi(struct udev_device
*parent
, char **path
, bool *supported_parent
) {
440 const char *devtype
, *id
, *name
;
442 devtype
= udev_device_get_devtype(parent
);
443 if (!streq_ptr(devtype
, "scsi_device"))
447 id
= udev_device_get_sysattr_value(parent
, "ieee1394_id");
449 path_prepend(path
, "ieee1394-0x%s", id
);
450 *supported_parent
= true;
451 return skip_subsystem(parent
, "scsi");
454 /* scsi sysfs does not have a "subsystem" for the transport */
455 name
= udev_device_get_syspath(parent
);
457 if (strstr(name
, "/rport-")) {
458 *supported_parent
= true;
459 return handle_scsi_fibre_channel(parent
, path
);
462 if (strstr(name
, "/end_device-")) {
463 *supported_parent
= true;
464 return handle_scsi_sas(parent
, path
);
467 if (strstr(name
, "/session")) {
468 *supported_parent
= true;
469 return handle_scsi_iscsi(parent
, path
);
472 if (strstr(name
, "/ata"))
473 return handle_scsi_ata(parent
, path
);
475 if (strstr(name
, "/vmbus_"))
476 return handle_scsi_hyperv(parent
, path
, 37);
477 else if (strstr(name
, "/VMBUS"))
478 return handle_scsi_hyperv(parent
, path
, 38);
480 return handle_scsi_default(parent
, path
);
483 static struct udev_device
*handle_cciss(struct udev_device
*parent
, char **path
) {
485 unsigned int controller
, disk
;
487 str
= udev_device_get_sysname(parent
);
488 if (sscanf(str
, "c%ud%u%*s", &controller
, &disk
) != 2)
491 path_prepend(path
, "cciss-disk%u", disk
);
492 return skip_subsystem(parent
, "cciss");
495 static void handle_scsi_tape(struct udev_device
*dev
, char **path
) {
498 /* must be the last device in the syspath */
502 name
= udev_device_get_sysname(dev
);
503 if (startswith(name
, "nst") && strchr("lma", name
[3]))
504 path_prepend(path
, "nst%c", name
[3]);
505 else if (startswith(name
, "st") && strchr("lma", name
[2]))
506 path_prepend(path
, "st%c", name
[2]);
509 static struct udev_device
*handle_usb(struct udev_device
*parent
, char **path
) {
510 const char *devtype
, *str
, *port
;
512 devtype
= udev_device_get_devtype(parent
);
515 if (!STR_IN_SET(devtype
, "usb_interface", "usb_device"))
518 str
= udev_device_get_sysname(parent
);
519 port
= strchr(str
, '-');
524 path_prepend(path
, "usb-0:%s", port
);
525 return skip_subsystem(parent
, "usb");
528 static struct udev_device
*handle_bcma(struct udev_device
*parent
, char **path
) {
532 sysname
= udev_device_get_sysname(parent
);
533 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
536 path_prepend(path
, "bcma-%u", core
);
540 /* Handle devices of AP bus in System z platform. */
541 static struct udev_device
*handle_ap(struct udev_device
*parent
, char **path
) {
542 const char *type
, *func
;
547 type
= udev_device_get_sysattr_value(parent
, "type");
548 func
= udev_device_get_sysattr_value(parent
, "ap_functions");
551 path_prepend(path
, "ap-%s-%s", type
, func
);
553 path_prepend(path
, "ap-%s", udev_device_get_sysname(parent
));
555 return skip_subsystem(parent
, "ap");
558 static int builtin_path_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
559 struct udev_device
*parent
;
560 _cleanup_free_
char *path
= NULL
;
561 bool supported_transport
= false;
562 bool supported_parent
= false;
566 /* walk up the chain of devices and compose path */
571 subsys
= udev_device_get_subsystem(parent
);
574 } else if (streq(subsys
, "scsi_tape")) {
575 handle_scsi_tape(parent
, &path
);
576 } else if (streq(subsys
, "scsi")) {
577 parent
= handle_scsi(parent
, &path
, &supported_parent
);
578 supported_transport
= true;
579 } else if (streq(subsys
, "cciss")) {
580 parent
= handle_cciss(parent
, &path
);
581 supported_transport
= true;
582 } else if (streq(subsys
, "usb")) {
583 parent
= handle_usb(parent
, &path
);
584 supported_transport
= true;
585 } else if (streq(subsys
, "bcma")) {
586 parent
= handle_bcma(parent
, &path
);
587 supported_transport
= true;
588 } else if (streq(subsys
, "serio")) {
589 path_prepend(&path
, "serio-%s", udev_device_get_sysnum(parent
));
590 parent
= skip_subsystem(parent
, "serio");
591 } else if (streq(subsys
, "pci")) {
592 path_prepend(&path
, "pci-%s", udev_device_get_sysname(parent
));
593 parent
= skip_subsystem(parent
, "pci");
594 supported_parent
= true;
595 } else if (streq(subsys
, "platform")) {
596 path_prepend(&path
, "platform-%s", udev_device_get_sysname(parent
));
597 parent
= skip_subsystem(parent
, "platform");
598 supported_transport
= true;
599 supported_parent
= true;
600 } else if (streq(subsys
, "acpi")) {
601 path_prepend(&path
, "acpi-%s", udev_device_get_sysname(parent
));
602 parent
= skip_subsystem(parent
, "acpi");
603 supported_parent
= true;
604 } else if (streq(subsys
, "xen")) {
605 path_prepend(&path
, "xen-%s", udev_device_get_sysname(parent
));
606 parent
= skip_subsystem(parent
, "xen");
607 supported_parent
= true;
608 } else if (streq(subsys
, "virtio")) {
609 parent
= skip_subsystem(parent
, "virtio");
610 supported_transport
= true;
611 } else if (streq(subsys
, "scm")) {
612 path_prepend(&path
, "scm-%s", udev_device_get_sysname(parent
));
613 parent
= skip_subsystem(parent
, "scm");
614 supported_transport
= true;
615 supported_parent
= true;
616 } else if (streq(subsys
, "ccw")) {
617 path_prepend(&path
, "ccw-%s", udev_device_get_sysname(parent
));
618 parent
= skip_subsystem(parent
, "ccw");
619 supported_transport
= true;
620 supported_parent
= true;
621 } else if (streq(subsys
, "ccwgroup")) {
622 path_prepend(&path
, "ccwgroup-%s", udev_device_get_sysname(parent
));
623 parent
= skip_subsystem(parent
, "ccwgroup");
624 supported_transport
= true;
625 supported_parent
= true;
626 } else if (streq(subsys
, "ap")) {
627 parent
= handle_ap(parent
, &path
);
628 supported_transport
= true;
629 supported_parent
= true;
630 } else if (streq(subsys
, "iucv")) {
631 path_prepend(&path
, "iucv-%s", udev_device_get_sysname(parent
));
632 parent
= skip_subsystem(parent
, "iucv");
633 supported_transport
= true;
634 supported_parent
= true;
635 } else if (streq(subsys
, "nvme")) {
636 const char *nsid
= udev_device_get_sysattr_value(dev
, "nsid");
639 path_prepend(&path
, "nvme-%s", nsid
);
640 parent
= skip_subsystem(parent
, "nvme");
641 supported_parent
= true;
642 supported_transport
= true;
647 parent
= udev_device_get_parent(parent
);
654 * Do not return devices with an unknown parent device type. They
655 * might produce conflicting IDs if the parent does not provide a
656 * unique and predictable name.
658 if (!supported_parent
)
662 * Do not return block devices without a well-known transport. Some
663 * devices do not expose their buses and do not provide a unique
664 * and predictable name that way.
666 if (streq_ptr(udev_device_get_subsystem(dev
), "block") && !supported_transport
)
670 char tag
[UTIL_NAME_SIZE
];
674 /* compose valid udev tag name */
675 for (p
= path
, i
= 0; *p
; p
++) {
676 if ((*p
>= '0' && *p
<= '9') ||
677 (*p
>= 'A' && *p
<= 'Z') ||
678 (*p
>= 'a' && *p
<= 'z') ||
684 /* skip all leading '_' */
688 /* avoid second '_' */
694 /* strip trailing '_' */
695 while (i
> 0 && tag
[i
-1] == '_')
699 udev_builtin_add_property(dev
, test
, "ID_PATH", path
);
700 udev_builtin_add_property(dev
, test
, "ID_PATH_TAG", tag
);
706 const struct udev_builtin udev_builtin_path_id
= {
708 .cmd
= builtin_path_id
,
709 .help
= "Compose persistent device path",