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