xref: /freebsd/sbin/mdconfig/mdconfig.c (revision c27a89543317fff77f5fde1423b4c7053303075e)
170d586c0SPoul-Henning Kamp /*
270d586c0SPoul-Henning Kamp  * ----------------------------------------------------------------------------
370d586c0SPoul-Henning Kamp  * "THE BEER-WARE LICENSE" (Revision 42):
470d586c0SPoul-Henning Kamp  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
570d586c0SPoul-Henning Kamp  * can do whatever you want with this stuff. If we meet some day, and you think
670d586c0SPoul-Henning Kamp  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
770d586c0SPoul-Henning Kamp  * ----------------------------------------------------------------------------
870d586c0SPoul-Henning Kamp  *
970d586c0SPoul-Henning Kamp  * $FreeBSD$
1070d586c0SPoul-Henning Kamp  *
1170d586c0SPoul-Henning Kamp  */
1270d586c0SPoul-Henning Kamp #include <sys/param.h>
13c27a8954SWojciech A. Koszek #include <sys/devicestat.h>
144bfd989fSWojciech A. Koszek #include <sys/ioctl.h>
1557e9624eSPoul-Henning Kamp #include <sys/linker.h>
1670d586c0SPoul-Henning Kamp #include <sys/mdioctl.h>
174bfd989fSWojciech A. Koszek #include <sys/module.h>
18c27a8954SWojciech A. Koszek #include <sys/resource.h>
19b830359bSPawel Jakub Dawidek #include <sys/stat.h>
20174b5e9aSPoul-Henning Kamp 
214bfd989fSWojciech A. Koszek #include <assert.h>
22c27a8954SWojciech A. Koszek #include <devstat.h>
234bfd989fSWojciech A. Koszek #include <err.h>
244bfd989fSWojciech A. Koszek #include <errno.h>
254bfd989fSWojciech A. Koszek #include <fcntl.h>
264bfd989fSWojciech A. Koszek #include <inttypes.h>
27c27a8954SWojciech A. Koszek #include <libgeom.h>
284bfd989fSWojciech A. Koszek #include <libutil.h>
29c27a8954SWojciech A. Koszek #include <stdarg.h>
304bfd989fSWojciech A. Koszek #include <stdio.h>
314bfd989fSWojciech A. Koszek #include <stdlib.h>
324bfd989fSWojciech A. Koszek #include <string.h>
334bfd989fSWojciech A. Koszek #include <unistd.h>
3470d586c0SPoul-Henning Kamp 
3570d586c0SPoul-Henning Kamp 
364bfd989fSWojciech A. Koszek static struct md_ioctl mdio;
374bfd989fSWojciech A. Koszek static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
384bfd989fSWojciech A. Koszek static int nflag;
3970d586c0SPoul-Henning Kamp 
40c27a8954SWojciech A. Koszek static void usage(void);
41c27a8954SWojciech A. Koszek static int md_find(char *, const char *);
42c27a8954SWojciech A. Koszek static int md_query(char *name);
43c27a8954SWojciech A. Koszek static int md_list(char *units, int opt);
44c27a8954SWojciech A. Koszek static char *geom_config_get(struct gconf *g, char *name);
45c27a8954SWojciech A. Koszek static void geom_config_dump(struct gconf *g);
46c27a8954SWojciech A. Koszek static void md_prthumanval(char *length);
47c27a8954SWojciech A. Koszek 
48c27a8954SWojciech A. Koszek #define OPT_VERBOSE	0x01
49c27a8954SWojciech A. Koszek #define OPT_UNIT	0x02
50c27a8954SWojciech A. Koszek #define OPT_DONE	0x04
51c27a8954SWojciech A. Koszek #define OPT_LIST	0x10
52c27a8954SWojciech A. Koszek 
53c27a8954SWojciech A. Koszek #define CLASS_NAME_MD	"MD"
54c27a8954SWojciech A. Koszek 
554bfd989fSWojciech A. Koszek static void
56c2ef0b73SPoul-Henning Kamp usage()
57c2ef0b73SPoul-Henning Kamp {
5878bb1162SRuslan Ermilov 	fprintf(stderr,
5978bb1162SRuslan Ermilov "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
6078bb1162SRuslan Ermilov "                [-s size] [-S sectorsize] [-u unit]\n"
6178bb1162SRuslan Ermilov "                [-x sectors/track] [-y heads/cyl]\n"
6278bb1162SRuslan Ermilov "       mdconfig -d -u unit\n"
6378bb1162SRuslan Ermilov "       mdconfig -l [-n] [-u unit]\n");
64c2ef0b73SPoul-Henning Kamp 	fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n");
653f6f9216SPoul-Henning Kamp 	fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
665d19b2f9SPawel Jakub Dawidek 	fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
675d19b2f9SPawel Jakub Dawidek 	fprintf(stderr, "\t\t       %%dk (kB), %%dm (MB), %%dg (GB) or\n");
685d19b2f9SPawel Jakub Dawidek 	fprintf(stderr, "\t\t       %%dt (TB)\n");
69c2ef0b73SPoul-Henning Kamp 	exit(1);
70c2ef0b73SPoul-Henning Kamp }
71c2ef0b73SPoul-Henning Kamp 
7270d586c0SPoul-Henning Kamp int
7370d586c0SPoul-Henning Kamp main(int argc, char **argv)
7470d586c0SPoul-Henning Kamp {
7570d586c0SPoul-Henning Kamp 	int ch, fd, i;
76c2ef0b73SPoul-Henning Kamp 	char *p;
77c2ef0b73SPoul-Henning Kamp 	int cmdline = 0;
78c27a8954SWojciech A. Koszek 	char *mdunit;
7970d586c0SPoul-Henning Kamp 
80b830359bSPawel Jakub Dawidek 	bzero(&mdio, sizeof(mdio));
8188b5b78dSPawel Jakub Dawidek 	mdio.md_file = malloc(PATH_MAX);
8288b5b78dSPawel Jakub Dawidek 	if (mdio.md_file == NULL)
8388b5b78dSPawel Jakub Dawidek 		err(1, "could not allocate memory");
8488b5b78dSPawel Jakub Dawidek 	bzero(mdio.md_file, PATH_MAX);
8570d586c0SPoul-Henning Kamp 	for (;;) {
86f79c46d3SRobert Watson 		ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:");
8770d586c0SPoul-Henning Kamp 		if (ch == -1)
8870d586c0SPoul-Henning Kamp 			break;
8970d586c0SPoul-Henning Kamp 		switch (ch) {
9070d586c0SPoul-Henning Kamp 		case 'a':
91c2ef0b73SPoul-Henning Kamp 			if (cmdline != 0)
92c2ef0b73SPoul-Henning Kamp 				usage();
9370d586c0SPoul-Henning Kamp 			action = ATTACH;
94c2ef0b73SPoul-Henning Kamp 			cmdline = 1;
9570d586c0SPoul-Henning Kamp 			break;
9670d586c0SPoul-Henning Kamp 		case 'd':
97c2ef0b73SPoul-Henning Kamp 			if (cmdline != 0)
98c2ef0b73SPoul-Henning Kamp 				usage();
9970d586c0SPoul-Henning Kamp 			action = DETACH;
1008f8def9eSPoul-Henning Kamp 			mdio.md_options = MD_AUTOUNIT;
1018f8def9eSPoul-Henning Kamp 			cmdline = 3;
102c2ef0b73SPoul-Henning Kamp 			break;
103174b5e9aSPoul-Henning Kamp 		case 'l':
104174b5e9aSPoul-Henning Kamp 			if (cmdline != 0)
105174b5e9aSPoul-Henning Kamp 				usage();
106174b5e9aSPoul-Henning Kamp 			action = LIST;
107174b5e9aSPoul-Henning Kamp 			mdio.md_options = MD_AUTOUNIT;
108174b5e9aSPoul-Henning Kamp 			cmdline = 3;
109174b5e9aSPoul-Henning Kamp 			break;
110f79c46d3SRobert Watson 		case 'n':
111f79c46d3SRobert Watson 			nflag = 1;
112f79c46d3SRobert Watson 			break;
113c2ef0b73SPoul-Henning Kamp 		case 't':
114c2ef0b73SPoul-Henning Kamp 			if (cmdline != 1)
115c2ef0b73SPoul-Henning Kamp 				usage();
116c2ef0b73SPoul-Henning Kamp 			if (!strcmp(optarg, "malloc")) {
117c2ef0b73SPoul-Henning Kamp 				mdio.md_type = MD_MALLOC;
118c2ef0b73SPoul-Henning Kamp 				mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
119c2ef0b73SPoul-Henning Kamp 			} else if (!strcmp(optarg, "preload")) {
120c2ef0b73SPoul-Henning Kamp 				mdio.md_type = MD_PRELOAD;
121c2ef0b73SPoul-Henning Kamp 				mdio.md_options = 0;
122c2ef0b73SPoul-Henning Kamp 			} else if (!strcmp(optarg, "vnode")) {
123c2ef0b73SPoul-Henning Kamp 				mdio.md_type = MD_VNODE;
124c2ef0b73SPoul-Henning Kamp 				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
125c2ef0b73SPoul-Henning Kamp 			} else if (!strcmp(optarg, "swap")) {
126c2ef0b73SPoul-Henning Kamp 				mdio.md_type = MD_SWAP;
127c2ef0b73SPoul-Henning Kamp 				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
128c2ef0b73SPoul-Henning Kamp 			} else {
129c2ef0b73SPoul-Henning Kamp 				usage();
130c2ef0b73SPoul-Henning Kamp 			}
131c2ef0b73SPoul-Henning Kamp 			cmdline=2;
13270d586c0SPoul-Henning Kamp 			break;
13370d586c0SPoul-Henning Kamp 		case 'f':
134ed23a390SMaxim Sobolev 			if (cmdline != 1 && cmdline != 2)
135c2ef0b73SPoul-Henning Kamp 				usage();
136ed23a390SMaxim Sobolev 			if (cmdline == 1) {
137ed23a390SMaxim Sobolev 				/* Imply ``-t vnode'' */
138ed23a390SMaxim Sobolev 				mdio.md_type = MD_VNODE;
139ed23a390SMaxim Sobolev 				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
140252bcf45SYaroslav Tykhiy 				cmdline = 2;
141ed23a390SMaxim Sobolev 			}
14261a6eb62SPawel Jakub Dawidek 			if (realpath(optarg, mdio.md_file) == NULL) {
14361a6eb62SPawel Jakub Dawidek 				err(1, "could not find full path for %s",
14461a6eb62SPawel Jakub Dawidek 				    optarg);
14561a6eb62SPawel Jakub Dawidek 			}
14661a6eb62SPawel Jakub Dawidek 			fd = open(mdio.md_file, O_RDONLY);
147e869d377SPoul-Henning Kamp 			if (fd < 0)
148e869d377SPoul-Henning Kamp 				err(1, "could not open %s", optarg);
149b830359bSPawel Jakub Dawidek 			else if (mdio.md_mediasize == 0) {
150b830359bSPawel Jakub Dawidek 				struct stat sb;
151b830359bSPawel Jakub Dawidek 
152b830359bSPawel Jakub Dawidek 				if (fstat(fd, &sb) == -1)
153b830359bSPawel Jakub Dawidek 					err(1, "could not stat %s", optarg);
154b830359bSPawel Jakub Dawidek 				mdio.md_mediasize = sb.st_size;
155b830359bSPawel Jakub Dawidek 			}
156e869d377SPoul-Henning Kamp 			close(fd);
15770d586c0SPoul-Henning Kamp 			break;
15870d586c0SPoul-Henning Kamp 		case 'o':
159c2ef0b73SPoul-Henning Kamp 			if (cmdline != 2)
160c2ef0b73SPoul-Henning Kamp 				usage();
1617a6b2b64SPoul-Henning Kamp 			if (!strcmp(optarg, "async"))
1627a6b2b64SPoul-Henning Kamp 				mdio.md_options |= MD_ASYNC;
1637a6b2b64SPoul-Henning Kamp 			else if (!strcmp(optarg, "noasync"))
1647a6b2b64SPoul-Henning Kamp 				mdio.md_options &= ~MD_ASYNC;
1657a6b2b64SPoul-Henning Kamp 			else if (!strcmp(optarg, "cluster"))
16670d586c0SPoul-Henning Kamp 				mdio.md_options |= MD_CLUSTER;
16770d586c0SPoul-Henning Kamp 			else if (!strcmp(optarg, "nocluster"))
16870d586c0SPoul-Henning Kamp 				mdio.md_options &= ~MD_CLUSTER;
169c2ef0b73SPoul-Henning Kamp 			else if (!strcmp(optarg, "compress"))
170c2ef0b73SPoul-Henning Kamp 				mdio.md_options |= MD_COMPRESS;
171c2ef0b73SPoul-Henning Kamp 			else if (!strcmp(optarg, "nocompress"))
172c2ef0b73SPoul-Henning Kamp 				mdio.md_options &= ~MD_COMPRESS;
17326a0ee75SDima Dorfman 			else if (!strcmp(optarg, "force"))
17426a0ee75SDima Dorfman 				mdio.md_options |= MD_FORCE;
17526a0ee75SDima Dorfman 			else if (!strcmp(optarg, "noforce"))
17626a0ee75SDima Dorfman 				mdio.md_options &= ~MD_FORCE;
177d31ba625SJohn-Mark Gurney 			else if (!strcmp(optarg, "readonly"))
178d31ba625SJohn-Mark Gurney 				mdio.md_options |= MD_READONLY;
179d31ba625SJohn-Mark Gurney 			else if (!strcmp(optarg, "noreadonly"))
180d31ba625SJohn-Mark Gurney 				mdio.md_options &= ~MD_READONLY;
18170d586c0SPoul-Henning Kamp 			else if (!strcmp(optarg, "reserve"))
18270d586c0SPoul-Henning Kamp 				mdio.md_options |= MD_RESERVE;
18370d586c0SPoul-Henning Kamp 			else if (!strcmp(optarg, "noreserve"))
18470d586c0SPoul-Henning Kamp 				mdio.md_options &= ~MD_RESERVE;
18570d586c0SPoul-Henning Kamp 			else
186d31ba625SJohn-Mark Gurney 				errx(1, "Unknown option: %s.", optarg);
18770d586c0SPoul-Henning Kamp 			break;
188ebe789d6SPoul-Henning Kamp 		case 'S':
189ebe789d6SPoul-Henning Kamp 			if (cmdline != 2)
190ebe789d6SPoul-Henning Kamp 				usage();
191b830359bSPawel Jakub Dawidek 			mdio.md_sectorsize = strtoul(optarg, &p, 0);
192ebe789d6SPoul-Henning Kamp 			break;
19370d586c0SPoul-Henning Kamp 		case 's':
194c2ef0b73SPoul-Henning Kamp 			if (cmdline != 2)
195c2ef0b73SPoul-Henning Kamp 				usage();
196b830359bSPawel Jakub Dawidek 			mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
197c2ef0b73SPoul-Henning Kamp 			if (p == NULL || *p == '\0')
198b830359bSPawel Jakub Dawidek 				mdio.md_mediasize *= DEV_BSIZE;
1990d79319aSPawel Jakub Dawidek 			else if (*p == 'b' || *p == 'B')
2000d79319aSPawel Jakub Dawidek 				; /* do nothing */
201c2ef0b73SPoul-Henning Kamp 			else if (*p == 'k' || *p == 'K')
202b830359bSPawel Jakub Dawidek 				mdio.md_mediasize <<= 10;
203c2ef0b73SPoul-Henning Kamp 			else if (*p == 'm' || *p == 'M')
204b830359bSPawel Jakub Dawidek 				mdio.md_mediasize <<= 20;
205c2ef0b73SPoul-Henning Kamp 			else if (*p == 'g' || *p == 'G')
206b830359bSPawel Jakub Dawidek 				mdio.md_mediasize <<= 30;
207b830359bSPawel Jakub Dawidek 			else if (*p == 't' || *p == 'T') {
208b830359bSPawel Jakub Dawidek 				mdio.md_mediasize <<= 30;
209b830359bSPawel Jakub Dawidek 				mdio.md_mediasize <<= 10;
210b830359bSPawel Jakub Dawidek 			} else
211c2ef0b73SPoul-Henning Kamp 				errx(1, "Unknown suffix on -s argument");
21270d586c0SPoul-Henning Kamp 			break;
21370d586c0SPoul-Henning Kamp 		case 'u':
2148f8def9eSPoul-Henning Kamp 			if (cmdline != 2 && cmdline != 3)
215c2ef0b73SPoul-Henning Kamp 				usage();
216c27a8954SWojciech A. Koszek 			mdunit = optarg;
21770d586c0SPoul-Henning Kamp 			mdio.md_options &= ~MD_AUTOUNIT;
21870d586c0SPoul-Henning Kamp 			break;
2194e8bfe14SPoul-Henning Kamp 		case 'x':
2204e8bfe14SPoul-Henning Kamp 			if (cmdline != 2)
2214e8bfe14SPoul-Henning Kamp 				usage();
2224e8bfe14SPoul-Henning Kamp 			mdio.md_fwsectors = strtoul(optarg, &p, 0);
2234e8bfe14SPoul-Henning Kamp 			break;
2244e8bfe14SPoul-Henning Kamp 		case 'y':
2254e8bfe14SPoul-Henning Kamp 			if (cmdline != 2)
2264e8bfe14SPoul-Henning Kamp 				usage();
2274e8bfe14SPoul-Henning Kamp 			mdio.md_fwheads = strtoul(optarg, &p, 0);
2284e8bfe14SPoul-Henning Kamp 			break;
22970d586c0SPoul-Henning Kamp 		default:
230c2ef0b73SPoul-Henning Kamp 			usage();
23170d586c0SPoul-Henning Kamp 		}
23270d586c0SPoul-Henning Kamp 	}
23353d745bcSDima Dorfman 	mdio.md_version = MDIOVERSION;
23470d586c0SPoul-Henning Kamp 
235a921cb31SPawel Jakub Dawidek 	if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
236a921cb31SPawel Jakub Dawidek 		err(1, "failed to load geom_md module");
237a921cb31SPawel Jakub Dawidek 
238174b5e9aSPoul-Henning Kamp 	fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
23970d586c0SPoul-Henning Kamp 	if (fd < 0)
240174b5e9aSPoul-Henning Kamp 		err(1, "open(/dev/%s)", MDCTL_NAME);
2412885b421SDima Dorfman 	if (cmdline == 2
2422885b421SDima Dorfman 	    && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
243b830359bSPawel Jakub Dawidek 		if (mdio.md_mediasize == 0)
2442885b421SDima Dorfman 			errx(1, "must specify -s for -t malloc or -t swap");
245252bcf45SYaroslav Tykhiy 	if (cmdline == 2 && mdio.md_type == MD_VNODE)
24688b5b78dSPawel Jakub Dawidek 		if (mdio.md_file[0] == '\0')
247252bcf45SYaroslav Tykhiy 			errx(1, "must specify -f for -t vnode");
248c313f09bSChristian S.J. Peron 	if (mdio.md_type == MD_VNODE &&
249c313f09bSChristian S.J. Peron 	    (mdio.md_options & MD_READONLY) == 0) {
250c313f09bSChristian S.J. Peron 		if (access(mdio.md_file, W_OK) < 0 &&
251c313f09bSChristian S.J. Peron 		    (errno == EACCES || errno == EPERM || errno == EROFS)) {
252c313f09bSChristian S.J. Peron 			fprintf(stderr,
253c313f09bSChristian S.J. Peron 			    "WARNING: opening backing store: %s readonly\n",
254c313f09bSChristian S.J. Peron 			    mdio.md_file);
255c313f09bSChristian S.J. Peron 			mdio.md_options |= MD_READONLY;
256c313f09bSChristian S.J. Peron 		}
257c313f09bSChristian S.J. Peron 	}
258174b5e9aSPoul-Henning Kamp 	if (action == LIST) {
259c27a8954SWojciech A. Koszek 		if (mdio.md_options & MD_AUTOUNIT) {
260c27a8954SWojciech A. Koszek 			/*
261c27a8954SWojciech A. Koszek 			 * Listing all devices. This is why we pass NULL
262c27a8954SWojciech A. Koszek 			 * together with OPT_LIST.
263c27a8954SWojciech A. Koszek 			 */
264c27a8954SWojciech A. Koszek 			md_list(NULL, OPT_LIST);
265c27a8954SWojciech A. Koszek 		} else {
266c27a8954SWojciech A. Koszek 			md_query(mdunit);
267c27a8954SWojciech A. Koszek 		}
268174b5e9aSPoul-Henning Kamp 	} else if (action == ATTACH) {
269252bcf45SYaroslav Tykhiy 		if (cmdline < 2)
270252bcf45SYaroslav Tykhiy 			usage();
27170d586c0SPoul-Henning Kamp 		i = ioctl(fd, MDIOCATTACH, &mdio);
272174b5e9aSPoul-Henning Kamp 		if (i < 0)
273174b5e9aSPoul-Henning Kamp 			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
27483da2a90SPoul-Henning Kamp 		if (mdio.md_options & MD_AUTOUNIT)
275f79c46d3SRobert Watson 			printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
27683da2a90SPoul-Henning Kamp 	} else if (action == DETACH) {
2778f8def9eSPoul-Henning Kamp 		if (mdio.md_options & MD_AUTOUNIT)
2788f8def9eSPoul-Henning Kamp 			usage();
279c2ef0b73SPoul-Henning Kamp 		i = ioctl(fd, MDIOCDETACH, &mdio);
28070d586c0SPoul-Henning Kamp 		if (i < 0)
281174b5e9aSPoul-Henning Kamp 			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
28283da2a90SPoul-Henning Kamp 	} else
28383da2a90SPoul-Henning Kamp 		usage();
284174b5e9aSPoul-Henning Kamp 	close (fd);
285174b5e9aSPoul-Henning Kamp 	return (0);
286174b5e9aSPoul-Henning Kamp }
287174b5e9aSPoul-Henning Kamp 
288c27a8954SWojciech A. Koszek /*
289c27a8954SWojciech A. Koszek  * Lists md(4) disks. Is used also as a query routine, since it handles XML
290c27a8954SWojciech A. Koszek  * interface. 'units' can be NULL for listing memory disks. It might be
291c27a8954SWojciech A. Koszek  * coma-separated string containing md(4) disk names. 'opt' distinguished
292c27a8954SWojciech A. Koszek  * between list and query mode.
293c27a8954SWojciech A. Koszek  */
2947e06d7bcSDima Dorfman static int
295c27a8954SWojciech A. Koszek md_list(char *units, int opt)
2967e06d7bcSDima Dorfman {
297c27a8954SWojciech A. Koszek 	struct gmesh gm;
298c27a8954SWojciech A. Koszek 	struct gprovider *pp;
299c27a8954SWojciech A. Koszek 	struct gconsumer *cp;
300c27a8954SWojciech A. Koszek 	struct gconf *gc;
301c27a8954SWojciech A. Koszek 	struct gconfig *gce;
302c27a8954SWojciech A. Koszek 	struct gident *gid;
303c27a8954SWojciech A. Koszek 	struct devstat *gsp, *gsq;
304c27a8954SWojciech A. Koszek 	struct ggeom *gg;
305c27a8954SWojciech A. Koszek 	struct gclass *gcl;
306c27a8954SWojciech A. Koszek 	void *sq;
307c27a8954SWojciech A. Koszek 	int retcode;
308c27a8954SWojciech A. Koszek 	signed int ch;
309c27a8954SWojciech A. Koszek 	int nl;
310c27a8954SWojciech A. Koszek 	char *type, *file, *length;
3117e06d7bcSDima Dorfman 
312c27a8954SWojciech A. Koszek 	type = file = length = NULL;
313174b5e9aSPoul-Henning Kamp 
314c27a8954SWojciech A. Koszek 	retcode = geom_gettree(&gm);
315c27a8954SWojciech A. Koszek 	if (retcode != 0)
316c27a8954SWojciech A. Koszek 		return (-1);
317c27a8954SWojciech A. Koszek 	retcode = geom_stats_open();
318c27a8954SWojciech A. Koszek 	if (retcode != 0)
319c27a8954SWojciech A. Koszek 		return (-1);
320c27a8954SWojciech A. Koszek 	sq = geom_stats_snapshot_get();
321c27a8954SWojciech A. Koszek 	if (sq == NULL)
322c27a8954SWojciech A. Koszek 		return (-1);
323c27a8954SWojciech A. Koszek 
324c27a8954SWojciech A. Koszek 	while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
325c27a8954SWojciech A. Koszek 		gid = geom_lookupid(&gm, gsp->id);
326c27a8954SWojciech A. Koszek 		if (gid == NULL)
327c27a8954SWojciech A. Koszek 			continue;
328c27a8954SWojciech A. Koszek 		if (gid->lg_what == ISPROVIDER) {
329c27a8954SWojciech A. Koszek 			pp = gid->lg_ptr;
330c27a8954SWojciech A. Koszek 			gg = pp->lg_geom;
331c27a8954SWojciech A. Koszek 			gcl = gg->lg_class;
332c27a8954SWojciech A. Koszek 			if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
333c27a8954SWojciech A. Koszek 				continue;
334c27a8954SWojciech A. Koszek 			if ((opt & OPT_UNIT) && (units != NULL)) {
335c27a8954SWojciech A. Koszek 				retcode = md_find(units, pp->lg_name);
336c27a8954SWojciech A. Koszek 				if (retcode != 1)
337c27a8954SWojciech A. Koszek 					continue;
338174b5e9aSPoul-Henning Kamp 			}
339c27a8954SWojciech A. Koszek 			gc = &pp->lg_config;
340c27a8954SWojciech A. Koszek 			printf("%s", pp->lg_name);
341c27a8954SWojciech A. Koszek 			if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
342c27a8954SWojciech A. Koszek 				type = geom_config_get(gc, "type");
343c27a8954SWojciech A. Koszek 				if (strcmp(type, "vnode") == 0)
344c27a8954SWojciech A. Koszek 					file = geom_config_get(gc, "file");
345c27a8954SWojciech A. Koszek 				length = geom_config_get(gc, "length");
346c27a8954SWojciech A. Koszek 				if (length == NULL)
347c27a8954SWojciech A. Koszek 					length = "<a>";
348c27a8954SWojciech A. Koszek 				printf("\t%s\t", type);
349c27a8954SWojciech A. Koszek 				md_prthumanval(length);
350c27a8954SWojciech A. Koszek 				if (file != NULL) {
351c27a8954SWojciech A. Koszek 					printf("\t%s", file);
352c27a8954SWojciech A. Koszek 					file = NULL;
353c27a8954SWojciech A. Koszek 				}
354c27a8954SWojciech A. Koszek 			}
355c27a8954SWojciech A. Koszek 			opt |= OPT_DONE;
356c27a8954SWojciech A. Koszek 			if (opt & OPT_LIST)
357c27a8954SWojciech A. Koszek 				printf(" ");
358c27a8954SWojciech A. Koszek 			else
359e39eff98SPoul-Henning Kamp 				printf("\n");
360c27a8954SWojciech A. Koszek 		}
361c27a8954SWojciech A. Koszek 	}
362c27a8954SWojciech A. Koszek 	if ((opt & OPT_LIST) && (opt & OPT_DONE))
363c27a8954SWojciech A. Koszek 		printf("\n");
364c27a8954SWojciech A. Koszek 	/* XXX: Check if it's enough to clean everything. */
365c27a8954SWojciech A. Koszek 	geom_stats_snapshot_free(sq);
366c27a8954SWojciech A. Koszek 	return (-1);
367c27a8954SWojciech A. Koszek }
368c27a8954SWojciech A. Koszek 
369c27a8954SWojciech A. Koszek /*
370c27a8954SWojciech A. Koszek  * Returns value of 'name' from gconfig structure.
371c27a8954SWojciech A. Koszek  */
372c27a8954SWojciech A. Koszek static char *
373c27a8954SWojciech A. Koszek geom_config_get(struct gconf *g, char *name)
374c27a8954SWojciech A. Koszek {
375c27a8954SWojciech A. Koszek 	struct gconfig *gce;
376c27a8954SWojciech A. Koszek 
377c27a8954SWojciech A. Koszek 	LIST_FOREACH(gce, g, lg_config) {
378c27a8954SWojciech A. Koszek 		if (strcmp(gce->lg_name, name) == 0)
379c27a8954SWojciech A. Koszek 			return (gce->lg_val);
380c27a8954SWojciech A. Koszek 	}
381c27a8954SWojciech A. Koszek 	return (NULL);
382c27a8954SWojciech A. Koszek }
383c27a8954SWojciech A. Koszek 
384c27a8954SWojciech A. Koszek /*
385c27a8954SWojciech A. Koszek  * List is comma separated list of MD disks. name is a
386c27a8954SWojciech A. Koszek  * device name we look for.  Returns 1 if found and 0
387c27a8954SWojciech A. Koszek  * otherwise.
388c27a8954SWojciech A. Koszek  */
389c27a8954SWojciech A. Koszek static int
390c27a8954SWojciech A. Koszek md_find(char *list, const char *name)
391c27a8954SWojciech A. Koszek {
392c27a8954SWojciech A. Koszek 	int ret;
393c27a8954SWojciech A. Koszek 	char num[16];
394c27a8954SWojciech A. Koszek 	char *ptr, *p, *u;
395c27a8954SWojciech A. Koszek 
396c27a8954SWojciech A. Koszek 	ret = 0;
397c27a8954SWojciech A. Koszek 	ptr = strdup(list);
398c27a8954SWojciech A. Koszek 	if (ptr == NULL)
399c27a8954SWojciech A. Koszek 		return (-1);
400c27a8954SWojciech A. Koszek 	for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
401c27a8954SWojciech A. Koszek 		if (strncmp(u, "/dev/", 5) == 0)
402c27a8954SWojciech A. Koszek 			u += 5;
403c27a8954SWojciech A. Koszek 		/* Just in case user specified number instead of full name */
404c27a8954SWojciech A. Koszek 		snprintf(num, sizeof(num), "md%s", u);
405c27a8954SWojciech A. Koszek 		if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
406c27a8954SWojciech A. Koszek 			ret = 1;
407c27a8954SWojciech A. Koszek 			break;
408c27a8954SWojciech A. Koszek 		}
409c27a8954SWojciech A. Koszek 	}
410c27a8954SWojciech A. Koszek 	free(ptr);
411c27a8954SWojciech A. Koszek 	return (ret);
412174b5e9aSPoul-Henning Kamp }
413174b5e9aSPoul-Henning Kamp 
414b830359bSPawel Jakub Dawidek static void
415c27a8954SWojciech A. Koszek md_prthumanval(char *length)
416b830359bSPawel Jakub Dawidek {
417b830359bSPawel Jakub Dawidek 	char buf[6];
418c27a8954SWojciech A. Koszek 	uint64_t bytes;
419c27a8954SWojciech A. Koszek 	char *endptr;
420b830359bSPawel Jakub Dawidek 
421c27a8954SWojciech A. Koszek 	bytes = strtoul(length, &endptr, 10);
422c27a8954SWojciech A. Koszek 	if (bytes == (unsigned)ULONG_MAX || *endptr != '\0')
423c27a8954SWojciech A. Koszek 		return;
424b830359bSPawel Jakub Dawidek 	humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
425b830359bSPawel Jakub Dawidek 	    bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
426b830359bSPawel Jakub Dawidek 	(void)printf("%6s", buf);
427b830359bSPawel Jakub Dawidek }
428b830359bSPawel Jakub Dawidek 
429c27a8954SWojciech A. Koszek int
430c27a8954SWojciech A. Koszek md_query(char *name)
431174b5e9aSPoul-Henning Kamp {
432c27a8954SWojciech A. Koszek 	return (md_list(name, OPT_UNIT));
43370d586c0SPoul-Henning Kamp }
434