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