1129470Spjd/*-
2213074Spjd * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3129470Spjd * All rights reserved.
4129470Spjd *
5129470Spjd * Redistribution and use in source and binary forms, with or without
6129470Spjd * modification, are permitted provided that the following conditions
7129470Spjd * are met:
8129470Spjd * 1. Redistributions of source code must retain the above copyright
9129470Spjd *    notice, this list of conditions and the following disclaimer.
10129470Spjd * 2. Redistributions in binary form must reproduce the above copyright
11129470Spjd *    notice, this list of conditions and the following disclaimer in the
12129470Spjd *    documentation and/or other materials provided with the distribution.
13155175Spjd *
14129470Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15129470Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16129470Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17129470Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18129470Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19129470Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20129470Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21129470Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22129470Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23129470Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24129470Spjd * SUCH DAMAGE.
25129470Spjd */
26129470Spjd
27129470Spjd#include <sys/cdefs.h>
28129470Spjd__FBSDID("$FreeBSD: stable/10/sbin/geom/misc/subr.c 330737 2018-03-10 04:17:01Z asomers $");
29129470Spjd
30129470Spjd#include <sys/param.h>
31129470Spjd#include <sys/disk.h>
32129470Spjd#include <sys/endian.h>
33129470Spjd#include <sys/uio.h>
34129470Spjd#include <errno.h>
35129470Spjd#include <fcntl.h>
36129470Spjd#include <paths.h>
37129470Spjd#include <stdio.h>
38129470Spjd#include <stdlib.h>
39209388Sae#include <limits.h>
40209388Sae#include <inttypes.h>
41129470Spjd#include <stdarg.h>
42129470Spjd#include <string.h>
43129470Spjd#include <strings.h>
44129470Spjd#include <unistd.h>
45129470Spjd#include <assert.h>
46129470Spjd#include <libgeom.h>
47129470Spjd
48129470Spjd#include "misc/subr.h"
49129470Spjd
50129470Spjd
51129470Spjdstruct std_metadata {
52129470Spjd	char		md_magic[16];
53129470Spjd	uint32_t	md_version;
54129470Spjd};
55129470Spjd
56129470Spjdstatic void
57213074Spjdstd_metadata_decode(const unsigned char *data, struct std_metadata *md)
58129470Spjd{
59129470Spjd
60129470Spjd        bcopy(data, md->md_magic, sizeof(md->md_magic));
61129470Spjd        md->md_version = le32dec(data + 16);
62129470Spjd}
63129470Spjd
64130591Spjd/*
65130591Spjd * Greatest Common Divisor.
66130591Spjd */
67213074Spjdstatic unsigned int
68213074Spjdgcd(unsigned int a, unsigned int b)
69130591Spjd{
70213074Spjd	unsigned int c;
71130591Spjd
72130591Spjd	while (b != 0) {
73130591Spjd		c = a;
74130591Spjd		a = b;
75130591Spjd		b = (c % b);
76130591Spjd	}
77130591Spjd	return (a);
78130591Spjd}
79130591Spjd
80130591Spjd/*
81130591Spjd * Least Common Multiple.
82130591Spjd */
83213074Spjdunsigned int
84213074Spjdg_lcm(unsigned int a, unsigned int b)
85130591Spjd{
86130591Spjd
87130591Spjd	return ((a * b) / gcd(a, b));
88130591Spjd}
89130591Spjd
90149302Spjduint32_t
91149302Spjdbitcount32(uint32_t x)
92149302Spjd{
93149302Spjd
94149302Spjd	x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
95149302Spjd	x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
96149302Spjd	x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
97149302Spjd	x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
98149302Spjd	x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
99149302Spjd	return (x);
100149302Spjd}
101149302Spjd
102209388Sae/*
103209388Sae * The size of a sector is context specific (i.e. determined by the
104209388Sae * media). But when users enter a value with a SI unit, they really
105209388Sae * mean the byte-size or byte-offset and not the size or offset in
106209388Sae * sectors. We should map the byte-oriented value into a sector-oriented
107209388Sae * value when we already know the sector size in bytes. At this time
108209388Sae * we can use g_parse_lba() function. It converts user specified
109209388Sae * value into sectors with following conditions:
110209388Sae * o  Sectors size taken as argument from caller.
111209388Sae * o  When no SI unit is specified the value is in sectors.
112209388Sae * o  With an SI unit the value is in bytes.
113209388Sae * o  The 'b' suffix forces byte interpretation and the 's'
114209388Sae *    suffix forces sector interpretation.
115209388Sae *
116209388Sae * Thus:
117209388Sae * o  2 and 2s mean 2 sectors, and 2b means 2 bytes.
118209388Sae * o  4k and 4kb mean 4096 bytes, and 4ks means 4096 sectors.
119209388Sae *
120209388Sae */
121209388Saeint
122213074Spjdg_parse_lba(const char *lbastr, unsigned int sectorsize, off_t *sectors)
123209388Sae{
124209388Sae	off_t number, mult, unit;
125209388Sae	char *s;
126209388Sae
127209388Sae	assert(lbastr != NULL);
128209388Sae	assert(sectorsize > 0);
129209388Sae	assert(sectors != NULL);
130209388Sae
131209388Sae	number = (off_t)strtoimax(lbastr, &s, 0);
132209392Sae	if (s == lbastr || number < 0)
133209388Sae		return (EINVAL);
134209388Sae
135209388Sae	mult = 1;
136209388Sae	unit = sectorsize;
137209388Sae	if (*s == '\0')
138209388Sae		goto done;
139209388Sae	switch (*s) {
140209388Sae	case 'e': case 'E':
141209388Sae		mult *= 1024;
142209388Sae		/* FALLTHROUGH */
143209388Sae	case 'p': case 'P':
144209388Sae		mult *= 1024;
145209388Sae		/* FALLTHROUGH */
146209388Sae	case 't': case 'T':
147209388Sae		mult *= 1024;
148209388Sae		/* FALLTHROUGH */
149209388Sae	case 'g': case 'G':
150209388Sae		mult *= 1024;
151209388Sae		/* FALLTHROUGH */
152209388Sae	case 'm': case 'M':
153209388Sae		mult *= 1024;
154209388Sae		/* FALLTHROUGH */
155209388Sae	case 'k': case 'K':
156209388Sae		mult *= 1024;
157209388Sae		break;
158209388Sae	default:
159209388Sae		goto sfx;
160209388Sae	}
161209388Sae	unit = 1;	/* bytes */
162209388Sae	s++;
163209388Sae	if (*s == '\0')
164209388Sae		goto done;
165209388Saesfx:
166209388Sae	switch (*s) {
167209388Sae	case 's': case 'S':
168209388Sae		unit = sectorsize;	/* sector */
169209388Sae		break;
170209388Sae	case 'b': case 'B':
171209388Sae		unit = 1;		/* bytes */
172209388Sae		break;
173209388Sae	default:
174209388Sae		return (EINVAL);
175209388Sae	}
176209388Sae	s++;
177209388Sae	if (*s != '\0')
178209388Sae		return (EINVAL);
179209388Saedone:
180209392Sae	if ((OFF_MAX / unit) < mult || (OFF_MAX / mult / unit) < number)
181209388Sae		return (ERANGE);
182209388Sae	number *= mult * unit;
183209388Sae	if (number % sectorsize)
184209388Sae		return (EINVAL);
185209388Sae	number /= sectorsize;
186209388Sae	*sectors = number;
187209388Sae	return (0);
188209388Sae}
189209388Sae
190130591Spjdoff_t
191130591Spjdg_get_mediasize(const char *name)
192130591Spjd{
193130591Spjd	off_t mediasize;
194130591Spjd	int fd;
195130591Spjd
196213074Spjd	fd = g_open(name, 0);
197130591Spjd	if (fd == -1)
198130591Spjd		return (0);
199213074Spjd	mediasize = g_mediasize(fd);
200213074Spjd	if (mediasize == -1)
201213074Spjd		mediasize = 0;
202213074Spjd	(void)g_close(fd);
203130591Spjd	return (mediasize);
204130591Spjd}
205130591Spjd
206213074Spjdunsigned int
207130591Spjdg_get_sectorsize(const char *name)
208130591Spjd{
209213074Spjd	ssize_t sectorsize;
210130591Spjd	int fd;
211130591Spjd
212213074Spjd	fd = g_open(name, 0);
213130591Spjd	if (fd == -1)
214130591Spjd		return (0);
215213074Spjd	sectorsize = g_sectorsize(fd);
216213074Spjd	if (sectorsize == -1)
217213074Spjd		sectorsize = 0;
218213074Spjd	(void)g_close(fd);
219213074Spjd	return ((unsigned int)sectorsize);
220130591Spjd}
221130591Spjd
222129470Spjdint
223213074Spjdg_metadata_read(const char *name, unsigned char *md, size_t size,
224213074Spjd    const char *magic)
225131603Spjd{
226131603Spjd	struct std_metadata stdmd;
227213074Spjd	unsigned char *sector;
228213074Spjd	ssize_t sectorsize;
229131603Spjd	off_t mediasize;
230131603Spjd	int error, fd;
231131603Spjd
232131603Spjd	sector = NULL;
233131603Spjd	error = 0;
234131603Spjd
235213074Spjd	fd = g_open(name, 0);
236131603Spjd	if (fd == -1)
237131603Spjd		return (errno);
238213074Spjd	mediasize = g_mediasize(fd);
239213074Spjd	if (mediasize == -1) {
240131603Spjd		error = errno;
241131603Spjd		goto out;
242131603Spjd	}
243213074Spjd	sectorsize = g_sectorsize(fd);
244213074Spjd	if (sectorsize == -1) {
245131603Spjd		error = errno;
246131603Spjd		goto out;
247131603Spjd	}
248213074Spjd	assert(sectorsize >= (ssize_t)size);
249131603Spjd	sector = malloc(sectorsize);
250131603Spjd	if (sector == NULL) {
251131603Spjd		error = ENOMEM;
252131603Spjd		goto out;
253131603Spjd	}
254131603Spjd	if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
255213074Spjd	    sectorsize) {
256131603Spjd		error = errno;
257131603Spjd		goto out;
258131603Spjd	}
259131603Spjd	if (magic != NULL) {
260131603Spjd		std_metadata_decode(sector, &stdmd);
261131603Spjd		if (strcmp(stdmd.md_magic, magic) != 0) {
262131603Spjd			error = EINVAL;
263131603Spjd			goto out;
264131603Spjd		}
265131603Spjd	}
266131603Spjd	bcopy(sector, md, size);
267131603Spjdout:
268131603Spjd	if (sector != NULL)
269131603Spjd		free(sector);
270213074Spjd	g_close(fd);
271131603Spjd	return (error);
272131603Spjd}
273131603Spjd
274330737Sasomers/*
275330737Sasomers * Actually write the GEOM label to the provider
276330737Sasomers *
277330737Sasomers * @param name	GEOM provider's name (ie "ada0")
278330737Sasomers * @param md	Pointer to the label data to write
279330737Sasomers * @param size	Size of the data pointed to by md
280330737Sasomers */
281131603Spjdint
282213074Spjdg_metadata_store(const char *name, const unsigned char *md, size_t size)
283129470Spjd{
284213074Spjd	unsigned char *sector;
285213074Spjd	ssize_t sectorsize;
286129470Spjd	off_t mediasize;
287129470Spjd	int error, fd;
288129470Spjd
289129470Spjd	sector = NULL;
290129470Spjd	error = 0;
291129470Spjd
292213074Spjd	fd = g_open(name, 1);
293129470Spjd	if (fd == -1)
294129470Spjd		return (errno);
295213074Spjd	mediasize = g_mediasize(fd);
296213074Spjd	if (mediasize == -1) {
297129470Spjd		error = errno;
298129470Spjd		goto out;
299129470Spjd	}
300213074Spjd	sectorsize = g_sectorsize(fd);
301213074Spjd	if (sectorsize == -1) {
302129470Spjd		error = errno;
303129470Spjd		goto out;
304129470Spjd	}
305213074Spjd	assert(sectorsize >= (ssize_t)size);
306129470Spjd	sector = malloc(sectorsize);
307129470Spjd	if (sector == NULL) {
308129470Spjd		error = ENOMEM;
309129470Spjd		goto out;
310129470Spjd	}
311129470Spjd	bcopy(md, sector, size);
312330737Sasomers	bzero(sector + size, sectorsize - size);
313129470Spjd	if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
314213074Spjd	    sectorsize) {
315129470Spjd		error = errno;
316129470Spjd		goto out;
317129470Spjd	}
318213074Spjd	(void)g_flush(fd);
319129470Spjdout:
320129470Spjd	if (sector != NULL)
321129470Spjd		free(sector);
322213074Spjd	(void)g_close(fd);
323129470Spjd	return (error);
324129470Spjd}
325129470Spjd
326129470Spjdint
327129470Spjdg_metadata_clear(const char *name, const char *magic)
328129470Spjd{
329129470Spjd	struct std_metadata md;
330213074Spjd	unsigned char *sector;
331213074Spjd	ssize_t sectorsize;
332129470Spjd	off_t mediasize;
333129470Spjd	int error, fd;
334129470Spjd
335129470Spjd	sector = NULL;
336129470Spjd	error = 0;
337129470Spjd
338213074Spjd	fd = g_open(name, 1);
339129470Spjd	if (fd == -1)
340129470Spjd		return (errno);
341213074Spjd	mediasize = g_mediasize(fd);
342130591Spjd	if (mediasize == 0) {
343129470Spjd		error = errno;
344129470Spjd		goto out;
345129470Spjd	}
346213074Spjd	sectorsize = g_sectorsize(fd);
347319258Sasomers	if (sectorsize <= 0) {
348129470Spjd		error = errno;
349129470Spjd		goto out;
350129470Spjd	}
351129470Spjd	sector = malloc(sectorsize);
352129470Spjd	if (sector == NULL) {
353129470Spjd		error = ENOMEM;
354129470Spjd		goto out;
355129470Spjd	}
356129470Spjd	if (magic != NULL) {
357129470Spjd		if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
358213074Spjd		    sectorsize) {
359129470Spjd			error = errno;
360129470Spjd			goto out;
361129470Spjd		}
362129470Spjd		std_metadata_decode(sector, &md);
363129470Spjd		if (strcmp(md.md_magic, magic) != 0) {
364129470Spjd			error = EINVAL;
365129470Spjd			goto out;
366129470Spjd		}
367129470Spjd	}
368129470Spjd	bzero(sector, sectorsize);
369129470Spjd	if (pwrite(fd, sector, sectorsize, mediasize - sectorsize) !=
370213074Spjd	    sectorsize) {
371129470Spjd		error = errno;
372129470Spjd		goto out;
373129470Spjd	}
374213074Spjd	(void)g_flush(fd);
375129470Spjdout:
376319258Sasomers	free(sector);
377213074Spjd	g_close(fd);
378129470Spjd	return (error);
379129470Spjd}
380129470Spjd
381129470Spjd/*
382129470Spjd * Set an error message, if one does not already exist.
383129470Spjd */
384129470Spjdvoid
385129470Spjdgctl_error(struct gctl_req *req, const char *error, ...)
386129470Spjd{
387129470Spjd	va_list ap;
388129470Spjd
389226718Spjd	if (req != NULL && req->error != NULL)
390129470Spjd		return;
391129470Spjd	va_start(ap, error);
392226718Spjd	if (req != NULL) {
393226718Spjd		vasprintf(&req->error, error, ap);
394226718Spjd	} else {
395226718Spjd		vfprintf(stderr, error, ap);
396226718Spjd		fprintf(stderr, "\n");
397226718Spjd	}
398129470Spjd	va_end(ap);
399129470Spjd}
400129470Spjd
401153190Spjdstatic void *
402153190Spjdgctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
403129470Spjd{
404153190Spjd	struct gctl_req_arg *argp;
405153190Spjd	char param[256];
406213074Spjd	unsigned int i;
407153190Spjd	void *p;
408129470Spjd
409153190Spjd	vsnprintf(param, sizeof(param), pfmt, ap);
410129470Spjd	for (i = 0; i < req->narg; i++) {
411153190Spjd		argp = &req->arg[i];
412153190Spjd		if (strcmp(param, argp->name))
413129470Spjd			continue;
414153190Spjd		if (!(argp->flag & GCTL_PARAM_RD))
415129470Spjd			continue;
416153190Spjd		p = argp->value;
417153190Spjd		if (len == 0) {
418153190Spjd			/* We are looking for a string. */
419153190Spjd			if (argp->len < 1) {
420153190Spjd				fprintf(stderr, "No length argument (%s).\n",
421153190Spjd				    param);
422153190Spjd				abort();
423153190Spjd			}
424153190Spjd			if (((char *)p)[argp->len - 1] != '\0') {
425153190Spjd				fprintf(stderr, "Unterminated argument (%s).\n",
426153190Spjd				    param);
427153190Spjd				abort();
428153190Spjd			}
429153190Spjd		} else if ((int)len != argp->len) {
430153190Spjd			fprintf(stderr, "Wrong length %s argument.\n", param);
431153190Spjd			abort();
432153190Spjd		}
433129470Spjd		return (p);
434129470Spjd	}
435153190Spjd	fprintf(stderr, "No such argument (%s).\n", param);
436153190Spjd	abort();
437129470Spjd}
438129470Spjd
439153190Spjdint
440153190Spjdgctl_get_int(struct gctl_req *req, const char *pfmt, ...)
441129470Spjd{
442153190Spjd	int *p;
443153190Spjd	va_list ap;
444129470Spjd
445153190Spjd	va_start(ap, pfmt);
446153190Spjd	p = gctl_get_param(req, sizeof(int), pfmt, ap);
447153190Spjd	va_end(ap);
448153190Spjd	return (*p);
449129470Spjd}
450129470Spjd
451153190Spjdintmax_t
452153190Spjdgctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
453129470Spjd{
454153190Spjd	intmax_t *p;
455153190Spjd	va_list ap;
456129470Spjd
457153190Spjd	va_start(ap, pfmt);
458153190Spjd	p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
459153190Spjd	va_end(ap);
460153190Spjd	return (*p);
461153190Spjd}
462153190Spjd
463153190Spjdconst char *
464153190Spjdgctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
465153190Spjd{
466153190Spjd	const char *p;
467153190Spjd	va_list ap;
468153190Spjd
469153190Spjd	va_start(ap, pfmt);
470153190Spjd	p = gctl_get_param(req, 0, pfmt, ap);
471153190Spjd	va_end(ap);
472129470Spjd	return (p);
473129470Spjd}
474166215Spjd
475166215Spjdint
476166215Spjdgctl_change_param(struct gctl_req *req, const char *name, int len,
477166215Spjd    const void *value)
478166215Spjd{
479166215Spjd	struct gctl_req_arg *ap;
480213074Spjd	unsigned int i;
481166215Spjd
482166215Spjd	if (req == NULL || req->error != NULL)
483166215Spjd		return (EDOOFUS);
484166215Spjd	for (i = 0; i < req->narg; i++) {
485166215Spjd		ap = &req->arg[i];
486166215Spjd		if (strcmp(ap->name, name) != 0)
487166215Spjd			continue;
488166215Spjd		ap->value = __DECONST(void *, value);
489166215Spjd		if (len >= 0) {
490166215Spjd			ap->flag &= ~GCTL_PARAM_ASCII;
491166215Spjd			ap->len = len;
492166215Spjd		} else if (len < 0) {
493166215Spjd			ap->flag |= GCTL_PARAM_ASCII;
494166215Spjd			ap->len = strlen(value) + 1;
495166215Spjd		}
496166215Spjd		return (0);
497166215Spjd	}
498166215Spjd	return (ENOENT);
499166215Spjd}
500179628Smarcel
501179628Smarcelint
502179628Smarcelgctl_delete_param(struct gctl_req *req, const char *name)
503179628Smarcel{
504179628Smarcel	struct gctl_req_arg *ap;
505179628Smarcel	unsigned int i;
506179628Smarcel
507179628Smarcel	if (req == NULL || req->error != NULL)
508179628Smarcel		return (EDOOFUS);
509179628Smarcel
510179628Smarcel	i = 0;
511179628Smarcel	while (i < req->narg) {
512179628Smarcel		ap = &req->arg[i];
513179628Smarcel		if (strcmp(ap->name, name) == 0)
514179628Smarcel			break;
515179628Smarcel		i++;
516179628Smarcel	}
517179628Smarcel	if (i == req->narg)
518179628Smarcel		return (ENOENT);
519179628Smarcel
520208886Sae	free(ap->name);
521179628Smarcel	req->narg--;
522179628Smarcel	while (i < req->narg) {
523179628Smarcel		req->arg[i] = req->arg[i + 1];
524179628Smarcel		i++;
525179628Smarcel	}
526179628Smarcel	return (0);
527179628Smarcel}
528179628Smarcel
529179628Smarcelint
530179628Smarcelgctl_has_param(struct gctl_req *req, const char *name)
531179628Smarcel{
532179628Smarcel	struct gctl_req_arg *ap;
533179628Smarcel	unsigned int i;
534179628Smarcel
535179628Smarcel	if (req == NULL || req->error != NULL)
536179628Smarcel		return (0);
537179628Smarcel
538179628Smarcel	for (i = 0; i < req->narg; i++) {
539179628Smarcel		ap = &req->arg[i];
540179628Smarcel		if (strcmp(ap->name, name) == 0)
541179628Smarcel			return (1);
542179628Smarcel	}
543179628Smarcel	return (0);
544179628Smarcel}
545