- The geom(8) utility only uses three types of arguments: string (char *),

value (intmax_t) and boolean (int).
  Based on that provide three functions:
        - gctl_get_ascii()
        - gctl_get_int()
        - gctl_get_intmax()
- Hide gctl_get_param() function, as it is only used internally in
  subr.c.
- Allow to provide argument name as (fmt, ...).
- Assert geom(8) bugs (missing argument is a geom(8) bug).

- Clean-up and simplify the code by using new functions and assumtions
  (no more checking for missing argument).

Tested by:	regression tests
This commit is contained in:
Pawel Jakub Dawidek 2005-12-07 01:38:27 +00:00
parent 5612eab744
commit f13942a746
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=153190
10 changed files with 329 additions and 731 deletions

View File

@ -94,7 +94,7 @@ concat_main(struct gctl_req *req, unsigned flags)
if ((flags & G_FLAG_VERBOSE) != 0)
verbose = 1;
name = gctl_get_asciiparam(req, "verb");
name = gctl_get_ascii(req, "verb");
if (name == NULL) {
gctl_error(req, "No '%s' argument.", "verb");
return;
@ -115,32 +115,20 @@ concat_label(struct gctl_req *req)
struct g_concat_metadata md;
u_char sector[512];
const char *name;
char param[16];
unsigned i;
int *hardcode, *nargs, error;
int error, i, hardcode, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs <= 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs <= 2) {
gctl_error(req, "Too few arguments.");
return;
}
hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
if (hardcode == NULL) {
gctl_error(req, "No '%s' argument.", "hardcode");
return;
}
hardcode = gctl_get_int(req, "hardcode");
/*
* Clear last sector first to spoil all components if device exists.
*/
for (i = 1; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, NULL);
if (error != 0) {
gctl_error(req, "Can't store metadata on %s: %s.", name,
@ -151,24 +139,18 @@ concat_label(struct gctl_req *req)
strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
md.md_version = G_CONCAT_VERSION;
name = gctl_get_asciiparam(req, "arg0");
if (name == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
name = gctl_get_ascii(req, "arg0");
strlcpy(md.md_name, name, sizeof(md.md_name));
md.md_id = arc4random();
md.md_all = *nargs - 1;
md.md_all = nargs - 1;
/*
* Ok, store metadata.
*/
for (i = 1; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
md.md_no = i - 1;
if (!*hardcode)
if (!hardcode)
bzero(md.md_provider, sizeof(md.md_provider));
else {
if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
@ -199,24 +181,16 @@ static void
concat_clear(struct gctl_req *req)
{
const char *name;
char param[16];
unsigned i;
int *nargs, error;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, G_CONCAT_MAGIC);
if (error != 0) {
fprintf(stderr, "Can't clear metadata on %s: %s.\n",
@ -247,23 +221,16 @@ concat_dump(struct gctl_req *req)
{
struct g_concat_metadata md, tmpmd;
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
G_CONCAT_MAGIC);
if (error != 0) {

View File

@ -228,7 +228,7 @@ eli_main(struct gctl_req *req, unsigned flags)
if ((flags & G_FLAG_VERBOSE) != 0)
verbose = 1;
name = gctl_get_asciiparam(req, "verb");
name = gctl_get_ascii(req, "verb");
if (name == NULL) {
gctl_error(req, "No '%s' argument.", "verb");
return;
@ -295,25 +295,14 @@ eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key,
{
struct hmac_ctx ctx;
const char *str;
int *nopassphrase;
int error;
int error, nopassphrase;
nopassphrase = gctl_get_paraml(req,
new ? "nonewpassphrase" : "nopassphrase", sizeof(*nopassphrase));
if (nopassphrase == NULL) {
gctl_error(req, "No '%s' argument.",
new ? "nonewpassphrase" : "nopassphrase");
return (NULL);
}
nopassphrase =
gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
g_eli_crypto_hmac_init(&ctx, NULL, 0);
str = gctl_get_asciiparam(req, new ? "newkeyfile" : "keyfile");
if (str == NULL) {
gctl_error(req, "No '%s' argument.",
new ? "newkeyfile" : "keyfile");
return (NULL);
}
str = gctl_get_ascii(req, new ? "newkeyfile" : "keyfile");
if (str[0] != '\0') {
char buf[MAXPHYS];
ssize_t done;
@ -342,7 +331,7 @@ eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key,
}
}
if (!*nopassphrase) {
if (!nopassphrase) {
char buf1[BUFSIZ], buf2[BUFSIZ], *p;
if (!new && md->md_iterations == -1) {
@ -506,26 +495,17 @@ eli_init(struct gctl_req *req)
unsigned char sector[sizeof(struct g_eli_metadata)];
unsigned char key[G_ELI_USERKEYLEN];
const char *str, *prov;
int *nargs, *boot;
unsigned secsize;
off_t mediasize;
intmax_t *valp;
int error;
intmax_t val;
int error, nargs, boot;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs != 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs != 1) {
gctl_error(req, "Too few arguments.");
return;
}
prov = gctl_get_asciiparam(req, "arg0");
if (prov == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
prov = gctl_get_ascii(req, "arg0");
mediasize = g_get_mediasize(prov);
secsize = g_get_sectorsize(prov);
if (mediasize == 0 || secsize == 0) {
@ -538,56 +518,35 @@ eli_init(struct gctl_req *req)
strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic));
md.md_version = G_ELI_VERSION;
md.md_flags = 0;
boot = gctl_get_paraml(req, "boot", sizeof(*boot));
if (boot == NULL) {
gctl_error(req, "No '%s' argument.", "boot");
return;
}
if (*boot) {
int *nonewpassphrase;
boot = gctl_get_int(req, "boot");
if (boot) {
int nonewpassphrase;
/* Part of key cannot be read on boot from a file. */
str = gctl_get_asciiparam(req, "newkeyfile");
if (str == NULL) {
gctl_error(req, "No '%s' argument.", "newkeyfile");
return;
}
str = gctl_get_ascii(req, "newkeyfile");
if (str[0] != '\0') {
gctl_error(req,
"Options -b and -K are mutually exclusive.");
return;
}
/* Key has to be given as a passphrase on boot. */
nonewpassphrase = gctl_get_paraml(req, "nonewpassphrase",
sizeof(*nonewpassphrase));
if (nonewpassphrase == NULL) {
gctl_error(req, "No '%s' argument.", "nonewpassphrase");
return;
}
if (*nonewpassphrase) {
nonewpassphrase = gctl_get_int(req, "nonewpassphrase");
if (nonewpassphrase) {
gctl_error(req,
"Options -b and -P are mutually exclusive.");
return;
}
md.md_flags |= G_ELI_FLAG_BOOT;
}
str = gctl_get_asciiparam(req, "algo");
if (str == NULL) {
gctl_error(req, "No '%s' argument.", "algo");
return;
}
str = gctl_get_ascii(req, "algo");
md.md_algo = g_eli_str2algo(str);
if (md.md_algo < CRYPTO_ALGORITHM_MIN ||
md.md_algo > CRYPTO_ALGORITHM_MAX) {
gctl_error(req, "Invalid encryption algorithm.");
return;
}
valp = gctl_get_paraml(req, "keylen", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "keylen");
return;
}
md.md_keylen = *valp;
val = gctl_get_intmax(req, "keylen");
md.md_keylen = val;
md.md_keylen = g_eli_keylen(md.md_algo, md.md_keylen);
if (md.md_keylen == 0) {
gctl_error(req, "Invalid key length.");
@ -595,26 +554,18 @@ eli_init(struct gctl_req *req)
}
md.md_provsize = mediasize;
valp = gctl_get_paraml(req, "iterations", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "iterations");
return;
}
md.md_iterations = *valp;
val = gctl_get_intmax(req, "iterations");
md.md_iterations = val;
valp = gctl_get_paraml(req, "sectorsize", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "sectorsize");
return;
}
if (*valp == 0)
val = gctl_get_intmax(req, "sectorsize");
if (val == 0)
md.md_sectorsize = secsize;
else {
if (*valp < 0 || (*valp % secsize) != 0) {
if (val < 0 || (val % secsize) != 0) {
gctl_error(req, "Invalid sector size.");
return;
}
md.md_sectorsize = *valp;
md.md_sectorsize = val;
}
md.md_keys = 0x01;
@ -657,22 +608,14 @@ eli_attach(struct gctl_req *req)
struct g_eli_metadata md;
unsigned char key[G_ELI_USERKEYLEN];
const char *prov;
int *nargs;
int nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs != 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs != 1) {
gctl_error(req, "Too few arguments.");
return;
}
prov = gctl_get_asciiparam(req, "arg0");
if (prov == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
prov = gctl_get_ascii(req, "arg0");
if (eli_metadata_read(req, prov, &md) == -1)
return;
@ -695,16 +638,12 @@ eli_setkey_attached(struct gctl_req *req, const char *prov,
struct g_eli_metadata *md)
{
unsigned char key[G_ELI_USERKEYLEN];
intmax_t *valp;
intmax_t val;
valp = gctl_get_paraml(req, "iterations", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "iterations");
return;
}
val = gctl_get_intmax(req, "iterations");
/* Check if iterations number should be changed. */
if (*valp != -1)
md->md_iterations = *valp;
if (val != -1)
md->md_iterations = val;
/* Generate key for Master Key encryption. */
if (eli_genkey(req, md, key, 1) == NULL) {
@ -723,7 +662,7 @@ eli_setkey_detached(struct gctl_req *req, const char *prov,
{
unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
unsigned char *mkeydst;
intmax_t *valp;
intmax_t val;
unsigned nkey;
int error;
@ -754,13 +693,9 @@ eli_setkey_detached(struct gctl_req *req, const char *prov,
if (verbose)
printf("Decrypted Master Key %u.\n", nkey);
valp = gctl_get_paraml(req, "keyno", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "keyno");
return;
}
if (*valp != -1)
nkey = *valp;
val = gctl_get_intmax(req, "keyno");
if (val != -1)
nkey = val;
#if 0
else
; /* Use the key number which was found during decryption. */
@ -770,13 +705,9 @@ eli_setkey_detached(struct gctl_req *req, const char *prov,
return;
}
valp = gctl_get_paraml(req, "iterations", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "iterations");
return;
}
val = gctl_get_intmax(req, "iterations");
/* Check if iterations number should and can be changed. */
if (*valp != -1) {
if (val != -1) {
if (bitcount32(md->md_keys) != 1) {
gctl_error(req, "To be able to use '-i' option, only "
"one key can be defined.");
@ -787,7 +718,7 @@ eli_setkey_detached(struct gctl_req *req, const char *prov,
"changed when '-i' option is used.");
return;
}
md->md_iterations = *valp;
md->md_iterations = val;
}
mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN;
@ -823,22 +754,14 @@ eli_setkey(struct gctl_req *req)
{
struct g_eli_metadata md;
const char *prov;
int *nargs;
int nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs != 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs != 1) {
gctl_error(req, "Too few arguments.");
return;
}
prov = gctl_get_asciiparam(req, "arg0");
if (prov == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
prov = gctl_get_ascii(req, "arg0");
if (eli_metadata_read(req, prov, &md) == -1)
return;
@ -861,48 +784,34 @@ eli_delkey_detached(struct gctl_req *req, const char *prov)
{
struct g_eli_metadata md;
unsigned char *mkeydst;
intmax_t *valp;
intmax_t val;
unsigned nkey;
int *all, *force;
int all, force;
if (eli_metadata_read(req, prov, &md) == -1)
return;
all = gctl_get_paraml(req, "all", sizeof(*all));
if (all == NULL) {
gctl_error(req, "No '%s' argument.", "all");
return;
}
if (*all)
all = gctl_get_int(req, "all");
if (all)
arc4rand(md.md_mkeys, sizeof(md.md_mkeys));
else {
force = gctl_get_paraml(req, "force", sizeof(*force));
if (force == NULL) {
gctl_error(req, "No '%s' argument.", "force");
return;
}
valp = gctl_get_paraml(req, "keyno", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "keyno");
return;
}
if (*valp == -1) {
force = gctl_get_int(req, "force");
val = gctl_get_intmax(req, "keyno");
if (val == -1) {
gctl_error(req, "Key number has to be specified.");
return;
}
nkey = *valp;
nkey = val;
if (nkey >= G_ELI_MAXMKEYS) {
gctl_error(req, "Invalid '%s' argument.", "keyno");
return;
}
if (!(md.md_keys & (1 << nkey)) && !*force) {
if (!(md.md_keys & (1 << nkey)) && !force) {
gctl_error(req, "Master Key %u is not set.", nkey);
return;
}
md.md_keys &= ~(1 << nkey);
if (md.md_keys == 0 && !*force) {
if (md.md_keys == 0 && !force) {
gctl_error(req, "This is the last Master Key. Use '-f' "
"option if you really want to remove it.");
return;
@ -919,22 +828,14 @@ static void
eli_delkey(struct gctl_req *req)
{
const char *prov;
int *nargs;
int nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs != 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs != 1) {
gctl_error(req, "Too few arguments.");
return;
}
prov = gctl_get_asciiparam(req, "arg0");
if (prov == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
prov = gctl_get_ascii(req, "arg0");
if (eli_is_attached(prov))
eli_delkey_attached(req, prov);
@ -975,21 +876,11 @@ static void
eli_kill(struct gctl_req *req)
{
const char *prov;
char param[16];
unsigned i;
int *nargs, *all;
int i, nargs, all;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
all = gctl_get_paraml(req, "all", sizeof(*all));
if (all == NULL) {
gctl_error(req, "No '%s' argument.", "all");
return;
}
if (!*all && *nargs == 0) {
nargs = gctl_get_int(req, "nargs");
all = gctl_get_int(req, "all");
if (!all && nargs == 0) {
gctl_error(req, "Too few arguments.");
return;
}
@ -1011,10 +902,8 @@ eli_kill(struct gctl_req *req)
/*
* Now the rest.
*/
for (i = 0; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
prov = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
prov = gctl_get_ascii(req, "arg%d", i);
if (!eli_is_attached(prov))
eli_kill_detached(req, prov);
}
@ -1028,28 +917,15 @@ eli_backup(struct gctl_req *req)
unsigned secsize;
unsigned char *sector;
off_t mediasize;
int *nargs, filefd, provfd;
int nargs, filefd, provfd;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs != 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs != 2) {
gctl_error(req, "Invalid number of arguments.");
return;
}
prov = gctl_get_asciiparam(req, "arg0");
if (prov == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
file = gctl_get_asciiparam(req, "arg1");
if (file == NULL) {
gctl_error(req, "No 'arg%u' argument.", 1);
return;
}
prov = gctl_get_ascii(req, "arg0");
file = gctl_get_ascii(req, "arg1");
provfd = filefd = -1;
sector = NULL;
@ -1122,28 +998,15 @@ eli_restore(struct gctl_req *req)
unsigned char *sector;
unsigned secsize;
off_t mediasize;
int *nargs, filefd, provfd;
int nargs, filefd, provfd;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs != 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs != 2) {
gctl_error(req, "Invalid number of arguments.");
return;
}
file = gctl_get_asciiparam(req, "arg0");
if (file == NULL) {
gctl_error(req, "No 'arg%u' argument.", 1);
return;
}
prov = gctl_get_asciiparam(req, "arg1");
if (prov == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
file = gctl_get_ascii(req, "arg0");
prov = gctl_get_ascii(req, "arg1");
provfd = filefd = -1;
sector = NULL;
@ -1212,23 +1075,16 @@ static void
eli_clear(struct gctl_req *req)
{
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, G_ELI_MAGIC);
if (error != 0) {
fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
@ -1246,23 +1102,16 @@ eli_dump(struct gctl_req *req)
{
struct g_eli_metadata md, tmpmd;
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(name, (unsigned char *)&tmpmd,
sizeof(tmpmd), G_ELI_MAGIC);
if (error != 0) {

View File

@ -89,7 +89,7 @@ label_main(struct gctl_req *req, unsigned flags)
if ((flags & G_FLAG_VERBOSE) != 0)
verbose = 1;
name = gctl_get_asciiparam(req, "verb");
name = gctl_get_ascii(req, "verb");
if (name == NULL) {
gctl_error(req, "No '%s' argument.", "verb");
return;
@ -110,14 +110,10 @@ label_label(struct gctl_req *req)
struct g_label_metadata md;
const char *name, *label;
u_char sector[512];
int *nargs, error;
int error, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs != 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs != 2) {
gctl_error(req, "Invalid number of arguments.");
return;
}
@ -125,11 +121,7 @@ label_label(struct gctl_req *req)
/*
* Clear last sector first to spoil all components if device exists.
*/
name = gctl_get_asciiparam(req, "arg1");
if (name == NULL) {
gctl_error(req, "No 'arg%u' argument.", 1);
return;
}
name = gctl_get_ascii(req, "arg1");
error = g_metadata_clear(name, NULL);
if (error != 0) {
gctl_error(req, "Can't store metadata on %s: %s.", name,
@ -139,11 +131,7 @@ label_label(struct gctl_req *req)
strlcpy(md.md_magic, G_LABEL_MAGIC, sizeof(md.md_magic));
md.md_version = G_LABEL_VERSION;
label = gctl_get_asciiparam(req, "arg0");
if (label == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
label = gctl_get_ascii(req, "arg0");
strlcpy(md.md_label, label, sizeof(md.md_label));
md.md_provsize = g_get_mediasize(name);
if (md.md_provsize == 0) {
@ -170,24 +158,16 @@ static void
label_clear(struct gctl_req *req)
{
const char *name;
char param[16];
unsigned i;
int *nargs, error;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, G_LABEL_MAGIC);
if (error != 0) {
fprintf(stderr, "Can't clear metadata on %s: %s.\n",
@ -214,23 +194,16 @@ label_dump(struct gctl_req *req)
{
struct g_label_metadata md, tmpmd;
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
G_LABEL_MAGIC);
if (error != 0) {

View File

@ -128,7 +128,7 @@ mirror_main(struct gctl_req *req, unsigned flags)
if ((flags & G_FLAG_VERBOSE) != 0)
verbose = 1;
name = gctl_get_asciiparam(req, "verb");
name = gctl_get_ascii(req, "verb");
if (name == NULL) {
gctl_error(req, "No '%s' argument.", "verb");
return;
@ -151,66 +151,41 @@ mirror_label(struct gctl_req *req)
struct g_mirror_metadata md;
u_char sector[512];
const char *str;
char param[16];
int *hardcode, *nargs, *noautosync, bal, error, i;
unsigned sectorsize;
off_t mediasize;
intmax_t *valp;
intmax_t val;
int error, i, nargs, bal, hardcode, noautosync;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 2) {
gctl_error(req, "Too few arguments.");
return;
}
strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
md.md_version = G_MIRROR_VERSION;
str = gctl_get_asciiparam(req, "arg0");
if (str == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
str = gctl_get_ascii(req, "arg0");
strlcpy(md.md_name, str, sizeof(md.md_name));
md.md_mid = arc4random();
md.md_all = *nargs - 1;
md.md_all = nargs - 1;
md.md_mflags = 0;
md.md_dflags = 0;
md.md_genid = 0;
md.md_syncid = 1;
md.md_sync_offset = 0;
valp = gctl_get_paraml(req, "slice", sizeof(*valp));
if (valp == NULL) {
gctl_error(req, "No '%s' argument.", "slice");
return;
}
md.md_slice = *valp;
str = gctl_get_asciiparam(req, "balance");
if (str == NULL) {
gctl_error(req, "No '%s' argument.", "balance");
return;
}
val = gctl_get_intmax(req, "slice");
md.md_slice = val;
str = gctl_get_ascii(req, "balance");
bal = balance_id(str);
if (bal == -1) {
gctl_error(req, "Invalid balance algorithm.");
return;
}
md.md_balance = bal;
noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
if (noautosync == NULL) {
gctl_error(req, "No '%s' argument.", "noautosync");
return;
}
if (*noautosync)
noautosync = gctl_get_int(req, "noautosync");
if (noautosync)
md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
if (hardcode == NULL) {
gctl_error(req, "No '%s' argument.", "hardcode");
return;
}
hardcode = gctl_get_int(req, "hardcode");
/*
* Calculate sectorsize by finding least common multiple from
@ -218,13 +193,11 @@ mirror_label(struct gctl_req *req)
*/
mediasize = 0;
sectorsize = 0;
for (i = 1; i < *nargs; i++) {
for (i = 1; i < nargs; i++) {
unsigned ssize;
off_t msize;
snprintf(param, sizeof(param), "arg%u", i);
str = gctl_get_asciiparam(req, param);
str = gctl_get_ascii(req, "arg%d", i);
msize = g_get_mediasize(str);
ssize = g_get_sectorsize(str);
if (msize == 0 || ssize == 0) {
@ -246,10 +219,8 @@ mirror_label(struct gctl_req *req)
/*
* Clear last sector first, to spoil all components if device exists.
*/
for (i = 1; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
str = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
str = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(str, NULL);
if (error != 0) {
gctl_error(req, "Can't store metadata on %s: %s.", str,
@ -261,15 +232,13 @@ mirror_label(struct gctl_req *req)
/*
* Ok, store metadata (use disk number as priority).
*/
for (i = 1; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
str = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
str = gctl_get_ascii(req, "arg%d", i);
md.md_did = arc4random();
md.md_priority = i - 1;
md.md_provsize = g_get_mediasize(str);
assert(md.md_provsize != 0);
if (!*hardcode)
if (!hardcode)
bzero(md.md_provider, sizeof(md.md_provider));
else {
if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
@ -293,23 +262,16 @@ static void
mirror_clear(struct gctl_req *req)
{
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, G_MIRROR_MAGIC);
if (error != 0) {
fprintf(stderr, "Can't clear metadata on %s: %s.\n",
@ -327,23 +289,16 @@ mirror_dump(struct gctl_req *req)
{
struct g_mirror_metadata md, tmpmd;
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
G_MIRROR_MAGIC);
if (error != 0) {
@ -369,28 +324,17 @@ mirror_activate(struct gctl_req *req)
{
struct g_mirror_metadata md, tmpmd;
const char *name, *path;
int *nargs, error, i;
char param[16];
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 2) {
gctl_error(req, "Too few arguments.");
return;
}
name = gctl_get_asciiparam(req, "arg0");
if (name == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
for (i = 1; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
path = gctl_get_asciiparam(req, param);
name = gctl_get_ascii(req, "arg0");
for (i = 1; i < nargs; i++) {
path = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(path, (u_char *)&tmpmd, sizeof(tmpmd),
G_MIRROR_MAGIC);
if (error != 0) {

View File

@ -119,7 +119,7 @@ raid3_main(struct gctl_req *req, unsigned flags)
if ((flags & G_FLAG_VERBOSE) != 0)
verbose = 1;
name = gctl_get_asciiparam(req, "verb");
name = gctl_get_ascii(req, "verb");
if (name == NULL) {
gctl_error(req, "No '%s' argument.", "verb");
return;
@ -140,71 +140,45 @@ raid3_label(struct gctl_req *req)
struct g_raid3_metadata md;
u_char sector[512];
const char *str;
char param[16];
int *hardcode, *nargs, *noautosync, *round_robin, *verify;
int error, i;
unsigned sectorsize, ssize;
off_t mediasize, msize;
int error, i, nargs, hardcode, noautosync, round_robin, verify;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 4) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 4) {
gctl_error(req, "Too few arguments.");
return;
}
if (bitcount32(*nargs - 2) != 1) {
if (bitcount32(nargs - 2) != 1) {
gctl_error(req, "Invalid number of components.");
return;
}
strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic));
md.md_version = G_RAID3_VERSION;
str = gctl_get_asciiparam(req, "arg0");
if (str == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
str = gctl_get_ascii(req, "arg0");
strlcpy(md.md_name, str, sizeof(md.md_name));
md.md_id = arc4random();
md.md_all = *nargs - 1;
md.md_all = nargs - 1;
md.md_mflags = 0;
md.md_dflags = 0;
md.md_genid = 0;
md.md_syncid = 1;
md.md_sync_offset = 0;
noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
if (noautosync == NULL) {
gctl_error(req, "No '%s' argument.", "noautosync");
return;
}
if (*noautosync)
noautosync = gctl_get_int(req, "noautosync");
if (noautosync)
md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin));
if (round_robin == NULL) {
gctl_error(req, "No '%s' argument.", "round_robin");
return;
}
if (*round_robin)
round_robin = gctl_get_int(req, "round_robin");
if (round_robin)
md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
verify = gctl_get_paraml(req, "verify", sizeof(*verify));
if (verify == NULL) {
gctl_error(req, "No '%s' argument.", "verify");
return;
}
if (*verify)
verify = gctl_get_int(req, "verify");
if (verify)
md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY;
if (*round_robin && *verify) {
if (round_robin && verify) {
gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w');
return;
}
hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
if (hardcode == NULL) {
gctl_error(req, "No '%s' argument.", "hardcode");
return;
}
hardcode = gctl_get_int(req, "hardcode");
/*
* Calculate sectorsize by finding least common multiple from
@ -212,10 +186,8 @@ raid3_label(struct gctl_req *req)
*/
mediasize = 0;
sectorsize = 0;
for (i = 1; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
str = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
str = gctl_get_ascii(req, "arg%d", i);
msize = g_get_mediasize(str);
ssize = g_get_sectorsize(str);
if (msize == 0 || ssize == 0) {
@ -231,16 +203,14 @@ raid3_label(struct gctl_req *req)
else
sectorsize = g_lcm(sectorsize, ssize);
}
md.md_mediasize = mediasize * (*nargs - 2);
md.md_sectorsize = sectorsize * (*nargs - 2);
md.md_mediasize = mediasize * (nargs - 2);
md.md_sectorsize = sectorsize * (nargs - 2);
/*
* Clear last sector first, to spoil all components if device exists.
*/
for (i = 1; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
str = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
str = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(str, NULL);
if (error != 0) {
gctl_error(req, "Can't store metadata on %s: %s.", str,
@ -252,10 +222,8 @@ raid3_label(struct gctl_req *req)
/*
* Ok, store metadata (use disk number as priority).
*/
for (i = 1; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
str = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
str = gctl_get_ascii(req, "arg%d", i);
msize = g_get_mediasize(str);
ssize = g_get_sectorsize(str);
if (mediasize < msize - ssize) {
@ -266,14 +234,14 @@ raid3_label(struct gctl_req *req)
md.md_no = i - 1;
md.md_provsize = msize;
if (!*hardcode)
if (!hardcode)
bzero(md.md_provider, sizeof(md.md_provider));
else {
if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
str += strlen(_PATH_DEV);
strlcpy(md.md_provider, str, sizeof(md.md_provider));
}
if (*verify && md.md_no == md.md_all - 1) {
if (verify && md.md_no == md.md_all - 1) {
/*
* In "verify" mode, force synchronization of parity
* component on start.
@ -297,23 +265,16 @@ static void
raid3_clear(struct gctl_req *req)
{
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, G_RAID3_MAGIC);
if (error != 0) {
fprintf(stderr, "Can't clear metadata on %s: %s.\n",
@ -331,23 +292,16 @@ raid3_dump(struct gctl_req *req)
{
struct g_raid3_metadata md, tmpmd;
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
G_RAID3_MAGIC);
if (error != 0) {

View File

@ -85,7 +85,7 @@ shsec_main(struct gctl_req *req, unsigned flags)
if ((flags & G_FLAG_VERBOSE) != 0)
verbose = 1;
name = gctl_get_asciiparam(req, "verb");
name = gctl_get_ascii(req, "verb");
if (name == NULL) {
gctl_error(req, "No '%s' argument.", "verb");
return;
@ -106,35 +106,24 @@ shsec_label(struct gctl_req *req)
struct g_shsec_metadata md;
off_t compsize, msize;
u_char sector[512];
unsigned i, ssize, secsize;
unsigned ssize, secsize;
const char *name;
char param[16];
int *hardcode, *nargs, error;
int error, i, nargs, hardcode;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs <= 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs <= 2) {
gctl_error(req, "Too few arguments.");
return;
}
hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
if (hardcode == NULL) {
gctl_error(req, "No '%s' argument.", "hardcode");
return;
}
hardcode = gctl_get_int(req, "hardcode");
/*
* Clear last sector first to spoil all components if device exists.
*/
compsize = 0;
secsize = 0;
for (i = 1; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
msize = g_get_mediasize(name);
ssize = g_get_sectorsize(name);
if (msize == 0 || ssize == 0) {
@ -160,22 +149,16 @@ shsec_label(struct gctl_req *req)
strlcpy(md.md_magic, G_SHSEC_MAGIC, sizeof(md.md_magic));
md.md_version = G_SHSEC_VERSION;
name = gctl_get_asciiparam(req, "arg0");
if (name == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
name = gctl_get_ascii(req, "arg0");
strlcpy(md.md_name, name, sizeof(md.md_name));
md.md_id = arc4random();
md.md_all = *nargs - 1;
md.md_all = nargs - 1;
/*
* Ok, store metadata.
*/
for (i = 1; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
msize = g_get_mediasize(name);
ssize = g_get_sectorsize(name);
if (compsize < msize - ssize) {
@ -186,7 +169,7 @@ shsec_label(struct gctl_req *req)
md.md_no = i - 1;
md.md_provsize = msize;
if (!*hardcode)
if (!hardcode)
bzero(md.md_provider, sizeof(md.md_provider));
else {
if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
@ -210,24 +193,16 @@ static void
shsec_clear(struct gctl_req *req)
{
const char *name;
char param[16];
unsigned i;
int *nargs, error;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, G_SHSEC_MAGIC);
if (error != 0) {
fprintf(stderr, "Can't clear metadata on %s: %s.\n",
@ -258,23 +233,16 @@ shsec_dump(struct gctl_req *req)
{
struct g_shsec_metadata md, tmpmd;
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
G_SHSEC_MAGIC);
if (error != 0) {

View File

@ -46,7 +46,7 @@ __FBSDID("$FreeBSD$");
uint32_t lib_version = G_LIB_VERSION;
uint32_t version = G_STRIPE_VERSION;
static intmax_t stripesize = 4096;
static intmax_t default_stripesize = 4096;
static void stripe_main(struct gctl_req *req, unsigned flags);
static void stripe_clear(struct gctl_req *req);
@ -59,7 +59,7 @@ struct g_command class_commands[] = {
},
{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
{
{ 's', "stripesize", &stripesize, G_TYPE_NUMBER },
{ 's', "stripesize", &default_stripesize, G_TYPE_NUMBER },
G_OPT_SENTINEL
},
"[-hv] [-s stripesize] name prov prov ..."
@ -77,7 +77,7 @@ struct g_command class_commands[] = {
{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main,
{
{ 'h', "hardcode", NULL, G_TYPE_NONE },
{ 's', "stripesize", &stripesize, G_TYPE_NUMBER },
{ 's', "stripesize", &default_stripesize, G_TYPE_NUMBER },
G_OPT_SENTINEL
},
"[-hv] [-s stripesize] name prov prov ..."
@ -102,7 +102,7 @@ stripe_main(struct gctl_req *req, unsigned flags)
if ((flags & G_FLAG_VERBOSE) != 0)
verbose = 1;
name = gctl_get_asciiparam(req, "verb");
name = gctl_get_ascii(req, "verb");
if (name == NULL) {
gctl_error(req, "No '%s' argument.", "verb");
return;
@ -121,38 +121,27 @@ static void
stripe_label(struct gctl_req *req)
{
struct g_stripe_metadata md;
intmax_t *stripesizep;
intmax_t stripesize;
off_t compsize, msize;
u_char sector[512];
unsigned i, ssize, secsize;
unsigned ssize, secsize;
const char *name;
char param[16];
int *hardcode, *nargs, error;
int error, i, nargs, hardcode;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs <= 2) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 3) {
gctl_error(req, "Too few arguments.");
return;
}
hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
if (hardcode == NULL) {
gctl_error(req, "No '%s' argument.", "hardcode");
return;
}
hardcode = gctl_get_int(req, "hardcode");
/*
* Clear last sector first to spoil all components if device exists.
*/
compsize = 0;
secsize = 0;
for (i = 1; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
msize = g_get_mediasize(name);
ssize = g_get_sectorsize(name);
if (msize == 0 || ssize == 0) {
@ -178,33 +167,23 @@ stripe_label(struct gctl_req *req)
strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
md.md_version = G_STRIPE_VERSION;
name = gctl_get_asciiparam(req, "arg0");
if (name == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
name = gctl_get_ascii(req, "arg0");
strlcpy(md.md_name, name, sizeof(md.md_name));
md.md_id = arc4random();
md.md_all = *nargs - 1;
stripesizep = gctl_get_paraml(req, "stripesize", sizeof(*stripesizep));
if (stripesizep == NULL) {
gctl_error(req, "No '%s' argument.", "stripesize");
return;
}
if ((*stripesizep % secsize) != 0) {
md.md_all = nargs - 1;
stripesize = gctl_get_intmax(req, "stripesize");
if ((stripesize % secsize) != 0) {
gctl_error(req, "Stripesize should be multiple of %u.",
secsize);
return;
}
md.md_stripesize = *stripesizep;
md.md_stripesize = stripesize;
/*
* Ok, store metadata.
*/
for (i = 1; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 1; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
msize = g_get_mediasize(name);
ssize = g_get_sectorsize(name);
if (compsize < msize - ssize) {
@ -215,7 +194,7 @@ stripe_label(struct gctl_req *req)
md.md_no = i - 1;
md.md_provsize = msize;
if (!*hardcode)
if (!hardcode)
bzero(md.md_provider, sizeof(md.md_provider));
else {
if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
@ -239,24 +218,16 @@ static void
stripe_clear(struct gctl_req *req)
{
const char *name;
char param[16];
unsigned i;
int *nargs, error;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < (unsigned)*nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_clear(name, G_STRIPE_MAGIC);
if (error != 0) {
fprintf(stderr, "Can't clear metadata on %s: %s.\n",
@ -288,23 +259,16 @@ stripe_dump(struct gctl_req *req)
{
struct g_stripe_metadata md, tmpmd;
const char *name;
char param[16];
int *nargs, error, i;
int error, i, nargs;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs < 1) {
nargs = gctl_get_int(req, "nargs");
if (nargs < 1) {
gctl_error(req, "Too few arguments.");
return;
}
for (i = 0; i < *nargs; i++) {
snprintf(param, sizeof(param), "arg%u", i);
name = gctl_get_asciiparam(req, param);
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
G_STRIPE_MAGIC);
if (error != 0) {

View File

@ -705,7 +705,8 @@ std_list(struct gctl_req *req, unsigned flags __unused)
struct gmesh mesh;
struct gclass *classp;
struct ggeom *gp;
int error, *nargs;
const char *name;
int error, i, nargs;
error = geom_gettree(&mesh);
if (error != 0) {
@ -718,22 +719,10 @@ std_list(struct gctl_req *req, unsigned flags __unused)
fprintf(stderr, "Class %s not found.\n", gclass_name);
return;
}
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
geom_deletetree(&mesh);
return;
}
if (*nargs > 0) {
int i;
for (i = 0; i < *nargs; i++) {
const char *name;
char param[16];
snprintf(param, sizeof(param), "arg%d", i);
name = gctl_get_asciiparam(req, param);
assert(name != NULL);
nargs = gctl_get_int(req, "nargs");
if (nargs > 0) {
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
gp = find_geom(classp, name);
if (gp != NULL)
list_one_geom(gp);
@ -853,8 +842,9 @@ std_status(struct gctl_req *req, unsigned flags __unused)
struct gmesh mesh;
struct gclass *classp;
struct ggeom *gp;
const char *name;
int name_len, status_len;
int error, *nargs, *script;
int error, i, n, nargs, script;
error = geom_gettree(&mesh);
if (error != 0) {
@ -866,28 +856,13 @@ std_status(struct gctl_req *req, unsigned flags __unused)
fprintf(stderr, "Class %s not found.\n", gclass_name);
goto end;
}
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
goto end;
}
script = gctl_get_paraml(req, "script", sizeof(*script));
if (script == NULL) {
gctl_error(req, "No '%s' argument.", "script");
goto end;
}
nargs = gctl_get_int(req, "nargs");
script = gctl_get_int(req, "script");
name_len = strlen("Name");
status_len = strlen("Status");
if (*nargs > 0) {
int i, n = 0;
for (i = 0; i < *nargs; i++) {
const char *name;
char param[16];
snprintf(param, sizeof(param), "arg%d", i);
name = gctl_get_asciiparam(req, param);
assert(name != NULL);
if (nargs > 0) {
for (i = 0, n = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
gp = find_geom(classp, name);
if (gp == NULL)
fprintf(stderr, "No such geom: %s.\n", name);
@ -899,8 +874,7 @@ std_status(struct gctl_req *req, unsigned flags __unused)
if (n == 0)
goto end;
} else {
int n = 0;
n = 0;
LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
if (LIST_EMPTY(&gp->lg_provider))
continue;
@ -910,23 +884,16 @@ std_status(struct gctl_req *req, unsigned flags __unused)
if (n == 0)
goto end;
}
if (!*script) {
if (!script) {
printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
"Components");
}
if (*nargs > 0) {
int i;
for (i = 0; i < *nargs; i++) {
const char *name;
char param[16];
snprintf(param, sizeof(param), "arg%d", i);
name = gctl_get_asciiparam(req, param);
assert(name != NULL);
if (nargs > 0) {
for (i = 0; i < nargs; i++) {
name = gctl_get_ascii(req, "arg%d", i);
gp = find_geom(classp, name);
if (gp != NULL) {
status_one_geom(gp, *script, name_len,
status_one_geom(gp, script, name_len,
status_len);
}
}
@ -934,7 +901,7 @@ std_status(struct gctl_req *req, unsigned flags __unused)
LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
if (LIST_EMPTY(&gp->lg_provider))
continue;
status_one_geom(gp, *script, name_len, status_len);
status_one_geom(gp, script, name_len, status_len);
}
}
end:

View File

@ -315,66 +315,76 @@ gctl_error(struct gctl_req *req, const char *error, ...)
va_end(ap);
}
void *
gctl_get_param(struct gctl_req *req, const char *param, int *len)
static void *
gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
{
unsigned i;
struct gctl_req_arg *argp;
char param[256];
void *p;
struct gctl_req_arg *ap;
for (i = 0; i < req->narg; i++) {
ap = &req->arg[i];
if (strcmp(param, ap->name))
continue;
if (!(ap->flag & GCTL_PARAM_RD))
continue;
p = ap->value;
if (len != NULL)
*len = ap->len;
return (p);
}
return (NULL);
}
char const *
gctl_get_asciiparam(struct gctl_req *req, const char *param)
{
unsigned i;
char const *p;
struct gctl_req_arg *ap;
vsnprintf(param, sizeof(param), pfmt, ap);
for (i = 0; i < req->narg; i++) {
ap = &req->arg[i];
if (strcmp(param, ap->name))
argp = &req->arg[i];
if (strcmp(param, argp->name))
continue;
if (!(ap->flag & GCTL_PARAM_RD))
if (!(argp->flag & GCTL_PARAM_RD))
continue;
p = ap->value;
if (ap->len < 1) {
gctl_error(req, "No length argument (%s)", param);
return (NULL);
}
if (p[ap->len - 1] != '\0') {
gctl_error(req, "Unterminated argument (%s)", param);
return (NULL);
p = argp->value;
if (len == 0) {
/* We are looking for a string. */
if (argp->len < 1) {
fprintf(stderr, "No length argument (%s).\n",
param);
abort();
}
if (((char *)p)[argp->len - 1] != '\0') {
fprintf(stderr, "Unterminated argument (%s).\n",
param);
abort();
}
} else if ((int)len != argp->len) {
fprintf(stderr, "Wrong length %s argument.\n", param);
abort();
}
return (p);
}
return (NULL);
fprintf(stderr, "No such argument (%s).\n", param);
abort();
}
void *
gctl_get_paraml(struct gctl_req *req, const char *param, int len)
int
gctl_get_int(struct gctl_req *req, const char *pfmt, ...)
{
int i;
void *p;
int *p;
va_list ap;
p = gctl_get_param(req, param, &i);
if (p == NULL)
gctl_error(req, "Missing %s argument", param);
else if (i != len) {
p = NULL;
gctl_error(req, "Wrong length %s argument", param);
}
va_start(ap, pfmt);
p = gctl_get_param(req, sizeof(int), pfmt, ap);
va_end(ap);
return (*p);
}
intmax_t
gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
{
intmax_t *p;
va_list ap;
va_start(ap, pfmt);
p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
va_end(ap);
return (*p);
}
const char *
gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
{
const char *p;
va_list ap;
va_start(ap, pfmt);
p = gctl_get_param(req, 0, pfmt, ap);
va_end(ap);
return (p);
}

View File

@ -28,6 +28,8 @@
#ifndef _SUBR_H_
#define _SUBR_H_
#include <stdint.h>
unsigned g_lcm(unsigned a, unsigned b);
uint32_t bitcount32(uint32_t x);
@ -40,7 +42,7 @@ int g_metadata_store(const char *name, u_char *md, size_t size);
int g_metadata_clear(const char *name, const char *magic);
void gctl_error(struct gctl_req *req, const char *error, ...);
void *gctl_get_param(struct gctl_req *req, const char *param, int *len);
char const *gctl_get_asciiparam(struct gctl_req *req, const char *param);
void *gctl_get_paraml(struct gctl_req *req, const char *param, int len);
int gctl_get_int(struct gctl_req *req, const char *pfmt, ...);
intmax_t gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...);
const char *gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...);
#endif /* !_SUBR_H_ */