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 = {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, "cluster"))
182 mdio.md_options |= MD_CLUSTER;
183 else if (!strcmp(optarg, "nocluster"))
184 mdio.md_options &= ~MD_CLUSTER;
185 else if (!strcmp(optarg, "compress"))
186 mdio.md_options |= MD_COMPRESS;
187 else if (!strcmp(optarg, "nocompress"))
188 mdio.md_options &= ~MD_COMPRESS;
189 else if (!strcmp(optarg, "force"))
190 mdio.md_options |= MD_FORCE;
191 else if (!strcmp(optarg, "noforce"))
192 mdio.md_options &= ~MD_FORCE;
193 else if (!strcmp(optarg, "readonly"))
194 mdio.md_options |= MD_READONLY;
195 else if (!strcmp(optarg, "noreadonly"))
196 mdio.md_options &= ~MD_READONLY;
197 else if (!strcmp(optarg, "ro"))
198 mdio.md_options |= MD_READONLY;
199 else if (!strcmp(optarg, "noro"))
200 mdio.md_options &= ~MD_READONLY;
201 else if (!strcmp(optarg, "reserve"))
202 mdio.md_options |= MD_RESERVE;
203 else if (!strcmp(optarg, "noreserve"))
204 mdio.md_options &= ~MD_RESERVE;
205 else if (!strcmp(optarg, "verify"))
206 mdio.md_options |= MD_VERIFY;
207 else if (!strcmp(optarg, "noverify"))
208 mdio.md_options &= ~MD_VERIFY;
210 errx(1, "unknown option: %s", optarg);
213 mdio.md_sectorsize = strtoul(optarg, &p, 0);
217 errx(1, "-s can be passed only once");
219 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
220 if (p == NULL || *p == '\0')
221 mdio.md_mediasize *= DEV_BSIZE;
222 else if (*p == 'b' || *p == 'B')
224 else if (*p == 'k' || *p == 'K')
225 mdio.md_mediasize <<= 10;
226 else if (*p == 'm' || *p == 'M')
227 mdio.md_mediasize <<= 20;
228 else if (*p == 'g' || *p == 'G')
229 mdio.md_mediasize <<= 30;
230 else if (*p == 't' || *p == 'T') {
231 mdio.md_mediasize <<= 30;
232 mdio.md_mediasize <<= 10;
233 } else if (*p == 'p' || *p == 'P') {
234 mdio.md_mediasize <<= 30;
235 mdio.md_mediasize <<= 20;
237 errx(1, "unknown suffix on -s argument");
240 if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
241 optarg += sizeof(_PATH_DEV) - 1;
242 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
243 optarg += sizeof(MD_NAME) - 1;
250 mdio.md_fwsectors = strtoul(optarg, &p, 0);
253 mdio.md_fwheads = strtoul(optarg, &p, 0);
256 strlcpy(mdio.md_label, optarg, PATH_MAX);
269 if (action == ATTACH) {
272 * Try to infer the type based on other arguments.
274 if (fflag != NULL || argc > 0) {
275 /* Imply ``-t vnode'' */
276 mdio.md_type = MD_VNODE;
277 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
279 } else if (sflag != NULL) {
280 /* Imply ``-t swap'' */
281 mdio.md_type = MD_SWAP;
282 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
285 errx(1, "unable to determine type");
288 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
289 errx(1, "only -t vnode can be used with file name");
291 if (mdio.md_type == MD_VNODE) {
302 if ((mdio.md_options & MD_READONLY) == 0 &&
303 access(mdio.md_file, W_OK) < 0 &&
304 (errno == EACCES || errno == EPERM ||
306 warnx("WARNING: opening backing store: %s "
307 "readonly", mdio.md_file);
308 mdio.md_options |= MD_READONLY;
312 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
313 mdio.md_type == MD_NULL) && sflag == NULL)
314 errx(1, "must specify -s for -t malloc, -t swap, "
316 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
317 errx(1, "must specify -f for -t vnode");
319 if (mdio.md_sectorsize != 0)
320 errx(1, "-S can only be used with -a");
321 if (action != RESIZE && sflag != NULL)
322 errx(1, "-s can only be used with -a and -r");
323 if (mdio.md_fwsectors != 0)
324 errx(1, "-x can only be used with -a");
325 if (mdio.md_fwheads != 0)
326 errx(1, "-y can only be used with -a");
327 if (fflag != NULL && action != LIST)
328 errx(1, "-f can only be used with -a and -l");
330 errx(1, "-t can only be used with -a");
332 errx(1, "file can only be used with -a");
333 if ((action != DETACH && action != RESIZE) &&
334 (mdio.md_options & ~MD_AUTOUNIT) != 0)
335 errx(1, "-o can only be used with -a, -d, and -r");
336 if (action == DETACH &&
337 (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
338 errx(1, "only -o [no]force can be used with -d");
339 if (action == RESIZE &&
340 (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
341 errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
344 if (action == RESIZE && sflag == NULL)
345 errx(1, "must specify -s for -r");
347 if (action != LIST && vflag == OPT_VERBOSE)
348 errx(1, "-v can only be used with -l");
351 mdio.md_unit = strtoul(uflag, &p, 0);
352 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
353 errx(1, "bad unit: %s", uflag);
354 mdio.md_options &= ~MD_AUTOUNIT;
357 mdio.md_version = MDIOVERSION;
359 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
360 err(1, "failed to load geom_md module");
362 fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
364 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
366 if (action == ATTACH) {
367 i = ioctl(fd, MDIOCATTACH, &mdio);
369 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
370 if (mdio.md_options & MD_AUTOUNIT)
371 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
372 } else if (action == DETACH) {
373 if (mdio.md_options & MD_AUTOUNIT)
374 errx(1, "-d requires -u");
375 i = ioctl(fd, MDIOCDETACH, &mdio);
377 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
378 } else if (action == RESIZE) {
379 if (mdio.md_options & MD_AUTOUNIT)
380 errx(1, "-r requires -u");
381 i = ioctl(fd, MDIOCRESIZE, &mdio);
383 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
384 } else if (action == LIST) {
385 if (mdio.md_options & MD_AUTOUNIT) {
387 * Listing all devices. This is why we pass NULL
388 * together with OPT_LIST.
390 return (md_list(NULL, OPT_LIST | vflag, fflag));
392 return (md_query(uflag, vflag, fflag));
400 md_set_file(const char *fn)
405 if (realpath(fn, mdio.md_file) == NULL)
406 err(1, "could not find full path for %s", fn);
407 fd = open(mdio.md_file, O_RDONLY);
409 err(1, "could not open %s", fn);
410 if (fstat(fd, &sb) == -1)
411 err(1, "could not stat %s", fn);
412 if (!S_ISREG(sb.st_mode))
413 errx(1, "%s is not a regular file", fn);
414 if (mdio.md_mediasize == 0)
415 mdio.md_mediasize = sb.st_size;
420 * Lists md(4) disks. Is used also as a query routine, since it handles XML
421 * interface. 'units' can be NULL for listing memory disks. It might be
422 * coma-separated string containing md(4) disk names. 'opt' distinguished
423 * between list and query mode.
426 md_list(const char *units, int opt, const char *fflag)
429 struct gprovider *pp;
436 int retcode, ffound, ufound;
438 const char *type, *file, *label;
440 type = file = length = NULL;
442 retcode = geom_gettree(&gm);
445 retcode = geom_stats_open();
448 sq = geom_stats_snapshot_get();
453 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
454 gid = geom_lookupid(&gm, gsp->id);
457 if (gid->lg_what == ISPROVIDER) {
461 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
463 if ((opt & OPT_UNIT) && (units != NULL)) {
464 retcode = md_find(units, pp->lg_name);
471 type = geom_config_get(gc, "type");
472 if (type != NULL && (strcmp(type, "vnode") == 0 ||
473 strcmp(type, "preload") == 0)) {
474 file = geom_config_get(gc, "file");
476 strcmp(fflag, file) != 0)
480 } else if (fflag != NULL)
482 if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
483 printf("%s", pp->lg_name + 2);
485 printf("%s", pp->lg_name);
487 if (opt & OPT_VERBOSE ||
488 ((opt & OPT_UNIT) && fflag == NULL)) {
489 length = geom_config_get(gc, "length");
490 printf("\t%s\t", type);
492 md_prthumanval(length);
495 printf("\t%s", file);
497 label = geom_config_get(gc, "label");
500 printf("\t%s", label);
503 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
509 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
511 /* XXX: Check if it's enough to clean everything. */
512 geom_stats_snapshot_free(sq);
513 if (opt & OPT_UNIT) {
514 if (((fflag == NULL) && ufound) ||
515 ((fflag == NULL) && (units != NULL) && ufound) ||
516 ((fflag != NULL) && ffound) ||
517 ((fflag != NULL) && (units != NULL) && ufound && ffound))
519 } else if (opt & OPT_LIST) {
520 if ((fflag == NULL) ||
521 ((fflag != NULL) && ffound))
528 * Returns value of 'name' from gconfig structure.
531 geom_config_get(struct gconf *g, const char *name)
535 LIST_FOREACH(gce, g, lg_config) {
536 if (strcmp(gce->lg_name, name) == 0)
537 return (gce->lg_val);
543 * List is comma separated list of MD disks. name is a
544 * device name we look for. Returns 1 if found and 0
548 md_find(const char *list, const char *name)
558 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
559 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
560 u += sizeof(_PATH_DEV) - 1;
561 /* Just in case user specified number instead of full name */
562 snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
563 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
573 md_prthumanval(char *length)
580 bytes = strtoumax(length, &endptr, 10);
581 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
583 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
584 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
585 (void)printf("%6s", buf);
589 md_query(const char *name, const int opt, const char *fflag)
592 return (md_list(name, opt | OPT_UNIT, fflag));