]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mdconfig/mdconfig.c
This commit was generated by cvs2svn to compensate for changes in r171364,
[FreeBSD/FreeBSD.git] / sbin / mdconfig / mdconfig.c
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                         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;
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         while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
342                 gid = geom_lookupid(&gm, gsp->id);
343                 if (gid == NULL)
344                         continue;
345                 if (gid->lg_what == ISPROVIDER) {
346                         pp = gid->lg_ptr;
347                         gg = pp->lg_geom;
348                         gcl = gg->lg_class;
349                         if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0)
350                                 continue;
351                         if ((opt & OPT_UNIT) && (units != NULL)) {
352                                 retcode = md_find(units, pp->lg_name);
353                                 if (retcode != 1)
354                                         continue;
355                         }
356                         gc = &pp->lg_config;
357                         printf("%s", pp->lg_name);
358                         if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
359                                 type = geom_config_get(gc, "type");
360                                 if (strcmp(type, "vnode") == 0)
361                                         file = geom_config_get(gc, "file");
362                                 length = geom_config_get(gc, "length");
363                                 if (length == NULL)
364                                         length = "<a>";
365                                 printf("\t%s\t", type);
366                                 md_prthumanval(length);
367                                 if (file != NULL) {
368                                         printf("\t%s", file);
369                                         file = NULL;
370                                 }
371                         }
372                         opt |= OPT_DONE;
373                         if (opt & OPT_LIST)
374                                 printf(" ");
375                         else
376                                 printf("\n");
377                 }
378         }
379         if ((opt & OPT_LIST) && (opt & OPT_DONE))
380                 printf("\n");
381         /* XXX: Check if it's enough to clean everything. */
382         geom_stats_snapshot_free(sq);
383         return (-1);
384 }
385
386 /*
387  * Returns value of 'name' from gconfig structure.
388  */
389 static char *
390 geom_config_get(struct gconf *g, char *name)
391 {
392         struct gconfig *gce;
393
394         LIST_FOREACH(gce, g, lg_config) {
395                 if (strcmp(gce->lg_name, name) == 0)
396                         return (gce->lg_val);
397         }
398         return (NULL);
399 }
400
401 /*
402  * List is comma separated list of MD disks. name is a
403  * device name we look for.  Returns 1 if found and 0
404  * otherwise.
405  */
406 static int
407 md_find(char *list, const char *name)
408 {
409         int ret;
410         char num[16];
411         char *ptr, *p, *u;
412
413         ret = 0;
414         ptr = strdup(list);
415         if (ptr == NULL)
416                 return (-1);
417         for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
418                 if (strncmp(u, "/dev/", 5) == 0)
419                         u += 5;
420                 /* Just in case user specified number instead of full name */
421                 snprintf(num, sizeof(num), "md%s", u);
422                 if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
423                         ret = 1;
424                         break;
425                 }
426         }
427         free(ptr);
428         return (ret);
429 }
430
431 static void
432 md_prthumanval(char *length)
433 {
434         char buf[6];
435         uint64_t bytes;
436         char *endptr;
437
438         bytes = strtoul(length, &endptr, 10);
439         if (bytes == (unsigned)ULONG_MAX || *endptr != '\0')
440                 return;
441         humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
442             bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
443         (void)printf("%6s", buf);
444 }
445
446 int
447 md_query(char *name)
448 {
449         return (md_list(name, OPT_UNIT));
450 }