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