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 [-o [no]force]\n"
62 " mdconfig -l [-v] [-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");
84 bzero(mdio.md_file, PATH_MAX);
86 ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:");
100 mdio.md_options = MD_AUTOUNIT;
107 mdio.md_options = MD_AUTOUNIT;
116 if (!strcmp(optarg, "malloc")) {
117 mdio.md_type = MD_MALLOC;
118 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
119 } else if (!strcmp(optarg, "preload")) {
120 mdio.md_type = MD_PRELOAD;
122 } else if (!strcmp(optarg, "vnode")) {
123 mdio.md_type = MD_VNODE;
124 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
125 } else if (!strcmp(optarg, "swap")) {
126 mdio.md_type = MD_SWAP;
127 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
139 /* Imply ``-t vnode'' */
140 mdio.md_type = MD_VNODE;
141 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
146 if (realpath(optarg, mdio.md_file) == NULL) {
147 err(1, "could not find full path for %s",
150 fd = open(mdio.md_file, O_RDONLY);
152 err(1, "could not open %s", optarg);
153 else if (mdio.md_mediasize == 0) {
156 if (fstat(fd, &sb) == -1)
157 err(1, "could not stat %s", optarg);
158 mdio.md_mediasize = sb.st_size;
163 if (action == DETACH) {
164 if (!strcmp(optarg, "force"))
165 mdio.md_options |= MD_FORCE;
166 else if (!strcmp(optarg, "noforce"))
167 mdio.md_options &= ~MD_FORCE;
169 errx(1, "Unknown option: %s.", optarg);
175 if (!strcmp(optarg, "async"))
176 mdio.md_options |= MD_ASYNC;
177 else if (!strcmp(optarg, "noasync"))
178 mdio.md_options &= ~MD_ASYNC;
179 else if (!strcmp(optarg, "cluster"))
180 mdio.md_options |= MD_CLUSTER;
181 else if (!strcmp(optarg, "nocluster"))
182 mdio.md_options &= ~MD_CLUSTER;
183 else if (!strcmp(optarg, "compress"))
184 mdio.md_options |= MD_COMPRESS;
185 else if (!strcmp(optarg, "nocompress"))
186 mdio.md_options &= ~MD_COMPRESS;
187 else if (!strcmp(optarg, "force"))
188 mdio.md_options |= MD_FORCE;
189 else if (!strcmp(optarg, "noforce"))
190 mdio.md_options &= ~MD_FORCE;
191 else if (!strcmp(optarg, "readonly"))
192 mdio.md_options |= MD_READONLY;
193 else if (!strcmp(optarg, "noreadonly"))
194 mdio.md_options &= ~MD_READONLY;
195 else if (!strcmp(optarg, "reserve"))
196 mdio.md_options |= MD_RESERVE;
197 else if (!strcmp(optarg, "noreserve"))
198 mdio.md_options &= ~MD_RESERVE;
200 errx(1, "Unknown option: %s.", optarg);
205 mdio.md_sectorsize = strtoul(optarg, &p, 0);
214 /* Imply ``-t swap'' */
215 mdio.md_type = MD_SWAP;
216 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
221 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
222 if (p == NULL || *p == '\0')
223 mdio.md_mediasize *= DEV_BSIZE;
224 else if (*p == 'b' || *p == 'B')
226 else if (*p == 'k' || *p == 'K')
227 mdio.md_mediasize <<= 10;
228 else if (*p == 'm' || *p == 'M')
229 mdio.md_mediasize <<= 20;
230 else if (*p == 'g' || *p == 'G')
231 mdio.md_mediasize <<= 30;
232 else if (*p == 't' || *p == 'T') {
233 mdio.md_mediasize <<= 30;
234 mdio.md_mediasize <<= 10;
236 errx(1, "Unknown suffix on -s argument");
239 if (cmdline != 2 && cmdline != 3)
241 if (!strncmp(optarg, "/dev/", 5))
243 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
244 optarg += sizeof(MD_NAME) - 1;
245 mdio.md_unit = strtoul(optarg, &p, 0);
246 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
247 errx(1, "bad unit: %s", optarg);
249 mdio.md_options &= ~MD_AUTOUNIT;
259 mdio.md_fwsectors = strtoul(optarg, &p, 0);
264 mdio.md_fwheads = strtoul(optarg, &p, 0);
270 mdio.md_version = MDIOVERSION;
272 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
273 err(1, "failed to load geom_md module");
275 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
277 err(1, "open(/dev/%s)", MDCTL_NAME);
279 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
280 if (mdio.md_mediasize == 0)
281 errx(1, "must specify -s for -t malloc or -t swap");
282 if (cmdline == 2 && mdio.md_type == MD_VNODE)
283 if (mdio.md_file[0] == '\0')
284 errx(1, "must specify -f for -t vnode");
285 if (mdio.md_type == MD_VNODE &&
286 (mdio.md_options & MD_READONLY) == 0) {
287 if (access(mdio.md_file, W_OK) < 0 &&
288 (errno == EACCES || errno == EPERM || errno == EROFS)) {
290 "WARNING: opening backing store: %s readonly\n",
292 mdio.md_options |= MD_READONLY;
295 if (action == LIST) {
296 if (mdio.md_options & MD_AUTOUNIT) {
298 * Listing all devices. This is why we pass NULL
299 * together with OPT_LIST.
301 md_list(NULL, OPT_LIST | vflag);
303 return (md_query(mdunit));
305 } else if (action == ATTACH) {
308 i = ioctl(fd, MDIOCATTACH, &mdio);
310 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
311 if (mdio.md_options & MD_AUTOUNIT)
312 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
313 } else if (action == DETACH) {
314 if (mdio.md_options & MD_AUTOUNIT)
316 i = ioctl(fd, MDIOCDETACH, &mdio);
318 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
326 * Lists md(4) disks. Is used also as a query routine, since it handles XML
327 * interface. 'units' can be NULL for listing memory disks. It might be
328 * coma-separated string containing md(4) disk names. 'opt' distinguished
329 * between list and query mode.
332 md_list(char *units, int opt)
335 struct gprovider *pp;
343 char *type, *file, *length;
345 type = file = length = NULL;
347 retcode = geom_gettree(&gm);
350 retcode = geom_stats_open();
353 sq = geom_stats_snapshot_get();
358 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
359 gid = geom_lookupid(&gm, gsp->id);
362 if (gid->lg_what == ISPROVIDER) {
366 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
368 if ((opt & OPT_UNIT) && (units != NULL)) {
369 retcode = md_find(units, pp->lg_name);
376 printf("%s", pp->lg_name);
377 if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
378 type = geom_config_get(gc, "type");
379 if (strcmp(type, "vnode") == 0)
380 file = geom_config_get(gc, "file");
381 length = geom_config_get(gc, "length");
384 printf("\t%s\t", type);
385 md_prthumanval(length);
387 printf("\t%s", file);
392 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
398 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
400 /* XXX: Check if it's enough to clean everything. */
401 geom_stats_snapshot_free(sq);
402 if ((opt & OPT_UNIT) && found)
409 * Returns value of 'name' from gconfig structure.
412 geom_config_get(struct gconf *g, char *name)
416 LIST_FOREACH(gce, g, lg_config) {
417 if (strcmp(gce->lg_name, name) == 0)
418 return (gce->lg_val);
424 * List is comma separated list of MD disks. name is a
425 * device name we look for. Returns 1 if found and 0
429 md_find(char *list, const char *name)
439 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
440 if (strncmp(u, "/dev/", 5) == 0)
442 /* Just in case user specified number instead of full name */
443 snprintf(num, sizeof(num), "md%s", u);
444 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
454 md_prthumanval(char *length)
461 bytes = strtoumax(length, &endptr, 10);
462 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
464 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
465 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
466 (void)printf("%6s", buf);
472 return (md_list(name, OPT_UNIT));