xref: /freebsd/lib/libgeom/geom_util.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
1 /*-
2  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/disk.h>
32 #include <sys/stat.h>
33 
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <paths.h>
42 
43 #include <libgeom.h>
44 
45 static char	*g_device_path_open(const char *, int *, int);
46 
47 /*
48  * Open the given provider and at least check if this is a block device.
49  */
50 int
51 g_open(const char *name, int dowrite)
52 {
53 	char *path;
54 	int fd;
55 
56 	path = g_device_path_open(name, &fd, dowrite);
57 	if (path != NULL)
58 		free(path);
59 	return (fd);
60 }
61 
62 int
63 g_close(int fd)
64 {
65 
66 	return (close(fd));
67 }
68 
69 static int
70 g_ioctl_arg(int fd, unsigned long cmd, void *arg)
71 {
72 	int ret;
73 
74 	if (arg != NULL)
75 		ret = ioctl(fd, cmd, arg);
76 	else
77 		ret = ioctl(fd, cmd);
78 	return (ret >= 0 ? 0 : -1);
79 }
80 
81 static int
82 g_ioctl(int fd, unsigned long cmd)
83 {
84 
85 	return (g_ioctl_arg(fd, cmd, NULL));
86 }
87 
88 /*
89  * Return media size of the given provider.
90  */
91 off_t
92 g_mediasize(int fd)
93 {
94 	off_t mediasize;
95 
96 	if (g_ioctl_arg(fd, DIOCGMEDIASIZE, &mediasize) == -1)
97 		mediasize = -1;
98 	return (mediasize);
99 }
100 
101 /*
102  * Return sector size of the given provider.
103  */
104 ssize_t
105 g_sectorsize(int fd)
106 {
107 	u_int sectorsize;
108 
109 	if (g_ioctl_arg(fd, DIOCGSECTORSIZE, &sectorsize) == -1)
110 		return (-1);
111 	return ((ssize_t)sectorsize);
112 }
113 
114 /*
115  * Return stripe size of the given provider.
116  */
117 off_t
118 g_stripesize(int fd)
119 {
120 	off_t stripesize;
121 
122 	if (g_ioctl_arg(fd, DIOCGSTRIPESIZE, &stripesize) == -1)
123 		return (-1);
124 	return (stripesize);
125 }
126 
127 /*
128  * Return stripe size of the given provider.
129  */
130 off_t
131 g_stripeoffset(int fd)
132 {
133 	off_t stripeoffset;
134 
135 	if (g_ioctl_arg(fd, DIOCGSTRIPEOFFSET, &stripeoffset) == -1)
136 		return (-1);
137 	return (stripeoffset);
138 }
139 
140 /*
141  * Return the correct provider name.
142  */
143 char *
144 g_providername(int fd)
145 {
146 	char name[MAXPATHLEN];
147 
148 	if (g_ioctl_arg(fd, DIOCGPROVIDERNAME, name) == -1)
149 		return (NULL);
150 	return (strdup(name));
151 }
152 
153 /*
154  * Call BIO_FLUSH for the given provider.
155  */
156 int
157 g_flush(int fd)
158 {
159 
160 	return (g_ioctl(fd, DIOCGFLUSH));
161 }
162 
163 /*
164  * Call BIO_DELETE for the given range.
165  */
166 int
167 g_delete(int fd, off_t offset, off_t length)
168 {
169 	off_t arg[2];
170 
171 	arg[0] = offset;
172 	arg[1] = length;
173 	return (g_ioctl_arg(fd, DIOCGDELETE, arg));
174 }
175 
176 /*
177  * Return ID of the given provider.
178  */
179 int
180 g_get_ident(int fd, char *ident, size_t size)
181 {
182 	char lident[DISK_IDENT_SIZE];
183 
184 	if (g_ioctl_arg(fd, DIOCGIDENT, lident) == -1)
185 		return (-1);
186 	if (lident[0] == '\0') {
187 		errno = ENOENT;
188 		return (-1);
189 	}
190 	if (strlcpy(ident, lident, size) >= size) {
191 		errno = ENAMETOOLONG;
192 		return (-1);
193 	}
194 	return (0);
195 }
196 
197 /*
198  * Return name of the provider, which has the given ID.
199  */
200 int
201 g_get_name(const char *ident, char *name, size_t size)
202 {
203 	int fd;
204 
205 	fd = g_open_by_ident(ident, 0, name, size);
206 	if (fd == -1)
207 		return (-1);
208 	g_close(fd);
209 	return (0);
210 }
211 
212 /*
213  * Find provider name by the given ID.
214  */
215 int
216 g_open_by_ident(const char *ident, int dowrite, char *name, size_t size)
217 {
218 	char lident[DISK_IDENT_SIZE];
219 	struct gmesh mesh;
220 	struct gclass *mp;
221 	struct ggeom *gp;
222 	struct gprovider *pp;
223 	int error, fd;
224 
225 	error = geom_gettree(&mesh);
226 	if (error != 0) {
227 		errno = error;
228 		return (-1);
229 	}
230 
231 	error = ENOENT;
232 	fd = -1;
233 
234 	LIST_FOREACH(mp, &mesh.lg_class, lg_class) {
235 		LIST_FOREACH(gp, &mp->lg_geom, lg_geom) {
236 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
237 				fd = g_open(pp->lg_name, dowrite);
238 				if (fd == -1)
239 					continue;
240 				if (g_get_ident(fd, lident,
241 				    sizeof(lident)) == -1) {
242 					g_close(fd);
243 					continue;
244 				}
245 				if (strcmp(ident, lident) != 0) {
246 					g_close(fd);
247 					continue;
248 				}
249 				error = 0;
250 				if (name != NULL && strlcpy(name, pp->lg_name,
251 				    size) >= size) {
252 					error = ENAMETOOLONG;
253 					g_close(fd);
254 				}
255 				goto end;
256 			}
257 		}
258 	}
259 end:
260 	geom_deletetree(&mesh);
261 	if (error != 0) {
262 		errno = error;
263 		return (-1);
264 	}
265 	return (fd);
266 }
267 
268 /*
269  * Return the device path device given a partial or full path to its node.
270  * A pointer can be provided, which will be set to an opened file descriptor of
271  * not NULL.
272  */
273 static char *
274 g_device_path_open(const char *devpath, int *fdp, int dowrite)
275 {
276 	char *path;
277 	int fd;
278 
279 	/* Make sure that we can fail. */
280 	if (fdp != NULL)
281 		*fdp = -1;
282 
283 	/* Use the device node if we're able to open it. */
284 	fd = open(devpath, dowrite ? O_RDWR : O_RDONLY);
285 	if (fd != -1) {
286 		if ((path = strdup(devpath)) == NULL) {
287 			close(fd);
288 			return (NULL);
289 		}
290 		goto fd_ok;
291 	}
292 
293 	/* If we're not given an absolute path, assume /dev/ prefix. */
294 	if (*devpath == '/')
295 		return (NULL);
296 
297 	asprintf(&path, "%s%s", _PATH_DEV, devpath);
298 	if (path == NULL)
299 		return (NULL);
300 	fd = open(path, dowrite ? O_RDWR : O_RDONLY);
301 	if (fd == -1) {
302 		free(path);
303 		return (NULL);
304 	}
305 
306 fd_ok:
307 	/*
308 	 * Let try to get sectorsize, which will prove it is a GEOM provider.
309 	 */
310 	if (g_sectorsize(fd) == -1) {
311 		free(path);
312 		close(fd);
313 		errno = EFTYPE;
314 		return (NULL);
315 	}
316 	if (fdp != NULL)
317 		*fdp = fd;
318 	else
319 		close(fd);
320 	return (path);
321 }
322 
323 char *
324 g_device_path(const char *devpath)
325 {
326 	return (g_device_path_open(devpath, NULL, 0));
327 }
328