udev-builtin-path_id.c revision 89f17d4f807f2fbd9918b513296e25ee2be108c3
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * compose persistent device path
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Logic based on Hannes Reinecke's shell script.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is free software: you can redistribute it and/or modify
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * it under the terms of the GNU General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is distributed in the hope that it will be useful,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * GNU General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * You should have received a copy of the GNU General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdio.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdlib.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdarg.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <ctype.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <fcntl.h>
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <dirent.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <getopt.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "udev.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int path_prepend(char **path, const char *fmt, ...)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering va_list va;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering char *pre;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int err = 0;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering va_start(va, fmt);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering err = vasprintf(&pre, fmt, va);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering va_end(va);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (err < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (*path != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *new;
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering err = asprintf(&new, "%s-%s", pre, *path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(pre);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (err < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(*path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *path = new;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *path = pre;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return err;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen/*
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen** Linux only supports 32 bit luns.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int format_lun_number(struct udev_device *dev, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* address method 0, peripheral device addressing with bus id of zero */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (lun < 256)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return path_prepend(path, "lun-%d", lun);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* handle all other lun addressing methods by using a variant of the original lun format */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return path_prepend(path, "lun-0x%04x%04x00000000", (lun & 0xffff), (lun >> 16) & 0xffff);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen{
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct udev_device *parent = dev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while (parent != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *subsystem;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering subsystem = udev_device_get_subsystem(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (subsystem == NULL || !streq(subsystem, subsys))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen break;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen dev = parent;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = udev_device_get_parent(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return dev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct udev *udev = udev_device_get_udev(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct udev_device *targetdev;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct udev_device *fcdev = NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *port;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char *lun = NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (targetdev == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (fcdev == NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen port = udev_device_get_sysattr_value(fcdev, "port_name");
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (port == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen format_lun_number(parent, &lun);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering path_prepend(path, "fc-%s-%s", port, lun);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (lun)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering free(lun);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringout:
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering udev_device_unref(fcdev);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return parent;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poetteringstatic struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering{
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev_device *targetdev;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev_device *target_parent;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev_device *sasdev;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *sas_address;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering char *lun = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (targetdev == NULL)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return NULL;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering target_parent = udev_device_get_parent(targetdev);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (target_parent == NULL)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering udev_device_get_sysname(target_parent));
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (sasdev == NULL)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return NULL;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sas_address == NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering goto out;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering format_lun_number(parent, &lun);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering path_prepend(path, "sas-%s-%s", sas_address, lun);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (lun)
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering free(lun);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poetteringout:
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering udev_device_unref(sasdev);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return parent;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering{
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev_device *transportdev;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev_device *sessiondev = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *target;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering char *connname;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev_device *conndev = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *addr;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *port;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *lun = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* find iscsi session */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering transportdev = parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (;;) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering transportdev = udev_device_get_parent(transportdev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (transportdev == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (startswith(udev_device_get_sysname(transportdev), "session"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* find iscsi session device */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sessiondev == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering target = udev_device_get_sysattr_value(sessiondev, "targetname");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (target == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(connname);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (conndev == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering addr = udev_device_get_sysattr_value(conndev, "persistent_address");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering port = udev_device_get_sysattr_value(conndev, "persistent_port");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (addr == NULL || port == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering format_lun_number(parent, &lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (lun)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_device_unref(sessiondev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_device_unref(conndev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi_default(struct udev_device *parent, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_device *hostdev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int host, bus, target, lun;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *name;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *base;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *pos;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering DIR *dir;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct dirent *dent;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering int basenum;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (hostdev == NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen name = udev_device_get_sysname(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Rebase host offset to get the local relative number
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Note: This is by definition racy, unreliable and too simple.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Please do not copy this model anywhere. It's just a left-over
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * from the time we had no idea how things should look like in
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * the end.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen *
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Making assumptions about a global in-kernel counter and use
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * that to calculate a local offset is a very broken concept. It
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * can only work as long as things are in strict order.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * The kernel needs to export the instance/port number of a
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * controller directly, without the need for rebase magic like
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * this. Manual driver unbind/bind, parallel hotplug/unplug will
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * get into the way of this "I hope it works" logic.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering basenum = -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering base = strdup(udev_device_get_syspath(hostdev));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (base == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering pos = strrchr(base, '/');
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (pos == NULL) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering parent = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering pos[0] = '\0';
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dir = opendir(base);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (dir == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *rest;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen int i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (dent->d_name[0] == '.')
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!startswith(dent->d_name, "host"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen i = strtoul(&dent->d_name[4], &rest, 10);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (rest[0] != '\0')
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * find the smallest number; the host really needs to export its
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * own instance number per parent device; relying on the global host
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * enumeration and plainly rebasing the numbers sounds unreliable
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (basenum == -1 || i < basenum)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering basenum = i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen closedir(dir);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (basenum == -1) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen host -= basenum;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(base);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return hostdev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_device *hostdev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_device *vmbusdev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *guid_str;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *lun = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char guid[38];
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen size_t i, k;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!hostdev)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering vmbusdev = udev_device_get_parent(hostdev);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!vmbusdev)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!guid_str)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (strlen(guid_str) < 37 || guid_str[0] != '{' || guid_str[36] != '}')
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen for (i = 1, k = 0; i < 36; i++) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (guid_str[i] == '-')
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering guid[k++] = guid_str[i];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering guid[k] = '\0';
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering format_lun_number(parent, &lun);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen path_prepend(path, "vmbus-%s-%s", guid, lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi(struct udev_device *parent, char **path)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *devtype;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *name;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *id;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen devtype = udev_device_get_devtype(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (devtype == NULL || !streq(devtype, "scsi_device"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* firewire */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering id = udev_device_get_sysattr_value(parent, "ieee1394_id");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (id != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "scsi");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "ieee1394-0x%s", id);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* scsi sysfs does not have a "subsystem" for the transport */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen name = udev_device_get_syspath(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (strstr(name, "/rport-") != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_fibre_channel(parent, path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (strstr(name, "/end_device-") != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = handle_scsi_sas(parent, path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (strstr(name, "/session") != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = handle_scsi_iscsi(parent, path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * We do not support the ATA transport class, it uses global counters
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * to name the ata devices which numbers spread across multiple
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * controllers.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * The real link numbers are not exported. Also, possible chains of ports
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * behind port multipliers cannot be composed that way.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen *
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Until all that is solved at the kernel level, there are no by-path/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * links for ATA devices.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (strstr(name, "/ata") != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (strstr(name, "/vmbus_") != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = handle_scsi_hyperv(parent, path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_default(parent, path);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic struct udev_device *handle_cciss(struct udev_device *parent, char **path)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen{
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering const char *str;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned int controller, disk;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen str = udev_device_get_sysname(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "cciss-disk%u", disk);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "cciss");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void handle_scsi_tape(struct udev_device *dev, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *name;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* must be the last device in the syspath */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (*path != NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering name = udev_device_get_sysname(dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen path_prepend(path, "nst%c", name[3]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "st%c", name[2]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_usb(struct udev_device *parent, char **path)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen{
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *devtype;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *str;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *port;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen devtype = udev_device_get_devtype(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (devtype == NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return parent;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return parent;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen str = udev_device_get_sysname(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen port = strchr(str, '-');
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (port == NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return parent;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen port++;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering parent = skip_subsystem(parent, "usb");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "usb-0:%s", port);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_bcma(struct udev_device *parent, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *sysname;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned int core;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sysname = udev_device_get_sysname(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "bcma-%u", core);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_device *scsi_dev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (scsi_dev != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *wwpn;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *lun;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *hba_id;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (hba_id != NULL && lun != NULL && wwpn != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "ccw");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_device *parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *path = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool some_transport = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* S390 ccw bus */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (parent != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen handle_ccw(parent, dev, &path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* walk up the chain of devices and compose path */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = dev;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen while (parent != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *subsys;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering subsys = udev_device_get_subsystem(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (subsys == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "scsi_tape")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering handle_scsi_tape(parent, &path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "scsi")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi(parent, &path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering some_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "cciss")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_cciss(parent, &path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering some_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "usb")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_usb(parent, &path);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering some_transport = true;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering } else if (streq(subsys, "bcma")) {
53755121e1c8ebd3db0330bc82965ecf9a986449Lennart Poettering parent = handle_bcma(parent, &path);
53755121e1c8ebd3db0330bc82965ecf9a986449Lennart Poettering some_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "serio")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "serio");
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering } else if (streq(subsys, "pci")) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "pci");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "platform")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek parent = skip_subsystem(parent, "platform");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering some_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "acpi")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "acpi");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "xen")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "xen");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "virtio")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "virtio");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "scm")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "scm");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering some_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = udev_device_get_parent(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Do not return a single-parent-device-only for block
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * devices, they might have entire buses behind it which
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * do not get unique IDs only by using the parent device.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!some_transport && streq(udev_device_get_subsystem(dev), "block")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(path);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering path = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (path != NULL) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering char tag[UTIL_NAME_SIZE];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size_t i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* compose valid udev tag name */
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering for (p = path, i = 0; *p; p++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if ((*p >= '0' && *p <= '9') ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (*p >= 'A' && *p <= 'Z') ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (*p >= 'a' && *p <= 'z') ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *p == '-') {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering tag[i++] = *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* skip all leading '_' */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (i == 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering /* avoid second '_' */
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (tag[i-1] == '_')
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering continue;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering tag[i++] = '_';
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* strip trailing '_' */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while (i > 0 && tag[i-1] == '_')
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering i--;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering tag[i] = '\0';
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_builtin_add_property(dev, test, "ID_PATH", path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return EXIT_SUCCESS;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return EXIT_FAILURE;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringconst struct udev_builtin udev_builtin_path_id = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .name = "path_id",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .cmd = builtin_path_id,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .help = "compose persistent device path",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .run_once = true,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering