geom_util.c revision 169299
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: head/lib/libgeom/geom_util.c 169299 2007-05-06 01:17:46Z pjd $"); 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/* 46 * Open the given provider and at least check if this is a block device. 47 */ 48int 49g_open(const char *name, int write) 50{ 51 char path[MAXPATHLEN]; 52 int fd; 53 54 if (name[0] == '/') 55 strlcpy(path, name, sizeof(path)); 56 else 57 snprintf(path, sizeof(path), "%s%s", _PATH_DEV, name); 58 59 fd = open(path, write ? O_RDWR : O_RDONLY); 60 if (fd == -1) 61 return (-1); 62 /* Let try to get sectorsize, which will prove it is a GEOM provider. */ 63 if (g_sectorsize(fd) == -1) { 64 close(fd); 65 errno = EFTYPE; 66 return (-1); 67 } 68 return (fd); 69} 70 71int 72g_close(int fd) 73{ 74 75 return (close(fd)); 76} 77 78static int 79g_ioctl_arg(int fd, unsigned long cmd, void *arg) 80{ 81 int ret; 82 83 if (arg != NULL) 84 ret = ioctl(fd, cmd, arg); 85 else 86 ret = ioctl(fd, cmd); 87 return (ret >= 0 ? 0 : -1); 88} 89 90static int 91g_ioctl(int fd, unsigned long cmd) 92{ 93 94 return (g_ioctl_arg(fd, cmd, NULL)); 95} 96 97/* 98 * Return media size of the given provider. 99 */ 100off_t 101g_mediasize(int fd) 102{ 103 off_t mediasize; 104 105 if (g_ioctl_arg(fd, DIOCGMEDIASIZE, &mediasize) == -1) 106 mediasize = -1; 107 return (mediasize); 108} 109 110/* 111 * Return sector size of the given provider. 112 */ 113ssize_t 114g_sectorsize(int fd) 115{ 116 u_int sectorsize; 117 118 if (g_ioctl_arg(fd, DIOCGSECTORSIZE, §orsize) == -1) 119 return (-1); 120 return ((ssize_t)sectorsize); 121} 122 123/* 124 * Call BIO_FLUSH for the given provider. 125 */ 126int 127g_flush(int fd) 128{ 129 130 return (g_ioctl(fd, DIOCGFLUSH)); 131} 132 133/* 134 * Call BIO_DELETE for the given range. 135 */ 136int 137g_delete(int fd, off_t offset, off_t length) 138{ 139 off_t arg[2]; 140 141 arg[0] = offset; 142 arg[1] = length; 143 return (g_ioctl_arg(fd, DIOCGDELETE, arg)); 144} 145 146/* 147 * Return ID of the given provider. 148 */ 149int 150g_get_ident(int fd, char *ident, size_t size) 151{ 152 char lident[DISK_IDENT_SIZE]; 153 154 if (g_ioctl_arg(fd, DIOCGIDENT, lident) == -1) 155 return (-1); 156 if (lident[0] == '\0') { 157 errno = ENOENT; 158 return (-1); 159 } 160 if (strlcpy(ident, lident, size) >= size) { 161 errno = ENAMETOOLONG; 162 return (-1); 163 } 164 return (0); 165} 166 167/* 168 * Return name of the provider, which has the given ID. 169 */ 170int 171g_get_name(const char *ident, char *name, size_t size) 172{ 173 int fd; 174 175 fd = g_open_by_ident(ident, 0, name, size); 176 if (fd == -1) 177 return (-1); 178 g_close(fd); 179 return (0); 180} 181 182/* 183 * Find provider name by the given ID. 184 */ 185int 186g_open_by_ident(const char *ident, int write, char *name, size_t size) 187{ 188 char lident[DISK_IDENT_SIZE]; 189 struct gmesh mesh; 190 struct gclass *mp; 191 struct ggeom *gp; 192 struct gprovider *pp; 193 int error, fd; 194 195 error = geom_gettree(&mesh); 196 if (error != 0) { 197 errno = error; 198 return (-1); 199 } 200 201 error = ENOENT; 202 fd = -1; 203 204 LIST_FOREACH(mp, &mesh.lg_class, lg_class) { 205 LIST_FOREACH(gp, &mp->lg_geom, lg_geom) { 206 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 207 fd = g_open(pp->lg_name, write); 208 if (fd == -1) 209 continue; 210 if (g_get_ident(fd, lident, 211 sizeof(lident)) == -1) { 212 g_close(fd); 213 continue; 214 } 215 if (strcmp(ident, lident) != 0) { 216 g_close(fd); 217 continue; 218 } 219 error = 0; 220 if (name != NULL && strlcpy(name, pp->lg_name, 221 size) >= size) { 222 error = ENAMETOOLONG; 223 g_close(fd); 224 } 225 goto end; 226 } 227 } 228 } 229end: 230 geom_deletetree(&mesh); 231 if (error != 0) { 232 errno = error; 233 return (-1); 234 } 235 return (fd); 236} 237