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>
36 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
45 /* Ignore strange nested directories */
46 if (dir_level > max_level)
49 path[PATH_MAX - 1] = '\0';
50 ptr = path + strlen(path);
53 space = PATH_MAX - (ptr - path);
59 while((entry = readdir(dir))) {
60 if (entry->d_name[0] == '.' ||
61 !strncmp(entry->d_name, "..", 2))
65 (!strcmp(entry->d_name, "shm") ||
66 !strcmp(entry->d_name, "fd") ||
67 !strcmp(entry->d_name, "char") ||
68 !strcmp(entry->d_name, "pts")))
71 strncpy(ptr, entry->d_name, space);
72 if (stat(path, &st) < 0)
75 if (S_ISDIR(st.st_mode)) {
76 result = __lookup_dev(path, dev, dir_level + 1, max_level);
79 } else if (S_ISBLK(st.st_mode)) {
80 /* workaround: ignore dm-X devices, these are internal kernel names */
81 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
83 if (st.st_rdev == dev) {
84 result = strdup(path);
95 * Non-udev systemd need to scan for device here.
97 static char *lookup_dev_old(int major, int minor)
100 char *result = NULL, buf[PATH_MAX + 1];
102 dev = makedev(major, minor);
103 strncpy(buf, "/dev", PATH_MAX);
104 buf[PATH_MAX] = '\0';
106 /* First try low level device */
107 if ((result = __lookup_dev(buf, dev, 0, 0)))
110 /* If it is dm, try DM dir */
111 if (dm_is_dm_device(major, minor)) {
112 strncpy(buf, dm_get_dir(), PATH_MAX);
113 if ((result = __lookup_dev(buf, dev, 0, 0)))
117 strncpy(buf, "/dev", PATH_MAX);
118 return __lookup_dev(buf, dev, 0, 4);
122 * Returns string pointing to device in /dev according to "major:minor" dev_id
124 char *crypt_lookup_dev(const char *dev_id)
127 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
131 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
134 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
137 len = readlink(path, link, sizeof(link) - 1);
139 /* Without /sys use old scan */
140 if (stat("/sys/dev/block", &st) < 0)
141 return lookup_dev_old(major, minor);
146 devname = strrchr(link, '/');
151 if (dm_is_dm_kernel_name(devname))
152 devpath = dm_device_path("/dev/mapper/", major, minor);
153 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
154 devpath = strdup(path);
157 * Check that path is correct.
159 if (devpath && ((stat(devpath, &st) < 0) ||
160 !S_ISBLK(st.st_mode) ||
161 (st.st_rdev != makedev(major, minor)))) {
163 /* Should never happen unless user mangles with dev nodes. */
164 return lookup_dev_old(major, minor);
170 static int _read_uint64(const char *sysfs_path, uint64_t *value)
175 if ((fd = open(sysfs_path, O_RDONLY)) < 0)
177 r = read(fd, tmp, sizeof(tmp));
183 if (sscanf(tmp, "%" PRIu64, value) != 1)
189 static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *attr)
193 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/%s",
194 major, minor, attr) < 0)
197 return _read_uint64(path, value);
200 static int _path_get_uint64(const char *sysfs_path, uint64_t *value, const char *attr)
204 if (snprintf(path, sizeof(path), "%s/%s",
205 sysfs_path, attr) < 0)
208 return _read_uint64(path, value);
211 int crypt_dev_is_rotational(int major, int minor)
215 if (!_sysfs_get_uint64(major, minor, &val, "queue/rotational"))
216 return 1; /* if failed, expect rotational disk */
221 int crypt_dev_is_partition(const char *dev_path)
226 if (stat(dev_path, &st) < 0)
229 if (!S_ISBLK(st.st_mode))
232 if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
239 uint64_t crypt_dev_partition_offset(const char *dev_path)
244 if (!crypt_dev_is_partition(dev_path))
247 if (stat(dev_path, &st) < 0)
250 if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
257 /* Try to find partition which match offset and size on top level device */
258 char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size)
260 char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
265 struct dirent *entry;
267 uint64_t part_offset, part_size;
269 if (stat(dev_path, &st) < 0)
272 if (!S_ISBLK(st.st_mode))
275 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d",
276 major(st.st_rdev), minor(st.st_rdev)) < 0)
279 len = readlink(path, link, sizeof(link) - 1);
283 /* Get top level disk name for sysfs search */
285 devname = strrchr(link, '/');
290 /* DM devices do not use kernel partitions. */
291 if (dm_is_dm_kernel_name(devname))
298 devname_len = strlen(devname);
299 while((entry = readdir(dir))) {
300 if (strncmp(entry->d_name, devname, devname_len))
303 if (snprintf(part_path, sizeof(part_path), "%s/%s",
304 path, entry->d_name) < 0)
307 if (stat(part_path, &st) < 0)
310 if (S_ISDIR(st.st_mode)) {
311 if (!_path_get_uint64(part_path, &part_offset, "start") ||
312 !_path_get_uint64(part_path, &part_size, "size"))
314 if (part_offset == offset && part_size == size &&
315 snprintf(part_path, sizeof(part_path), "/dev/%s",
316 entry->d_name) > 0) {
317 result = strdup(part_path);
327 /* Try to find base device from partition */
328 char *crypt_get_base_device(const char *dev_path)
330 char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
334 if (!crypt_dev_is_partition(dev_path))
337 if (stat(dev_path, &st) < 0)
340 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d",
341 major(st.st_rdev), minor(st.st_rdev)) < 0)
344 len = readlink(path, link, sizeof(link) - 1);
348 /* Get top level disk name for sysfs search */
350 devname = strrchr(link, '/');
354 devname = strrchr(link, '/');
359 if (dm_is_dm_kernel_name(devname))
362 snprintf(part_path, sizeof(part_path), "/dev/%s", devname);
363 return strdup(part_path);