1 /*- 2 * Copyright (c) 2000-2004 Poul-Henning Kamp <phk@FreeBSD.org> 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/devicestat.h> 35 #include <sys/ioctl.h> 36 #include <sys/linker.h> 37 #include <sys/mdioctl.h> 38 #include <sys/module.h> 39 #include <sys/resource.h> 40 #include <sys/stat.h> 41 42 #include <assert.h> 43 #include <devstat.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <inttypes.h> 48 #include <libgeom.h> 49 #include <libutil.h> 50 #include <paths.h> 51 #include <stdarg.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 static struct md_ioctl mdio; 58 static enum {UNSET, ATTACH, DETACH, RESIZE, LIST} action = UNSET; 59 static int nflag; 60 61 static void usage(void); 62 static void md_set_file(const char *); 63 static int md_find(const char *, const char *); 64 static int md_query(const char *, const int, const char *); 65 static int md_list(const char *, int, const char *); 66 static char *geom_config_get(struct gconf *g, const char *name); 67 static void md_prthumanval(char *length); 68 69 #define OPT_VERBOSE 0x01 70 #define OPT_UNIT 0x02 71 #define OPT_DONE 0x04 72 #define OPT_LIST 0x10 73 74 #define CLASS_NAME_MD "MD" 75 76 static void 77 usage(void) 78 { 79 80 fprintf(stderr, 81 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n" 82 " [-s size] [-S sectorsize] [-u unit]\n" 83 " [-x sectors/track] [-y heads/cylinder]\n" 84 " mdconfig -d -u unit [-o [no]force]\n" 85 " mdconfig -r -u unit -s size [-o [no]force]\n" 86 " mdconfig -l [-v] [-n] [-f file] [-u unit]\n" 87 " mdconfig file\n"); 88 fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n"); 89 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n"); 90 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n"); 91 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB), \n"); 92 fprintf(stderr, "\t\t %%dt (TB), or %%dp (PB)\n"); 93 exit(1); 94 } 95 96 int 97 main(int argc, char **argv) 98 { 99 int ch, fd, i, vflag; 100 char *p; 101 char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL; 102 103 bzero(&mdio, sizeof(mdio)); 104 mdio.md_file = malloc(PATH_MAX); 105 if (mdio.md_file == NULL) 106 err(1, "could not allocate memory"); 107 vflag = 0; 108 bzero(mdio.md_file, PATH_MAX); 109 110 if (argc == 1) 111 usage(); 112 113 while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:")) != -1) { 114 switch (ch) { 115 case 'a': 116 if (action != UNSET && action != ATTACH) 117 errx(1, "-a is mutually exclusive " 118 "with -d, -r, and -l"); 119 action = ATTACH; 120 break; 121 case 'd': 122 if (action != UNSET && action != DETACH) 123 errx(1, "-d is mutually exclusive " 124 "with -a, -r, and -l"); 125 action = DETACH; 126 mdio.md_options |= MD_AUTOUNIT; 127 break; 128 case 'r': 129 if (action != UNSET && action != RESIZE) 130 errx(1, "-r is mutually exclusive " 131 "with -a, -d, and -l"); 132 action = RESIZE; 133 mdio.md_options |= MD_AUTOUNIT; 134 break; 135 case 'l': 136 if (action != UNSET && action != LIST) 137 errx(1, "-l is mutually exclusive " 138 "with -a, -r, and -d"); 139 action = LIST; 140 mdio.md_options |= MD_AUTOUNIT; 141 break; 142 case 'n': 143 nflag = 1; 144 break; 145 case 't': 146 if (tflag != NULL) 147 errx(1, "-t can be passed only once"); 148 tflag = optarg; 149 if (!strcmp(optarg, "malloc")) { 150 mdio.md_type = MD_MALLOC; 151 mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS; 152 } else if (!strcmp(optarg, "vnode")) { 153 mdio.md_type = MD_VNODE; 154 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 155 } else if (!strcmp(optarg, "swap")) { 156 mdio.md_type = MD_SWAP; 157 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 158 } else if (!strcmp(optarg, "null")) { 159 mdio.md_type = MD_NULL; 160 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 161 } else 162 errx(1, "unknown type: %s", optarg); 163 break; 164 case 'f': 165 if (fflag != NULL) 166 errx(1, "-f can be passed only once"); 167 fflag = realpath(optarg, NULL); 168 if (fflag == NULL) 169 err(1, "realpath"); 170 break; 171 case 'o': 172 if (!strcmp(optarg, "async")) 173 mdio.md_options |= MD_ASYNC; 174 else if (!strcmp(optarg, "noasync")) 175 mdio.md_options &= ~MD_ASYNC; 176 else if (!strcmp(optarg, "cluster")) 177 mdio.md_options |= MD_CLUSTER; 178 else if (!strcmp(optarg, "nocluster")) 179 mdio.md_options &= ~MD_CLUSTER; 180 else if (!strcmp(optarg, "compress")) 181 mdio.md_options |= MD_COMPRESS; 182 else if (!strcmp(optarg, "nocompress")) 183 mdio.md_options &= ~MD_COMPRESS; 184 else if (!strcmp(optarg, "force")) 185 mdio.md_options |= MD_FORCE; 186 else if (!strcmp(optarg, "noforce")) 187 mdio.md_options &= ~MD_FORCE; 188 else if (!strcmp(optarg, "readonly")) 189 mdio.md_options |= MD_READONLY; 190 else if (!strcmp(optarg, "noreadonly")) 191 mdio.md_options &= ~MD_READONLY; 192 else if (!strcmp(optarg, "reserve")) 193 mdio.md_options |= MD_RESERVE; 194 else if (!strcmp(optarg, "noreserve")) 195 mdio.md_options &= ~MD_RESERVE; 196 else if (!strcmp(optarg, "verify")) 197 mdio.md_options |= MD_VERIFY; 198 else if (!strcmp(optarg, "noverify")) 199 mdio.md_options &= ~MD_VERIFY; 200 else 201 errx(1, "unknown option: %s", optarg); 202 break; 203 case 'S': 204 mdio.md_sectorsize = strtoul(optarg, &p, 0); 205 break; 206 case 's': 207 if (sflag != NULL) 208 errx(1, "-s can be passed only once"); 209 sflag = optarg; 210 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0); 211 if (p == NULL || *p == '\0') 212 mdio.md_mediasize *= DEV_BSIZE; 213 else if (*p == 'b' || *p == 'B') 214 ; /* do nothing */ 215 else if (*p == 'k' || *p == 'K') 216 mdio.md_mediasize <<= 10; 217 else if (*p == 'm' || *p == 'M') 218 mdio.md_mediasize <<= 20; 219 else if (*p == 'g' || *p == 'G') 220 mdio.md_mediasize <<= 30; 221 else if (*p == 't' || *p == 'T') { 222 mdio.md_mediasize <<= 30; 223 mdio.md_mediasize <<= 10; 224 } else if (*p == 'p' || *p == 'P') { 225 mdio.md_mediasize <<= 30; 226 mdio.md_mediasize <<= 20; 227 } else 228 errx(1, "unknown suffix on -s argument"); 229 break; 230 case 'u': 231 if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 232 optarg += sizeof(_PATH_DEV) - 1; 233 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1)) 234 optarg += sizeof(MD_NAME) - 1; 235 uflag = optarg; 236 break; 237 case 'v': 238 vflag = OPT_VERBOSE; 239 break; 240 case 'x': 241 mdio.md_fwsectors = strtoul(optarg, &p, 0); 242 break; 243 case 'y': 244 mdio.md_fwheads = strtoul(optarg, &p, 0); 245 break; 246 default: 247 usage(); 248 } 249 } 250 251 argc -= optind; 252 argv += optind; 253 254 if (action == UNSET) 255 action = ATTACH; 256 257 if (action == ATTACH) { 258 if (tflag == NULL) { 259 /* 260 * Try to infer the type based on other arguments. 261 */ 262 if (fflag != NULL || argc > 0) { 263 /* Imply ``-t vnode'' */ 264 mdio.md_type = MD_VNODE; 265 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | 266 MD_COMPRESS; 267 } else if (sflag != NULL) { 268 /* Imply ``-t swap'' */ 269 mdio.md_type = MD_SWAP; 270 mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | 271 MD_COMPRESS; 272 } else 273 errx(1, "unable to determine type"); 274 } 275 276 if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE) 277 errx(1, "only -t vnode can be used with file name"); 278 279 if (mdio.md_type == MD_VNODE) { 280 if (fflag != NULL) { 281 if (argc != 0) 282 usage(); 283 md_set_file(fflag); 284 } else { 285 if (argc != 1) 286 usage(); 287 md_set_file(*argv); 288 } 289 290 if ((mdio.md_options & MD_READONLY) == 0 && 291 access(mdio.md_file, W_OK) < 0 && 292 (errno == EACCES || errno == EPERM || 293 errno == EROFS)) { 294 warnx("WARNING: opening backing store: %s " 295 "readonly", mdio.md_file); 296 mdio.md_options |= MD_READONLY; 297 } 298 } 299 300 if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP || 301 mdio.md_type == MD_NULL) && sflag == NULL) 302 errx(1, "must specify -s for -t malloc, -t swap, " 303 "or -t null"); 304 if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0') 305 errx(1, "must specify -f for -t vnode"); 306 } else { 307 if (mdio.md_sectorsize != 0) 308 errx(1, "-S can only be used with -a"); 309 if (action != RESIZE && sflag != NULL) 310 errx(1, "-s can only be used with -a and -r"); 311 if (mdio.md_fwsectors != 0) 312 errx(1, "-x can only be used with -a"); 313 if (mdio.md_fwheads != 0) 314 errx(1, "-y can only be used with -a"); 315 if (fflag != NULL && action != LIST) 316 errx(1, "-f can only be used with -a and -l"); 317 if (tflag != NULL) 318 errx(1, "-t can only be used with -a"); 319 if (argc > 0) 320 errx(1, "file can only be used with -a"); 321 if ((action != DETACH && action != RESIZE) && 322 (mdio.md_options & ~MD_AUTOUNIT) != 0) 323 errx(1, "-o can only be used with -a, -d, and -r"); 324 if (action == DETACH && 325 (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0) 326 errx(1, "only -o [no]force can be used with -d"); 327 if (action == RESIZE && 328 (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0) 329 errx(1, "only -o [no]force and -o [no]reserve can be used with -r"); 330 } 331 332 if (action == RESIZE && sflag == NULL) 333 errx(1, "must specify -s for -r"); 334 335 if (action != LIST && vflag == OPT_VERBOSE) 336 errx(1, "-v can only be used with -l"); 337 338 if (uflag != NULL) { 339 mdio.md_unit = strtoul(uflag, &p, 0); 340 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0') 341 errx(1, "bad unit: %s", uflag); 342 mdio.md_options &= ~MD_AUTOUNIT; 343 } 344 345 mdio.md_version = MDIOVERSION; 346 347 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1) 348 err(1, "failed to load geom_md module"); 349 350 fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0); 351 if (fd < 0) 352 err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME); 353 354 if (action == ATTACH) { 355 i = ioctl(fd, MDIOCATTACH, &mdio); 356 if (i < 0) 357 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); 358 if (mdio.md_options & MD_AUTOUNIT) 359 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit); 360 } else if (action == DETACH) { 361 if (mdio.md_options & MD_AUTOUNIT) 362 errx(1, "-d requires -u"); 363 i = ioctl(fd, MDIOCDETACH, &mdio); 364 if (i < 0) 365 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); 366 } else if (action == RESIZE) { 367 if (mdio.md_options & MD_AUTOUNIT) 368 errx(1, "-r requires -u"); 369 i = ioctl(fd, MDIOCRESIZE, &mdio); 370 if (i < 0) 371 err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); 372 } else if (action == LIST) { 373 if (mdio.md_options & MD_AUTOUNIT) { 374 /* 375 * Listing all devices. This is why we pass NULL 376 * together with OPT_LIST. 377 */ 378 return (md_list(NULL, OPT_LIST | vflag, fflag)); 379 } else 380 return (md_query(uflag, vflag, fflag)); 381 } else 382 usage(); 383 close(fd); 384 return (0); 385 } 386 387 static void 388 md_set_file(const char *fn) 389 { 390 struct stat sb; 391 int fd; 392 393 if (realpath(fn, mdio.md_file) == NULL) 394 err(1, "could not find full path for %s", fn); 395 fd = open(mdio.md_file, O_RDONLY); 396 if (fd < 0) 397 err(1, "could not open %s", fn); 398 if (fstat(fd, &sb) == -1) 399 err(1, "could not stat %s", fn); 400 if (!S_ISREG(sb.st_mode)) 401 errx(1, "%s is not a regular file", fn); 402 if (mdio.md_mediasize == 0) 403 mdio.md_mediasize = sb.st_size; 404 close(fd); 405 } 406 407 /* 408 * Lists md(4) disks. Is used also as a query routine, since it handles XML 409 * interface. 'units' can be NULL for listing memory disks. It might be 410 * coma-separated string containing md(4) disk names. 'opt' distinguished 411 * between list and query mode. 412 */ 413 static int 414 md_list(const char *units, int opt, const char *fflag) 415 { 416 struct gmesh gm; 417 struct gprovider *pp; 418 struct gconf *gc; 419 struct gident *gid; 420 struct devstat *gsp; 421 struct ggeom *gg; 422 struct gclass *gcl; 423 void *sq; 424 int retcode, ffound, ufound; 425 char *type, *file, *length; 426 427 type = file = length = NULL; 428 429 retcode = geom_gettree(&gm); 430 if (retcode != 0) 431 return (-1); 432 retcode = geom_stats_open(); 433 if (retcode != 0) 434 return (-1); 435 sq = geom_stats_snapshot_get(); 436 if (sq == NULL) 437 return (-1); 438 439 ffound = ufound = 0; 440 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) { 441 gid = geom_lookupid(&gm, gsp->id); 442 if (gid == NULL) 443 continue; 444 if (gid->lg_what == ISPROVIDER) { 445 pp = gid->lg_ptr; 446 gg = pp->lg_geom; 447 gcl = gg->lg_class; 448 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0) 449 continue; 450 if ((opt & OPT_UNIT) && (units != NULL)) { 451 retcode = md_find(units, pp->lg_name); 452 if (retcode != 1) 453 continue; 454 else 455 ufound = 1; 456 } 457 gc = &pp->lg_config; 458 type = geom_config_get(gc, "type"); 459 if (type != NULL && (strcmp(type, "vnode") == 0 || 460 strcmp(type, "preload") == 0)) { 461 file = geom_config_get(gc, "file"); 462 if (fflag != NULL && 463 strcmp(fflag, file) != 0) 464 continue; 465 else 466 ffound = 1; 467 } else if (fflag != NULL) 468 continue; 469 if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0) 470 printf("%s", pp->lg_name + 2); 471 else 472 printf("%s", pp->lg_name); 473 474 if (opt & OPT_VERBOSE || 475 ((opt & OPT_UNIT) && fflag == NULL)) { 476 length = geom_config_get(gc, "length"); 477 printf("\t%s\t", type); 478 if (length != NULL) 479 md_prthumanval(length); 480 if (file != NULL) { 481 printf("\t%s", file); 482 file = NULL; 483 } 484 } 485 opt |= OPT_DONE; 486 if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE)) 487 printf(" "); 488 else 489 printf("\n"); 490 } 491 } 492 if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE)) 493 printf("\n"); 494 /* XXX: Check if it's enough to clean everything. */ 495 geom_stats_snapshot_free(sq); 496 if (opt & OPT_UNIT) { 497 if (((fflag == NULL) && ufound) || 498 ((fflag == NULL) && (units != NULL) && ufound) || 499 ((fflag != NULL) && ffound) || 500 ((fflag != NULL) && (units != NULL) && ufound && ffound)) 501 return (0); 502 } else if (opt & OPT_LIST) { 503 if ((fflag == NULL) || 504 ((fflag != NULL) && ffound)) 505 return (0); 506 } 507 return (-1); 508 } 509 510 /* 511 * Returns value of 'name' from gconfig structure. 512 */ 513 static char * 514 geom_config_get(struct gconf *g, const char *name) 515 { 516 struct gconfig *gce; 517 518 LIST_FOREACH(gce, g, lg_config) { 519 if (strcmp(gce->lg_name, name) == 0) 520 return (gce->lg_val); 521 } 522 return (NULL); 523 } 524 525 /* 526 * List is comma separated list of MD disks. name is a 527 * device name we look for. Returns 1 if found and 0 528 * otherwise. 529 */ 530 static int 531 md_find(const char *list, const char *name) 532 { 533 int ret; 534 char num[PATH_MAX]; 535 char *ptr, *p, *u; 536 537 ret = 0; 538 ptr = strdup(list); 539 if (ptr == NULL) 540 return (-1); 541 for (p = ptr; (u = strsep(&p, ",")) != NULL;) { 542 if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 543 u += sizeof(_PATH_DEV) - 1; 544 /* Just in case user specified number instead of full name */ 545 snprintf(num, sizeof(num), "%s%s", MD_NAME, u); 546 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) { 547 ret = 1; 548 break; 549 } 550 } 551 free(ptr); 552 return (ret); 553 } 554 555 static void 556 md_prthumanval(char *length) 557 { 558 char buf[6]; 559 uintmax_t bytes; 560 char *endptr; 561 562 errno = 0; 563 bytes = strtoumax(length, &endptr, 10); 564 if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX) 565 return; 566 humanize_number(buf, sizeof(buf), (int64_t)bytes, "", 567 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 568 (void)printf("%6s", buf); 569 } 570 571 static int 572 md_query(const char *name, const int opt, const char *fflag) 573 { 574 575 return (md_list(name, opt | OPT_UNIT, fflag)); 576 } 577