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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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);
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));
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 crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor)
172 char path[PATH_MAX], tmp[64];
175 if (snprintf(path, sizeof(path), "/sys/block/%s/dev", kname) < 0)
178 if ((fd = open(path, O_RDONLY)) < 0)
180 r = read(fd, tmp, sizeof(tmp));
183 if (r <= 0 || sscanf(tmp, "%d:%d", major, minor) != 2)
189 static int crypt_sysfs_get_holders_dir(const char *device, char *path, int size)
193 if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
196 if (snprintf(path, size, "/sys/dev/block/%d:%d/holders",
197 major(st.st_rdev), minor(st.st_rdev)) < 0)
203 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size)
207 char path[PATH_MAX], *dmname;
208 int major, minor, r = 0;
210 if (!crypt_sysfs_get_holders_dir(device, path, sizeof(path)))
213 if (!(dir = opendir(path)))
216 while (!r && (d = readdir(dir))) {
217 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
220 if (!dm_is_dm_kernel_name(d->d_name)) {
225 if (!crypt_sysfs_get_major_minor(d->d_name, &major, &minor)) {
230 if (!(dmname = dm_device_path(NULL, major, minor))) {
234 r = dm_check_segment(dmname, offset, size);