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>
48 #include "misc/subr.h"
57 std_metadata_decode(const u_char *data, struct std_metadata *md)
60 bcopy(data, md->md_magic, sizeof(md->md_magic));
61 md->md_version = le32dec(data + 16);
65 pathgen(const char *name, char *path, size_t size)
68 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) != 0)
69 snprintf(path, size, "%s%s", _PATH_DEV, name);
71 strlcpy(path, name, size);
75 * Greatest Common Divisor.
78 gcd(unsigned a, unsigned b)
91 * Least Common Multiple.
94 g_lcm(unsigned a, unsigned b)
97 return ((a * b) / gcd(a, b));
101 bitcount32(uint32_t x)
104 x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
105 x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
106 x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
107 x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
108 x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
113 * The size of a sector is context specific (i.e. determined by the
114 * media). But when users enter a value with a SI unit, they really
115 * mean the byte-size or byte-offset and not the size or offset in
116 * sectors. We should map the byte-oriented value into a sector-oriented
117 * value when we already know the sector size in bytes. At this time
118 * we can use g_parse_lba() function. It converts user specified
119 * value into sectors with following conditions:
120 * o Sectors size taken as argument from caller.
121 * o When no SI unit is specified the value is in sectors.
122 * o With an SI unit the value is in bytes.
123 * o The 'b' suffix forces byte interpretation and the 's'
124 * suffix forces sector interpretation.
127 * o 2 and 2s mean 2 sectors, and 2b means 2 bytes.
128 * o 4k and 4kb mean 4096 bytes, and 4ks means 4096 sectors.
132 g_parse_lba(const char *lbastr, unsigned sectorsize, off_t *sectors)
134 off_t number, mult, unit;
137 assert(lbastr != NULL);
138 assert(sectorsize > 0);
139 assert(sectors != NULL);
141 number = (off_t)strtoimax(lbastr, &s, 0);
142 if (s == lbastr || number < 0)
171 unit = 1; /* bytes */
178 unit = sectorsize; /* sector */
181 unit = 1; /* bytes */
190 if ((OFF_MAX / unit) < mult || (OFF_MAX / mult / unit) < number)
192 number *= mult * unit;
193 if (number % sectorsize)
195 number /= sectorsize;
201 g_get_mediasize(const char *name)
203 char path[MAXPATHLEN];
207 pathgen(name, path, sizeof(path));
208 fd = open(path, O_RDONLY);
211 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0) {
220 g_get_sectorsize(const char *name)
222 char path[MAXPATHLEN];
226 pathgen(name, path, sizeof(path));
227 fd = open(path, O_RDONLY);
230 if (ioctl(fd, DIOCGSECTORSIZE, §orsize) < 0) {
239 g_metadata_read(const char *name, u_char *md, size_t size, const char *magic)
241 struct std_metadata stdmd;
242 char path[MAXPATHLEN];
248 pathgen(name, path, sizeof(path));
252 fd = open(path, O_RDONLY);
255 mediasize = g_get_mediasize(name);
256 if (mediasize == 0) {
260 sectorsize = g_get_sectorsize(name);
261 if (sectorsize == 0) {
265 assert(sectorsize >= size);
266 sector = malloc(sectorsize);
267 if (sector == NULL) {
271 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
272 (ssize_t)sectorsize) {
277 std_metadata_decode(sector, &stdmd);
278 if (strcmp(stdmd.md_magic, magic) != 0) {
283 bcopy(sector, md, size);
292 g_metadata_store(const char *name, u_char *md, size_t size)
294 char path[MAXPATHLEN];
300 pathgen(name, path, sizeof(path));
304 fd = open(path, O_WRONLY);
307 mediasize = g_get_mediasize(name);
308 if (mediasize == 0) {
312 sectorsize = g_get_sectorsize(name);
313 if (sectorsize == 0) {
317 assert(sectorsize >= size);
318 sector = malloc(sectorsize);
319 if (sector == NULL) {
323 bcopy(md, sector, size);
324 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
325 (ssize_t)sectorsize) {
329 (void)ioctl(fd, DIOCGFLUSH, NULL);
338 g_metadata_clear(const char *name, const char *magic)
340 struct std_metadata md;
341 char path[MAXPATHLEN];
347 pathgen(name, path, sizeof(path));
351 fd = open(path, O_RDWR);
354 mediasize = g_get_mediasize(name);
355 if (mediasize == 0) {
359 sectorsize = g_get_sectorsize(name);
360 if (sectorsize == 0) {
364 sector = malloc(sectorsize);
365 if (sector == NULL) {
370 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
371 (ssize_t)sectorsize) {
375 std_metadata_decode(sector, &md);
376 if (strcmp(md.md_magic, magic) != 0) {
381 bzero(sector, sectorsize);
382 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
383 (ssize_t)sectorsize) {
387 (void)ioctl(fd, DIOCGFLUSH, NULL);
396 * Set an error message, if one does not already exist.
399 gctl_error(struct gctl_req *req, const char *error, ...)
403 if (req->error != NULL)
406 vasprintf(&req->error, error, ap);
411 gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
413 struct gctl_req_arg *argp;
418 vsnprintf(param, sizeof(param), pfmt, ap);
419 for (i = 0; i < req->narg; i++) {
421 if (strcmp(param, argp->name))
423 if (!(argp->flag & GCTL_PARAM_RD))
427 /* We are looking for a string. */
429 fprintf(stderr, "No length argument (%s).\n",
433 if (((char *)p)[argp->len - 1] != '\0') {
434 fprintf(stderr, "Unterminated argument (%s).\n",
438 } else if ((int)len != argp->len) {
439 fprintf(stderr, "Wrong length %s argument.\n", param);
444 fprintf(stderr, "No such argument (%s).\n", param);
449 gctl_get_int(struct gctl_req *req, const char *pfmt, ...)
455 p = gctl_get_param(req, sizeof(int), pfmt, ap);
461 gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
467 p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
473 gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
479 p = gctl_get_param(req, 0, pfmt, ap);
485 gctl_change_param(struct gctl_req *req, const char *name, int len,
488 struct gctl_req_arg *ap;
491 if (req == NULL || req->error != NULL)
493 for (i = 0; i < req->narg; i++) {
495 if (strcmp(ap->name, name) != 0)
497 ap->value = __DECONST(void *, value);
499 ap->flag &= ~GCTL_PARAM_ASCII;
501 } else if (len < 0) {
502 ap->flag |= GCTL_PARAM_ASCII;
503 ap->len = strlen(value) + 1;
511 gctl_delete_param(struct gctl_req *req, const char *name)
513 struct gctl_req_arg *ap;
516 if (req == NULL || req->error != NULL)
520 while (i < req->narg) {
522 if (strcmp(ap->name, name) == 0)
531 while (i < req->narg) {
532 req->arg[i] = req->arg[i + 1];
539 gctl_has_param(struct gctl_req *req, const char *name)
541 struct gctl_req_arg *ap;
544 if (req == NULL || req->error != NULL)
547 for (i = 0; i < req->narg; i++) {
549 if (strcmp(ap->name, name) == 0)