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