2000-12-28 21:57:57 +01:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2005-08-30 18:45:32 +02:00
|
|
|
#include <errno.h>
|
2000-12-28 21:57:57 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2004-09-16 23:32:13 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <libutil.h>
|
2000-12-28 21:57:57 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <err.h>
|
2004-09-16 23:32:13 +02:00
|
|
|
|
2000-12-28 21:57:57 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/param.h>
|
2001-02-24 17:26:41 +01:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/linker.h>
|
2000-12-28 21:57:57 +01:00
|
|
|
#include <sys/mdioctl.h>
|
2004-09-16 23:32:13 +02:00
|
|
|
#include <sys/stat.h>
|
2001-02-25 14:12:57 +01:00
|
|
|
|
|
|
|
int list(const int);
|
2001-08-07 21:27:46 +02:00
|
|
|
void mdmaybeload(void);
|
2001-02-25 14:12:57 +01:00
|
|
|
int query(const int, const int);
|
2001-08-07 21:27:46 +02:00
|
|
|
void usage(void);
|
2000-12-28 21:57:57 +01:00
|
|
|
|
|
|
|
struct md_ioctl mdio;
|
|
|
|
|
2001-02-25 14:12:57 +01:00
|
|
|
enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
|
2000-12-28 21:57:57 +01:00
|
|
|
|
2003-06-11 08:38:24 +02:00
|
|
|
int nflag;
|
|
|
|
|
2000-12-31 12:20:49 +01:00
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2004-11-13 18:08:52 +01:00
|
|
|
fprintf(stderr,
|
2005-02-10 10:19:34 +01:00
|
|
|
"usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
|
2004-11-13 18:08:52 +01:00
|
|
|
" [-s size] [-S sectorsize] [-u unit]\n"
|
|
|
|
" [-x sectors/track] [-y heads/cyl]\n"
|
|
|
|
" mdconfig -d -u unit\n"
|
|
|
|
" mdconfig -l [-n] [-u unit]\n");
|
2000-12-31 12:20:49 +01:00
|
|
|
fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n");
|
2001-02-26 16:31:47 +01:00
|
|
|
fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
|
2005-03-01 15:56:49 +01:00
|
|
|
fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
|
|
|
|
fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n");
|
|
|
|
fprintf(stderr, "\t\t %%dt (TB)\n");
|
2000-12-31 12:20:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-12-28 21:57:57 +01:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch, fd, i;
|
2000-12-31 12:20:49 +01:00
|
|
|
char *p;
|
|
|
|
int cmdline = 0;
|
2000-12-28 21:57:57 +01:00
|
|
|
|
2004-09-16 23:32:13 +02:00
|
|
|
bzero(&mdio, sizeof(mdio));
|
2004-12-27 18:20:06 +01:00
|
|
|
mdio.md_file = malloc(PATH_MAX);
|
|
|
|
if (mdio.md_file == NULL)
|
|
|
|
err(1, "could not allocate memory");
|
|
|
|
bzero(mdio.md_file, PATH_MAX);
|
2000-12-28 21:57:57 +01:00
|
|
|
for (;;) {
|
2003-06-11 08:38:24 +02:00
|
|
|
ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:");
|
2000-12-28 21:57:57 +01:00
|
|
|
if (ch == -1)
|
|
|
|
break;
|
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
2000-12-31 12:20:49 +01:00
|
|
|
if (cmdline != 0)
|
|
|
|
usage();
|
2000-12-28 21:57:57 +01:00
|
|
|
action = ATTACH;
|
2000-12-31 12:20:49 +01:00
|
|
|
cmdline = 1;
|
2000-12-28 21:57:57 +01:00
|
|
|
break;
|
|
|
|
case 'd':
|
2000-12-31 12:20:49 +01:00
|
|
|
if (cmdline != 0)
|
|
|
|
usage();
|
2000-12-28 21:57:57 +01:00
|
|
|
action = DETACH;
|
2000-12-31 14:03:42 +01:00
|
|
|
mdio.md_options = MD_AUTOUNIT;
|
|
|
|
cmdline = 3;
|
2000-12-31 12:20:49 +01:00
|
|
|
break;
|
2001-02-25 14:12:57 +01:00
|
|
|
case 'l':
|
|
|
|
if (cmdline != 0)
|
|
|
|
usage();
|
|
|
|
action = LIST;
|
|
|
|
mdio.md_options = MD_AUTOUNIT;
|
|
|
|
cmdline = 3;
|
|
|
|
break;
|
2003-06-11 08:38:24 +02:00
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
2000-12-31 12:20:49 +01:00
|
|
|
case 't':
|
|
|
|
if (cmdline != 1)
|
|
|
|
usage();
|
|
|
|
if (!strcmp(optarg, "malloc")) {
|
|
|
|
mdio.md_type = MD_MALLOC;
|
|
|
|
mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
|
|
|
|
} else if (!strcmp(optarg, "preload")) {
|
|
|
|
mdio.md_type = MD_PRELOAD;
|
|
|
|
mdio.md_options = 0;
|
|
|
|
} else if (!strcmp(optarg, "vnode")) {
|
|
|
|
mdio.md_type = MD_VNODE;
|
|
|
|
mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
|
|
|
|
} else if (!strcmp(optarg, "swap")) {
|
|
|
|
mdio.md_type = MD_SWAP;
|
|
|
|
mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
cmdline=2;
|
2000-12-28 21:57:57 +01:00
|
|
|
break;
|
|
|
|
case 'f':
|
2001-03-09 22:15:08 +01:00
|
|
|
if (cmdline != 1 && cmdline != 2)
|
2000-12-31 12:20:49 +01:00
|
|
|
usage();
|
2001-03-09 22:15:08 +01:00
|
|
|
if (cmdline == 1) {
|
|
|
|
/* Imply ``-t vnode'' */
|
|
|
|
mdio.md_type = MD_VNODE;
|
|
|
|
mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
|
2003-04-01 17:23:55 +02:00
|
|
|
cmdline = 2;
|
2001-03-09 22:15:08 +01:00
|
|
|
}
|
2004-11-06 14:07:02 +01:00
|
|
|
if (realpath(optarg, mdio.md_file) == NULL) {
|
|
|
|
err(1, "could not find full path for %s",
|
|
|
|
optarg);
|
|
|
|
}
|
|
|
|
fd = open(mdio.md_file, O_RDONLY);
|
2004-07-25 10:17:23 +02:00
|
|
|
if (fd < 0)
|
|
|
|
err(1, "could not open %s", optarg);
|
2004-09-16 23:32:13 +02:00
|
|
|
else if (mdio.md_mediasize == 0) {
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (fstat(fd, &sb) == -1)
|
|
|
|
err(1, "could not stat %s", optarg);
|
|
|
|
mdio.md_mediasize = sb.st_size;
|
|
|
|
}
|
2004-07-25 10:17:23 +02:00
|
|
|
close(fd);
|
2000-12-28 21:57:57 +01:00
|
|
|
break;
|
|
|
|
case 'o':
|
2000-12-31 12:20:49 +01:00
|
|
|
if (cmdline != 2)
|
|
|
|
usage();
|
2004-03-10 21:41:09 +01: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 21:57:57 +01:00
|
|
|
mdio.md_options |= MD_CLUSTER;
|
|
|
|
else if (!strcmp(optarg, "nocluster"))
|
|
|
|
mdio.md_options &= ~MD_CLUSTER;
|
2000-12-31 12:20:49 +01:00
|
|
|
else if (!strcmp(optarg, "compress"))
|
|
|
|
mdio.md_options |= MD_COMPRESS;
|
|
|
|
else if (!strcmp(optarg, "nocompress"))
|
|
|
|
mdio.md_options &= ~MD_COMPRESS;
|
2001-08-07 21:23:16 +02:00
|
|
|
else if (!strcmp(optarg, "force"))
|
|
|
|
mdio.md_options |= MD_FORCE;
|
|
|
|
else if (!strcmp(optarg, "noforce"))
|
|
|
|
mdio.md_options &= ~MD_FORCE;
|
2004-09-08 22:28:29 +02:00
|
|
|
else if (!strcmp(optarg, "readonly"))
|
|
|
|
mdio.md_options |= MD_READONLY;
|
|
|
|
else if (!strcmp(optarg, "noreadonly"))
|
|
|
|
mdio.md_options &= ~MD_READONLY;
|
2000-12-28 21:57:57 +01:00
|
|
|
else if (!strcmp(optarg, "reserve"))
|
|
|
|
mdio.md_options |= MD_RESERVE;
|
|
|
|
else if (!strcmp(optarg, "noreserve"))
|
|
|
|
mdio.md_options &= ~MD_RESERVE;
|
|
|
|
else
|
2004-09-08 22:28:29 +02:00
|
|
|
errx(1, "Unknown option: %s.", optarg);
|
2000-12-28 21:57:57 +01:00
|
|
|
break;
|
2003-03-03 14:05:00 +01:00
|
|
|
case 'S':
|
|
|
|
if (cmdline != 2)
|
|
|
|
usage();
|
2004-09-16 23:32:13 +02:00
|
|
|
mdio.md_sectorsize = strtoul(optarg, &p, 0);
|
2003-03-03 14:05:00 +01:00
|
|
|
break;
|
2000-12-28 21:57:57 +01:00
|
|
|
case 's':
|
2000-12-31 12:20:49 +01:00
|
|
|
if (cmdline != 2)
|
|
|
|
usage();
|
2004-09-16 23:32:13 +02:00
|
|
|
mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
|
2000-12-31 12:20:49 +01:00
|
|
|
if (p == NULL || *p == '\0')
|
2004-09-16 23:32:13 +02:00
|
|
|
mdio.md_mediasize *= DEV_BSIZE;
|
2005-01-22 21:02:01 +01:00
|
|
|
else if (*p == 'b' || *p == 'B')
|
|
|
|
; /* do nothing */
|
2000-12-31 12:20:49 +01:00
|
|
|
else if (*p == 'k' || *p == 'K')
|
2004-09-16 23:32:13 +02:00
|
|
|
mdio.md_mediasize <<= 10;
|
2000-12-31 12:20:49 +01:00
|
|
|
else if (*p == 'm' || *p == 'M')
|
2004-09-16 23:32:13 +02:00
|
|
|
mdio.md_mediasize <<= 20;
|
2000-12-31 12:20:49 +01:00
|
|
|
else if (*p == 'g' || *p == 'G')
|
2004-09-16 23:32:13 +02:00
|
|
|
mdio.md_mediasize <<= 30;
|
|
|
|
else if (*p == 't' || *p == 'T') {
|
|
|
|
mdio.md_mediasize <<= 30;
|
|
|
|
mdio.md_mediasize <<= 10;
|
|
|
|
} else
|
2000-12-31 12:20:49 +01:00
|
|
|
errx(1, "Unknown suffix on -s argument");
|
2000-12-28 21:57:57 +01:00
|
|
|
break;
|
|
|
|
case 'u':
|
2000-12-31 14:03:42 +01:00
|
|
|
if (cmdline != 2 && cmdline != 3)
|
2000-12-31 12:20:49 +01:00
|
|
|
usage();
|
2001-01-31 09:41:18 +01:00
|
|
|
if (!strncmp(optarg, "/dev/", 5))
|
|
|
|
optarg += 5;
|
2001-02-25 14:12:57 +01:00
|
|
|
if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
|
2001-06-21 03:39:01 +02:00
|
|
|
optarg += sizeof(MD_NAME) - 1;
|
2001-06-21 03:36:09 +02:00
|
|
|
mdio.md_unit = strtoul(optarg, &p, 0);
|
2003-07-11 07:47:05 +02:00
|
|
|
if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
|
2001-06-21 03:36:09 +02:00
|
|
|
errx(1, "bad unit: %s", optarg);
|
2000-12-28 21:57:57 +01:00
|
|
|
mdio.md_options &= ~MD_AUTOUNIT;
|
|
|
|
break;
|
2003-04-09 13:59:29 +02:00
|
|
|
case 'x':
|
|
|
|
if (cmdline != 2)
|
|
|
|
usage();
|
|
|
|
mdio.md_fwsectors = strtoul(optarg, &p, 0);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
if (cmdline != 2)
|
|
|
|
usage();
|
|
|
|
mdio.md_fwheads = strtoul(optarg, &p, 0);
|
|
|
|
break;
|
2000-12-28 21:57:57 +01:00
|
|
|
default:
|
2000-12-31 12:20:49 +01:00
|
|
|
usage();
|
2000-12-28 21:57:57 +01:00
|
|
|
}
|
|
|
|
}
|
2001-12-20 07:38:21 +01:00
|
|
|
mdio.md_version = MDIOVERSION;
|
2000-12-28 21:57:57 +01:00
|
|
|
|
2001-02-24 17:26:41 +01:00
|
|
|
mdmaybeload();
|
2001-02-25 14:12:57 +01:00
|
|
|
fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
|
2000-12-28 21:57:57 +01:00
|
|
|
if (fd < 0)
|
2001-02-25 14:12:57 +01:00
|
|
|
err(1, "open(/dev/%s)", MDCTL_NAME);
|
2001-06-21 03:36:09 +02:00
|
|
|
if (cmdline == 2
|
|
|
|
&& (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
|
2004-09-16 23:32:13 +02:00
|
|
|
if (mdio.md_mediasize == 0)
|
2001-06-21 03:36:09 +02:00
|
|
|
errx(1, "must specify -s for -t malloc or -t swap");
|
2003-04-01 17:23:55 +02:00
|
|
|
if (cmdline == 2 && mdio.md_type == MD_VNODE)
|
2004-12-27 18:20:06 +01:00
|
|
|
if (mdio.md_file[0] == '\0')
|
2003-04-01 17:23:55 +02:00
|
|
|
errx(1, "must specify -f for -t vnode");
|
2005-08-30 18:45:32 +02:00
|
|
|
if (mdio.md_type == MD_VNODE &&
|
|
|
|
(mdio.md_options & MD_READONLY) == 0) {
|
|
|
|
if (access(mdio.md_file, W_OK) < 0 &&
|
|
|
|
(errno == EACCES || errno == EPERM || errno == EROFS)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: opening backing store: %s readonly\n",
|
|
|
|
mdio.md_file);
|
|
|
|
mdio.md_options |= MD_READONLY;
|
|
|
|
}
|
|
|
|
}
|
2001-02-25 14:12:57 +01:00
|
|
|
if (action == LIST) {
|
|
|
|
if (mdio.md_options & MD_AUTOUNIT)
|
|
|
|
list(fd);
|
|
|
|
else
|
|
|
|
query(fd, mdio.md_unit);
|
|
|
|
} else if (action == ATTACH) {
|
2003-04-01 17:23:55 +02:00
|
|
|
if (cmdline < 2)
|
|
|
|
usage();
|
2000-12-28 21:57:57 +01:00
|
|
|
i = ioctl(fd, MDIOCATTACH, &mdio);
|
2001-02-25 14:12:57 +01:00
|
|
|
if (i < 0)
|
|
|
|
err(1, "ioctl(/dev/%s)", MDCTL_NAME);
|
2001-03-09 21:05:06 +01:00
|
|
|
if (mdio.md_options & MD_AUTOUNIT)
|
2003-06-11 08:38:24 +02:00
|
|
|
printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
|
2001-03-09 21:05:06 +01:00
|
|
|
} else if (action == DETACH) {
|
2000-12-31 14:03:42 +01:00
|
|
|
if (mdio.md_options & MD_AUTOUNIT)
|
|
|
|
usage();
|
2000-12-28 21:57:57 +01:00
|
|
|
i = ioctl(fd, MDIOCDETACH, &mdio);
|
2001-02-25 14:12:57 +01:00
|
|
|
if (i < 0)
|
|
|
|
err(1, "ioctl(/dev/%s)", MDCTL_NAME);
|
2001-03-09 21:05:06 +01:00
|
|
|
} else
|
|
|
|
usage();
|
2001-02-25 14:12:57 +01:00
|
|
|
close (fd);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
list(const int fd)
|
|
|
|
{
|
2001-06-21 04:10:26 +02:00
|
|
|
int unit;
|
2001-02-25 14:12:57 +01:00
|
|
|
|
2003-01-27 09:01:08 +01:00
|
|
|
if (ioctl(fd, MDIOCLIST, &mdio) < 0)
|
|
|
|
err(1, "ioctl(/dev/%s)", MDCTL_NAME);
|
|
|
|
for (unit = 0; unit < mdio.md_pad[0] && unit < MDNPAD - 1; unit++) {
|
2003-06-11 08:38:24 +02:00
|
|
|
printf("%s%s%d", unit > 0 ? " " : "",
|
|
|
|
nflag ? "" : MD_NAME, mdio.md_pad[unit + 1]);
|
2001-02-25 14:12:57 +01:00
|
|
|
}
|
2003-01-27 09:01:08 +01:00
|
|
|
if (mdio.md_pad[0] - unit > 0)
|
|
|
|
printf(" ... %d more", mdio.md_pad[0] - unit);
|
2004-11-06 10:56:27 +01:00
|
|
|
if (unit > 0)
|
|
|
|
printf("\n");
|
2001-02-25 14:12:57 +01:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-09-16 23:32:13 +02:00
|
|
|
static void
|
|
|
|
prthumanval(int64_t bytes)
|
|
|
|
{
|
|
|
|
char buf[6];
|
|
|
|
|
|
|
|
humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
|
|
|
|
bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
|
|
|
|
(void)printf("%6s", buf);
|
|
|
|
}
|
|
|
|
|
2001-02-25 14:12:57 +01:00
|
|
|
int
|
|
|
|
query(const int fd, const int unit)
|
|
|
|
{
|
|
|
|
|
2001-12-20 07:38:21 +01:00
|
|
|
mdio.md_version = MDIOVERSION;
|
2001-02-25 14:12:57 +01:00
|
|
|
mdio.md_unit = unit;
|
|
|
|
|
|
|
|
if (ioctl(fd, MDIOCQUERY, &mdio) < 0)
|
|
|
|
err(1, "ioctl(/dev/%s)", MDCTL_NAME);
|
|
|
|
|
2004-09-16 23:32:13 +02:00
|
|
|
(void)printf("%s%d\t", MD_NAME, mdio.md_unit);
|
2001-02-25 14:12:57 +01:00
|
|
|
switch (mdio.md_type) {
|
|
|
|
case MD_MALLOC:
|
2004-09-16 23:32:13 +02:00
|
|
|
(void)printf("malloc");
|
2001-02-25 14:12:57 +01:00
|
|
|
break;
|
|
|
|
case MD_PRELOAD:
|
2004-09-16 23:32:13 +02:00
|
|
|
(void)printf("preload");
|
2001-02-25 14:12:57 +01:00
|
|
|
break;
|
|
|
|
case MD_SWAP:
|
2004-09-16 23:32:13 +02:00
|
|
|
(void)printf("swap");
|
2001-02-25 14:12:57 +01:00
|
|
|
break;
|
|
|
|
case MD_VNODE:
|
2004-09-16 23:32:13 +02:00
|
|
|
(void)printf("vnode");
|
2001-02-25 14:12:57 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-09-16 23:32:13 +02:00
|
|
|
printf("\t");
|
|
|
|
prthumanval(mdio.md_mediasize);
|
2004-11-06 14:07:02 +01:00
|
|
|
if (mdio.md_type == MD_VNODE)
|
|
|
|
printf("\t%s", mdio.md_file);
|
2004-09-16 23:32:13 +02:00
|
|
|
printf("\n");
|
2001-02-25 14:12:57 +01:00
|
|
|
|
2000-12-28 21:57:57 +01:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-02-24 17:26:41 +01:00
|
|
|
void
|
|
|
|
mdmaybeload(void)
|
|
|
|
{
|
2005-11-11 12:31:23 +01:00
|
|
|
char name1[64], name2[64];
|
2001-02-24 17:26:41 +01:00
|
|
|
|
2005-11-11 12:31:23 +01:00
|
|
|
snprintf(name1, sizeof(name1), "g_%s", MD_NAME);
|
|
|
|
snprintf(name2, sizeof(name2), "geom_%s", MD_NAME);
|
|
|
|
if (modfind(name1) == -1) {
|
|
|
|
/* Not present in kernel, try loading it. */
|
|
|
|
if (kldload(name2) == -1 || modfind(name1) == -1) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"%s module not available!", name2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-24 17:26:41 +01:00
|
|
|
}
|