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) {
247 g_metadata_clear(const char *name, const char *magic)
249 struct std_metadata md;
250 char path[MAXPATHLEN];
256 pathgen(name, path, sizeof(path));
260 fd = open(path, O_RDWR);
263 mediasize = g_get_mediasize(name);
264 if (mediasize == 0) {
268 sectorsize = g_get_sectorsize(name);
269 if (sectorsize == 0) {
273 sector = malloc(sectorsize);
274 if (sector == NULL) {
279 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
280 (ssize_t)sectorsize) {
284 std_metadata_decode(sector, &md);
285 if (strcmp(md.md_magic, magic) != 0) {
290 bzero(sector, sectorsize);
291 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
292 (ssize_t)sectorsize) {
304 * Set an error message, if one does not already exist.
307 gctl_error(struct gctl_req *req, const char *error, ...)
311 if (req->error != NULL)
314 vasprintf(&req->error, error, ap);
319 gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
321 struct gctl_req_arg *argp;
326 vsnprintf(param, sizeof(param), pfmt, ap);
327 for (i = 0; i < req->narg; i++) {
329 if (strcmp(param, argp->name))
331 if (!(argp->flag & GCTL_PARAM_RD))
335 /* We are looking for a string. */
337 fprintf(stderr, "No length argument (%s).\n",
341 if (((char *)p)[argp->len - 1] != '\0') {
342 fprintf(stderr, "Unterminated argument (%s).\n",
346 } else if ((int)len != argp->len) {
347 fprintf(stderr, "Wrong length %s argument.\n", param);
352 fprintf(stderr, "No such argument (%s).\n", param);
357 gctl_get_int(struct gctl_req *req, const char *pfmt, ...)
363 p = gctl_get_param(req, sizeof(int), pfmt, ap);
369 gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
375 p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
381 gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
387 p = gctl_get_param(req, 0, pfmt, ap);
393 gctl_change_param(struct gctl_req *req, const char *name, int len,
396 struct gctl_req_arg *ap;
399 if (req == NULL || req->error != NULL)
401 for (i = 0; i < req->narg; i++) {
403 if (strcmp(ap->name, name) != 0)
405 ap->value = __DECONST(void *, value);
407 ap->flag &= ~GCTL_PARAM_ASCII;
409 } else if (len < 0) {
410 ap->flag |= GCTL_PARAM_ASCII;
411 ap->len = strlen(value) + 1;
419 gctl_delete_param(struct gctl_req *req, const char *name)
421 struct gctl_req_arg *ap;
424 if (req == NULL || req->error != NULL)
428 while (i < req->narg) {
430 if (strcmp(ap->name, name) == 0)
438 while (i < req->narg) {
439 req->arg[i] = req->arg[i + 1];
446 gctl_has_param(struct gctl_req *req, const char *name)
448 struct gctl_req_arg *ap;
451 if (req == NULL || req->error != NULL)
454 for (i = 0; i < req->narg; i++) {
456 if (strcmp(ap->name, name) == 0)