2 * devname - search for device name
4 * Copyright (C) 2004, Jana Saout <jana@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2013, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SYSMACROS_H
35 #include <sys/sysmacros.h> /* for major, minor */
39 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
48 /* Ignore strange nested directories */
49 if (dir_level > max_level)
52 path[PATH_MAX - 1] = '\0';
53 ptr = path + strlen(path);
56 space = PATH_MAX - (ptr - path);
62 while((entry = readdir(dir))) {
63 if (entry->d_name[0] == '.' ||
64 !strncmp(entry->d_name, "..", 2))
68 (!strcmp(entry->d_name, "shm") ||
69 !strcmp(entry->d_name, "fd") ||
70 !strcmp(entry->d_name, "char") ||
71 !strcmp(entry->d_name, "pts")))
74 strncpy(ptr, entry->d_name, space);
75 if (stat(path, &st) < 0)
78 if (S_ISDIR(st.st_mode)) {
79 result = __lookup_dev(path, dev, dir_level + 1, max_level);
82 } else if (S_ISBLK(st.st_mode)) {
83 /* workaround: ignore dm-X devices, these are internal kernel names */
84 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
86 if (st.st_rdev == dev) {
87 result = strdup(path);
98 * Non-udev systemd need to scan for device here.
100 static char *lookup_dev_old(int major, int minor)
103 char *result = NULL, buf[PATH_MAX + 1];
105 dev = makedev(major, minor);
106 strncpy(buf, "/dev", PATH_MAX);
107 buf[PATH_MAX] = '\0';
109 /* First try low level device */
110 if ((result = __lookup_dev(buf, dev, 0, 0)))
113 /* If it is dm, try DM dir */
114 if (dm_is_dm_device(major, minor)) {
115 strncpy(buf, dm_get_dir(), PATH_MAX);
116 if ((result = __lookup_dev(buf, dev, 0, 0)))
120 strncpy(buf, "/dev", PATH_MAX);
121 return __lookup_dev(buf, dev, 0, 4);
125 * Returns string pointing to device in /dev according to "major:minor" dev_id
127 char *crypt_lookup_dev(const char *dev_id)
130 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
134 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
137 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
140 len = readlink(path, link, sizeof(link) - 1);
142 /* Without /sys use old scan */
143 if (stat("/sys/dev/block", &st) < 0)
144 return lookup_dev_old(major, minor);
149 devname = strrchr(link, '/');
154 if (dm_is_dm_kernel_name(devname))
155 devpath = dm_device_path("/dev/mapper/", major, minor);
156 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
157 devpath = strdup(path);
160 * Check that path is correct.
162 if (devpath && ((stat(devpath, &st) < 0) ||
163 !S_ISBLK(st.st_mode) ||
164 (st.st_rdev != makedev(major, minor)))) {
166 /* Should never happen unless user mangles with dev nodes. */
167 return lookup_dev_old(major, minor);
173 static int _read_uint64(const char *sysfs_path, uint64_t *value)
178 if ((fd = open(sysfs_path, O_RDONLY)) < 0)
180 r = read(fd, tmp, sizeof(tmp));
186 if (sscanf(tmp, "%" PRIu64, value) != 1)
192 static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *attr)
196 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/%s",
197 major, minor, attr) < 0)
200 return _read_uint64(path, value);
203 static int _path_get_uint64(const char *sysfs_path, uint64_t *value, const char *attr)
207 if (snprintf(path, sizeof(path), "%s/%s",
208 sysfs_path, attr) < 0)
211 return _read_uint64(path, value);
214 int crypt_dev_is_rotational(int major, int minor)
218 if (!_sysfs_get_uint64(major, minor, &val, "queue/rotational"))
219 return 1; /* if failed, expect rotational disk */
224 int crypt_dev_is_partition(const char *dev_path)
229 if (stat(dev_path, &st) < 0)
232 if (!S_ISBLK(st.st_mode))
235 if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
242 uint64_t crypt_dev_partition_offset(const char *dev_path)
247 if (!crypt_dev_is_partition(dev_path))
250 if (stat(dev_path, &st) < 0)
253 if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
260 /* Try to find partition which match offset and size on top level device */
261 char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size)
263 char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
268 struct dirent *entry;
270 uint64_t part_offset, part_size;
272 if (stat(dev_path, &st) < 0)
275 if (!S_ISBLK(st.st_mode))
278 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d",
279 major(st.st_rdev), minor(st.st_rdev)) < 0)
282 len = readlink(path, link, sizeof(link) - 1);
286 /* Get top level disk name for sysfs search */
288 devname = strrchr(link, '/');
293 /* DM devices do not use kernel partitions. */
294 if (dm_is_dm_kernel_name(devname))
301 devname_len = strlen(devname);
302 while((entry = readdir(dir))) {
303 if (strncmp(entry->d_name, devname, devname_len))
306 if (snprintf(part_path, sizeof(part_path), "%s/%s",
307 path, entry->d_name) < 0)
310 if (stat(part_path, &st) < 0)
313 if (S_ISDIR(st.st_mode)) {
314 if (!_path_get_uint64(part_path, &part_offset, "start") ||
315 !_path_get_uint64(part_path, &part_size, "size"))
317 if (part_offset == offset && part_size == size &&
318 snprintf(part_path, sizeof(part_path), "/dev/%s",
319 entry->d_name) > 0) {
320 result = strdup(part_path);
330 /* Try to find base device from partition */
331 char *crypt_get_base_device(const char *dev_path)
333 char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
337 if (!crypt_dev_is_partition(dev_path))
340 if (stat(dev_path, &st) < 0)
343 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d",
344 major(st.st_rdev), minor(st.st_rdev)) < 0)
347 len = readlink(path, link, sizeof(link) - 1);
351 /* Get top level disk name for sysfs search */
353 devname = strrchr(link, '/');
357 devname = strrchr(link, '/');
362 if (dm_is_dm_kernel_name(devname))
365 snprintf(part_path, sizeof(part_path), "/dev/%s", devname);
366 return strdup(part_path);