summaryrefslogtreecommitdiffstats
path: root/lib/libgeom/geom_util.c
blob: 539b6c62dbb6d58755874fa1f5c9b06f58207cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*-
 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/disk.h>
#include <sys/stat.h>

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <paths.h>

#include <libgeom.h>

static char	*g_device_path_open(const char *, int *, int);

/*
 * Open the given provider and at least check if this is a block device.
 */
int
g_open(const char *name, int dowrite)
{
	char *path;
	int fd;

	path = g_device_path_open(name, &fd, dowrite);
	if (path != NULL)
		free(path);
	if (fd == -1)
		return (-1);
	return (fd);
}

int
g_close(int fd)
{

	return (close(fd));
}

static int
g_ioctl_arg(int fd, unsigned long cmd, void *arg)
{
	int ret;

	if (arg != NULL)
		ret = ioctl(fd, cmd, arg);
	else
		ret = ioctl(fd, cmd);
	return (ret >= 0 ? 0 : -1);
}

static int
g_ioctl(int fd, unsigned long cmd)
{

	return (g_ioctl_arg(fd, cmd, NULL));
}

/*
 * Return media size of the given provider.
 */
off_t
g_mediasize(int fd)
{
	off_t mediasize;

	if (g_ioctl_arg(fd, DIOCGMEDIASIZE, &mediasize) == -1)
		mediasize = -1;
	return (mediasize);
}

/*
 * Return sector size of the given provider.
 */
ssize_t
g_sectorsize(int fd)
{
	u_int sectorsize;

	if (g_ioctl_arg(fd, DIOCGSECTORSIZE, &sectorsize) == -1)
		return (-1);
	return ((ssize_t)sectorsize);
}

/*
 * Return the correct provider name.
 */
char *
g_providername(int fd)
{
	char name[MAXPATHLEN];

	if (g_ioctl_arg(fd, DIOCGPROVIDERNAME, name) == -1)
		return (NULL);
	return (strdup(name));
}

/*
 * Call BIO_FLUSH for the given provider.
 */
int
g_flush(int fd)
{

	return (g_ioctl(fd, DIOCGFLUSH));
}

/*
 * Call BIO_DELETE for the given range.
 */
int
g_delete(int fd, off_t offset, off_t length)
{
	off_t arg[2];

	arg[0] = offset;
	arg[1] = length;
	return (g_ioctl_arg(fd, DIOCGDELETE, arg));
}

/*
 * Return ID of the given provider.
 */
int
g_get_ident(int fd, char *ident, size_t size)
{
	char lident[DISK_IDENT_SIZE];

	if (g_ioctl_arg(fd, DIOCGIDENT, lident) == -1)
		return (-1);
	if (lident[0] == '\0') {
		errno = ENOENT;
		return (-1);
	}
	if (strlcpy(ident, lident, size) >= size) {
		errno = ENAMETOOLONG;
		return (-1);
	}
	return (0);
}

/*
 * Return name of the provider, which has the given ID.
 */
int
g_get_name(const char *ident, char *name, size_t size)
{
	int fd;

	fd = g_open_by_ident(ident, 0, name, size);
	if (fd == -1)
		return (-1);
	g_close(fd);
	return (0);
}

/*
 * Find provider name by the given ID.
 */
int
g_open_by_ident(const char *ident, int dowrite, char *name, size_t size)
{
	char lident[DISK_IDENT_SIZE];
	struct gmesh mesh;
	struct gclass *mp;
	struct ggeom *gp;
	struct gprovider *pp;
	int error, fd;

	error = geom_gettree(&mesh);
	if (error != 0) {
		errno = error;
		return (-1);
	}

	error = ENOENT;
	fd = -1;

	LIST_FOREACH(mp, &mesh.lg_class, lg_class) {
		LIST_FOREACH(gp, &mp->lg_geom, lg_geom) {
			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
				fd = g_open(pp->lg_name, dowrite);
				if (fd == -1)
					continue;
				if (g_get_ident(fd, lident,
				    sizeof(lident)) == -1) {
					g_close(fd);
					continue;
				}
				if (strcmp(ident, lident) != 0) {
					g_close(fd);
					continue;
				}
				error = 0;
				if (name != NULL && strlcpy(name, pp->lg_name,
				    size) >= size) {
					error = ENAMETOOLONG;
					g_close(fd);
				}
				goto end;
			}
		}
	}
end:
	geom_deletetree(&mesh);
	if (error != 0) {
		errno = error;
		return (-1);
	}
	return (fd);
}

/*
 * Return the device path device given a partial or full path to its node.
 * A pointer can be provided, which will be set to an opened file descriptor of
 * not NULL.
 */
static char *
g_device_path_open(const char *devpath, int *fdp, int dowrite)
{
	char *path;
	int fd;

	/* Make sure that we can fail. */
	if (fdp != NULL)
		*fdp = -1;
	/* Use the device node if we're able to open it. */
	do {
		fd = open(devpath, dowrite ? O_RDWR : O_RDONLY);
		if (fd == -1)
			break;
		/*
		 * Let try to get sectorsize, which will prove it is a GEOM
		 * provider. 
		 */
		if (g_sectorsize(fd) == -1) {
			close(fd);
			errno = EFTYPE;
			return (NULL);
		}
		if ((path = strdup(devpath)) == NULL) {
			close(fd);
			return (NULL);
		}
		if (fdp != NULL)
			*fdp = fd;
		else
			close(fd);
		return (path);
	} while (0);

	/* If we're not given an absolute path, assume /dev/ prefix. */
	if (*devpath != '/') {
		asprintf(&path, "%s%s", _PATH_DEV, devpath);
		if (path == NULL)
			return (NULL);
		fd = open(path, dowrite ? O_RDWR : O_RDONLY);
		if (fd == -1) {
			free(path);
			return (NULL);
		}
		/*
		 * Let try to get sectorsize, which will prove it is a GEOM
		 * provider.
		 */
		if (g_sectorsize(fd) == -1) {
			free(path);
			close(fd);
			errno = EFTYPE;
			return (NULL);
		}
		if (fdp != NULL)
			*fdp = fd;
		else
			close(fd);
		return (path);
	}
	return (NULL);
}

char *
g_device_path(const char *devpath)
{
	return (g_device_path_open(devpath, NULL, 0));
}
OpenPOWER on IntegriCloud