2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
45 static char *g_device_path_open(const char *, int *, int);
48 * Open the given provider and at least check if this is a block device.
51 g_open(const char *name, int dowrite)
56 path = g_device_path_open(name, &fd, dowrite);
72 g_ioctl_arg(int fd, unsigned long cmd, void *arg)
77 ret = ioctl(fd, cmd, arg);
80 return (ret >= 0 ? 0 : -1);
84 g_ioctl(int fd, unsigned long cmd)
87 return (g_ioctl_arg(fd, cmd, NULL));
91 * Return media size of the given provider.
98 if (g_ioctl_arg(fd, DIOCGMEDIASIZE, &mediasize) == -1)
104 * Return sector size of the given provider.
111 if (g_ioctl_arg(fd, DIOCGSECTORSIZE, §orsize) == -1)
113 return ((ssize_t)sectorsize);
117 * Return stripe size of the given provider.
124 if (g_ioctl_arg(fd, DIOCGSTRIPESIZE, &stripesize) == -1)
130 * Return stripe size of the given provider.
133 g_stripeoffset(int fd)
137 if (g_ioctl_arg(fd, DIOCGSTRIPEOFFSET, &stripeoffset) == -1)
139 return (stripeoffset);
143 * Return the correct provider name.
146 g_providername(int fd)
148 char name[MAXPATHLEN];
150 if (g_ioctl_arg(fd, DIOCGPROVIDERNAME, name) == -1)
152 return (strdup(name));
156 * Call BIO_FLUSH for the given provider.
162 return (g_ioctl(fd, DIOCGFLUSH));
166 * Call BIO_DELETE for the given range.
169 g_delete(int fd, off_t offset, off_t length)
175 return (g_ioctl_arg(fd, DIOCGDELETE, arg));
179 * Return ID of the given provider.
182 g_get_ident(int fd, char *ident, size_t size)
184 char lident[DISK_IDENT_SIZE];
186 if (g_ioctl_arg(fd, DIOCGIDENT, lident) == -1)
188 if (lident[0] == '\0') {
192 if (strlcpy(ident, lident, size) >= size) {
193 errno = ENAMETOOLONG;
200 * Return name of the provider, which has the given ID.
203 g_get_name(const char *ident, char *name, size_t size)
207 fd = g_open_by_ident(ident, 0, name, size);
215 * Find provider name by the given ID.
218 g_open_by_ident(const char *ident, int dowrite, char *name, size_t size)
220 char lident[DISK_IDENT_SIZE];
224 struct gprovider *pp;
227 error = geom_gettree(&mesh);
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);
242 if (g_get_ident(fd, lident,
243 sizeof(lident)) == -1) {
247 if (strcmp(ident, lident) != 0) {
252 if (name != NULL && strlcpy(name, pp->lg_name,
254 error = ENAMETOOLONG;
262 geom_deletetree(&mesh);
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
276 g_device_path_open(const char *devpath, int *fdp, int dowrite)
281 /* Make sure that we can fail. */
284 /* Use the device node if we're able to open it. */
286 fd = open(devpath, dowrite ? O_RDWR : O_RDONLY);
290 * Let try to get sectorsize, which will prove it is a GEOM
293 if (g_sectorsize(fd) == -1) {
298 if ((path = strdup(devpath)) == NULL) {
309 /* If we're not given an absolute path, assume /dev/ prefix. */
310 if (*devpath != '/') {
311 asprintf(&path, "%s%s", _PATH_DEV, devpath);
314 fd = open(path, dowrite ? O_RDWR : O_RDONLY);
320 * Let try to get sectorsize, which will prove it is a GEOM
323 if (g_sectorsize(fd) == -1) {
339 g_device_path(const char *devpath)
341 return (g_device_path_open(devpath, NULL, 0));