2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2004 Poul-Henning Kamp <phk@FreeBSD.org>
5 * Copyright (c) 2012 The FreeBSD Foundation
8 * Portions of this software were developed by Edward Tomasz Napierala
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/param.h>
36 #include <sys/devicestat.h>
37 #include <sys/ioctl.h>
38 #include <sys/linker.h>
39 #include <sys/mdioctl.h>
40 #include <sys/module.h>
41 #include <sys/resource.h>
59 static struct md_ioctl mdio;
60 static enum {UNSET, ATTACH, DETACH, RESIZE, LIST} action = UNSET;
63 static void usage(void);
64 static void md_set_file(const char *);
65 static int md_find(const char *, const char *);
66 static int md_query(const char *, const int, const char *);
67 static int md_list(const char *, int, const char *);
68 static char *geom_config_get(struct gconf *g, const char *name);
69 static void md_prthumanval(char *length);
71 #define OPT_VERBOSE 0x01
76 #define CLASS_NAME_MD "MD"
83 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
84 " [-s size] [-S sectorsize] [-u unit]\n"
85 " [-x sectors/track] [-y heads/cylinder]\n"
86 " mdconfig -d -u unit [-o [no]force]\n"
87 " mdconfig -r -u unit -s size [-o [no]force]\n"
88 " mdconfig -l [-v] [-n] [-f file] [-u unit]\n"
90 fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n");
91 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
92 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
93 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB), \n");
94 fprintf(stderr, "\t\t %%dt (TB), or %%dp (PB)\n");
99 main(int argc, char **argv)
101 int ch, fd, i, vflag;
103 char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
105 bzero(&mdio, sizeof(mdio));
106 mdio.md_file = malloc(PATH_MAX);
107 if (mdio.md_file == NULL)
108 err(1, "could not allocate memory");
110 bzero(mdio.md_file, PATH_MAX);
115 while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:")) != -1) {
118 if (action != UNSET && action != ATTACH)
119 errx(1, "-a is mutually exclusive "
120 "with -d, -r, and -l");
124 if (action != UNSET && action != DETACH)
125 errx(1, "-d is mutually exclusive "
126 "with -a, -r, and -l");
128 mdio.md_options |= MD_AUTOUNIT;
131 if (action != UNSET && action != RESIZE)
132 errx(1, "-r is mutually exclusive "
133 "with -a, -d, and -l");
135 mdio.md_options |= MD_AUTOUNIT;
138 if (action != UNSET && action != LIST)
139 errx(1, "-l is mutually exclusive "
140 "with -a, -r, and -d");
142 mdio.md_options |= MD_AUTOUNIT;
149 errx(1, "-t can be passed only once");
151 if (!strcmp(optarg, "malloc")) {
152 mdio.md_type = MD_MALLOC;
153 mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
154 } else if (!strcmp(optarg, "vnode")) {
155 mdio.md_type = MD_VNODE;
156 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
157 } else if (!strcmp(optarg, "swap")) {
158 mdio.md_type = MD_SWAP;
159 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
160 } else if (!strcmp(optarg, "null")) {
161 mdio.md_type = MD_NULL;
162 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
164 errx(1, "unknown type: %s", optarg);
168 errx(1, "-f can be passed only once");
169 fflag = realpath(optarg, NULL);
174 if (!strcmp(optarg, "async"))
175 mdio.md_options |= MD_ASYNC;
176 else if (!strcmp(optarg, "noasync"))
177 mdio.md_options &= ~MD_ASYNC;
178 else if (!strcmp(optarg, "cluster"))
179 mdio.md_options |= MD_CLUSTER;
180 else if (!strcmp(optarg, "nocluster"))
181 mdio.md_options &= ~MD_CLUSTER;
182 else if (!strcmp(optarg, "compress"))
183 mdio.md_options |= MD_COMPRESS;
184 else if (!strcmp(optarg, "nocompress"))
185 mdio.md_options &= ~MD_COMPRESS;
186 else if (!strcmp(optarg, "force"))
187 mdio.md_options |= MD_FORCE;
188 else if (!strcmp(optarg, "noforce"))
189 mdio.md_options &= ~MD_FORCE;
190 else if (!strcmp(optarg, "readonly"))
191 mdio.md_options |= MD_READONLY;
192 else if (!strcmp(optarg, "noreadonly"))
193 mdio.md_options &= ~MD_READONLY;
194 else if (!strcmp(optarg, "reserve"))
195 mdio.md_options |= MD_RESERVE;
196 else if (!strcmp(optarg, "noreserve"))
197 mdio.md_options &= ~MD_RESERVE;
199 errx(1, "unknown option: %s", optarg);
202 mdio.md_sectorsize = strtoul(optarg, &p, 0);
206 errx(1, "-s can be passed only once");
208 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
209 if (p == NULL || *p == '\0')
210 mdio.md_mediasize *= DEV_BSIZE;
211 else if (*p == 'b' || *p == 'B')
213 else if (*p == 'k' || *p == 'K')
214 mdio.md_mediasize <<= 10;
215 else if (*p == 'm' || *p == 'M')
216 mdio.md_mediasize <<= 20;
217 else if (*p == 'g' || *p == 'G')
218 mdio.md_mediasize <<= 30;
219 else if (*p == 't' || *p == 'T') {
220 mdio.md_mediasize <<= 30;
221 mdio.md_mediasize <<= 10;
222 } else if (*p == 'p' || *p == 'P') {
223 mdio.md_mediasize <<= 30;
224 mdio.md_mediasize <<= 20;
226 errx(1, "unknown suffix on -s argument");
229 if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
230 optarg += sizeof(_PATH_DEV) - 1;
231 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
232 optarg += sizeof(MD_NAME) - 1;
239 mdio.md_fwsectors = strtoul(optarg, &p, 0);
242 mdio.md_fwheads = strtoul(optarg, &p, 0);
255 if (action == ATTACH) {
258 * Try to infer the type based on other arguments.
260 if (fflag != NULL || argc > 0) {
261 /* Imply ``-t vnode'' */
262 mdio.md_type = MD_VNODE;
263 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
265 } else if (sflag != NULL) {
266 /* Imply ``-t swap'' */
267 mdio.md_type = MD_SWAP;
268 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
271 errx(1, "unable to determine type");
274 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
275 errx(1, "only -t vnode can be used with file name");
277 if (mdio.md_type == MD_VNODE) {
288 if ((mdio.md_options & MD_READONLY) == 0 &&
289 access(mdio.md_file, W_OK) < 0 &&
290 (errno == EACCES || errno == EPERM ||
292 warnx("WARNING: opening backing store: %s "
293 "readonly", mdio.md_file);
294 mdio.md_options |= MD_READONLY;
298 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
299 mdio.md_type == MD_NULL) && sflag == NULL)
300 errx(1, "must specify -s for -t malloc, -t swap, "
302 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
303 errx(1, "must specify -f for -t vnode");
305 if (mdio.md_sectorsize != 0)
306 errx(1, "-S can only be used with -a");
307 if (action != RESIZE && sflag != NULL)
308 errx(1, "-s can only be used with -a and -r");
309 if (mdio.md_fwsectors != 0)
310 errx(1, "-x can only be used with -a");
311 if (mdio.md_fwheads != 0)
312 errx(1, "-y can only be used with -a");
313 if (fflag != NULL && action != LIST)
314 errx(1, "-f can only be used with -a and -l");
316 errx(1, "-t can only be used with -a");
318 errx(1, "file can only be used with -a");
319 if ((action != DETACH && action != RESIZE) &&
320 (mdio.md_options & ~MD_AUTOUNIT) != 0)
321 errx(1, "-o can only be used with -a, -d, and -r");
322 if (action == DETACH &&
323 (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
324 errx(1, "only -o [no]force can be used with -d");
325 if (action == RESIZE &&
326 (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
327 errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
330 if (action == RESIZE && sflag == NULL)
331 errx(1, "must specify -s for -r");
333 if (action != LIST && vflag == OPT_VERBOSE)
334 errx(1, "-v can only be used with -l");
337 mdio.md_unit = strtoul(uflag, &p, 0);
338 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
339 errx(1, "bad unit: %s", uflag);
340 mdio.md_options &= ~MD_AUTOUNIT;
343 mdio.md_version = MDIOVERSION;
345 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
346 err(1, "failed to load geom_md module");
348 fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
350 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
352 if (action == ATTACH) {
353 i = ioctl(fd, MDIOCATTACH, &mdio);
355 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
356 if (mdio.md_options & MD_AUTOUNIT)
357 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
358 } else if (action == DETACH) {
359 if (mdio.md_options & MD_AUTOUNIT)
360 errx(1, "-d requires -u");
361 i = ioctl(fd, MDIOCDETACH, &mdio);
363 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
364 } else if (action == RESIZE) {
365 if (mdio.md_options & MD_AUTOUNIT)
366 errx(1, "-r requires -u");
367 i = ioctl(fd, MDIOCRESIZE, &mdio);
369 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
370 } else if (action == LIST) {
371 if (mdio.md_options & MD_AUTOUNIT) {
373 * Listing all devices. This is why we pass NULL
374 * together with OPT_LIST.
376 return (md_list(NULL, OPT_LIST | vflag, fflag));
378 return (md_query(uflag, vflag, fflag));
386 md_set_file(const char *fn)
391 if (realpath(fn, mdio.md_file) == NULL)
392 err(1, "could not find full path for %s", fn);
393 fd = open(mdio.md_file, O_RDONLY);
395 err(1, "could not open %s", fn);
396 if (fstat(fd, &sb) == -1)
397 err(1, "could not stat %s", fn);
398 if (!S_ISREG(sb.st_mode))
399 errx(1, "%s is not a regular file", fn);
400 if (mdio.md_mediasize == 0)
401 mdio.md_mediasize = sb.st_size;
406 * Lists md(4) disks. Is used also as a query routine, since it handles XML
407 * interface. 'units' can be NULL for listing memory disks. It might be
408 * coma-separated string containing md(4) disk names. 'opt' distinguished
409 * between list and query mode.
412 md_list(const char *units, int opt, const char *fflag)
415 struct gprovider *pp;
422 int retcode, ffound, ufound;
423 char *type, *file, *length;
425 type = file = length = NULL;
427 retcode = geom_gettree(&gm);
430 retcode = geom_stats_open();
433 sq = geom_stats_snapshot_get();
438 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
439 gid = geom_lookupid(&gm, gsp->id);
442 if (gid->lg_what == ISPROVIDER) {
446 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
448 if ((opt & OPT_UNIT) && (units != NULL)) {
449 retcode = md_find(units, pp->lg_name);
456 type = geom_config_get(gc, "type");
457 if (strcmp(type, "vnode") == 0) {
458 file = geom_config_get(gc, "file");
460 strcmp(fflag, file) != 0)
464 } else if (fflag != NULL)
466 if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
467 printf("%s", pp->lg_name + 2);
469 printf("%s", pp->lg_name);
471 if (opt & OPT_VERBOSE ||
472 ((opt & OPT_UNIT) && fflag == NULL)) {
473 length = geom_config_get(gc, "length");
474 printf("\t%s\t", type);
476 md_prthumanval(length);
478 printf("\t%s", file);
483 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
489 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
491 /* XXX: Check if it's enough to clean everything. */
492 geom_stats_snapshot_free(sq);
493 if (opt & OPT_UNIT) {
494 if (((fflag == NULL) && ufound) ||
495 ((fflag == NULL) && (units != NULL) && ufound) ||
496 ((fflag != NULL) && ffound) ||
497 ((fflag != NULL) && (units != NULL) && ufound && ffound))
499 } else if (opt & OPT_LIST) {
500 if ((fflag == NULL) ||
501 ((fflag != NULL) && ffound))
508 * Returns value of 'name' from gconfig structure.
511 geom_config_get(struct gconf *g, const char *name)
515 LIST_FOREACH(gce, g, lg_config) {
516 if (strcmp(gce->lg_name, name) == 0)
517 return (gce->lg_val);
523 * List is comma separated list of MD disks. name is a
524 * device name we look for. Returns 1 if found and 0
528 md_find(const char *list, const char *name)
538 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
539 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
540 u += sizeof(_PATH_DEV) - 1;
541 /* Just in case user specified number instead of full name */
542 snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
543 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
553 md_prthumanval(char *length)
560 bytes = strtoumax(length, &endptr, 10);
561 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
563 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
564 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
565 (void)printf("%6s", buf);
569 md_query(const char *name, const int opt, const char *fflag)
572 return (md_list(name, opt | OPT_UNIT, fflag));