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 #include <sys/param.h> 13 #include <sys/devicestat.h> 14 #include <sys/ioctl.h> 15 #include <sys/linker.h> 16 #include <sys/mdioctl.h> 17 #include <sys/module.h> 18 #include <sys/resource.h> 19 #include <sys/stat.h> 20 21 #include <assert.h> 22 #include <devstat.h> 23 #include <err.h> 24 #include <errno.h> 25 #include <fcntl.h> 26 #include <inttypes.h> 27 #include <libgeom.h> 28 #include <libutil.h> 29 #include <stdarg.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 35 36 static struct md_ioctl mdio; 37 static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET; 38 static int nflag; 39 40 static void usage(void); 41 static int md_find(char *, const char *); 42 static int md_query(char *name); 43 static int md_list(char *units, int opt); 44 static char *geom_config_get(struct gconf *g, char *name); 45 static void md_prthumanval(char *length); 46 47 #define OPT_VERBOSE 0x01 48 #define OPT_UNIT 0x02 49 #define OPT_DONE 0x04 50 #define OPT_LIST 0x10 51 52 #define CLASS_NAME_MD "MD" 53 54 static void 55 usage() 56 { 57 fprintf(stderr, 58 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n" 59 " [-s size] [-S sectorsize] [-u unit]\n" 60 " [-x sectors/track] [-y heads/cyl]\n" 61 " mdconfig -d -u unit\n" 62 " mdconfig -l [-n] [-u unit]\n"); 63 fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n"); 64 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n"); 65 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n"); 66 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n"); 67 fprintf(stderr, "\t\t %%dt (TB)\n"); 68 exit(1); 69 } 70 71 int 72 main(int argc, char **argv) 73 { 74 int ch, fd, i; 75 char *p; 76 int cmdline = 0; 77 char *mdunit; 78 79 bzero(&mdio, sizeof(mdio)); 80 mdio.md_file = malloc(PATH_MAX); 81 if (mdio.md_file == NULL) 82 err(1, "could not allocate memory"); 83 bzero(mdio.md_file, PATH_MAX); 84 for (;;) { 85 ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:"); 86 if (ch == -1) 87 break; 88 switch (ch) { 89 case 'a': 90 if (cmdline != 0) 91 usage(); 92 action = ATTACH; 93 cmdline = 1; 94 break; 95 case 'd': 96 if (cmdline != 0) 97 usage(); 98 action = DETACH; 99 mdio.md_options = MD_AUTOUNIT; 100 cmdline = 3; 101 break; 102 case 'l': 103 if (cmdline != 0) 104 usage(); 105 action = LIST; 106 mdio.md_options = MD_AUTOUNIT; 107 cmdline = 3; 108 break; 109 case 'n': 110 nflag = 1; 111 break; 112 case 't': 113 if (cmdline != 1) 114 usage(); 115 if (!strcmp(optarg, "malloc")) { 116 mdio.md_type = MD_MALLOC; 117 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS; 118 } else if (!strcmp(optarg, "preload")) { 119 mdio.md_type = MD_PRELOAD; 120 mdio.md_options = 0; 121 } else if (!strcmp(optarg, "vnode")) { 122 mdio.md_type = MD_VNODE; 123 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 124 } else if (!strcmp(optarg, "swap")) { 125 mdio.md_type = MD_SWAP; 126 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 127 } else { 128 usage(); 129 } 130 cmdline=2; 131 break; 132 case 'f': 133 if (cmdline != 1 && cmdline != 2) 134 usage(); 135 if (cmdline == 1) { 136 /* Imply ``-t vnode'' */ 137 mdio.md_type = MD_VNODE; 138 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 139 cmdline = 2; 140 } 141 if (realpath(optarg, mdio.md_file) == NULL) { 142 err(1, "could not find full path for %s", 143 optarg); 144 } 145 fd = open(mdio.md_file, O_RDONLY); 146 if (fd < 0) 147 err(1, "could not open %s", optarg); 148 else if (mdio.md_mediasize == 0) { 149 struct stat sb; 150 151 if (fstat(fd, &sb) == -1) 152 err(1, "could not stat %s", optarg); 153 mdio.md_mediasize = sb.st_size; 154 } 155 close(fd); 156 break; 157 case 'o': 158 if (cmdline != 2) 159 usage(); 160 if (!strcmp(optarg, "async")) 161 mdio.md_options |= MD_ASYNC; 162 else if (!strcmp(optarg, "noasync")) 163 mdio.md_options &= ~MD_ASYNC; 164 else if (!strcmp(optarg, "cluster")) 165 mdio.md_options |= MD_CLUSTER; 166 else if (!strcmp(optarg, "nocluster")) 167 mdio.md_options &= ~MD_CLUSTER; 168 else if (!strcmp(optarg, "compress")) 169 mdio.md_options |= MD_COMPRESS; 170 else if (!strcmp(optarg, "nocompress")) 171 mdio.md_options &= ~MD_COMPRESS; 172 else if (!strcmp(optarg, "force")) 173 mdio.md_options |= MD_FORCE; 174 else if (!strcmp(optarg, "noforce")) 175 mdio.md_options &= ~MD_FORCE; 176 else if (!strcmp(optarg, "readonly")) 177 mdio.md_options |= MD_READONLY; 178 else if (!strcmp(optarg, "noreadonly")) 179 mdio.md_options &= ~MD_READONLY; 180 else if (!strcmp(optarg, "reserve")) 181 mdio.md_options |= MD_RESERVE; 182 else if (!strcmp(optarg, "noreserve")) 183 mdio.md_options &= ~MD_RESERVE; 184 else 185 errx(1, "Unknown option: %s.", optarg); 186 break; 187 case 'S': 188 if (cmdline != 2) 189 usage(); 190 mdio.md_sectorsize = strtoul(optarg, &p, 0); 191 break; 192 case 's': 193 if (cmdline != 2) 194 usage(); 195 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0); 196 if (p == NULL || *p == '\0') 197 mdio.md_mediasize *= DEV_BSIZE; 198 else if (*p == 'b' || *p == 'B') 199 ; /* do nothing */ 200 else if (*p == 'k' || *p == 'K') 201 mdio.md_mediasize <<= 10; 202 else if (*p == 'm' || *p == 'M') 203 mdio.md_mediasize <<= 20; 204 else if (*p == 'g' || *p == 'G') 205 mdio.md_mediasize <<= 30; 206 else if (*p == 't' || *p == 'T') { 207 mdio.md_mediasize <<= 30; 208 mdio.md_mediasize <<= 10; 209 } else 210 errx(1, "Unknown suffix on -s argument"); 211 break; 212 case 'u': 213 if (cmdline != 2 && cmdline != 3) 214 usage(); 215 if (!strncmp(optarg, "/dev/", 5)) 216 optarg += 5; 217 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1)) 218 optarg += sizeof(MD_NAME) - 1; 219 mdio.md_unit = strtoul(optarg, &p, 0); 220 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0') 221 errx(1, "bad unit: %s", optarg); 222 mdunit = optarg; 223 mdio.md_options &= ~MD_AUTOUNIT; 224 break; 225 case 'x': 226 if (cmdline != 2) 227 usage(); 228 mdio.md_fwsectors = strtoul(optarg, &p, 0); 229 break; 230 case 'y': 231 if (cmdline != 2) 232 usage(); 233 mdio.md_fwheads = strtoul(optarg, &p, 0); 234 break; 235 default: 236 usage(); 237 } 238 } 239 mdio.md_version = MDIOVERSION; 240 241 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1) 242 err(1, "failed to load geom_md module"); 243 244 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0); 245 if (fd < 0) 246 err(1, "open(/dev/%s)", MDCTL_NAME); 247 if (cmdline == 2 248 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP)) 249 if (mdio.md_mediasize == 0) 250 errx(1, "must specify -s for -t malloc or -t swap"); 251 if (cmdline == 2 && mdio.md_type == MD_VNODE) 252 if (mdio.md_file[0] == '\0') 253 errx(1, "must specify -f for -t vnode"); 254 if (mdio.md_type == MD_VNODE && 255 (mdio.md_options & MD_READONLY) == 0) { 256 if (access(mdio.md_file, W_OK) < 0 && 257 (errno == EACCES || errno == EPERM || errno == EROFS)) { 258 fprintf(stderr, 259 "WARNING: opening backing store: %s readonly\n", 260 mdio.md_file); 261 mdio.md_options |= MD_READONLY; 262 } 263 } 264 if (action == LIST) { 265 if (mdio.md_options & MD_AUTOUNIT) { 266 /* 267 * Listing all devices. This is why we pass NULL 268 * together with OPT_LIST. 269 */ 270 md_list(NULL, OPT_LIST); 271 } else { 272 md_query(mdunit); 273 } 274 } else if (action == ATTACH) { 275 if (cmdline < 2) 276 usage(); 277 i = ioctl(fd, MDIOCATTACH, &mdio); 278 if (i < 0) 279 err(1, "ioctl(/dev/%s)", MDCTL_NAME); 280 if (mdio.md_options & MD_AUTOUNIT) 281 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit); 282 } else if (action == DETACH) { 283 if (mdio.md_options & MD_AUTOUNIT) 284 usage(); 285 i = ioctl(fd, MDIOCDETACH, &mdio); 286 if (i < 0) 287 err(1, "ioctl(/dev/%s)", MDCTL_NAME); 288 } else 289 usage(); 290 close (fd); 291 return (0); 292 } 293 294 /* 295 * Lists md(4) disks. Is used also as a query routine, since it handles XML 296 * interface. 'units' can be NULL for listing memory disks. It might be 297 * coma-separated string containing md(4) disk names. 'opt' distinguished 298 * between list and query mode. 299 */ 300 static int 301 md_list(char *units, int opt) 302 { 303 struct gmesh gm; 304 struct gprovider *pp; 305 struct gconf *gc; 306 struct gident *gid; 307 struct devstat *gsp; 308 struct ggeom *gg; 309 struct gclass *gcl; 310 void *sq; 311 int retcode; 312 char *type, *file, *length; 313 314 type = file = length = NULL; 315 316 retcode = geom_gettree(&gm); 317 if (retcode != 0) 318 return (-1); 319 retcode = geom_stats_open(); 320 if (retcode != 0) 321 return (-1); 322 sq = geom_stats_snapshot_get(); 323 if (sq == NULL) 324 return (-1); 325 326 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) { 327 gid = geom_lookupid(&gm, gsp->id); 328 if (gid == NULL) 329 continue; 330 if (gid->lg_what == ISPROVIDER) { 331 pp = gid->lg_ptr; 332 gg = pp->lg_geom; 333 gcl = gg->lg_class; 334 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0) 335 continue; 336 if ((opt & OPT_UNIT) && (units != NULL)) { 337 retcode = md_find(units, pp->lg_name); 338 if (retcode != 1) 339 continue; 340 } 341 gc = &pp->lg_config; 342 printf("%s", pp->lg_name); 343 if (opt & OPT_VERBOSE || opt & OPT_UNIT) { 344 type = geom_config_get(gc, "type"); 345 if (strcmp(type, "vnode") == 0) 346 file = geom_config_get(gc, "file"); 347 length = geom_config_get(gc, "length"); 348 if (length == NULL) 349 length = "<a>"; 350 printf("\t%s\t", type); 351 md_prthumanval(length); 352 if (file != NULL) { 353 printf("\t%s", file); 354 file = NULL; 355 } 356 } 357 opt |= OPT_DONE; 358 if (opt & OPT_LIST) 359 printf(" "); 360 else 361 printf("\n"); 362 } 363 } 364 if ((opt & OPT_LIST) && (opt & OPT_DONE)) 365 printf("\n"); 366 /* XXX: Check if it's enough to clean everything. */ 367 geom_stats_snapshot_free(sq); 368 return (-1); 369 } 370 371 /* 372 * Returns value of 'name' from gconfig structure. 373 */ 374 static char * 375 geom_config_get(struct gconf *g, char *name) 376 { 377 struct gconfig *gce; 378 379 LIST_FOREACH(gce, g, lg_config) { 380 if (strcmp(gce->lg_name, name) == 0) 381 return (gce->lg_val); 382 } 383 return (NULL); 384 } 385 386 /* 387 * List is comma separated list of MD disks. name is a 388 * device name we look for. Returns 1 if found and 0 389 * otherwise. 390 */ 391 static int 392 md_find(char *list, const char *name) 393 { 394 int ret; 395 char num[16]; 396 char *ptr, *p, *u; 397 398 ret = 0; 399 ptr = strdup(list); 400 if (ptr == NULL) 401 return (-1); 402 for (p = ptr; (u = strsep(&p, ",")) != NULL;) { 403 if (strncmp(u, "/dev/", 5) == 0) 404 u += 5; 405 /* Just in case user specified number instead of full name */ 406 snprintf(num, sizeof(num), "md%s", u); 407 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) { 408 ret = 1; 409 break; 410 } 411 } 412 free(ptr); 413 return (ret); 414 } 415 416 static void 417 md_prthumanval(char *length) 418 { 419 char buf[6]; 420 uint64_t bytes; 421 char *endptr; 422 423 bytes = strtoul(length, &endptr, 10); 424 if (bytes == (unsigned)ULONG_MAX || *endptr != '\0') 425 return; 426 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 427 bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 428 (void)printf("%6s", buf); 429 } 430 431 int 432 md_query(char *name) 433 { 434 return (md_list(name, OPT_UNIT)); 435 } 436