Debian Codegraph @

src/udev/udev-builtin-path_id.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * compose persistent device path
4 *
5 * Logic based on Hannes Reinecke's shell script.
6 */
7
8 #include <linux/usb/ch11.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "alloc-util.h"
13 #include "device-private.h"
14 #include "device-util.h"
15 #include "dirent-util.h"
16 #include "fd-util.h"
17 #include "parse-util.h"
18 #include "string-util.h"
19 #include "sysexits.h"
20 #include "udev-builtin.h"
21
22 _printf_(2,3)
23 static void path_prepend(char **path, const char *fmt, ...) {
24 va_list va;
25 _cleanup_free_ char *pre = NULL;
26 int r;
27
28 va_start(va, fmt);
29 r = vasprintf(&pre, fmt, va);
30 va_end(va);
31 if (r < 0) {
32 log_oom();
33 exit(EX_OSERR);
34 }
35
36 if (*path) {
37 char *new;
38
39 new = strjoin(pre, "-", *path);
40 if (!new) {
41 log_oom();
42 exit(EX_OSERR);
43 }
44
45 free_and_replace(*path, new);
46 } else
47 *path = TAKE_PTR(pre);
48 }
49
50 /*
51 ** Linux only supports 32 bit luns.
52 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
53 */
54 static int format_lun_number(sd_device *dev, char **path) {
55 unsigned lun;
56 int r;
57
58 r = device_get_sysnum_unsigned(dev, &lun);
59 if (r < 0)
60 return r;
61 if (lun < 256)
62 /* address method 0, peripheral device addressing with bus id of zero */
63 path_prepend(path, "lun-%u", lun);
64 else
65 /* handle all other lun addressing methods by using a variant of the original lun format */
66 path_prepend(path, "lun-0x%04x%04x00000000", lun & 0xffff, (lun >> 16) & 0xffff);
67
68 return 0;
69 }
70
71 static sd_device* skip_subsystem(sd_device *dev, const char *subsys) {
72 sd_device *parent;
73
74 assert(dev);
75 assert(subsys);
76
77 /* Unlike the function name, this drops multiple parent devices EXCEPT FOR THE LAST ONE.
78 * The last one will be dropped at the end of the loop in builtin_path_id().
79 * E.g.
80 * Input: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0
81 * Output: /sys/devices/pci0000:00/0000:00:14.0/usb1
82 */
83
84 for (parent = dev; ; ) {
85 if (device_in_subsystem(parent, subsys) <= 0)
86 break;
87
88 dev = parent;
89 if (sd_device_get_parent(dev, &parent) < 0)
90 break;
91 }
92
93 return dev;
94 }
95
96 static sd_device* handle_scsi_fibre_channel(sd_device *parent, char **path) {
97 sd_device *targetdev;
98 _cleanup_(sd_device_unrefp) sd_device *fcdev = NULL;
99 const char *port, *sysname;
100 _cleanup_free_ char *lun = NULL;
101
102 assert(parent);
103 assert(path);
104
105 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
106 return NULL;
107 if (sd_device_get_sysname(targetdev, &sysname) < 0)
108 return NULL;
109 if (sd_device_new_from_subsystem_sysname(&fcdev, "fc_transport", sysname) < 0)
110 return NULL;
111 if (device_get_sysattr_safe_string(fcdev, "port_name", &port) < 0)
112 return NULL;
113
114 format_lun_number(parent, &lun);
115 path_prepend(path, "fc-%s-%s", port, lun);
116 return parent;
117 }
118
119 static sd_device* handle_scsi_sas_wide_port(sd_device *parent, char **path) {
120 sd_device *targetdev, *target_parent;
121 _cleanup_(sd_device_unrefp) sd_device *sasdev = NULL;
122 const char *sas_address, *sysname;
123 _cleanup_free_ char *lun = NULL;
124
125 assert(parent);
126 assert(path);
127
128 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
129 return NULL;
130 if (sd_device_get_parent(targetdev, &target_parent) < 0)
131 return NULL;
132 if (sd_device_get_sysname(target_parent, &sysname) < 0)
133 return NULL;
134 if (sd_device_new_from_subsystem_sysname(&sasdev, "sas_device", sysname) < 0)
135 return NULL;
136 if (device_get_sysattr_safe_string(sasdev, "sas_address", &sas_address) < 0)
137 return NULL;
138
139 format_lun_number(parent, &lun);
140 path_prepend(path, "sas-%s-%s", sas_address, lun);
141 return parent;
142 }
143
144 static sd_device* handle_scsi_sas(sd_device *parent, char **path) {
145 sd_device *targetdev, *target_parent, *port, *expander;
146 _cleanup_(sd_device_unrefp) sd_device *target_sasdev = NULL, *expander_sasdev = NULL, *port_sasdev = NULL;
147 const char *sas_address = NULL, *phy_id, *sysname;
148 unsigned num_phys;
149 _cleanup_free_ char *lun = NULL;
150
151 assert(parent);
152 assert(path);
153
154 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
155 return NULL;
156 if (sd_device_get_parent(targetdev, &target_parent) < 0)
157 return NULL;
158 if (sd_device_get_sysname(target_parent, &sysname) < 0)
159 return NULL;
160 /* Get sas device */
161 if (sd_device_new_from_subsystem_sysname(&target_sasdev, "sas_device", sysname) < 0)
162 return NULL;
163 /* The next parent is sas port */
164 if (sd_device_get_parent(target_parent, &port) < 0)
165 return NULL;
166 if (sd_device_get_sysname(port, &sysname) < 0)
167 return NULL;
168 /* Get port device */
169 if (sd_device_new_from_subsystem_sysname(&port_sasdev, "sas_port", sysname) < 0)
170 return NULL;
171 if (device_get_sysattr_unsigned(port_sasdev, "num_phys", &num_phys) < 0)
172 return NULL;
173 /* Check if this is a wide port (i.e. num_phys is 2 or higher) */
174 if (num_phys > 1)
175 return handle_scsi_sas_wide_port(parent, path);
176
177 /* Get connected phy */
178 if (device_get_sysattr_safe_string(target_sasdev, "phy_identifier", &phy_id) < 0)
179 return NULL;
180
181 /* The port's parent is either hba or expander */
182 if (sd_device_get_parent(port, &expander) < 0)
183 return NULL;
184
185 if (sd_device_get_sysname(expander, &sysname) < 0)
186 return NULL;
187 /* Get expander device */
188 if (sd_device_new_from_subsystem_sysname(&expander_sasdev, "sas_device", sysname) >= 0) {
189 /* Get expander's address */
190 if (device_get_sysattr_safe_string(expander_sasdev, "sas_address", &sas_address) < 0)
191 return NULL;
192 }
193
194 format_lun_number(parent, &lun);
195 if (sas_address)
196 path_prepend(path, "sas-exp%s-phy%s-%s", sas_address, phy_id, lun);
197 else
198 path_prepend(path, "sas-phy%s-%s", phy_id, lun);
199
200 return parent;
201 }
202
203 static sd_device* handle_scsi_iscsi(sd_device *parent, char **path) {
204 sd_device *transportdev;
205 _cleanup_(sd_device_unrefp) sd_device *sessiondev = NULL, *conndev = NULL;
206 const char *target, *connname, *addr, *port, *sysname, *sysnum;
207 _cleanup_free_ char *lun = NULL;
208
209 assert(parent);
210 assert(path);
211
212 /* find iscsi session */
213 for (transportdev = parent; ; ) {
214
215 if (sd_device_get_parent(transportdev, &transportdev) < 0)
216 return NULL;
217 if (sd_device_get_sysname(transportdev, &sysname) < 0)
218 return NULL;
219 if (startswith(sysname, "session"))
220 break;
221 }
222
223 /* find iscsi session device */
224 if (sd_device_new_from_subsystem_sysname(&sessiondev, "iscsi_session", sysname) < 0)
225 return NULL;
226
227 if (device_get_sysattr_safe_string(sessiondev, "targetname", &target) < 0)
228 return NULL;
229
230 if (sd_device_get_sysnum(transportdev, &sysnum) < 0)
231 return NULL;
232 connname = strjoina("connection", sysnum, ":0");
233 if (sd_device_new_from_subsystem_sysname(&conndev, "iscsi_connection", connname) < 0)
234 return NULL;
235
236 if (device_get_sysattr_safe_string(conndev, "persistent_address", &addr) < 0)
237 return NULL;
238 if (device_get_sysattr_safe_string(conndev, "persistent_port", &port) < 0)
239 return NULL;
240
241 format_lun_number(parent, &lun);
242 path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
243 return parent;
244 }
245
246 static sd_device* handle_scsi_ata(sd_device *parent, char **path, char **compat_path) {
247 sd_device *targetdev, *target_parent;
248 _cleanup_(sd_device_unrefp) sd_device *atadev = NULL;
249 const char *port_no, *sysname, *name;
250 unsigned host, bus, target, lun;
251
252 assert(parent);
253 assert(path);
254
255 if (sd_device_get_sysname(parent, &name) < 0)
256 return NULL;
257 if (sscanf(name, "%u:%u:%u:%u", &host, &bus, &target, &lun) != 4)
258 return NULL;
259
260 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &targetdev) < 0)
261 return NULL;
262
263 if (sd_device_get_parent(targetdev, &target_parent) < 0)
264 return NULL;
265
266 if (sd_device_get_sysname(target_parent, &sysname) < 0)
267 return NULL;
268 if (sd_device_new_from_subsystem_sysname(&atadev, "ata_port", sysname) < 0)
269 return NULL;
270
271 if (device_get_sysattr_safe_string(atadev, "port_no", &port_no) < 0)
272 return NULL;
273
274 if (bus != 0)
275 /* Devices behind port multiplier have a bus != 0 */
276 path_prepend(path, "ata-%s.%u.0", port_no, bus);
277 else
278 /* Master/slave are distinguished by target id */
279 path_prepend(path, "ata-%s.%u", port_no, target);
280
281 /* old compatible persistent link for ATA devices */
282 if (compat_path)
283 path_prepend(compat_path, "ata-%s", port_no);
284
285 return parent;
286 }
287
288 static sd_device* handle_scsi_default(sd_device *parent, char **path) {
289 sd_device *hostdev;
290 int host, bus, target, lun;
291 const char *name, *base, *pos;
292 _cleanup_closedir_ DIR *dir = NULL;
293 int basenum = -1;
294
295 assert(parent);
296 assert(path);
297
298 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &hostdev) < 0)
299 return NULL;
300
301 if (sd_device_get_sysname(parent, &name) < 0)
302 return NULL;
303 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
304 return NULL;
305
306 /*
307 * Rebase host offset to get the local relative number
308 *
309 * Note: This is by definition racy, unreliable and too simple.
310 * Please do not copy this model anywhere. It's just a left-over
311 * from the time we had no idea how things should look like in
312 * the end.
313 *
314 * Making assumptions about a global in-kernel counter and use
315 * that to calculate a local offset is a very broken concept. It
316 * can only work as long as things are in strict order.
317 *
318 * The kernel needs to export the instance/port number of a
319 * controller directly, without the need for rebase magic like
320 * this. Manual driver unbind/bind, parallel hotplug/unplug will
321 * get into the way of this "I hope it works" logic.
322 */
323
324 if (sd_device_get_syspath(hostdev, &base) < 0)
325 return NULL;
326 pos = strrchr(base, '/');
327 if (!pos)
328 return NULL;
329
330 base = strndupa_safe(base, pos - base);
331 dir = opendir(base);
332 if (!dir)
333 return NULL;
334
335 FOREACH_DIRENT_ALL(de, dir, break) {
336 unsigned i;
337
338 if (de->d_name[0] == '.')
339 continue;
340 if (!IN_SET(de->d_type, DT_DIR, DT_LNK))
341 continue;
342 if (!startswith(de->d_name, "host"))
343 continue;
344 if (safe_atou_full(&de->d_name[4], 10, &i) < 0)
345 continue;
346 /*
347 * find the smallest number; the host really needs to export its
348 * own instance number per parent device; relying on the global host
349 * enumeration and plainly rebasing the numbers sounds unreliable
350 */
351 if (basenum == -1 || (int) i < basenum)
352 basenum = i;
353 }
354 if (basenum == -1)
355 return hostdev;
356 host -= basenum;
357
358 path_prepend(path, "scsi-%i:%i:%i:%i", host, bus, target, lun);
359 return hostdev;
360 }
361
362 static sd_device* handle_scsi_hyperv(sd_device *parent, char **path, size_t guid_str_len) {
363 sd_device *hostdev, *vmbusdev;
364 const char *guid_str;
365 _cleanup_free_ char *lun = NULL;
366 char guid[39];
367
368 assert(parent);
369 assert(path);
370 assert(guid_str_len < sizeof(guid));
371
372 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &hostdev) < 0)
373 return NULL;
374
375 if (sd_device_get_parent(hostdev, &vmbusdev) < 0)
376 return NULL;
377
378 if (device_get_sysattr_safe_string(vmbusdev, "device_id", &guid_str) < 0)
379 return NULL;
380
381 if (strlen(guid_str) < guid_str_len || guid_str[0] != '{' || guid_str[guid_str_len-1] != '}')
382 return NULL;
383
384 size_t k = 0;
385 for (size_t i = 1; i < guid_str_len-1; i++) {
386 if (guid_str[i] == '-')
387 continue;
388 guid[k++] = guid_str[i];
389 }
390 guid[k] = '\0';
391
392 format_lun_number(parent, &lun);
393 path_prepend(path, "vmbus-%s-%s", guid, lun);
394 return parent;
395 }
396
397 static sd_device* handle_scsi(sd_device *parent, char **path, char **compat_path, bool *supported_parent) {
398 const char *id, *name;
399
400 assert(supported_parent);
401
402 if (device_is_devtype(parent, "scsi_device") <= 0)
403 return parent;
404
405 /* firewire */
406 if (device_get_sysattr_safe_string(parent, "ieee1394_id", &id) >= 0) {
407 path_prepend(path, "ieee1394-0x%s", id);
408 *supported_parent = true;
409 return skip_subsystem(parent, "scsi");
410 }
411
412 /* scsi sysfs does not have a "subsystem" for the transport */
413 if (sd_device_get_syspath(parent, &name) < 0)
414 return NULL;
415
416 if (strstr(name, "/rport-")) {
417 *supported_parent = true;
418 return handle_scsi_fibre_channel(parent, path);
419 }
420
421 if (strstr(name, "/end_device-")) {
422 *supported_parent = true;
423 return handle_scsi_sas(parent, path);
424 }
425
426 if (strstr(name, "/session")) {
427 *supported_parent = true;
428 return handle_scsi_iscsi(parent, path);
429 }
430
431 if (strstr(name, "/ata"))
432 return handle_scsi_ata(parent, path, compat_path);
433
434 if (strstr(name, "/vmbus_"))
435 return handle_scsi_hyperv(parent, path, 37);
436 else if (strstr(name, "/VMBUS"))
437 return handle_scsi_hyperv(parent, path, 38);
438
439 return handle_scsi_default(parent, path);
440 }
441
442 static sd_device* handle_cciss(sd_device *parent, char **path) {
443 const char *str;
444 unsigned controller, disk;
445
446 if (sd_device_get_sysname(parent, &str) < 0)
447 return NULL;
448 if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
449 return NULL;
450
451 path_prepend(path, "cciss-disk%u", disk);
452 return skip_subsystem(parent, "cciss");
453 }
454
455 static void handle_scsi_tape(sd_device *dev, char **path) {
456 const char *name;
457
458 assert(path);
459
460 /* must be the last device in the syspath */
461 if (*path)
462 return;
463
464 if (sd_device_get_sysname(dev, &name) < 0)
465 return;
466
467 if (startswith(name, "nst") && strchr("lma", name[3]))
468 path_prepend(path, "nst%c", name[3]);
469 else if (startswith(name, "st") && strchr("lma", name[2]))
470 path_prepend(path, "st%c", name[2]);
471 }
472
473 static int get_usb_revision(sd_device *dev) {
474 uint8_t protocol;
475 int r;
476
477 assert(dev);
478
479 /* Returns usb revision 1, 2, or 3. */
480
481 r = device_get_sysattr_u8_full(dev, "bDeviceProtocol", 16, &protocol);
482 if (r < 0)
483 return r;
484
485 switch (protocol) {
486 case USB_HUB_PR_HS_NO_TT: /* Full speed hub (USB1) or Hi-speed hub without TT (USB2) */
487
488 /* See speed_show() in drivers/usb/core/sysfs.c of the kernel. */
489 r = device_get_sysattr_streq(dev, "speed", "480");
490 if (r < 0)
491 return r;
492 if (r > 0)
493 return 2;
494
495 return 1;
496
497 case USB_HUB_PR_HS_SINGLE_TT: /* Hi-speed hub with single TT */
498 case USB_HUB_PR_HS_MULTI_TT: /* Hi-speed hub with multiple TT */
499 return 2;
500
501 case USB_HUB_PR_SS: /* Super speed hub */
502 return 3;
503
504 default:
505 return -EPROTONOSUPPORT;
506 }
507 }
508
509 static sd_device* handle_usb(sd_device *parent, char **path) {
510 const char *str, *port;
511 int r;
512
513 if (device_is_devtype(parent, "usb_interface") <= 0 && device_is_devtype(parent, "usb_device") <= 0)
514 return parent;
515
516 if (sd_device_get_sysname(parent, &str) < 0)
517 return parent;
518 port = strchr(str, '-');
519 if (!port)
520 return parent;
521 port++;
522
523 parent = skip_subsystem(parent, "usb");
524 if (!parent)
525 return NULL;
526
527 /* USB host number may change across reboots (and probably even without reboot). The part after USB
528 * host number is determined by device topology and so does not change. Hence, drop the host number
529 * and always use '0' instead.
530 *
531 * xHCI host controllers may register two (or more?) USB root hubs for USB 2.0 and USB 3.0, and the
532 * sysname, whose host number replaced with 0, of a device under the hubs may conflict with others.
533 * To avoid the conflict, let's include the USB revision of the root hub to the PATH_ID.
534 * See issue https://github.com/systemd/systemd/issues/19406 for more details. */
535 r = get_usb_revision(parent);
536 if (r < 0) {
537 log_device_debug_errno(parent, r, "Failed to get the USB revision number, ignoring: %m");
538 path_prepend(path, "usb-0:%s", port);
539 } else {
540 assert(r > 0);
541 path_prepend(path, "usbv%i-0:%s", r, port);
542 }
543
544 return parent;
545 }
546
547 static sd_device* handle_bcma(sd_device *parent, char **path) {
548 const char *sysname;
549 unsigned core;
550
551 if (sd_device_get_sysname(parent, &sysname) < 0)
552 return NULL;
553 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
554 return NULL;
555
556 path_prepend(path, "bcma-%u", core);
557 return parent;
558 }
559
560 /* Handle devices of AP bus in System z platform. */
561 static sd_device* handle_ap(sd_device *parent, char **path) {
562 const char *type, *func;
563
564 assert(parent);
565 assert(path);
566
567 if (device_get_sysattr_safe_string(parent, "type", &type) >= 0 &&
568 device_get_sysattr_safe_string(parent, "ap_functions", &func) >= 0)
569 path_prepend(path, "ap-%s-%s", type, func);
570 else {
571 const char *sysname;
572
573 if (sd_device_get_sysname(parent, &sysname) >= 0)
574 path_prepend(path, "ap-%s", sysname);
575 }
576
577 return skip_subsystem(parent, "ap");
578 }
579
580 static int find_real_nvme_parent(sd_device *dev, sd_device **ret) {
581 _cleanup_(sd_device_unrefp) sd_device *nvme = NULL;
582 const char *sysname, *end, *devpath;
583 int r;
584
585 /* If the device belongs to "nvme-subsystem" (not to be confused with "nvme"), which happens when
586 * NVMe multipathing is enabled in the kernel (/sys/module/nvme_core/parameters/multipath is Y),
587 * then the syspath is something like the following:
588 * /sys/devices/virtual/nvme-subsystem/nvme-subsys0/nvme0n1
589 * Hence, we need to find the 'real parent' in "nvme" subsystem, e.g,
590 * /sys/devices/pci0000:00/0000:00:1c.4/0000:3c:00.0/nvme/nvme0 */
591
592 assert(dev);
593 assert(ret);
594
595 r = sd_device_get_sysname(dev, &sysname);
596 if (r < 0)
597 return r;
598
599 /* The sysname format of nvme block device is nvme%d[c%d]n%d[p%d], e.g. nvme0n1p2 or nvme0c1n2.
600 * (Note, nvme device with 'c' can be ignored, as they are hidden. )
601 * The sysname format of nvme subsystem device is nvme%d.
602 * See nvme_alloc_ns() and nvme_init_ctrl() in drivers/nvme/host/core.c for more details. */
603 end = startswith(sysname, "nvme");
604 if (!end)
605 return -ENXIO;
606
607 end += strspn(end, DIGITS);
608 sysname = strndupa_safe(sysname, end - sysname);
609
610 r = sd_device_new_from_subsystem_sysname(&nvme, "nvme", sysname);
611 if (r < 0)
612 return r;
613
614 r = sd_device_get_devpath(nvme, &devpath);
615 if (r < 0)
616 return r;
617
618 /* If the 'real parent' is (still) virtual, e.g. for nvmf disks, refuse to set ID_PATH. */
619 if (path_startswith(devpath, "/devices/virtual/"))
620 return -ENXIO;
621
622 *ret = TAKE_PTR(nvme);
623 return 0;
624 }
625
626 static void add_id_with_usb_revision(UdevEvent *event, char *path) {
627 char *p;
628
629 assert(event);
630 assert(path);
631
632 /* When the path contains the USB revision, let's adds ID_PATH_WITH_USB_REVISION property and
633 * drop the version specifier for later use. */
634
635 p = strstrafter(path, "-usbv");
636 if (!p)
637 return;
638 if (!ascii_isdigit(p[0]))
639 return;
640 if (p[1] != '-')
641 return;
642
643 (void) udev_builtin_add_property(event, "ID_PATH_WITH_USB_REVISION", path);
644
645 /* Drop the USB revision specifier for backward compatibility. */
646 memmove(p - 1, p + 1, strlen(p + 1) + 1);
647 }
648
649 static void add_id_tag(UdevEvent *event, const char *path) {
650 char tag[UDEV_NAME_SIZE];
651 size_t i = 0;
652
653 /* compose valid udev tag name */
654 for (const char *p = path; *p && i < sizeof(tag) - 1; p++) {
655 if (ascii_isdigit(*p) ||
656 ascii_isalpha(*p) ||
657 *p == '-') {
658 tag[i++] = *p;
659 continue;
660 }
661
662 /* skip all leading '_' */
663 if (i == 0)
664 continue;
665
666 /* avoid second '_' */
667 if (tag[i-1] == '_')
668 continue;
669
670 tag[i++] = '_';
671 }
672 /* strip trailing '_' */
673 while (i > 0 && tag[i-1] == '_')
674 i--;
675 tag[i] = '\0';
676
677 (void) udev_builtin_add_property(event, "ID_PATH_TAG", tag);
678 }
679
680 static int builtin_path_id(UdevEvent *event, int argc, char *argv[]) {
681 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
682 _cleanup_(sd_device_unrefp) sd_device *dev_other_branch = NULL;
683 _cleanup_free_ char *path = NULL, *compat_path = NULL;
684 bool supported_transport = false, supported_parent = false;
685 int r;
686
687 /* walk up the chain of devices and compose path */
688 for (sd_device *parent = dev; parent; ) {
689 const char *sysname;
690
691 if (sd_device_get_sysname(parent, &sysname) < 0) {
692 ;
693 } else if (device_in_subsystem(parent, "scsi_tape") > 0) {
694 handle_scsi_tape(parent, &path);
695 } else if (device_in_subsystem(parent, "scsi") > 0) {
696 parent = handle_scsi(parent, &path, &compat_path, &supported_parent);
697 supported_transport = true;
698 } else if (device_in_subsystem(parent, "cciss") > 0) {
699 parent = handle_cciss(parent, &path);
700 supported_transport = true;
701 } else if (device_in_subsystem(parent, "usb") > 0) {
702 parent = handle_usb(parent, &path);
703 supported_transport = true;
704 } else if (device_in_subsystem(parent, "bcma") > 0) {
705 parent = handle_bcma(parent, &path);
706 supported_transport = true;
707 } else if (device_in_subsystem(parent, "serio") > 0) {
708 const char *sysnum;
709
710 if (sd_device_get_sysnum(parent, &sysnum) >= 0) {
711 path_prepend(&path, "serio-%s", sysnum);
712 parent = skip_subsystem(parent, "serio");
713 }
714 } else if (device_in_subsystem(parent, "auxiliary") > 0) {
715 unsigned sfnum;
716
717 /* sfnum is the user-defined sub-function number (devlink port add ...
718 * sfnum N). Prepend it so an SF leaf device gets an ID_PATH distinct
719 * from its parent PF/VF; aux devices without 'sfnum' emit no token to
720 * preserve pre-patch ID_PATH values. */
721 if (device_get_sysattr_unsigned(parent, "sfnum", &sfnum) >= 0) {
722 path_prepend(&path, "sf-%u", sfnum);
723 if (compat_path)
724 path_prepend(&compat_path, "sf-%u", sfnum);
725 }
726 } else if (device_in_subsystem(parent, "pci") > 0) {
727 path_prepend(&path, "pci-%s", sysname);
728 if (compat_path)
729 path_prepend(&compat_path, "pci-%s", sysname);
730 parent = skip_subsystem(parent, "pci");
731 supported_parent = true;
732 } else if (device_in_subsystem(parent, "platform") > 0) {
733 path_prepend(&path, "platform-%s", sysname);
734 if (compat_path)
735 path_prepend(&compat_path, "platform-%s", sysname);
736 parent = skip_subsystem(parent, "platform");
737 supported_transport = true;
738 supported_parent = true;
739 } else if (device_in_subsystem(parent, "amba") > 0) {
740 path_prepend(&path, "amba-%s", sysname);
741 if (compat_path)
742 path_prepend(&compat_path, "amba-%s", sysname);
743 parent = skip_subsystem(parent, "amba");
744 supported_transport = true;
745 supported_parent = true;
746 } else if (device_in_subsystem(parent, "acpi") > 0) {
747 path_prepend(&path, "acpi-%s", sysname);
748 if (compat_path)
749 path_prepend(&compat_path, "acpi-%s", sysname);
750 parent = skip_subsystem(parent, "acpi");
751 supported_parent = true;
752 } else if (device_in_subsystem(parent, "xen") > 0) {
753 path_prepend(&path, "xen-%s", sysname);
754 if (compat_path)
755 path_prepend(&compat_path, "xen-%s", sysname);
756 parent = skip_subsystem(parent, "xen");
757 supported_parent = true;
758 } else if (device_in_subsystem(parent, "virtio") > 0) {
759 parent = skip_subsystem(parent, "virtio");
760 supported_transport = true;
761 } else if (device_in_subsystem(parent, "scm") > 0) {
762 path_prepend(&path, "scm-%s", sysname);
763 if (compat_path)
764 path_prepend(&compat_path, "scm-%s", sysname);
765 parent = skip_subsystem(parent, "scm");
766 supported_transport = true;
767 supported_parent = true;
768 } else if (device_in_subsystem(parent, "ccw") > 0) {
769 path_prepend(&path, "ccw-%s", sysname);
770 if (compat_path)
771 path_prepend(&compat_path, "ccw-%s", sysname);
772 parent = skip_subsystem(parent, "ccw");
773 supported_transport = true;
774 supported_parent = true;
775 } else if (device_in_subsystem(parent, "ccwgroup") > 0) {
776 path_prepend(&path, "ccwgroup-%s", sysname);
777 if (compat_path)
778 path_prepend(&compat_path, "ccwgroup-%s", sysname);
779 parent = skip_subsystem(parent, "ccwgroup");
780 supported_transport = true;
781 supported_parent = true;
782 } else if (device_in_subsystem(parent, "ap") > 0) {
783 parent = handle_ap(parent, &path);
784 supported_transport = true;
785 supported_parent = true;
786 } else if (device_in_subsystem(parent, "iucv") > 0) {
787 path_prepend(&path, "iucv-%s", sysname);
788 if (compat_path)
789 path_prepend(&compat_path, "iucv-%s", sysname);
790 parent = skip_subsystem(parent, "iucv");
791 supported_transport = true;
792 supported_parent = true;
793 } else if (device_in_subsystem(parent, "nvme", "nvme-subsystem") > 0) {
794 const char *nsid;
795
796 if (device_get_sysattr_safe_string(dev, "nsid", &nsid) >= 0) {
797 path_prepend(&path, "nvme-%s", nsid);
798 if (compat_path)
799 path_prepend(&compat_path, "nvme-%s", nsid);
800
801 if (device_in_subsystem(parent, "nvme-subsystem") > 0) {
802 r = find_real_nvme_parent(dev, &dev_other_branch);
803 if (r < 0)
804 return r;
805
806 parent = dev_other_branch;
807 }
808
809 parent = skip_subsystem(parent, "nvme");
810 supported_parent = true;
811 supported_transport = true;
812 }
813 } else if (device_in_subsystem(parent, "spi") > 0) {
814 const char *sysnum;
815
816 if (sd_device_get_sysnum(parent, &sysnum) >= 0) {
817 path_prepend(&path, "cs-%s", sysnum);
818 parent = skip_subsystem(parent, "spi");
819 }
820 }
821
822 if (!parent)
823 break;
824 if (sd_device_get_parent(parent, &parent) < 0)
825 break;
826 }
827
828 if (!path)
829 return -ENOENT;
830
831 /*
832 * Do not return devices with an unknown parent device type. They
833 * might produce conflicting IDs if the parent does not provide a
834 * unique and predictable name.
835 */
836 if (!supported_parent)
837 return -ENOENT;
838
839 /*
840 * Do not return block devices without a well-known transport. Some
841 * devices do not expose their buses and do not provide a unique
842 * and predictable name that way.
843 */
844 if (device_in_subsystem(dev, "block") > 0 && !supported_transport)
845 return -ENOENT;
846
847 add_id_with_usb_revision(event, path);
848
849 (void) udev_builtin_add_property(event, "ID_PATH", path);
850
851 add_id_tag(event, path);
852
853 /*
854 * Compatible link generation for ATA devices
855 * we assign compat_link to the env variable
856 * ID_PATH_ATA_COMPAT
857 */
858 if (compat_path)
859 (void) udev_builtin_add_property(event, "ID_PATH_ATA_COMPAT", compat_path);
860
861 return 0;
862 }
863
864 const UdevBuiltin udev_builtin_path_id = {
865 .name = "path_id",
866 .cmd = builtin_path_id,
867 .help = "Compose persistent device path",
868 .run_once = true,
869 };