chiark / gitweb /
fstab-generator: Honor mount.usr*= on kernel command line
[elogind.git] / src / udev / udev-builtin-path_id.c
1 /*
2  * compose persistent device path
3  *
4  * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
5  *
6  * Logic based on Hannes Reinecke's shell script.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <dirent.h>
31 #include <getopt.h>
32
33 #include "udev.h"
34
35 _printf_(2,3)
36 static int path_prepend(char **path, const char *fmt, ...) {
37         va_list va;
38         char *pre;
39         int err = 0;
40
41         va_start(va, fmt);
42         err = vasprintf(&pre, fmt, va);
43         va_end(va);
44         if (err < 0)
45                 goto out;
46
47         if (*path != NULL) {
48                 char *new;
49
50                 err = asprintf(&new, "%s-%s", pre, *path);
51                 free(pre);
52                 if (err < 0)
53                         goto out;
54                 free(*path);
55                 *path = new;
56         } else {
57                 *path = pre;
58         }
59 out:
60         return err;
61 }
62
63 /*
64 ** Linux only supports 32 bit luns.
65 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
66 */
67 static int format_lun_number(struct udev_device *dev, char **path) {
68         unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
69
70         /* address method 0, peripheral device addressing with bus id of zero */
71         if (lun < 256)
72                 return path_prepend(path, "lun-%lu", lun);
73         /* handle all other lun addressing methods by using a variant of the original lun format */
74         return path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
75 }
76
77 static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys) {
78         struct udev_device *parent = dev;
79
80         while (parent != NULL) {
81                 const char *subsystem;
82
83                 subsystem = udev_device_get_subsystem(parent);
84                 if (subsystem == NULL || !streq(subsystem, subsys))
85                         break;
86                 dev = parent;
87                 parent = udev_device_get_parent(parent);
88         }
89         return dev;
90 }
91
92 static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path) {
93         struct udev *udev  = udev_device_get_udev(parent);
94         struct udev_device *targetdev;
95         struct udev_device *fcdev = NULL;
96         const char *port;
97         char *lun = NULL;
98
99         targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
100         if (targetdev == NULL)
101                 return NULL;
102
103         fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
104         if (fcdev == NULL)
105                 return NULL;
106         port = udev_device_get_sysattr_value(fcdev, "port_name");
107         if (port == NULL) {
108                 parent = NULL;
109                 goto out;
110         }
111
112         format_lun_number(parent, &lun);
113         path_prepend(path, "fc-%s-%s", port, lun);
114         if (lun)
115                 free(lun);
116 out:
117         udev_device_unref(fcdev);
118         return parent;
119 }
120
121 static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path) {
122         struct udev *udev  = udev_device_get_udev(parent);
123         struct udev_device *targetdev;
124         struct udev_device *target_parent;
125         struct udev_device *sasdev;
126         const char *sas_address;
127         char *lun = NULL;
128
129         targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
130         if (targetdev == NULL)
131                 return NULL;
132
133         target_parent = udev_device_get_parent(targetdev);
134         if (target_parent == NULL)
135                 return NULL;
136
137         sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
138                                 udev_device_get_sysname(target_parent));
139         if (sasdev == NULL)
140                 return NULL;
141
142         sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
143         if (sas_address == NULL) {
144                 parent = NULL;
145                 goto out;
146         }
147
148         format_lun_number(parent, &lun);
149         path_prepend(path, "sas-%s-%s", sas_address, lun);
150         if (lun)
151                 free(lun);
152 out:
153         udev_device_unref(sasdev);
154         return parent;
155 }
156
157 static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) {
158         struct udev *udev  = udev_device_get_udev(parent);
159         struct udev_device *transportdev;
160         struct udev_device *sessiondev = NULL;
161         const char *target;
162         char *connname;
163         struct udev_device *conndev = NULL;
164         const char *addr;
165         const char *port;
166         char *lun = NULL;
167
168         /* find iscsi session */
169         transportdev = parent;
170         for (;;) {
171                 transportdev = udev_device_get_parent(transportdev);
172                 if (transportdev == NULL)
173                         return NULL;
174                 if (startswith(udev_device_get_sysname(transportdev), "session"))
175                         break;
176         }
177
178         /* find iscsi session device */
179         sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
180         if (sessiondev == NULL)
181                 return NULL;
182         target = udev_device_get_sysattr_value(sessiondev, "targetname");
183         if (target == NULL) {
184                 parent = NULL;
185                 goto out;
186         }
187
188         if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
189                 parent = NULL;
190                 goto out;
191         }
192         conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
193         free(connname);
194         if (conndev == NULL) {
195                 parent = NULL;
196                 goto out;
197         }
198         addr = udev_device_get_sysattr_value(conndev, "persistent_address");
199         port = udev_device_get_sysattr_value(conndev, "persistent_port");
200         if (addr == NULL || port == NULL) {
201                 parent = NULL;
202                 goto out;
203         }
204
205         format_lun_number(parent, &lun);
206         path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
207         if (lun)
208                 free(lun);
209 out:
210         udev_device_unref(sessiondev);
211         udev_device_unref(conndev);
212         return parent;
213 }
214
215 static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path) {
216         struct udev_device *hostdev;
217         int host, bus, target, lun;
218         const char *name;
219         char *base;
220         char *pos;
221         DIR *dir;
222         struct dirent *dent;
223         int basenum;
224
225         hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
226         if (hostdev == NULL)
227                 return NULL;
228
229         name = udev_device_get_sysname(parent);
230         if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
231                 return NULL;
232
233         /*
234          * Rebase host offset to get the local relative number
235          *
236          * Note: This is by definition racy, unreliable and too simple.
237          * Please do not copy this model anywhere. It's just a left-over
238          * from the time we had no idea how things should look like in
239          * the end.
240          *
241          * Making assumptions about a global in-kernel counter and use
242          * that to calculate a local offset is a very broken concept. It
243          * can only work as long as things are in strict order.
244          *
245          * The kernel needs to export the instance/port number of a
246          * controller directly, without the need for rebase magic like
247          * this. Manual driver unbind/bind, parallel hotplug/unplug will
248          * get into the way of this "I hope it works" logic.
249          */
250         basenum = -1;
251         base = strdup(udev_device_get_syspath(hostdev));
252         if (base == NULL)
253                 return NULL;
254         pos = strrchr(base, '/');
255         if (pos == NULL) {
256                 parent = NULL;
257                 goto out;
258         }
259         pos[0] = '\0';
260         dir = opendir(base);
261         if (dir == NULL) {
262                 parent = NULL;
263                 goto out;
264         }
265         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
266                 char *rest;
267                 int i;
268
269                 if (dent->d_name[0] == '.')
270                         continue;
271                 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
272                         continue;
273                 if (!startswith(dent->d_name, "host"))
274                         continue;
275                 i = strtoul(&dent->d_name[4], &rest, 10);
276                 if (rest[0] != '\0')
277                         continue;
278                 /*
279                  * find the smallest number; the host really needs to export its
280                  * own instance number per parent device; relying on the global host
281                  * enumeration and plainly rebasing the numbers sounds unreliable
282                  */
283                 if (basenum == -1 || i < basenum)
284                         basenum = i;
285         }
286         closedir(dir);
287         if (basenum == -1) {
288                 parent = NULL;
289                 goto out;
290         }
291         host -= basenum;
292
293         path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
294 out:
295         free(base);
296         return hostdev;
297 }
298
299 static struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path) {
300         struct udev_device *hostdev;
301         struct udev_device *vmbusdev;
302         const char *guid_str;
303         char *lun = NULL;
304         char guid[38];
305         size_t i, k;
306
307         hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
308         if (!hostdev)
309                 return NULL;
310
311         vmbusdev = udev_device_get_parent(hostdev);
312         if (!vmbusdev)
313                 return NULL;
314
315         guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
316         if (!guid_str)
317                 return NULL;
318
319         if (strlen(guid_str) < 37 || guid_str[0] != '{' || guid_str[36] != '}')
320                 return NULL;
321
322         for (i = 1, k = 0; i < 36; i++) {
323                 if (guid_str[i] == '-')
324                         continue;
325                 guid[k++] = guid_str[i];
326         }
327         guid[k] = '\0';
328
329         format_lun_number(parent, &lun);
330         path_prepend(path, "vmbus-%s-%s", guid, lun);
331         free(lun);
332         return parent;
333 }
334
335 static struct udev_device *handle_scsi(struct udev_device *parent, char **path) {
336         const char *devtype;
337         const char *name;
338         const char *id;
339
340         devtype = udev_device_get_devtype(parent);
341         if (devtype == NULL || !streq(devtype, "scsi_device"))
342                 return parent;
343
344         /* firewire */
345         id = udev_device_get_sysattr_value(parent, "ieee1394_id");
346         if (id != NULL) {
347                 parent = skip_subsystem(parent, "scsi");
348                 path_prepend(path, "ieee1394-0x%s", id);
349                 goto out;
350         }
351
352         /* scsi sysfs does not have a "subsystem" for the transport */
353         name = udev_device_get_syspath(parent);
354
355         if (strstr(name, "/rport-") != NULL) {
356                 parent = handle_scsi_fibre_channel(parent, path);
357                 goto out;
358         }
359
360         if (strstr(name, "/end_device-") != NULL) {
361                 parent = handle_scsi_sas(parent, path);
362                 goto out;
363         }
364
365         if (strstr(name, "/session") != NULL) {
366                 parent = handle_scsi_iscsi(parent, path);
367                 goto out;
368         }
369
370         /*
371          * We do not support the ATA transport class, it uses global counters
372          * to name the ata devices which numbers spread across multiple
373          * controllers.
374          *
375          * The real link numbers are not exported. Also, possible chains of ports
376          * behind port multipliers cannot be composed that way.
377          *
378          * Until all that is solved at the kernel level, there are no by-path/
379          * links for ATA devices.
380          */
381         if (strstr(name, "/ata") != NULL) {
382                 parent = NULL;
383                 goto out;
384         }
385
386         if (strstr(name, "/vmbus_") != NULL) {
387                 parent = handle_scsi_hyperv(parent, path);
388                 goto out;
389         }
390
391         parent = handle_scsi_default(parent, path);
392 out:
393         return parent;
394 }
395
396 static struct udev_device *handle_cciss(struct udev_device *parent, char **path) {
397         const char *str;
398         unsigned int controller, disk;
399
400         str = udev_device_get_sysname(parent);
401         if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
402                 return NULL;
403
404         path_prepend(path, "cciss-disk%u", disk);
405         parent = skip_subsystem(parent, "cciss");
406         return parent;
407 }
408
409 static void handle_scsi_tape(struct udev_device *dev, char **path) {
410         const char *name;
411
412         /* must be the last device in the syspath */
413         if (*path != NULL)
414                 return;
415
416         name = udev_device_get_sysname(dev);
417         if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
418                 path_prepend(path, "nst%c", name[3]);
419         else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
420                 path_prepend(path, "st%c", name[2]);
421 }
422
423 static struct udev_device *handle_usb(struct udev_device *parent, char **path) {
424         const char *devtype;
425         const char *str;
426         const char *port;
427
428         devtype = udev_device_get_devtype(parent);
429         if (devtype == NULL)
430                 return parent;
431         if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
432                 return parent;
433
434         str = udev_device_get_sysname(parent);
435         port = strchr(str, '-');
436         if (port == NULL)
437                 return parent;
438         port++;
439
440         parent = skip_subsystem(parent, "usb");
441         path_prepend(path, "usb-0:%s", port);
442         return parent;
443 }
444
445 static struct udev_device *handle_bcma(struct udev_device *parent, char **path) {
446         const char *sysname;
447         unsigned int core;
448
449         sysname = udev_device_get_sysname(parent);
450         if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
451                 return NULL;
452
453         path_prepend(path, "bcma-%u", core);
454         return parent;
455 }
456
457 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path) {
458         struct udev_device *scsi_dev;
459
460         scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
461         if (scsi_dev != NULL) {
462                 const char *wwpn;
463                 const char *lun;
464                 const char *hba_id;
465
466                 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
467                 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
468                 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
469                 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
470                         path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
471                         goto out;
472                 }
473         }
474
475         path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
476 out:
477         parent = skip_subsystem(parent, "ccw");
478         return parent;
479 }
480
481 static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) {
482         struct udev_device *parent;
483         char *path = NULL;
484         bool supported_transport = false;
485         bool supported_parent = false;
486
487         /* S390 ccw bus */
488         parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
489         if (parent != NULL) {
490                 handle_ccw(parent, dev, &path);
491                 goto out;
492         }
493
494         /* walk up the chain of devices and compose path */
495         parent = dev;
496         while (parent != NULL) {
497                 const char *subsys;
498
499                 subsys = udev_device_get_subsystem(parent);
500                 if (subsys == NULL) {
501                         ;
502                 } else if (streq(subsys, "scsi_tape")) {
503                         handle_scsi_tape(parent, &path);
504                 } else if (streq(subsys, "scsi")) {
505                         parent = handle_scsi(parent, &path);
506                         supported_transport = true;
507                 } else if (streq(subsys, "cciss")) {
508                         parent = handle_cciss(parent, &path);
509                         supported_transport = true;
510                 } else if (streq(subsys, "usb")) {
511                         parent = handle_usb(parent, &path);
512                         supported_transport = true;
513                 } else if (streq(subsys, "bcma")) {
514                         parent = handle_bcma(parent, &path);
515                         supported_transport = true;
516                 } else if (streq(subsys, "serio")) {
517                         path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
518                         parent = skip_subsystem(parent, "serio");
519                 } else if (streq(subsys, "pci")) {
520                         path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
521                         parent = skip_subsystem(parent, "pci");
522                         supported_parent = true;
523                 } else if (streq(subsys, "platform")) {
524                         path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
525                         parent = skip_subsystem(parent, "platform");
526                         supported_transport = true;
527                         supported_parent = true;
528                 } else if (streq(subsys, "acpi")) {
529                         path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
530                         parent = skip_subsystem(parent, "acpi");
531                         supported_parent = true;
532                 } else if (streq(subsys, "xen")) {
533                         path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
534                         parent = skip_subsystem(parent, "xen");
535                         supported_parent = true;
536                 } else if (streq(subsys, "scm")) {
537                         path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
538                         parent = skip_subsystem(parent, "scm");
539                         supported_transport = true;
540                         supported_parent = true;
541                 }
542
543                 parent = udev_device_get_parent(parent);
544         }
545
546         /*
547          * Do return devices with have an unknown type of parent device, they
548          * might produce conflicting IDs below multiple independent parent
549          * devices.
550          */
551         if (!supported_parent) {
552                 free(path);
553                 path = NULL;
554         }
555
556         /*
557          * Do not return a have-only a single-parent block devices, some
558          * have entire hidden buses behind it, and not create predictable
559          * IDs that way.
560          */
561         if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport) {
562                 free(path);
563                 path = NULL;
564         }
565
566 out:
567         if (path != NULL) {
568                 char tag[UTIL_NAME_SIZE];
569                 size_t i;
570                 const char *p;
571
572                 /* compose valid udev tag name */
573                 for (p = path, i = 0; *p; p++) {
574                         if ((*p >= '0' && *p <= '9') ||
575                             (*p >= 'A' && *p <= 'Z') ||
576                             (*p >= 'a' && *p <= 'z') ||
577                             *p == '-') {
578                                 tag[i++] = *p;
579                                 continue;
580                         }
581
582                         /* skip all leading '_' */
583                         if (i == 0)
584                                 continue;
585
586                         /* avoid second '_' */
587                         if (tag[i-1] == '_')
588                                 continue;
589
590                         tag[i++] = '_';
591                 }
592                 /* strip trailing '_' */
593                 while (i > 0 && tag[i-1] == '_')
594                         i--;
595                 tag[i] = '\0';
596
597                 udev_builtin_add_property(dev, test, "ID_PATH", path);
598                 udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
599                 free(path);
600                 return EXIT_SUCCESS;
601         }
602         return EXIT_FAILURE;
603 }
604
605 const struct udev_builtin udev_builtin_path_id = {
606         .name = "path_id",
607         .cmd = builtin_path_id,
608         .help = "compose persistent device path",
609         .run_once = true,
610 };