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 int md_find(char *, const char *);
42 static int md_query(char *name);
43 static int md_list(char *units, int opt);
44 static char *geom_config_get(struct gconf *g, char *name);
45 static void md_prthumanval(char *length);
47 #define OPT_VERBOSE 0x01
52 #define CLASS_NAME_MD "MD"
58 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
59 " [-s size] [-S sectorsize] [-u unit]\n"
60 " [-x sectors/track] [-y heads/cyl]\n"
61 " mdconfig -d -u unit\n"
62 " mdconfig -l [-n] [-u unit]\n");
63 fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n");
64 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
65 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
66 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n");
67 fprintf(stderr, "\t\t %%dt (TB)\n");
72 main(int argc, char **argv)
79 bzero(&mdio, sizeof(mdio));
80 mdio.md_file = malloc(PATH_MAX);
81 if (mdio.md_file == NULL)
82 err(1, "could not allocate memory");
83 bzero(mdio.md_file, PATH_MAX);
85 ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:");
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;
145 if (realpath(optarg, mdio.md_file) == NULL) {
146 err(1, "could not find full path for %s",
149 fd = open(mdio.md_file, O_RDONLY);
151 err(1, "could not open %s", optarg);
152 else if (mdio.md_mediasize == 0) {
155 if (fstat(fd, &sb) == -1)
156 err(1, "could not stat %s", optarg);
157 mdio.md_mediasize = sb.st_size;
164 if (!strcmp(optarg, "async"))
165 mdio.md_options |= MD_ASYNC;
166 else if (!strcmp(optarg, "noasync"))
167 mdio.md_options &= ~MD_ASYNC;
168 else if (!strcmp(optarg, "cluster"))
169 mdio.md_options |= MD_CLUSTER;
170 else if (!strcmp(optarg, "nocluster"))
171 mdio.md_options &= ~MD_CLUSTER;
172 else if (!strcmp(optarg, "compress"))
173 mdio.md_options |= MD_COMPRESS;
174 else if (!strcmp(optarg, "nocompress"))
175 mdio.md_options &= ~MD_COMPRESS;
176 else if (!strcmp(optarg, "force"))
177 mdio.md_options |= MD_FORCE;
178 else if (!strcmp(optarg, "noforce"))
179 mdio.md_options &= ~MD_FORCE;
180 else if (!strcmp(optarg, "readonly"))
181 mdio.md_options |= MD_READONLY;
182 else if (!strcmp(optarg, "noreadonly"))
183 mdio.md_options &= ~MD_READONLY;
184 else if (!strcmp(optarg, "reserve"))
185 mdio.md_options |= MD_RESERVE;
186 else if (!strcmp(optarg, "noreserve"))
187 mdio.md_options &= ~MD_RESERVE;
189 errx(1, "Unknown option: %s.", optarg);
194 mdio.md_sectorsize = strtoul(optarg, &p, 0);
203 /* Imply ``-t swap'' */
204 mdio.md_type = MD_SWAP;
205 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
210 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
211 if (p == NULL || *p == '\0')
212 mdio.md_mediasize *= DEV_BSIZE;
213 else if (*p == 'b' || *p == 'B')
215 else if (*p == 'k' || *p == 'K')
216 mdio.md_mediasize <<= 10;
217 else if (*p == 'm' || *p == 'M')
218 mdio.md_mediasize <<= 20;
219 else if (*p == 'g' || *p == 'G')
220 mdio.md_mediasize <<= 30;
221 else if (*p == 't' || *p == 'T') {
222 mdio.md_mediasize <<= 30;
223 mdio.md_mediasize <<= 10;
225 errx(1, "Unknown suffix on -s argument");
228 if (cmdline != 2 && cmdline != 3)
230 if (!strncmp(optarg, "/dev/", 5))
232 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
233 optarg += sizeof(MD_NAME) - 1;
234 mdio.md_unit = strtoul(optarg, &p, 0);
235 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
236 errx(1, "bad unit: %s", optarg);
238 mdio.md_options &= ~MD_AUTOUNIT;
243 mdio.md_fwsectors = strtoul(optarg, &p, 0);
248 mdio.md_fwheads = strtoul(optarg, &p, 0);
254 mdio.md_version = MDIOVERSION;
256 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
257 err(1, "failed to load geom_md module");
259 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
261 err(1, "open(/dev/%s)", MDCTL_NAME);
263 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
264 if (mdio.md_mediasize == 0)
265 errx(1, "must specify -s for -t malloc or -t swap");
266 if (cmdline == 2 && mdio.md_type == MD_VNODE)
267 if (mdio.md_file[0] == '\0')
268 errx(1, "must specify -f for -t vnode");
269 if (mdio.md_type == MD_VNODE &&
270 (mdio.md_options & MD_READONLY) == 0) {
271 if (access(mdio.md_file, W_OK) < 0 &&
272 (errno == EACCES || errno == EPERM || errno == EROFS)) {
274 "WARNING: opening backing store: %s readonly\n",
276 mdio.md_options |= MD_READONLY;
279 if (action == LIST) {
280 if (mdio.md_options & MD_AUTOUNIT) {
282 * Listing all devices. This is why we pass NULL
283 * together with OPT_LIST.
285 md_list(NULL, OPT_LIST);
287 return (md_query(mdunit));
289 } else if (action == ATTACH) {
292 i = ioctl(fd, MDIOCATTACH, &mdio);
294 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
295 if (mdio.md_options & MD_AUTOUNIT)
296 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
297 } else if (action == DETACH) {
298 if (mdio.md_options & MD_AUTOUNIT)
300 i = ioctl(fd, MDIOCDETACH, &mdio);
302 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
310 * Lists md(4) disks. Is used also as a query routine, since it handles XML
311 * interface. 'units' can be NULL for listing memory disks. It might be
312 * coma-separated string containing md(4) disk names. 'opt' distinguished
313 * between list and query mode.
316 md_list(char *units, int opt)
319 struct gprovider *pp;
327 char *type, *file, *length;
329 type = file = length = NULL;
331 retcode = geom_gettree(&gm);
334 retcode = geom_stats_open();
337 sq = geom_stats_snapshot_get();
342 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
343 gid = geom_lookupid(&gm, gsp->id);
346 if (gid->lg_what == ISPROVIDER) {
350 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
352 if ((opt & OPT_UNIT) && (units != NULL)) {
353 retcode = md_find(units, pp->lg_name);
360 printf("%s", pp->lg_name);
361 if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
362 type = geom_config_get(gc, "type");
363 if (strcmp(type, "vnode") == 0)
364 file = geom_config_get(gc, "file");
365 length = geom_config_get(gc, "length");
368 printf("\t%s\t", type);
369 md_prthumanval(length);
371 printf("\t%s", file);
382 if ((opt & OPT_LIST) && (opt & OPT_DONE))
384 /* XXX: Check if it's enough to clean everything. */
385 geom_stats_snapshot_free(sq);
386 if ((opt & OPT_UNIT) && found)
393 * Returns value of 'name' from gconfig structure.
396 geom_config_get(struct gconf *g, char *name)
400 LIST_FOREACH(gce, g, lg_config) {
401 if (strcmp(gce->lg_name, name) == 0)
402 return (gce->lg_val);
408 * List is comma separated list of MD disks. name is a
409 * device name we look for. Returns 1 if found and 0
413 md_find(char *list, const char *name)
423 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
424 if (strncmp(u, "/dev/", 5) == 0)
426 /* Just in case user specified number instead of full name */
427 snprintf(num, sizeof(num), "md%s", u);
428 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
438 md_prthumanval(char *length)
444 bytes = strtoul(length, &endptr, 10);
445 if (bytes == (unsigned)ULONG_MAX || *endptr != '\0')
447 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
448 bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
449 (void)printf("%6s", buf);
455 return (md_list(name, OPT_UNIT));