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