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