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] [-L label]\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 = {cache, cluster, compress, force,\n");
92 fprintf(stderr, "\t\t mustdealloc, readonly, reserve, ro,\n");
93 fprintf(stderr, "\t\t verify}\n");
94 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
95 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB), \n");
96 fprintf(stderr, "\t\t %%dt (TB), or %%dp (PB)\n");
101 main(int argc, char **argv)
103 int ch, fd, i, vflag;
105 char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
107 bzero(&mdio, sizeof(mdio));
108 mdio.md_file = malloc(PATH_MAX);
109 mdio.md_label = malloc(PATH_MAX);
110 if (mdio.md_file == NULL || mdio.md_label == NULL)
111 err(1, "could not allocate memory");
113 bzero(mdio.md_file, PATH_MAX);
114 bzero(mdio.md_label, PATH_MAX);
119 while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:L:")) != -1) {
122 if (action != UNSET && action != ATTACH)
123 errx(1, "-a is mutually exclusive "
124 "with -d, -r, and -l");
128 if (action != UNSET && action != DETACH)
129 errx(1, "-d is mutually exclusive "
130 "with -a, -r, and -l");
132 mdio.md_options |= MD_AUTOUNIT;
135 if (action != UNSET && action != RESIZE)
136 errx(1, "-r is mutually exclusive "
137 "with -a, -d, and -l");
139 mdio.md_options |= MD_AUTOUNIT;
142 if (action != UNSET && action != LIST)
143 errx(1, "-l is mutually exclusive "
144 "with -a, -r, and -d");
146 mdio.md_options |= MD_AUTOUNIT;
153 errx(1, "-t can be passed only once");
155 if (!strcmp(optarg, "malloc")) {
156 mdio.md_type = MD_MALLOC;
157 mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
158 } else if (!strcmp(optarg, "vnode")) {
159 mdio.md_type = MD_VNODE;
160 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
161 } else if (!strcmp(optarg, "swap")) {
162 mdio.md_type = MD_SWAP;
163 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
164 } else if (!strcmp(optarg, "null")) {
165 mdio.md_type = MD_NULL;
166 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
168 errx(1, "unknown type: %s", optarg);
172 errx(1, "-f can be passed only once");
173 fflag = realpath(optarg, NULL);
178 if (!strcmp(optarg, "async"))
179 mdio.md_options |= MD_ASYNC;
180 else if (!strcmp(optarg, "noasync"))
181 mdio.md_options &= ~MD_ASYNC;
182 else if (!strcmp(optarg, "cache"))
183 mdio.md_options |= MD_CACHE;
184 else if (!strcmp(optarg, "nocache"))
185 mdio.md_options &= ~MD_CACHE;
186 else if (!strcmp(optarg, "cluster"))
187 mdio.md_options |= MD_CLUSTER;
188 else if (!strcmp(optarg, "nocluster"))
189 mdio.md_options &= ~MD_CLUSTER;
190 else if (!strcmp(optarg, "compress"))
191 mdio.md_options |= MD_COMPRESS;
192 else if (!strcmp(optarg, "nocompress"))
193 mdio.md_options &= ~MD_COMPRESS;
194 else if (!strcmp(optarg, "force"))
195 mdio.md_options |= MD_FORCE;
196 else if (!strcmp(optarg, "noforce"))
197 mdio.md_options &= ~MD_FORCE;
198 else if (!strcmp(optarg, "mustdealloc"))
199 mdio.md_options |= MD_MUSTDEALLOC;
200 else if (!strcmp(optarg, "nomustdealloc"))
201 mdio.md_options &= ~MD_MUSTDEALLOC;
202 else if (!strcmp(optarg, "readonly"))
203 mdio.md_options |= MD_READONLY;
204 else if (!strcmp(optarg, "noreadonly"))
205 mdio.md_options &= ~MD_READONLY;
206 else if (!strcmp(optarg, "ro"))
207 mdio.md_options |= MD_READONLY;
208 else if (!strcmp(optarg, "noro"))
209 mdio.md_options &= ~MD_READONLY;
210 else if (!strcmp(optarg, "reserve"))
211 mdio.md_options |= MD_RESERVE;
212 else if (!strcmp(optarg, "noreserve"))
213 mdio.md_options &= ~MD_RESERVE;
214 else if (!strcmp(optarg, "verify"))
215 mdio.md_options |= MD_VERIFY;
216 else if (!strcmp(optarg, "noverify"))
217 mdio.md_options &= ~MD_VERIFY;
219 errx(1, "unknown option: %s", optarg);
222 mdio.md_sectorsize = strtoul(optarg, &p, 0);
226 errx(1, "-s can be passed only once");
228 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
229 if (p == NULL || *p == '\0')
230 mdio.md_mediasize *= DEV_BSIZE;
231 else if (*p == 'b' || *p == 'B')
233 else if (*p == 'k' || *p == 'K')
234 mdio.md_mediasize <<= 10;
235 else if (*p == 'm' || *p == 'M')
236 mdio.md_mediasize <<= 20;
237 else if (*p == 'g' || *p == 'G')
238 mdio.md_mediasize <<= 30;
239 else if (*p == 't' || *p == 'T') {
240 mdio.md_mediasize <<= 30;
241 mdio.md_mediasize <<= 10;
242 } else if (*p == 'p' || *p == 'P') {
243 mdio.md_mediasize <<= 30;
244 mdio.md_mediasize <<= 20;
246 errx(1, "unknown suffix on -s argument");
249 if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
250 optarg += sizeof(_PATH_DEV) - 1;
251 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
252 optarg += sizeof(MD_NAME) - 1;
259 mdio.md_fwsectors = strtoul(optarg, &p, 0);
262 mdio.md_fwheads = strtoul(optarg, &p, 0);
265 strlcpy(mdio.md_label, optarg, PATH_MAX);
278 if (action == ATTACH) {
281 * Try to infer the type based on other arguments.
283 if (fflag != NULL || argc > 0) {
284 /* Imply ``-t vnode'' */
285 mdio.md_type = MD_VNODE;
286 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
288 } else if (sflag != NULL) {
289 /* Imply ``-t swap'' */
290 mdio.md_type = MD_SWAP;
291 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
294 errx(1, "unable to determine type");
297 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
298 errx(1, "only -t vnode can be used with file name");
300 if (mdio.md_type == MD_VNODE) {
311 if ((mdio.md_options & MD_READONLY) == 0 &&
312 access(mdio.md_file, W_OK) < 0 &&
313 (errno == EACCES || errno == EPERM ||
315 warnx("WARNING: opening backing store: %s "
316 "readonly", mdio.md_file);
317 mdio.md_options |= MD_READONLY;
321 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
322 mdio.md_type == MD_NULL) && sflag == NULL)
323 errx(1, "must specify -s for -t malloc, -t swap, "
325 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
326 errx(1, "must specify -f for -t vnode");
328 if (mdio.md_sectorsize != 0)
329 errx(1, "-S can only be used with -a");
330 if (action != RESIZE && sflag != NULL)
331 errx(1, "-s can only be used with -a and -r");
332 if (mdio.md_fwsectors != 0)
333 errx(1, "-x can only be used with -a");
334 if (mdio.md_fwheads != 0)
335 errx(1, "-y can only be used with -a");
336 if (fflag != NULL && action != LIST)
337 errx(1, "-f can only be used with -a and -l");
339 errx(1, "-t can only be used with -a");
341 errx(1, "file can only be used with -a");
342 if ((action != DETACH && action != RESIZE) &&
343 (mdio.md_options & ~MD_AUTOUNIT) != 0)
344 errx(1, "-o can only be used with -a, -d, and -r");
345 if (action == DETACH &&
346 (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
347 errx(1, "only -o [no]force can be used with -d");
348 if (action == RESIZE &&
349 (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
350 errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
353 if (action == RESIZE && sflag == NULL)
354 errx(1, "must specify -s for -r");
356 if (action != LIST && vflag == OPT_VERBOSE)
357 errx(1, "-v can only be used with -l");
360 mdio.md_unit = strtoul(uflag, &p, 0);
361 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
362 errx(1, "bad unit: %s", uflag);
363 mdio.md_options &= ~MD_AUTOUNIT;
366 mdio.md_version = MDIOVERSION;
368 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
369 err(1, "failed to load geom_md module");
371 fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
373 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
375 if (action == ATTACH) {
376 i = ioctl(fd, MDIOCATTACH, &mdio);
378 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
379 if (mdio.md_options & MD_AUTOUNIT)
380 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
381 } else if (action == DETACH) {
382 if (mdio.md_options & MD_AUTOUNIT)
383 errx(1, "-d requires -u");
384 i = ioctl(fd, MDIOCDETACH, &mdio);
386 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
387 } else if (action == RESIZE) {
388 if (mdio.md_options & MD_AUTOUNIT)
389 errx(1, "-r requires -u");
390 i = ioctl(fd, MDIOCRESIZE, &mdio);
392 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
393 } else if (action == LIST) {
394 if (mdio.md_options & MD_AUTOUNIT) {
396 * Listing all devices. This is why we pass NULL
397 * together with OPT_LIST.
399 return (md_list(NULL, OPT_LIST | vflag, fflag));
401 return (md_query(uflag, vflag, fflag));
409 md_set_file(const char *fn)
414 if (realpath(fn, mdio.md_file) == NULL)
415 err(1, "could not find full path for %s", fn);
416 fd = open(mdio.md_file, O_RDONLY);
418 err(1, "could not open %s", fn);
419 if (fstat(fd, &sb) == -1)
420 err(1, "could not stat %s", fn);
421 if (!S_ISREG(sb.st_mode))
422 errx(1, "%s is not a regular file", fn);
423 if (mdio.md_mediasize == 0)
424 mdio.md_mediasize = sb.st_size;
429 * Lists md(4) disks. Is used also as a query routine, since it handles XML
430 * interface. 'units' can be NULL for listing memory disks. It might be
431 * comma-separated string containing md(4) disk names. 'opt' distinguished
432 * between list and query mode.
435 md_list(const char *units, int opt, const char *fflag)
438 struct gprovider *pp;
445 int retcode, ffound, ufound;
447 const char *type, *file, *label;
449 type = file = length = NULL;
451 retcode = geom_gettree(&gm);
454 retcode = geom_stats_open();
457 sq = geom_stats_snapshot_get();
462 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
463 gid = geom_lookupid(&gm, gsp->id);
466 if (gid->lg_what == ISPROVIDER) {
470 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
472 if ((opt & OPT_UNIT) && (units != NULL)) {
473 retcode = md_find(units, pp->lg_name);
480 type = geom_config_get(gc, "type");
481 if (type != NULL && (strcmp(type, "vnode") == 0 ||
482 strcmp(type, "preload") == 0)) {
483 file = geom_config_get(gc, "file");
485 strcmp(fflag, file) != 0)
489 } else if (fflag != NULL)
491 if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
492 printf("%s", pp->lg_name + 2);
494 printf("%s", pp->lg_name);
496 if (opt & OPT_VERBOSE ||
497 ((opt & OPT_UNIT) && fflag == NULL)) {
498 length = geom_config_get(gc, "length");
499 printf("\t%s\t", type);
501 md_prthumanval(length);
504 printf("\t%s", file);
506 label = geom_config_get(gc, "label");
509 printf("\t%s", label);
512 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
518 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
520 /* XXX: Check if it's enough to clean everything. */
521 geom_stats_snapshot_free(sq);
522 if (opt & OPT_UNIT) {
523 if (((fflag == NULL) && ufound) ||
524 ((fflag == NULL) && (units != NULL) && ufound) ||
525 ((fflag != NULL) && ffound) ||
526 ((fflag != NULL) && (units != NULL) && ufound && ffound))
528 } else if (opt & OPT_LIST) {
529 if ((fflag == NULL) ||
530 ((fflag != NULL) && ffound))
537 * Returns value of 'name' from gconfig structure.
540 geom_config_get(struct gconf *g, const char *name)
544 LIST_FOREACH(gce, g, lg_config) {
545 if (strcmp(gce->lg_name, name) == 0)
546 return (gce->lg_val);
552 * List is comma separated list of MD disks. name is a
553 * device name we look for. Returns 1 if found and 0
557 md_find(const char *list, const char *name)
567 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
568 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
569 u += sizeof(_PATH_DEV) - 1;
570 /* Just in case user specified number instead of full name */
571 snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
572 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
582 md_prthumanval(char *length)
589 bytes = strtoumax(length, &endptr, 10);
590 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
592 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
593 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
594 (void)printf("%6s", buf);
598 md_query(const char *name, const int opt, const char *fflag)
601 return (md_list(name, opt | OPT_UNIT, fflag));