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, 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, "vnode")) {
119 mdio.md_type = MD_VNODE;
120 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
121 } else if (!strcmp(optarg, "swap")) {
122 mdio.md_type = MD_SWAP;
123 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
135 /* Imply ``-t vnode'' */
136 mdio.md_type = MD_VNODE;
137 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
145 if (action == DETACH) {
146 if (!strcmp(optarg, "force"))
147 mdio.md_options |= MD_FORCE;
148 else if (!strcmp(optarg, "noforce"))
149 mdio.md_options &= ~MD_FORCE;
151 errx(1, "Unknown option: %s.", optarg);
157 if (!strcmp(optarg, "async"))
158 mdio.md_options |= MD_ASYNC;
159 else if (!strcmp(optarg, "noasync"))
160 mdio.md_options &= ~MD_ASYNC;
161 else if (!strcmp(optarg, "cluster"))
162 mdio.md_options |= MD_CLUSTER;
163 else if (!strcmp(optarg, "nocluster"))
164 mdio.md_options &= ~MD_CLUSTER;
165 else if (!strcmp(optarg, "compress"))
166 mdio.md_options |= MD_COMPRESS;
167 else if (!strcmp(optarg, "nocompress"))
168 mdio.md_options &= ~MD_COMPRESS;
169 else if (!strcmp(optarg, "force"))
170 mdio.md_options |= MD_FORCE;
171 else if (!strcmp(optarg, "noforce"))
172 mdio.md_options &= ~MD_FORCE;
173 else if (!strcmp(optarg, "readonly"))
174 mdio.md_options |= MD_READONLY;
175 else if (!strcmp(optarg, "noreadonly"))
176 mdio.md_options &= ~MD_READONLY;
177 else if (!strcmp(optarg, "reserve"))
178 mdio.md_options |= MD_RESERVE;
179 else if (!strcmp(optarg, "noreserve"))
180 mdio.md_options &= ~MD_RESERVE;
182 errx(1, "Unknown option: %s.", optarg);
187 mdio.md_sectorsize = strtoul(optarg, &p, 0);
196 /* Imply ``-t swap'' */
197 mdio.md_type = MD_SWAP;
198 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
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 (cmdline != 2 && cmdline != 3)
223 if (!strncmp(optarg, "/dev/", 5))
225 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
226 optarg += sizeof(MD_NAME) - 1;
227 mdio.md_unit = strtoul(optarg, &p, 0);
228 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
229 errx(1, "bad unit: %s", optarg);
231 mdio.md_options &= ~MD_AUTOUNIT;
241 mdio.md_fwsectors = strtoul(optarg, &p, 0);
246 mdio.md_fwheads = strtoul(optarg, &p, 0);
255 if (action == UNSET) {
259 mdio.md_type = MD_VNODE;
260 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
265 mdio.md_version = MDIOVERSION;
267 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
268 err(1, "failed to load geom_md module");
270 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
272 err(1, "open(/dev/%s)", MDCTL_NAME);
274 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
275 if (mdio.md_mediasize == 0)
276 errx(1, "must specify -s for -t malloc or -t swap");
277 if (cmdline == 2 && mdio.md_type == MD_VNODE)
278 if (mdio.md_file[0] == '\0')
279 errx(1, "must specify -f for -t vnode");
280 if (mdio.md_type == MD_VNODE &&
281 (mdio.md_options & MD_READONLY) == 0) {
282 if (access(mdio.md_file, W_OK) < 0 &&
283 (errno == EACCES || errno == EPERM || errno == EROFS)) {
285 "WARNING: opening backing store: %s readonly\n",
287 mdio.md_options |= MD_READONLY;
290 if (action == LIST) {
291 if (mdio.md_options & MD_AUTOUNIT) {
293 * Listing all devices. This is why we pass NULL
294 * together with OPT_LIST.
296 md_list(NULL, OPT_LIST | vflag);
298 return (md_query(mdunit));
300 } else if (action == ATTACH) {
303 i = ioctl(fd, MDIOCATTACH, &mdio);
305 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
306 if (mdio.md_options & MD_AUTOUNIT)
307 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
308 } else if (action == DETACH) {
309 if (mdio.md_options & MD_AUTOUNIT)
311 i = ioctl(fd, MDIOCDETACH, &mdio);
313 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
321 md_set_file(const char *fn)
326 if (realpath(fn, mdio.md_file) == NULL)
327 err(1, "could not find full path for %s", fn);
328 fd = open(mdio.md_file, O_RDONLY);
330 err(1, "could not open %s", fn);
331 if (fstat(fd, &sb) == -1)
332 err(1, "could not stat %s", fn);
333 if (!S_ISREG(sb.st_mode))
334 errx(1, "%s is not a regular file", fn);
335 if (mdio.md_mediasize == 0)
336 mdio.md_mediasize = sb.st_size;
341 * Lists md(4) disks. Is used also as a query routine, since it handles XML
342 * interface. 'units' can be NULL for listing memory disks. It might be
343 * coma-separated string containing md(4) disk names. 'opt' distinguished
344 * between list and query mode.
347 md_list(char *units, int opt)
350 struct gprovider *pp;
358 char *type, *file, *length;
360 type = file = length = NULL;
362 retcode = geom_gettree(&gm);
365 retcode = geom_stats_open();
368 sq = geom_stats_snapshot_get();
373 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
374 gid = geom_lookupid(&gm, gsp->id);
377 if (gid->lg_what == ISPROVIDER) {
381 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
383 if ((opt & OPT_UNIT) && (units != NULL)) {
384 retcode = md_find(units, pp->lg_name);
391 if (nflag && strncmp(pp->lg_name, "md", 2) == 0)
392 printf("%s", pp->lg_name + 2);
394 printf("%s", pp->lg_name);
396 if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
397 type = geom_config_get(gc, "type");
398 if (strcmp(type, "vnode") == 0)
399 file = geom_config_get(gc, "file");
400 length = geom_config_get(gc, "length");
401 printf("\t%s\t", type);
403 md_prthumanval(length);
405 printf("\t%s", file);
410 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
416 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
418 /* XXX: Check if it's enough to clean everything. */
419 geom_stats_snapshot_free(sq);
420 if ((opt & OPT_UNIT) && found)
427 * Returns value of 'name' from gconfig structure.
430 geom_config_get(struct gconf *g, const char *name)
434 LIST_FOREACH(gce, g, lg_config) {
435 if (strcmp(gce->lg_name, name) == 0)
436 return (gce->lg_val);
442 * List is comma separated list of MD disks. name is a
443 * device name we look for. Returns 1 if found and 0
447 md_find(char *list, const char *name)
457 for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
458 if (strncmp(u, "/dev/", 5) == 0)
460 /* Just in case user specified number instead of full name */
461 snprintf(num, sizeof(num), "md%s", u);
462 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
472 md_prthumanval(char *length)
479 bytes = strtoumax(length, &endptr, 10);
480 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
482 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
483 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
484 (void)printf("%6s", buf);
490 return (md_list(name, OPT_UNIT));