189ef44efSEdward Tomasz Napierala /*- 289ef44efSEdward Tomasz Napierala * Copyright (c) 2000-2004 Poul-Henning Kamp <phk@FreeBSD.org> 377c43773SEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 489ef44efSEdward Tomasz Napierala * All rights reserved. 589ef44efSEdward Tomasz Napierala * 677c43773SEdward Tomasz Napierala * Portions of this software were developed by Edward Tomasz Napierala 777c43773SEdward Tomasz Napierala * under sponsorship from the FreeBSD Foundation. 877c43773SEdward Tomasz Napierala * 989ef44efSEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without 1089ef44efSEdward Tomasz Napierala * modification, are permitted provided that the following conditions 1189ef44efSEdward Tomasz Napierala * are met: 1289ef44efSEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright 1389ef44efSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer. 1489ef44efSEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright 1589ef44efSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the 1689ef44efSEdward Tomasz Napierala * documentation and/or other materials provided with the distribution. 1789ef44efSEdward Tomasz Napierala * 1889ef44efSEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1989ef44efSEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2089ef44efSEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2189ef44efSEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2289ef44efSEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2389ef44efSEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2489ef44efSEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2589ef44efSEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2689ef44efSEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2789ef44efSEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2889ef44efSEdward Tomasz Napierala * SUCH DAMAGE. 2970d586c0SPoul-Henning Kamp * 3070d586c0SPoul-Henning Kamp * $FreeBSD$ 3170d586c0SPoul-Henning Kamp */ 3289ef44efSEdward Tomasz Napierala 3370d586c0SPoul-Henning Kamp #include <sys/param.h> 34c27a8954SWojciech A. Koszek #include <sys/devicestat.h> 354bfd989fSWojciech A. Koszek #include <sys/ioctl.h> 3657e9624eSPoul-Henning Kamp #include <sys/linker.h> 3770d586c0SPoul-Henning Kamp #include <sys/mdioctl.h> 384bfd989fSWojciech A. Koszek #include <sys/module.h> 39c27a8954SWojciech A. Koszek #include <sys/resource.h> 40b830359bSPawel Jakub Dawidek #include <sys/stat.h> 41174b5e9aSPoul-Henning Kamp 424bfd989fSWojciech A. Koszek #include <assert.h> 43c27a8954SWojciech A. Koszek #include <devstat.h> 444bfd989fSWojciech A. Koszek #include <err.h> 454bfd989fSWojciech A. Koszek #include <errno.h> 464bfd989fSWojciech A. Koszek #include <fcntl.h> 474bfd989fSWojciech A. Koszek #include <inttypes.h> 48c27a8954SWojciech A. Koszek #include <libgeom.h> 494bfd989fSWojciech A. Koszek #include <libutil.h> 50b78ae23fSHiroki Sato #include <paths.h> 51c27a8954SWojciech A. Koszek #include <stdarg.h> 524bfd989fSWojciech A. Koszek #include <stdio.h> 534bfd989fSWojciech A. Koszek #include <stdlib.h> 544bfd989fSWojciech A. Koszek #include <string.h> 554bfd989fSWojciech A. Koszek #include <unistd.h> 5670d586c0SPoul-Henning Kamp 574bfd989fSWojciech A. Koszek static struct md_ioctl mdio; 58dc604f0cSEdward Tomasz Napierala static enum {UNSET, ATTACH, DETACH, RESIZE, LIST} action = UNSET; 594bfd989fSWojciech A. Koszek static int nflag; 6070d586c0SPoul-Henning Kamp 61c27a8954SWojciech A. Koszek static void usage(void); 626c49c6cfSDag-Erling Smørgrav static void md_set_file(const char *); 63b78ae23fSHiroki Sato static int md_find(const char *, const char *); 64b78ae23fSHiroki Sato static int md_query(const char *, const int, const char *); 65b78ae23fSHiroki Sato static int md_list(const char *, int, const char *); 66a4ca6a49SJaakko Heinonen static char *geom_config_get(struct gconf *g, const char *name); 67c27a8954SWojciech A. Koszek static void md_prthumanval(char *length); 68c27a8954SWojciech A. Koszek 69c27a8954SWojciech A. Koszek #define OPT_VERBOSE 0x01 70c27a8954SWojciech A. Koszek #define OPT_UNIT 0x02 71c27a8954SWojciech A. Koszek #define OPT_DONE 0x04 72c27a8954SWojciech A. Koszek #define OPT_LIST 0x10 73c27a8954SWojciech A. Koszek 74c27a8954SWojciech A. Koszek #define CLASS_NAME_MD "MD" 75c27a8954SWojciech A. Koszek 764bfd989fSWojciech A. Koszek static void 77a4ca6a49SJaakko Heinonen usage(void) 78c2ef0b73SPoul-Henning Kamp { 7977c43773SEdward Tomasz Napierala 8078bb1162SRuslan Ermilov fprintf(stderr, 8178bb1162SRuslan Ermilov "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n" 8278bb1162SRuslan Ermilov " [-s size] [-S sectorsize] [-u unit]\n" 83db280204SJoel Dahl " [-x sectors/track] [-y heads/cylinder]\n" 84a9ebb311SEdward Tomasz Napierala " mdconfig -d -u unit [-o [no]force]\n" 85dc604f0cSEdward Tomasz Napierala " mdconfig -r -u unit -s size [-o [no]force]\n" 86b78ae23fSHiroki Sato " mdconfig -l [-v] [-n] [-f file] [-u unit]\n" 876c49c6cfSDag-Erling Smørgrav " mdconfig file\n"); 88734e78dfSJaakko Heinonen fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n"); 893f6f9216SPoul-Henning Kamp fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n"); 905d19b2f9SPawel Jakub Dawidek fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n"); 91d5b0fb47SEdward Tomasz Napierala fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB), \n"); 92d5b0fb47SEdward Tomasz Napierala fprintf(stderr, "\t\t %%dt (TB), or %%dp (PB)\n"); 93c2ef0b73SPoul-Henning Kamp exit(1); 94c2ef0b73SPoul-Henning Kamp } 95c2ef0b73SPoul-Henning Kamp 9670d586c0SPoul-Henning Kamp int 9770d586c0SPoul-Henning Kamp main(int argc, char **argv) 9870d586c0SPoul-Henning Kamp { 99c94b8307SDmitry Morozovsky int ch, fd, i, vflag; 100c2ef0b73SPoul-Henning Kamp char *p; 101dc604f0cSEdward Tomasz Napierala char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL; 10270d586c0SPoul-Henning Kamp 103b830359bSPawel Jakub Dawidek bzero(&mdio, sizeof(mdio)); 10488b5b78dSPawel Jakub Dawidek mdio.md_file = malloc(PATH_MAX); 10588b5b78dSPawel Jakub Dawidek if (mdio.md_file == NULL) 10688b5b78dSPawel Jakub Dawidek err(1, "could not allocate memory"); 107c94b8307SDmitry Morozovsky vflag = 0; 10888b5b78dSPawel Jakub Dawidek bzero(mdio.md_file, PATH_MAX); 10977c43773SEdward Tomasz Napierala 11077c43773SEdward Tomasz Napierala if (argc == 1) 11177c43773SEdward Tomasz Napierala usage(); 11277c43773SEdward Tomasz Napierala 113dc604f0cSEdward Tomasz Napierala while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:")) != -1) { 11470d586c0SPoul-Henning Kamp switch (ch) { 11570d586c0SPoul-Henning Kamp case 'a': 11677c43773SEdward Tomasz Napierala if (action != UNSET && action != ATTACH) 117dc604f0cSEdward Tomasz Napierala errx(1, "-a is mutually exclusive " 118dc604f0cSEdward Tomasz Napierala "with -d, -r, and -l"); 11970d586c0SPoul-Henning Kamp action = ATTACH; 12070d586c0SPoul-Henning Kamp break; 12170d586c0SPoul-Henning Kamp case 'd': 12277c43773SEdward Tomasz Napierala if (action != UNSET && action != DETACH) 123dc604f0cSEdward Tomasz Napierala errx(1, "-d is mutually exclusive " 124dc604f0cSEdward Tomasz Napierala "with -a, -r, and -l"); 12570d586c0SPoul-Henning Kamp action = DETACH; 12677c43773SEdward Tomasz Napierala mdio.md_options |= MD_AUTOUNIT; 127c2ef0b73SPoul-Henning Kamp break; 128dc604f0cSEdward Tomasz Napierala case 'r': 129dc604f0cSEdward Tomasz Napierala if (action != UNSET && action != RESIZE) 130dc604f0cSEdward Tomasz Napierala errx(1, "-r is mutually exclusive " 131dc604f0cSEdward Tomasz Napierala "with -a, -d, and -l"); 132dc604f0cSEdward Tomasz Napierala action = RESIZE; 133dc604f0cSEdward Tomasz Napierala mdio.md_options |= MD_AUTOUNIT; 134dc604f0cSEdward Tomasz Napierala break; 135174b5e9aSPoul-Henning Kamp case 'l': 13677c43773SEdward Tomasz Napierala if (action != UNSET && action != LIST) 137dc604f0cSEdward Tomasz Napierala errx(1, "-l is mutually exclusive " 138dc604f0cSEdward Tomasz Napierala "with -a, -r, and -d"); 139174b5e9aSPoul-Henning Kamp action = LIST; 14077c43773SEdward Tomasz Napierala mdio.md_options |= MD_AUTOUNIT; 141174b5e9aSPoul-Henning Kamp break; 142f79c46d3SRobert Watson case 'n': 143f79c46d3SRobert Watson nflag = 1; 144f79c46d3SRobert Watson break; 145c2ef0b73SPoul-Henning Kamp case 't': 14677c43773SEdward Tomasz Napierala if (tflag != NULL) 14777c43773SEdward Tomasz Napierala errx(1, "-t can be passed only once"); 14877c43773SEdward Tomasz Napierala tflag = optarg; 149c2ef0b73SPoul-Henning Kamp if (!strcmp(optarg, "malloc")) { 150c2ef0b73SPoul-Henning Kamp mdio.md_type = MD_MALLOC; 15177c43773SEdward Tomasz Napierala mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS; 152c2ef0b73SPoul-Henning Kamp } else if (!strcmp(optarg, "vnode")) { 153c2ef0b73SPoul-Henning Kamp mdio.md_type = MD_VNODE; 15477c43773SEdward Tomasz Napierala mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 155c2ef0b73SPoul-Henning Kamp } else if (!strcmp(optarg, "swap")) { 156c2ef0b73SPoul-Henning Kamp mdio.md_type = MD_SWAP; 15777c43773SEdward Tomasz Napierala mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 1580efd9bfdSEdward Tomasz Napierala } else if (!strcmp(optarg, "null")) { 1590efd9bfdSEdward Tomasz Napierala mdio.md_type = MD_NULL; 1600efd9bfdSEdward Tomasz Napierala mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 16177c43773SEdward Tomasz Napierala } else 16277c43773SEdward Tomasz Napierala errx(1, "unknown type: %s", optarg); 16370d586c0SPoul-Henning Kamp break; 16470d586c0SPoul-Henning Kamp case 'f': 16577c43773SEdward Tomasz Napierala if (fflag != NULL) 16677c43773SEdward Tomasz Napierala errx(1, "-f can be passed only once"); 167cab9f71fSXin LI fflag = realpath(optarg, NULL); 168cab9f71fSXin LI if (fflag == NULL) 169cab9f71fSXin LI err(1, "realpath"); 17070d586c0SPoul-Henning Kamp break; 17170d586c0SPoul-Henning Kamp case 'o': 1727a6b2b64SPoul-Henning Kamp if (!strcmp(optarg, "async")) 1737a6b2b64SPoul-Henning Kamp mdio.md_options |= MD_ASYNC; 1747a6b2b64SPoul-Henning Kamp else if (!strcmp(optarg, "noasync")) 1757a6b2b64SPoul-Henning Kamp mdio.md_options &= ~MD_ASYNC; 1767a6b2b64SPoul-Henning Kamp else if (!strcmp(optarg, "cluster")) 17770d586c0SPoul-Henning Kamp mdio.md_options |= MD_CLUSTER; 17870d586c0SPoul-Henning Kamp else if (!strcmp(optarg, "nocluster")) 17970d586c0SPoul-Henning Kamp mdio.md_options &= ~MD_CLUSTER; 180c2ef0b73SPoul-Henning Kamp else if (!strcmp(optarg, "compress")) 181c2ef0b73SPoul-Henning Kamp mdio.md_options |= MD_COMPRESS; 182c2ef0b73SPoul-Henning Kamp else if (!strcmp(optarg, "nocompress")) 183c2ef0b73SPoul-Henning Kamp mdio.md_options &= ~MD_COMPRESS; 18426a0ee75SDima Dorfman else if (!strcmp(optarg, "force")) 18526a0ee75SDima Dorfman mdio.md_options |= MD_FORCE; 18626a0ee75SDima Dorfman else if (!strcmp(optarg, "noforce")) 18726a0ee75SDima Dorfman mdio.md_options &= ~MD_FORCE; 188d31ba625SJohn-Mark Gurney else if (!strcmp(optarg, "readonly")) 189d31ba625SJohn-Mark Gurney mdio.md_options |= MD_READONLY; 190d31ba625SJohn-Mark Gurney else if (!strcmp(optarg, "noreadonly")) 191d31ba625SJohn-Mark Gurney mdio.md_options &= ~MD_READONLY; 19270d586c0SPoul-Henning Kamp else if (!strcmp(optarg, "reserve")) 19370d586c0SPoul-Henning Kamp mdio.md_options |= MD_RESERVE; 19470d586c0SPoul-Henning Kamp else if (!strcmp(optarg, "noreserve")) 19570d586c0SPoul-Henning Kamp mdio.md_options &= ~MD_RESERVE; 196*9a81ba0fSStephen J. Kiernan else if (!strcmp(optarg, "verify")) 197*9a81ba0fSStephen J. Kiernan mdio.md_options |= MD_VERIFY; 198*9a81ba0fSStephen J. Kiernan else if (!strcmp(optarg, "noverify")) 199*9a81ba0fSStephen J. Kiernan mdio.md_options &= ~MD_VERIFY; 20070d586c0SPoul-Henning Kamp else 20177c43773SEdward Tomasz Napierala errx(1, "unknown option: %s", optarg); 20270d586c0SPoul-Henning Kamp break; 203ebe789d6SPoul-Henning Kamp case 'S': 204b830359bSPawel Jakub Dawidek mdio.md_sectorsize = strtoul(optarg, &p, 0); 205ebe789d6SPoul-Henning Kamp break; 20670d586c0SPoul-Henning Kamp case 's': 207dc604f0cSEdward Tomasz Napierala if (sflag != NULL) 208dc604f0cSEdward Tomasz Napierala errx(1, "-s can be passed only once"); 209dc604f0cSEdward Tomasz Napierala sflag = optarg; 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; 224d5b0fb47SEdward Tomasz Napierala } else if (*p == 'p' || *p == 'P') { 225d5b0fb47SEdward Tomasz Napierala mdio.md_mediasize <<= 30; 226d5b0fb47SEdward Tomasz Napierala mdio.md_mediasize <<= 20; 227b830359bSPawel Jakub Dawidek } else 22877c43773SEdward Tomasz Napierala errx(1, "unknown suffix on -s argument"); 22970d586c0SPoul-Henning Kamp break; 23070d586c0SPoul-Henning Kamp case 'u': 231db3fdd7fSHiroki Sato if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 232db3fdd7fSHiroki Sato optarg += sizeof(_PATH_DEV) - 1; 233ea3d97aeSWojciech A. Koszek if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1)) 234ea3d97aeSWojciech A. Koszek optarg += sizeof(MD_NAME) - 1; 23577c43773SEdward Tomasz Napierala uflag = optarg; 23670d586c0SPoul-Henning Kamp break; 237c94b8307SDmitry Morozovsky case 'v': 238c94b8307SDmitry Morozovsky vflag = OPT_VERBOSE; 239c94b8307SDmitry Morozovsky break; 2404e8bfe14SPoul-Henning Kamp case 'x': 2414e8bfe14SPoul-Henning Kamp mdio.md_fwsectors = strtoul(optarg, &p, 0); 2424e8bfe14SPoul-Henning Kamp break; 2434e8bfe14SPoul-Henning Kamp case 'y': 2444e8bfe14SPoul-Henning Kamp mdio.md_fwheads = strtoul(optarg, &p, 0); 2454e8bfe14SPoul-Henning Kamp break; 24670d586c0SPoul-Henning Kamp default: 247c2ef0b73SPoul-Henning Kamp usage(); 24870d586c0SPoul-Henning Kamp } 24970d586c0SPoul-Henning Kamp } 2506c49c6cfSDag-Erling Smørgrav 2516c49c6cfSDag-Erling Smørgrav argc -= optind; 2526c49c6cfSDag-Erling Smørgrav argv += optind; 25377c43773SEdward Tomasz Napierala 25477c43773SEdward Tomasz Napierala if (action == UNSET) 25577c43773SEdward Tomasz Napierala action = ATTACH; 25677c43773SEdward Tomasz Napierala 25777c43773SEdward Tomasz Napierala if (action == ATTACH) { 25877c43773SEdward Tomasz Napierala if (tflag == NULL) { 25977c43773SEdward Tomasz Napierala /* 26077c43773SEdward Tomasz Napierala * Try to infer the type based on other arguments. 26177c43773SEdward Tomasz Napierala */ 26277c43773SEdward Tomasz Napierala if (fflag != NULL || argc > 0) { 26377c43773SEdward Tomasz Napierala /* Imply ``-t vnode'' */ 26477c43773SEdward Tomasz Napierala mdio.md_type = MD_VNODE; 26577c43773SEdward Tomasz Napierala mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | 26677c43773SEdward Tomasz Napierala MD_COMPRESS; 267dc604f0cSEdward Tomasz Napierala } else if (sflag != NULL) { 26877c43773SEdward Tomasz Napierala /* Imply ``-t swap'' */ 26977c43773SEdward Tomasz Napierala mdio.md_type = MD_SWAP; 27077c43773SEdward Tomasz Napierala mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | 27177c43773SEdward Tomasz Napierala MD_COMPRESS; 27277c43773SEdward Tomasz Napierala } else 27377c43773SEdward Tomasz Napierala errx(1, "unable to determine type"); 27477c43773SEdward Tomasz Napierala } 27577c43773SEdward Tomasz Napierala 27677c43773SEdward Tomasz Napierala if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE) 27777c43773SEdward Tomasz Napierala errx(1, "only -t vnode can be used with file name"); 27877c43773SEdward Tomasz Napierala 27977c43773SEdward Tomasz Napierala if (mdio.md_type == MD_VNODE) { 28077c43773SEdward Tomasz Napierala if (fflag != NULL) { 28177c43773SEdward Tomasz Napierala if (argc != 0) 28277c43773SEdward Tomasz Napierala usage(); 28377c43773SEdward Tomasz Napierala md_set_file(fflag); 28477c43773SEdward Tomasz Napierala } else { 2856c49c6cfSDag-Erling Smørgrav if (argc != 1) 2866c49c6cfSDag-Erling Smørgrav usage(); 2876c49c6cfSDag-Erling Smørgrav md_set_file(*argv); 2886c49c6cfSDag-Erling Smørgrav } 2896c49c6cfSDag-Erling Smørgrav 29077c43773SEdward Tomasz Napierala if ((mdio.md_options & MD_READONLY) == 0 && 29177c43773SEdward Tomasz Napierala access(mdio.md_file, W_OK) < 0 && 29277c43773SEdward Tomasz Napierala (errno == EACCES || errno == EPERM || 29377c43773SEdward Tomasz Napierala errno == EROFS)) { 29477c43773SEdward Tomasz Napierala warnx("WARNING: opening backing store: %s " 29577c43773SEdward Tomasz Napierala "readonly", mdio.md_file); 29677c43773SEdward Tomasz Napierala mdio.md_options |= MD_READONLY; 29777c43773SEdward Tomasz Napierala } 29877c43773SEdward Tomasz Napierala } 29977c43773SEdward Tomasz Napierala 3000efd9bfdSEdward Tomasz Napierala if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP || 3010efd9bfdSEdward Tomasz Napierala mdio.md_type == MD_NULL) && sflag == NULL) 3020efd9bfdSEdward Tomasz Napierala errx(1, "must specify -s for -t malloc, -t swap, " 3030efd9bfdSEdward Tomasz Napierala "or -t null"); 30477c43773SEdward Tomasz Napierala if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0') 30577c43773SEdward Tomasz Napierala errx(1, "must specify -f for -t vnode"); 30677c43773SEdward Tomasz Napierala } else { 30777c43773SEdward Tomasz Napierala if (mdio.md_sectorsize != 0) 30877c43773SEdward Tomasz Napierala errx(1, "-S can only be used with -a"); 309dc604f0cSEdward Tomasz Napierala if (action != RESIZE && sflag != NULL) 310dc604f0cSEdward Tomasz Napierala errx(1, "-s can only be used with -a and -r"); 31177c43773SEdward Tomasz Napierala if (mdio.md_fwsectors != 0) 31277c43773SEdward Tomasz Napierala errx(1, "-x can only be used with -a"); 31377c43773SEdward Tomasz Napierala if (mdio.md_fwheads != 0) 31477c43773SEdward Tomasz Napierala errx(1, "-y can only be used with -a"); 315b78ae23fSHiroki Sato if (fflag != NULL && action != LIST) 316b78ae23fSHiroki Sato errx(1, "-f can only be used with -a and -l"); 31777c43773SEdward Tomasz Napierala if (tflag != NULL) 31877c43773SEdward Tomasz Napierala errx(1, "-t can only be used with -a"); 31977c43773SEdward Tomasz Napierala if (argc > 0) 32077c43773SEdward Tomasz Napierala errx(1, "file can only be used with -a"); 321dc604f0cSEdward Tomasz Napierala if ((action != DETACH && action != RESIZE) && 322dc604f0cSEdward Tomasz Napierala (mdio.md_options & ~MD_AUTOUNIT) != 0) 323dc604f0cSEdward Tomasz Napierala errx(1, "-o can only be used with -a, -d, and -r"); 32477c43773SEdward Tomasz Napierala if (action == DETACH && 32577c43773SEdward Tomasz Napierala (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0) 32677c43773SEdward Tomasz Napierala errx(1, "only -o [no]force can be used with -d"); 327dc604f0cSEdward Tomasz Napierala if (action == RESIZE && 328dc604f0cSEdward Tomasz Napierala (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0) 329dc604f0cSEdward Tomasz Napierala errx(1, "only -o [no]force and -o [no]reserve can be used with -r"); 33077c43773SEdward Tomasz Napierala } 33177c43773SEdward Tomasz Napierala 332dc604f0cSEdward Tomasz Napierala if (action == RESIZE && sflag == NULL) 333dc604f0cSEdward Tomasz Napierala errx(1, "must specify -s for -r"); 334dc604f0cSEdward Tomasz Napierala 33577c43773SEdward Tomasz Napierala if (action != LIST && vflag == OPT_VERBOSE) 33677c43773SEdward Tomasz Napierala errx(1, "-v can only be used with -l"); 33777c43773SEdward Tomasz Napierala 33877c43773SEdward Tomasz Napierala if (uflag != NULL) { 33977c43773SEdward Tomasz Napierala mdio.md_unit = strtoul(uflag, &p, 0); 34077c43773SEdward Tomasz Napierala if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0') 34177c43773SEdward Tomasz Napierala errx(1, "bad unit: %s", uflag); 34277c43773SEdward Tomasz Napierala mdio.md_options &= ~MD_AUTOUNIT; 34377c43773SEdward Tomasz Napierala } 34477c43773SEdward Tomasz Napierala 34553d745bcSDima Dorfman mdio.md_version = MDIOVERSION; 34670d586c0SPoul-Henning Kamp 347a921cb31SPawel Jakub Dawidek if (!kld_isloaded("g_md") && kld_load("geom_md") == -1) 348a921cb31SPawel Jakub Dawidek err(1, "failed to load geom_md module"); 349a921cb31SPawel Jakub Dawidek 350b78ae23fSHiroki Sato fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0); 35170d586c0SPoul-Henning Kamp if (fd < 0) 352b78ae23fSHiroki Sato err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME); 35377c43773SEdward Tomasz Napierala 35477c43773SEdward Tomasz Napierala if (action == ATTACH) { 35570d586c0SPoul-Henning Kamp i = ioctl(fd, MDIOCATTACH, &mdio); 356174b5e9aSPoul-Henning Kamp if (i < 0) 357b78ae23fSHiroki Sato err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); 35883da2a90SPoul-Henning Kamp if (mdio.md_options & MD_AUTOUNIT) 359f79c46d3SRobert Watson printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit); 36083da2a90SPoul-Henning Kamp } else if (action == DETACH) { 3618f8def9eSPoul-Henning Kamp if (mdio.md_options & MD_AUTOUNIT) 36277c43773SEdward Tomasz Napierala errx(1, "-d requires -u"); 363c2ef0b73SPoul-Henning Kamp i = ioctl(fd, MDIOCDETACH, &mdio); 36470d586c0SPoul-Henning Kamp if (i < 0) 365b78ae23fSHiroki Sato err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); 366dc604f0cSEdward Tomasz Napierala } else if (action == RESIZE) { 367dc604f0cSEdward Tomasz Napierala if (mdio.md_options & MD_AUTOUNIT) 368dc604f0cSEdward Tomasz Napierala errx(1, "-r requires -u"); 369dc604f0cSEdward Tomasz Napierala i = ioctl(fd, MDIOCRESIZE, &mdio); 370dc604f0cSEdward Tomasz Napierala if (i < 0) 371b78ae23fSHiroki Sato err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); 37277c43773SEdward Tomasz Napierala } else if (action == LIST) { 37377c43773SEdward Tomasz Napierala if (mdio.md_options & MD_AUTOUNIT) { 37477c43773SEdward Tomasz Napierala /* 37577c43773SEdward Tomasz Napierala * Listing all devices. This is why we pass NULL 37677c43773SEdward Tomasz Napierala * together with OPT_LIST. 37777c43773SEdward Tomasz Napierala */ 378dc017e34SHiroki Sato return (md_list(NULL, OPT_LIST | vflag, fflag)); 37977c43773SEdward Tomasz Napierala } else 380b78ae23fSHiroki Sato return (md_query(uflag, vflag, fflag)); 38183da2a90SPoul-Henning Kamp } else 38283da2a90SPoul-Henning Kamp usage(); 383174b5e9aSPoul-Henning Kamp close(fd); 384174b5e9aSPoul-Henning Kamp return (0); 385174b5e9aSPoul-Henning Kamp } 386174b5e9aSPoul-Henning Kamp 3876c49c6cfSDag-Erling Smørgrav static void 3886c49c6cfSDag-Erling Smørgrav md_set_file(const char *fn) 3896c49c6cfSDag-Erling Smørgrav { 3906c49c6cfSDag-Erling Smørgrav struct stat sb; 3916c49c6cfSDag-Erling Smørgrav int fd; 3926c49c6cfSDag-Erling Smørgrav 3936c49c6cfSDag-Erling Smørgrav if (realpath(fn, mdio.md_file) == NULL) 3946c49c6cfSDag-Erling Smørgrav err(1, "could not find full path for %s", fn); 3956c49c6cfSDag-Erling Smørgrav fd = open(mdio.md_file, O_RDONLY); 3966c49c6cfSDag-Erling Smørgrav if (fd < 0) 3976c49c6cfSDag-Erling Smørgrav err(1, "could not open %s", fn); 3986c49c6cfSDag-Erling Smørgrav if (fstat(fd, &sb) == -1) 3996c49c6cfSDag-Erling Smørgrav err(1, "could not stat %s", fn); 4006c49c6cfSDag-Erling Smørgrav if (!S_ISREG(sb.st_mode)) 4016c49c6cfSDag-Erling Smørgrav errx(1, "%s is not a regular file", fn); 4026c49c6cfSDag-Erling Smørgrav if (mdio.md_mediasize == 0) 4036c49c6cfSDag-Erling Smørgrav mdio.md_mediasize = sb.st_size; 4046c49c6cfSDag-Erling Smørgrav close(fd); 4056c49c6cfSDag-Erling Smørgrav } 4066c49c6cfSDag-Erling Smørgrav 407c27a8954SWojciech A. Koszek /* 408c27a8954SWojciech A. Koszek * Lists md(4) disks. Is used also as a query routine, since it handles XML 409c27a8954SWojciech A. Koszek * interface. 'units' can be NULL for listing memory disks. It might be 410c27a8954SWojciech A. Koszek * coma-separated string containing md(4) disk names. 'opt' distinguished 411c27a8954SWojciech A. Koszek * between list and query mode. 412c27a8954SWojciech A. Koszek */ 4137e06d7bcSDima Dorfman static int 414b78ae23fSHiroki Sato md_list(const char *units, int opt, const char *fflag) 4157e06d7bcSDima Dorfman { 416c27a8954SWojciech A. Koszek struct gmesh gm; 417c27a8954SWojciech A. Koszek struct gprovider *pp; 418c27a8954SWojciech A. Koszek struct gconf *gc; 419c27a8954SWojciech A. Koszek struct gident *gid; 420c3345c66SJason Evans struct devstat *gsp; 421c27a8954SWojciech A. Koszek struct ggeom *gg; 422c27a8954SWojciech A. Koszek struct gclass *gcl; 423c27a8954SWojciech A. Koszek void *sq; 424dc017e34SHiroki Sato int retcode, ffound, ufound; 425c27a8954SWojciech A. Koszek char *type, *file, *length; 4267e06d7bcSDima Dorfman 427c27a8954SWojciech A. Koszek type = file = length = NULL; 428174b5e9aSPoul-Henning Kamp 429c27a8954SWojciech A. Koszek retcode = geom_gettree(&gm); 430c27a8954SWojciech A. Koszek if (retcode != 0) 431c27a8954SWojciech A. Koszek return (-1); 432c27a8954SWojciech A. Koszek retcode = geom_stats_open(); 433c27a8954SWojciech A. Koszek if (retcode != 0) 434c27a8954SWojciech A. Koszek return (-1); 435c27a8954SWojciech A. Koszek sq = geom_stats_snapshot_get(); 436c27a8954SWojciech A. Koszek if (sq == NULL) 437c27a8954SWojciech A. Koszek return (-1); 438c27a8954SWojciech A. Koszek 439dc017e34SHiroki Sato ffound = ufound = 0; 440c27a8954SWojciech A. Koszek while ((gsp = geom_stats_snapshot_next(sq)) != NULL) { 441c27a8954SWojciech A. Koszek gid = geom_lookupid(&gm, gsp->id); 442c27a8954SWojciech A. Koszek if (gid == NULL) 443c27a8954SWojciech A. Koszek continue; 444c27a8954SWojciech A. Koszek if (gid->lg_what == ISPROVIDER) { 445c27a8954SWojciech A. Koszek pp = gid->lg_ptr; 446c27a8954SWojciech A. Koszek gg = pp->lg_geom; 447c27a8954SWojciech A. Koszek gcl = gg->lg_class; 448c27a8954SWojciech A. Koszek if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0) 449c27a8954SWojciech A. Koszek continue; 450c27a8954SWojciech A. Koszek if ((opt & OPT_UNIT) && (units != NULL)) { 451c27a8954SWojciech A. Koszek retcode = md_find(units, pp->lg_name); 452c27a8954SWojciech A. Koszek if (retcode != 1) 453c27a8954SWojciech A. Koszek continue; 45469fcb537SFlorent Thoumie else 455dc017e34SHiroki Sato ufound = 1; 456174b5e9aSPoul-Henning Kamp } 457c27a8954SWojciech A. Koszek gc = &pp->lg_config; 458b78ae23fSHiroki Sato type = geom_config_get(gc, "type"); 45930545786SEdward Tomasz Napierala if (type != NULL && (strcmp(type, "vnode") == 0 || 46030545786SEdward Tomasz Napierala strcmp(type, "preload") == 0)) { 461b78ae23fSHiroki Sato file = geom_config_get(gc, "file"); 462b78ae23fSHiroki Sato if (fflag != NULL && 463b78ae23fSHiroki Sato strcmp(fflag, file) != 0) 464b78ae23fSHiroki Sato continue; 465dc017e34SHiroki Sato else 466dc017e34SHiroki Sato ffound = 1; 467fa3483e6SXin LI } else if (fflag != NULL) 468fa3483e6SXin LI continue; 469b78ae23fSHiroki Sato if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0) 470c3e257a9SRebecca Cran printf("%s", pp->lg_name + 2); 471c3e257a9SRebecca Cran else 472c3e257a9SRebecca Cran printf("%s", pp->lg_name); 473c3e257a9SRebecca Cran 474b78ae23fSHiroki Sato if (opt & OPT_VERBOSE || 475b78ae23fSHiroki Sato ((opt & OPT_UNIT) && fflag == NULL)) { 476c27a8954SWojciech A. Koszek length = geom_config_get(gc, "length"); 477c27a8954SWojciech A. Koszek printf("\t%s\t", type); 478a4ca6a49SJaakko Heinonen if (length != NULL) 479c27a8954SWojciech A. Koszek md_prthumanval(length); 480c27a8954SWojciech A. Koszek if (file != NULL) { 481c27a8954SWojciech A. Koszek printf("\t%s", file); 482c27a8954SWojciech A. Koszek file = NULL; 483c27a8954SWojciech A. Koszek } 484c27a8954SWojciech A. Koszek } 485c27a8954SWojciech A. Koszek opt |= OPT_DONE; 486c94b8307SDmitry Morozovsky if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE)) 487c27a8954SWojciech A. Koszek printf(" "); 488c27a8954SWojciech A. Koszek else 489e39eff98SPoul-Henning Kamp printf("\n"); 490c27a8954SWojciech A. Koszek } 491c27a8954SWojciech A. Koszek } 492c94b8307SDmitry Morozovsky if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE)) 493c27a8954SWojciech A. Koszek printf("\n"); 494c27a8954SWojciech A. Koszek /* XXX: Check if it's enough to clean everything. */ 495c27a8954SWojciech A. Koszek geom_stats_snapshot_free(sq); 4964103b0b0SHiroki Sato if (opt & OPT_UNIT) { 4974103b0b0SHiroki Sato if (((fflag == NULL) && ufound) || 4984103b0b0SHiroki Sato ((fflag == NULL) && (units != NULL) && ufound) || 4994103b0b0SHiroki Sato ((fflag != NULL) && ffound) || 5004103b0b0SHiroki Sato ((fflag != NULL) && (units != NULL) && ufound && ffound)) 50169fcb537SFlorent Thoumie return (0); 5024103b0b0SHiroki Sato } else if (opt & OPT_LIST) { 5034103b0b0SHiroki Sato if ((fflag == NULL) || 5044103b0b0SHiroki Sato ((fflag != NULL) && ffound)) 5054103b0b0SHiroki Sato return (0); 5064103b0b0SHiroki Sato } 507c27a8954SWojciech A. Koszek return (-1); 508c27a8954SWojciech A. Koszek } 509c27a8954SWojciech A. Koszek 510c27a8954SWojciech A. Koszek /* 511c27a8954SWojciech A. Koszek * Returns value of 'name' from gconfig structure. 512c27a8954SWojciech A. Koszek */ 513c27a8954SWojciech A. Koszek static char * 514a4ca6a49SJaakko Heinonen geom_config_get(struct gconf *g, const char *name) 515c27a8954SWojciech A. Koszek { 516c27a8954SWojciech A. Koszek struct gconfig *gce; 517c27a8954SWojciech A. Koszek 518c27a8954SWojciech A. Koszek LIST_FOREACH(gce, g, lg_config) { 519c27a8954SWojciech A. Koszek if (strcmp(gce->lg_name, name) == 0) 520c27a8954SWojciech A. Koszek return (gce->lg_val); 521c27a8954SWojciech A. Koszek } 522c27a8954SWojciech A. Koszek return (NULL); 523c27a8954SWojciech A. Koszek } 524c27a8954SWojciech A. Koszek 525c27a8954SWojciech A. Koszek /* 526c27a8954SWojciech A. Koszek * List is comma separated list of MD disks. name is a 527c27a8954SWojciech A. Koszek * device name we look for. Returns 1 if found and 0 528c27a8954SWojciech A. Koszek * otherwise. 529c27a8954SWojciech A. Koszek */ 530c27a8954SWojciech A. Koszek static int 531b78ae23fSHiroki Sato md_find(const char *list, const char *name) 532c27a8954SWojciech A. Koszek { 533c27a8954SWojciech A. Koszek int ret; 534db3fdd7fSHiroki Sato char num[PATH_MAX]; 535c27a8954SWojciech A. Koszek char *ptr, *p, *u; 536c27a8954SWojciech A. Koszek 537c27a8954SWojciech A. Koszek ret = 0; 538c27a8954SWojciech A. Koszek ptr = strdup(list); 539c27a8954SWojciech A. Koszek if (ptr == NULL) 540c27a8954SWojciech A. Koszek return (-1); 541c27a8954SWojciech A. Koszek for (p = ptr; (u = strsep(&p, ",")) != NULL;) { 542db3fdd7fSHiroki Sato if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 543db3fdd7fSHiroki Sato u += sizeof(_PATH_DEV) - 1; 544c27a8954SWojciech A. Koszek /* Just in case user specified number instead of full name */ 545db3fdd7fSHiroki Sato snprintf(num, sizeof(num), "%s%s", MD_NAME, u); 546c27a8954SWojciech A. Koszek if (strcmp(u, name) == 0 || strcmp(num, name) == 0) { 547c27a8954SWojciech A. Koszek ret = 1; 548c27a8954SWojciech A. Koszek break; 549c27a8954SWojciech A. Koszek } 550c27a8954SWojciech A. Koszek } 551c27a8954SWojciech A. Koszek free(ptr); 552c27a8954SWojciech A. Koszek return (ret); 553174b5e9aSPoul-Henning Kamp } 554174b5e9aSPoul-Henning Kamp 555b830359bSPawel Jakub Dawidek static void 556c27a8954SWojciech A. Koszek md_prthumanval(char *length) 557b830359bSPawel Jakub Dawidek { 558b830359bSPawel Jakub Dawidek char buf[6]; 5590e3ebc63SJaakko Heinonen uintmax_t bytes; 560c27a8954SWojciech A. Koszek char *endptr; 561b830359bSPawel Jakub Dawidek 5620e3ebc63SJaakko Heinonen errno = 0; 5630e3ebc63SJaakko Heinonen bytes = strtoumax(length, &endptr, 10); 5640e3ebc63SJaakko Heinonen if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX) 565c27a8954SWojciech A. Koszek return; 5660e3ebc63SJaakko Heinonen humanize_number(buf, sizeof(buf), (int64_t)bytes, "", 5670e3ebc63SJaakko Heinonen HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 568b830359bSPawel Jakub Dawidek (void)printf("%6s", buf); 569b830359bSPawel Jakub Dawidek } 570b830359bSPawel Jakub Dawidek 571cff3dc08SEdward Tomasz Napierala static int 572b78ae23fSHiroki Sato md_query(const char *name, const int opt, const char *fflag) 573174b5e9aSPoul-Henning Kamp { 57477c43773SEdward Tomasz Napierala 575b78ae23fSHiroki Sato return (md_list(name, opt | OPT_UNIT, fflag)); 57670d586c0SPoul-Henning Kamp } 577