* Add --shared option for creating non-overlapping crypt segments.
[platform/upstream/cryptsetup.git] / lib / utils_devpath.c
1 /*
2  * devname - search for device name
3  *
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.
7  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include "utils_dm.h"
32
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
36 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
37 {
38         struct dirent *entry;
39         struct stat st;
40         char *ptr;
41         char *result = NULL;
42         DIR *dir;
43         int space;
44
45         /* Ignore strange nested directories */
46         if (dir_level > max_level)
47                 return NULL;
48
49         path[PATH_MAX - 1] = '\0';
50         ptr = path + strlen(path);
51         *ptr++ = '/';
52         *ptr = '\0';
53         space = PATH_MAX - (ptr - path);
54
55         dir = opendir(path);
56         if (!dir)
57                 return NULL;
58
59         while((entry = readdir(dir))) {
60                 if (entry->d_name[0] == '.' ||
61                     !strncmp(entry->d_name, "..", 2))
62                         continue;
63
64                 if (dir_level == 0 &&
65                     (!strcmp(entry->d_name, "shm") ||
66                      !strcmp(entry->d_name, "fd") ||
67                      !strcmp(entry->d_name, "char") ||
68                      !strcmp(entry->d_name, "pts")))
69                         continue;
70
71                 strncpy(ptr, entry->d_name, space);
72                 if (stat(path, &st) < 0)
73                         continue;
74
75                 if (S_ISDIR(st.st_mode)) {
76                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
77                         if (result)
78                                 break;
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))
82                                 continue;
83                         if (st.st_rdev == dev) {
84                                 result = strdup(path);
85                                 break;
86                         }
87                 }
88         }
89
90         closedir(dir);
91         return result;
92 }
93
94 /*
95  * Non-udev systemd need to scan for device here.
96  */
97 static char *lookup_dev_old(int major, int minor)
98 {
99         dev_t dev;
100         char *result = NULL, buf[PATH_MAX + 1];
101
102         dev = makedev(major, minor);
103         strncpy(buf, "/dev", PATH_MAX);
104         buf[PATH_MAX] = '\0';
105
106         /* First try low level device */
107         if ((result = __lookup_dev(buf, dev, 0, 0)))
108                 return result;
109
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)))
114                         return result;
115         }
116
117         strncpy(buf, "/dev", PATH_MAX);
118         return  __lookup_dev(buf, dev, 0, 4);
119 }
120
121 /*
122  * Returns string pointing to device in /dev according to "major:minor" dev_id
123  */
124 char *crypt_lookup_dev(const char *dev_id)
125 {
126         int major, minor;
127         char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
128         struct stat st;
129         ssize_t len;
130
131         if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
132                 return NULL;
133
134         if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
135                 return NULL;
136
137         len = readlink(path, link, sizeof(link));
138         if (len < 0) {
139                 /* Without /sys use old scan */
140                 if (stat("/sys/dev/block", &st) < 0)
141                         return lookup_dev_old(major, minor);
142                 return NULL;
143         }
144
145         link[len] = '\0';
146         devname = strrchr(link, '/');
147         if (!devname)
148                 return NULL;
149         devname++;
150
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);
155
156         /*
157          * Check that path is correct.
158          */
159         if (devpath && ((stat(devpath, &st) < 0) ||
160             !S_ISBLK(st.st_mode) ||
161             (st.st_rdev != makedev(major, minor)))) {
162                 free(devpath);
163                 /* Should never happen unless user mangles with dev nodes. */
164                 return lookup_dev_old(major, minor);
165         }
166
167         return devpath;
168 }
169
170 static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor)
171 {
172         char path[PATH_MAX], tmp[64];
173         int fd, r = 0;
174
175         if (snprintf(path, sizeof(path), "/sys/block/%s/dev", kname) < 0)
176                 return 0;
177
178         if ((fd = open(path, O_RDONLY)) < 0)
179                 return 0;
180         r = read(fd, tmp, sizeof(tmp));
181         close(fd);
182
183         if (r <= 0 || sscanf(tmp, "%d:%d", major, minor) != 2)
184                 return 0;
185
186         return 1;
187 }
188
189 static int crypt_sysfs_get_holders_dir(const char *device, char *path, int size)
190 {
191         struct stat st;
192
193         if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
194                 return 0;
195
196         if (snprintf(path, size, "/sys/dev/block/%d:%d/holders",
197                      major(st.st_rdev), minor(st.st_rdev)) < 0)
198                 return 0;
199
200         return 1;
201 }
202
203 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size)
204 {
205         DIR *dir;
206         struct dirent *d;
207         char path[PATH_MAX], *dmname;
208         int major, minor, r = 0;
209
210         if (!crypt_sysfs_get_holders_dir(device, path, sizeof(path)))
211                 return -EINVAL;
212
213         if (!(dir = opendir(path)))
214                 return -EINVAL;
215
216         while (!r && (d = readdir(dir))) {
217                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
218                         continue;
219
220                 if (!dm_is_dm_kernel_name(d->d_name)) {
221                         r = -EBUSY;
222                         break;
223                 }
224
225                 if (!crypt_sysfs_get_major_minor(d->d_name, &major, &minor)) {
226                         r = -EINVAL;
227                         break;
228                 }
229
230                 if (!(dmname = dm_device_path(NULL, major, minor))) {
231                         r = -EINVAL;
232                         break;
233                 }
234                 r = dm_check_segment(dmname, offset, size);
235                 free(dmname);
236         }
237         closedir(dir);
238
239         return r;
240 }