1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000-2004 Poul-Henning Kamp <phk@FreeBSD.org>
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Edward Tomasz Napierala
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/devicestat.h>
35#include <sys/ioctl.h>
36#include <sys/linker.h>
37#include <sys/mdioctl.h>
38#include <sys/module.h>
39#include <sys/resource.h>
40#include <sys/stat.h>
41
42#include <assert.h>
43#include <devstat.h>
44#include <err.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <inttypes.h>
48#include <libgeom.h>
49#include <libutil.h>
50#include <paths.h>
51#include <stdarg.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56
57static struct md_ioctl mdio;
58static enum {UNSET, ATTACH, DETACH, RESIZE, LIST} action = UNSET;
59static int md_fd, nflag;
60
61static void usage(void) __dead2;
62static void md_set_file(const char *);
63static int md_find(const char *, const char *);
64static int md_query(const char *, const int, const char *);
65static int md_list(const char *, int, const char *);
66static char *geom_config_get(struct gconf *g, const char *name);
67static void md_prthumanval(char *length);
68static void print_options(const char *s, const char *);
69
70#define OPT_VERBOSE	0x01
71#define OPT_UNIT	0x02
72#define OPT_DONE	0x04
73#define OPT_LIST	0x10
74
75#define CLASS_NAME_MD	"MD"
76
77static void
78usage(void)
79{
80
81	fprintf(stderr,
82"usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
83"                [-s size] [-S sectorsize] [-u unit] [-L label]\n"
84"                [-x sectors/track] [-y heads/cylinder]\n"
85"       mdconfig -d -u unit [-o [no]force]\n"
86"       mdconfig -r -u unit -s size [-o [no]force]\n"
87"       mdconfig -l [-v] [-n] [-f file] [-u unit]\n"
88"       mdconfig file\n");
89	fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n");
90	fprintf(stderr, "\t\toption = {async, cache, cluster, compress,\n");
91	fprintf(stderr, "\t\t          force, mustdealloc, readonly, ro,\n");
92	fprintf(stderr, "\t\t          reserve, verify}\n");
93	fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
94	fprintf(stderr, "\t\t       %%dk (kB), %%dm (MB), %%dg (GB),\n");
95	fprintf(stderr, "\t\t       %%dt (TB), or %%dp (PB)\n");
96	exit(1);
97}
98
99int
100main(int argc, char **argv)
101{
102	int ch, i, vflag;
103	char *p;
104	char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
105
106	bzero(&mdio, sizeof(mdio));
107	mdio.md_file = malloc(PATH_MAX);
108	mdio.md_label = malloc(PATH_MAX);
109	if (mdio.md_file == NULL || mdio.md_label == NULL)
110		err(1, "could not allocate memory");
111	vflag = 0;
112	bzero(mdio.md_file, PATH_MAX);
113	bzero(mdio.md_label, PATH_MAX);
114
115	if (argc == 1)
116		usage();
117
118	while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:L:")) != -1) {
119		switch (ch) {
120		case 'a':
121			if (action != UNSET && action != ATTACH)
122				errx(1, "-a is mutually exclusive "
123				    "with -d, -r, and -l");
124			action = ATTACH;
125			break;
126		case 'd':
127			if (action != UNSET && action != DETACH)
128				errx(1, "-d is mutually exclusive "
129				    "with -a, -r, and -l");
130			action = DETACH;
131			mdio.md_options |= MD_AUTOUNIT;
132			break;
133		case 'r':
134			if (action != UNSET && action != RESIZE)
135				errx(1, "-r is mutually exclusive "
136				    "with -a, -d, and -l");
137			action = RESIZE;
138			mdio.md_options |= MD_AUTOUNIT;
139			break;
140		case 'l':
141			if (action != UNSET && action != LIST)
142				errx(1, "-l is mutually exclusive "
143				    "with -a, -r, and -d");
144			action = LIST;
145			mdio.md_options |= MD_AUTOUNIT;
146			break;
147		case 'n':
148			nflag = 1;
149			break;
150		case 't':
151			if (tflag != NULL)
152				errx(1, "-t can be passed only once");
153			tflag = optarg;
154			if (!strcmp(optarg, "malloc")) {
155				mdio.md_type = MD_MALLOC;
156				mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
157			} else if (!strcmp(optarg, "vnode")) {
158				mdio.md_type = MD_VNODE;
159				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
160			} else if (!strcmp(optarg, "swap")) {
161				mdio.md_type = MD_SWAP;
162				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
163			} else if (!strcmp(optarg, "null")) {
164				mdio.md_type = MD_NULL;
165				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
166			} else
167				errx(1, "unknown type: %s", optarg);
168			break;
169		case 'f':
170			if (fflag != NULL)
171				errx(1, "-f can be passed only once");
172			fflag = realpath(optarg, NULL);
173			if (fflag == NULL)
174				err(1, "realpath");
175			break;
176		case 'o':
177			if (!strcmp(optarg, "async"))
178				mdio.md_options |= MD_ASYNC;
179			else if (!strcmp(optarg, "noasync"))
180				mdio.md_options &= ~MD_ASYNC;
181			else if (!strcmp(optarg, "cache"))
182				mdio.md_options |= MD_CACHE;
183			else if (!strcmp(optarg, "nocache"))
184				mdio.md_options &= ~MD_CACHE;
185			else if (!strcmp(optarg, "cluster"))
186				mdio.md_options |= MD_CLUSTER;
187			else if (!strcmp(optarg, "nocluster"))
188				mdio.md_options &= ~MD_CLUSTER;
189			else if (!strcmp(optarg, "compress"))
190				mdio.md_options |= MD_COMPRESS;
191			else if (!strcmp(optarg, "nocompress"))
192				mdio.md_options &= ~MD_COMPRESS;
193			else if (!strcmp(optarg, "force"))
194				mdio.md_options |= MD_FORCE;
195			else if (!strcmp(optarg, "noforce"))
196				mdio.md_options &= ~MD_FORCE;
197			else if (!strcmp(optarg, "mustdealloc"))
198				mdio.md_options |= MD_MUSTDEALLOC;
199			else if (!strcmp(optarg, "nomustdealloc"))
200				mdio.md_options &= ~MD_MUSTDEALLOC;
201			else if (!strcmp(optarg, "readonly"))
202				mdio.md_options |= MD_READONLY;
203			else if (!strcmp(optarg, "noreadonly"))
204				mdio.md_options &= ~MD_READONLY;
205			else if (!strcmp(optarg, "ro"))
206				mdio.md_options |= MD_READONLY;
207			else if (!strcmp(optarg, "noro"))
208				mdio.md_options &= ~MD_READONLY;
209			else if (!strcmp(optarg, "reserve"))
210				mdio.md_options |= MD_RESERVE;
211			else if (!strcmp(optarg, "noreserve"))
212				mdio.md_options &= ~MD_RESERVE;
213			else if (!strcmp(optarg, "verify"))
214				mdio.md_options |= MD_VERIFY;
215			else if (!strcmp(optarg, "noverify"))
216				mdio.md_options &= ~MD_VERIFY;
217			else
218				errx(1, "unknown option: %s", optarg);
219			break;
220		case 'S':
221			mdio.md_sectorsize = strtoul(optarg, &p, 0);
222			break;
223		case 's':
224			if (sflag != NULL)
225				errx(1, "-s can be passed only once");
226			sflag = optarg;
227			mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
228			if (p == NULL || *p == '\0')
229				mdio.md_mediasize *= DEV_BSIZE;
230			else if (*p == 'b' || *p == 'B')
231				; /* do nothing */
232			else if (*p == 'k' || *p == 'K')
233				mdio.md_mediasize <<= 10;
234			else if (*p == 'm' || *p == 'M')
235				mdio.md_mediasize <<= 20;
236			else if (*p == 'g' || *p == 'G')
237				mdio.md_mediasize <<= 30;
238			else if (*p == 't' || *p == 'T') {
239				mdio.md_mediasize <<= 30;
240				mdio.md_mediasize <<= 10;
241			} else if (*p == 'p' || *p == 'P') {
242				mdio.md_mediasize <<= 30;
243				mdio.md_mediasize <<= 20;
244			} else
245				errx(1, "unknown suffix on -s argument");
246			break;
247		case 'u':
248			if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
249				optarg += sizeof(_PATH_DEV) - 1;
250			if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
251				optarg += sizeof(MD_NAME) - 1;
252			uflag = optarg;
253			break;
254		case 'v':
255			vflag = OPT_VERBOSE;
256			break;
257		case 'x':
258			mdio.md_fwsectors = strtoul(optarg, &p, 0);
259			break;
260		case 'y':
261			mdio.md_fwheads = strtoul(optarg, &p, 0);
262			break;
263		case 'L':
264			strlcpy(mdio.md_label, optarg, PATH_MAX);
265			break;
266		default:
267			usage();
268		}
269	}
270
271	argc -= optind;
272	argv += optind;
273
274	if (action == UNSET)
275		action = ATTACH;
276
277	if (action == ATTACH) {
278		if (tflag == NULL) {
279			/*
280			 * Try to infer the type based on other arguments.
281			 */
282			if (fflag != NULL || argc > 0) {
283				/* Imply ``-t vnode'' */
284				mdio.md_type = MD_VNODE;
285				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
286				    MD_COMPRESS;
287			} else if (sflag != NULL) {
288				/* Imply ``-t swap'' */
289				mdio.md_type = MD_SWAP;
290				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
291				    MD_COMPRESS;
292			} else
293				errx(1, "unable to determine type");
294		}
295
296		if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
297			errx(1, "only -t vnode can be used with file name");
298
299		if (mdio.md_type == MD_VNODE) {
300			if (fflag != NULL) {
301				if (argc != 0)
302					usage();
303				md_set_file(fflag);
304			} else {
305				if (argc != 1)
306					usage();
307				md_set_file(*argv);
308			}
309
310			if ((mdio.md_options & MD_READONLY) == 0 &&
311			    access(mdio.md_file, W_OK) < 0 &&
312			    (errno == EACCES || errno == EPERM ||
313			     errno == EROFS)) {
314				warnx("WARNING: opening backing store: %s "
315				    "readonly", mdio.md_file);
316				mdio.md_options |= MD_READONLY;
317			}
318		}
319
320		if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
321		    mdio.md_type == MD_NULL) && sflag == NULL)
322			errx(1, "must specify -s for -t malloc, -t swap, "
323			    "or -t null");
324		if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
325			errx(1, "must specify -f for -t vnode");
326	} else {
327		if (mdio.md_sectorsize != 0)
328			errx(1, "-S can only be used with -a");
329		if (action != RESIZE && sflag != NULL)
330			errx(1, "-s can only be used with -a and -r");
331		if (mdio.md_fwsectors != 0)
332			errx(1, "-x can only be used with -a");
333		if (mdio.md_fwheads != 0)
334			errx(1, "-y can only be used with -a");
335		if (fflag != NULL && action != LIST)
336			errx(1, "-f can only be used with -a and -l");
337		if (tflag != NULL)
338			errx(1, "-t can only be used with -a");
339		if (argc > 0)
340			errx(1, "file can only be used with -a");
341		if ((action != DETACH && action != RESIZE) &&
342		    (mdio.md_options & ~MD_AUTOUNIT) != 0)
343			errx(1, "-o can only be used with -a, -d, and -r");
344		if (action == DETACH &&
345		    (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
346			errx(1, "only -o [no]force can be used with -d");
347		if (action == RESIZE &&
348		    (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
349			errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
350	}
351
352	if (action == RESIZE && sflag == NULL)
353		errx(1, "must specify -s for -r");
354
355	if (action != LIST && vflag == OPT_VERBOSE)
356		errx(1, "-v can only be used with -l");
357
358	if (uflag != NULL) {
359		mdio.md_unit = strtoul(uflag, &p, 0);
360		if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
361			errx(1, "bad unit: %s", uflag);
362		mdio.md_options &= ~MD_AUTOUNIT;
363	}
364
365	mdio.md_version = MDIOVERSION;
366
367	if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
368		err(1, "failed to load geom_md module");
369
370	md_fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
371	if (md_fd < 0)
372		err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
373
374	if (action == ATTACH) {
375		i = ioctl(md_fd, MDIOCATTACH, &mdio);
376		if (i < 0)
377			err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
378		if (mdio.md_options & MD_AUTOUNIT)
379			printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
380	} else if (action == DETACH) {
381		if (mdio.md_options & MD_AUTOUNIT)
382			errx(1, "-d requires -u");
383		i = ioctl(md_fd, MDIOCDETACH, &mdio);
384		if (i < 0)
385			err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
386	} else if (action == RESIZE) {
387		if (mdio.md_options & MD_AUTOUNIT)
388			errx(1, "-r requires -u");
389		i = ioctl(md_fd, MDIOCRESIZE, &mdio);
390		if (i < 0)
391			err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
392	} else if (action == LIST) {
393		if (mdio.md_options & MD_AUTOUNIT) {
394			/*
395			 * Listing all devices. This is why we pass NULL
396			 * together with OPT_LIST.
397			 */
398			return (md_list(NULL, OPT_LIST | vflag, fflag));
399		} else
400			return (md_query(uflag, vflag, fflag));
401	} else
402		usage();
403	close(md_fd);
404	return (0);
405}
406
407static void
408print_options(const char *dev, const char *file)
409{
410	struct md_ioctl mdiox;
411	int unit;
412	char sep = '\0';
413
414	if (sscanf(dev, "md%d", &unit) != 1)
415		err(1, "invalid device: %s", dev);
416
417	memset(&mdiox, 0, sizeof(mdiox));
418	mdiox.md_version = MDIOVERSION;
419	mdiox.md_unit = unit;
420	mdiox.md_file = file[0] == '-' ? NULL : strdup(file);
421
422	if (ioctl(md_fd, MDIOCQUERY, &mdiox) < 0)
423		err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
424
425	if (mdiox.md_file != NULL)
426		free(mdiox.md_file);
427
428	printf("\t");
429	if (mdiox.md_options & MD_ASYNC) {
430		printf("%casync", sep);
431		sep = ',';
432	}
433	if (mdiox.md_options & MD_CACHE) {
434		printf("%ccache", sep);
435		sep = ',';
436	}
437	if (mdiox.md_options & MD_CLUSTER) {
438		printf("%ccluster", sep);
439		sep = ',';
440	}
441	if (mdiox.md_options & MD_COMPRESS) {
442		printf("%ccompress", sep);
443		sep = ',';
444	}
445	if (mdiox.md_options & MD_FORCE) {
446		printf("%cforce", sep);
447		sep = ',';
448	}
449	if (mdiox.md_options & MD_READONLY) {
450		printf("%creadonly", sep);
451		sep = ',';
452	}
453	if (mdiox.md_options & MD_RESERVE) {
454		printf("%creserve", sep);
455		sep = ',';
456	}
457	if (mdiox.md_options & MD_VERIFY) {
458		printf("%cverify", sep);
459		sep = ',';
460	}
461	if (mdiox.md_options & MD_MUSTDEALLOC) {
462		printf("%cmustdealloc", sep);
463		sep = ',';
464	}
465}
466
467static void
468md_set_file(const char *fn)
469{
470	struct stat sb;
471	int fd;
472
473	if (realpath(fn, mdio.md_file) == NULL)
474		err(1, "could not find full path for %s", fn);
475	fd = open(mdio.md_file, O_RDONLY);
476	if (fd < 0)
477		err(1, "could not open %s", fn);
478	if (fstat(fd, &sb) == -1)
479		err(1, "could not stat %s", fn);
480	if (!S_ISREG(sb.st_mode))
481		errx(1, "%s is not a regular file", fn);
482	if (mdio.md_mediasize == 0)
483		mdio.md_mediasize = sb.st_size;
484	close(fd);
485}
486
487/*
488 * Lists md(4) disks. Is used also as a query routine, since it handles XML
489 * interface. 'units' can be NULL for listing memory disks. It might be
490 * comma-separated string containing md(4) disk names. 'opt' distinguished
491 * between list and query mode.
492 */
493static int
494md_list(const char *units, int opt, const char *fflag)
495{
496	struct gmesh gm;
497	struct gprovider *pp;
498	struct gconf *gc;
499	struct gident *gid;
500	struct devstat *gsp;
501	struct ggeom *gg;
502	struct gclass *gcl;
503	void *sq;
504	int retcode, ffound, ufound;
505	char *length;
506	const char *type, *file, *label;
507
508	type = file = length = NULL;
509
510	retcode = geom_gettree(&gm);
511	if (retcode != 0)
512		return (-1);
513	retcode = geom_stats_open();
514	if (retcode != 0)
515		return (-1);
516	sq = geom_stats_snapshot_get();
517	if (sq == NULL)
518		return (-1);
519
520	ffound = ufound = 0;
521	while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
522		gid = geom_lookupid(&gm, gsp->id);
523		if (gid == NULL)
524			continue;
525		if (gid->lg_what == ISPROVIDER) {
526			pp = gid->lg_ptr;
527			gg = pp->lg_geom;
528			gcl = gg->lg_class;
529			if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
530				continue;
531			if ((opt & OPT_UNIT) && (units != NULL)) {
532				retcode = md_find(units, pp->lg_name);
533				if (retcode != 1)
534					continue;
535				else
536					ufound = 1;
537			}
538			gc = &pp->lg_config;
539			type = geom_config_get(gc, "type");
540			if (type != NULL && (strcmp(type, "vnode") == 0 ||
541			    strcmp(type, "preload") == 0)) {
542				file = geom_config_get(gc, "file");
543				if (fflag != NULL &&
544				    strcmp(fflag, file) != 0)
545					continue;
546				else
547					ffound = 1;
548			} else if (fflag != NULL)
549					continue;
550			if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
551				printf("%s", pp->lg_name + 2);
552			else
553				printf("%s", pp->lg_name);
554
555			if (opt & OPT_VERBOSE ||
556			    ((opt & OPT_UNIT) && fflag == NULL)) {
557				length = geom_config_get(gc, "length");
558				printf("\t%s\t", type);
559				if (length != NULL)
560					md_prthumanval(length);
561				if (file == NULL)
562					file = "-";
563				printf("\t%s", file);
564				label = geom_config_get(gc, "label");
565				if (label == NULL)
566					label = "-";
567				printf("\t%s", label);
568				print_options(pp->lg_name, file);
569				file = label = NULL;
570			}
571			opt |= OPT_DONE;
572			if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
573				printf(" ");
574			else
575				printf("\n");
576		}
577	}
578	if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
579		printf("\n");
580	/* XXX: Check if it's enough to clean everything. */
581	geom_stats_snapshot_free(sq);
582	if (opt & OPT_UNIT) {
583		if (((fflag == NULL) && ufound) ||
584		    ((fflag == NULL) && (units != NULL) && ufound) ||
585		    ((fflag != NULL) && ffound) ||
586		    ((fflag != NULL) && (units != NULL) && ufound && ffound))
587			return (0);
588	} else if (opt & OPT_LIST) {
589		if ((fflag == NULL) ||
590		    ((fflag != NULL) && ffound))
591			return (0);
592	}
593	return (-1);
594}
595
596/*
597 * Returns value of 'name' from gconfig structure.
598 */
599static char *
600geom_config_get(struct gconf *g, const char *name)
601{
602	struct gconfig *gce;
603
604	LIST_FOREACH(gce, g, lg_config) {
605		if (strcmp(gce->lg_name, name) == 0)
606			return (gce->lg_val);
607	}
608	return (NULL);
609}
610
611/*
612 * List is comma separated list of MD disks. name is a
613 * device name we look for.  Returns 1 if found and 0
614 * otherwise.
615 */
616static int
617md_find(const char *list, const char *name)
618{
619	int ret;
620	char num[PATH_MAX];
621	char *ptr, *p, *u;
622
623	ret = 0;
624	ptr = strdup(list);
625	if (ptr == NULL)
626		return (-1);
627	for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
628		if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
629			u += sizeof(_PATH_DEV) - 1;
630		/* Just in case user specified number instead of full name */
631		snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
632		if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
633			ret = 1;
634			break;
635		}
636	}
637	free(ptr);
638	return (ret);
639}
640
641static void
642md_prthumanval(char *length)
643{
644	char buf[6];
645	uintmax_t bytes;
646	char *endptr;
647
648	errno = 0;
649	bytes = strtoumax(length, &endptr, 10);
650	if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
651		return;
652	humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
653	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
654	(void)printf("%6s", buf);
655}
656
657static int
658md_query(const char *name, const int opt, const char *fflag)
659{
660
661	return (md_list(name, opt | OPT_UNIT, fflag));
662}
663