xref: /freebsd/lib/libgeom/geom_util.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 	if (fd == -1)
60 		return (-1);
61 	return (fd);
62 }
63 
64 int
65 g_close(int fd)
66 {
67 
68 	return (close(fd));
69 }
70 
71 static int
72 g_ioctl_arg(int fd, unsigned long cmd, void *arg)
73 {
74 	int ret;
75 
76 	if (arg != NULL)
77 		ret = ioctl(fd, cmd, arg);
78 	else
79 		ret = ioctl(fd, cmd);
80 	return (ret >= 0 ? 0 : -1);
81 }
82 
83 static int
84 g_ioctl(int fd, unsigned long cmd)
85 {
86 
87 	return (g_ioctl_arg(fd, cmd, NULL));
88 }
89 
90 /*
91  * Return media size of the given provider.
92  */
93 off_t
94 g_mediasize(int fd)
95 {
96 	off_t mediasize;
97 
98 	if (g_ioctl_arg(fd, DIOCGMEDIASIZE, &mediasize) == -1)
99 		mediasize = -1;
100 	return (mediasize);
101 }
102 
103 /*
104  * Return sector size of the given provider.
105  */
106 ssize_t
107 g_sectorsize(int fd)
108 {
109 	u_int sectorsize;
110 
111 	if (g_ioctl_arg(fd, DIOCGSECTORSIZE, &sectorsize) == -1)
112 		return (-1);
113 	return ((ssize_t)sectorsize);
114 }
115 
116 /*
117  * Return the correct provider name.
118  */
119 char *
120 g_providername(int fd)
121 {
122 	char name[MAXPATHLEN];
123 
124 	if (g_ioctl_arg(fd, DIOCGPROVIDERNAME, name) == -1)
125 		return (NULL);
126 	return (strdup(name));
127 }
128 
129 /*
130  * Call BIO_FLUSH for the given provider.
131  */
132 int
133 g_flush(int fd)
134 {
135 
136 	return (g_ioctl(fd, DIOCGFLUSH));
137 }
138 
139 /*
140  * Call BIO_DELETE for the given range.
141  */
142 int
143 g_delete(int fd, off_t offset, off_t length)
144 {
145 	off_t arg[2];
146 
147 	arg[0] = offset;
148 	arg[1] = length;
149 	return (g_ioctl_arg(fd, DIOCGDELETE, arg));
150 }
151 
152 /*
153  * Return ID of the given provider.
154  */
155 int
156 g_get_ident(int fd, char *ident, size_t size)
157 {
158 	char lident[DISK_IDENT_SIZE];
159 
160 	if (g_ioctl_arg(fd, DIOCGIDENT, lident) == -1)
161 		return (-1);
162 	if (lident[0] == '\0') {
163 		errno = ENOENT;
164 		return (-1);
165 	}
166 	if (strlcpy(ident, lident, size) >= size) {
167 		errno = ENAMETOOLONG;
168 		return (-1);
169 	}
170 	return (0);
171 }
172 
173 /*
174  * Return name of the provider, which has the given ID.
175  */
176 int
177 g_get_name(const char *ident, char *name, size_t size)
178 {
179 	int fd;
180 
181 	fd = g_open_by_ident(ident, 0, name, size);
182 	if (fd == -1)
183 		return (-1);
184 	g_close(fd);
185 	return (0);
186 }
187 
188 /*
189  * Find provider name by the given ID.
190  */
191 int
192 g_open_by_ident(const char *ident, int dowrite, char *name, size_t size)
193 {
194 	char lident[DISK_IDENT_SIZE];
195 	struct gmesh mesh;
196 	struct gclass *mp;
197 	struct ggeom *gp;
198 	struct gprovider *pp;
199 	int error, fd;
200 
201 	error = geom_gettree(&mesh);
202 	if (error != 0) {
203 		errno = error;
204 		return (-1);
205 	}
206 
207 	error = ENOENT;
208 	fd = -1;
209 
210 	LIST_FOREACH(mp, &mesh.lg_class, lg_class) {
211 		LIST_FOREACH(gp, &mp->lg_geom, lg_geom) {
212 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
213 				fd = g_open(pp->lg_name, dowrite);
214 				if (fd == -1)
215 					continue;
216 				if (g_get_ident(fd, lident,
217 				    sizeof(lident)) == -1) {
218 					g_close(fd);
219 					continue;
220 				}
221 				if (strcmp(ident, lident) != 0) {
222 					g_close(fd);
223 					continue;
224 				}
225 				error = 0;
226 				if (name != NULL && strlcpy(name, pp->lg_name,
227 				    size) >= size) {
228 					error = ENAMETOOLONG;
229 					g_close(fd);
230 				}
231 				goto end;
232 			}
233 		}
234 	}
235 end:
236 	geom_deletetree(&mesh);
237 	if (error != 0) {
238 		errno = error;
239 		return (-1);
240 	}
241 	return (fd);
242 }
243 
244 /*
245  * Return the device path device given a partial or full path to its node.
246  * A pointer can be provided, which will be set to an opened file descriptor of
247  * not NULL.
248  */
249 static char *
250 g_device_path_open(const char *devpath, int *fdp, int dowrite)
251 {
252 	char *path;
253 	int fd;
254 
255 	/* Make sure that we can fail. */
256 	if (fdp != NULL)
257 		*fdp = -1;
258 	/* Use the device node if we're able to open it. */
259 	do {
260 		fd = open(devpath, dowrite ? O_RDWR : O_RDONLY);
261 		if (fd == -1)
262 			break;
263 		/*
264 		 * Let try to get sectorsize, which will prove it is a GEOM
265 		 * provider.
266 		 */
267 		if (g_sectorsize(fd) == -1) {
268 			close(fd);
269 			errno = EFTYPE;
270 			return (NULL);
271 		}
272 		if ((path = strdup(devpath)) == NULL) {
273 			close(fd);
274 			return (NULL);
275 		}
276 		if (fdp != NULL)
277 			*fdp = fd;
278 		else
279 			close(fd);
280 		return (path);
281 	} while (0);
282 
283 	/* If we're not given an absolute path, assume /dev/ prefix. */
284 	if (*devpath != '/') {
285 		asprintf(&path, "%s%s", _PATH_DEV, devpath);
286 		if (path == NULL)
287 			return (NULL);
288 		fd = open(path, dowrite ? O_RDWR : O_RDONLY);
289 		if (fd == -1) {
290 			free(path);
291 			return (NULL);
292 		}
293 		/*
294 		 * Let try to get sectorsize, which will prove it is a GEOM
295 		 * provider.
296 		 */
297 		if (g_sectorsize(fd) == -1) {
298 			free(path);
299 			close(fd);
300 			errno = EFTYPE;
301 			return (NULL);
302 		}
303 		if (fdp != NULL)
304 			*fdp = fd;
305 		else
306 			close(fd);
307 		return (path);
308 	}
309 	return (NULL);
310 }
311 
312 char *
313 g_device_path(const char *devpath)
314 {
315 	return (g_device_path_open(devpath, NULL, 0));
316 }
317