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