xref: /freebsd/sbin/mdconfig/mdconfig.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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$
10  *
11  */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <err.h>
19 #include <sys/ioctl.h>
20 #include <sys/param.h>
21 #include <sys/module.h>
22 #include <sys/linker.h>
23 #include <sys/mdioctl.h>
24 #include <sys/sysctl.h>
25 #include <sys/queue.h>
26 
27 int	 list(const int);
28 void	 mdmaybeload(void);
29 int	 query(const int, const int);
30 void	 usage(void);
31 
32 struct md_ioctl mdio;
33 
34 enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
35 
36 int nflag;
37 
38 void
39 usage()
40 {
41 	fprintf(stderr, "usage:\n");
42 	fprintf(stderr, "\tmdconfig -a -t type [-n] [-o [no]option]... [ -f file] [-s size] [-S sectorsize] [-u unit]\n");
43 	fprintf(stderr, "\tmdconfig -d -u unit\n");
44 	fprintf(stderr, "\tmdconfig -l [-n] [-u unit]\n");
45 	fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n");
46 	fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
47 	fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%dk (kB), %%dm (MB) or %%dg (GB)\n");
48 	exit(1);
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54 	int ch, fd, i;
55 	char *p;
56 	int cmdline = 0;
57 
58 	for (;;) {
59 		ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:");
60 		if (ch == -1)
61 			break;
62 		switch (ch) {
63 		case 'a':
64 			if (cmdline != 0)
65 				usage();
66 			action = ATTACH;
67 			cmdline = 1;
68 			break;
69 		case 'd':
70 			if (cmdline != 0)
71 				usage();
72 			action = DETACH;
73 			mdio.md_options = MD_AUTOUNIT;
74 			cmdline = 3;
75 			break;
76 		case 'l':
77 			if (cmdline != 0)
78 				usage();
79 			action = LIST;
80 			mdio.md_options = MD_AUTOUNIT;
81 			cmdline = 3;
82 			break;
83 		case 'n':
84 			nflag = 1;
85 			break;
86 		case 't':
87 			if (cmdline != 1)
88 				usage();
89 			if (!strcmp(optarg, "malloc")) {
90 				mdio.md_type = MD_MALLOC;
91 				mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
92 			} else if (!strcmp(optarg, "preload")) {
93 				mdio.md_type = MD_PRELOAD;
94 				mdio.md_options = 0;
95 			} else if (!strcmp(optarg, "vnode")) {
96 				mdio.md_type = MD_VNODE;
97 				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
98 			} else if (!strcmp(optarg, "swap")) {
99 				mdio.md_type = MD_SWAP;
100 				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
101 			} else {
102 				usage();
103 			}
104 			cmdline=2;
105 			break;
106 		case 'f':
107 			if (cmdline != 1 && cmdline != 2)
108 				usage();
109 			if (cmdline == 1) {
110 				/* Imply ``-t vnode'' */
111 				mdio.md_type = MD_VNODE;
112 				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
113 				cmdline = 2;
114 			}
115 			mdio.md_file = optarg;
116 			break;
117 		case 'o':
118 			if (cmdline != 2)
119 				usage();
120 			if (!strcmp(optarg, "cluster"))
121 				mdio.md_options |= MD_CLUSTER;
122 			else if (!strcmp(optarg, "nocluster"))
123 				mdio.md_options &= ~MD_CLUSTER;
124 			else if (!strcmp(optarg, "compress"))
125 				mdio.md_options |= MD_COMPRESS;
126 			else if (!strcmp(optarg, "nocompress"))
127 				mdio.md_options &= ~MD_COMPRESS;
128 			else if (!strcmp(optarg, "force"))
129 				mdio.md_options |= MD_FORCE;
130 			else if (!strcmp(optarg, "noforce"))
131 				mdio.md_options &= ~MD_FORCE;
132 			else if (!strcmp(optarg, "reserve"))
133 				mdio.md_options |= MD_RESERVE;
134 			else if (!strcmp(optarg, "noreserve"))
135 				mdio.md_options &= ~MD_RESERVE;
136 			else
137 				errx(1, "Unknown option.");
138 			break;
139 		case 'S':
140 			if (cmdline != 2)
141 				usage();
142 			mdio.md_secsize = strtoul(optarg, &p, 0);
143 			break;
144 		case 's':
145 			if (cmdline != 2)
146 				usage();
147 			mdio.md_size = strtoul(optarg, &p, 0);
148 			if (p == NULL || *p == '\0')
149 				;
150 			else if (*p == 'k' || *p == 'K')
151 				mdio.md_size *= (1024 / DEV_BSIZE);
152 			else if (*p == 'm' || *p == 'M')
153 				mdio.md_size *= (1024 * 1024 / DEV_BSIZE);
154 			else if (*p == 'g' || *p == 'G')
155 				mdio.md_size *= (1024 * 1024 * 1024 / DEV_BSIZE);
156 			else
157 				errx(1, "Unknown suffix on -s argument");
158 			break;
159 		case 'u':
160 			if (cmdline != 2 && cmdline != 3)
161 				usage();
162 			if (!strncmp(optarg, "/dev/", 5))
163 				optarg += 5;
164 			if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
165 				optarg += sizeof(MD_NAME) - 1;
166 			mdio.md_unit = strtoul(optarg, &p, 0);
167 			if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
168 				errx(1, "bad unit: %s", optarg);
169 			mdio.md_options &= ~MD_AUTOUNIT;
170 			break;
171 		case 'x':
172 			if (cmdline != 2)
173 				usage();
174 			mdio.md_fwsectors = strtoul(optarg, &p, 0);
175 			break;
176 		case 'y':
177 			if (cmdline != 2)
178 				usage();
179 			mdio.md_fwheads = strtoul(optarg, &p, 0);
180 			break;
181 		default:
182 			usage();
183 		}
184 	}
185 	mdio.md_version = MDIOVERSION;
186 
187 	mdmaybeload();
188 	fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
189 	if (fd < 0)
190 		err(1, "open(/dev/%s)", MDCTL_NAME);
191 	if (cmdline == 2
192 	    && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
193 		if (mdio.md_size == 0)
194 			errx(1, "must specify -s for -t malloc or -t swap");
195 	if (cmdline == 2 && mdio.md_type == MD_VNODE)
196 		if (mdio.md_file == NULL)
197 			errx(1, "must specify -f for -t vnode");
198 	if (action == LIST) {
199 		if (mdio.md_options & MD_AUTOUNIT)
200 			list(fd);
201 		else
202 			query(fd, mdio.md_unit);
203 	} else if (action == ATTACH) {
204 		if (cmdline < 2)
205 			usage();
206 		i = ioctl(fd, MDIOCATTACH, &mdio);
207 		if (i < 0)
208 			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
209 		if (mdio.md_options & MD_AUTOUNIT)
210 			printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
211 	} else if (action == DETACH) {
212 		if (mdio.md_options & MD_AUTOUNIT)
213 			usage();
214 		i = ioctl(fd, MDIOCDETACH, &mdio);
215 		if (i < 0)
216 			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
217 	} else
218 		usage();
219 	close (fd);
220 	return (0);
221 }
222 
223 struct dl {
224 	int		unit;
225 	SLIST_ENTRY(dl)	slist;
226 };
227 
228 SLIST_HEAD(, dl) dlist = SLIST_HEAD_INITIALIZER(&dlist);
229 
230 int
231 list(const int fd)
232 {
233 	int unit;
234 
235 	if (ioctl(fd, MDIOCLIST, &mdio) < 0)
236 		err(1, "ioctl(/dev/%s)", MDCTL_NAME);
237 	for (unit = 0; unit < mdio.md_pad[0] && unit < MDNPAD - 1; unit++) {
238 		printf("%s%s%d", unit > 0 ? " " : "",
239 		    nflag ? "" : MD_NAME, mdio.md_pad[unit + 1]);
240 	}
241 	if (mdio.md_pad[0] - unit > 0)
242 		printf(" ... %d more", mdio.md_pad[0] - unit);
243 	printf("\n");
244 	return (0);
245 }
246 
247 int
248 query(const int fd, const int unit)
249 {
250 
251 	mdio.md_version = MDIOVERSION;
252 	mdio.md_unit = unit;
253 
254 	if (ioctl(fd, MDIOCQUERY, &mdio) < 0)
255 		err(1, "ioctl(/dev/%s)", MDCTL_NAME);
256 
257 	switch (mdio.md_type) {
258 	case MD_MALLOC:
259 		(void)printf("%s%d\tmalloc\t%d KBytes\n", MD_NAME,
260 		    mdio.md_unit, mdio.md_size / 2);
261 		break;
262 	case MD_PRELOAD:
263 		(void)printf("%s%d\tpreload\t%d KBytes\n", MD_NAME,
264 		    mdio.md_unit, mdio.md_size / 2);
265 		break;
266 	case MD_SWAP:
267 		(void)printf("%s%d\tswap\t%d KBytes\n", MD_NAME,
268 		    mdio.md_unit, mdio.md_size / 2);
269 		break;
270 	case MD_VNODE:
271 		(void)printf("%s%d\tvnode\t%d KBytes\n", MD_NAME,
272 		    mdio.md_unit, mdio.md_size / 2);
273 		break;
274 	}
275 
276 	return (0);
277 }
278 
279 void
280 mdmaybeload(void)
281 {
282         struct module_stat mstat;
283         int fileid, modid;
284         const char *name;
285 	char *cp;
286 
287 	name = MD_NAME;
288         /* scan files in kernel */
289         mstat.version = sizeof(struct module_stat);
290         for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
291                 /* scan modules in file */
292                 for (modid = kldfirstmod(fileid); modid > 0;
293                      modid = modfnext(modid)) {
294                         if (modstat(modid, &mstat) < 0)
295                                 continue;
296                         /* strip bus name if present */
297                         if ((cp = strchr(mstat.name, '/')) != NULL) {
298                                 cp++;
299                         } else {
300                                 cp = mstat.name;
301                         }
302                         /* already loaded? */
303                         if (!strcmp(name, cp))
304                                 return;
305                 }
306         }
307         /* not present, we should try to load it */
308         kldload(name);
309 }
310 
311