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 * ----------------------------------------------------------------------------
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>
28 void mdmaybeload(void);
29 int query(const int, const int);
34 enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
39 fprintf(stderr, "usage:\n");
40 fprintf(stderr, "\tmdconfig -a -t type [-o [no]option]... [ -f file] [-s size] [-u unit]\n");
41 fprintf(stderr, "\tmdconfig -d -u unit\n");
42 fprintf(stderr, "\tmdconfig -l [-u unit]\n");
43 fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n");
44 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
45 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%dk (kB), %%dm (MB) or %%dg (GB)\n");
50 main(int argc, char **argv)
57 ch = getopt(argc, argv, "ab:df:lo:s:t:u:");
71 mdio.md_options = MD_AUTOUNIT;
78 mdio.md_options = MD_AUTOUNIT;
84 if (!strcmp(optarg, "malloc")) {
85 mdio.md_type = MD_MALLOC;
86 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
87 } else if (!strcmp(optarg, "preload")) {
88 mdio.md_type = MD_PRELOAD;
90 } else if (!strcmp(optarg, "vnode")) {
91 mdio.md_type = MD_VNODE;
92 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
93 } else if (!strcmp(optarg, "swap")) {
94 mdio.md_type = MD_SWAP;
95 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
102 if (cmdline != 1 && cmdline != 2)
105 /* Imply ``-t vnode'' */
106 mdio.md_type = MD_VNODE;
107 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
109 mdio.md_file = optarg;
114 if (!strcmp(optarg, "cluster"))
115 mdio.md_options |= MD_CLUSTER;
116 else if (!strcmp(optarg, "nocluster"))
117 mdio.md_options &= ~MD_CLUSTER;
118 else if (!strcmp(optarg, "compress"))
119 mdio.md_options |= MD_COMPRESS;
120 else if (!strcmp(optarg, "nocompress"))
121 mdio.md_options &= ~MD_COMPRESS;
122 else if (!strcmp(optarg, "force"))
123 mdio.md_options |= MD_FORCE;
124 else if (!strcmp(optarg, "noforce"))
125 mdio.md_options &= ~MD_FORCE;
126 else if (!strcmp(optarg, "reserve"))
127 mdio.md_options |= MD_RESERVE;
128 else if (!strcmp(optarg, "noreserve"))
129 mdio.md_options &= ~MD_RESERVE;
131 errx(1, "Unknown option.");
136 mdio.md_size = strtoul(optarg, &p, 0);
137 if (p == NULL || *p == '\0')
139 else if (*p == 'k' || *p == 'K')
140 mdio.md_size *= (1024 / DEV_BSIZE);
141 else if (*p == 'm' || *p == 'M')
142 mdio.md_size *= (1024 * 1024 / DEV_BSIZE);
143 else if (*p == 'g' || *p == 'G')
144 mdio.md_size *= (1024 * 1024 * 1024 / DEV_BSIZE);
146 errx(1, "Unknown suffix on -s argument");
149 if (cmdline != 2 && cmdline != 3)
151 if (!strncmp(optarg, "/dev/", 5))
153 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
154 optarg += sizeof(MD_NAME) - 1;
155 mdio.md_unit = strtoul(optarg, &p, 0);
156 if ((unsigned)mdio.md_unit == ULONG_MAX || *p != '\0')
157 errx(1, "bad unit: %s", optarg);
158 mdio.md_options &= ~MD_AUTOUNIT;
164 mdio.md_version = MDIOVERSION;
167 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
169 err(1, "open(/dev/%s)", MDCTL_NAME);
171 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
172 if (mdio.md_size == 0)
173 errx(1, "must specify -s for -t malloc or -t swap");
174 if (action == LIST) {
175 if (mdio.md_options & MD_AUTOUNIT)
178 query(fd, mdio.md_unit);
179 } else if (action == ATTACH) {
180 i = ioctl(fd, MDIOCATTACH, &mdio);
182 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
183 if (mdio.md_options & MD_AUTOUNIT)
184 printf("%s%d\n", MD_NAME, mdio.md_unit);
185 } else if (action == DETACH) {
186 if (mdio.md_options & MD_AUTOUNIT)
188 i = ioctl(fd, MDIOCDETACH, &mdio);
190 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
199 SLIST_ENTRY(dl) slist;
202 SLIST_HEAD(, dl) dlist = SLIST_HEAD_INITIALIZER(&dlist);
207 char *disklist, *p, *p2, *p3;
210 struct dl *dp, *di, *dn;
212 if (sysctlbyname("kern.disks", NULL, &dll, NULL, 0) == -1)
213 err(1, "sysctlbyname: kern.disks");
214 if ( (disklist = malloc(dll)) == NULL)
216 if (sysctlbyname("kern.disks", disklist, &dll, NULL, NULL) == -1)
217 err(1, "sysctlbyname: kern.disks");
220 (p2 = strsep(&p, " ")) != NULL;) {
221 if (strncmp(p2, MD_NAME, sizeof(MD_NAME) - 1) != 0)
223 p2 += sizeof(MD_NAME) - 1;
224 unit = strtoul(p2, &p3, 10);
227 dp = calloc(sizeof *dp, 1);
229 dn = SLIST_FIRST(&dlist);
230 if (dn == NULL || dn->unit > unit) {
231 SLIST_INSERT_HEAD(&dlist, dp, slist);
233 SLIST_FOREACH(di, &dlist, slist) {
234 dn = SLIST_NEXT(di, slist);
235 if (dn == NULL || dn->unit > unit) {
236 SLIST_INSERT_AFTER(di, dp, slist);
242 SLIST_FOREACH(di, &dlist, slist)
244 while (!SLIST_EMPTY(&dlist)) {
245 di = SLIST_FIRST(&dlist);
246 SLIST_REMOVE_HEAD(&dlist, slist);
254 query(const int fd, const int unit)
257 mdio.md_version = MDIOVERSION;
260 if (ioctl(fd, MDIOCQUERY, &mdio) < 0)
261 err(1, "ioctl(/dev/%s)", MDCTL_NAME);
263 switch (mdio.md_type) {
265 (void)printf("%s%d\tmalloc\t%d KBytes\n", MD_NAME,
266 mdio.md_unit, mdio.md_size / 2);
269 (void)printf("%s%d\tpreload\t%d KBytes\n", MD_NAME,
270 mdio.md_unit, mdio.md_size / 2);
273 (void)printf("%s%d\tswap\t%d KBytes\n", MD_NAME,
274 mdio.md_unit, mdio.md_size / 2);
277 (void)printf("%s%d\tvnode\t%d KBytes\n", MD_NAME,
278 mdio.md_unit, mdio.md_size / 2);
288 struct module_stat mstat;
294 /* scan files in kernel */
295 mstat.version = sizeof(struct module_stat);
296 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
297 /* scan modules in file */
298 for (modid = kldfirstmod(fileid); modid > 0;
299 modid = modfnext(modid)) {
300 if (modstat(modid, &mstat) < 0)
302 /* strip bus name if present */
303 if ((cp = strchr(mstat.name, '/')) != NULL) {
308 /* already loaded? */
309 if (!strcmp(name, cp))
313 /* not present, we should try to load it */