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 void 37 usage() 38 { 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"); 46 exit(1); 47 } 48 49 int 50 main(int argc, char **argv) 51 { 52 int ch, fd, i; 53 char *p; 54 int cmdline = 0; 55 56 for (;;) { 57 ch = getopt(argc, argv, "ab:df:lo:s:t:u:"); 58 if (ch == -1) 59 break; 60 switch (ch) { 61 case 'a': 62 if (cmdline != 0) 63 usage(); 64 action = ATTACH; 65 cmdline = 1; 66 break; 67 case 'd': 68 if (cmdline != 0) 69 usage(); 70 action = DETACH; 71 mdio.md_options = MD_AUTOUNIT; 72 cmdline = 3; 73 break; 74 case 'l': 75 if (cmdline != 0) 76 usage(); 77 action = LIST; 78 mdio.md_options = MD_AUTOUNIT; 79 cmdline = 3; 80 break; 81 case 't': 82 if (cmdline != 1) 83 usage(); 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; 89 mdio.md_options = 0; 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; 96 } else { 97 usage(); 98 } 99 cmdline=2; 100 break; 101 case 'f': 102 if (cmdline != 1 && cmdline != 2) 103 usage(); 104 if (cmdline == 1) { 105 /* Imply ``-t vnode'' */ 106 mdio.md_type = MD_VNODE; 107 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 108 } 109 mdio.md_file = optarg; 110 break; 111 case 'o': 112 if (cmdline != 2) 113 usage(); 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; 130 else 131 errx(1, "Unknown option."); 132 break; 133 case 's': 134 if (cmdline != 2) 135 usage(); 136 mdio.md_size = strtoul(optarg, &p, 0); 137 if (p == NULL || *p == '\0') 138 ; 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); 145 else 146 errx(1, "Unknown suffix on -s argument"); 147 break; 148 case 'u': 149 if (cmdline != 2 && cmdline != 3) 150 usage(); 151 if (!strncmp(optarg, "/dev/", 5)) 152 optarg += 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; 159 break; 160 default: 161 usage(); 162 } 163 } 164 mdio.md_version = MDIOVERSION; 165 166 mdmaybeload(); 167 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0); 168 if (fd < 0) 169 err(1, "open(/dev/%s)", MDCTL_NAME); 170 if (cmdline == 2 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) 176 list(fd); 177 else 178 query(fd, mdio.md_unit); 179 } else if (action == ATTACH) { 180 i = ioctl(fd, MDIOCATTACH, &mdio); 181 if (i < 0) 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) 187 usage(); 188 i = ioctl(fd, MDIOCDETACH, &mdio); 189 if (i < 0) 190 err(1, "ioctl(/dev/%s)", MDCTL_NAME); 191 } else 192 usage(); 193 close (fd); 194 return (0); 195 } 196 197 struct dl { 198 int unit; 199 SLIST_ENTRY(dl) slist; 200 }; 201 202 SLIST_HEAD(, dl) dlist = SLIST_HEAD_INITIALIZER(&dlist); 203 204 int 205 list(const int fd) 206 { 207 int unit; 208 209 if (ioctl(fd, MDIOCLIST, &mdio) < 0) 210 err(1, "ioctl(/dev/%s)", MDCTL_NAME); 211 for (unit = 0; unit < mdio.md_pad[0] && unit < MDNPAD - 1; unit++) { 212 printf("%smd%d", unit > 0 ? " " : "", mdio.md_pad[unit + 1]); 213 } 214 if (mdio.md_pad[0] - unit > 0) 215 printf(" ... %d more", mdio.md_pad[0] - unit); 216 printf("\n"); 217 return (0); 218 } 219 220 int 221 query(const int fd, const int unit) 222 { 223 224 mdio.md_version = MDIOVERSION; 225 mdio.md_unit = unit; 226 227 if (ioctl(fd, MDIOCQUERY, &mdio) < 0) 228 err(1, "ioctl(/dev/%s)", MDCTL_NAME); 229 230 switch (mdio.md_type) { 231 case MD_MALLOC: 232 (void)printf("%s%d\tmalloc\t%d KBytes\n", MD_NAME, 233 mdio.md_unit, mdio.md_size / 2); 234 break; 235 case MD_PRELOAD: 236 (void)printf("%s%d\tpreload\t%d KBytes\n", MD_NAME, 237 mdio.md_unit, mdio.md_size / 2); 238 break; 239 case MD_SWAP: 240 (void)printf("%s%d\tswap\t%d KBytes\n", MD_NAME, 241 mdio.md_unit, mdio.md_size / 2); 242 break; 243 case MD_VNODE: 244 (void)printf("%s%d\tvnode\t%d KBytes\n", MD_NAME, 245 mdio.md_unit, mdio.md_size / 2); 246 break; 247 } 248 249 return (0); 250 } 251 252 void 253 mdmaybeload(void) 254 { 255 struct module_stat mstat; 256 int fileid, modid; 257 const char *name; 258 char *cp; 259 260 name = MD_NAME; 261 /* scan files in kernel */ 262 mstat.version = sizeof(struct module_stat); 263 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 264 /* scan modules in file */ 265 for (modid = kldfirstmod(fileid); modid > 0; 266 modid = modfnext(modid)) { 267 if (modstat(modid, &mstat) < 0) 268 continue; 269 /* strip bus name if present */ 270 if ((cp = strchr(mstat.name, '/')) != NULL) { 271 cp++; 272 } else { 273 cp = mstat.name; 274 } 275 /* already loaded? */ 276 if (!strcmp(name, cp)) 277 return; 278 } 279 } 280 /* not present, we should try to load it */ 281 kldload(name); 282 } 283 284