2 * Copyright (c) 2004-2010 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 unsigned char *data, struct std_metadata *md)
60 bcopy(data, md->md_magic, sizeof(md->md_magic));
61 md->md_version = le32dec(data + 16);
65 * Greatest Common Divisor.
68 gcd(unsigned int a, unsigned int b)
81 * Least Common Multiple.
84 g_lcm(unsigned int a, unsigned int b)
87 return ((a * b) / gcd(a, b));
91 bitcount32(uint32_t x)
94 x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
95 x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
96 x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
97 x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
98 x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
103 * The size of a sector is context specific (i.e. determined by the
104 * media). But when users enter a value with a SI unit, they really
105 * mean the byte-size or byte-offset and not the size or offset in
106 * sectors. We should map the byte-oriented value into a sector-oriented
107 * value when we already know the sector size in bytes. At this time
108 * we can use g_parse_lba() function. It converts user specified
109 * value into sectors with following conditions:
110 * o Sectors size taken as argument from caller.
111 * o When no SI unit is specified the value is in sectors.
112 * o With an SI unit the value is in bytes.
113 * o The 'b' suffix forces byte interpretation and the 's'
114 * suffix forces sector interpretation.
117 * o 2 and 2s mean 2 sectors, and 2b means 2 bytes.
118 * o 4k and 4kb mean 4096 bytes, and 4ks means 4096 sectors.
122 g_parse_lba(const char *lbastr, unsigned int sectorsize, off_t *sectors)
124 off_t number, mult, unit;
127 assert(lbastr != NULL);
128 assert(sectorsize > 0);
129 assert(sectors != NULL);
131 number = (off_t)strtoimax(lbastr, &s, 0);
132 if (s == lbastr || number < 0)
161 unit = 1; /* bytes */
168 unit = sectorsize; /* sector */
171 unit = 1; /* bytes */
180 if ((OFF_MAX / unit) < mult || (OFF_MAX / mult / unit) < number)
182 number *= mult * unit;
183 if (number % sectorsize)
185 number /= sectorsize;
191 g_get_mediasize(const char *name)
196 fd = g_open(name, 0);
199 mediasize = g_mediasize(fd);
207 g_get_sectorsize(const char *name)
212 fd = g_open(name, 0);
215 sectorsize = g_sectorsize(fd);
216 if (sectorsize == -1)
219 return ((unsigned int)sectorsize);
223 g_metadata_read(const char *name, unsigned char *md, size_t size,
226 struct std_metadata stdmd;
227 unsigned char *sector;
235 fd = g_open(name, 0);
238 mediasize = g_mediasize(fd);
239 if (mediasize == -1) {
243 sectorsize = g_sectorsize(fd);
244 if (sectorsize == -1) {
248 assert(sectorsize >= (ssize_t)size);
249 sector = malloc(sectorsize);
250 if (sector == NULL) {
254 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
260 std_metadata_decode(sector, &stdmd);
261 if (strcmp(stdmd.md_magic, magic) != 0) {
266 bcopy(sector, md, size);
275 g_metadata_store(const char *name, const unsigned char *md, size_t size)
277 unsigned char *sector;
285 fd = g_open(name, 1);
288 mediasize = g_mediasize(fd);
289 if (mediasize == -1) {
293 sectorsize = g_sectorsize(fd);
294 if (sectorsize == -1) {
298 assert(sectorsize >= (ssize_t)size);
299 sector = malloc(sectorsize);
300 if (sector == NULL) {
304 bcopy(md, sector, size);
305 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
319 g_metadata_clear(const char *name, const char *magic)
321 struct std_metadata md;
322 unsigned char *sector;
330 fd = g_open(name, 1);
333 mediasize = g_mediasize(fd);
334 if (mediasize == 0) {
338 sectorsize = g_sectorsize(fd);
339 if (sectorsize == 0) {
343 sector = malloc(sectorsize);
344 if (sector == NULL) {
349 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
354 std_metadata_decode(sector, &md);
355 if (strcmp(md.md_magic, magic) != 0) {
360 bzero(sector, sectorsize);
361 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
375 * Set an error message, if one does not already exist.
378 gctl_error(struct gctl_req *req, const char *error, ...)
382 if (req != NULL && req->error != NULL)
386 vasprintf(&req->error, error, ap);
388 vfprintf(stderr, error, ap);
389 fprintf(stderr, "\n");
395 gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
397 struct gctl_req_arg *argp;
402 vsnprintf(param, sizeof(param), pfmt, ap);
403 for (i = 0; i < req->narg; i++) {
405 if (strcmp(param, argp->name))
407 if (!(argp->flag & GCTL_PARAM_RD))
411 /* We are looking for a string. */
413 fprintf(stderr, "No length argument (%s).\n",
417 if (((char *)p)[argp->len - 1] != '\0') {
418 fprintf(stderr, "Unterminated argument (%s).\n",
422 } else if ((int)len != argp->len) {
423 fprintf(stderr, "Wrong length %s argument.\n", param);
428 fprintf(stderr, "No such argument (%s).\n", param);
433 gctl_get_int(struct gctl_req *req, const char *pfmt, ...)
439 p = gctl_get_param(req, sizeof(int), pfmt, ap);
445 gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
451 p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
457 gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
463 p = gctl_get_param(req, 0, pfmt, ap);
469 gctl_change_param(struct gctl_req *req, const char *name, int len,
472 struct gctl_req_arg *ap;
475 if (req == NULL || req->error != NULL)
477 for (i = 0; i < req->narg; i++) {
479 if (strcmp(ap->name, name) != 0)
481 ap->value = __DECONST(void *, value);
483 ap->flag &= ~GCTL_PARAM_ASCII;
485 } else if (len < 0) {
486 ap->flag |= GCTL_PARAM_ASCII;
487 ap->len = strlen(value) + 1;
495 gctl_delete_param(struct gctl_req *req, const char *name)
497 struct gctl_req_arg *ap;
500 if (req == NULL || req->error != NULL)
504 while (i < req->narg) {
506 if (strcmp(ap->name, name) == 0)
515 while (i < req->narg) {
516 req->arg[i] = req->arg[i + 1];
523 gctl_has_param(struct gctl_req *req, const char *name)
525 struct gctl_req_arg *ap;
528 if (req == NULL || req->error != NULL)
531 for (i = 0; i < req->narg; i++) {
533 if (strcmp(ap->name, name) == 0)