170d586c0SPoul-Henning Kamp /* 270d586c0SPoul-Henning Kamp * ---------------------------------------------------------------------------- 370d586c0SPoul-Henning Kamp * "THE BEER-WARE LICENSE" (Revision 42): 470d586c0SPoul-Henning Kamp * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 570d586c0SPoul-Henning Kamp * can do whatever you want with this stuff. If we meet some day, and you think 670d586c0SPoul-Henning Kamp * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 770d586c0SPoul-Henning Kamp * ---------------------------------------------------------------------------- 870d586c0SPoul-Henning Kamp * 970d586c0SPoul-Henning Kamp * $FreeBSD$ 1070d586c0SPoul-Henning Kamp * 1170d586c0SPoul-Henning Kamp */ 1270d586c0SPoul-Henning Kamp #include <sys/param.h> 13c27a8954SWojciech A. Koszek #include <sys/devicestat.h> 144bfd989fSWojciech A. Koszek #include <sys/ioctl.h> 1557e9624eSPoul-Henning Kamp #include <sys/linker.h> 1670d586c0SPoul-Henning Kamp #include <sys/mdioctl.h> 174bfd989fSWojciech A. Koszek #include <sys/module.h> 18c27a8954SWojciech A. Koszek #include <sys/resource.h> 19b830359bSPawel Jakub Dawidek #include <sys/stat.h> 20174b5e9aSPoul-Henning Kamp 214bfd989fSWojciech A. Koszek #include <assert.h> 22c27a8954SWojciech A. Koszek #include <devstat.h> 234bfd989fSWojciech A. Koszek #include <err.h> 244bfd989fSWojciech A. Koszek #include <errno.h> 254bfd989fSWojciech A. Koszek #include <fcntl.h> 264bfd989fSWojciech A. Koszek #include <inttypes.h> 27c27a8954SWojciech A. Koszek #include <libgeom.h> 284bfd989fSWojciech A. Koszek #include <libutil.h> 29c27a8954SWojciech A. Koszek #include <stdarg.h> 304bfd989fSWojciech A. Koszek #include <stdio.h> 314bfd989fSWojciech A. Koszek #include <stdlib.h> 324bfd989fSWojciech A. Koszek #include <string.h> 334bfd989fSWojciech A. Koszek #include <unistd.h> 3470d586c0SPoul-Henning Kamp 3570d586c0SPoul-Henning Kamp 364bfd989fSWojciech A. Koszek static struct md_ioctl mdio; 374bfd989fSWojciech A. Koszek static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET; 384bfd989fSWojciech A. Koszek static int nflag; 3970d586c0SPoul-Henning Kamp 40c27a8954SWojciech A. Koszek static void usage(void); 41c27a8954SWojciech A. Koszek static int md_find(char *, const char *); 42c27a8954SWojciech A. Koszek static int md_query(char *name); 43c27a8954SWojciech A. Koszek static int md_list(char *units, int opt); 44c27a8954SWojciech A. Koszek static char *geom_config_get(struct gconf *g, char *name); 45c27a8954SWojciech A. Koszek static void md_prthumanval(char *length); 46c27a8954SWojciech A. Koszek 47c27a8954SWojciech A. Koszek #define OPT_VERBOSE 0x01 48c27a8954SWojciech A. Koszek #define OPT_UNIT 0x02 49c27a8954SWojciech A. Koszek #define OPT_DONE 0x04 50c27a8954SWojciech A. Koszek #define OPT_LIST 0x10 51c27a8954SWojciech A. Koszek 52c27a8954SWojciech A. Koszek #define CLASS_NAME_MD "MD" 53c27a8954SWojciech A. Koszek 544bfd989fSWojciech A. Koszek static void 55c2ef0b73SPoul-Henning Kamp usage() 56c2ef0b73SPoul-Henning Kamp { 5778bb1162SRuslan Ermilov fprintf(stderr, 5878bb1162SRuslan Ermilov "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n" 5978bb1162SRuslan Ermilov " [-s size] [-S sectorsize] [-u unit]\n" 6078bb1162SRuslan Ermilov " [-x sectors/track] [-y heads/cyl]\n" 6178bb1162SRuslan Ermilov " mdconfig -d -u unit\n" 6278bb1162SRuslan Ermilov " mdconfig -l [-n] [-u unit]\n"); 63c2ef0b73SPoul-Henning Kamp fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n"); 643f6f9216SPoul-Henning Kamp fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n"); 655d19b2f9SPawel Jakub Dawidek fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n"); 665d19b2f9SPawel Jakub Dawidek fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n"); 675d19b2f9SPawel Jakub Dawidek fprintf(stderr, "\t\t %%dt (TB)\n"); 68c2ef0b73SPoul-Henning Kamp exit(1); 69c2ef0b73SPoul-Henning Kamp } 70c2ef0b73SPoul-Henning Kamp 7170d586c0SPoul-Henning Kamp int 7270d586c0SPoul-Henning Kamp main(int argc, char **argv) 7370d586c0SPoul-Henning Kamp { 7470d586c0SPoul-Henning Kamp int ch, fd, i; 75c2ef0b73SPoul-Henning Kamp char *p; 76c2ef0b73SPoul-Henning Kamp int cmdline = 0; 77c27a8954SWojciech A. Koszek char *mdunit; 7870d586c0SPoul-Henning Kamp 79b830359bSPawel Jakub Dawidek bzero(&mdio, sizeof(mdio)); 8088b5b78dSPawel Jakub Dawidek mdio.md_file = malloc(PATH_MAX); 8188b5b78dSPawel Jakub Dawidek if (mdio.md_file == NULL) 8288b5b78dSPawel Jakub Dawidek err(1, "could not allocate memory"); 8388b5b78dSPawel Jakub Dawidek bzero(mdio.md_file, PATH_MAX); 8470d586c0SPoul-Henning Kamp for (;;) { 85f79c46d3SRobert Watson ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:"); 8670d586c0SPoul-Henning Kamp if (ch == -1) 8770d586c0SPoul-Henning Kamp break; 8870d586c0SPoul-Henning Kamp switch (ch) { 8970d586c0SPoul-Henning Kamp case 'a': 90c2ef0b73SPoul-Henning Kamp if (cmdline != 0) 91c2ef0b73SPoul-Henning Kamp usage(); 9270d586c0SPoul-Henning Kamp action = ATTACH; 93c2ef0b73SPoul-Henning Kamp cmdline = 1; 9470d586c0SPoul-Henning Kamp break; 9570d586c0SPoul-Henning Kamp case 'd': 96c2ef0b73SPoul-Henning Kamp if (cmdline != 0) 97c2ef0b73SPoul-Henning Kamp usage(); 9870d586c0SPoul-Henning Kamp action = DETACH; 998f8def9eSPoul-Henning Kamp mdio.md_options = MD_AUTOUNIT; 1008f8def9eSPoul-Henning Kamp cmdline = 3; 101c2ef0b73SPoul-Henning Kamp break; 102174b5e9aSPoul-Henning Kamp case 'l': 103174b5e9aSPoul-Henning Kamp if (cmdline != 0) 104174b5e9aSPoul-Henning Kamp usage(); 105174b5e9aSPoul-Henning Kamp action = LIST; 106174b5e9aSPoul-Henning Kamp mdio.md_options = MD_AUTOUNIT; 107174b5e9aSPoul-Henning Kamp cmdline = 3; 108174b5e9aSPoul-Henning Kamp break; 109f79c46d3SRobert Watson case 'n': 110f79c46d3SRobert Watson nflag = 1; 111f79c46d3SRobert Watson break; 112c2ef0b73SPoul-Henning Kamp case 't': 113c2ef0b73SPoul-Henning Kamp if (cmdline != 1) 114c2ef0b73SPoul-Henning Kamp usage(); 115c2ef0b73SPoul-Henning Kamp if (!strcmp(optarg, "malloc")) { 116c2ef0b73SPoul-Henning Kamp mdio.md_type = MD_MALLOC; 117c2ef0b73SPoul-Henning Kamp mdio.md_options = MD_AUTOUNIT | MD_COMPRESS; 118c2ef0b73SPoul-Henning Kamp } else if (!strcmp(optarg, "preload")) { 119c2ef0b73SPoul-Henning Kamp mdio.md_type = MD_PRELOAD; 120c2ef0b73SPoul-Henning Kamp mdio.md_options = 0; 121c2ef0b73SPoul-Henning Kamp } else if (!strcmp(optarg, "vnode")) { 122c2ef0b73SPoul-Henning Kamp mdio.md_type = MD_VNODE; 123c2ef0b73SPoul-Henning Kamp mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 124c2ef0b73SPoul-Henning Kamp } else if (!strcmp(optarg, "swap")) { 125c2ef0b73SPoul-Henning Kamp mdio.md_type = MD_SWAP; 126c2ef0b73SPoul-Henning Kamp mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 127c2ef0b73SPoul-Henning Kamp } else { 128c2ef0b73SPoul-Henning Kamp usage(); 129c2ef0b73SPoul-Henning Kamp } 130c2ef0b73SPoul-Henning Kamp cmdline=2; 13170d586c0SPoul-Henning Kamp break; 13270d586c0SPoul-Henning Kamp case 'f': 1331253fe1eSNick Hibma if (cmdline == 0) { 1341253fe1eSNick Hibma action = ATTACH; 1351253fe1eSNick Hibma cmdline = 1; 1361253fe1eSNick Hibma } 137ed23a390SMaxim Sobolev if (cmdline == 1) { 138ed23a390SMaxim Sobolev /* Imply ``-t vnode'' */ 139ed23a390SMaxim Sobolev mdio.md_type = MD_VNODE; 140ed23a390SMaxim Sobolev mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 141252bcf45SYaroslav Tykhiy cmdline = 2; 142ed23a390SMaxim Sobolev } 14335ce0ff2SNick Hibma if (cmdline != 2) 14435ce0ff2SNick Hibma usage(); 14561a6eb62SPawel Jakub Dawidek if (realpath(optarg, mdio.md_file) == NULL) { 14661a6eb62SPawel Jakub Dawidek err(1, "could not find full path for %s", 14761a6eb62SPawel Jakub Dawidek optarg); 14861a6eb62SPawel Jakub Dawidek } 14961a6eb62SPawel Jakub Dawidek fd = open(mdio.md_file, O_RDONLY); 150e869d377SPoul-Henning Kamp if (fd < 0) 151e869d377SPoul-Henning Kamp err(1, "could not open %s", optarg); 152b830359bSPawel Jakub Dawidek else if (mdio.md_mediasize == 0) { 153b830359bSPawel Jakub Dawidek struct stat sb; 154b830359bSPawel Jakub Dawidek 155b830359bSPawel Jakub Dawidek if (fstat(fd, &sb) == -1) 156b830359bSPawel Jakub Dawidek err(1, "could not stat %s", optarg); 157b830359bSPawel Jakub Dawidek mdio.md_mediasize = sb.st_size; 158b830359bSPawel Jakub Dawidek } 159e869d377SPoul-Henning Kamp close(fd); 16070d586c0SPoul-Henning Kamp break; 16170d586c0SPoul-Henning Kamp case 'o': 162c2ef0b73SPoul-Henning Kamp if (cmdline != 2) 163c2ef0b73SPoul-Henning Kamp usage(); 1647a6b2b64SPoul-Henning Kamp if (!strcmp(optarg, "async")) 1657a6b2b64SPoul-Henning Kamp mdio.md_options |= MD_ASYNC; 1667a6b2b64SPoul-Henning Kamp else if (!strcmp(optarg, "noasync")) 1677a6b2b64SPoul-Henning Kamp mdio.md_options &= ~MD_ASYNC; 1687a6b2b64SPoul-Henning Kamp else if (!strcmp(optarg, "cluster")) 16970d586c0SPoul-Henning Kamp mdio.md_options |= MD_CLUSTER; 17070d586c0SPoul-Henning Kamp else if (!strcmp(optarg, "nocluster")) 17170d586c0SPoul-Henning Kamp mdio.md_options &= ~MD_CLUSTER; 172c2ef0b73SPoul-Henning Kamp else if (!strcmp(optarg, "compress")) 173c2ef0b73SPoul-Henning Kamp mdio.md_options |= MD_COMPRESS; 174c2ef0b73SPoul-Henning Kamp else if (!strcmp(optarg, "nocompress")) 175c2ef0b73SPoul-Henning Kamp mdio.md_options &= ~MD_COMPRESS; 17626a0ee75SDima Dorfman else if (!strcmp(optarg, "force")) 17726a0ee75SDima Dorfman mdio.md_options |= MD_FORCE; 17826a0ee75SDima Dorfman else if (!strcmp(optarg, "noforce")) 17926a0ee75SDima Dorfman mdio.md_options &= ~MD_FORCE; 180d31ba625SJohn-Mark Gurney else if (!strcmp(optarg, "readonly")) 181d31ba625SJohn-Mark Gurney mdio.md_options |= MD_READONLY; 182d31ba625SJohn-Mark Gurney else if (!strcmp(optarg, "noreadonly")) 183d31ba625SJohn-Mark Gurney mdio.md_options &= ~MD_READONLY; 18470d586c0SPoul-Henning Kamp else if (!strcmp(optarg, "reserve")) 18570d586c0SPoul-Henning Kamp mdio.md_options |= MD_RESERVE; 18670d586c0SPoul-Henning Kamp else if (!strcmp(optarg, "noreserve")) 18770d586c0SPoul-Henning Kamp mdio.md_options &= ~MD_RESERVE; 18870d586c0SPoul-Henning Kamp else 189d31ba625SJohn-Mark Gurney errx(1, "Unknown option: %s.", optarg); 19070d586c0SPoul-Henning Kamp break; 191ebe789d6SPoul-Henning Kamp case 'S': 192ebe789d6SPoul-Henning Kamp if (cmdline != 2) 193ebe789d6SPoul-Henning Kamp usage(); 194b830359bSPawel Jakub Dawidek mdio.md_sectorsize = strtoul(optarg, &p, 0); 195ebe789d6SPoul-Henning Kamp break; 19670d586c0SPoul-Henning Kamp case 's': 19735ce0ff2SNick Hibma if (cmdline == 0) { 19835ce0ff2SNick Hibma /* Imply ``-a'' */ 19935ce0ff2SNick Hibma action = ATTACH; 20035ce0ff2SNick Hibma cmdline = 1; 20135ce0ff2SNick Hibma } 20235ce0ff2SNick Hibma if (cmdline == 1) { 2037090e3d1SNick Hibma /* Imply ``-t swap'' */ 2047090e3d1SNick Hibma mdio.md_type = MD_SWAP; 2057090e3d1SNick Hibma mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 20635ce0ff2SNick Hibma cmdline = 2; 20735ce0ff2SNick Hibma } 208c2ef0b73SPoul-Henning Kamp if (cmdline != 2) 209c2ef0b73SPoul-Henning Kamp usage(); 210b830359bSPawel Jakub Dawidek mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0); 211c2ef0b73SPoul-Henning Kamp if (p == NULL || *p == '\0') 212b830359bSPawel Jakub Dawidek mdio.md_mediasize *= DEV_BSIZE; 2130d79319aSPawel Jakub Dawidek else if (*p == 'b' || *p == 'B') 2140d79319aSPawel Jakub Dawidek ; /* do nothing */ 215c2ef0b73SPoul-Henning Kamp else if (*p == 'k' || *p == 'K') 216b830359bSPawel Jakub Dawidek mdio.md_mediasize <<= 10; 217c2ef0b73SPoul-Henning Kamp else if (*p == 'm' || *p == 'M') 218b830359bSPawel Jakub Dawidek mdio.md_mediasize <<= 20; 219c2ef0b73SPoul-Henning Kamp else if (*p == 'g' || *p == 'G') 220b830359bSPawel Jakub Dawidek mdio.md_mediasize <<= 30; 221b830359bSPawel Jakub Dawidek else if (*p == 't' || *p == 'T') { 222b830359bSPawel Jakub Dawidek mdio.md_mediasize <<= 30; 223b830359bSPawel Jakub Dawidek mdio.md_mediasize <<= 10; 224b830359bSPawel Jakub Dawidek } else 225c2ef0b73SPoul-Henning Kamp errx(1, "Unknown suffix on -s argument"); 22670d586c0SPoul-Henning Kamp break; 22770d586c0SPoul-Henning Kamp case 'u': 2288f8def9eSPoul-Henning Kamp if (cmdline != 2 && cmdline != 3) 229c2ef0b73SPoul-Henning Kamp usage(); 230ea3d97aeSWojciech A. Koszek if (!strncmp(optarg, "/dev/", 5)) 231ea3d97aeSWojciech A. Koszek optarg += 5; 232ea3d97aeSWojciech A. Koszek if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1)) 233ea3d97aeSWojciech A. Koszek optarg += sizeof(MD_NAME) - 1; 234ea3d97aeSWojciech A. Koszek mdio.md_unit = strtoul(optarg, &p, 0); 235ea3d97aeSWojciech A. Koszek if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0') 236ea3d97aeSWojciech A. Koszek errx(1, "bad unit: %s", optarg); 237c27a8954SWojciech A. Koszek mdunit = optarg; 23870d586c0SPoul-Henning Kamp mdio.md_options &= ~MD_AUTOUNIT; 23970d586c0SPoul-Henning Kamp break; 2404e8bfe14SPoul-Henning Kamp case 'x': 2414e8bfe14SPoul-Henning Kamp if (cmdline != 2) 2424e8bfe14SPoul-Henning Kamp usage(); 2434e8bfe14SPoul-Henning Kamp mdio.md_fwsectors = strtoul(optarg, &p, 0); 2444e8bfe14SPoul-Henning Kamp break; 2454e8bfe14SPoul-Henning Kamp case 'y': 2464e8bfe14SPoul-Henning Kamp if (cmdline != 2) 2474e8bfe14SPoul-Henning Kamp usage(); 2484e8bfe14SPoul-Henning Kamp mdio.md_fwheads = strtoul(optarg, &p, 0); 2494e8bfe14SPoul-Henning Kamp break; 25070d586c0SPoul-Henning Kamp default: 251c2ef0b73SPoul-Henning Kamp usage(); 25270d586c0SPoul-Henning Kamp } 25370d586c0SPoul-Henning Kamp } 25453d745bcSDima Dorfman mdio.md_version = MDIOVERSION; 25570d586c0SPoul-Henning Kamp 256a921cb31SPawel Jakub Dawidek if (!kld_isloaded("g_md") && kld_load("geom_md") == -1) 257a921cb31SPawel Jakub Dawidek err(1, "failed to load geom_md module"); 258a921cb31SPawel Jakub Dawidek 259174b5e9aSPoul-Henning Kamp fd = open("/dev/" MDCTL_NAME, O_RDWR, 0); 26070d586c0SPoul-Henning Kamp if (fd < 0) 261174b5e9aSPoul-Henning Kamp err(1, "open(/dev/%s)", MDCTL_NAME); 2622885b421SDima Dorfman if (cmdline == 2 2632885b421SDima Dorfman && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP)) 264b830359bSPawel Jakub Dawidek if (mdio.md_mediasize == 0) 2652885b421SDima Dorfman errx(1, "must specify -s for -t malloc or -t swap"); 266252bcf45SYaroslav Tykhiy if (cmdline == 2 && mdio.md_type == MD_VNODE) 26788b5b78dSPawel Jakub Dawidek if (mdio.md_file[0] == '\0') 268252bcf45SYaroslav Tykhiy errx(1, "must specify -f for -t vnode"); 269c313f09bSChristian S.J. Peron if (mdio.md_type == MD_VNODE && 270c313f09bSChristian S.J. Peron (mdio.md_options & MD_READONLY) == 0) { 271c313f09bSChristian S.J. Peron if (access(mdio.md_file, W_OK) < 0 && 272c313f09bSChristian S.J. Peron (errno == EACCES || errno == EPERM || errno == EROFS)) { 273c313f09bSChristian S.J. Peron fprintf(stderr, 274c313f09bSChristian S.J. Peron "WARNING: opening backing store: %s readonly\n", 275c313f09bSChristian S.J. Peron mdio.md_file); 276c313f09bSChristian S.J. Peron mdio.md_options |= MD_READONLY; 277c313f09bSChristian S.J. Peron } 278c313f09bSChristian S.J. Peron } 279174b5e9aSPoul-Henning Kamp if (action == LIST) { 280c27a8954SWojciech A. Koszek if (mdio.md_options & MD_AUTOUNIT) { 281c27a8954SWojciech A. Koszek /* 282c27a8954SWojciech A. Koszek * Listing all devices. This is why we pass NULL 283c27a8954SWojciech A. Koszek * together with OPT_LIST. 284c27a8954SWojciech A. Koszek */ 285c27a8954SWojciech A. Koszek md_list(NULL, OPT_LIST); 286c27a8954SWojciech A. Koszek } else { 28769fcb537SFlorent Thoumie return (md_query(mdunit)); 288c27a8954SWojciech A. Koszek } 289174b5e9aSPoul-Henning Kamp } else if (action == ATTACH) { 290252bcf45SYaroslav Tykhiy if (cmdline < 2) 291252bcf45SYaroslav Tykhiy usage(); 29270d586c0SPoul-Henning Kamp i = ioctl(fd, MDIOCATTACH, &mdio); 293174b5e9aSPoul-Henning Kamp if (i < 0) 294174b5e9aSPoul-Henning Kamp err(1, "ioctl(/dev/%s)", MDCTL_NAME); 29583da2a90SPoul-Henning Kamp if (mdio.md_options & MD_AUTOUNIT) 296f79c46d3SRobert Watson printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit); 29783da2a90SPoul-Henning Kamp } else if (action == DETACH) { 2988f8def9eSPoul-Henning Kamp if (mdio.md_options & MD_AUTOUNIT) 2998f8def9eSPoul-Henning Kamp usage(); 300c2ef0b73SPoul-Henning Kamp i = ioctl(fd, MDIOCDETACH, &mdio); 30170d586c0SPoul-Henning Kamp if (i < 0) 302174b5e9aSPoul-Henning Kamp err(1, "ioctl(/dev/%s)", MDCTL_NAME); 30383da2a90SPoul-Henning Kamp } else 30483da2a90SPoul-Henning Kamp usage(); 305174b5e9aSPoul-Henning Kamp close (fd); 306174b5e9aSPoul-Henning Kamp return (0); 307174b5e9aSPoul-Henning Kamp } 308174b5e9aSPoul-Henning Kamp 309c27a8954SWojciech A. Koszek /* 310c27a8954SWojciech A. Koszek * Lists md(4) disks. Is used also as a query routine, since it handles XML 311c27a8954SWojciech A. Koszek * interface. 'units' can be NULL for listing memory disks. It might be 312c27a8954SWojciech A. Koszek * coma-separated string containing md(4) disk names. 'opt' distinguished 313c27a8954SWojciech A. Koszek * between list and query mode. 314c27a8954SWojciech A. Koszek */ 3157e06d7bcSDima Dorfman static int 316c27a8954SWojciech A. Koszek md_list(char *units, int opt) 3177e06d7bcSDima Dorfman { 318c27a8954SWojciech A. Koszek struct gmesh gm; 319c27a8954SWojciech A. Koszek struct gprovider *pp; 320c27a8954SWojciech A. Koszek struct gconf *gc; 321c27a8954SWojciech A. Koszek struct gident *gid; 322c3345c66SJason Evans struct devstat *gsp; 323c27a8954SWojciech A. Koszek struct ggeom *gg; 324c27a8954SWojciech A. Koszek struct gclass *gcl; 325c27a8954SWojciech A. Koszek void *sq; 32669fcb537SFlorent Thoumie int retcode, found; 327c27a8954SWojciech A. Koszek char *type, *file, *length; 3287e06d7bcSDima Dorfman 329c27a8954SWojciech A. Koszek type = file = length = NULL; 330174b5e9aSPoul-Henning Kamp 331c27a8954SWojciech A. Koszek retcode = geom_gettree(&gm); 332c27a8954SWojciech A. Koszek if (retcode != 0) 333c27a8954SWojciech A. Koszek return (-1); 334c27a8954SWojciech A. Koszek retcode = geom_stats_open(); 335c27a8954SWojciech A. Koszek if (retcode != 0) 336c27a8954SWojciech A. Koszek return (-1); 337c27a8954SWojciech A. Koszek sq = geom_stats_snapshot_get(); 338c27a8954SWojciech A. Koszek if (sq == NULL) 339c27a8954SWojciech A. Koszek return (-1); 340c27a8954SWojciech A. Koszek 34169fcb537SFlorent Thoumie found = 0; 342c27a8954SWojciech A. Koszek while ((gsp = geom_stats_snapshot_next(sq)) != NULL) { 343c27a8954SWojciech A. Koszek gid = geom_lookupid(&gm, gsp->id); 344c27a8954SWojciech A. Koszek if (gid == NULL) 345c27a8954SWojciech A. Koszek continue; 346c27a8954SWojciech A. Koszek if (gid->lg_what == ISPROVIDER) { 347c27a8954SWojciech A. Koszek pp = gid->lg_ptr; 348c27a8954SWojciech A. Koszek gg = pp->lg_geom; 349c27a8954SWojciech A. Koszek gcl = gg->lg_class; 350c27a8954SWojciech A. Koszek if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0) 351c27a8954SWojciech A. Koszek continue; 352c27a8954SWojciech A. Koszek if ((opt & OPT_UNIT) && (units != NULL)) { 353c27a8954SWojciech A. Koszek retcode = md_find(units, pp->lg_name); 354c27a8954SWojciech A. Koszek if (retcode != 1) 355c27a8954SWojciech A. Koszek continue; 35669fcb537SFlorent Thoumie else 35769fcb537SFlorent Thoumie found = 1; 358174b5e9aSPoul-Henning Kamp } 359c27a8954SWojciech A. Koszek gc = &pp->lg_config; 360c27a8954SWojciech A. Koszek printf("%s", pp->lg_name); 361c27a8954SWojciech A. Koszek if (opt & OPT_VERBOSE || opt & OPT_UNIT) { 362c27a8954SWojciech A. Koszek type = geom_config_get(gc, "type"); 363c27a8954SWojciech A. Koszek if (strcmp(type, "vnode") == 0) 364c27a8954SWojciech A. Koszek file = geom_config_get(gc, "file"); 365c27a8954SWojciech A. Koszek length = geom_config_get(gc, "length"); 366c27a8954SWojciech A. Koszek if (length == NULL) 367c27a8954SWojciech A. Koszek length = "<a>"; 368c27a8954SWojciech A. Koszek printf("\t%s\t", type); 369c27a8954SWojciech A. Koszek md_prthumanval(length); 370c27a8954SWojciech A. Koszek if (file != NULL) { 371c27a8954SWojciech A. Koszek printf("\t%s", file); 372c27a8954SWojciech A. Koszek file = NULL; 373c27a8954SWojciech A. Koszek } 374c27a8954SWojciech A. Koszek } 375c27a8954SWojciech A. Koszek opt |= OPT_DONE; 376c27a8954SWojciech A. Koszek if (opt & OPT_LIST) 377c27a8954SWojciech A. Koszek printf(" "); 378c27a8954SWojciech A. Koszek else 379e39eff98SPoul-Henning Kamp printf("\n"); 380c27a8954SWojciech A. Koszek } 381c27a8954SWojciech A. Koszek } 382c27a8954SWojciech A. Koszek if ((opt & OPT_LIST) && (opt & OPT_DONE)) 383c27a8954SWojciech A. Koszek printf("\n"); 384c27a8954SWojciech A. Koszek /* XXX: Check if it's enough to clean everything. */ 385c27a8954SWojciech A. Koszek geom_stats_snapshot_free(sq); 38669fcb537SFlorent Thoumie if ((opt & OPT_UNIT) && found) 38769fcb537SFlorent Thoumie return (0); 38869fcb537SFlorent Thoumie else 389c27a8954SWojciech A. Koszek return (-1); 390c27a8954SWojciech A. Koszek } 391c27a8954SWojciech A. Koszek 392c27a8954SWojciech A. Koszek /* 393c27a8954SWojciech A. Koszek * Returns value of 'name' from gconfig structure. 394c27a8954SWojciech A. Koszek */ 395c27a8954SWojciech A. Koszek static char * 396c27a8954SWojciech A. Koszek geom_config_get(struct gconf *g, char *name) 397c27a8954SWojciech A. Koszek { 398c27a8954SWojciech A. Koszek struct gconfig *gce; 399c27a8954SWojciech A. Koszek 400c27a8954SWojciech A. Koszek LIST_FOREACH(gce, g, lg_config) { 401c27a8954SWojciech A. Koszek if (strcmp(gce->lg_name, name) == 0) 402c27a8954SWojciech A. Koszek return (gce->lg_val); 403c27a8954SWojciech A. Koszek } 404c27a8954SWojciech A. Koszek return (NULL); 405c27a8954SWojciech A. Koszek } 406c27a8954SWojciech A. Koszek 407c27a8954SWojciech A. Koszek /* 408c27a8954SWojciech A. Koszek * List is comma separated list of MD disks. name is a 409c27a8954SWojciech A. Koszek * device name we look for. Returns 1 if found and 0 410c27a8954SWojciech A. Koszek * otherwise. 411c27a8954SWojciech A. Koszek */ 412c27a8954SWojciech A. Koszek static int 413c27a8954SWojciech A. Koszek md_find(char *list, const char *name) 414c27a8954SWojciech A. Koszek { 415c27a8954SWojciech A. Koszek int ret; 416c27a8954SWojciech A. Koszek char num[16]; 417c27a8954SWojciech A. Koszek char *ptr, *p, *u; 418c27a8954SWojciech A. Koszek 419c27a8954SWojciech A. Koszek ret = 0; 420c27a8954SWojciech A. Koszek ptr = strdup(list); 421c27a8954SWojciech A. Koszek if (ptr == NULL) 422c27a8954SWojciech A. Koszek return (-1); 423c27a8954SWojciech A. Koszek for (p = ptr; (u = strsep(&p, ",")) != NULL;) { 424c27a8954SWojciech A. Koszek if (strncmp(u, "/dev/", 5) == 0) 425c27a8954SWojciech A. Koszek u += 5; 426c27a8954SWojciech A. Koszek /* Just in case user specified number instead of full name */ 427c27a8954SWojciech A. Koszek snprintf(num, sizeof(num), "md%s", u); 428c27a8954SWojciech A. Koszek if (strcmp(u, name) == 0 || strcmp(num, name) == 0) { 429c27a8954SWojciech A. Koszek ret = 1; 430c27a8954SWojciech A. Koszek break; 431c27a8954SWojciech A. Koszek } 432c27a8954SWojciech A. Koszek } 433c27a8954SWojciech A. Koszek free(ptr); 434c27a8954SWojciech A. Koszek return (ret); 435174b5e9aSPoul-Henning Kamp } 436174b5e9aSPoul-Henning Kamp 437b830359bSPawel Jakub Dawidek static void 438c27a8954SWojciech A. Koszek md_prthumanval(char *length) 439b830359bSPawel Jakub Dawidek { 440b830359bSPawel Jakub Dawidek char buf[6]; 441c27a8954SWojciech A. Koszek uint64_t bytes; 442c27a8954SWojciech A. Koszek char *endptr; 443b830359bSPawel Jakub Dawidek 444c27a8954SWojciech A. Koszek bytes = strtoul(length, &endptr, 10); 445c27a8954SWojciech A. Koszek if (bytes == (unsigned)ULONG_MAX || *endptr != '\0') 446c27a8954SWojciech A. Koszek return; 447b830359bSPawel Jakub Dawidek humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), 448b830359bSPawel Jakub Dawidek bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 449b830359bSPawel Jakub Dawidek (void)printf("%6s", buf); 450b830359bSPawel Jakub Dawidek } 451b830359bSPawel Jakub Dawidek 452c27a8954SWojciech A. Koszek int 453c27a8954SWojciech A. Koszek md_query(char *name) 454174b5e9aSPoul-Henning Kamp { 455c27a8954SWojciech A. Koszek return (md_list(name, OPT_UNIT)); 45670d586c0SPoul-Henning Kamp } 457