2014-12-07 13:44:34 +03:00
|
|
|
/* $NetBSD: ccdconfig.c,v 1.56 2014/12/07 10:44:34 mlelstv Exp $ */
|
1995-08-17 20:37:17 +04:00
|
|
|
|
1996-02-28 04:01:17 +03:00
|
|
|
/*-
|
1997-01-30 07:02:08 +03:00
|
|
|
* Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
|
1995-08-17 20:37:17 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
1996-02-28 04:01:17 +03:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe.
|
|
|
|
*
|
1995-08-17 20:37:17 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
1996-02-28 04:01:17 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
1997-07-31 04:11:51 +04:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
1996-02-28 04:01:17 +03:00
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
1995-08-17 20:37:17 +04:00
|
|
|
*/
|
|
|
|
|
1997-07-20 09:00:51 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2008-07-20 05:20:21 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1996, 1997\
|
|
|
|
The NetBSD Foundation, Inc. All rights reserved.");
|
2014-12-07 13:44:34 +03:00
|
|
|
__RCSID("$NetBSD: ccdconfig.c,v 1.56 2014/12/07 10:44:34 mlelstv Exp $");
|
1997-07-20 09:00:51 +04:00
|
|
|
#endif
|
|
|
|
|
1995-08-17 20:37:17 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1996-05-16 11:11:17 +04:00
|
|
|
#include <util.h>
|
1995-08-17 20:37:17 +04:00
|
|
|
|
|
|
|
#include <dev/ccdvar.h>
|
|
|
|
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
1998-07-06 11:50:18 +04:00
|
|
|
|
1997-12-05 23:01:51 +03:00
|
|
|
static size_t lineno;
|
1998-07-06 11:50:18 +04:00
|
|
|
static gid_t egid;
|
1997-12-05 23:01:51 +03:00
|
|
|
static int verbose;
|
2001-01-28 03:59:26 +03:00
|
|
|
static const char *ccdconf = _PATH_CCDCONF;
|
1995-08-17 20:37:17 +04:00
|
|
|
|
2011-08-27 20:29:51 +04:00
|
|
|
static struct flagval {
|
2001-01-28 03:59:26 +03:00
|
|
|
const char *fv_flag;
|
1995-08-17 20:37:17 +04:00
|
|
|
int fv_val;
|
|
|
|
} flagvaltab[] = {
|
|
|
|
{ "CCDF_UNIFORM", CCDF_UNIFORM },
|
2003-10-17 09:23:56 +04:00
|
|
|
{ "CCDF_NOLABEL", CCDF_NOLABEL },
|
1995-08-17 20:37:17 +04:00
|
|
|
{ NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CCD_CONFIG 0 /* configure a device */
|
|
|
|
#define CCD_CONFIGALL 1 /* configure all devices */
|
|
|
|
#define CCD_UNCONFIG 2 /* unconfigure a device */
|
|
|
|
#define CCD_UNCONFIGALL 3 /* unconfigure all devices */
|
|
|
|
#define CCD_DUMP 4 /* dump a ccd's configuration */
|
|
|
|
|
2005-01-20 18:49:24 +03:00
|
|
|
static int checkdev(char *);
|
|
|
|
static int do_io(char *, u_long, struct ccd_ioctl *);
|
|
|
|
static int do_single(int, char **, int);
|
|
|
|
static int do_all(int);
|
|
|
|
static int dump_ccd(int, char **, int);
|
|
|
|
static int flags_to_val(char *);
|
|
|
|
static int pathtounit(char *, int *);
|
|
|
|
static char *resolve_ccdname(char *);
|
2011-08-27 20:29:51 +04:00
|
|
|
__dead static void usage(void);
|
1995-08-17 20:37:17 +04:00
|
|
|
|
|
|
|
int
|
2005-01-20 18:49:24 +03:00
|
|
|
main(int argc, char *argv[])
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
|
|
|
int ch, options = 0, action = CCD_CONFIG;
|
|
|
|
|
1998-07-06 11:50:18 +04:00
|
|
|
egid = getegid();
|
|
|
|
setegid(getgid());
|
2013-04-27 21:12:36 +04:00
|
|
|
while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
|
1995-08-17 20:37:17 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'c':
|
|
|
|
action = CCD_CONFIG;
|
|
|
|
++options;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
action = CCD_CONFIGALL;
|
|
|
|
++options;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
ccdconf = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g':
|
|
|
|
action = CCD_DUMP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
action = CCD_UNCONFIG;
|
|
|
|
++options;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
action = CCD_UNCONFIGALL;
|
|
|
|
++options;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (options > 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case CCD_CONFIG:
|
|
|
|
case CCD_UNCONFIG:
|
|
|
|
exit(do_single(argc, argv, action));
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
case CCD_CONFIGALL:
|
|
|
|
case CCD_UNCONFIGALL:
|
|
|
|
exit(do_all(action));
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
case CCD_DUMP:
|
1997-07-20 09:00:51 +04:00
|
|
|
default:
|
1997-01-30 07:02:08 +03:00
|
|
|
exit(dump_ccd(argc, argv, action));
|
1995-08-17 20:37:17 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-01-20 18:49:24 +03:00
|
|
|
do_single(int argc, char **argv, int action)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
|
|
|
struct ccd_ioctl ccio;
|
|
|
|
char *ccd, *cp, *cp2, **disks;
|
2014-12-07 13:44:34 +03:00
|
|
|
char buf[MAXPATHLEN];
|
1997-07-20 09:00:51 +04:00
|
|
|
int noflags = 0, i, ileave, flags, j;
|
2014-12-07 13:44:34 +03:00
|
|
|
unsigned int ndisks, ui;
|
|
|
|
int ret = 1;
|
1995-08-17 20:37:17 +04:00
|
|
|
|
1997-07-20 09:00:51 +04:00
|
|
|
flags = 0;
|
1997-09-14 12:39:43 +04:00
|
|
|
memset(&ccio, 0, sizeof(ccio));
|
1995-08-17 20:37:17 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If unconfiguring, all arguments are treated as ccds.
|
|
|
|
*/
|
|
|
|
if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
|
|
|
|
for (i = 0; argc != 0; ) {
|
|
|
|
cp = *argv++; --argc;
|
|
|
|
if ((ccd = resolve_ccdname(cp)) == NULL) {
|
|
|
|
warnx("invalid ccd name: %s", cp);
|
|
|
|
i = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (do_io(ccd, CCDIOCCLR, &ccio))
|
|
|
|
i = 1;
|
|
|
|
else
|
|
|
|
if (verbose)
|
1995-08-23 05:06:59 +04:00
|
|
|
printf("%s unconfigured\n", cp);
|
2006-03-20 04:00:34 +03:00
|
|
|
free(ccd);
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure there are enough arguments. */
|
1998-08-25 23:18:12 +04:00
|
|
|
if (argc < 4) {
|
1995-08-17 20:37:17 +04:00
|
|
|
if (argc == 3) {
|
|
|
|
/* Assume that no flags are specified. */
|
|
|
|
noflags = 1;
|
|
|
|
} else {
|
|
|
|
if (action == CCD_CONFIGALL) {
|
1997-12-05 23:01:51 +03:00
|
|
|
warnx("%s: bad line: %lu", ccdconf,
|
|
|
|
(u_long)lineno);
|
1995-08-17 20:37:17 +04:00
|
|
|
return (1);
|
|
|
|
} else
|
|
|
|
usage();
|
|
|
|
}
|
1998-08-25 23:18:12 +04:00
|
|
|
}
|
1995-08-17 20:37:17 +04:00
|
|
|
|
|
|
|
/* First argument is the ccd to configure. */
|
|
|
|
cp = *argv++; --argc;
|
|
|
|
if ((ccd = resolve_ccdname(cp)) == NULL) {
|
|
|
|
warnx("invalid ccd name: %s", cp);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next argument is the interleave factor. */
|
|
|
|
cp = *argv++; --argc;
|
|
|
|
errno = 0; /* to check for ERANGE */
|
|
|
|
ileave = (int)strtol(cp, &cp2, 10);
|
|
|
|
if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
|
|
|
|
warnx("invalid interleave factor: %s", cp);
|
2006-03-17 19:14:48 +03:00
|
|
|
free(ccd);
|
1995-08-17 20:37:17 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noflags == 0) {
|
|
|
|
/* Next argument is the ccd configuration flags. */
|
|
|
|
cp = *argv++; --argc;
|
|
|
|
if ((flags = flags_to_val(cp)) < 0) {
|
|
|
|
warnx("invalid flags argument: %s", cp);
|
2006-03-17 19:14:48 +03:00
|
|
|
free(ccd);
|
1995-08-17 20:37:17 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next is the list of disks to make the ccd from. */
|
2013-04-27 21:12:36 +04:00
|
|
|
disks = emalloc(argc * sizeof(char *));
|
2014-12-07 13:44:34 +03:00
|
|
|
for (ndisks = 0; argc != 0; ++argv, --argc) {
|
|
|
|
if (getfsspecname(buf, sizeof(buf), *argv) == NULL) {
|
|
|
|
warn("%s", *argv);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = strdup(buf);
|
|
|
|
if (cp == NULL) {
|
|
|
|
warn("%s", cp);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
1995-08-17 20:37:17 +04:00
|
|
|
if ((j = checkdev(cp)) == 0)
|
2014-12-07 13:44:34 +03:00
|
|
|
disks[ndisks++] = cp;
|
1995-08-17 20:37:17 +04:00
|
|
|
else {
|
|
|
|
warnx("%s: %s", cp, strerror(j));
|
2014-12-07 13:44:34 +03:00
|
|
|
goto error;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the ccio. */
|
|
|
|
ccio.ccio_disks = disks;
|
2014-12-07 13:44:34 +03:00
|
|
|
ccio.ccio_ndisks = ndisks;
|
1995-08-17 20:37:17 +04:00
|
|
|
ccio.ccio_ileave = ileave;
|
|
|
|
ccio.ccio_flags = flags;
|
|
|
|
|
2014-12-07 13:44:34 +03:00
|
|
|
if (do_io(ccd, CCDIOCSET, &ccio))
|
|
|
|
goto error;
|
1995-08-17 20:37:17 +04:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
printf("ccd%d: %d components ", ccio.ccio_unit,
|
|
|
|
ccio.ccio_ndisks);
|
2009-03-16 15:52:07 +03:00
|
|
|
for (ui = 0; ui < ccio.ccio_ndisks; ++ui) {
|
|
|
|
if ((cp2 = strrchr(disks[ui], '/')) != NULL)
|
1995-08-17 20:37:17 +04:00
|
|
|
++cp2;
|
|
|
|
else
|
2009-03-16 15:52:07 +03:00
|
|
|
cp2 = disks[ui];
|
1995-08-17 20:37:17 +04:00
|
|
|
printf("%c%s%c",
|
2009-03-16 15:52:07 +03:00
|
|
|
ui == 0 ? '(' : ' ', cp2,
|
|
|
|
ui == ccio.ccio_ndisks - 1 ? ')' : ',');
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
2014-08-16 23:27:27 +04:00
|
|
|
printf(", %ju blocks ", (uintmax_t)ccio.ccio_size);
|
1995-08-17 20:37:17 +04:00
|
|
|
if (ccio.ccio_ileave != 0)
|
|
|
|
printf("interleaved at %d blocks\n", ccio.ccio_ileave);
|
|
|
|
else
|
|
|
|
printf("concatenated\n");
|
|
|
|
}
|
|
|
|
|
2014-12-07 13:44:34 +03:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
error:
|
2006-03-17 19:14:48 +03:00
|
|
|
free(ccd);
|
2014-12-07 13:44:34 +03:00
|
|
|
while (ndisks > 0)
|
|
|
|
free(disks[--ndisks]);
|
1995-08-17 20:37:17 +04:00
|
|
|
free(disks);
|
2014-12-07 13:44:34 +03:00
|
|
|
return (ret);
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-01-20 18:49:24 +03:00
|
|
|
do_all(int action)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
|
|
|
FILE *f;
|
2003-09-19 12:35:15 +04:00
|
|
|
char *line, *cp, *vp, **argv, **nargv;
|
1997-12-05 23:01:51 +03:00
|
|
|
int argc, rval;
|
|
|
|
size_t len;
|
1995-08-17 20:37:17 +04:00
|
|
|
|
1997-07-20 09:00:51 +04:00
|
|
|
rval = 0;
|
|
|
|
|
1997-12-30 08:40:47 +03:00
|
|
|
(void)setegid(getgid());
|
1995-08-17 20:37:17 +04:00
|
|
|
if ((f = fopen(ccdconf, "r")) == NULL) {
|
1997-12-30 08:40:47 +03:00
|
|
|
(void)setegid(egid);
|
1995-08-17 20:37:17 +04:00
|
|
|
warn("fopen: %s", ccdconf);
|
|
|
|
return (1);
|
|
|
|
}
|
1997-12-30 08:40:47 +03:00
|
|
|
(void)setegid(egid);
|
1995-08-17 20:37:17 +04:00
|
|
|
|
1997-12-01 06:40:51 +03:00
|
|
|
while ((line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL))
|
|
|
|
!= NULL) {
|
1995-08-17 20:37:17 +04:00
|
|
|
argc = 0;
|
|
|
|
argv = NULL;
|
1997-12-01 06:40:51 +03:00
|
|
|
if (len == 0)
|
1995-08-17 20:37:17 +04:00
|
|
|
goto end_of_line;
|
1997-12-01 06:40:51 +03:00
|
|
|
|
|
|
|
for (cp = line; cp != NULL; ) {
|
|
|
|
while ((vp = strsep(&cp, "\t ")) != NULL && *vp == '\0')
|
|
|
|
;
|
|
|
|
if (vp == NULL)
|
|
|
|
continue;
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
nargv = erealloc(argv, sizeof(char *) * (argc + 1));
|
2003-09-19 12:35:15 +04:00
|
|
|
argv = nargv;
|
|
|
|
argc++;
|
1997-12-01 06:40:51 +03:00
|
|
|
argv[argc - 1] = vp;
|
|
|
|
|
1995-08-17 20:37:17 +04:00
|
|
|
/*
|
|
|
|
* If our action is to unconfigure all, then pass
|
|
|
|
* just the first token to do_single() and ignore
|
|
|
|
* the rest. Since this will be encountered on
|
|
|
|
* our first pass through the line, the Right
|
|
|
|
* Thing will happen.
|
|
|
|
*/
|
|
|
|
if (action == CCD_UNCONFIGALL) {
|
|
|
|
if (do_single(argc, argv, action))
|
|
|
|
rval = 1;
|
|
|
|
goto end_of_line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc != 0)
|
|
|
|
if (do_single(argc, argv, action))
|
|
|
|
rval = 1;
|
|
|
|
|
|
|
|
end_of_line:
|
|
|
|
if (argv != NULL)
|
|
|
|
free(argv);
|
1997-12-01 06:40:51 +03:00
|
|
|
free(line);
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)fclose(f);
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-01-20 18:49:24 +03:00
|
|
|
checkdev(char *path)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st) != 0)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-01-20 18:49:24 +03:00
|
|
|
pathtounit(char *path, int *unitp)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st) != 0)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2006-02-17 02:25:18 +03:00
|
|
|
*unitp = DISKUNIT(st.st_rdev);
|
1995-08-17 20:37:17 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2005-01-20 18:49:24 +03:00
|
|
|
resolve_ccdname(char *name)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
1997-07-20 09:00:51 +04:00
|
|
|
char c, *path;
|
2003-10-09 00:39:07 +04:00
|
|
|
size_t len;
|
1995-08-17 20:37:17 +04:00
|
|
|
int rawpart;
|
|
|
|
|
|
|
|
if (name[0] == '/' || name[0] == '.') {
|
|
|
|
/* Assume they gave the correct pathname. */
|
2013-04-27 21:12:36 +04:00
|
|
|
return estrdup(name);
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(name);
|
|
|
|
c = name[len - 1];
|
|
|
|
|
2004-10-28 23:43:29 +04:00
|
|
|
if (isdigit((unsigned char)c)) {
|
2003-10-09 00:39:07 +04:00
|
|
|
if ((rawpart = getrawpartition()) < 0)
|
2013-04-27 21:12:36 +04:00
|
|
|
return NULL;
|
|
|
|
easprintf(&path, "/dev/%s%c", name, 'a' + rawpart);
|
1995-08-17 20:37:17 +04:00
|
|
|
} else
|
2013-04-27 21:12:36 +04:00
|
|
|
easprintf(&path, "/dev/%s", name);
|
1995-08-17 20:37:17 +04:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
return path;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-01-20 18:49:24 +03:00
|
|
|
do_io(char *path, u_long cmd, struct ccd_ioctl *cciop)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
|
|
|
int fd;
|
2001-01-28 03:59:26 +03:00
|
|
|
const char *cp;
|
1995-08-17 20:37:17 +04:00
|
|
|
|
|
|
|
if ((fd = open(path, O_RDWR, 0640)) < 0) {
|
|
|
|
warn("open: %s", path);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, cmd, cciop) < 0) {
|
|
|
|
switch (cmd) {
|
|
|
|
case CCDIOCSET:
|
|
|
|
cp = "CCDIOCSET";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CCDIOCCLR:
|
|
|
|
cp = "CCDIOCCLR";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cp = "unknown";
|
|
|
|
}
|
|
|
|
warn("ioctl (%s): %s", cp, path);
|
2011-01-05 02:31:29 +03:00
|
|
|
(void)close(fd);
|
1995-08-17 20:37:17 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2011-01-05 02:31:29 +03:00
|
|
|
(void)close(fd);
|
1995-08-17 20:37:17 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
static void
|
|
|
|
print_ccd_info(int u, struct ccddiskinfo *ccd, char *str)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
2013-04-27 21:12:36 +04:00
|
|
|
static int header_printed = 0;
|
1995-08-17 20:37:17 +04:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
if (header_printed == 0 && verbose) {
|
|
|
|
printf("# ccd\t\tileave\tflags\t\tsize\tcomponent devices\n");
|
|
|
|
header_printed = 1;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
/* Dump out softc information. */
|
2014-08-17 11:05:44 +04:00
|
|
|
printf("ccd%d\t\t%d\t0x%x\t%ju\t", u, ccd->ccd_ileave,
|
|
|
|
ccd->ccd_flags & CCDF_USERMASK,
|
|
|
|
(uintmax_t)ccd->ccd_size * DEV_BSIZE);
|
1995-08-17 20:37:17 +04:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
/* Read component pathname and display component info. */
|
|
|
|
for (size_t i = 0; i < ccd->ccd_ndisks; ++i) {
|
|
|
|
fputs(str, stdout);
|
|
|
|
fputc((i + 1 < ccd->ccd_ndisks) ? ' ' : '\n', stdout);
|
|
|
|
str += strlen(str) + 1;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
2013-04-27 21:12:36 +04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
1995-08-17 20:37:17 +04:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
static int
|
|
|
|
printccdinfo(int u)
|
|
|
|
{
|
|
|
|
struct ccddiskinfo ccd;
|
|
|
|
size_t s = sizeof(ccd);
|
|
|
|
size_t len;
|
|
|
|
const char *str;
|
2006-02-17 02:25:18 +03:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
if (sysctlbyname(str = "kern.ccd.info", &ccd, &s, &u, sizeof(u))
|
|
|
|
== -1) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
warnx("ccd unit %d not configured", u);
|
|
|
|
else
|
|
|
|
warn("error getting %s for ccd%d", str, u);
|
|
|
|
return 1;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
if (sysctlbyname(str = "kern.ccd.components", NULL, &len, &u, sizeof(u))
|
|
|
|
== -1) {
|
|
|
|
warn("Error getting %s for ccd%d", str, u);
|
|
|
|
return 1;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
char *names;
|
|
|
|
names = emalloc(len);
|
|
|
|
if (sysctlbyname(str = "kern.ccd.components", names, &len, &u,
|
|
|
|
sizeof(u)) == -1) {
|
|
|
|
warn("error getting %s for ccd%d", str, u);
|
|
|
|
free(names);
|
|
|
|
return 1;
|
2006-02-17 02:25:18 +03:00
|
|
|
}
|
2013-04-27 21:12:36 +04:00
|
|
|
print_ccd_info(u, &ccd, names);
|
|
|
|
free(names);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-17 02:25:18 +03:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
static int
|
|
|
|
dump_ccd(int argc, char **argv, int action)
|
|
|
|
{
|
|
|
|
const char *sys;
|
|
|
|
int errs = 0;
|
1995-08-17 20:37:17 +04:00
|
|
|
if (argc == 0) {
|
2013-04-27 21:12:36 +04:00
|
|
|
int *units;
|
|
|
|
size_t nunits = 0;
|
|
|
|
if (sysctlbyname(sys = "kern.ccd.units", NULL, &nunits, NULL, 0)
|
|
|
|
== -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
warnx("no ccd driver in the kernel");
|
|
|
|
return 1;
|
|
|
|
case ENOMEM:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err(EXIT_FAILURE, "1 error getting %s", sys);
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
2013-04-27 21:12:36 +04:00
|
|
|
}
|
1995-08-17 20:37:17 +04:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
if (nunits == 0) {
|
|
|
|
warnx("no concatenated disks configured");
|
|
|
|
return 1;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
units = emalloc(nunits);
|
1995-08-17 20:37:17 +04:00
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
if (sysctlbyname(sys, units, &nunits, NULL, 0) == -1)
|
|
|
|
err(EXIT_FAILURE, "2 error getting %s", sys);
|
|
|
|
nunits /= sizeof(*units);
|
|
|
|
for (size_t i = 0; i < nunits; i++)
|
|
|
|
errs += printccdinfo(units[i]);
|
|
|
|
free(units);
|
|
|
|
return errs;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
/* Dump ccd configuration to stdout. */
|
|
|
|
while (argc) {
|
2013-05-03 04:01:15 +04:00
|
|
|
int i = 0; /* XXX: vax gcc */
|
2013-04-27 21:12:36 +04:00
|
|
|
int error;
|
|
|
|
char *cp = *argv++; --argc;
|
|
|
|
char *ccd;
|
|
|
|
|
|
|
|
if ((ccd = resolve_ccdname(cp)) == NULL) {
|
|
|
|
warnx("invalid ccd name: %s", cp);
|
|
|
|
errs++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((error = pathtounit(ccd, &i)) != 0) {
|
|
|
|
warn("%s", ccd);
|
|
|
|
free(ccd);
|
|
|
|
errs++;
|
|
|
|
continue;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
2013-04-27 21:12:36 +04:00
|
|
|
errs += printccdinfo(i);
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
2013-04-27 21:12:36 +04:00
|
|
|
return errs;
|
1995-08-17 20:37:17 +04:00
|
|
|
}
|
|
|
|
|
2013-04-27 21:12:36 +04:00
|
|
|
|
1995-08-17 20:37:17 +04:00
|
|
|
static int
|
2005-01-20 18:49:24 +03:00
|
|
|
flags_to_val(char *flags)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
|
|
|
char *cp, *tok;
|
|
|
|
int i, tmp, val = ~CCDF_USERMASK;
|
|
|
|
size_t flagslen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The most common case is that of NIL flags, so check for
|
|
|
|
* those first.
|
|
|
|
*/
|
|
|
|
if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
|
|
|
|
strcmp("0", flags) == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
flagslen = strlen(flags);
|
|
|
|
|
|
|
|
/* Check for values represented by strings. */
|
2013-04-27 21:12:36 +04:00
|
|
|
cp = estrdup(flags);
|
1995-08-17 20:37:17 +04:00
|
|
|
tmp = 0;
|
|
|
|
for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
|
|
|
|
for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
|
|
|
|
if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
|
|
|
|
break;
|
|
|
|
if (flagvaltab[i].fv_flag == NULL) {
|
|
|
|
free(cp);
|
|
|
|
goto bad_string;
|
|
|
|
}
|
|
|
|
tmp |= flagvaltab[i].fv_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we get here, the string was ok. */
|
|
|
|
free(cp);
|
|
|
|
val = tmp;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
bad_string:
|
|
|
|
|
|
|
|
/* Check for values represented in hex. */
|
|
|
|
if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
|
|
|
|
errno = 0; /* to check for ERANGE */
|
|
|
|
val = (int)strtol(&flags[2], &cp, 16);
|
|
|
|
if ((errno == ERANGE) || (*cp != '\0'))
|
|
|
|
return (-1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for values represented in decimal. */
|
|
|
|
errno = 0; /* to check for ERANGE */
|
|
|
|
val = (int)strtol(flags, &cp, 10);
|
|
|
|
if ((errno == ERANGE) || (*cp != '\0'))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-01-20 18:49:24 +03:00
|
|
|
usage(void)
|
1995-08-17 20:37:17 +04:00
|
|
|
{
|
2001-02-20 01:48:57 +03:00
|
|
|
const char *progname = getprogname();
|
|
|
|
|
|
|
|
fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] dev [...]\n",
|
|
|
|
progname);
|
|
|
|
fprintf(stderr, " %s -C [-v] [-f config_file]\n", progname);
|
|
|
|
fprintf(stderr, " %s -u [-v] ccd [...]\n", progname);
|
|
|
|
fprintf(stderr, " %s -U [-v] [-f config_file]\n", progname);
|
2013-04-27 21:12:36 +04:00
|
|
|
fprintf(stderr, " %s -g [ccd [...]]\n",
|
2001-02-20 01:48:57 +03:00
|
|
|
progname);
|
1995-08-17 20:37:17 +04:00
|
|
|
exit(1);
|
|
|
|
}
|