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 readonly, reserve, ro, 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");
100 main(int argc, char **argv)
102 int ch, fd, i, vflag;
104 char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
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");
112 bzero(mdio.md_file, PATH_MAX);
113 bzero(mdio.md_label, PATH_MAX);
118 while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:L:")) != -1) {
121 if (action != UNSET && action != ATTACH)
122 errx(1, "-a is mutually exclusive "
123 "with -d, -r, and -l");
127 if (action != UNSET && action != DETACH)
128 errx(1, "-d is mutually exclusive "
129 "with -a, -r, and -l");
131 mdio.md_options |= MD_AUTOUNIT;
134 if (action != UNSET && action != RESIZE)
135 errx(1, "-r is mutually exclusive "
136 "with -a, -d, and -l");
138 mdio.md_options |= MD_AUTOUNIT;
141 if (action != UNSET && action != LIST)
142 errx(1, "-l is mutually exclusive "
143 "with -a, -r, and -d");
145 mdio.md_options |= MD_AUTOUNIT;
152 errx(1, "-t can be passed only once");
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;
167 errx(1, "unknown type: %s", optarg);
171 errx(1, "-f can be passed only once");
172 fflag = realpath(optarg, NULL);
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, "readonly"))
198 mdio.md_options |= MD_READONLY;
199 else if (!strcmp(optarg, "noreadonly"))
200 mdio.md_options &= ~MD_READONLY;
201 else if (!strcmp(optarg, "ro"))
202 mdio.md_options |= MD_READONLY;
203 else if (!strcmp(optarg, "noro"))
204 mdio.md_options &= ~MD_READONLY;
205 else if (!strcmp(optarg, "reserve"))
206 mdio.md_options |= MD_RESERVE;
207 else if (!strcmp(optarg, "noreserve"))
208 mdio.md_options &= ~MD_RESERVE;
209 else if (!strcmp(optarg, "verify"))
210 mdio.md_options |= MD_VERIFY;
211 else if (!strcmp(optarg, "noverify"))
212 mdio.md_options &= ~MD_VERIFY;
214 errx(1, "unknown option: %s", optarg);
217 mdio.md_sectorsize = strtoul(optarg, &p, 0);
221 errx(1, "-s can be passed only once");
223 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
224 if (p == NULL || *p == '\0')
225 mdio.md_mediasize *= DEV_BSIZE;
226 else if (*p == 'b' || *p == 'B')
228 else if (*p == 'k' || *p == 'K')
229 mdio.md_mediasize <<= 10;
230 else if (*p == 'm' || *p == 'M')
231 mdio.md_mediasize <<= 20;
232 else if (*p == 'g' || *p == 'G')
233 mdio.md_mediasize <<= 30;
234 else if (*p == 't' || *p == 'T') {
235 mdio.md_mediasize <<= 30;
236 mdio.md_mediasize <<= 10;
237 } else if (*p == 'p' || *p == 'P') {
238 mdio.md_mediasize <<= 30;
239 mdio.md_mediasize <<= 20;
241 errx(1, "unknown suffix on -s argument");
244 if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
245 optarg += sizeof(_PATH_DEV) - 1;
246 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
247 optarg += sizeof(MD_NAME) - 1;
254 mdio.md_fwsectors = strtoul(optarg, &p, 0);
257 mdio.md_fwheads = strtoul(optarg, &p, 0);
260 strlcpy(mdio.md_label, optarg, PATH_MAX);
273 if (action == ATTACH) {
276 * Try to infer the type based on other arguments.
278 if (fflag != NULL || argc > 0) {
279 /* Imply ``-t vnode'' */
280 mdio.md_type = MD_VNODE;
281 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
283 } else if (sflag != NULL) {
284 /* Imply ``-t swap'' */
285 mdio.md_type = MD_SWAP;
286 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
289 errx(1, "unable to determine type");
292 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
293 errx(1, "only -t vnode can be used with file name");
295 if (mdio.md_type == MD_VNODE) {
306 if ((mdio.md_options & MD_READONLY) == 0 &&
307 access(mdio.md_file, W_OK) < 0 &&
308 (errno == EACCES || errno == EPERM ||
310 warnx("WARNING: opening backing store: %s "
311 "readonly", mdio.md_file);
312 mdio.md_options |= MD_READONLY;
316 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
317 mdio.md_type == MD_NULL) && sflag == NULL)
318 errx(1, "must specify -s for -t malloc, -t swap, "
320 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
321 errx(1, "must specify -f for -t vnode");
323 if (mdio.md_sectorsize != 0)
324 errx(1, "-S can only be used with -a");
325 if (action != RESIZE && sflag != NULL)
326 errx(1, "-s can only be used with -a and -r");
327 if (mdio.md_fwsectors != 0)
328 errx(1, "-x can only be used with -a");
329 if (mdio.md_fwheads != 0)
330 errx(1, "-y can only be used with -a");
331 if (fflag != NULL && action != LIST)
332 errx(1, "-f can only be used with -a and -l");
334 errx(1, "-t can only be used with -a");
336 errx(1, "file can only be used with -a");
337 if ((action != DETACH && action != RESIZE) &&
338 (mdio.md_options & ~MD_AUTOUNIT) != 0)
339 errx(1, "-o can only be used with -a, -d, and -r");
340 if (action == DETACH &&
341 (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
342 errx(1, "only -o [no]force can be used with -d");
343 if (action == RESIZE &&
344 (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
345 errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
348 if (action == RESIZE && sflag == NULL)
349 errx(1, "must specify -s for -r");
351 if (action != LIST && vflag == OPT_VERBOSE)
352 errx(1, "-v can only be used with -l");
355 mdio.md_unit = strtoul(uflag, &p, 0);
356 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
357 errx(1, "bad unit: %s", uflag);
358 mdio.md_options &= ~MD_AUTOUNIT;
361 mdio.md_version = MDIOVERSION;
363 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
364 err(1, "failed to load geom_md module");
366 fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
368 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
370 if (action == ATTACH) {
371 i = ioctl(fd, MDIOCATTACH, &mdio);
373 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
374 if (mdio.md_options & MD_AUTOUNIT)
375 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
376 } else if (action == DETACH) {
377 if (mdio.md_options & MD_AUTOUNIT)
378 errx(1, "-d requires -u");
379 i = ioctl(fd, MDIOCDETACH, &mdio);
381 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
382 } else if (action == RESIZE) {
383 if (mdio.md_options & MD_AUTOUNIT)
384 errx(1, "-r requires -u");
385 i = ioctl(fd, MDIOCRESIZE, &mdio);
387 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
388 } else if (action == LIST) {
389 if (mdio.md_options & MD_AUTOUNIT) {
391 * Listing all devices. This is why we pass NULL
392 * together with OPT_LIST.
394 return (md_list(NULL, OPT_LIST | vflag, fflag));
396 return (md_query(uflag, vflag, fflag));
404 md_set_file(const char *fn)
409 if (realpath(fn, mdio.md_file) == NULL)
410 err(1, "could not find full path for %s", fn);
411 fd = open(mdio.md_file, O_RDONLY);
413 err(1, "could not open %s", fn);
414 if (fstat(fd, &sb) == -1)
415 err(1, "could not stat %s", fn);
416 if (!S_ISREG(sb.st_mode))
417 errx(1, "%s is not a regular file", fn);
418 if (mdio.md_mediasize == 0)
419 mdio.md_mediasize = sb.st_size;
424 * Lists md(4) disks. Is used also as a query routine, since it handles XML
425 * interface. 'units' can be NULL for listing memory disks. It might be
426 * coma-separated string containing md(4) disk names. 'opt' distinguished
427 * between list and query mode.
430 md_list(const char *units, int opt, const char *fflag)
433 struct gprovider *pp;
440 int retcode, ffound, ufound;
442 const char *type, *file, *label;
444 type = file = length = NULL;
446 retcode = geom_gettree(&gm);
449 retcode = geom_stats_open();
452 sq = geom_stats_snapshot_get();
457 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
458 gid = geom_lookupid(&gm, gsp->id);
461 if (gid->lg_what == ISPROVIDER) {
465 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
467 if ((opt & OPT_UNIT) && (units != NULL)) {
468 retcode = md_find(units, pp->lg_name);
475 type = geom_config_get(gc, "type");
476 if (type != NULL && (strcmp(type, "vnode") == 0 ||
477 strcmp(type, "preload") == 0)) {
478 file = geom_config_get(gc, "file");
480 strcmp(fflag, file) != 0)
484 } else if (fflag != NULL)
486 if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
487 printf("%s", pp->lg_name + 2);
489 printf("%s", pp->lg_name);
491 if (opt & OPT_VERBOSE ||
492 ((opt & OPT_UNIT) && fflag == NULL)) {
493 length = geom_config_get(gc, "length");
494 printf("\t%s\t", type);
496 md_prthumanval(length);
499 printf("\t%s", file);
501 label = geom_config_get(gc, "label");
504 printf("\t%s", label);
507 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
513 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
515 /* XXX: Check if it's enough to clean everything. */
516 geom_stats_snapshot_free(sq);
517 if (opt & OPT_UNIT) {
518 if (((fflag == NULL) && ufound) ||
519 ((fflag == NULL) && (units != NULL) && ufound) ||
520 ((fflag != NULL) && ffound) ||
521 ((fflag != NULL) && (units != NULL) && ufound && ffound))
523 } else if (opt & OPT_LIST) {
524 if ((fflag == NULL) ||
525 ((fflag != NULL) && ffound))
532 * Returns value of 'name' from gconfig structure.
535 geom_config_get(struct gconf *g, const char *name)
539 LIST_FOREACH(gce, g, lg_config) {
540 if (strcmp(gce->lg_name, name) == 0)
541 return (gce->lg_val);
547 * List is comma separated list of MD disks. name is a
548 * device name we look for. Returns 1 if found and 0
552 md_find(const char *list, const char *name)
562 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
563 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
564 u += sizeof(_PATH_DEV) - 1;
565 /* Just in case user specified number instead of full name */
566 snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
567 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
577 md_prthumanval(char *length)
584 bytes = strtoumax(length, &endptr, 10);
585 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
587 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
588 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
589 (void)printf("%6s", buf);
593 md_query(const char *name, const int opt, const char *fflag)
596 return (md_list(name, opt | OPT_UNIT, fflag));