2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
12 #include <sys/param.h>
13 #include <sys/devicestat.h>
14 #include <sys/ioctl.h>
15 #include <sys/linker.h>
16 #include <sys/mdioctl.h>
17 #include <sys/module.h>
18 #include <sys/resource.h>
36 static struct md_ioctl mdio;
37 static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
40 static void usage(void);
41 static void md_set_file(const char *);
42 static int md_find(char *, const char *);
43 static int md_query(char *name);
44 static int md_list(char *units, int opt);
45 static char *geom_config_get(struct gconf *g, const char *name);
46 static void md_prthumanval(char *length);
48 #define OPT_VERBOSE 0x01
53 #define CLASS_NAME_MD "MD"
59 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
60 " [-s size] [-S sectorsize] [-u unit]\n"
61 " [-x sectors/track] [-y heads/cylinder]\n"
62 " mdconfig -d -u unit [-o [no]force]\n"
63 " mdconfig -l [-v] [-n] [-u unit]\n"
65 fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n");
66 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
67 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
68 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n");
69 fprintf(stderr, "\t\t %%dt (TB)\n");
74 main(int argc, char **argv)
81 bzero(&mdio, sizeof(mdio));
82 mdio.md_file = malloc(PATH_MAX);
83 if (mdio.md_file == NULL)
84 err(1, "could not allocate memory");
86 bzero(mdio.md_file, PATH_MAX);
87 while ((ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:")) != -1) {
99 mdio.md_options = MD_AUTOUNIT;
106 mdio.md_options = MD_AUTOUNIT;
115 if (!strcmp(optarg, "malloc")) {
116 mdio.md_type = MD_MALLOC;
117 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
118 } else if (!strcmp(optarg, "preload")) {
119 mdio.md_type = MD_PRELOAD;
121 } else if (!strcmp(optarg, "vnode")) {
122 mdio.md_type = MD_VNODE;
123 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
124 } else if (!strcmp(optarg, "swap")) {
125 mdio.md_type = MD_SWAP;
126 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
138 /* Imply ``-t vnode'' */
139 mdio.md_type = MD_VNODE;
140 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
148 if (action == DETACH) {
149 if (!strcmp(optarg, "force"))
150 mdio.md_options |= MD_FORCE;
151 else if (!strcmp(optarg, "noforce"))
152 mdio.md_options &= ~MD_FORCE;
154 errx(1, "Unknown option: %s.", optarg);
160 if (!strcmp(optarg, "async"))
161 mdio.md_options |= MD_ASYNC;
162 else if (!strcmp(optarg, "noasync"))
163 mdio.md_options &= ~MD_ASYNC;
164 else if (!strcmp(optarg, "cluster"))
165 mdio.md_options |= MD_CLUSTER;
166 else if (!strcmp(optarg, "nocluster"))
167 mdio.md_options &= ~MD_CLUSTER;
168 else if (!strcmp(optarg, "compress"))
169 mdio.md_options |= MD_COMPRESS;
170 else if (!strcmp(optarg, "nocompress"))
171 mdio.md_options &= ~MD_COMPRESS;
172 else if (!strcmp(optarg, "force"))
173 mdio.md_options |= MD_FORCE;
174 else if (!strcmp(optarg, "noforce"))
175 mdio.md_options &= ~MD_FORCE;
176 else if (!strcmp(optarg, "readonly"))
177 mdio.md_options |= MD_READONLY;
178 else if (!strcmp(optarg, "noreadonly"))
179 mdio.md_options &= ~MD_READONLY;
180 else if (!strcmp(optarg, "reserve"))
181 mdio.md_options |= MD_RESERVE;
182 else if (!strcmp(optarg, "noreserve"))
183 mdio.md_options &= ~MD_RESERVE;
185 errx(1, "Unknown option: %s.", optarg);
190 mdio.md_sectorsize = strtoul(optarg, &p, 0);
199 /* Imply ``-t swap'' */
200 mdio.md_type = MD_SWAP;
201 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
206 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
207 if (p == NULL || *p == '\0')
208 mdio.md_mediasize *= DEV_BSIZE;
209 else if (*p == 'b' || *p == 'B')
211 else if (*p == 'k' || *p == 'K')
212 mdio.md_mediasize <<= 10;
213 else if (*p == 'm' || *p == 'M')
214 mdio.md_mediasize <<= 20;
215 else if (*p == 'g' || *p == 'G')
216 mdio.md_mediasize <<= 30;
217 else if (*p == 't' || *p == 'T') {
218 mdio.md_mediasize <<= 30;
219 mdio.md_mediasize <<= 10;
221 errx(1, "Unknown suffix on -s argument");
224 if (cmdline != 2 && cmdline != 3)
226 if (!strncmp(optarg, "/dev/", 5))
228 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
229 optarg += sizeof(MD_NAME) - 1;
230 mdio.md_unit = strtoul(optarg, &p, 0);
231 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
232 errx(1, "bad unit: %s", optarg);
234 mdio.md_options &= ~MD_AUTOUNIT;
244 mdio.md_fwsectors = strtoul(optarg, &p, 0);
249 mdio.md_fwheads = strtoul(optarg, &p, 0);
258 if (action == UNSET) {
262 mdio.md_type = MD_VNODE;
263 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
268 mdio.md_version = MDIOVERSION;
270 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
271 err(1, "failed to load geom_md module");
273 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
275 err(1, "open(/dev/%s)", MDCTL_NAME);
277 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
278 if (mdio.md_mediasize == 0)
279 errx(1, "must specify -s for -t malloc or -t swap");
280 if (cmdline == 2 && mdio.md_type == MD_VNODE)
281 if (mdio.md_file[0] == '\0')
282 errx(1, "must specify -f for -t vnode");
283 if (mdio.md_type == MD_VNODE &&
284 (mdio.md_options & MD_READONLY) == 0) {
285 if (access(mdio.md_file, W_OK) < 0 &&
286 (errno == EACCES || errno == EPERM || errno == EROFS)) {
288 "WARNING: opening backing store: %s readonly\n",
290 mdio.md_options |= MD_READONLY;
293 if (action == LIST) {
294 if (mdio.md_options & MD_AUTOUNIT) {
296 * Listing all devices. This is why we pass NULL
297 * together with OPT_LIST.
299 md_list(NULL, OPT_LIST | vflag);
301 return (md_query(mdunit));
303 } else if (action == ATTACH) {
306 i = ioctl(fd, MDIOCATTACH, &mdio);
308 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
309 if (mdio.md_options & MD_AUTOUNIT)
310 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
311 } else if (action == DETACH) {
312 if (mdio.md_options & MD_AUTOUNIT)
314 i = ioctl(fd, MDIOCDETACH, &mdio);
316 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
324 md_set_file(const char *fn)
329 if (realpath(fn, mdio.md_file) == NULL)
330 err(1, "could not find full path for %s", fn);
331 fd = open(mdio.md_file, O_RDONLY);
333 err(1, "could not open %s", fn);
334 if (fstat(fd, &sb) == -1)
335 err(1, "could not stat %s", fn);
336 if (!S_ISREG(sb.st_mode))
337 errx(1, "%s is not a regular file", fn);
338 if (mdio.md_mediasize == 0)
339 mdio.md_mediasize = sb.st_size;
344 * Lists md(4) disks. Is used also as a query routine, since it handles XML
345 * interface. 'units' can be NULL for listing memory disks. It might be
346 * coma-separated string containing md(4) disk names. 'opt' distinguished
347 * between list and query mode.
350 md_list(char *units, int opt)
353 struct gprovider *pp;
361 char *type, *file, *length;
363 type = file = length = NULL;
365 retcode = geom_gettree(&gm);
368 retcode = geom_stats_open();
371 sq = geom_stats_snapshot_get();
376 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
377 gid = geom_lookupid(&gm, gsp->id);
380 if (gid->lg_what == ISPROVIDER) {
384 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
386 if ((opt & OPT_UNIT) && (units != NULL)) {
387 retcode = md_find(units, pp->lg_name);
394 if (nflag && strncmp(pp->lg_name, "md", 2) == 0)
395 printf("%s", pp->lg_name + 2);
397 printf("%s", pp->lg_name);
399 if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
400 type = geom_config_get(gc, "type");
401 if (strcmp(type, "vnode") == 0)
402 file = geom_config_get(gc, "file");
403 length = geom_config_get(gc, "length");
404 printf("\t%s\t", type);
406 md_prthumanval(length);
408 printf("\t%s", file);
413 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
419 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
421 /* XXX: Check if it's enough to clean everything. */
422 geom_stats_snapshot_free(sq);
423 if ((opt & OPT_UNIT) && found)
430 * Returns value of 'name' from gconfig structure.
433 geom_config_get(struct gconf *g, const char *name)
437 LIST_FOREACH(gce, g, lg_config) {
438 if (strcmp(gce->lg_name, name) == 0)
439 return (gce->lg_val);
445 * List is comma separated list of MD disks. name is a
446 * device name we look for. Returns 1 if found and 0
450 md_find(char *list, const char *name)
460 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
461 if (strncmp(u, "/dev/", 5) == 0)
463 /* Just in case user specified number instead of full name */
464 snprintf(num, sizeof(num), "md%s", u);
465 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
475 md_prthumanval(char *length)
482 bytes = strtoumax(length, &endptr, 10);
483 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
485 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
486 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
487 (void)printf("%6s", buf);
493 return (md_list(name, OPT_UNIT));