2 * compose persistent device path
4 * Copyright (C) 2009-2011 Kay Sievers <kay.sievers@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/>.
35 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)
69 unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
71 /* address method 0, peripheral device addressing with bus id of zero */
73 return path_prepend(path, "lun-%d", lun);
74 /* handle all other lun addressing methods by using a variant of the original lun format */
75 return path_prepend(path, "lun-0x%04x%04x00000000", (lun & 0xffff), (lun >> 16) & 0xffff);
78 static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys)
80 struct udev_device *parent = dev;
82 while (parent != NULL) {
83 const char *subsystem;
85 subsystem = udev_device_get_subsystem(parent);
86 if (subsystem == NULL || strcmp(subsystem, subsys) != 0)
89 parent = udev_device_get_parent(parent);
94 static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path)
96 struct udev *udev = udev_device_get_udev(parent);
97 struct udev_device *targetdev;
98 struct udev_device *fcdev = NULL;
102 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
103 if (targetdev == NULL)
106 fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
109 port = udev_device_get_sysattr_value(fcdev, "port_name");
115 format_lun_number(parent, &lun);
116 path_prepend(path, "fc-%s-%s", port, lun);
120 udev_device_unref(fcdev);
124 static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
126 struct udev *udev = udev_device_get_udev(parent);
127 struct udev_device *targetdev;
128 struct udev_device *target_parent;
129 struct udev_device *sasdev;
130 const char *sas_address;
133 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
134 if (targetdev == NULL)
137 target_parent = udev_device_get_parent(targetdev);
138 if (target_parent == NULL)
141 sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
142 udev_device_get_sysname(target_parent));
146 sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
147 if (sas_address == NULL) {
152 format_lun_number(parent, &lun);
153 path_prepend(path, "sas-%s-%s", sas_address, lun);
157 udev_device_unref(sasdev);
161 static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
163 struct udev *udev = udev_device_get_udev(parent);
164 struct udev_device *transportdev;
165 struct udev_device *sessiondev = NULL;
168 struct udev_device *conndev = NULL;
173 /* find iscsi session */
174 transportdev = parent;
176 transportdev = udev_device_get_parent(transportdev);
177 if (transportdev == NULL)
179 if (startswith(udev_device_get_sysname(transportdev), "session"))
183 /* find iscsi session device */
184 sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
185 if (sessiondev == NULL)
187 target = udev_device_get_sysattr_value(sessiondev, "targetname");
188 if (target == NULL) {
193 if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
197 conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
199 if (conndev == NULL) {
203 addr = udev_device_get_sysattr_value(conndev, "persistent_address");
204 port = udev_device_get_sysattr_value(conndev, "persistent_port");
205 if (addr == NULL || port == NULL) {
210 format_lun_number(parent, &lun);
211 path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
215 udev_device_unref(sessiondev);
216 udev_device_unref(conndev);
220 static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path)
222 struct udev_device *hostdev;
223 int host, bus, target, lun;
231 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
235 name = udev_device_get_sysname(parent);
236 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
239 /* rebase host offset to get the local relative number */
241 base = strdup(udev_device_get_syspath(hostdev));
244 pos = strrchr(base, '/');
255 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
259 if (dent->d_name[0] == '.')
261 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
263 if (!startswith(dent->d_name, "host"))
265 i = strtoul(&dent->d_name[4], &rest, 10);
269 * find the smallest number; the host really needs to export its
270 * own instance number per parent device; relying on the global host
271 * enumeration and plainly rebasing the numbers sounds unreliable
273 if (basenum == -1 || i < basenum)
283 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
289 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
295 devtype = udev_device_get_devtype(parent);
296 if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
300 id = udev_device_get_sysattr_value(parent, "ieee1394_id");
302 parent = skip_subsystem(parent, "scsi");
303 path_prepend(path, "ieee1394-0x%s", id);
307 /* lousy scsi sysfs does not have a "subsystem" for the transport */
308 name = udev_device_get_syspath(parent);
310 if (strstr(name, "/rport-") != NULL) {
311 parent = handle_scsi_fibre_channel(parent, path);
315 if (strstr(name, "/end_device-") != NULL) {
316 parent = handle_scsi_sas(parent, path);
320 if (strstr(name, "/session") != NULL) {
321 parent = handle_scsi_iscsi(parent, path);
326 * We do not support the ATA transport class, it creates duplicated link
327 * names as the fake SCSI host adapters are all separated, they are all
328 * re-based as host == 0. ATA should just stop faking two duplicated
329 * hierarchies for a single topology and leave the SCSI stuff alone;
330 * until that happens, there are no by-path/ links for ATA devices behind
331 * an ATA transport class.
333 if (strstr(name, "/ata") != NULL) {
338 parent = handle_scsi_default(parent, path);
343 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
346 unsigned int controller, disk;
348 str = udev_device_get_sysname(parent);
349 if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
352 path_prepend(path, "cciss-disk%u", disk);
353 parent = skip_subsystem(parent, "cciss");
357 static void handle_scsi_tape(struct udev_device *dev, char **path)
361 /* must be the last device in the syspath */
365 name = udev_device_get_sysname(dev);
366 if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
367 path_prepend(path, "nst%c", name[3]);
368 else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
369 path_prepend(path, "st%c", name[2]);
372 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
378 devtype = udev_device_get_devtype(parent);
381 if (strcmp(devtype, "usb_interface") != 0 && strcmp(devtype, "usb_device") != 0)
384 str = udev_device_get_sysname(parent);
385 port = strchr(str, '-');
390 parent = skip_subsystem(parent, "usb");
391 path_prepend(path, "usb-0:%s", port);
395 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
397 struct udev_device *scsi_dev;
399 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
400 if (scsi_dev != NULL) {
405 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
406 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
407 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
408 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
409 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
414 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
416 parent = skip_subsystem(parent, "ccw");
420 static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test)
422 struct udev_device *parent;
424 bool some_transport = false;
427 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
428 if (parent != NULL) {
429 handle_ccw(parent, dev, &path);
433 /* walk up the chain of devices and compose path */
435 while (parent != NULL) {
438 subsys = udev_device_get_subsystem(parent);
439 if (subsys == NULL) {
441 } else if (strcmp(subsys, "scsi_tape") == 0) {
442 handle_scsi_tape(parent, &path);
443 } else if (strcmp(subsys, "scsi") == 0) {
444 parent = handle_scsi(parent, &path);
445 some_transport = true;
446 } else if (strcmp(subsys, "cciss") == 0) {
447 parent = handle_cciss(parent, &path);
448 some_transport = true;
449 } else if (strcmp(subsys, "usb") == 0) {
450 parent = handle_usb(parent, &path);
451 some_transport = true;
452 } else if (strcmp(subsys, "serio") == 0) {
453 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
454 parent = skip_subsystem(parent, "serio");
455 } else if (strcmp(subsys, "pci") == 0) {
456 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
457 parent = skip_subsystem(parent, "pci");
458 } else if (strcmp(subsys, "platform") == 0) {
459 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
460 parent = skip_subsystem(parent, "platform");
461 some_transport = true;
462 } else if (strcmp(subsys, "acpi") == 0) {
463 path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
464 parent = skip_subsystem(parent, "acpi");
465 } else if (strcmp(subsys, "xen") == 0) {
466 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
467 parent = skip_subsystem(parent, "xen");
468 } else if (strcmp(subsys, "virtio") == 0) {
469 path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent));
470 parent = skip_subsystem(parent, "virtio");
473 parent = udev_device_get_parent(parent);
477 * Do not return a single-parent-device-only for block
478 * devices, they might have entire buses behind it which
479 * do not get unique IDs only by using the parent device.
481 if (!some_transport && streq(udev_device_get_subsystem(dev), "block")) {
488 char tag[UTIL_NAME_SIZE];
492 /* compose valid udev tag name */
493 for (p = path, i = 0; *p; p++) {
494 if ((*p >= '0' && *p <= '9') ||
495 (*p >= 'A' && *p <= 'Z') ||
496 (*p >= 'a' && *p <= 'z') ||
502 /* skip all leading '_' */
506 /* avoid second '_' */
512 /* strip trailing '_' */
513 while (i > 0 && tag[i-1] == '_')
517 udev_builtin_add_property(dev, test, "ID_PATH", path);
518 udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
525 const struct udev_builtin udev_builtin_path_id = {
527 .cmd = builtin_path_id,
528 .help = "compose persistent device path",