mdconfig.c revision 70541
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: head/sbin/mdconfig/mdconfig.c 70541 2000-12-31 13:03:42Z phk $ 10 * 11 */ 12 13#include <stdio.h> 14#include <stdlib.h> 15#include <fcntl.h> 16#include <unistd.h> 17#include <string.h> 18#include <err.h> 19#include <sys/ioctl.h> 20#include <sys/param.h> 21#include <sys/mdioctl.h> 22 23struct md_ioctl mdio; 24 25enum {UNSET, ATTACH, DETACH} action = UNSET; 26 27void 28usage() 29{ 30 fprintf(stderr, "Usage:\n"); 31 fprintf(stderr, "\tmdconfig -a -t type [-o [no]option]... [ -f file] [-s size] [-u unit]\n"); 32 fprintf(stderr, "\tmdconfig -d -u unit\n"); 33 fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n"); 34 fprintf(stderr, "\t\toption = {cluster, compress, reserve, autounit}\n"); 35 fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%dk (kB), %%dm (MB) or %%dg (GB)\n"); 36 exit(1); 37} 38 39int 40main(int argc, char **argv) 41{ 42 int ch, fd, i; 43 char *p; 44 int cmdline = 0; 45 46 for (;;) { 47 ch = getopt(argc, argv, "adf:o:s:t:u:"); 48 if (ch == -1) 49 break; 50 switch (ch) { 51 case 'a': 52 if (cmdline != 0) 53 usage(); 54 action = ATTACH; 55 cmdline = 1; 56 break; 57 case 'd': 58 if (cmdline != 0) 59 usage(); 60 action = DETACH; 61 mdio.md_options = MD_AUTOUNIT; 62 cmdline = 3; 63 break; 64 case 't': 65 if (cmdline != 1) 66 usage(); 67 if (!strcmp(optarg, "malloc")) { 68 mdio.md_type = MD_MALLOC; 69 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS; 70 } else if (!strcmp(optarg, "preload")) { 71 mdio.md_type = MD_PRELOAD; 72 mdio.md_options = 0; 73 } else if (!strcmp(optarg, "vnode")) { 74 mdio.md_type = MD_VNODE; 75 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 76 } else if (!strcmp(optarg, "swap")) { 77 mdio.md_type = MD_SWAP; 78 mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; 79 } else { 80 usage(); 81 } 82 cmdline=2; 83 break; 84 case 'f': 85 if (cmdline != 2) 86 usage(); 87 strncpy(mdio.md_file, optarg, sizeof(mdio.md_file) - 1); 88 break; 89 case 'o': 90 if (cmdline != 2) 91 usage(); 92 if (!strcmp(optarg, "cluster")) 93 mdio.md_options |= MD_CLUSTER; 94 else if (!strcmp(optarg, "nocluster")) 95 mdio.md_options &= ~MD_CLUSTER; 96 else if (!strcmp(optarg, "compress")) 97 mdio.md_options |= MD_COMPRESS; 98 else if (!strcmp(optarg, "nocompress")) 99 mdio.md_options &= ~MD_COMPRESS; 100 else if (!strcmp(optarg, "reserve")) 101 mdio.md_options |= MD_RESERVE; 102 else if (!strcmp(optarg, "noreserve")) 103 mdio.md_options &= ~MD_RESERVE; 104 else if (!strcmp(optarg, "autounit")) 105 mdio.md_options |= MD_AUTOUNIT; 106 else if (!strcmp(optarg, "noautounit")) 107 mdio.md_options &= ~MD_AUTOUNIT; 108 else 109 errx(1, "Unknown option."); 110 break; 111 case 's': 112 if (cmdline != 2) 113 usage(); 114 mdio.md_size = strtoul(optarg, &p, 0); 115 if (p == NULL || *p == '\0') 116 ; 117 else if (*p == 'k' || *p == 'K') 118 mdio.md_size *= (1024 / DEV_BSIZE); 119 else if (*p == 'm' || *p == 'M') 120 mdio.md_size *= (1024 * 1024 / DEV_BSIZE); 121 else if (*p == 'g' || *p == 'G') 122 mdio.md_size *= (1024 * 1024 * 1024 / DEV_BSIZE); 123 else 124 errx(1, "Unknown suffix on -s argument"); 125 break; 126 case 'u': 127 if (cmdline != 2 && cmdline != 3) 128 usage(); 129 mdio.md_unit = strtoul(optarg, NULL, 0); 130 mdio.md_options &= ~MD_AUTOUNIT; 131 break; 132 default: 133 usage(); 134 } 135 } 136 137 fd = open("/dev/mdctl", O_RDWR, 0); 138 if (fd < 0) 139 err(1, "open(/dev/mdctl)"); 140 if (action == ATTACH) { 141 i = ioctl(fd, MDIOCATTACH, &mdio); 142 } else { 143 if (mdio.md_options & MD_AUTOUNIT) 144 usage(); 145 i = ioctl(fd, MDIOCDETACH, &mdio); 146 } 147 if (i < 0) 148 err(1, "ioctl(/dev/mdctl)"); 149 if (mdio.md_options & MD_AUTOUNIT) 150 printf("md%d\n", mdio.md_unit); 151 return (0); 152} 153 154