xref: /freebsd/sbin/mdconfig/mdconfig.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
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 			fd = open(optarg, O_RDONLY);
117 			if (fd < 0)
118 				err(1, "could not open %s", optarg);
119 			close(fd);
120 			break;
121 		case 'o':
122 			if (cmdline != 2)
123 				usage();
124 			if (!strcmp(optarg, "async"))
125 				mdio.md_options |= MD_ASYNC;
126 			else if (!strcmp(optarg, "noasync"))
127 				mdio.md_options &= ~MD_ASYNC;
128 			else if (!strcmp(optarg, "cluster"))
129 				mdio.md_options |= MD_CLUSTER;
130 			else if (!strcmp(optarg, "nocluster"))
131 				mdio.md_options &= ~MD_CLUSTER;
132 			else if (!strcmp(optarg, "compress"))
133 				mdio.md_options |= MD_COMPRESS;
134 			else if (!strcmp(optarg, "nocompress"))
135 				mdio.md_options &= ~MD_COMPRESS;
136 			else if (!strcmp(optarg, "force"))
137 				mdio.md_options |= MD_FORCE;
138 			else if (!strcmp(optarg, "noforce"))
139 				mdio.md_options &= ~MD_FORCE;
140 			else if (!strcmp(optarg, "reserve"))
141 				mdio.md_options |= MD_RESERVE;
142 			else if (!strcmp(optarg, "noreserve"))
143 				mdio.md_options &= ~MD_RESERVE;
144 			else
145 				errx(1, "Unknown option.");
146 			break;
147 		case 'S':
148 			if (cmdline != 2)
149 				usage();
150 			mdio.md_secsize = strtoul(optarg, &p, 0);
151 			break;
152 		case 's':
153 			if (cmdline != 2)
154 				usage();
155 			mdio.md_size = strtoul(optarg, &p, 0);
156 			if (p == NULL || *p == '\0')
157 				;
158 			else if (*p == 'k' || *p == 'K')
159 				mdio.md_size *= (1024 / DEV_BSIZE);
160 			else if (*p == 'm' || *p == 'M')
161 				mdio.md_size *= (1024 * 1024 / DEV_BSIZE);
162 			else if (*p == 'g' || *p == 'G')
163 				mdio.md_size *= (1024 * 1024 * 1024 / DEV_BSIZE);
164 			else
165 				errx(1, "Unknown suffix on -s argument");
166 			break;
167 		case 'u':
168 			if (cmdline != 2 && cmdline != 3)
169 				usage();
170 			if (!strncmp(optarg, "/dev/", 5))
171 				optarg += 5;
172 			if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
173 				optarg += sizeof(MD_NAME) - 1;
174 			mdio.md_unit = strtoul(optarg, &p, 0);
175 			if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
176 				errx(1, "bad unit: %s", optarg);
177 			mdio.md_options &= ~MD_AUTOUNIT;
178 			break;
179 		case 'x':
180 			if (cmdline != 2)
181 				usage();
182 			mdio.md_fwsectors = strtoul(optarg, &p, 0);
183 			break;
184 		case 'y':
185 			if (cmdline != 2)
186 				usage();
187 			mdio.md_fwheads = strtoul(optarg, &p, 0);
188 			break;
189 		default:
190 			usage();
191 		}
192 	}
193 	mdio.md_version = MDIOVERSION;
194 
195 	mdmaybeload();
196 	fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
197 	if (fd < 0)
198 		err(1, "open(/dev/%s)", MDCTL_NAME);
199 	if (cmdline == 2
200 	    && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
201 		if (mdio.md_size == 0)
202 			errx(1, "must specify -s for -t malloc or -t swap");
203 	if (cmdline == 2 && mdio.md_type == MD_VNODE)
204 		if (mdio.md_file == NULL)
205 			errx(1, "must specify -f for -t vnode");
206 	if (action == LIST) {
207 		if (mdio.md_options & MD_AUTOUNIT)
208 			list(fd);
209 		else
210 			query(fd, mdio.md_unit);
211 	} else if (action == ATTACH) {
212 		if (cmdline < 2)
213 			usage();
214 		i = ioctl(fd, MDIOCATTACH, &mdio);
215 		if (i < 0)
216 			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
217 		if (mdio.md_options & MD_AUTOUNIT)
218 			printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
219 	} else if (action == DETACH) {
220 		if (mdio.md_options & MD_AUTOUNIT)
221 			usage();
222 		i = ioctl(fd, MDIOCDETACH, &mdio);
223 		if (i < 0)
224 			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
225 	} else
226 		usage();
227 	close (fd);
228 	return (0);
229 }
230 
231 struct dl {
232 	int		unit;
233 	SLIST_ENTRY(dl)	slist;
234 };
235 
236 SLIST_HEAD(, dl) dlist = SLIST_HEAD_INITIALIZER(&dlist);
237 
238 int
239 list(const int fd)
240 {
241 	int unit;
242 
243 	if (ioctl(fd, MDIOCLIST, &mdio) < 0)
244 		err(1, "ioctl(/dev/%s)", MDCTL_NAME);
245 	for (unit = 0; unit < mdio.md_pad[0] && unit < MDNPAD - 1; unit++) {
246 		printf("%s%s%d", unit > 0 ? " " : "",
247 		    nflag ? "" : MD_NAME, mdio.md_pad[unit + 1]);
248 	}
249 	if (mdio.md_pad[0] - unit > 0)
250 		printf(" ... %d more", mdio.md_pad[0] - unit);
251 	printf("\n");
252 	return (0);
253 }
254 
255 int
256 query(const int fd, const int unit)
257 {
258 
259 	mdio.md_version = MDIOVERSION;
260 	mdio.md_unit = unit;
261 
262 	if (ioctl(fd, MDIOCQUERY, &mdio) < 0)
263 		err(1, "ioctl(/dev/%s)", MDCTL_NAME);
264 
265 	switch (mdio.md_type) {
266 	case MD_MALLOC:
267 		(void)printf("%s%d\tmalloc\t%d KBytes\n", MD_NAME,
268 		    mdio.md_unit, mdio.md_size / 2);
269 		break;
270 	case MD_PRELOAD:
271 		(void)printf("%s%d\tpreload\t%d KBytes\n", MD_NAME,
272 		    mdio.md_unit, mdio.md_size / 2);
273 		break;
274 	case MD_SWAP:
275 		(void)printf("%s%d\tswap\t%d KBytes\n", MD_NAME,
276 		    mdio.md_unit, mdio.md_size / 2);
277 		break;
278 	case MD_VNODE:
279 		(void)printf("%s%d\tvnode\t%d KBytes\n", MD_NAME,
280 		    mdio.md_unit, mdio.md_size / 2);
281 		break;
282 	}
283 
284 	return (0);
285 }
286 
287 void
288 mdmaybeload(void)
289 {
290         struct module_stat mstat;
291         int fileid, modid;
292         const char *name;
293 	char *cp;
294 
295 	name = MD_NAME;
296         /* scan files in kernel */
297         mstat.version = sizeof(struct module_stat);
298         for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
299                 /* scan modules in file */
300                 for (modid = kldfirstmod(fileid); modid > 0;
301                      modid = modfnext(modid)) {
302                         if (modstat(modid, &mstat) < 0)
303                                 continue;
304                         /* strip bus name if present */
305                         if ((cp = strchr(mstat.name, '/')) != NULL) {
306                                 cp++;
307                         } else {
308                                 cp = mstat.name;
309                         }
310                         /* already loaded? */
311                         if (!strcmp(name, cp))
312                                 return;
313                 }
314         }
315         /* not present, we should try to load it */
316         kldload(name);
317 }
318 
319