mdconfig.c revision 221144
1/*
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 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: head/sbin/mdconfig/mdconfig.c 221144 2011-04-27 21:40:49Z des $
10 *
11 */
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>
19#include <sys/stat.h>
20
21#include <assert.h>
22#include <devstat.h>
23#include <err.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <inttypes.h>
27#include <libgeom.h>
28#include <libutil.h>
29#include <stdarg.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35
36static struct md_ioctl mdio;
37static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
38static int nflag;
39
40static void usage(void);
41static int md_find(char *, const char *);
42static int md_query(char *name);
43static int md_list(char *units, int opt);
44static char *geom_config_get(struct gconf *g, const char *name);
45static void md_prthumanval(char *length);
46
47#define OPT_VERBOSE	0x01
48#define OPT_UNIT	0x02
49#define OPT_DONE	0x04
50#define OPT_LIST	0x10
51
52#define CLASS_NAME_MD	"MD"
53
54static void
55usage(void)
56{
57	fprintf(stderr,
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/cylinder]\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");
68	exit(1);
69}
70
71int
72main(int argc, char **argv)
73{
74	int ch, fd, i, vflag;
75	char *p;
76	int cmdline = 0;
77	char *mdunit = NULL;
78
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	vflag = 0;
84	bzero(mdio.md_file, PATH_MAX);
85	for (;;) {
86		ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:");
87		if (ch == -1)
88			break;
89		switch (ch) {
90		case 'a':
91			if (cmdline != 0)
92				usage();
93			action = ATTACH;
94			cmdline = 1;
95			break;
96		case 'd':
97			if (cmdline != 0)
98				usage();
99			action = DETACH;
100			mdio.md_options = MD_AUTOUNIT;
101			cmdline = 3;
102			break;
103		case 'l':
104			if (cmdline != 0)
105				usage();
106			action = LIST;
107			mdio.md_options = MD_AUTOUNIT;
108			cmdline = 3;
109			break;
110		case 'n':
111			nflag = 1;
112			break;
113		case 't':
114			if (cmdline != 1)
115				usage();
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;
121				mdio.md_options = 0;
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;
128			} else {
129				usage();
130			}
131			cmdline=2;
132			break;
133		case 'f':
134			if (cmdline == 0) {
135				action = ATTACH;
136				cmdline = 1;
137			}
138			if (cmdline == 1) {
139				/* Imply ``-t vnode'' */
140				mdio.md_type = MD_VNODE;
141				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
142				cmdline = 2;
143			}
144			if (cmdline != 2)
145				usage();
146			if (realpath(optarg, mdio.md_file) == NULL) {
147				err(1, "could not find full path for %s",
148				    optarg);
149			}
150			fd = open(mdio.md_file, O_RDONLY);
151			if (fd < 0)
152				err(1, "could not open %s", optarg);
153			else if (mdio.md_mediasize == 0) {
154				struct stat sb;
155
156				if (fstat(fd, &sb) == -1)
157					err(1, "could not stat %s", optarg);
158				mdio.md_mediasize = sb.st_size;
159			}
160			close(fd);
161			break;
162		case 'o':
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;
168				else
169					errx(1, "Unknown option: %s.", optarg);
170				break;
171			}
172
173			if (cmdline != 2)
174				usage();
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;
199			else
200				errx(1, "Unknown option: %s.", optarg);
201			break;
202		case 'S':
203			if (cmdline != 2)
204				usage();
205			mdio.md_sectorsize = strtoul(optarg, &p, 0);
206			break;
207		case 's':
208			if (cmdline == 0) {
209				/* Imply ``-a'' */
210				action = ATTACH;
211				cmdline = 1;
212			}
213			if (cmdline == 1) {
214				/* Imply ``-t swap'' */
215				mdio.md_type = MD_SWAP;
216				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
217				cmdline = 2;
218			}
219			if (cmdline != 2)
220				usage();
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')
225				; /* do nothing */
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;
235			} else
236				errx(1, "Unknown suffix on -s argument");
237			break;
238		case 'u':
239			if (cmdline != 2 && cmdline != 3)
240				usage();
241			if (!strncmp(optarg, "/dev/", 5))
242				optarg += 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);
248			mdunit = optarg;
249			mdio.md_options &= ~MD_AUTOUNIT;
250			break;
251		case 'v':
252			if (cmdline != 3)
253				usage();
254			vflag = OPT_VERBOSE;
255			break;
256		case 'x':
257			if (cmdline != 2)
258				usage();
259			mdio.md_fwsectors = strtoul(optarg, &p, 0);
260			break;
261		case 'y':
262			if (cmdline != 2)
263				usage();
264			mdio.md_fwheads = strtoul(optarg, &p, 0);
265			break;
266		default:
267			usage();
268		}
269	}
270	mdio.md_version = MDIOVERSION;
271
272	if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
273		err(1, "failed to load geom_md module");
274
275	fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
276	if (fd < 0)
277		err(1, "open(/dev/%s)", MDCTL_NAME);
278	if (cmdline == 2
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)) {
289			fprintf(stderr,
290			    "WARNING: opening backing store: %s readonly\n",
291			    mdio.md_file);
292			mdio.md_options |= MD_READONLY;
293		}
294	}
295	if (action == LIST) {
296		if (mdio.md_options & MD_AUTOUNIT) {
297			/*
298			 * Listing all devices. This is why we pass NULL
299			 * together with OPT_LIST.
300			 */
301			md_list(NULL, OPT_LIST | vflag);
302		} else {
303			return (md_query(mdunit));
304		}
305	} else if (action == ATTACH) {
306		if (cmdline < 2)
307			usage();
308		i = ioctl(fd, MDIOCATTACH, &mdio);
309		if (i < 0)
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)
315			usage();
316		i = ioctl(fd, MDIOCDETACH, &mdio);
317		if (i < 0)
318			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
319	} else
320		usage();
321	close (fd);
322	return (0);
323}
324
325/*
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.
330 */
331static int
332md_list(char *units, int opt)
333{
334	struct gmesh gm;
335	struct gprovider *pp;
336	struct gconf *gc;
337	struct gident *gid;
338	struct devstat *gsp;
339	struct ggeom *gg;
340	struct gclass *gcl;
341	void *sq;
342	int retcode, found;
343	char *type, *file, *length;
344
345	type = file = length = NULL;
346
347	retcode = geom_gettree(&gm);
348	if (retcode != 0)
349		return (-1);
350	retcode = geom_stats_open();
351	if (retcode != 0)
352		return (-1);
353	sq = geom_stats_snapshot_get();
354	if (sq == NULL)
355		return (-1);
356
357	found = 0;
358	while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
359		gid = geom_lookupid(&gm, gsp->id);
360		if (gid == NULL)
361			continue;
362		if (gid->lg_what == ISPROVIDER) {
363			pp = gid->lg_ptr;
364			gg = pp->lg_geom;
365			gcl = gg->lg_class;
366			if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
367				continue;
368			if ((opt & OPT_UNIT) && (units != NULL)) {
369				retcode = md_find(units, pp->lg_name);
370				if (retcode != 1)
371					continue;
372				else
373					found = 1;
374			}
375			gc = &pp->lg_config;
376			if (nflag && strncmp(pp->lg_name, "md", 2) == 0)
377				printf("%s", pp->lg_name + 2);
378			else
379				printf("%s", pp->lg_name);
380
381			if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
382				type = geom_config_get(gc, "type");
383				if (strcmp(type, "vnode") == 0)
384					file = geom_config_get(gc, "file");
385				length = geom_config_get(gc, "length");
386				printf("\t%s\t", type);
387				if (length != NULL)
388					md_prthumanval(length);
389				if (file != NULL) {
390					printf("\t%s", file);
391					file = NULL;
392				}
393			}
394			opt |= OPT_DONE;
395			if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE))
396				printf(" ");
397			else
398				printf("\n");
399		}
400	}
401	if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE))
402		printf("\n");
403	/* XXX: Check if it's enough to clean everything. */
404	geom_stats_snapshot_free(sq);
405	if ((opt & OPT_UNIT) && found)
406		return (0);
407	else
408		return (-1);
409}
410
411/*
412 * Returns value of 'name' from gconfig structure.
413 */
414static char *
415geom_config_get(struct gconf *g, const char *name)
416{
417	struct gconfig *gce;
418
419	LIST_FOREACH(gce, g, lg_config) {
420		if (strcmp(gce->lg_name, name) == 0)
421			return (gce->lg_val);
422	}
423	return (NULL);
424}
425
426/*
427 * List is comma separated list of MD disks. name is a
428 * device name we look for.  Returns 1 if found and 0
429 * otherwise.
430 */
431static int
432md_find(char *list, const char *name)
433{
434	int ret;
435	char num[16];
436	char *ptr, *p, *u;
437
438	ret = 0;
439	ptr = strdup(list);
440	if (ptr == NULL)
441		return (-1);
442	for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
443		if (strncmp(u, "/dev/", 5) == 0)
444			u += 5;
445		/* Just in case user specified number instead of full name */
446		snprintf(num, sizeof(num), "md%s", u);
447		if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
448			ret = 1;
449			break;
450		}
451	}
452	free(ptr);
453	return (ret);
454}
455
456static void
457md_prthumanval(char *length)
458{
459	char buf[6];
460	uintmax_t bytes;
461	char *endptr;
462
463	errno = 0;
464	bytes = strtoumax(length, &endptr, 10);
465	if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX)
466		return;
467	humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
468	    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
469	(void)printf("%6s", buf);
470}
471
472int
473md_query(char *name)
474{
475	return (md_list(name, OPT_UNIT));
476}
477