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