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 [-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;
165 if (!strcmp(optarg, "async"))
166 mdio.md_options |= MD_ASYNC;
167 else if (!strcmp(optarg, "noasync"))
168 mdio.md_options &= ~MD_ASYNC;
169 else if (!strcmp(optarg, "cluster"))
170 mdio.md_options |= MD_CLUSTER;
171 else if (!strcmp(optarg, "nocluster"))
172 mdio.md_options &= ~MD_CLUSTER;
173 else if (!strcmp(optarg, "compress"))
174 mdio.md_options |= MD_COMPRESS;
175 else if (!strcmp(optarg, "nocompress"))
176 mdio.md_options &= ~MD_COMPRESS;
177 else if (!strcmp(optarg, "force"))
178 mdio.md_options |= MD_FORCE;
179 else if (!strcmp(optarg, "noforce"))
180 mdio.md_options &= ~MD_FORCE;
181 else if (!strcmp(optarg, "readonly"))
182 mdio.md_options |= MD_READONLY;
183 else if (!strcmp(optarg, "noreadonly"))
184 mdio.md_options &= ~MD_READONLY;
185 else if (!strcmp(optarg, "reserve"))
186 mdio.md_options |= MD_RESERVE;
187 else if (!strcmp(optarg, "noreserve"))
188 mdio.md_options &= ~MD_RESERVE;
190 errx(1, "Unknown option: %s.", optarg);
195 mdio.md_sectorsize = strtoul(optarg, &p, 0);
204 /* Imply ``-t swap'' */
205 mdio.md_type = MD_SWAP;
206 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
211 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
212 if (p == NULL || *p == '\0')
213 mdio.md_mediasize *= DEV_BSIZE;
214 else if (*p == 'b' || *p == 'B')
216 else if (*p == 'k' || *p == 'K')
217 mdio.md_mediasize <<= 10;
218 else if (*p == 'm' || *p == 'M')
219 mdio.md_mediasize <<= 20;
220 else if (*p == 'g' || *p == 'G')
221 mdio.md_mediasize <<= 30;
222 else if (*p == 't' || *p == 'T') {
223 mdio.md_mediasize <<= 30;
224 mdio.md_mediasize <<= 10;
226 errx(1, "Unknown suffix on -s argument");
229 if (cmdline != 2 && cmdline != 3)
231 if (!strncmp(optarg, "/dev/", 5))
233 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
234 optarg += sizeof(MD_NAME) - 1;
235 mdio.md_unit = strtoul(optarg, &p, 0);
236 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
237 errx(1, "bad unit: %s", optarg);
239 mdio.md_options &= ~MD_AUTOUNIT;
249 mdio.md_fwsectors = strtoul(optarg, &p, 0);
254 mdio.md_fwheads = strtoul(optarg, &p, 0);
260 mdio.md_version = MDIOVERSION;
262 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
263 err(1, "failed to load geom_md module");
265 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
267 err(1, "open(/dev/%s)", MDCTL_NAME);
269 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
270 if (mdio.md_mediasize == 0)
271 errx(1, "must specify -s for -t malloc or -t swap");
272 if (cmdline == 2 && mdio.md_type == MD_VNODE)
273 if (mdio.md_file[0] == '\0')
274 errx(1, "must specify -f for -t vnode");
275 if (mdio.md_type == MD_VNODE &&
276 (mdio.md_options & MD_READONLY) == 0) {
277 if (access(mdio.md_file, W_OK) < 0 &&
278 (errno == EACCES || errno == EPERM || errno == EROFS)) {
280 "WARNING: opening backing store: %s readonly\n",
282 mdio.md_options |= MD_READONLY;
285 if (action == LIST) {
286 if (mdio.md_options & MD_AUTOUNIT) {
288 * Listing all devices. This is why we pass NULL
289 * together with OPT_LIST.
291 md_list(NULL, OPT_LIST | vflag);
293 return (md_query(mdunit));
295 } else if (action == ATTACH) {
298 i = ioctl(fd, MDIOCATTACH, &mdio);
300 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
301 if (mdio.md_options & MD_AUTOUNIT)
302 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
303 } else if (action == DETACH) {
304 if (mdio.md_options & MD_AUTOUNIT)
306 i = ioctl(fd, MDIOCDETACH, &mdio);
308 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
316 * Lists md(4) disks. Is used also as a query routine, since it handles XML
317 * interface. 'units' can be NULL for listing memory disks. It might be
318 * coma-separated string containing md(4) disk names. 'opt' distinguished
319 * between list and query mode.
322 md_list(char *units, int opt)
325 struct gprovider *pp;
333 char *type, *file, *length;
335 type = file = length = NULL;
337 retcode = geom_gettree(&gm);
340 retcode = geom_stats_open();
343 sq = geom_stats_snapshot_get();
348 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
349 gid = geom_lookupid(&gm, gsp->id);
352 if (gid->lg_what == ISPROVIDER) {
356 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
358 if ((opt & OPT_UNIT) && (units != NULL)) {
359 retcode = md_find(units, pp->lg_name);
366 printf("%s", pp->lg_name);
367 if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
368 type = geom_config_get(gc, "type");
369 if (strcmp(type, "vnode") == 0)
370 file = geom_config_get(gc, "file");
371 length = geom_config_get(gc, "length");
374 printf("\t%s\t", type);
375 md_prthumanval(length);
377 printf("\t%s", file);
382 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
388 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
390 /* XXX: Check if it's enough to clean everything. */
391 geom_stats_snapshot_free(sq);
392 if ((opt & OPT_UNIT) && found)
399 * Returns value of 'name' from gconfig structure.
402 geom_config_get(struct gconf *g, char *name)
406 LIST_FOREACH(gce, g, lg_config) {
407 if (strcmp(gce->lg_name, name) == 0)
408 return (gce->lg_val);
414 * List is comma separated list of MD disks. name is a
415 * device name we look for. Returns 1 if found and 0
419 md_find(char *list, const char *name)
429 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
430 if (strncmp(u, "/dev/", 5) == 0)
432 /* Just in case user specified number instead of full name */
433 snprintf(num, sizeof(num), "md%s", u);
434 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
444 md_prthumanval(char *length)
450 bytes = strtoul(length, &endptr, 10);
451 if (bytes == (unsigned)ULONG_MAX || *endptr != '\0')
453 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
454 bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
455 (void)printf("%6s", buf);
461 return (md_list(name, OPT_UNIT));