2 * devname - search for device name
4 * Copyright (C) 2004, Christophe Saout <christophe@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 char *crypt_lookup_dev(const char *dev_id);
37 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
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 /* Try to find partition which match offset and size on top level device */
243 char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size)
245 char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
250 struct dirent *entry;
252 uint64_t part_offset, part_size;
254 if (stat(dev_path, &st) < 0)
257 if (!S_ISBLK(st.st_mode))
260 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d",
261 major(st.st_rdev), minor(st.st_rdev)) < 0)
264 len = readlink(path, link, sizeof(link) - 1);
268 /* Get top level disk name for sysfs search */
270 devname = strrchr(link, '/');
275 /* DM devices do not use kernel partitions. */
276 if (dm_is_dm_kernel_name(devname))
283 devname_len = strlen(devname);
284 while((entry = readdir(dir))) {
285 if (strncmp(entry->d_name, devname, devname_len))
288 if (snprintf(part_path, sizeof(part_path), "%s/%s",
289 path, entry->d_name) < 0)
292 if (stat(part_path, &st) < 0)
295 if (S_ISDIR(st.st_mode)) {
296 if (!_path_get_uint64(part_path, &part_offset, "start") ||
297 !_path_get_uint64(part_path, &part_size, "size"))
299 if (part_offset == offset && part_size == size &&
300 snprintf(part_path, sizeof(part_path), "/dev/%s",
301 entry->d_name) > 0) {
302 result = strdup(part_path);