2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/endian.h>
50 #include "misc/subr.h"
59 std_metadata_decode(const unsigned char *data, struct std_metadata *md)
62 bcopy(data, md->md_magic, sizeof(md->md_magic));
63 md->md_version = le32dec(data + 16);
67 * Greatest Common Divisor.
70 gcd(unsigned int a, unsigned int b)
83 * Least Common Multiple.
86 g_lcm(unsigned int a, unsigned int b)
89 return ((a * b) / gcd(a, b));
93 bitcount32(uint32_t x)
96 x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
97 x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
98 x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
99 x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
100 x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
105 * The size of a sector is context specific (i.e. determined by the
106 * media). But when users enter a value with a SI unit, they really
107 * mean the byte-size or byte-offset and not the size or offset in
108 * sectors. We should map the byte-oriented value into a sector-oriented
109 * value when we already know the sector size in bytes. At this time
110 * we can use g_parse_lba() function. It converts user specified
111 * value into sectors with following conditions:
112 * o Sectors size taken as argument from caller.
113 * o When no SI unit is specified the value is in sectors.
114 * o With an SI unit the value is in bytes.
115 * o The 'b' suffix forces byte interpretation and the 's'
116 * suffix forces sector interpretation.
119 * o 2 and 2s mean 2 sectors, and 2b means 2 bytes.
120 * o 4k and 4kb mean 4096 bytes, and 4ks means 4096 sectors.
124 g_parse_lba(const char *lbastr, unsigned int sectorsize, off_t *sectors)
126 off_t number, mult, unit;
129 assert(lbastr != NULL);
130 assert(sectorsize > 0);
131 assert(sectors != NULL);
133 number = (off_t)strtoimax(lbastr, &s, 0);
134 if (s == lbastr || number < 0)
163 unit = 1; /* bytes */
170 unit = sectorsize; /* sector */
173 unit = 1; /* bytes */
182 if ((OFF_MAX / unit) < mult || (OFF_MAX / mult / unit) < number)
184 number *= mult * unit;
185 if (number % sectorsize)
187 number /= sectorsize;
193 g_get_mediasize(const char *name)
198 fd = g_open(name, 0);
201 mediasize = g_mediasize(fd);
209 g_get_sectorsize(const char *name)
214 fd = g_open(name, 0);
217 sectorsize = g_sectorsize(fd);
218 if (sectorsize == -1)
221 return ((unsigned int)sectorsize);
225 g_metadata_read(const char *name, unsigned char *md, size_t size,
228 struct std_metadata stdmd;
229 unsigned char *sector;
237 fd = g_open(name, 0);
240 mediasize = g_mediasize(fd);
241 if (mediasize == -1) {
245 sectorsize = g_sectorsize(fd);
246 if (sectorsize == -1) {
250 assert(sectorsize >= (ssize_t)size);
251 sector = malloc(sectorsize);
252 if (sector == NULL) {
256 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
262 std_metadata_decode(sector, &stdmd);
263 if (strcmp(stdmd.md_magic, magic) != 0) {
268 bcopy(sector, md, size);
277 * Actually write the GEOM label to the provider
279 * @param name GEOM provider's name (ie "ada0")
280 * @param md Pointer to the label data to write
281 * @param size Size of the data pointed to by md
284 g_metadata_store(const char *name, const unsigned char *md, size_t size)
286 unsigned char *sector;
294 fd = g_open(name, 1);
297 mediasize = g_mediasize(fd);
298 if (mediasize == -1) {
302 sectorsize = g_sectorsize(fd);
303 if (sectorsize == -1) {
307 assert(sectorsize >= (ssize_t)size);
308 sector = malloc(sectorsize);
309 if (sector == NULL) {
313 bcopy(md, sector, size);
314 bzero(sector + size, sectorsize - size);
315 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
329 g_metadata_clear(const char *name, const char *magic)
331 struct std_metadata md;
332 unsigned char *sector;
340 fd = g_open(name, 1);
343 mediasize = g_mediasize(fd);
344 if (mediasize == 0) {
348 sectorsize = g_sectorsize(fd);
349 if (sectorsize <= 0) {
353 sector = malloc(sectorsize);
354 if (sector == NULL) {
359 if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
364 std_metadata_decode(sector, &md);
365 if (strcmp(md.md_magic, magic) != 0) {
370 bzero(sector, sectorsize);
371 if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
384 * Set an error message, if one does not already exist.
387 gctl_error(struct gctl_req *req, const char *error, ...)
391 if (req != NULL && req->error != NULL)
395 vasprintf(&req->error, error, ap);
397 vfprintf(stderr, error, ap);
398 fprintf(stderr, "\n");
404 gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
406 struct gctl_req_arg *argp;
411 vsnprintf(param, sizeof(param), pfmt, ap);
412 for (i = 0; i < req->narg; i++) {
414 if (strcmp(param, argp->name))
416 if (!(argp->flag & GCTL_PARAM_RD))
420 /* We are looking for a string. */
422 fprintf(stderr, "No length argument (%s).\n",
426 if (((char *)p)[argp->len - 1] != '\0') {
427 fprintf(stderr, "Unterminated argument (%s).\n",
431 } else if ((int)len != argp->len) {
432 fprintf(stderr, "Wrong length %s argument.\n", param);
437 fprintf(stderr, "No such argument (%s).\n", param);
442 gctl_get_int(struct gctl_req *req, const char *pfmt, ...)
448 p = gctl_get_param(req, sizeof(int), pfmt, ap);
454 gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
460 p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
466 gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
472 p = gctl_get_param(req, 0, pfmt, ap);
478 gctl_change_param(struct gctl_req *req, const char *name, int len,
481 struct gctl_req_arg *ap;
484 if (req == NULL || req->error != NULL)
486 for (i = 0; i < req->narg; i++) {
488 if (strcmp(ap->name, name) != 0)
490 ap->value = __DECONST(void *, value);
492 ap->flag &= ~GCTL_PARAM_ASCII;
494 } else if (len < 0) {
495 ap->flag |= GCTL_PARAM_ASCII;
496 ap->len = strlen(value) + 1;
504 gctl_delete_param(struct gctl_req *req, const char *name)
506 struct gctl_req_arg *ap;
509 if (req == NULL || req->error != NULL)
513 while (i < req->narg) {
515 if (strcmp(ap->name, name) == 0)
524 while (i < req->narg) {
525 req->arg[i] = req->arg[i + 1];
532 gctl_has_param(struct gctl_req *req, const char *name)
534 struct gctl_req_arg *ap;
537 if (req == NULL || req->error != NULL)
540 for (i = 0; i < req->narg; i++) {
542 if (strcmp(ap->name, name) == 0)