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
45static 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 */
50int
51g_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
64int
65g_close(int fd)
66{
67
68	return (close(fd));
69}
70
71static int
72g_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
83static int
84g_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 */
93off_t
94g_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 */
106ssize_t
107g_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 stripe size of the given provider.
118 */
119off_t
120g_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 */
132off_t
133g_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 */
145char *
146g_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 */
158int
159g_flush(int fd)
160{
161
162	return (g_ioctl(fd, DIOCGFLUSH));
163}
164
165/*
166 * Call BIO_DELETE for the given range.
167 */
168int
169g_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 */
181int
182g_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 */
202int
203g_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 */
217int
218g_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	}
261end:
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 */
275static char *
276g_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	/* Use the device node if we're able to open it. */
285	do {
286		fd = open(devpath, dowrite ? O_RDWR : O_RDONLY);
287		if (fd == -1)
288			break;
289		/*
290		 * Let try to get sectorsize, which will prove it is a GEOM
291		 * provider.
292		 */
293		if (g_sectorsize(fd) == -1) {
294			close(fd);
295			errno = EFTYPE;
296			return (NULL);
297		}
298		if ((path = strdup(devpath)) == NULL) {
299			close(fd);
300			return (NULL);
301		}
302		if (fdp != NULL)
303			*fdp = fd;
304		else
305			close(fd);
306		return (path);
307	} while (0);
308
309	/* If we're not given an absolute path, assume /dev/ prefix. */
310	if (*devpath != '/') {
311		asprintf(&path, "%s%s", _PATH_DEV, devpath);
312		if (path == NULL)
313			return (NULL);
314		fd = open(path, dowrite ? O_RDWR : O_RDONLY);
315		if (fd == -1) {
316			free(path);
317			return (NULL);
318		}
319		/*
320		 * Let try to get sectorsize, which will prove it is a GEOM
321		 * provider.
322		 */
323		if (g_sectorsize(fd) == -1) {
324			free(path);
325			close(fd);
326			errno = EFTYPE;
327			return (NULL);
328		}
329		if (fdp != NULL)
330			*fdp = fd;
331		else
332			close(fd);
333		return (path);
334	}
335	return (NULL);
336}
337
338char *
339g_device_path(const char *devpath)
340{
341	return (g_device_path_open(devpath, NULL, 0));
342}
343