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