xref: /freebsd/sys/contrib/openzfs/lib/libzutil/os/linux/zutil_device_path_os.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25  */
26 
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/efi_partition.h>
34 
35 #ifdef HAVE_LIBUDEV
36 #include <libudev.h>
37 #endif
38 
39 #include <libzutil.h>
40 
41 /*
42  * Append partition suffix to an otherwise fully qualified device path.
43  * This is used to generate the name the full path as its stored in
44  * ZPOOL_CONFIG_PATH for whole disk devices.  On success the new length
45  * of 'path' will be returned on error a negative value is returned.
46  */
47 int
zfs_append_partition(char * path,size_t max_len)48 zfs_append_partition(char *path, size_t max_len)
49 {
50 	int len = strlen(path);
51 
52 	if ((strncmp(path, UDISK_ROOT, strlen(UDISK_ROOT)) == 0) ||
53 	    (strncmp(path, ZVOL_ROOT, strlen(ZVOL_ROOT)) == 0)) {
54 		if (len + 6 >= max_len)
55 			return (-1);
56 
57 		(void) strcat(path, "-part1");
58 		len += 6;
59 	} else {
60 		if (len + 2 >= max_len)
61 			return (-1);
62 
63 		if (isdigit(path[len-1])) {
64 			(void) strcat(path, "p1");
65 			len += 2;
66 		} else {
67 			(void) strcat(path, "1");
68 			len += 1;
69 		}
70 	}
71 
72 	return (len);
73 }
74 
75 /*
76  * Remove partition suffix from a vdev path.  Partition suffixes may take three
77  * forms: "-partX", "pX", or "X", where X is a string of digits.  The second
78  * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
79  * third case only occurs when preceded by a string matching the regular
80  * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
81  *
82  * caller must free the returned string
83  */
84 char *
zfs_strip_partition(const char * path)85 zfs_strip_partition(const char *path)
86 {
87 	char *tmp = strdup(path);
88 	char *part = NULL, *d = NULL;
89 	if (!tmp)
90 		return (NULL);
91 
92 	if ((part = strstr(tmp, "-part")) && part != tmp) {
93 		d = part + 5;
94 	} else if ((part = strrchr(tmp, 'p')) &&
95 	    part > tmp + 1 && isdigit(*(part-1))) {
96 		d = part + 1;
97 	} else if ((tmp[0] == 'h' || tmp[0] == 's' || tmp[0] == 'v') &&
98 	    tmp[1] == 'd') {
99 		for (d = &tmp[2]; isalpha(*d); part = ++d) { }
100 	} else if (strncmp("xvd", tmp, 3) == 0) {
101 		for (d = &tmp[3]; isalpha(*d); part = ++d) { }
102 	}
103 	if (part && d && *d != '\0') {
104 		for (; isdigit(*d); d++) { }
105 		if (*d == '\0')
106 			*part = '\0';
107 	}
108 
109 	return (tmp);
110 }
111 
112 /*
113  * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
114  *
115  * path:	/dev/sda1
116  * returns:	/dev/sda
117  *
118  * Returned string must be freed.
119  */
120 static char *
zfs_strip_partition_path(const char * path)121 zfs_strip_partition_path(const char *path)
122 {
123 	char *newpath = strdup(path);
124 	char *sd_offset;
125 	char *new_sd;
126 
127 	if (!newpath)
128 		return (NULL);
129 
130 	/* Point to "sda1" part of "/dev/sda1" */
131 	sd_offset = strrchr(newpath, '/') + 1;
132 
133 	/* Get our new name "sda" */
134 	new_sd = zfs_strip_partition(sd_offset);
135 	if (!new_sd) {
136 		free(newpath);
137 		return (NULL);
138 	}
139 
140 	/* Paste the "sda" where "sda1" was */
141 	strlcpy(sd_offset, new_sd, strlen(sd_offset) + 1);
142 
143 	/* Free temporary "sda" */
144 	free(new_sd);
145 
146 	return (newpath);
147 }
148 
149 /*
150  * Strip the unwanted portion of a device path.
151  */
152 const char *
zfs_strip_path(const char * path)153 zfs_strip_path(const char *path)
154 {
155 	size_t spath_count;
156 	const char *const *spaths = zpool_default_search_paths(&spath_count);
157 
158 	for (size_t i = 0; i < spath_count; ++i)
159 		if (strncmp(path, spaths[i], strlen(spaths[i])) == 0 &&
160 		    path[strlen(spaths[i])] == '/')
161 			return (path + strlen(spaths[i]) + 1);
162 
163 	return (path);
164 }
165 
166 /*
167  * Read the contents of a sysfs file into an allocated buffer and remove the
168  * last newline.
169  *
170  * This is useful for reading sysfs files that return a single string.  Return
171  * an allocated string pointer on success, NULL otherwise.  Returned buffer
172  * must be freed by the user.
173  */
174 static char *
zfs_read_sysfs_file(char * filepath)175 zfs_read_sysfs_file(char *filepath)
176 {
177 	char buf[4096];	/* all sysfs files report 4k size */
178 	char *str = NULL;
179 
180 	FILE *fp = fopen(filepath, "r");
181 	if (fp == NULL) {
182 		return (NULL);
183 	}
184 	if (fgets(buf, sizeof (buf), fp) == buf) {
185 		/* success */
186 
187 		/* Remove the last newline (if any) */
188 		size_t len = strlen(buf);
189 		if (buf[len - 1] == '\n') {
190 			buf[len - 1] = '\0';
191 		}
192 		str = strdup(buf);
193 	}
194 
195 	fclose(fp);
196 
197 	return (str);
198 }
199 
200 /*
201  * Given a dev name like "nvme0n1", return the full PCI slot sysfs path to
202  * the drive (in /sys/bus/pci/slots).
203  *
204  * For example:
205  *     dev:            "nvme0n1"
206  *     returns:        "/sys/bus/pci/slots/0"
207  *
208  * 'dev' must be an NVMe device.
209  *
210  * Returned string must be freed.  Returns NULL on error or no sysfs path.
211  */
212 static char *
zfs_get_pci_slots_sys_path(const char * dev_name)213 zfs_get_pci_slots_sys_path(const char *dev_name)
214 {
215 	DIR *dp = NULL;
216 	struct dirent *ep;
217 	char *address1 = NULL;
218 	char *address2 = NULL;
219 	char *path = NULL;
220 	char buf[MAXPATHLEN];
221 	char *tmp;
222 
223 	/* If they preface 'dev' with a path (like "/dev") then strip it off */
224 	tmp = strrchr(dev_name, '/');
225 	if (tmp != NULL)
226 		dev_name = tmp + 1;    /* +1 since we want the chr after '/' */
227 
228 	if (strncmp("nvme", dev_name, 4) != 0)
229 		return (NULL);
230 
231 	(void) snprintf(buf, sizeof (buf), "/sys/block/%s/device/address",
232 	    dev_name);
233 
234 	address1 = zfs_read_sysfs_file(buf);
235 	if (!address1)
236 		return (NULL);
237 
238 	/*
239 	 * /sys/block/nvme0n1/device/address format will
240 	 * be "0000:01:00.0" while /sys/bus/pci/slots/0/address will be
241 	 * "0000:01:00".  Just NULL terminate at the '.' so they match.
242 	 */
243 	tmp = strrchr(address1, '.');
244 	if (tmp != NULL)
245 		*tmp = '\0';
246 
247 	dp = opendir("/sys/bus/pci/slots/");
248 	if (dp == NULL) {
249 		free(address1);
250 		return (NULL);
251 	}
252 
253 	/*
254 	 * Look through all the /sys/bus/pci/slots/ subdirs
255 	 */
256 	while ((ep = readdir(dp))) {
257 		/*
258 		 * We only care about directory names that are a single number.
259 		 * Sometimes there's other directories like
260 		 * "/sys/bus/pci/slots/0-3/" in there - skip those.
261 		 */
262 		if (!zfs_isnumber(ep->d_name))
263 			continue;
264 
265 		(void) snprintf(buf, sizeof (buf),
266 		    "/sys/bus/pci/slots/%s/address", ep->d_name);
267 
268 		address2 = zfs_read_sysfs_file(buf);
269 		if (!address2)
270 			continue;
271 
272 		if (strcmp(address1, address2) == 0) {
273 			/* Addresses match, we're all done */
274 			free(address2);
275 			if (asprintf(&path, "/sys/bus/pci/slots/%s",
276 			    ep->d_name) == -1) {
277 				continue;
278 			}
279 			break;
280 		}
281 		free(address2);
282 	}
283 
284 	closedir(dp);
285 	free(address1);
286 
287 	return (path);
288 }
289 
290 /*
291  * Given a dev name like "sda", return the full enclosure sysfs path to
292  * the disk.  You can also pass in the name with "/dev" prepended
293  * to it (like /dev/sda).  This works for both JBODs and NVMe PCI devices.
294  *
295  * For example, disk "sda" in enclosure slot 1:
296  *     dev_name:       "sda"
297  *     returns:        "/sys/class/enclosure/1:0:3:0/Slot 1"
298  *
299  * Or:
300  *
301  *      dev_name:   "nvme0n1"
302  *      returns:    "/sys/bus/pci/slots/0"
303  *
304  * 'dev' must be a non-devicemapper device.
305  *
306  * Returned string must be freed.  Returns NULL on error.
307  */
308 char *
zfs_get_enclosure_sysfs_path(const char * dev_name)309 zfs_get_enclosure_sysfs_path(const char *dev_name)
310 {
311 	DIR *dp = NULL;
312 	struct dirent *ep;
313 	char buf[MAXPATHLEN];
314 	char *tmp1 = NULL;
315 	char *tmp2 = NULL;
316 	char *tmp3 = NULL;
317 	char *path = NULL;
318 	size_t size;
319 	int tmpsize;
320 
321 	if (dev_name == NULL)
322 		return (NULL);
323 
324 	/* If they preface 'dev' with a path (like "/dev") then strip it off */
325 	tmp1 = strrchr(dev_name, '/');
326 	if (tmp1 != NULL)
327 		dev_name = tmp1 + 1;    /* +1 since we want the chr after '/' */
328 
329 	tmpsize = asprintf(&tmp1, "/sys/block/%s/device", dev_name);
330 	if (tmpsize == -1 || tmp1 == NULL) {
331 		tmp1 = NULL;
332 		goto end;
333 	}
334 
335 	dp = opendir(tmp1);
336 	if (dp == NULL)
337 		goto end;
338 
339 	/*
340 	 * Look though all sysfs entries in /sys/block/<dev>/device for
341 	 * the enclosure symlink.
342 	 */
343 	while ((ep = readdir(dp))) {
344 		/* Ignore everything that's not our enclosure_device link */
345 		if (strstr(ep->d_name, "enclosure_device") == NULL)
346 			continue;
347 
348 		if (tmp2 != NULL)
349 			free(tmp2);
350 		if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1) {
351 			tmp2 = NULL;
352 			break;
353 		}
354 
355 		size = readlink(tmp2, buf, sizeof (buf));
356 
357 		/* Did readlink fail or crop the link name? */
358 		if (size == -1 || size >= sizeof (buf))
359 			break;
360 
361 		/*
362 		 * We got a valid link.  readlink() doesn't terminate strings
363 		 * so we have to do it.
364 		 */
365 		buf[size] = '\0';
366 
367 		/*
368 		 * Our link will look like:
369 		 *
370 		 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
371 		 *
372 		 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
373 		 */
374 		tmp3 = strstr(buf, "enclosure");
375 		if (tmp3 == NULL)
376 			break;
377 
378 		if (path != NULL)
379 			free(path);
380 		if (asprintf(&path, "/sys/class/%s", tmp3) == -1) {
381 			/* If asprintf() fails, 'path' is undefined */
382 			path = NULL;
383 			break;
384 		}
385 	}
386 
387 end:
388 	free(tmp2);
389 	free(tmp1);
390 
391 	if (dp != NULL)
392 		closedir(dp);
393 
394 	if (!path) {
395 		/*
396 		 * This particular disk isn't in a JBOD.  It could be an NVMe
397 		 * drive. If so, look up the NVMe device's path in
398 		 * /sys/bus/pci/slots/. Within that directory is a 'attention'
399 		 * file which controls the NVMe fault LED.
400 		 */
401 		path = zfs_get_pci_slots_sys_path(dev_name);
402 	}
403 
404 	return (path);
405 }
406 
407 /*
408  * Allocate and return the underlying device name for a device mapper device.
409  *
410  * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
411  * DM device (like /dev/disk/by-vdev/A0) are also allowed.
412  *
413  * If the DM device has multiple underlying devices (like with multipath
414  * DM devices), then favor underlying devices that have a symlink back to their
415  * back to their enclosure device in sysfs.  This will be useful for the
416  * zedlet scripts that toggle the fault LED.
417  *
418  * Returns an underlying device name, or NULL on error or no match.  If dm_name
419  * is not a DM device then return NULL.
420  *
421  * NOTE: The returned name string must be *freed*.
422  */
423 static char *
dm_get_underlying_path(const char * dm_name)424 dm_get_underlying_path(const char *dm_name)
425 {
426 	DIR *dp = NULL;
427 	struct dirent *ep;
428 	char *realp;
429 	char *tmp = NULL;
430 	char *path = NULL;
431 	char *dev_str;
432 	char *first_path = NULL;
433 	char *enclosure_path;
434 
435 	if (dm_name == NULL)
436 		return (NULL);
437 
438 	/* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
439 	realp = realpath(dm_name, NULL);
440 	if (realp == NULL)
441 		return (NULL);
442 
443 	/*
444 	 * If they preface 'dev' with a path (like "/dev") then strip it off.
445 	 * We just want the 'dm-N' part.
446 	 */
447 	tmp = strrchr(realp, '/');
448 	if (tmp != NULL)
449 		dev_str = tmp + 1;    /* +1 since we want the chr after '/' */
450 	else
451 		dev_str = tmp;
452 
453 	if (asprintf(&tmp, "/sys/block/%s/slaves/", dev_str) == -1) {
454 		tmp = NULL;
455 		goto end;
456 	}
457 
458 	dp = opendir(tmp);
459 	if (dp == NULL)
460 		goto end;
461 
462 	/*
463 	 * A device-mapper device can have multiple paths to it (multipath).
464 	 * Favor paths that have a symlink back to their enclosure device.
465 	 * We have to do this since some enclosures may only provide a symlink
466 	 * back for one underlying path to a disk and not the other.
467 	 *
468 	 * If no paths have links back to their enclosure, then just return the
469 	 * first path.
470 	 */
471 	while ((ep = readdir(dp))) {
472 		if (ep->d_type != DT_DIR) {	/* skip "." and ".." dirs */
473 			if (!first_path)
474 				first_path = strdup(ep->d_name);
475 
476 			enclosure_path =
477 			    zfs_get_enclosure_sysfs_path(ep->d_name);
478 
479 			if (!enclosure_path)
480 				continue;
481 
482 			if (asprintf(&path, "/dev/%s", ep->d_name) == -1)
483 				path = NULL;
484 			free(enclosure_path);
485 			break;
486 		}
487 	}
488 
489 end:
490 	if (dp != NULL)
491 		closedir(dp);
492 	free(tmp);
493 	free(realp);
494 
495 	if (!path && first_path) {
496 		/*
497 		 * None of the underlying paths had a link back to their
498 		 * enclosure devices.  Throw up out hands and return the first
499 		 * underlying path.
500 		 */
501 		if (asprintf(&path, "/dev/%s", first_path) == -1)
502 			path = NULL;
503 	}
504 
505 	free(first_path);
506 	return (path);
507 }
508 
509 /*
510  * Return B_TRUE if device is a device mapper or multipath device.
511  * Return B_FALSE if not.
512  */
513 boolean_t
zfs_dev_is_dm(const char * dev_name)514 zfs_dev_is_dm(const char *dev_name)
515 {
516 
517 	char *tmp;
518 	tmp = dm_get_underlying_path(dev_name);
519 	if (tmp == NULL)
520 		return (B_FALSE);
521 
522 	free(tmp);
523 	return (B_TRUE);
524 }
525 
526 /*
527  * By "whole disk" we mean an entire physical disk (something we can
528  * label, toggle the write cache on, etc.) as opposed to the full
529  * capacity of a pseudo-device such as lofi or did.  We act as if we
530  * are labeling the disk, which should be a pretty good test of whether
531  * it's a viable device or not.  Returns B_TRUE if it is and B_FALSE if
532  * it isn't.
533  */
534 boolean_t
zfs_dev_is_whole_disk(const char * dev_name)535 zfs_dev_is_whole_disk(const char *dev_name)
536 {
537 	struct dk_gpt *label = NULL;
538 	int fd;
539 
540 	if ((fd = open(dev_name, O_RDONLY | O_DIRECT | O_CLOEXEC)) < 0)
541 		return (B_FALSE);
542 
543 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
544 		(void) close(fd);
545 		return (B_FALSE);
546 	}
547 
548 	efi_free(label);
549 	(void) close(fd);
550 
551 	return (B_TRUE);
552 }
553 
554 /*
555  * Lookup the underlying device for a device name
556  *
557  * Often you'll have a symlink to a device, a partition device,
558  * or a multipath device, and want to look up the underlying device.
559  * This function returns the underlying device name.  If the device
560  * name is already the underlying device, then just return the same
561  * name.  If the device is a DM device with multiple underlying devices
562  * then return the first one.
563  *
564  * For example:
565  *
566  * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
567  * dev_name:	/dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
568  * returns:	/dev/sda
569  *
570  * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
571  * dev_name:	/dev/mapper/mpatha
572  * returns:	/dev/sda (first device)
573  *
574  * 3. /dev/sda (already the underlying device)
575  * dev_name:	/dev/sda
576  * returns:	/dev/sda
577  *
578  * 4. /dev/dm-3 (mapped to /dev/sda)
579  * dev_name:	/dev/dm-3
580  * returns:	/dev/sda
581  *
582  * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
583  * dev_name:	/dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
584  * returns:	/dev/sdb
585  *
586  * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
587  * dev_name:	/dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
588  * returns:	/dev/sda
589  *
590  * Returns underlying device name, or NULL on error or no match.
591  *
592  * NOTE: The returned name string must be *freed*.
593  */
594 char *
zfs_get_underlying_path(const char * dev_name)595 zfs_get_underlying_path(const char *dev_name)
596 {
597 	char *name = NULL;
598 	char *tmp;
599 
600 	if (dev_name == NULL)
601 		return (NULL);
602 
603 	tmp = dm_get_underlying_path(dev_name);
604 
605 	/* dev_name not a DM device, so just un-symlinkize it */
606 	if (tmp == NULL)
607 		tmp = realpath(dev_name, NULL);
608 
609 	if (tmp != NULL) {
610 		name = zfs_strip_partition_path(tmp);
611 		free(tmp);
612 	}
613 
614 	return (name);
615 }
616 
617 
618 #ifdef HAVE_LIBUDEV
619 
620 /*
621  * A disk is considered a multipath whole disk when:
622  *	DEVNAME key value has "dm-"
623  *	DM_UUID key exists and starts with 'mpath-'
624  *	ID_PART_TABLE_TYPE key does not exist or is not gpt
625  *	ID_FS_LABEL key does not exist (disk isn't labeled)
626  */
627 static boolean_t
is_mpath_udev_sane(struct udev_device * dev)628 is_mpath_udev_sane(struct udev_device *dev)
629 {
630 	const char *devname, *type, *uuid, *label;
631 
632 	devname = udev_device_get_property_value(dev, "DEVNAME");
633 	type = udev_device_get_property_value(dev, "ID_PART_TABLE_TYPE");
634 	uuid = udev_device_get_property_value(dev, "DM_UUID");
635 	label = udev_device_get_property_value(dev, "ID_FS_LABEL");
636 
637 	if ((devname != NULL && strncmp(devname, "/dev/dm-", 8) == 0) &&
638 	    ((type == NULL) || (strcmp(type, "gpt") != 0)) &&
639 	    ((uuid != NULL) && (strncmp(uuid, "mpath-", 6) == 0)) &&
640 	    (label == NULL)) {
641 		return (B_TRUE);
642 	}
643 
644 	return (B_FALSE);
645 }
646 
647 /*
648  * Check if a disk is a multipath "blank" disk:
649  *
650  * 1. The disk has udev values that suggest it's a multipath disk
651  * 2. The disk is not currently labeled with a filesystem of any type
652  * 3. There are no partitions on the disk
653  */
654 boolean_t
is_mpath_whole_disk(const char * path)655 is_mpath_whole_disk(const char *path)
656 {
657 	struct udev *udev;
658 	struct udev_device *dev = NULL;
659 	char nodepath[MAXPATHLEN];
660 	char *sysname;
661 
662 	if (realpath(path, nodepath) == NULL)
663 		return (B_FALSE);
664 	sysname = strrchr(nodepath, '/') + 1;
665 	if (strncmp(sysname, "dm-", 3) != 0)
666 		return (B_FALSE);
667 	if ((udev = udev_new()) == NULL)
668 		return (B_FALSE);
669 	if ((dev = udev_device_new_from_subsystem_sysname(udev, "block",
670 	    sysname)) == NULL) {
671 		udev_device_unref(dev);
672 		return (B_FALSE);
673 	}
674 
675 	/* Sanity check some udev values */
676 	boolean_t is_sane = is_mpath_udev_sane(dev);
677 	udev_device_unref(dev);
678 
679 	return (is_sane);
680 }
681 
682 #else /* HAVE_LIBUDEV */
683 
684 boolean_t
is_mpath_whole_disk(const char * path)685 is_mpath_whole_disk(const char *path)
686 {
687 	(void) path;
688 	return (B_FALSE);
689 }
690 
691 #endif /* HAVE_LIBUDEV */
692