2 * Copyright (c) 2000-2004 Poul-Henning Kamp <phk@FreeBSD.org>
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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>
57 static struct md_ioctl mdio;
58 static enum {UNSET, ATTACH, DETACH, RESIZE, LIST} action = UNSET;
61 static void usage(void);
62 static void md_set_file(const char *);
63 static int md_find(const char *, const char *);
64 static int md_query(const char *, const int, const char *);
65 static int md_list(const char *, int, const char *);
66 static char *geom_config_get(struct gconf *g, const char *name);
67 static void md_prthumanval(char *length);
69 #define OPT_VERBOSE 0x01
74 #define CLASS_NAME_MD "MD"
81 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
82 " [-s size] [-S sectorsize] [-u unit]\n"
83 " [-x sectors/track] [-y heads/cylinder]\n"
84 " mdconfig -d -u unit [-o [no]force]\n"
85 " mdconfig -r -u unit -s size [-o [no]force]\n"
86 " mdconfig -l [-v] [-n] [-f file] [-u unit]\n"
88 fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n");
89 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
90 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
91 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n");
92 fprintf(stderr, "\t\t %%dt (TB)\n");
97 main(int argc, char **argv)
101 char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
103 bzero(&mdio, sizeof(mdio));
104 mdio.md_file = malloc(PATH_MAX);
105 if (mdio.md_file == NULL)
106 err(1, "could not allocate memory");
108 bzero(mdio.md_file, PATH_MAX);
113 while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:")) != -1) {
116 if (action != UNSET && action != ATTACH)
117 errx(1, "-a is mutually exclusive "
118 "with -d, -r, and -l");
122 if (action != UNSET && action != DETACH)
123 errx(1, "-d is mutually exclusive "
124 "with -a, -r, and -l");
126 mdio.md_options |= MD_AUTOUNIT;
129 if (action != UNSET && action != RESIZE)
130 errx(1, "-r is mutually exclusive "
131 "with -a, -d, and -l");
133 mdio.md_options |= MD_AUTOUNIT;
136 if (action != UNSET && action != LIST)
137 errx(1, "-l is mutually exclusive "
138 "with -a, -r, and -d");
140 mdio.md_options |= MD_AUTOUNIT;
147 errx(1, "-t can be passed only once");
149 if (!strcmp(optarg, "malloc")) {
150 mdio.md_type = MD_MALLOC;
151 mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
152 } else if (!strcmp(optarg, "vnode")) {
153 mdio.md_type = MD_VNODE;
154 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
155 } else if (!strcmp(optarg, "swap")) {
156 mdio.md_type = MD_SWAP;
157 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
159 errx(1, "unknown type: %s", optarg);
163 errx(1, "-f can be passed only once");
164 fflag = realpath(optarg, NULL);
169 if (!strcmp(optarg, "async"))
170 mdio.md_options |= MD_ASYNC;
171 else if (!strcmp(optarg, "noasync"))
172 mdio.md_options &= ~MD_ASYNC;
173 else if (!strcmp(optarg, "cluster"))
174 mdio.md_options |= MD_CLUSTER;
175 else if (!strcmp(optarg, "nocluster"))
176 mdio.md_options &= ~MD_CLUSTER;
177 else if (!strcmp(optarg, "compress"))
178 mdio.md_options |= MD_COMPRESS;
179 else if (!strcmp(optarg, "nocompress"))
180 mdio.md_options &= ~MD_COMPRESS;
181 else if (!strcmp(optarg, "force"))
182 mdio.md_options |= MD_FORCE;
183 else if (!strcmp(optarg, "noforce"))
184 mdio.md_options &= ~MD_FORCE;
185 else if (!strcmp(optarg, "readonly"))
186 mdio.md_options |= MD_READONLY;
187 else if (!strcmp(optarg, "noreadonly"))
188 mdio.md_options &= ~MD_READONLY;
189 else if (!strcmp(optarg, "reserve"))
190 mdio.md_options |= MD_RESERVE;
191 else if (!strcmp(optarg, "noreserve"))
192 mdio.md_options &= ~MD_RESERVE;
194 errx(1, "unknown option: %s", optarg);
197 mdio.md_sectorsize = strtoul(optarg, &p, 0);
201 errx(1, "-s can be passed only once");
203 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
204 if (p == NULL || *p == '\0')
205 mdio.md_mediasize *= DEV_BSIZE;
206 else if (*p == 'b' || *p == 'B')
208 else if (*p == 'k' || *p == 'K')
209 mdio.md_mediasize <<= 10;
210 else if (*p == 'm' || *p == 'M')
211 mdio.md_mediasize <<= 20;
212 else if (*p == 'g' || *p == 'G')
213 mdio.md_mediasize <<= 30;
214 else if (*p == 't' || *p == 'T') {
215 mdio.md_mediasize <<= 30;
216 mdio.md_mediasize <<= 10;
218 errx(1, "unknown suffix on -s argument");
221 if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
222 optarg += sizeof(_PATH_DEV) - 1;
223 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
224 optarg += sizeof(MD_NAME) - 1;
231 mdio.md_fwsectors = strtoul(optarg, &p, 0);
234 mdio.md_fwheads = strtoul(optarg, &p, 0);
247 if (action == ATTACH) {
250 * Try to infer the type based on other arguments.
252 if (fflag != NULL || argc > 0) {
253 /* Imply ``-t vnode'' */
254 mdio.md_type = MD_VNODE;
255 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
257 } else if (sflag != NULL) {
258 /* Imply ``-t swap'' */
259 mdio.md_type = MD_SWAP;
260 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
263 errx(1, "unable to determine type");
266 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
267 errx(1, "only -t vnode can be used with file name");
269 if (mdio.md_type == MD_VNODE) {
280 if ((mdio.md_options & MD_READONLY) == 0 &&
281 access(mdio.md_file, W_OK) < 0 &&
282 (errno == EACCES || errno == EPERM ||
284 warnx("WARNING: opening backing store: %s "
285 "readonly", mdio.md_file);
286 mdio.md_options |= MD_READONLY;
290 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP) &&
292 errx(1, "must specify -s for -t malloc or -t swap");
293 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
294 errx(1, "must specify -f for -t vnode");
296 if (mdio.md_sectorsize != 0)
297 errx(1, "-S can only be used with -a");
298 if (action != RESIZE && sflag != NULL)
299 errx(1, "-s can only be used with -a and -r");
300 if (mdio.md_fwsectors != 0)
301 errx(1, "-x can only be used with -a");
302 if (mdio.md_fwheads != 0)
303 errx(1, "-y can only be used with -a");
304 if (fflag != NULL && action != LIST)
305 errx(1, "-f can only be used with -a and -l");
307 errx(1, "-t can only be used with -a");
309 errx(1, "file can only be used with -a");
310 if ((action != DETACH && action != RESIZE) &&
311 (mdio.md_options & ~MD_AUTOUNIT) != 0)
312 errx(1, "-o can only be used with -a, -d, and -r");
313 if (action == DETACH &&
314 (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
315 errx(1, "only -o [no]force can be used with -d");
316 if (action == RESIZE &&
317 (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
318 errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
321 if (action == RESIZE && sflag == NULL)
322 errx(1, "must specify -s for -r");
324 if (action != LIST && vflag == OPT_VERBOSE)
325 errx(1, "-v can only be used with -l");
328 mdio.md_unit = strtoul(uflag, &p, 0);
329 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
330 errx(1, "bad unit: %s", uflag);
331 mdio.md_options &= ~MD_AUTOUNIT;
334 mdio.md_version = MDIOVERSION;
336 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
337 err(1, "failed to load geom_md module");
339 fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
341 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
343 if (action == ATTACH) {
344 i = ioctl(fd, MDIOCATTACH, &mdio);
346 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
347 if (mdio.md_options & MD_AUTOUNIT)
348 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
349 } else if (action == DETACH) {
350 if (mdio.md_options & MD_AUTOUNIT)
351 errx(1, "-d requires -u");
352 i = ioctl(fd, MDIOCDETACH, &mdio);
354 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
355 } else if (action == RESIZE) {
356 if (mdio.md_options & MD_AUTOUNIT)
357 errx(1, "-r requires -u");
358 i = ioctl(fd, MDIOCRESIZE, &mdio);
360 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
361 } else if (action == LIST) {
362 if (mdio.md_options & MD_AUTOUNIT) {
364 * Listing all devices. This is why we pass NULL
365 * together with OPT_LIST.
367 return (md_list(NULL, OPT_LIST | vflag, fflag));
369 return (md_query(uflag, vflag, fflag));
377 md_set_file(const char *fn)
382 if (realpath(fn, mdio.md_file) == NULL)
383 err(1, "could not find full path for %s", fn);
384 fd = open(mdio.md_file, O_RDONLY);
386 err(1, "could not open %s", fn);
387 if (fstat(fd, &sb) == -1)
388 err(1, "could not stat %s", fn);
389 if (!S_ISREG(sb.st_mode))
390 errx(1, "%s is not a regular file", fn);
391 if (mdio.md_mediasize == 0)
392 mdio.md_mediasize = sb.st_size;
397 * Lists md(4) disks. Is used also as a query routine, since it handles XML
398 * interface. 'units' can be NULL for listing memory disks. It might be
399 * coma-separated string containing md(4) disk names. 'opt' distinguished
400 * between list and query mode.
403 md_list(const char *units, int opt, const char *fflag)
406 struct gprovider *pp;
413 int retcode, ffound, ufound;
414 char *type, *file, *length;
416 type = file = length = NULL;
418 retcode = geom_gettree(&gm);
421 retcode = geom_stats_open();
424 sq = geom_stats_snapshot_get();
429 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
430 gid = geom_lookupid(&gm, gsp->id);
433 if (gid->lg_what == ISPROVIDER) {
437 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
439 if ((opt & OPT_UNIT) && (units != NULL)) {
440 retcode = md_find(units, pp->lg_name);
447 type = geom_config_get(gc, "type");
448 if (strcmp(type, "vnode") == 0) {
449 file = geom_config_get(gc, "file");
451 strcmp(fflag, file) != 0)
455 } else if (fflag != NULL)
457 if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
458 printf("%s", pp->lg_name + 2);
460 printf("%s", pp->lg_name);
462 if (opt & OPT_VERBOSE ||
463 ((opt & OPT_UNIT) && fflag == NULL)) {
464 length = geom_config_get(gc, "length");
465 printf("\t%s\t", type);
467 md_prthumanval(length);
469 printf("\t%s", file);
474 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
480 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
482 /* XXX: Check if it's enough to clean everything. */
483 geom_stats_snapshot_free(sq);
484 if (((opt & OPT_UNIT) && (fflag == NULL) && ufound) ||
485 ((opt & OPT_UNIT) == 0 && (fflag != NULL) && ffound) ||
486 ((opt & OPT_UNIT) && (fflag != NULL) && ufound && ffound))
493 * Returns value of 'name' from gconfig structure.
496 geom_config_get(struct gconf *g, const char *name)
500 LIST_FOREACH(gce, g, lg_config) {
501 if (strcmp(gce->lg_name, name) == 0)
502 return (gce->lg_val);
508 * List is comma separated list of MD disks. name is a
509 * device name we look for. Returns 1 if found and 0
513 md_find(const char *list, const char *name)
523 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
524 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
525 u += sizeof(_PATH_DEV) - 1;
526 /* Just in case user specified number instead of full name */
527 snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
528 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
538 md_prthumanval(char *length)
545 bytes = strtoumax(length, &endptr, 10);
546 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
548 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
549 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
550 (void)printf("%6s", buf);
554 md_query(const char *name, const int opt, const char *fflag)
557 return (md_list(name, opt | OPT_UNIT, fflag));