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
) {
401 struct udev_device
*hostdev
;
402 struct udev_device
*vmbusdev
;
403 const char *guid_str
;
404 _cleanup_free_
char *lun
= NULL
;
411 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
415 vmbusdev
= udev_device_get_parent(hostdev
);
419 guid_str
= udev_device_get_sysattr_value(vmbusdev
, "device_id");
423 if (strlen(guid_str
) < 37 || guid_str
[0] != '{' || guid_str
[36] != '}')
426 for (i
= 1, k
= 0; i
< 36; i
++) {
427 if (guid_str
[i
] == '-')
429 guid
[k
++] = guid_str
[i
];
433 format_lun_number(parent
, &lun
);
434 path_prepend(path
, "vmbus-%s-%s", guid
, lun
);
438 static struct udev_device
*handle_scsi(struct udev_device
*parent
, char **path
, bool *supported_parent
) {
439 const char *devtype
, *id
, *name
;
441 devtype
= udev_device_get_devtype(parent
);
442 if (!streq_ptr(devtype
, "scsi_device"))
446 id
= udev_device_get_sysattr_value(parent
, "ieee1394_id");
448 path_prepend(path
, "ieee1394-0x%s", id
);
449 *supported_parent
= true;
450 return skip_subsystem(parent
, "scsi");
453 /* scsi sysfs does not have a "subsystem" for the transport */
454 name
= udev_device_get_syspath(parent
);
456 if (strstr(name
, "/rport-")) {
457 *supported_parent
= true;
458 return handle_scsi_fibre_channel(parent
, path
);
461 if (strstr(name
, "/end_device-")) {
462 *supported_parent
= true;
463 return handle_scsi_sas(parent
, path
);
466 if (strstr(name
, "/session")) {
467 *supported_parent
= true;
468 return handle_scsi_iscsi(parent
, path
);
471 if (strstr(name
, "/ata"))
472 return handle_scsi_ata(parent
, path
);
474 if (strstr(name
, "/vmbus_"))
475 return handle_scsi_hyperv(parent
, path
);
477 return handle_scsi_default(parent
, path
);
480 static struct udev_device
*handle_cciss(struct udev_device
*parent
, char **path
) {
482 unsigned int controller
, disk
;
484 str
= udev_device_get_sysname(parent
);
485 if (sscanf(str
, "c%ud%u%*s", &controller
, &disk
) != 2)
488 path_prepend(path
, "cciss-disk%u", disk
);
489 return skip_subsystem(parent
, "cciss");
492 static void handle_scsi_tape(struct udev_device
*dev
, char **path
) {
495 /* must be the last device in the syspath */
499 name
= udev_device_get_sysname(dev
);
500 if (startswith(name
, "nst") && strchr("lma", name
[3]))
501 path_prepend(path
, "nst%c", name
[3]);
502 else if (startswith(name
, "st") && strchr("lma", name
[2]))
503 path_prepend(path
, "st%c", name
[2]);
506 static struct udev_device
*handle_usb(struct udev_device
*parent
, char **path
) {
507 const char *devtype
, *str
, *port
;
509 devtype
= udev_device_get_devtype(parent
);
512 if (!STR_IN_SET(devtype
, "usb_interface", "usb_device"))
515 str
= udev_device_get_sysname(parent
);
516 port
= strchr(str
, '-');
521 path_prepend(path
, "usb-0:%s", port
);
522 return skip_subsystem(parent
, "usb");
525 static struct udev_device
*handle_bcma(struct udev_device
*parent
, char **path
) {
529 sysname
= udev_device_get_sysname(parent
);
530 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
533 path_prepend(path
, "bcma-%u", core
);
537 /* Handle devices of AP bus in System z platform. */
538 static struct udev_device
*handle_ap(struct udev_device
*parent
, char **path
) {
539 const char *type
, *func
;
544 type
= udev_device_get_sysattr_value(parent
, "type");
545 func
= udev_device_get_sysattr_value(parent
, "ap_functions");
548 path_prepend(path
, "ap-%s-%s", type
, func
);
550 path_prepend(path
, "ap-%s", udev_device_get_sysname(parent
));
552 return skip_subsystem(parent
, "ap");
555 static int builtin_path_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
556 struct udev_device
*parent
;
557 _cleanup_free_
char *path
= NULL
;
558 bool supported_transport
= false;
559 bool supported_parent
= false;
563 /* walk up the chain of devices and compose path */
568 subsys
= udev_device_get_subsystem(parent
);
571 } else if (streq(subsys
, "scsi_tape")) {
572 handle_scsi_tape(parent
, &path
);
573 } else if (streq(subsys
, "scsi")) {
574 parent
= handle_scsi(parent
, &path
, &supported_parent
);
575 supported_transport
= true;
576 } else if (streq(subsys
, "cciss")) {
577 parent
= handle_cciss(parent
, &path
);
578 supported_transport
= true;
579 } else if (streq(subsys
, "usb")) {
580 parent
= handle_usb(parent
, &path
);
581 supported_transport
= true;
582 } else if (streq(subsys
, "bcma")) {
583 parent
= handle_bcma(parent
, &path
);
584 supported_transport
= true;
585 } else if (streq(subsys
, "serio")) {
586 path_prepend(&path
, "serio-%s", udev_device_get_sysnum(parent
));
587 parent
= skip_subsystem(parent
, "serio");
588 } else if (streq(subsys
, "pci")) {
589 path_prepend(&path
, "pci-%s", udev_device_get_sysname(parent
));
590 parent
= skip_subsystem(parent
, "pci");
591 supported_parent
= true;
592 } else if (streq(subsys
, "platform")) {
593 path_prepend(&path
, "platform-%s", udev_device_get_sysname(parent
));
594 parent
= skip_subsystem(parent
, "platform");
595 supported_transport
= true;
596 supported_parent
= true;
597 } else if (streq(subsys
, "acpi")) {
598 path_prepend(&path
, "acpi-%s", udev_device_get_sysname(parent
));
599 parent
= skip_subsystem(parent
, "acpi");
600 supported_parent
= true;
601 } else if (streq(subsys
, "xen")) {
602 path_prepend(&path
, "xen-%s", udev_device_get_sysname(parent
));
603 parent
= skip_subsystem(parent
, "xen");
604 supported_parent
= true;
605 } else if (streq(subsys
, "virtio")) {
606 parent
= skip_subsystem(parent
, "virtio");
607 supported_transport
= true;
608 } else if (streq(subsys
, "scm")) {
609 path_prepend(&path
, "scm-%s", udev_device_get_sysname(parent
));
610 parent
= skip_subsystem(parent
, "scm");
611 supported_transport
= true;
612 supported_parent
= true;
613 } else if (streq(subsys
, "ccw")) {
614 path_prepend(&path
, "ccw-%s", udev_device_get_sysname(parent
));
615 parent
= skip_subsystem(parent
, "ccw");
616 supported_transport
= true;
617 supported_parent
= true;
618 } else if (streq(subsys
, "ccwgroup")) {
619 path_prepend(&path
, "ccwgroup-%s", udev_device_get_sysname(parent
));
620 parent
= skip_subsystem(parent
, "ccwgroup");
621 supported_transport
= true;
622 supported_parent
= true;
623 } else if (streq(subsys
, "ap")) {
624 parent
= handle_ap(parent
, &path
);
625 supported_transport
= true;
626 supported_parent
= true;
627 } else if (streq(subsys
, "iucv")) {
628 path_prepend(&path
, "iucv-%s", udev_device_get_sysname(parent
));
629 parent
= skip_subsystem(parent
, "iucv");
630 supported_transport
= true;
631 supported_parent
= true;
632 } else if (streq(subsys
, "nvme")) {
633 const char *nsid
= udev_device_get_sysattr_value(dev
, "nsid");
636 path_prepend(&path
, "nvme-%s", nsid
);
637 parent
= skip_subsystem(parent
, "nvme");
638 supported_parent
= true;
639 supported_transport
= true;
644 parent
= udev_device_get_parent(parent
);
651 * Do not return devices with an unknown parent device type. They
652 * might produce conflicting IDs if the parent does not provide a
653 * unique and predictable name.
655 if (!supported_parent
)
659 * Do not return block devices without a well-known transport. Some
660 * devices do not expose their buses and do not provide a unique
661 * and predictable name that way.
663 if (streq_ptr(udev_device_get_subsystem(dev
), "block") && !supported_transport
)
667 char tag
[UTIL_NAME_SIZE
];
671 /* compose valid udev tag name */
672 for (p
= path
, i
= 0; *p
; p
++) {
673 if ((*p
>= '0' && *p
<= '9') ||
674 (*p
>= 'A' && *p
<= 'Z') ||
675 (*p
>= 'a' && *p
<= 'z') ||
681 /* skip all leading '_' */
685 /* avoid second '_' */
691 /* strip trailing '_' */
692 while (i
> 0 && tag
[i
-1] == '_')
696 udev_builtin_add_property(dev
, test
, "ID_PATH", path
);
697 udev_builtin_add_property(dev
, test
, "ID_PATH_TAG", tag
);
703 const struct udev_builtin udev_builtin_path_id
= {
705 .cmd
= builtin_path_id
,
706 .help
= "Compose persistent device path",