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/>.
36 static int path_prepend(char **path
, const char *fmt
, ...) {
42 err
= vasprintf(&pre
, fmt
, va
);
50 err
= asprintf(&new, "%s-%s", pre
, *path
);
64 ** Linux only supports 32 bit luns.
65 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
67 static int format_lun_number(struct udev_device
*dev
, char **path
) {
68 unsigned long lun
= strtoul(udev_device_get_sysnum(dev
), NULL
, 10);
70 /* address method 0, peripheral device addressing with bus id of zero */
72 return path_prepend(path
, "lun-%lu", lun
);
73 /* handle all other lun addressing methods by using a variant of the original lun format */
74 return path_prepend(path
, "lun-0x%04lx%04lx00000000", lun
& 0xffff, (lun
>> 16) & 0xffff);
77 static struct udev_device
*skip_subsystem(struct udev_device
*dev
, const char *subsys
) {
78 struct udev_device
*parent
= dev
;
80 while (parent
!= NULL
) {
81 const char *subsystem
;
83 subsystem
= udev_device_get_subsystem(parent
);
84 if (subsystem
== NULL
|| !streq(subsystem
, subsys
))
87 parent
= udev_device_get_parent(parent
);
92 static struct udev_device
*handle_scsi_fibre_channel(struct udev_device
*parent
, char **path
) {
93 struct udev
*udev
= udev_device_get_udev(parent
);
94 struct udev_device
*targetdev
;
95 struct udev_device
*fcdev
= NULL
;
99 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
100 if (targetdev
== NULL
)
103 fcdev
= udev_device_new_from_subsystem_sysname(udev
, "fc_transport", udev_device_get_sysname(targetdev
));
106 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 udev_device_unref(fcdev
);
121 static struct udev_device
*handle_scsi_sas(struct udev_device
*parent
, char **path
) {
122 struct udev
*udev
= udev_device_get_udev(parent
);
123 struct udev_device
*targetdev
;
124 struct udev_device
*target_parent
;
125 struct udev_device
*sasdev
;
126 const char *sas_address
;
129 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
130 if (targetdev
== NULL
)
133 target_parent
= udev_device_get_parent(targetdev
);
134 if (target_parent
== NULL
)
137 sasdev
= udev_device_new_from_subsystem_sysname(udev
, "sas_device",
138 udev_device_get_sysname(target_parent
));
142 sas_address
= udev_device_get_sysattr_value(sasdev
, "sas_address");
143 if (sas_address
== NULL
) {
148 format_lun_number(parent
, &lun
);
149 path_prepend(path
, "sas-%s-%s", sas_address
, lun
);
153 udev_device_unref(sasdev
);
157 static struct udev_device
*handle_scsi_iscsi(struct udev_device
*parent
, char **path
) {
158 struct udev
*udev
= udev_device_get_udev(parent
);
159 struct udev_device
*transportdev
;
160 struct udev_device
*sessiondev
= NULL
;
163 struct udev_device
*conndev
= NULL
;
168 /* find iscsi session */
169 transportdev
= parent
;
171 transportdev
= udev_device_get_parent(transportdev
);
172 if (transportdev
== NULL
)
174 if (startswith(udev_device_get_sysname(transportdev
), "session"))
178 /* find iscsi session device */
179 sessiondev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_session", udev_device_get_sysname(transportdev
));
180 if (sessiondev
== NULL
)
182 target
= udev_device_get_sysattr_value(sessiondev
, "targetname");
183 if (target
== NULL
) {
188 if (asprintf(&connname
, "connection%s:0", udev_device_get_sysnum(transportdev
)) < 0) {
192 conndev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_connection", connname
);
194 if (conndev
== NULL
) {
198 addr
= udev_device_get_sysattr_value(conndev
, "persistent_address");
199 port
= udev_device_get_sysattr_value(conndev
, "persistent_port");
200 if (addr
== NULL
|| port
== NULL
) {
205 format_lun_number(parent
, &lun
);
206 path_prepend(path
, "ip-%s:%s-iscsi-%s-%s", addr
, port
, target
, lun
);
210 udev_device_unref(sessiondev
);
211 udev_device_unref(conndev
);
215 static struct udev_device
*handle_scsi_default(struct udev_device
*parent
, char **path
) {
216 struct udev_device
*hostdev
;
217 int host
, bus
, target
, lun
;
225 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
229 name
= udev_device_get_sysname(parent
);
230 if (sscanf(name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4)
234 * Rebase host offset to get the local relative number
236 * Note: This is by definition racy, unreliable and too simple.
237 * Please do not copy this model anywhere. It's just a left-over
238 * from the time we had no idea how things should look like in
241 * Making assumptions about a global in-kernel counter and use
242 * that to calculate a local offset is a very broken concept. It
243 * can only work as long as things are in strict order.
245 * The kernel needs to export the instance/port number of a
246 * controller directly, without the need for rebase magic like
247 * this. Manual driver unbind/bind, parallel hotplug/unplug will
248 * get into the way of this "I hope it works" logic.
251 base
= strdup(udev_device_get_syspath(hostdev
));
254 pos
= strrchr(base
, '/');
265 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
269 if (dent
->d_name
[0] == '.')
271 if (dent
->d_type
!= DT_DIR
&& dent
->d_type
!= DT_LNK
)
273 if (!startswith(dent
->d_name
, "host"))
275 i
= strtoul(&dent
->d_name
[4], &rest
, 10);
279 * find the smallest number; the host really needs to export its
280 * own instance number per parent device; relying on the global host
281 * enumeration and plainly rebasing the numbers sounds unreliable
283 if (basenum
== -1 || i
< basenum
)
293 path_prepend(path
, "scsi-%u:%u:%u:%u", host
, bus
, target
, lun
);
299 static struct udev_device
*handle_scsi_hyperv(struct udev_device
*parent
, char **path
) {
300 struct udev_device
*hostdev
;
301 struct udev_device
*vmbusdev
;
302 const char *guid_str
;
307 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
311 vmbusdev
= udev_device_get_parent(hostdev
);
315 guid_str
= udev_device_get_sysattr_value(vmbusdev
, "device_id");
319 if (strlen(guid_str
) < 37 || guid_str
[0] != '{' || guid_str
[36] != '}')
322 for (i
= 1, k
= 0; i
< 36; i
++) {
323 if (guid_str
[i
] == '-')
325 guid
[k
++] = guid_str
[i
];
329 format_lun_number(parent
, &lun
);
330 path_prepend(path
, "vmbus-%s-%s", guid
, lun
);
335 static struct udev_device
*handle_scsi(struct udev_device
*parent
, char **path
) {
340 devtype
= udev_device_get_devtype(parent
);
341 if (devtype
== NULL
|| !streq(devtype
, "scsi_device"))
345 id
= udev_device_get_sysattr_value(parent
, "ieee1394_id");
347 parent
= skip_subsystem(parent
, "scsi");
348 path_prepend(path
, "ieee1394-0x%s", id
);
352 /* scsi sysfs does not have a "subsystem" for the transport */
353 name
= udev_device_get_syspath(parent
);
355 if (strstr(name
, "/rport-") != NULL
) {
356 parent
= handle_scsi_fibre_channel(parent
, path
);
360 if (strstr(name
, "/end_device-") != NULL
) {
361 parent
= handle_scsi_sas(parent
, path
);
365 if (strstr(name
, "/session") != NULL
) {
366 parent
= handle_scsi_iscsi(parent
, path
);
371 * We do not support the ATA transport class, it uses global counters
372 * to name the ata devices which numbers spread across multiple
375 * The real link numbers are not exported. Also, possible chains of ports
376 * behind port multipliers cannot be composed that way.
378 * Until all that is solved at the kernel level, there are no by-path/
379 * links for ATA devices.
381 if (strstr(name
, "/ata") != NULL
) {
386 if (strstr(name
, "/vmbus_") != NULL
) {
387 parent
= handle_scsi_hyperv(parent
, path
);
391 parent
= handle_scsi_default(parent
, path
);
396 static struct udev_device
*handle_cciss(struct udev_device
*parent
, char **path
) {
398 unsigned int controller
, disk
;
400 str
= udev_device_get_sysname(parent
);
401 if (sscanf(str
, "c%ud%u%*s", &controller
, &disk
) != 2)
404 path_prepend(path
, "cciss-disk%u", disk
);
405 parent
= skip_subsystem(parent
, "cciss");
409 static void handle_scsi_tape(struct udev_device
*dev
, char **path
) {
412 /* must be the last device in the syspath */
416 name
= udev_device_get_sysname(dev
);
417 if (startswith(name
, "nst") && strchr("lma", name
[3]) != NULL
)
418 path_prepend(path
, "nst%c", name
[3]);
419 else if (startswith(name
, "st") && strchr("lma", name
[2]) != NULL
)
420 path_prepend(path
, "st%c", name
[2]);
423 static struct udev_device
*handle_usb(struct udev_device
*parent
, char **path
) {
428 devtype
= udev_device_get_devtype(parent
);
431 if (!streq(devtype
, "usb_interface") && !streq(devtype
, "usb_device"))
434 str
= udev_device_get_sysname(parent
);
435 port
= strchr(str
, '-');
440 parent
= skip_subsystem(parent
, "usb");
441 path_prepend(path
, "usb-0:%s", port
);
445 static struct udev_device
*handle_bcma(struct udev_device
*parent
, char **path
) {
449 sysname
= udev_device_get_sysname(parent
);
450 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
453 path_prepend(path
, "bcma-%u", core
);
457 static struct udev_device
*handle_ccw(struct udev_device
*parent
, struct udev_device
*dev
, char **path
) {
458 struct udev_device
*scsi_dev
;
460 scsi_dev
= udev_device_get_parent_with_subsystem_devtype(dev
, "scsi", "scsi_device");
461 if (scsi_dev
!= NULL
) {
466 hba_id
= udev_device_get_sysattr_value(scsi_dev
, "hba_id");
467 wwpn
= udev_device_get_sysattr_value(scsi_dev
, "wwpn");
468 lun
= udev_device_get_sysattr_value(scsi_dev
, "fcp_lun");
469 if (hba_id
!= NULL
&& lun
!= NULL
&& wwpn
!= NULL
) {
470 path_prepend(path
, "ccw-%s-zfcp-%s:%s", hba_id
, wwpn
, lun
);
475 path_prepend(path
, "ccw-%s", udev_device_get_sysname(parent
));
477 parent
= skip_subsystem(parent
, "ccw");
481 static int builtin_path_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
482 struct udev_device
*parent
;
484 bool supported_transport
= false;
485 bool supported_parent
= false;
488 parent
= udev_device_get_parent_with_subsystem_devtype(dev
, "ccw", NULL
);
489 if (parent
!= NULL
) {
490 handle_ccw(parent
, dev
, &path
);
494 /* walk up the chain of devices and compose path */
496 while (parent
!= NULL
) {
499 subsys
= udev_device_get_subsystem(parent
);
500 if (subsys
== NULL
) {
502 } else if (streq(subsys
, "scsi_tape")) {
503 handle_scsi_tape(parent
, &path
);
504 } else if (streq(subsys
, "scsi")) {
505 parent
= handle_scsi(parent
, &path
);
506 supported_transport
= true;
507 } else if (streq(subsys
, "cciss")) {
508 parent
= handle_cciss(parent
, &path
);
509 supported_transport
= true;
510 } else if (streq(subsys
, "usb")) {
511 parent
= handle_usb(parent
, &path
);
512 supported_transport
= true;
513 } else if (streq(subsys
, "bcma")) {
514 parent
= handle_bcma(parent
, &path
);
515 supported_transport
= true;
516 } else if (streq(subsys
, "serio")) {
517 path_prepend(&path
, "serio-%s", udev_device_get_sysnum(parent
));
518 parent
= skip_subsystem(parent
, "serio");
519 } else if (streq(subsys
, "pci")) {
520 path_prepend(&path
, "pci-%s", udev_device_get_sysname(parent
));
521 parent
= skip_subsystem(parent
, "pci");
522 supported_parent
= true;
523 } else if (streq(subsys
, "platform")) {
524 path_prepend(&path
, "platform-%s", udev_device_get_sysname(parent
));
525 parent
= skip_subsystem(parent
, "platform");
526 supported_transport
= true;
527 supported_parent
= true;
528 } else if (streq(subsys
, "acpi")) {
529 path_prepend(&path
, "acpi-%s", udev_device_get_sysname(parent
));
530 parent
= skip_subsystem(parent
, "acpi");
531 supported_parent
= true;
532 } else if (streq(subsys
, "xen")) {
533 path_prepend(&path
, "xen-%s", udev_device_get_sysname(parent
));
534 parent
= skip_subsystem(parent
, "xen");
535 supported_parent
= true;
536 } else if (streq(subsys
, "scm")) {
537 path_prepend(&path
, "scm-%s", udev_device_get_sysname(parent
));
538 parent
= skip_subsystem(parent
, "scm");
539 supported_transport
= true;
540 supported_parent
= true;
543 parent
= udev_device_get_parent(parent
);
547 * Do return devices with have an unknown type of parent device, they
548 * might produce conflicting IDs below multiple independent parent
551 if (!supported_parent
) {
557 * Do not return a have-only a single-parent block devices, some
558 * have entire hidden buses behind it, and not create predictable
561 if (streq(udev_device_get_subsystem(dev
), "block") && !supported_transport
) {
568 char tag
[UTIL_NAME_SIZE
];
572 /* compose valid udev tag name */
573 for (p
= path
, i
= 0; *p
; p
++) {
574 if ((*p
>= '0' && *p
<= '9') ||
575 (*p
>= 'A' && *p
<= 'Z') ||
576 (*p
>= 'a' && *p
<= 'z') ||
582 /* skip all leading '_' */
586 /* avoid second '_' */
592 /* strip trailing '_' */
593 while (i
> 0 && tag
[i
-1] == '_')
597 udev_builtin_add_property(dev
, test
, "ID_PATH", path
);
598 udev_builtin_add_property(dev
, test
, "ID_PATH_TAG", tag
);
605 const struct udev_builtin udev_builtin_path_id
= {
607 .cmd
= builtin_path_id
,
608 .help
= "compose persistent device path",