udev-builtin-path_id.c revision ba86822db70d9ffd02ad78cd02b237ff8c569c7a
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * compose persistent device path
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Logic based on Hannes Reinecke's shell script.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * This program is free software: you can redistribute it and/or modify
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * it under the terms of the GNU General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * the Free Software Foundation, either version 2 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * This program is distributed in the hope that it will be useful,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * but WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * GNU General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * You should have received a copy of the GNU General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * along with this program. If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdio.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdlib.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdarg.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <unistd.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <string.h>
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk#include <ctype.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <fcntl.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <errno.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <dirent.h>
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include <getopt.h>
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "udev.h"
a5c32cff1f56afe6f0c6c70d91a88a7a8238b2d7Harald Hoyer
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_printf_(2,3)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int path_prepend(char **path, const char *fmt, ...) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek va_list va;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *pre;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int err = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek va_start(va, fmt);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek err = vasprintf(&pre, fmt, va);
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering va_end(va);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (err < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (*path != NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *new;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek err = asprintf(&new, "%s-%s", pre, *path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(pre);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (err < 0)
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(*path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *path = new;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *path = pre;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekout:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return err;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek** Linux only supports 32 bit luns.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek*/
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic int format_lun_number(struct udev_device *dev, char **path) {
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* address method 0, peripheral device addressing with bus id of zero */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (lun < 256)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return path_prepend(path, "lun-%lu", lun);
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack /* handle all other lun addressing methods by using a variant of the original lun format */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *parent = dev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(dev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(subsys);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack while (parent != NULL) {
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack const char *subsystem;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack subsystem = udev_device_get_subsystem(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (subsystem == NULL || !streq(subsystem, subsys))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dev = parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = udev_device_get_parent(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return dev;
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek}
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev *udev = udev_device_get_udev(parent);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek struct udev_device *targetdev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *fcdev = NULL;
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer const char *port;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *lun = NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (targetdev == NULL)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fcdev == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek port = udev_device_get_sysattr_value(fcdev, "port_name");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (port == NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek format_lun_number(parent, &lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "fc-%s-%s", port, lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekout:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_unref(fcdev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct udev_device *handle_scsi_sas_wide_port(struct udev_device *parent, char **path) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev *udev = udev_device_get_udev(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *targetdev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *target_parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *sasdev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *sas_address;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *lun = NULL;
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (targetdev == NULL)
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer return NULL;
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer target_parent = udev_device_get_parent(targetdev);
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer if (target_parent == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_get_sysname(target_parent));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sasdev == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sas_address == NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek format_lun_number(parent, &lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "sas-%s-%s", sas_address, lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekout:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_unref(sasdev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return parent;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek}
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmekstatic struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek{
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek struct udev *udev = udev_device_get_udev(parent);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek struct udev_device *targetdev;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek struct udev_device *target_parent;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek struct udev_device *port;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek struct udev_device *expander;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek struct udev_device *target_sasdev = NULL;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek struct udev_device *expander_sasdev = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *port_sasdev = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *sas_address = NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek const char *phy_id;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek const char *phy_count;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek char *lun = NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert(parent);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek assert(path);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (targetdev == NULL)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek target_parent = udev_device_get_parent(targetdev);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek if (target_parent == NULL)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return NULL;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek /* Get sas device */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek target_sasdev = udev_device_new_from_subsystem_sysname(udev,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "sas_device", udev_device_get_sysname(target_parent));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (target_sasdev == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* The next parent is sas port */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek port = udev_device_get_parent(target_parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (port == NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering /* Get port device */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek port_sasdev = udev_device_new_from_subsystem_sysname(udev,
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering "sas_port", udev_device_get_sysname(port));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek phy_count = udev_device_get_sysattr_value(port_sasdev, "num_phys");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (phy_count == NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Check if we are simple disk */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strncmp(phy_count, "1", 2) != 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = handle_scsi_sas_wide_port(parent, path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Get connected phy */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek phy_id = udev_device_get_sysattr_value(target_sasdev, "phy_identifier");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (phy_id == NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* The port's parent is either hba or expander */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek expander = udev_device_get_parent(port);
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) if (expander == NULL) {
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) }
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Get expander device */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek expander_sasdev = udev_device_new_from_subsystem_sysname(udev,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "sas_device", udev_device_get_sysname(expander));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (expander_sasdev != NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Get expander's address */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sas_address = udev_device_get_sysattr_value(expander_sasdev,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "sas_address");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sas_address == NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek format_lun_number(parent, &lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sas_address)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "sas-exp%s-phy%s-%s", sas_address, phy_id, lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "sas-phy%s-%s", phy_id, lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekout:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_unref(target_sasdev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_unref(expander_sasdev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek udev_device_unref(port_sasdev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev *udev = udev_device_get_udev(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *transportdev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *sessiondev = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *target;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *connname;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *conndev = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *addr;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *port;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *lun = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* find iscsi session */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek transportdev = parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (;;) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek transportdev = udev_device_get_parent(transportdev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (transportdev == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (startswith(udev_device_get_sysname(transportdev), "session"))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* find iscsi session device */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sessiondev == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek target = udev_device_get_sysattr_value(sessiondev, "targetname");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (target == NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek free(connname);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (conndev == NULL) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek parent = NULL;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek goto out;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek addr = udev_device_get_sysattr_value(conndev, "persistent_address");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek port = udev_device_get_sysattr_value(conndev, "persistent_port");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (addr == NULL || port == NULL) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek parent = NULL;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek goto out;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek format_lun_number(parent, &lun);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek free(lun);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekout:
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek udev_device_unref(sessiondev);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek udev_device_unref(conndev);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return parent;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct udev_device *handle_scsi_ata(struct udev_device *parent, char **path) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev *udev = udev_device_get_udev(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *targetdev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *target_parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *atadev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *port_no;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(parent);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (!targetdev)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek target_parent = udev_device_get_parent(targetdev);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (!target_parent)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek atadev = udev_device_new_from_subsystem_sysname(udev, "ata_port", udev_device_get_sysname(target_parent));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!atadev)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return NULL;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk port_no = udev_device_get_sysattr_value(atadev, "port_no");
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (!port_no) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk parent = NULL;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto out;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk path_prepend(path, "ata-%s", port_no);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukout:
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk udev_device_unref(atadev);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return parent;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk}
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic struct udev_device *handle_scsi_default(struct udev_device *parent, char **path) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk struct udev_device *hostdev;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk int host, bus, target, lun;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk const char *name;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk char *base;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering char *pos;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering DIR *dir;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering struct dirent *dent;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering int basenum;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert(parent);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert(path);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (hostdev == NULL)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return NULL;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek name = udev_device_get_sysname(parent);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return NULL;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek /*
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * Rebase host offset to get the local relative number
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek *
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * Note: This is by definition racy, unreliable and too simple.
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * Please do not copy this model anywhere. It's just a left-over
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * from the time we had no idea how things should look like in
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek * the end.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Making assumptions about a global in-kernel counter and use
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * that to calculate a local offset is a very broken concept. It
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * can only work as long as things are in strict order.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * The kernel needs to export the instance/port number of a
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * controller directly, without the need for rebase magic like
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * this. Manual driver unbind/bind, parallel hotplug/unplug will
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * get into the way of this "I hope it works" logic.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek basenum = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek base = strdup(udev_device_get_syspath(hostdev));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (base == NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek pos = strrchr(base, '/');
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (pos == NULL) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek parent = NULL;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek pos[0] = '\0';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dir = opendir(base);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (dir == NULL) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering parent = NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering goto out;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char *rest;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int i;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (dent->d_name[0] == '.')
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering continue;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering continue;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!startswith(dent->d_name, "host"))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering continue;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering i = strtoul(&dent->d_name[4], &rest, 10);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (rest[0] != '\0')
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering continue;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /*
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * find the smallest number; the host really needs to export its
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * own instance number per parent device; relying on the global host
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * enumeration and plainly rebasing the numbers sounds unreliable
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (basenum == -1 || i < basenum)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering basenum = i;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering closedir(dir);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (basenum == -1) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering parent = NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering goto out;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering host -= basenum;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringout:
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering free(base);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return hostdev;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering struct udev_device *hostdev;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering struct udev_device *vmbusdev;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering const char *guid_str;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char *lun = NULL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char guid[38];
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering size_t i, k;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!hostdev)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek vmbusdev = udev_device_get_parent(hostdev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!vmbusdev)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!guid_str)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strlen(guid_str) < 37 || guid_str[0] != '{' || guid_str[36] != '}')
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (i = 1, k = 0; i < 36; i++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (guid_str[i] == '-')
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek guid[k++] = guid_str[i];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek guid[k] = '\0';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek format_lun_number(parent, &lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "vmbus-%s-%s", guid, lun);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(lun);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering return parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct udev_device *handle_scsi(struct udev_device *parent, char **path, bool *supported_parent) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *devtype;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *name;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *id;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek devtype = udev_device_get_devtype(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (devtype == NULL || !streq(devtype, "scsi_device"))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* firewire */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek id = udev_device_get_sysattr_value(parent, "ieee1394_id");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (id != NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = skip_subsystem(parent, "scsi");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "ieee1394-0x%s", id);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *supported_parent = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* scsi sysfs does not have a "subsystem" for the transport */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek name = udev_device_get_syspath(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strstr(name, "/rport-") != NULL) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk parent = handle_scsi_fibre_channel(parent, path);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering *supported_parent = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strstr(name, "/end_device-") != NULL) {
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek parent = handle_scsi_sas(parent, path);
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek *supported_parent = true;
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek goto out;
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek }
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek if (strstr(name, "/session") != NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = handle_scsi_iscsi(parent, path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *supported_parent = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strstr(name, "/ata") != NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = handle_scsi_ata(parent, path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto out;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (strstr(name, "/vmbus_") != NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = handle_scsi_hyperv(parent, path);
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek goto out;
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek }
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek parent = handle_scsi_default(parent, path);
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmekout:
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek return parent;
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek}
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poetteringstatic struct udev_device *handle_cciss(struct udev_device *parent, char **path) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *str;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned int controller, disk;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek str = udev_device_get_sysname(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "cciss-disk%u", disk);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek parent = skip_subsystem(parent, "cciss");
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering return parent;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic void handle_scsi_tape(struct udev_device *dev, char **path) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering const char *name;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering /* must be the last device in the syspath */
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler if (*path != NULL)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek name = udev_device_get_sysname(dev);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek path_prepend(path, "nst%c", name[3]);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "st%c", name[2]);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering
82499507b369fea3033a74c22813bf423301aef4Lennart Poetteringstatic struct udev_device *handle_usb(struct udev_device *parent, char **path) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering const char *devtype;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek const char *str;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek const char *port;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek devtype = udev_device_get_devtype(parent);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (devtype == NULL)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return parent;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek str = udev_device_get_sysname(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek port = strchr(str, '-');
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (port == NULL)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return parent;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek port++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = skip_subsystem(parent, "usb");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(path, "usb-0:%s", port);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return parent;
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic struct udev_device *handle_bcma(struct udev_device *parent, char **path) {
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther const char *sysname;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned int core;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther sysname = udev_device_get_sysname(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek path_prepend(path, "bcma-%u", core);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return parent;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *scsi_dev;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(parent);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(dev);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn if (scsi_dev != NULL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *wwpn;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek const char *lun;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *hba_id;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden if (hba_id != NULL && lun != NULL && wwpn != NULL) {
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden goto out;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekout:
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering parent = skip_subsystem(parent, "ccw");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return parent;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct udev_device *parent;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering char *path = NULL;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering bool supported_transport = false;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering bool supported_parent = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert(dev);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering /* S390 ccw bus */
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (parent != NULL) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek handle_ccw(parent, dev, &path);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek goto out;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering /* walk up the chain of devices and compose path */
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering parent = dev;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering while (parent != NULL) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering const char *subsys;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering subsys = udev_device_get_subsystem(parent);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (subsys == NULL) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering ;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering } else if (streq(subsys, "scsi_tape")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek handle_scsi_tape(parent, &path);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering } else if (streq(subsys, "scsi")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = handle_scsi(parent, &path, &supported_parent);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering supported_transport = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(subsys, "cciss")) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering parent = handle_cciss(parent, &path);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek supported_transport = true;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering } else if (streq(subsys, "usb")) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering parent = handle_usb(parent, &path);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering supported_transport = true;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering } else if (streq(subsys, "bcma")) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering parent = handle_bcma(parent, &path);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering supported_transport = true;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering } else if (streq(subsys, "serio")) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek parent = skip_subsystem(parent, "serio");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering } else if (streq(subsys, "pci")) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering parent = skip_subsystem(parent, "pci");
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering supported_parent = true;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering } else if (streq(subsys, "platform")) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering parent = skip_subsystem(parent, "platform");
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering supported_transport = true;
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering supported_parent = true;
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering } else if (streq(subsys, "acpi")) {
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering parent = skip_subsystem(parent, "acpi");
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering supported_parent = true;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering } else if (streq(subsys, "xen")) {
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee parent = skip_subsystem(parent, "xen");
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee supported_parent = true;
ef1673d16907726d83bdff2e57b5261997a85020Mirco Tischler } else if (streq(subsys, "scm")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek parent = skip_subsystem(parent, "scm");
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering supported_transport = true;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering supported_parent = true;
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (parent)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering parent = udev_device_get_parent(parent);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering }
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /*
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering * Do not return devices with an unknown parent device type. They
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering * might produce conflicting IDs if the parent does not provide a
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman * unique and predictable name.
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering */
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (!supported_parent)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering path = mfree(path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /*
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * Do not return block devices without a well-known transport. Some
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * devices do not expose their buses and do not provide a unique
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek * and predictable name that way.
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek path = mfree(path);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekout:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (path != NULL) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek char tag[UTIL_NAME_SIZE];
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek size_t i;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek const char *p;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* compose valid udev tag name */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (p = path, i = 0; *p; p++) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if ((*p >= '0' && *p <= '9') ||
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek (*p >= 'A' && *p <= 'Z') ||
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek (*p >= 'a' && *p <= 'z') ||
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *p == '-') {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek tag[i++] = *p;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek continue;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* skip all leading '_' */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (i == 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek continue;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* avoid second '_' */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (tag[i-1] == '_')
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek continue;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek tag[i++] = '_';
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* strip trailing '_' */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek while (i > 0 && tag[i-1] == '_')
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek i--;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek tag[i] = '\0';
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek udev_builtin_add_property(dev, test, "ID_PATH", path);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek free(path);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return EXIT_SUCCESS;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return EXIT_FAILURE;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekconst struct udev_builtin udev_builtin_path_id = {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek .name = "path_id",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek .cmd = builtin_path_id,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek .help = "Compose persistent device path",
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek .run_once = true,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek};
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering