2 * Copyright (c) 2004 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>
32 #include <sys/endian.h>
46 #include "misc/subr.h"
55 std_metadata_decode(const u_char *data, struct std_metadata *md)
58 bcopy(data, md->md_magic, sizeof(md->md_magic));
59 md->md_version = le32dec(data + 16);
63 pathgen(const char *name, char *path, size_t size)
66 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) != 0)
67 snprintf(path, size, "%s%s", _PATH_DEV, name);
69 strlcpy(path, name, size);
73 * Greatest Common Divisor.
76 gcd(unsigned a, unsigned b)
89 * Least Common Multiple.
92 g_lcm(unsigned a, unsigned b)
95 return ((a * b) / gcd(a, b));
99 bitcount32(uint32_t x)
102 x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
103 x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
104 x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
105 x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
106 x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
111 g_get_mediasize(const char *name)
113 char path[MAXPATHLEN];
117 pathgen(name, path, sizeof(path));
118 fd = open(path, O_RDONLY);
121 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
130 g_get_sectorsize(const char *name)
132 char path[MAXPATHLEN];
136 pathgen(name, path, sizeof(path));
137 fd = open(path, O_RDONLY);
140 if (ioctl(fd, DIOCGSECTORSIZE, §orsize) < 0) {
149 g_metadata_read(const char *name, u_char *md, size_t size, const char *magic)
151 struct std_metadata stdmd;
152 char path[MAXPATHLEN];
158 pathgen(name, path, sizeof(path));
162 fd = open(path, O_RDONLY);
165 mediasize = g_get_mediasize(name);
166 if (mediasize == 0) {
170 sectorsize = g_get_sectorsize(name);
171 if (sectorsize == 0) {
175 assert(sectorsize >= size);
176 sector = malloc(sectorsize);
177 if (sector == NULL) {
181 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
182 (ssize_t)sectorsize) {
187 std_metadata_decode(sector, &stdmd);
188 if (strcmp(stdmd.md_magic, magic) != 0) {
193 bcopy(sector, md, size);
202 g_metadata_store(const char *name, u_char *md, size_t size)
204 char path[MAXPATHLEN];
210 pathgen(name, path, sizeof(path));
214 fd = open(path, O_WRONLY);
217 mediasize = g_get_mediasize(name);
218 if (mediasize == 0) {
222 sectorsize = g_get_sectorsize(name);
223 if (sectorsize == 0) {
227 assert(sectorsize >= size);
228 sector = malloc(sectorsize);
229 if (sector == NULL) {
233 bcopy(md, sector, size);
234 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
235 (ssize_t)sectorsize) {
239 (void)ioctl(fd, DIOCGFLUSH, NULL);
248 g_metadata_clear(const char *name, const char *magic)
250 struct std_metadata md;
251 char path[MAXPATHLEN];
257 pathgen(name, path, sizeof(path));
261 fd = open(path, O_RDWR);
264 mediasize = g_get_mediasize(name);
265 if (mediasize == 0) {
269 sectorsize = g_get_sectorsize(name);
270 if (sectorsize == 0) {
274 sector = malloc(sectorsize);
275 if (sector == NULL) {
280 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
281 (ssize_t)sectorsize) {
285 std_metadata_decode(sector, &md);
286 if (strcmp(md.md_magic, magic) != 0) {
291 bzero(sector, sectorsize);
292 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
293 (ssize_t)sectorsize) {
297 (void)ioctl(fd, DIOCGFLUSH, NULL);
306 * Set an error message, if one does not already exist.
309 gctl_error(struct gctl_req *req, const char *error, ...)
313 if (req->error != NULL)
316 vasprintf(&req->error, error, ap);
321 gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
323 struct gctl_req_arg *argp;
328 vsnprintf(param, sizeof(param), pfmt, ap);
329 for (i = 0; i < req->narg; i++) {
331 if (strcmp(param, argp->name))
333 if (!(argp->flag & GCTL_PARAM_RD))
337 /* We are looking for a string. */
339 fprintf(stderr, "No length argument (%s).\n",
343 if (((char *)p)[argp->len - 1] != '\0') {
344 fprintf(stderr, "Unterminated argument (%s).\n",
348 } else if ((int)len != argp->len) {
349 fprintf(stderr, "Wrong length %s argument.\n", param);
354 fprintf(stderr, "No such argument (%s).\n", param);
359 gctl_get_int(struct gctl_req *req, const char *pfmt, ...)
365 p = gctl_get_param(req, sizeof(int), pfmt, ap);
371 gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
377 p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
383 gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
389 p = gctl_get_param(req, 0, pfmt, ap);
395 gctl_change_param(struct gctl_req *req, const char *name, int len,
398 struct gctl_req_arg *ap;
401 if (req == NULL || req->error != NULL)
403 for (i = 0; i < req->narg; i++) {
405 if (strcmp(ap->name, name) != 0)
407 ap->value = __DECONST(void *, value);
409 ap->flag &= ~GCTL_PARAM_ASCII;
411 } else if (len < 0) {
412 ap->flag |= GCTL_PARAM_ASCII;
413 ap->len = strlen(value) + 1;
421 gctl_delete_param(struct gctl_req *req, const char *name)
423 struct gctl_req_arg *ap;
426 if (req == NULL || req->error != NULL)
430 while (i < req->narg) {
432 if (strcmp(ap->name, name) == 0)
440 while (i < req->narg) {
441 req->arg[i] = req->arg[i + 1];
448 gctl_has_param(struct gctl_req *req, const char *name)
450 struct gctl_req_arg *ap;
453 if (req == NULL || req->error != NULL)
456 for (i = 0; i < req->narg; i++) {
458 if (strcmp(ap->name, name) == 0)