2012-01-21 11:48:48 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2000-2004 Poul-Henning Kamp <phk@FreeBSD.org>
|
2012-01-27 11:48:44 +00:00
|
|
|
* Copyright (c) 2012 The FreeBSD Foundation
|
2012-01-21 11:48:48 +00:00
|
|
|
* All rights reserved.
|
2000-12-28 20:57:57 +00:00
|
|
|
*
|
2012-01-27 11:48:44 +00:00
|
|
|
* Portions of this software were developed by Edward Tomasz Napierala
|
|
|
|
* under sponsorship from the FreeBSD Foundation.
|
|
|
|
*
|
2012-01-21 11:48:48 +00: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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 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.
|
2000-12-28 20:57:57 +00:00
|
|
|
*
|
2012-01-21 11:48:48 +00:00
|
|
|
* $FreeBSD$
|
2000-12-28 20:57:57 +00:00
|
|
|
*/
|
2012-01-21 11:48:48 +00:00
|
|
|
|
2006-03-26 22:52:36 +00:00
|
|
|
#include <sys/param.h>
|
2006-03-26 23:21:11 +00:00
|
|
|
#include <sys/devicestat.h>
|
2006-03-26 22:52:36 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/mdioctl.h>
|
|
|
|
#include <sys/module.h>
|
2006-03-26 23:21:11 +00:00
|
|
|
#include <sys/resource.h>
|
2006-03-26 22:52:36 +00:00
|
|
|
#include <sys/stat.h>
|
2000-12-28 20:57:57 +00:00
|
|
|
|
2006-03-26 22:52:36 +00:00
|
|
|
#include <assert.h>
|
2006-03-26 23:21:11 +00:00
|
|
|
#include <devstat.h>
|
2006-03-26 22:52:36 +00:00
|
|
|
#include <err.h>
|
2005-08-30 16:45:32 +00:00
|
|
|
#include <errno.h>
|
2000-12-28 20:57:57 +00:00
|
|
|
#include <fcntl.h>
|
2004-09-16 21:32:13 +00:00
|
|
|
#include <inttypes.h>
|
2006-03-26 23:21:11 +00:00
|
|
|
#include <libgeom.h>
|
2004-09-16 21:32:13 +00:00
|
|
|
#include <libutil.h>
|
2013-06-20 14:30:16 +00:00
|
|
|
#include <paths.h>
|
2006-03-26 23:21:11 +00:00
|
|
|
#include <stdarg.h>
|
2006-03-26 22:52:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2000-12-28 20:57:57 +00:00
|
|
|
#include <string.h>
|
2006-03-26 22:52:36 +00:00
|
|
|
#include <unistd.h>
|
2000-12-28 20:57:57 +00:00
|
|
|
|
2006-03-26 22:52:36 +00:00
|
|
|
static struct md_ioctl mdio;
|
2012-07-07 20:32:21 +00:00
|
|
|
static enum {UNSET, ATTACH, DETACH, RESIZE, LIST} action = UNSET;
|
2006-03-26 22:52:36 +00:00
|
|
|
static int nflag;
|
2003-06-11 06:38:24 +00:00
|
|
|
|
2006-03-26 23:21:11 +00:00
|
|
|
static void usage(void);
|
2011-04-29 22:40:11 +00:00
|
|
|
static void md_set_file(const char *);
|
2013-06-20 14:30:16 +00:00
|
|
|
static int md_find(const char *, const char *);
|
|
|
|
static int md_query(const char *, const int, const char *);
|
|
|
|
static int md_list(const char *, int, const char *);
|
2010-01-22 08:51:58 +00:00
|
|
|
static char *geom_config_get(struct gconf *g, const char *name);
|
2006-03-26 23:21:11 +00:00
|
|
|
static void md_prthumanval(char *length);
|
|
|
|
|
|
|
|
#define OPT_VERBOSE 0x01
|
|
|
|
#define OPT_UNIT 0x02
|
|
|
|
#define OPT_DONE 0x04
|
|
|
|
#define OPT_LIST 0x10
|
|
|
|
|
|
|
|
#define CLASS_NAME_MD "MD"
|
|
|
|
|
2006-03-26 22:52:36 +00:00
|
|
|
static void
|
2010-01-22 08:51:58 +00:00
|
|
|
usage(void)
|
2000-12-31 11:20:49 +00:00
|
|
|
{
|
2012-01-27 11:48:44 +00:00
|
|
|
|
2004-11-13 17:08:52 +00:00
|
|
|
fprintf(stderr,
|
2005-02-10 09:19:34 +00:00
|
|
|
"usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
|
2004-11-13 17:08:52 +00:00
|
|
|
" [-s size] [-S sectorsize] [-u unit]\n"
|
2010-11-13 15:28:44 +00:00
|
|
|
" [-x sectors/track] [-y heads/cylinder]\n"
|
2009-01-10 17:17:18 +00:00
|
|
|
" mdconfig -d -u unit [-o [no]force]\n"
|
2012-07-07 20:32:21 +00:00
|
|
|
" mdconfig -r -u unit -s size [-o [no]force]\n"
|
2013-06-20 14:30:16 +00:00
|
|
|
" mdconfig -l [-v] [-n] [-f file] [-u unit]\n"
|
2011-04-29 22:40:11 +00:00
|
|
|
" mdconfig file\n");
|
2012-11-21 16:56:47 +00:00
|
|
|
fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n");
|
2001-02-26 15:31:47 +00:00
|
|
|
fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
|
2005-03-01 14:56:49 +00:00
|
|
|
fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
|
2015-10-10 09:37:31 +00:00
|
|
|
fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB), \n");
|
|
|
|
fprintf(stderr, "\t\t %%dt (TB), or %%dp (PB)\n");
|
2000-12-31 11:20:49 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-12-28 20:57:57 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-06-21 15:04:42 +00:00
|
|
|
int ch, fd, i, vflag;
|
2000-12-31 11:20:49 +00:00
|
|
|
char *p;
|
2012-07-07 20:32:21 +00:00
|
|
|
char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
|
2000-12-28 20:57:57 +00:00
|
|
|
|
2004-09-16 21:32:13 +00:00
|
|
|
bzero(&mdio, sizeof(mdio));
|
2004-12-27 17:20:06 +00:00
|
|
|
mdio.md_file = malloc(PATH_MAX);
|
|
|
|
if (mdio.md_file == NULL)
|
|
|
|
err(1, "could not allocate memory");
|
2008-06-21 15:04:42 +00:00
|
|
|
vflag = 0;
|
2004-12-27 17:20:06 +00:00
|
|
|
bzero(mdio.md_file, PATH_MAX);
|
2012-01-27 11:48:44 +00:00
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
usage();
|
|
|
|
|
2012-07-07 20:32:21 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:")) != -1) {
|
2000-12-28 20:57:57 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
2012-01-27 11:48:44 +00:00
|
|
|
if (action != UNSET && action != ATTACH)
|
2012-07-07 20:32:21 +00:00
|
|
|
errx(1, "-a is mutually exclusive "
|
|
|
|
"with -d, -r, and -l");
|
2000-12-28 20:57:57 +00:00
|
|
|
action = ATTACH;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2012-01-27 11:48:44 +00:00
|
|
|
if (action != UNSET && action != DETACH)
|
2012-07-07 20:32:21 +00:00
|
|
|
errx(1, "-d is mutually exclusive "
|
|
|
|
"with -a, -r, and -l");
|
2000-12-28 20:57:57 +00:00
|
|
|
action = DETACH;
|
2012-01-27 11:48:44 +00:00
|
|
|
mdio.md_options |= MD_AUTOUNIT;
|
2000-12-31 11:20:49 +00:00
|
|
|
break;
|
2012-07-07 20:32:21 +00:00
|
|
|
case 'r':
|
|
|
|
if (action != UNSET && action != RESIZE)
|
|
|
|
errx(1, "-r is mutually exclusive "
|
|
|
|
"with -a, -d, and -l");
|
|
|
|
action = RESIZE;
|
|
|
|
mdio.md_options |= MD_AUTOUNIT;
|
|
|
|
break;
|
2001-02-25 13:12:57 +00:00
|
|
|
case 'l':
|
2012-01-27 11:48:44 +00:00
|
|
|
if (action != UNSET && action != LIST)
|
2012-07-07 20:32:21 +00:00
|
|
|
errx(1, "-l is mutually exclusive "
|
|
|
|
"with -a, -r, and -d");
|
2001-02-25 13:12:57 +00:00
|
|
|
action = LIST;
|
2012-01-27 11:48:44 +00:00
|
|
|
mdio.md_options |= MD_AUTOUNIT;
|
2001-02-25 13:12:57 +00:00
|
|
|
break;
|
2003-06-11 06:38:24 +00:00
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
2000-12-31 11:20:49 +00:00
|
|
|
case 't':
|
2012-01-27 11:48:44 +00:00
|
|
|
if (tflag != NULL)
|
|
|
|
errx(1, "-t can be passed only once");
|
|
|
|
tflag = optarg;
|
2000-12-31 11:20:49 +00:00
|
|
|
if (!strcmp(optarg, "malloc")) {
|
|
|
|
mdio.md_type = MD_MALLOC;
|
2012-01-27 11:48:44 +00:00
|
|
|
mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
|
2000-12-31 11:20:49 +00:00
|
|
|
} else if (!strcmp(optarg, "vnode")) {
|
|
|
|
mdio.md_type = MD_VNODE;
|
2012-01-27 11:48:44 +00:00
|
|
|
mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
|
2000-12-31 11:20:49 +00:00
|
|
|
} else if (!strcmp(optarg, "swap")) {
|
|
|
|
mdio.md_type = MD_SWAP;
|
2012-01-27 11:48:44 +00:00
|
|
|
mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
|
2013-12-04 07:38:23 +00:00
|
|
|
} else if (!strcmp(optarg, "null")) {
|
|
|
|
mdio.md_type = MD_NULL;
|
|
|
|
mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
|
2012-01-27 11:48:44 +00:00
|
|
|
} else
|
|
|
|
errx(1, "unknown type: %s", optarg);
|
2000-12-28 20:57:57 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2012-01-27 11:48:44 +00:00
|
|
|
if (fflag != NULL)
|
|
|
|
errx(1, "-f can be passed only once");
|
2013-07-31 07:06:58 +00:00
|
|
|
fflag = realpath(optarg, NULL);
|
|
|
|
if (fflag == NULL)
|
|
|
|
err(1, "realpath");
|
2000-12-28 20:57:57 +00:00
|
|
|
break;
|
|
|
|
case 'o':
|
2004-03-10 20:41:09 +00:00
|
|
|
if (!strcmp(optarg, "async"))
|
|
|
|
mdio.md_options |= MD_ASYNC;
|
|
|
|
else if (!strcmp(optarg, "noasync"))
|
|
|
|
mdio.md_options &= ~MD_ASYNC;
|
|
|
|
else if (!strcmp(optarg, "cluster"))
|
2000-12-28 20:57:57 +00:00
|
|
|
mdio.md_options |= MD_CLUSTER;
|
|
|
|
else if (!strcmp(optarg, "nocluster"))
|
|
|
|
mdio.md_options &= ~MD_CLUSTER;
|
2000-12-31 11:20:49 +00:00
|
|
|
else if (!strcmp(optarg, "compress"))
|
|
|
|
mdio.md_options |= MD_COMPRESS;
|
|
|
|
else if (!strcmp(optarg, "nocompress"))
|
|
|
|
mdio.md_options &= ~MD_COMPRESS;
|
2001-08-07 19:23:16 +00:00
|
|
|
else if (!strcmp(optarg, "force"))
|
|
|
|
mdio.md_options |= MD_FORCE;
|
|
|
|
else if (!strcmp(optarg, "noforce"))
|
|
|
|
mdio.md_options &= ~MD_FORCE;
|
2004-09-08 20:28:29 +00:00
|
|
|
else if (!strcmp(optarg, "readonly"))
|
|
|
|
mdio.md_options |= MD_READONLY;
|
|
|
|
else if (!strcmp(optarg, "noreadonly"))
|
|
|
|
mdio.md_options &= ~MD_READONLY;
|
2000-12-28 20:57:57 +00:00
|
|
|
else if (!strcmp(optarg, "reserve"))
|
|
|
|
mdio.md_options |= MD_RESERVE;
|
|
|
|
else if (!strcmp(optarg, "noreserve"))
|
|
|
|
mdio.md_options &= ~MD_RESERVE;
|
|
|
|
else
|
2012-01-27 11:48:44 +00:00
|
|
|
errx(1, "unknown option: %s", optarg);
|
2000-12-28 20:57:57 +00:00
|
|
|
break;
|
2003-03-03 13:05:00 +00:00
|
|
|
case 'S':
|
2004-09-16 21:32:13 +00:00
|
|
|
mdio.md_sectorsize = strtoul(optarg, &p, 0);
|
2003-03-03 13:05:00 +00:00
|
|
|
break;
|
2000-12-28 20:57:57 +00:00
|
|
|
case 's':
|
2012-07-07 20:32:21 +00:00
|
|
|
if (sflag != NULL)
|
|
|
|
errx(1, "-s can be passed only once");
|
|
|
|
sflag = optarg;
|
2004-09-16 21:32:13 +00:00
|
|
|
mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
|
2000-12-31 11:20:49 +00:00
|
|
|
if (p == NULL || *p == '\0')
|
2004-09-16 21:32:13 +00:00
|
|
|
mdio.md_mediasize *= DEV_BSIZE;
|
2005-01-22 20:02:01 +00:00
|
|
|
else if (*p == 'b' || *p == 'B')
|
|
|
|
; /* do nothing */
|
2000-12-31 11:20:49 +00:00
|
|
|
else if (*p == 'k' || *p == 'K')
|
2004-09-16 21:32:13 +00:00
|
|
|
mdio.md_mediasize <<= 10;
|
2000-12-31 11:20:49 +00:00
|
|
|
else if (*p == 'm' || *p == 'M')
|
2004-09-16 21:32:13 +00:00
|
|
|
mdio.md_mediasize <<= 20;
|
2000-12-31 11:20:49 +00:00
|
|
|
else if (*p == 'g' || *p == 'G')
|
2004-09-16 21:32:13 +00:00
|
|
|
mdio.md_mediasize <<= 30;
|
|
|
|
else if (*p == 't' || *p == 'T') {
|
|
|
|
mdio.md_mediasize <<= 30;
|
|
|
|
mdio.md_mediasize <<= 10;
|
2015-10-10 09:37:31 +00:00
|
|
|
} else if (*p == 'p' || *p == 'P') {
|
|
|
|
mdio.md_mediasize <<= 30;
|
|
|
|
mdio.md_mediasize <<= 20;
|
2004-09-16 21:32:13 +00:00
|
|
|
} else
|
2012-01-27 11:48:44 +00:00
|
|
|
errx(1, "unknown suffix on -s argument");
|
2000-12-28 20:57:57 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2013-06-21 01:47:22 +00:00
|
|
|
if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
|
|
|
|
optarg += sizeof(_PATH_DEV) - 1;
|
2006-03-27 00:46:22 +00:00
|
|
|
if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
|
|
|
|
optarg += sizeof(MD_NAME) - 1;
|
2012-01-27 11:48:44 +00:00
|
|
|
uflag = optarg;
|
2000-12-28 20:57:57 +00:00
|
|
|
break;
|
2008-06-21 15:04:42 +00:00
|
|
|
case 'v':
|
|
|
|
vflag = OPT_VERBOSE;
|
|
|
|
break;
|
2003-04-09 11:59:29 +00:00
|
|
|
case 'x':
|
|
|
|
mdio.md_fwsectors = strtoul(optarg, &p, 0);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
mdio.md_fwheads = strtoul(optarg, &p, 0);
|
|
|
|
break;
|
2000-12-28 20:57:57 +00:00
|
|
|
default:
|
2000-12-31 11:20:49 +00:00
|
|
|
usage();
|
2000-12-28 20:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-29 22:40:11 +00:00
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2012-01-27 11:48:44 +00:00
|
|
|
|
|
|
|
if (action == UNSET)
|
2011-04-29 22:40:11 +00:00
|
|
|
action = ATTACH;
|
2012-01-27 11:48:44 +00:00
|
|
|
|
|
|
|
if (action == ATTACH) {
|
|
|
|
if (tflag == NULL) {
|
|
|
|
/*
|
|
|
|
* Try to infer the type based on other arguments.
|
|
|
|
*/
|
|
|
|
if (fflag != NULL || argc > 0) {
|
|
|
|
/* Imply ``-t vnode'' */
|
|
|
|
mdio.md_type = MD_VNODE;
|
|
|
|
mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
|
|
|
|
MD_COMPRESS;
|
2012-07-07 20:32:21 +00:00
|
|
|
} else if (sflag != NULL) {
|
2012-01-27 11:48:44 +00:00
|
|
|
/* Imply ``-t swap'' */
|
|
|
|
mdio.md_type = MD_SWAP;
|
|
|
|
mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
|
|
|
|
MD_COMPRESS;
|
|
|
|
} else
|
|
|
|
errx(1, "unable to determine type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
|
|
|
|
errx(1, "only -t vnode can be used with file name");
|
|
|
|
|
|
|
|
if (mdio.md_type == MD_VNODE) {
|
|
|
|
if (fflag != NULL) {
|
|
|
|
if (argc != 0)
|
|
|
|
usage();
|
|
|
|
md_set_file(fflag);
|
|
|
|
} else {
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
md_set_file(*argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mdio.md_options & MD_READONLY) == 0 &&
|
|
|
|
access(mdio.md_file, W_OK) < 0 &&
|
|
|
|
(errno == EACCES || errno == EPERM ||
|
|
|
|
errno == EROFS)) {
|
|
|
|
warnx("WARNING: opening backing store: %s "
|
|
|
|
"readonly", mdio.md_file);
|
|
|
|
mdio.md_options |= MD_READONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:38:23 +00:00
|
|
|
if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
|
|
|
|
mdio.md_type == MD_NULL) && sflag == NULL)
|
|
|
|
errx(1, "must specify -s for -t malloc, -t swap, "
|
|
|
|
"or -t null");
|
2012-01-27 11:48:44 +00:00
|
|
|
if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
|
|
|
|
errx(1, "must specify -f for -t vnode");
|
|
|
|
} else {
|
|
|
|
if (mdio.md_sectorsize != 0)
|
|
|
|
errx(1, "-S can only be used with -a");
|
2012-07-07 20:32:21 +00:00
|
|
|
if (action != RESIZE && sflag != NULL)
|
|
|
|
errx(1, "-s can only be used with -a and -r");
|
2012-01-27 11:48:44 +00:00
|
|
|
if (mdio.md_fwsectors != 0)
|
|
|
|
errx(1, "-x can only be used with -a");
|
|
|
|
if (mdio.md_fwheads != 0)
|
|
|
|
errx(1, "-y can only be used with -a");
|
2013-06-20 14:30:16 +00:00
|
|
|
if (fflag != NULL && action != LIST)
|
|
|
|
errx(1, "-f can only be used with -a and -l");
|
2012-01-27 11:48:44 +00:00
|
|
|
if (tflag != NULL)
|
|
|
|
errx(1, "-t can only be used with -a");
|
|
|
|
if (argc > 0)
|
|
|
|
errx(1, "file can only be used with -a");
|
2012-07-07 20:32:21 +00:00
|
|
|
if ((action != DETACH && action != RESIZE) &&
|
|
|
|
(mdio.md_options & ~MD_AUTOUNIT) != 0)
|
|
|
|
errx(1, "-o can only be used with -a, -d, and -r");
|
2012-01-27 11:48:44 +00:00
|
|
|
if (action == DETACH &&
|
|
|
|
(mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
|
|
|
|
errx(1, "only -o [no]force can be used with -d");
|
2012-07-07 20:32:21 +00:00
|
|
|
if (action == RESIZE &&
|
|
|
|
(mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
|
|
|
|
errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
|
2012-01-27 11:48:44 +00:00
|
|
|
}
|
|
|
|
|
2012-07-07 20:32:21 +00:00
|
|
|
if (action == RESIZE && sflag == NULL)
|
|
|
|
errx(1, "must specify -s for -r");
|
|
|
|
|
2012-01-27 11:48:44 +00:00
|
|
|
if (action != LIST && vflag == OPT_VERBOSE)
|
|
|
|
errx(1, "-v can only be used with -l");
|
|
|
|
|
|
|
|
if (uflag != NULL) {
|
|
|
|
mdio.md_unit = strtoul(uflag, &p, 0);
|
|
|
|
if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
|
|
|
|
errx(1, "bad unit: %s", uflag);
|
|
|
|
mdio.md_options &= ~MD_AUTOUNIT;
|
2011-04-29 22:40:11 +00:00
|
|
|
}
|
|
|
|
|
2001-12-20 06:38:21 +00:00
|
|
|
mdio.md_version = MDIOVERSION;
|
2000-12-28 20:57:57 +00:00
|
|
|
|
2006-02-18 11:40:24 +00:00
|
|
|
if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
|
|
|
|
err(1, "failed to load geom_md module");
|
|
|
|
|
2013-06-20 14:30:16 +00:00
|
|
|
fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
|
2000-12-28 20:57:57 +00:00
|
|
|
if (fd < 0)
|
2013-06-20 14:30:16 +00:00
|
|
|
err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
|
2012-01-27 11:48:44 +00:00
|
|
|
|
|
|
|
if (action == ATTACH) {
|
2000-12-28 20:57:57 +00:00
|
|
|
i = ioctl(fd, MDIOCATTACH, &mdio);
|
2001-02-25 13:12:57 +00:00
|
|
|
if (i < 0)
|
2013-06-20 14:30:16 +00:00
|
|
|
err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
|
2001-03-09 20:05:06 +00:00
|
|
|
if (mdio.md_options & MD_AUTOUNIT)
|
2003-06-11 06:38:24 +00:00
|
|
|
printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
|
2001-03-09 20:05:06 +00:00
|
|
|
} else if (action == DETACH) {
|
2000-12-31 13:03:42 +00:00
|
|
|
if (mdio.md_options & MD_AUTOUNIT)
|
2012-01-27 11:48:44 +00:00
|
|
|
errx(1, "-d requires -u");
|
2000-12-28 20:57:57 +00:00
|
|
|
i = ioctl(fd, MDIOCDETACH, &mdio);
|
2001-02-25 13:12:57 +00:00
|
|
|
if (i < 0)
|
2013-06-20 14:30:16 +00:00
|
|
|
err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
|
2012-07-07 20:32:21 +00:00
|
|
|
} else if (action == RESIZE) {
|
|
|
|
if (mdio.md_options & MD_AUTOUNIT)
|
|
|
|
errx(1, "-r requires -u");
|
|
|
|
i = ioctl(fd, MDIOCRESIZE, &mdio);
|
|
|
|
if (i < 0)
|
2013-06-20 14:30:16 +00:00
|
|
|
err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
|
2012-01-27 11:48:44 +00:00
|
|
|
} else if (action == LIST) {
|
|
|
|
if (mdio.md_options & MD_AUTOUNIT) {
|
|
|
|
/*
|
|
|
|
* Listing all devices. This is why we pass NULL
|
|
|
|
* together with OPT_LIST.
|
|
|
|
*/
|
2013-06-20 17:01:02 +00:00
|
|
|
return (md_list(NULL, OPT_LIST | vflag, fflag));
|
2012-01-27 11:48:44 +00:00
|
|
|
} else
|
2013-06-20 14:30:16 +00:00
|
|
|
return (md_query(uflag, vflag, fflag));
|
2001-03-09 20:05:06 +00:00
|
|
|
} else
|
|
|
|
usage();
|
2012-01-27 11:48:44 +00:00
|
|
|
close(fd);
|
2001-02-25 13:12:57 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-04-29 22:40:11 +00:00
|
|
|
static void
|
|
|
|
md_set_file(const char *fn)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (realpath(fn, mdio.md_file) == NULL)
|
|
|
|
err(1, "could not find full path for %s", fn);
|
|
|
|
fd = open(mdio.md_file, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
err(1, "could not open %s", fn);
|
|
|
|
if (fstat(fd, &sb) == -1)
|
|
|
|
err(1, "could not stat %s", fn);
|
|
|
|
if (!S_ISREG(sb.st_mode))
|
|
|
|
errx(1, "%s is not a regular file", fn);
|
|
|
|
if (mdio.md_mediasize == 0)
|
|
|
|
mdio.md_mediasize = sb.st_size;
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2006-03-26 23:21:11 +00:00
|
|
|
/*
|
|
|
|
* Lists md(4) disks. Is used also as a query routine, since it handles XML
|
|
|
|
* interface. 'units' can be NULL for listing memory disks. It might be
|
|
|
|
* coma-separated string containing md(4) disk names. 'opt' distinguished
|
|
|
|
* between list and query mode.
|
|
|
|
*/
|
2005-12-22 10:32:11 +00:00
|
|
|
static int
|
2013-06-20 14:30:16 +00:00
|
|
|
md_list(const char *units, int opt, const char *fflag)
|
2006-03-26 23:21:11 +00:00
|
|
|
{
|
|
|
|
struct gmesh gm;
|
|
|
|
struct gprovider *pp;
|
|
|
|
struct gconf *gc;
|
|
|
|
struct gident *gid;
|
2006-03-27 05:33:35 +00:00
|
|
|
struct devstat *gsp;
|
2006-03-26 23:21:11 +00:00
|
|
|
struct ggeom *gg;
|
|
|
|
struct gclass *gcl;
|
|
|
|
void *sq;
|
2013-06-20 17:01:02 +00:00
|
|
|
int retcode, ffound, ufound;
|
2006-03-26 23:21:11 +00:00
|
|
|
char *type, *file, *length;
|
|
|
|
|
|
|
|
type = file = length = NULL;
|
|
|
|
|
|
|
|
retcode = geom_gettree(&gm);
|
|
|
|
if (retcode != 0)
|
|
|
|
return (-1);
|
|
|
|
retcode = geom_stats_open();
|
|
|
|
if (retcode != 0)
|
|
|
|
return (-1);
|
|
|
|
sq = geom_stats_snapshot_get();
|
|
|
|
if (sq == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2013-06-20 17:01:02 +00:00
|
|
|
ffound = ufound = 0;
|
2006-03-26 23:21:11 +00:00
|
|
|
while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
|
|
|
|
gid = geom_lookupid(&gm, gsp->id);
|
|
|
|
if (gid == NULL)
|
|
|
|
continue;
|
|
|
|
if (gid->lg_what == ISPROVIDER) {
|
|
|
|
pp = gid->lg_ptr;
|
|
|
|
gg = pp->lg_geom;
|
|
|
|
gcl = gg->lg_class;
|
|
|
|
if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
|
|
|
|
continue;
|
|
|
|
if ((opt & OPT_UNIT) && (units != NULL)) {
|
|
|
|
retcode = md_find(units, pp->lg_name);
|
|
|
|
if (retcode != 1)
|
|
|
|
continue;
|
2007-11-16 17:40:24 +00:00
|
|
|
else
|
2013-06-20 17:01:02 +00:00
|
|
|
ufound = 1;
|
2006-03-26 23:21:11 +00:00
|
|
|
}
|
|
|
|
gc = &pp->lg_config;
|
2013-06-20 14:30:16 +00:00
|
|
|
type = geom_config_get(gc, "type");
|
2017-02-19 16:08:58 +00:00
|
|
|
if (type != NULL && (strcmp(type, "vnode") == 0 ||
|
|
|
|
strcmp(type, "preload") == 0)) {
|
2013-06-20 14:30:16 +00:00
|
|
|
file = geom_config_get(gc, "file");
|
|
|
|
if (fflag != NULL &&
|
|
|
|
strcmp(fflag, file) != 0)
|
|
|
|
continue;
|
2013-06-20 17:01:02 +00:00
|
|
|
else
|
|
|
|
ffound = 1;
|
2013-07-02 19:42:47 +00:00
|
|
|
} else if (fflag != NULL)
|
|
|
|
continue;
|
2013-06-20 14:30:16 +00:00
|
|
|
if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
|
2011-02-14 09:58:47 +00:00
|
|
|
printf("%s", pp->lg_name + 2);
|
|
|
|
else
|
|
|
|
printf("%s", pp->lg_name);
|
|
|
|
|
2013-06-20 14:30:16 +00:00
|
|
|
if (opt & OPT_VERBOSE ||
|
|
|
|
((opt & OPT_UNIT) && fflag == NULL)) {
|
2006-03-26 23:21:11 +00:00
|
|
|
length = geom_config_get(gc, "length");
|
|
|
|
printf("\t%s\t", type);
|
2010-01-22 08:51:58 +00:00
|
|
|
if (length != NULL)
|
|
|
|
md_prthumanval(length);
|
2006-03-26 23:21:11 +00:00
|
|
|
if (file != NULL) {
|
|
|
|
printf("\t%s", file);
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opt |= OPT_DONE;
|
2008-06-21 15:04:42 +00:00
|
|
|
if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
|
2006-03-26 23:21:11 +00:00
|
|
|
printf(" ");
|
|
|
|
else
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2008-06-21 15:04:42 +00:00
|
|
|
if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
|
2006-03-26 23:21:11 +00:00
|
|
|
printf("\n");
|
|
|
|
/* XXX: Check if it's enough to clean everything. */
|
|
|
|
geom_stats_snapshot_free(sq);
|
Return 0 if:
1. "-u N" specified, no -f, and mdN found,
2. no -u, "-f /pathname" specified, and mdN associated with /pathname found,
3. "-u N" specified, "-f /pathname" specified, and both of them found,
4. "-l" specified and no -f,
5. "-l" specified, "-f /pathname" specified, and /pathname found.
otherwise return -1.
Spotted by: Julian H. Stacey
2013-10-24 01:06:44 +00:00
|
|
|
if (opt & OPT_UNIT) {
|
|
|
|
if (((fflag == NULL) && ufound) ||
|
|
|
|
((fflag == NULL) && (units != NULL) && ufound) ||
|
|
|
|
((fflag != NULL) && ffound) ||
|
|
|
|
((fflag != NULL) && (units != NULL) && ufound && ffound))
|
|
|
|
return (0);
|
|
|
|
} else if (opt & OPT_LIST) {
|
|
|
|
if ((fflag == NULL) ||
|
|
|
|
((fflag != NULL) && ffound))
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (-1);
|
2006-03-26 23:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns value of 'name' from gconfig structure.
|
|
|
|
*/
|
|
|
|
static char *
|
2010-01-22 08:51:58 +00:00
|
|
|
geom_config_get(struct gconf *g, const char *name)
|
2005-12-22 10:32:11 +00:00
|
|
|
{
|
2006-03-26 23:21:11 +00:00
|
|
|
struct gconfig *gce;
|
|
|
|
|
|
|
|
LIST_FOREACH(gce, g, lg_config) {
|
|
|
|
if (strcmp(gce->lg_name, name) == 0)
|
|
|
|
return (gce->lg_val);
|
|
|
|
}
|
|
|
|
return (NULL);
|
2005-12-22 10:32:11 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 23:21:11 +00:00
|
|
|
/*
|
|
|
|
* List is comma separated list of MD disks. name is a
|
|
|
|
* device name we look for. Returns 1 if found and 0
|
|
|
|
* otherwise.
|
|
|
|
*/
|
2006-03-26 22:52:36 +00:00
|
|
|
static int
|
2013-06-20 14:30:16 +00:00
|
|
|
md_find(const char *list, const char *name)
|
2001-02-25 13:12:57 +00:00
|
|
|
{
|
2006-03-26 23:21:11 +00:00
|
|
|
int ret;
|
2013-06-21 01:47:22 +00:00
|
|
|
char num[PATH_MAX];
|
2006-03-26 23:21:11 +00:00
|
|
|
char *ptr, *p, *u;
|
2001-02-25 13:12:57 +00:00
|
|
|
|
2006-03-26 23:21:11 +00:00
|
|
|
ret = 0;
|
|
|
|
ptr = strdup(list);
|
|
|
|
if (ptr == NULL)
|
|
|
|
return (-1);
|
|
|
|
for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
|
2013-06-21 01:47:22 +00:00
|
|
|
if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
|
|
|
|
u += sizeof(_PATH_DEV) - 1;
|
2006-03-26 23:21:11 +00:00
|
|
|
/* Just in case user specified number instead of full name */
|
2013-06-21 01:47:22 +00:00
|
|
|
snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
|
2006-03-26 23:21:11 +00:00
|
|
|
if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
2001-02-25 13:12:57 +00:00
|
|
|
}
|
2006-03-26 23:21:11 +00:00
|
|
|
free(ptr);
|
|
|
|
return (ret);
|
2001-02-25 13:12:57 +00:00
|
|
|
}
|
|
|
|
|
2004-09-16 21:32:13 +00:00
|
|
|
static void
|
2006-03-26 23:21:11 +00:00
|
|
|
md_prthumanval(char *length)
|
2004-09-16 21:32:13 +00:00
|
|
|
{
|
|
|
|
char buf[6];
|
2010-01-18 14:07:41 +00:00
|
|
|
uintmax_t bytes;
|
2006-03-26 23:21:11 +00:00
|
|
|
char *endptr;
|
2004-09-16 21:32:13 +00:00
|
|
|
|
2010-01-18 14:07:41 +00:00
|
|
|
errno = 0;
|
|
|
|
bytes = strtoumax(length, &endptr, 10);
|
|
|
|
if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
|
2006-03-26 23:21:11 +00:00
|
|
|
return;
|
2010-01-18 14:07:41 +00:00
|
|
|
humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
|
|
|
|
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
2004-09-16 21:32:13 +00:00
|
|
|
(void)printf("%6s", buf);
|
|
|
|
}
|
|
|
|
|
2012-03-14 10:10:15 +00:00
|
|
|
static int
|
2013-06-20 14:30:16 +00:00
|
|
|
md_query(const char *name, const int opt, const char *fflag)
|
2001-02-25 13:12:57 +00:00
|
|
|
{
|
2012-01-27 11:48:44 +00:00
|
|
|
|
2013-06-20 14:30:16 +00:00
|
|
|
return (md_list(name, opt | OPT_UNIT, fflag));
|
2000-12-28 20:57:57 +00:00
|
|
|
}
|