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