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