1 /* 2 * ---------------------------------------------------------------------------- 3 * "THE BEER-WARE LICENSE" (Revision 42): 4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 5 * can do whatever you want with this stuff. If we meet some day, and you think 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 7 * ---------------------------------------------------------------------------- 8 * 9 * $FreeBSD$ 10 * 11 */ 12 #include <sys/param.h> 13 #include <sys/devicestat.h> 14 #include <sys/ioctl.h> 15 #include <sys/linker.h> 16 #include <sys/mdioctl.h> 17 #include <sys/module.h> 18 #include <sys/resource.h> 19 #include <sys/stat.h> 20 21 #include <assert.h> 22 #include <devstat.h> 23 #include <err.h> 24 #include <errno.h> 25 #include <fcntl.h> 26 #include <inttypes.h> 27 #include <libgeom.h> 28 #include <libutil.h> 29 #include <stdarg.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 35 36 static struct md_ioctl mdio; 37 static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET; 38 static int nflag; 39 40 static void usage(void); 41 static int md_find(char *, const char *); 42 static int md_query(char *name); 43 static int md_list(char *units, int opt); 44 static char *geom_config_get(struct gconf *g, char *name); 45 static void md_prthumanval(char *length); 46 47 #define OPT_VERBOSE 0x01 48 #define OPT_UNIT 0x02 49 #define OPT_DONE 0x04 50 #define OPT_LIST 0x10 51 52 #define CLASS_NAME_MD "MD" 53 54 static void 55 usage() 56 { 57 fprintf(stderr, 58 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n" 59 " [-s size] [-S sectorsize] [-u unit]\n" 60 " [-x sectors/track] [-y heads/cyl]\n" 61 " mdconfig -d -u unit\n" 62 " mdconfig -l [-n] [-u unit]\n"); 63 fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n"); 64 fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n"); 65 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n"); 66 fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n"); 67 fprintf(stderr, "\t\t %%dt (TB)\n"); 68 exit(1); 69 } 70 71 int 72 main(int argc, char **argv) 73 { 74 int ch, fd, i; 75 char *p; 76 int cmdline = 0; 77 char *mdunit; 78 79 bzero(&mdio, sizeof(mdio)); 80 mdio.md_file = malloc(PATH_MAX); 81 if (mdio.md_file == NULL) 82 err(1, "could not allocate memory"); 83 bzero(mdio.md_file, PATH_MAX); 84 for (;;) { 85 ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:"); 86 if (ch == -1) 87 break; 88 switch (ch) { 89 case 'a': 90 if (cmdline != 0) 91 usage(); 92 action = ATTACH; 93 cmdline = 1; 94 break; 95 case 'd': 96 if (cmdline != 0) 97 usage(); 98 action = DETACH; 99 mdio.md_options = MD_AUTOUNIT; 100 cmdline = 3; 101 break; 102 case 'l': 103 if (cmdline != 0) 104 usage(); 105 action = LIST; 106 mdio.md_options = MD_AUTOUNIT; 107 cmdline = 3; 108 break; 109 case 'n': 110 nflag = 1; 111 break; 112 case 't': 113 if (cmdline != 1) 114 usage(); 115 if (!strcmp(optarg, "malloc")) { 116 mdio.md_type = MD_MALLOC; 117 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS; 118 } else if (!strcmp(optarg, "preload")) { 119 mdio.md_type = MD_PRELOAD; 120 mdio.md_options = 0; 121 } else if (!strcmp(optarg, "vnode")) { 122 mdio.md_type = MD_VNODE; 123 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 124 } else if (!strcmp(optarg, "swap")) { 125 mdio.md_type = MD_SWAP; 126 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 127 } else { 128 usage(); 129 } 130 cmdline=2; 131 break; 132 case 'f': 133 if (cmdline == 0) { 134 action = ATTACH; 135 cmdline = 1; 136 } 137 if (cmdline == 1) { 138 /* Imply ``-t vnode'' */ 139 mdio.md_type = MD_VNODE; 140 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 141 cmdline = 2; 142 } 143 if (cmdline != 2) 144 usage(); 145 if (realpath(optarg, mdio.md_file) == NULL) { 146 err(1, "could not find full path for %s", 147 optarg); 148 } 149 fd = open(mdio.md_file, O_RDONLY); 150 if (fd < 0) 151 err(1, "could not open %s", optarg); 152 else if (mdio.md_mediasize == 0) { 153 struct stat sb; 154 155 if (fstat(fd, &sb) == -1) 156 err(1, "could not stat %s", optarg); 157 mdio.md_mediasize = sb.st_size; 158 } 159 close(fd); 160 break; 161 case 'o': 162 if (cmdline != 2) 163 usage(); 164 if (!strcmp(optarg, "async")) 165 mdio.md_options |= MD_ASYNC; 166 else if (!strcmp(optarg, "noasync")) 167 mdio.md_options &= ~MD_ASYNC; 168 else if (!strcmp(optarg, "cluster")) 169 mdio.md_options |= MD_CLUSTER; 170 else if (!strcmp(optarg, "nocluster")) 171 mdio.md_options &= ~MD_CLUSTER; 172 else if (!strcmp(optarg, "compress")) 173 mdio.md_options |= MD_COMPRESS; 174 else if (!strcmp(optarg, "nocompress")) 175 mdio.md_options &= ~MD_COMPRESS; 176 else if (!strcmp(optarg, "force")) 177 mdio.md_options |= MD_FORCE; 178 else if (!strcmp(optarg, "noforce")) 179 mdio.md_options &= ~MD_FORCE; 180 else if (!strcmp(optarg, "readonly")) 181 mdio.md_options |= MD_READONLY; 182 else if (!strcmp(optarg, "noreadonly")) 183 mdio.md_options &= ~MD_READONLY; 184 else if (!strcmp(optarg, "reserve")) 185 mdio.md_options |= MD_RESERVE; 186 else if (!strcmp(optarg, "noreserve")) 187 mdio.md_options &= ~MD_RESERVE; 188 else 189 errx(1, "Unknown option: %s.", optarg); 190 break; 191 case 'S': 192 if (cmdline != 2) 193 usage(); 194 mdio.md_sectorsize = strtoul(optarg, &p, 0); 195 break; 196 case 's': 197 if (cmdline == 0) { 198 /* Imply ``-a'' */ 199 action = ATTACH; 200 cmdline = 1; 201 } 202 if (cmdline == 1) { 203 /* Imply ``-t swap'' */ 204 mdio.md_type = MD_SWAP; 205 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 206 cmdline = 2; 207 } 208 if (cmdline != 2) 209 usage(); 210 mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0); 211 if (p == NULL || *p == '\0') 212 mdio.md_mediasize *= DEV_BSIZE; 213 else if (*p == 'b' || *p == 'B') 214 ; /* do nothing */ 215 else if (*p == 'k' || *p == 'K') 216 mdio.md_mediasize <<= 10; 217 else if (*p == 'm' || *p == 'M') 218 mdio.md_mediasize <<= 20; 219 else if (*p == 'g' || *p == 'G') 220 mdio.md_mediasize <<= 30; 221 else if (*p == 't' || *p == 'T') { 222 mdio.md_mediasize <<= 30; 223 mdio.md_mediasize <<= 10; 224 } else 225 errx(1, "Unknown suffix on -s argument"); 226 break; 227 case 'u': 228 if (cmdline != 2 && cmdline != 3) 229 usage(); 230 if (!strncmp(optarg, "/dev/", 5)) 231 optarg += 5; 232 if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1)) 233 optarg += sizeof(MD_NAME) - 1; 234 mdio.md_unit = strtoul(optarg, &p, 0); 235 if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0') 236 errx(1, "bad unit: %s", optarg); 237 mdunit = optarg; 238 mdio.md_options &= ~MD_AUTOUNIT; 239 break; 240 case 'x': 241 if (cmdline != 2) 242 usage(); 243 mdio.md_fwsectors = strtoul(optarg, &p, 0); 244 break; 245 case 'y': 246 if (cmdline != 2) 247 usage(); 248 mdio.md_fwheads = strtoul(optarg, &p, 0); 249 break; 250 default: 251 usage(); 252 } 253 } 254 mdio.md_version = MDIOVERSION; 255 256 if (!kld_isloaded("g_md") && kld_load("geom_md") == -1) 257 err(1, "failed to load geom_md module"); 258 259 fd = open("/dev/" MDCTL_NAME, O_RDWR, 0); 260 if (fd < 0) 261 err(1, "open(/dev/%s)", MDCTL_NAME); 262 if (cmdline == 2 263 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP)) 264 if (mdio.md_mediasize == 0) 265 errx(1, "must specify -s for -t malloc or -t swap"); 266 if (cmdline == 2 && mdio.md_type == MD_VNODE) 267 if (mdio.md_file[0] == '\0') 268 errx(1, "must specify -f for -t vnode"); 269 if (mdio.md_type == MD_VNODE && 270 (mdio.md_options & MD_READONLY) == 0) { 271 if (access(mdio.md_file, W_OK) < 0 && 272 (errno == EACCES || errno == EPERM || errno == EROFS)) { 273 fprintf(stderr, 274 "WARNING: opening backing store: %s readonly\n", 275 mdio.md_file); 276 mdio.md_options |= MD_READONLY; 277 } 278 } 279 if (action == LIST) { 280 if (mdio.md_options & MD_AUTOUNIT) { 281 /* 282 * Listing all devices. This is why we pass NULL 283 * together with OPT_LIST. 284 */ 285 md_list(NULL, OPT_LIST); 286 } else { 287 return (md_query(mdunit)); 288 } 289 } else if (action == ATTACH) { 290 if (cmdline < 2) 291 usage(); 292 i = ioctl(fd, MDIOCATTACH, &mdio); 293 if (i < 0) 294 err(1, "ioctl(/dev/%s)", MDCTL_NAME); 295 if (mdio.md_options & MD_AUTOUNIT) 296 printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit); 297 } else if (action == DETACH) { 298 if (mdio.md_options & MD_AUTOUNIT) 299 usage(); 300 i = ioctl(fd, MDIOCDETACH, &mdio); 301 if (i < 0) 302 err(1, "ioctl(/dev/%s)", MDCTL_NAME); 303 } else 304 usage(); 305 close (fd); 306 return (0); 307 } 308 309 /* 310 * Lists md(4) disks. Is used also as a query routine, since it handles XML 311 * interface. 'units' can be NULL for listing memory disks. It might be 312 * coma-separated string containing md(4) disk names. 'opt' distinguished 313 * between list and query mode. 314 */ 315 static int 316 md_list(char *units, int opt) 317 { 318 struct gmesh gm; 319 struct gprovider *pp; 320 struct gconf *gc; 321 struct gident *gid; 322 struct devstat *gsp; 323 struct ggeom *gg; 324 struct gclass *gcl; 325 void *sq; 326 int retcode, found; 327 char *type, *file, *length; 328 329 type = file = length = NULL; 330 331 retcode = geom_gettree(&gm); 332 if (retcode != 0) 333 return (-1); 334 retcode = geom_stats_open(); 335 if (retcode != 0) 336 return (-1); 337 sq = geom_stats_snapshot_get(); 338 if (sq == NULL) 339 return (-1); 340 341 found = 0; 342 while ((gsp = geom_stats_snapshot_next(sq)) != NULL) { 343 gid = geom_lookupid(&gm, gsp->id); 344 if (gid == NULL) 345 continue; 346 if (gid->lg_what == ISPROVIDER) { 347 pp = gid->lg_ptr; 348 gg = pp->lg_geom; 349 gcl = gg->lg_class; 350 if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0) 351 continue; 352 if ((opt & OPT_UNIT) && (units != NULL)) { 353 retcode = md_find(units, pp->lg_name); 354 if (retcode != 1) 355 continue; 356 else 357 found = 1; 358 } 359 gc = &pp->lg_config; 360 printf("%s", pp->lg_name); 361 if (opt & OPT_VERBOSE || opt & OPT_UNIT) { 362 type = geom_config_get(gc, "type"); 363 if (strcmp(type, "vnode") == 0) 364 file = geom_config_get(gc, "file"); 365 length = geom_config_get(gc, "length"); 366 if (length == NULL) 367 length = "<a>"; 368 printf("\t%s\t", type); 369 md_prthumanval(length); 370 if (file != NULL) { 371 printf("\t%s", file); 372 file = NULL; 373 } 374 } 375 opt |= OPT_DONE; 376 if (opt & OPT_LIST) 377 printf(" "); 378 else 379 printf("\n"); 380 } 381 } 382 if ((opt & OPT_LIST) && (opt & OPT_DONE)) 383 printf("\n"); 384 /* XXX: Check if it's enough to clean everything. */ 385 geom_stats_snapshot_free(sq); 386 if ((opt & OPT_UNIT) && found) 387 return (0); 388 else 389 return (-1); 390 } 391 392 /* 393 * Returns value of 'name' from gconfig structure. 394 */ 395 static char * 396 geom_config_get(struct gconf *g, char *name) 397 { 398 struct gconfig *gce; 399 400 LIST_FOREACH(gce, g, lg_config) { 401 if (strcmp(gce->lg_name, name) == 0) 402 return (gce->lg_val); 403 } 404 return (NULL); 405 } 406 407 /* 408 * List is comma separated list of MD disks. name is a 409 * device name we look for. Returns 1 if found and 0 410 * otherwise. 411 */ 412 static int 413 md_find(char *list, const char *name) 414 { 415 int ret; 416 char num[16]; 417 char *ptr, *p, *u; 418 419 ret = 0; 420 ptr = strdup(list); 421 if (ptr == NULL) 422 return (-1); 423 for (p = ptr; (u = strsep(&p, ",")) != NULL;) { 424 if (strncmp(u, "/dev/", 5) == 0) 425 u += 5; 426 /* Just in case user specified number instead of full name */ 427 snprintf(num, sizeof(num), "md%s", u); 428 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) { 429 ret = 1; 430 break; 431 } 432 } 433 free(ptr); 434 return (ret); 435 } 436 437 static void 438 md_prthumanval(char *length) 439 { 440 char buf[6]; 441 uint64_t bytes; 442 char *endptr; 443 444 bytes = strtoul(length, &endptr, 10); 445 if (bytes == (unsigned)ULONG_MAX || *endptr != '\0') 446 return; 447 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 448 bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 449 (void)printf("%6s", buf); 450 } 451 452 int 453 md_query(char *name) 454 { 455 return (md_list(name, OPT_UNIT)); 456 } 457