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-2011, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
33 char *crypt_lookup_dev(const char *dev_id);
34 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size);
35 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
37 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
46 /* Ignore strange nested directories */
47 if (dir_level > max_level)
50 path[PATH_MAX - 1] = '\0';
51 ptr = path + strlen(path);
54 space = PATH_MAX - (ptr - path);
60 while((entry = readdir(dir))) {
61 if (entry->d_name[0] == '.' ||
62 !strncmp(entry->d_name, "..", 2))
66 (!strcmp(entry->d_name, "shm") ||
67 !strcmp(entry->d_name, "fd") ||
68 !strcmp(entry->d_name, "char") ||
69 !strcmp(entry->d_name, "pts")))
72 strncpy(ptr, entry->d_name, space);
73 if (stat(path, &st) < 0)
76 if (S_ISDIR(st.st_mode)) {
77 result = __lookup_dev(path, dev, dir_level + 1, max_level);
80 } else if (S_ISBLK(st.st_mode)) {
81 /* workaround: ignore dm-X devices, these are internal kernel names */
82 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
84 if (st.st_rdev == dev) {
85 result = strdup(path);
96 * Non-udev systemd need to scan for device here.
98 static char *lookup_dev_old(int major, int minor)
101 char *result = NULL, buf[PATH_MAX + 1];
103 dev = makedev(major, minor);
104 strncpy(buf, "/dev", PATH_MAX);
105 buf[PATH_MAX] = '\0';
107 /* First try low level device */
108 if ((result = __lookup_dev(buf, dev, 0, 0)))
111 /* If it is dm, try DM dir */
112 if (dm_is_dm_device(major, minor)) {
113 strncpy(buf, dm_get_dir(), PATH_MAX);
114 if ((result = __lookup_dev(buf, dev, 0, 0)))
118 strncpy(buf, "/dev", PATH_MAX);
119 return __lookup_dev(buf, dev, 0, 4);
123 * Returns string pointing to device in /dev according to "major:minor" dev_id
125 char *crypt_lookup_dev(const char *dev_id)
128 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
132 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
135 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
138 len = readlink(path, link, sizeof(link));
140 /* Without /sys use old scan */
141 if (stat("/sys/dev/block", &st) < 0)
142 return lookup_dev_old(major, minor);
147 devname = strrchr(link, '/');
152 if (dm_is_dm_kernel_name(devname))
153 devpath = dm_device_path("/dev/mapper/", major, minor);
154 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
155 devpath = strdup(path);
158 * Check that path is correct.
160 if (devpath && ((stat(devpath, &st) < 0) ||
161 !S_ISBLK(st.st_mode) ||
162 (st.st_rdev != makedev(major, minor)))) {
164 /* Should never happen unless user mangles with dev nodes. */
165 return lookup_dev_old(major, minor);
171 static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor)
173 char path[PATH_MAX], tmp[64] = {0};
176 if (snprintf(path, sizeof(path), "/sys/block/%s/dev", kname) < 0)
179 if ((fd = open(path, O_RDONLY)) < 0)
181 r = read(fd, tmp, sizeof(tmp));
188 if (sscanf(tmp, "%d:%d", major, minor) != 2)
194 static int crypt_sysfs_get_holders_dir(const char *device, char *path, int size)
198 if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
201 if (snprintf(path, size, "/sys/dev/block/%d:%d/holders",
202 major(st.st_rdev), minor(st.st_rdev)) < 0)
208 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size)
212 char path[PATH_MAX], *dmname;
213 int major, minor, r = 0;
215 if (!crypt_sysfs_get_holders_dir(device, path, sizeof(path)))
218 if (!(dir = opendir(path)))
221 while (!r && (d = readdir(dir))) {
222 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
225 if (!dm_is_dm_kernel_name(d->d_name)) {
230 if (!crypt_sysfs_get_major_minor(d->d_name, &major, &minor)) {
235 if (!(dmname = dm_device_path(NULL, major, minor))) {
239 r = dm_check_segment(dmname, offset, size);
247 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
249 char path[PATH_MAX], tmp[64] = {0};
252 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
256 if ((fd = open(path, O_RDONLY)) < 0)
258 r = read(fd, tmp, sizeof(tmp));
264 if (sscanf(tmp, "%d", rotational) != 1)