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